2323# src/service/configuration.h
2424IGNORE_FILE=" .format_diff_ignore"
2525
26+ # Verbose mode - can be enabled via -v flag or VERBOSE environment variable
27+ VERBOSE=" ${VERBOSE:- 0} "
28+
29+ # Print debug message if verbose mode is enabled
30+ debug_print () {
31+ if [ " $VERBOSE " = " 1" ]; then
32+ echo " [DEBUG] $* " >&2
33+ fi
34+ }
35+
2636# Function to check for clang-format version. Fails if not found
2737get_clang_format_cmd () {
38+ # Check if clang-format is available
39+ local CLANG_FORMAT_BINARY=" clang-format"
40+ if command -v clang-format-18 > /dev/null 2>&1 ; then
41+ CLANG_FORMAT_BINARY=" clang-format-18"
42+ debug_print " Found clang-format-18 at: $( command -v clang-format-18) "
43+ elif command -v clang-format > /dev/null 2>&1 ; then
44+ CLANG_FORMAT_BINARY=" clang-format"
45+ debug_print " Found clang-format at: $( command -v clang-format) "
46+ else
47+ echo " Error: clang-format or clang-format-18 not found. Please install clang-format."
48+ exit 1
49+ fi
50+ debug_print " clang-format version: $( $( command -v ${CLANG_FORMAT_BINARY} ) --version) "
51+
2852 # Check if git-clang-format is available
2953 if command -v git-clang-format-18 > /dev/null 2>&1 ; then
30- echo " git-clang-format-18 --binary $( command -v clang-format-18) "
54+ debug_print " Found git-clang-format-18 at: $( command -v git-clang-format-18) "
55+ echo " git-clang-format-18 --binary $( command -v ${CLANG_FORMAT_BINARY} ) "
3156 elif command -v git-clang-format > /dev/null 2>&1 ; then
32- echo " git-clang-format --binary $( command -v clang-format) "
57+ debug_print " Found git-clang-format at: $( command -v git-clang-format) "
58+ echo " git-clang-format --binary $( command -v ${CLANG_FORMAT_BINARY} ) "
3359 else
3460 echo " Error: git-clang-format or git-clang-format-18 not found. Please install clang-format."
3561 exit 1
@@ -58,16 +84,20 @@ element_not_in_list() {
5884# file exists
5985get_ignorelist () {
6086 if ! [[ -f " $IGNORE_FILE " ]]; then
87+ debug_print " No ignore file found at: $IGNORE_FILE "
6188 echo " "
6289 return 1
6390 fi
6491
92+ debug_print " Reading ignore patterns from: $IGNORE_FILE "
6593 while IFS= read -r line; do
6694 if [[ -z " $line " ]]; then
6795 continue
6896 fi
6997
98+ debug_print " Processing ignore pattern: $line "
7099 while IFS= read -r -d $' \0' file_to_ignore; do
100+ debug_print " Ignoring file: $file_to_ignore "
71101 echo " $file_to_ignore "
72102 done < <( find . -wholename " $line " -print0)
73103 done < " $IGNORE_FILE "
@@ -100,9 +130,12 @@ filter_ignored_files() {
100130format_diff () {
101131 local branch_out_point=" $1 "
102132
133+ debug_print " Branch out point: $branch_out_point "
134+
103135 # Check if there are modified files matching the pattern
104136 local all_diffs
105137 all_diffs=$( grep -E ' \.(c|cpp|h|hpp)$' code_diff)
138+ debug_print " Modified C/C++ files: $( echo -n " $all_diffs " | wc -l) files"
106139
107140 local diffs
108141 diffs=$( filter_ignored_files " $all_diffs " )
@@ -112,16 +145,25 @@ format_diff() {
112145 return 0
113146 fi
114147
148+ debug_print " Files to check after filtering:"
149+ if [ " $VERBOSE " = " 1" ]; then
150+ echo " $diffs " | while read -r file; do
151+ debug_print " $file "
152+ done
153+ fi
154+
115155 local clang_format_cmd
116156 local formatted_files
117157
118158 # Run git clang-format for c, cpp, h and hpp files
119159 if clang_format_cmd=$( get_clang_format_cmd) ; then
160+ debug_print " Running command: $clang_format_cmd $branch_out_point <files>"
120161 # ${diffs} can't be quoted here in current state (can only handle source
121162 # files without space in the name)
122163 # We want error output as well, since clang-format can error out on files
123164 # and say that no files were changed
124165 formatted_files=$( 2>&1 $clang_format_cmd " ${branch_out_point} " ${diffs} )
166+ debug_print " Command output: $formatted_files "
125167 else
126168 # clang_format_cmd contains error message
127169 formatted_files=$clang_format_cmd
@@ -161,21 +203,35 @@ format_diff() {
161203# the most commonly used main branch
162204get_main_branch () {
163205 if ! [ -f .main_branch ]; then
206+ debug_print " No .main_branch file found, defaulting to origin/master"
164207 echo " origin/master"
165208 return
166209 fi
167210
168- cat .main_branch
211+ local main_branch
212+ main_branch=$( cat .main_branch)
213+ debug_print " Main branch from .main_branch: $main_branch "
214+ echo " $main_branch "
169215}
170216
171- # Check if a repository root directory has been passed to the script
172- if [ -n " $1 " ]; then
173- ROOT=" $1 "
174- else
175- ROOT=" ."
176- fi
217+ # Parse command line arguments
218+ ROOT=" ."
219+ while [[ $# -gt 0 ]]; do
220+ case $1 in
221+ -v|--verbose)
222+ VERBOSE=1
223+ debug_print " Verbose mode enabled"
224+ shift
225+ ;;
226+ * )
227+ ROOT=" $1 "
228+ shift
229+ ;;
230+ esac
231+ done
177232
178233# Error out if we can't switch directory
234+ debug_print " Changing to directory: ${ROOT} "
179235if ! cd " ${ROOT} " ; then
180236 echo " Could not enter directory: ${ROOT} "
181237
@@ -191,16 +247,25 @@ MAIN_BRANCH=$(get_main_branch)
191247echo " Checking clang-format against \" ${MAIN_BRANCH} \" ..."
192248
193249# Find the common ancestor between the current branch and the branch-out point
250+ debug_print " Running: git merge-base ${MAIN_BRANCH} HEAD"
194251if ! BRANCH_OUT_POINT=$( git merge-base " ${MAIN_BRANCH} " HEAD) ; then
195252 echo " Incorrect branch ${MAIN_BRANCH} "
196253 if [ -n " $CI " ]; then
197254 echo " Incorrect main branch ${MAIN_BRANCH} " > diff.md
198255 fi
199256 exit 1
200257fi
258+ debug_print " Merge base: $BRANCH_OUT_POINT "
201259
202260# Generate diff of modified files against the main
261+ debug_print " Running: git diff --name-only ${MAIN_BRANCH} --diff-filter=d"
203262git diff --name-only " ${MAIN_BRANCH} " --diff-filter=d > code_diff
263+ if [ " $VERBOSE " = " 1" ]; then
264+ debug_print " Modified files:"
265+ while read -r file; do
266+ debug_print " $file "
267+ done < code_diff
268+ fi
204269
205270# Call the function to format the modified parts of source files
206271format_diff " ${BRANCH_OUT_POINT} "
0 commit comments