-
Notifications
You must be signed in to change notification settings - Fork 11
/
.clang-format-check.sh
executable file
·50 lines (42 loc) · 1.3 KB
/
.clang-format-check.sh
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
#! /bin/bash
test "$1" == "--autofix" && AUTOFIX=1 && shift
CLANG_FORMAT=${1:-clang-format}
echo -n "Running clang-format checks, version: "
${CLANG_FORMAT} --version
if [ 0 != $? ]; then
echo -e "\033[1;31mclang-format missing: ${CLANG_FORMAT}\033[0m"
exit 1
fi
if [ "$TRAVIS" != "true" ] ; then
# Not in a pull request, so compare against parent commit
base_commit="origin/dev"
echo "Checking against parent commit $(git rev-parse $base_commit)"
else
base_commit="$TRAVIS_COMMIT_RANGE"
echo "Checking against commit $base_commit"
fi
# To simplify CI debugging, tell what files are considered.
echo "--- Listing all changed files:"
git diff --name-only ${base_commit}
echo "---"
filesToCheck="$(git diff --name-only ${base_commit} | grep -e '.(\.C\|\.cpp\|\.cxx\|\.h)$' || true)"
for f in $filesToCheck; do
if test -n "$AUTOFIX"
then
echo "fixing $f, if needed."
$CLANG_FORMAT -i -style=file "$f"
else
echo " Checking: ${f}"
d=$(diff -u "$f" <($CLANG_FORMAT -style=file "$f") || true)
if ! [ -z "$d" ]; then
echo "$d"
fail=1
fi
fi
done
if [ "$fail" = 1 ]; then
echo -e "\033[1;31mYou must pass the clang-format checks before submitting a pull request.\033[0m"
exit 1
fi
echo -e "\033[1;32m\xE2\x9C\x93 passed clang-format checks\033[0m $1";
exit 0