]>
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 | |
6d028ff3 | 27 | |
6fb460aa | 28 | FORMAT=clang-format-19 |
8e416b30 OM |
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 | ||
ba5ba96c | 37 | for file in "${@}"; do |
4a4e056a | 38 | if [ -h "$file" -o ! -f "$file" ]; then |
6d028ff3 OM |
39 | if [ $verbose = 1 ]; then |
40 | echo "$file: skipped, not a regular file or unreadable" | |
41 | fi | |
4a4e056a OM |
42 | continue |
43 | fi | |
2a8fe6ca | 44 | tmp=$(mktemp "$file.XXXXXXXX") |
8e416b30 | 45 | if ! $FORMAT -style=file "$file" > "$tmp"; then |
4a4e056a | 46 | rm "$tmp" |
ba5ba96c | 47 | else |
4a4e056a OM |
48 | if ! cmp -s "$file" "$tmp"; then |
49 | echo "$file: reformatted" | |
50 | mv "$tmp" "$file" | |
ba5ba96c | 51 | else |
6d028ff3 OM |
52 | if [ $verbose = 1 ]; then |
53 | echo "$file: already formatted to perfection" | |
54 | fi | |
4a4e056a | 55 | rm "$tmp" |
ba5ba96c OM |
56 | fi |
57 | fi | |
58 | done | |
59 |