]> git.ipfire.org Git - thirdparty/pdns.git/blob - build-scripts/format-code
Do not use github deprecated API for changelog script
[thirdparty/pdns.git] / build-scripts / format-code
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 if [ x$CIRCLECI = xtrue ]; then
28 verbose=0
29 fi
30
31 FORMAT=clang-format-8
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
40 for file in "${@}"; do
41 if [ -h "$file" -o ! -f "$file" ]; then
42 if [ $verbose = 1 ]; then
43 echo "$file: skipped, not a regular file or unreadable"
44 fi
45 continue
46 fi
47 tmp=$(mktemp "$file.XXXXXXXX")
48 if ! $FORMAT -style=file "$file" > "$tmp"; then
49 rm "$tmp"
50 else
51 if ! cmp -s "$file" "$tmp"; then
52 echo "$file: reformatted"
53 mv "$tmp" "$file"
54 else
55 if [ $verbose = 1 ]; then
56 echo "$file: already formatted to perfection"
57 fi
58 rm "$tmp"
59 fi
60 fi
61 done
62