forked from krocki/mlc-llm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clang-format.sh
executable file
·63 lines (57 loc) · 1.62 KB
/
clang-format.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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
set -eo pipefail
set -x
: ${NUM_THREADS:=$(nproc)}
: ${WORKSPACE_CWD:=$(pwd)}
: ${GPU:="cpu"}
INPLACE_FORMAT=${INPLACE_FORMAT:=false}
LINT_ALL_FILES=true
REVISION=$(git rev-list --max-parents=0 HEAD)
while (($#)); do
case "$1" in
-i)
INPLACE_FORMAT=true
shift 1
;;
--rev)
LINT_ALL_FILES=false
REVISION=$2
shift 2
;;
*)
echo "Usage: clang-format.sh [-i] [--rev <commit>]"
echo ""
echo "Run clang-format on files that changed since <commit> or on all files in the repo"
echo "Examples:"
echo "- Compare last one commit: clang-format.sh --rev HEAD~1"
echo "- Compare against upstream/main: clang-format.sh --rev upstream/main"
echo "The -i will format files in-place instead of checking them."
exit 1
;;
esac
done
cleanup() {
if [ -f /tmp/$$.clang-format.txt ]; then
echo ""
echo "---------clang-format log----------"
cat /tmp/$$.clang-format.txt
fi
rm -rf /tmp/$$.clang-format.txt
}
trap cleanup 0
if [[ "$INPLACE_FORMAT" == "true" ]]; then
echo "Running inplace git-clang-format against $REVISION"
git-clang-format --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION"
exit 0
fi
if [[ "$LINT_ALL_FILES" == "true" ]]; then
echo "Running git-clang-format against all C++ files"
git-clang-format --diff --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION" 1>/tmp/$$.clang-format.txt
else
echo "Running git-clang-format against $REVISION"
git-clang-format --diff --extensions h,hh,hpp,c,cc,cpp,mm "$REVISION" 1>/tmp/$$.clang-format.txt
fi
if grep --quiet -E "diff" </tmp/$$.clang-format.txt; then
echo "clang-format lint error found. Consider running clang-format on these files to fix them."
exit 1
fi