]> git.ipfire.org Git - thirdparty/pdns.git/blame_incremental - build-scripts/format-code
Merge pull request #15825 from miodvallat/fewer_mistakes
[thirdparty/pdns.git] / build-scripts / format-code
... / ...
CommitLineData
1#!/bin/sh
2
3#
4# Reformat code, but do not touch if no changes.
5#
6
7if [ "$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
10fi
11
12if [ $# = 0 ]; then
13 echo usage: $0 file...
14 echo
15 echo format C++ files, does not touch non-regular files
16 exit 0
17fi
18if [ ! -e .clang-format ]; then
19 echo "No .clang-format file found in .";
20 exit 1
21fi
22
23verbose=0
24if [ -t 1 ]; then
25 verbose=1
26fi
27
28FORMAT=clang-format-19
29if ! which $FORMAT 2> /dev/null; then
30 FORMAT=clang-format
31fi
32
33if [ $verbose = 1 ]; then
34 echo Using executable $FORMAT
35fi
36
37for 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
58done
59