Sort headers using clang-tidy
[geeqie.git] / scripts / clang-tidy-check.sh
index 237e62d..e5c49e8 100755 (executable)
 #**********************************************************************
 
 ## @file
-## @brief Run clang-tidy on all source files.
+## @brief Run clang-tidy on source files.
 ##
 
+show_help()
+{
+       printf "Run clang-tidy on source files.
+
+-a --all  Process all source files - default is changed files only
+-f --fix  Fix errors where possible
+-h --help Display this message
+\n\n"
+}
+
+process_file()
+{
+       if [ -z "$file" ]
+       then
+               return
+       fi
+
+       i=$((i + 1))
+       printf '%d/%d %s\n' "$i" "$total_files" "$file"
+       clang-tidy ${fix+"--fix-errors"} --config-file ./.clang-tidy -p ./build "$file" 2> /dev/null
+
+       secs_now=$(cut --delimiter='.' --fields=1 < /proc/uptime)
+
+       elapsed_time=$((secs_now - secs_start))
+       remaining_files=$((total_files - i))
+       average_time=$((elapsed_time / i))
+       estimated_time=$((average_time * remaining_files))
+
+       printf 'Remaining: %dm:%ds\n' $((estimated_time % 3600 / 60)) $((estimated_time % 60))
+}
+
 if [ ! -d ".git" ] || [ ! -d "src" ] || [ ! -f "geeqie.1" ]
 then
        printf '%s\n' "This is not a Geeqie project folder"
        exit 1
 fi
 
-i=0
-
-secs_start=$(cut --delimiter='.' --fields=1 < /proc/uptime)
-total=$(find src -name "*.cc" | wc --lines)
+# if variable fix is defined in this way, clang-tidy gives errors.
+# fix=""
+process_all=0
 
-while read -r file
+while :
 do
-       i=$((i + 1))
-       printf '%d/%d %s\n' "$i" "$total" "$file"
-       clang-tidy --config-file ./.clang-tidy -p ./build "$file" 2>/dev/null
+       case $1 in
+               -h | -\? | --help)
+                       show_help
 
-       secs_now=$(cut --delimiter='.' --fields=1 < /proc/uptime)
+                       exit 0
+                       ;;
+               -a | --all)
+                       process_all=1
+                       ;;
+               -f | --fix)
+                       fix="--fix-errors"
+                       ;;
+               --) # End of all options.
+                       shift
+                       break
+                       ;;
+               ?*)
+                       printf 'Unknown option %s\n' "$1" >&2
+
+                       exit 1
+                       ;;
+               *)
+                       break
+                       ;;
+       esac
 
-       elapsed_time=$(( secs_now - secs_start ))
-       remaining_files=$(( total - i ))
-       average_time=$(( elapsed_time / i ))
-       estimated=$(( average_time * remaining_files ))
+       shift
+done
 
-       printf 'Remaining: %dm:%ds\n' $((estimated%3600/60)) $((estimated%60))
-done << EOF
+if [ ! -d "build" ]
+then
+       meson setup build
+fi
+ninja -C build
+
+i=0
+secs_start=$(cut --delimiter='.' --fields=1 < /proc/uptime)
+
+if [ "$process_all" -eq 1 ]
+then
+       total_files=$(find src -name "*.cc" | wc --lines)
+
+       while read -r file
+       do
+               process_file
+       done << EOF
 $(find src -name "*.cc" | sort)
 EOF
+else
+       total_files=$(git diff --name-only ./src/*.cc | wc --lines)
+
+       while read -r file
+       do
+               process_file
+       done << EOF
+$(git diff --name-only ./src/*.cc | sort)
+EOF
+fi