summaryrefslogtreecommitdiff
path: root/scripts/format.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/format.sh')
-rwxr-xr-xscripts/format.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/scripts/format.sh b/scripts/format.sh
new file mode 100755
index 0000000..3b77776
--- /dev/null
+++ b/scripts/format.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+#
+# format.sh — Formats all source files in the project using clang-format.
+#
+# Usage:
+# ./scripts/format.sh # Format all C/C++ source files
+# ./scripts/format.sh --check # Check formatting without modifying files
+#
+
+set -euo pipefail
+
+# Project root (one level up from scripts/)
+ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+cd "$ROOT_DIR"
+
+# Find all C/C++ files (excluding build directories and third-party code)
+FILES=$(find src include -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) \
+ ! -path "*/build/*" ! -path "*/third_party/*")
+
+# Default clang-format style (fallback if no .clang-format exists)
+STYLE="file"
+if [ ! -f ".clang-format" ]; then
+ STYLE="llvm"
+fi
+
+# Command arguments
+CMD_ARGS=("-i" "--style=${STYLE}")
+if [[ "${1:-}" == "--check" ]]; then
+ CMD_ARGS=("--dry-run" "--Werror" "--style=${STYLE}")
+ echo "Checking code format..."
+else
+ echo "Formatting source files..."
+fi
+
+# Run clang-format on each file
+for file in $FILES; do
+ clang-format "${CMD_ARGS[@]}" "$file"
+done
+
+if [[ "${1:-}" == "--check" ]]; then
+ echo "Format check passed!"
+else
+ echo "Formatting complete."
+fi
+