Skip to content

Commit fc46fcd

Browse files
authored
fix(check-formatting.sh): use relative directory (#835)
The `bin/check-formatting.sh` script changed its directory to `bin` as the first step, consequently expecting paths of files to check to be relative to `bin`, when typically they would be given relative to the working directory. When not finding a file, the script would just print a warning, but still exit with error code 0, which is probably the reason why this went unnoticed for quite a while. This commit fixes the relative directory issue. Further, when a given path cannot be checked, we return a non-zero code now. Lastly, the script would count the number of files with formatting issues and return that number as error code. This has the issue that if the number of violating files is a multiple of 256, error code would falsly be returned.
1 parent 5041ca5 commit fc46fcd

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

bin/check-formatting.sh

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
set -e
23

34
print_help() {
45
echo "This script verifies if one or more files are formatted using the provided"
@@ -13,8 +14,6 @@ print_help() {
1314
echo " warnings are returned for each invalid path."
1415
}
1516

16-
cd "$(dirname "$0")"
17-
1817
# Get files list
1918
FILES=()
2019
while [[ $# -gt 0 ]]
@@ -31,40 +30,24 @@ do
3130
shift
3231
done
3332

34-
# Verifies a single file is formatted following .clang-format style
35-
run_check() {
36-
local FILE="$1"
37-
CHECK_RESULT=0
38-
# Get diff between current content and formatted content
39-
local OUTPUT=$(diff -u <(cat "$FILE") <(clang-format --style=file "$FILE"))
40-
# Shows diff output only if files are different
41-
if [[ $OUTPUT != "" ]]; then
42-
echo "$OUTPUT"
43-
CHECK_RESULT=1
44-
fi
45-
}
46-
47-
# Result of this script, incremented by one for each unformatted file
33+
# Result of this script
4834
RESULT=0
49-
# List of warnings
50-
WARNINGS=()
5135

5236
# Iterate files to run format check
5337
for INDEX in "${!FILES[@]}"; do
5438
FILE="${FILES[$INDEX]}"
5539
if [[ -f "$FILE" ]]; then
56-
run_check $FILE
57-
RESULT=$(( RESULT + CHECK_RESULT ))
40+
if ! clang-format --dry-run --Werror "$FILE"; then
41+
RESULT=1
42+
fi
5843
elif [[ -d "$FILE" ]]; then
59-
WARNINGS+=("Path is a directory: $FILE")
44+
echo "Path is a directory: $FILE"
45+
RESULT=1
6046
else
61-
WARNINGS+=("File not found: $FILE")
47+
echo "File not found: $FILE"
48+
RESULT=1
6249
fi
6350
done
6451

65-
for INDEX in "${!WARNINGS[@]}"; do
66-
echo "${WARNINGS[$INDEX]}"
67-
done
68-
6952
echo "Done"
7053
exit $RESULT

0 commit comments

Comments
 (0)