]>
Commit | Line | Data |
---|---|---|
ba5ba96c OM |
1 | #!/bin/sh |
2 | ||
3 | # | |
4 | # Reformat code, but do not touch if no changes. | |
ba5ba96c OM |
5 | # |
6 | ||
4a4e056a | 7 | if [ "$0" != "./build-scripts/format-code" -a "$0" != "build-scripts/format-code" ]; then |
ba5ba96c OM |
8 | echo "Please run me from the root checkout dir" |
9 | exit 1 | |
10 | fi | |
11 | ||
4a4e056a OM |
12 | if [ $# = 0 ]; then |
13 | echo usage: $0 file... | |
14 | echo | |
15 | echo format C++ files, does not touch non-regular files | |
16 | exit 0 | |
17 | fi | |
ba5ba96c OM |
18 | if [ ! -e .clang-format ]; then |
19 | echo "No .clang-format file found in ."; | |
20 | exit 1 | |
21 | fi | |
22 | ||
6d028ff3 | 23 | verbose=0 |
a37beaad | 24 | if [ -t 1 ]; then |
6d028ff3 OM |
25 | verbose=1 |
26 | fi | |
27 | if [ x$CIRCLECI = xtrue ]; then | |
28 | verbose=0 | |
29 | fi | |
30 | ||
4a3bd953 | 31 | FORMAT=clang-format-11 |
8e416b30 OM |
32 | if ! which $FORMAT 2> /dev/null; then |
33 | FORMAT=clang-format | |
34 | fi | |
35 | ||
36 | if [ $verbose = 1 ]; then | |
37 | echo Using executable $FORMAT | |
38 | fi | |
39 | ||
ba5ba96c | 40 | for file in "${@}"; do |
4a4e056a | 41 | if [ -h "$file" -o ! -f "$file" ]; then |
6d028ff3 OM |
42 | if [ $verbose = 1 ]; then |
43 | echo "$file: skipped, not a regular file or unreadable" | |
44 | fi | |
4a4e056a OM |
45 | continue |
46 | fi | |
2a8fe6ca | 47 | tmp=$(mktemp "$file.XXXXXXXX") |
8e416b30 | 48 | if ! $FORMAT -style=file "$file" > "$tmp"; then |
4a4e056a | 49 | rm "$tmp" |
ba5ba96c | 50 | else |
4a4e056a OM |
51 | if ! cmp -s "$file" "$tmp"; then |
52 | echo "$file: reformatted" | |
53 | mv "$tmp" "$file" | |
ba5ba96c | 54 | else |
6d028ff3 OM |
55 | if [ $verbose = 1 ]; then |
56 | echo "$file: already formatted to perfection" | |
57 | fi | |
4a4e056a | 58 | rm "$tmp" |
ba5ba96c OM |
59 | fi |
60 | fi | |
61 | done | |
62 |