Sort headers using clang-tidy
[geeqie.git] / scripts / clang-tidy-check.sh
1 #! /bin/sh
2 #**********************************************************************
3 # Copyright (C) 2024 - The Geeqie Team
4 #
5 # Author: Colin Clark
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #**********************************************************************
21
22 ## @file
23 ## @brief Run clang-tidy on source files.
24 ##
25
26 show_help()
27 {
28         printf "Run clang-tidy on source files.
29
30 -a --all  Process all source files - default is changed files only
31 -f --fix  Fix errors where possible
32 -h --help Display this message
33 \n\n"
34 }
35
36 process_file()
37 {
38         if [ -z "$file" ]
39         then
40                 return
41         fi
42
43         i=$((i + 1))
44         printf '%d/%d %s\n' "$i" "$total_files" "$file"
45         clang-tidy ${fix+"--fix-errors"} --config-file ./.clang-tidy -p ./build "$file" 2> /dev/null
46
47         secs_now=$(cut --delimiter='.' --fields=1 < /proc/uptime)
48
49         elapsed_time=$((secs_now - secs_start))
50         remaining_files=$((total_files - i))
51         average_time=$((elapsed_time / i))
52         estimated_time=$((average_time * remaining_files))
53
54         printf 'Remaining: %dm:%ds\n' $((estimated_time % 3600 / 60)) $((estimated_time % 60))
55 }
56
57 if [ ! -d ".git" ] || [ ! -d "src" ] || [ ! -f "geeqie.1" ]
58 then
59         printf '%s\n' "This is not a Geeqie project folder"
60         exit 1
61 fi
62
63 # if variable fix is defined in this way, clang-tidy gives errors.
64 # fix=""
65 process_all=0
66
67 while :
68 do
69         case $1 in
70                 -h | -\? | --help)
71                         show_help
72
73                         exit 0
74                         ;;
75                 -a | --all)
76                         process_all=1
77                         ;;
78                 -f | --fix)
79                         fix="--fix-errors"
80                         ;;
81                 --) # End of all options.
82                         shift
83                         break
84                         ;;
85                 ?*)
86                         printf 'Unknown option %s\n' "$1" >&2
87
88                         exit 1
89                         ;;
90                 *)
91                         break
92                         ;;
93         esac
94
95         shift
96 done
97
98 if [ ! -d "build" ]
99 then
100         meson setup build
101 fi
102 ninja -C build
103
104 i=0
105 secs_start=$(cut --delimiter='.' --fields=1 < /proc/uptime)
106
107 if [ "$process_all" -eq 1 ]
108 then
109         total_files=$(find src -name "*.cc" | wc --lines)
110
111         while read -r file
112         do
113                 process_file
114         done << EOF
115 $(find src -name "*.cc" | sort)
116 EOF
117 else
118         total_files=$(git diff --name-only ./src/*.cc | wc --lines)
119
120         while read -r file
121         do
122                 process_file
123         done << EOF
124 $(git diff --name-only ./src/*.cc | sort)
125 EOF
126 fi