-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathformat.sh
executable file
·95 lines (84 loc) · 1.84 KB
/
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
#!/bin/bash
# Formats the repository.
usage() {
echo "usage: $0 [OPTIONS]"
echo "Format the repository."
echo
echo " -h, --help Show this message."
echo " -i, --in-place Reformat in-place."
}
in_place=0
while [[ $# -gt 0 ]]; do
case $1 in
-h | --help)
usage
exit 0
;;
-i | --in-place)
in_place=1
;;
*)
echo "invalid option: $1" >&2
usage
exit 1
;;
esac
shift
done
# Find clang-format.
set -e
./bazel build --color=yes --curses=no @llvm//:clang-format
CLANG_FORMAT=$(./bazel cquery \
--color=yes \
--curses=no \
--output=starlark \
--starlark:expr='target.files_to_run.executable.path' \
@llvm//:clang-format)
echo
set +e
LICENSE_LINES=$(wc -l < ./LICENSE.inc)
function license() {
local f=$1
if [[ $in_place != 0 ]]; then
# For all C++ the first interesting line is a #directive.
local first=$(grep -oahnm 1 "^#" "$f" | grep -oP '\d+')
local rest=$(tail -n +$first "$f")
cat LICENSE.inc > $f
echo >> $f
echo "$rest" >> $f
else
head -n $LICENSE_LINES "$f" |
git --no-pager \
diff --color=always --no-index --exit-code \
./LICENSE.inc -
return $?
fi
}
bad=0
total=0
for file in $(find . -name '*.cc' -or -name '*.h'); do
license "$file"
bad=$((bad + $?))
if [[ $in_place != 0 ]]; then
$CLANG_FORMAT -i "$file"
else
$CLANG_FORMAT "$file" | \
git --no-pager \
diff --color=always --no-index --exit-code \
"$file" -
bad=$((bad + $?))
if [[ $? != 0 ]]; then
echo
fi
fi
total=$((total + 1))
done
if [[ $in_place != 0 ]]; then
./bazel run --color=yes --curses=no //third_party/fuchsia:format_guards -- --fix
else
./bazel run --color=yes --curses=no //third_party/fuchsia:format_guards
bad=$((bad + $?))
fi
if [[ $bad -gt 0 ]]; then
exit 1
fi