Bump to ccache 4.4
[platform/upstream/ccache.git] / misc / format-files
1 #!/bin/sh
2
3 set -eu
4
5 tmp_file=$(mktemp)
6 trap "rm $tmp_file" EXIT
7
8 all=
9 check=
10
11 for arg in "$@"; do
12     case $arg in
13         --all)
14             all=$arg
15             ;;
16         --check)
17             check=$arg
18             ;;
19         *)
20             break
21             ;;
22     esac
23     shift
24 done
25
26 if [ -n "$all" ]; then
27     exec sh "$0" $check $(git ls-files '*.[ch]' '*.[ch]pp' ':!:src/third_party')
28 fi
29
30 clang_format="$(dirname "$0")/clang-format"
31 [ -t 1 ] && cf_color="--color=always" || cf_color=""
32
33 if [ -n "${VERBOSE:-}" ]; then
34     "$clang_format" --version
35 fi
36
37 status=0
38 for file in "$@"; do
39     "$clang_format" "$file" >"$tmp_file"
40     if cmp -s "$file" "$tmp_file"; then
41         continue
42     fi
43
44     if [ -n "$check" ]; then
45         status=1
46         echo "Error: $file not formatted with Clang-Format"
47         echo 'Run "make format" or apply this diff:'
48         git diff $cf_color --no-index "$file" "$tmp_file" \
49             | sed -E -e "s!^---.*!--- a/$file!" \
50                      -e "s!^\+\+\+.*!+++ b/$file!" \
51                      -e "/diff --/d" -e "/index /d" \
52                      -e "s/.[0-9]*.clang-format.tmp//"
53     else
54         echo "Reformatted $file"
55         cp "$tmp_file" "$file"
56     fi
57 done
58
59 exit $status