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