66
77# Ensure GIV_HOME and GIV_TEMPLATE_DIR are initialized
88
9- # Source config and project_metadata for tests and runtime
10- if [ -n " $BATS_TEST_DIRNAME " ]; then
11- . " $BATS_TEST_DIRNAME /../src/commands/config.sh"
9+ # Source project_metadata for tests and runtime
10+ if [ -n " ${BATS_TEST_DIRNAME:- } " ]; then
1211 . " $BATS_TEST_DIRNAME /../src/project_metadata.sh"
1312else
14- . " $( dirname " $0 " ) /commands/config.sh"
1513 . " $( dirname " $0 " ) /project_metadata.sh"
1614fi
1715
@@ -85,6 +83,27 @@ print_commit_metadata() {
8583}
8684
8785
86+ # Get git diff output for a commit and write to file
87+ get_diff () {
88+ commit=" $1 "
89+ diff_pattern=" $2 "
90+ output_file=" $3 "
91+
92+ print_debug " Getting diff for commit: $commit with pattern: $diff_pattern "
93+
94+ case " $commit " in
95+ --cached)
96+ git --no-pager diff --cached --unified=3 --no-prefix --color=never -- $diff_pattern > " $output_file " 2> /dev/null || true
97+ ;;
98+ --current | " " )
99+ git --no-pager diff --unified=3 --no-prefix --color=never -- $diff_pattern > " $output_file " 2> /dev/null || true
100+ ;;
101+ * )
102+ git --no-pager diff " ${commit} ^!" --unified=3 --no-prefix --color=never -- $diff_pattern > " $output_file " 2> /dev/null || true
103+ ;;
104+ esac
105+ }
106+
88107# helper: builds main diff output (tracked + optional untracked)
89108build_diff () {
90109 commit=" $1 "
@@ -189,10 +208,12 @@ build_history() {
189208 printf ' **Date:** %s\n' " $( get_commit_date " $commit " ) " >> " $hist " || { print_error " Failed to write commit date to history file" ; return 1; }
190209
191210 print_debug " Getting version for commit $commit "
192- ver=$( get_metadata_value " version" " $commit " ) || { print_error " Failed to get project version for commit: $commit " ; return 1 ; }
211+ ver=$( get_metadata_value " version" " $commit " 2> /dev/null || true )
193212 if [ -n " $ver " ]; then
194213 print_debug " Version found: $ver "
195214 printf ' **Version:** %s\n' " $ver " >> " $hist " || { print_error " Failed to write version to history file" ; return 1; }
215+ else
216+ print_debug " No version found for commit $commit "
196217 fi
197218
198219 print_debug " Getting message header for commit $commit "
@@ -289,9 +310,15 @@ summarize_commit() {
289310 print_debug " Summary cache path: $summary_cache "
290311
291312 if [ -f " $summary_cache " ]; then
292- print_debug " Cache hit for commit: $commit "
293- cat " $summary_cache "
294- return 0
313+ # Check if cache has proper metadata format (starts with "Commit:")
314+ if head -1 " $summary_cache " | grep -q " ^Commit:" ; then
315+ print_debug " Cache hit for commit: $commit with proper metadata"
316+ cat " $summary_cache "
317+ return 0
318+ else
319+ print_debug " Cache exists but lacks metadata, regenerating for commit: $commit "
320+ rm -f " $summary_cache "
321+ fi
295322 fi
296323
297324 hist=$( portable_mktemp " hist.${commit} .XXXXXXX" ) || { print_error " Failed to create temp file for history" ; return 1; }
@@ -305,7 +332,7 @@ summarize_commit() {
305332 return 1
306333 fi
307334
308- sc_version=$( get_metadata_value " version" " $commit " ) || { print_error " Failed to get project version for $commit " ; return 1 ; }
335+ sc_version=$( get_metadata_value " version" " $commit " 2> /dev/null || true )
309336 print_debug " Commit version: $sc_version "
310337
311338 summary_template=$( build_commit_summary_prompt " $sc_version " " $hist " )
@@ -322,8 +349,15 @@ summarize_commit() {
322349 return 1
323350 fi
324351
325- mv " $res_file " " $summary_cache "
352+ # Prepend commit metadata to the response
353+ tmp_output=$( portable_mktemp " output.${commit} .XXXXXXX" ) || { print_error " Failed to create temp output file" ; return 1; }
354+ print_debug " Saving commit metadata for: $commit "
355+ save_commit_metadata " $commit " " $tmp_output "
356+ cat " $res_file " >> " $tmp_output "
357+
358+ mv " $tmp_output " " $summary_cache "
326359 print_debug " Summary cached at: $summary_cache "
360+ print_debug " Final output contains: $( head -1 " $summary_cache " ) "
327361 cat " $summary_cache "
328362}
329363
0 commit comments