-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcode-format.sh
executable file
·127 lines (117 loc) · 3.09 KB
/
code-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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
base='--cached'
target='master'
show_only=0
fail_on_diff=0
format_all=0
while [ "$#" -gt 0 ]; do
case $1 in
-h | --help)
echo "Usage: $0 [--fail-on-diff] [--show-only] [--format-all] [--base|-b <base(=--cached)>] [--target|-t <target(=origin/master)>]"
exit 0
;;
--fail-on-diff)
fail_on_diff=1
shift
;;
--show-only)
show_only=1
shift
;;
--format-all)
format_all=1
shift
;;
--base | -b)
base="$2"
shift
shift
;;
--target | -t)
target="$2"
shift
shift
;;
*)
echo "error: unrecognized option $1"
exit 1
;;
esac
done
# hack for code format
if [ -f "pre-commit" ]; then
cp pre-commit ./.git/hooks/pre-commit
fi
files_to_check=()
if [ "$format_all" -eq 1 ]; then
files_to_check=($(git ls-files --exclude-standard | grep -v "$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs -I {} echo -n '\|{}' | cut -c 3-)"))
else
files_to_check=($(git diff $base $target --name-only --diff-filter=ACMRT))
fi
files_to_check_cpp=$(printf "%s\n" "${files_to_check[@]}" | grep "\.\(c\|cpp\|cc\|h\|hpp\|cu\|cuh\)$")
files_to_check_py=$(printf "%s\n" "${files_to_check[@]}" | grep "\.\(py\)$")
files_to_check_pyi=$(printf "%s\n" "${files_to_check[@]}" | grep "\.\(pyi\)$")
if [ "$files_to_check_cpp" = "" ] && [ "$files_to_check_py" = "" ] && [ "$files_to_check_pyi" = "" ]; then
exit 0
fi
if ! clang-format -version >/dev/null; then
echo "error: clang-format not found, can be installed by: pip3 install clang-format"
exit 1
fi
if ! black --version >/dev/null; then
echo "error: black not found, can be installed by: pip3 install black"
exit 1
fi
if [ "$show_only" -eq 1 ]; then
# cpp files
for f in $files_to_check_cpp; do
if [ -L $f ]; then
continue
fi
result=$(diff <(git show :$f) <(git show :$f | clang-format -style=file --assume-filename=$f))
if [ "$result" != "" ]; then
echo "===== $f ====="
echo -e "$result"
has_diff=1
fi
done
# .py files
for f in $files_to_check_py; do
if [ -L $f ]; then
continue
fi
result=$(diff <(git show :$f) <(git show :$f | black -q -))
if [ "$result" != "" ]; then
echo "===== $f ====="
echo -e "$result"
has_diff=1
fi
done
# .pyi files
for f in $files_to_check_pyi; do
if [ -L $f ]; then
continue
fi
result=$(diff <(git show :$f) <(git show :$f | black --pyi -q -))
if [ "$result" != "" ]; then
echo "===== $f ====="
echo -e "$result"
has_diff=1
fi
done
else
echo "Formatting cpp files by clang-format..."
echo $files_to_check_cpp | xargs clang-format -i --style=file
if [[ ! -z $files_to_check_py ]]; then
echo "Formatting py files by black..."
echo $files_to_check_py | xargs black -q
fi
if [[ ! -z $files_to_check_pyi ]]; then
echo "Formatting pyi files by black..."
echo $files_to_check_pyi | xargs black --pyi -q
fi
fi
if [[ "$fail_on_diff" -eq 1 ]] && [[ "$has_diff" -eq 1 ]]; then
echo "code format check failed, please run the following command before commit: ./code-format.sh"
exit 1
fi