-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck-pre-commit
More file actions
executable file
·48 lines (39 loc) · 1.43 KB
/
check-pre-commit
File metadata and controls
executable file
·48 lines (39 loc) · 1.43 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
#!/bin/bash
# This script is designed for use in a GitHub Actions CI environment to verify that files changed in a
# pull request or push adhere to the project's coding standards. It checks for trailing whitespaces,
# ensures tabs are replaced by spaces, and validates C/C++ files with clang-format.
# If any issues are found, it will fail the CI job, preventing merges with style violations.
set -ex
set -o pipefail
if [[ "$GITHUB_BASE_REF" != "" ]]; then
# In a PR
git fetch origin $GITHUB_BASE_REF
base=$GITHUB_BASE_REF
else
base=main
git fetch origin main
fi
files=$(git diff --name-only origin/$base...HEAD | grep -E '.*\.(sh|s|S|asm|ld|txt|cmake|clang-format|c|h|hpp|cpp|cppm)$' || true)
exit_code=0
for file in $files; do
# trailing white space
diff -u <(cat $1) <(sed -E 's/[ '$'\t'']+$//g' $1) > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "::error file=$file::trailing whitespace detected"
exit_code=1
fi
# trailing white space
diff -u <(cat $1) <(sed -E 's/\t/ /g' $1) > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
echo "::error file=$file::tab character detected"
exit_code=1
fi
# clang-format for C/C++ source files
if [[ "$file" =~ \.(h|c|hpp|cpp|cppm)$ ]]; then
if ! clang-format --dry-run --Werror "$file"; then
echo "::error file=$file::clang-format style violation"
exit_code=1
fi
fi
done
exit $exit_code