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