-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck-format.sh
More file actions
executable file
路66 lines (55 loc) 路 1.41 KB
/
Copy pathcheck-format.sh
File metadata and controls
executable file
路66 lines (55 loc) 路 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
usage() {
cat <<EOF
Usage: ./check-format.sh [files...]
Without arguments, checks all tracked C/C++ source files for clang-format compliance.
With arguments, only the specified files are checked (must exist).
Exit code 0 if all files are properly formatted, non-zero otherwise.
EOF
}
if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
usage
exit 0
fi
if ! command -v clang-format >/dev/null 2>&1; then
echo "Error: clang-format not found in PATH" >&2
exit 1
fi
if [[ $# -gt 0 ]]; then
files=("$@")
else
if command -v mapfile >/dev/null 2>&1; then
mapfile -t files < <(git ls-files '*.c' '*.cpp' '*.h' '*.hpp')
else
files=()
while IFS= read -r line; do
files+=("$line")
done < <(git ls-files '*.c' '*.cpp' '*.h' '*.hpp')
fi
fi
if [[ ${#files[@]} -eq 0 ]]; then
echo "No files to check." >&2
exit 0
fi
fail=0
for file in "${files[@]}"; do
if [[ ! -f $file ]]; then
echo "Skip (not a file): $file" >&2
continue
fi
# Check if file is properly formatted by comparing original with formatted version
if ! diff -q "$file" <(clang-format "$file" --style=file) >/dev/null 2>&1; then
echo "Format check failed: $file" >&2
fail=1
else
echo "Format check passed: $file" >&2
fi
done
if [[ $fail -eq 0 ]]; then
echo "All files are properly formatted." >&2
else
echo "Some files are not properly formatted. Run ./format.sh to fix them." >&2
fi
exit $fail