Skip to content

Commit 60bd9dc

Browse files
authored
Merge pull request #598 from valdezm/fix/update-agent-context-missing-sections
Fix update-agent-context.sh to handle files without required sections
2 parents eb030da + 09cf4f6 commit 60bd9dc

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

scripts/bash/update-agent-context.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,25 @@ update_existing_agent_file() {
391391
new_change_entry="- $CURRENT_BRANCH: Added $NEW_DB"
392392
fi
393393

394+
# Check if sections exist in the file
395+
local has_active_technologies=0
396+
local has_recent_changes=0
397+
398+
if grep -q "^## Active Technologies" "$target_file" 2>/dev/null; then
399+
has_active_technologies=1
400+
fi
401+
402+
if grep -q "^## Recent Changes" "$target_file" 2>/dev/null; then
403+
has_recent_changes=1
404+
fi
405+
394406
# Process file line by line
395407
local in_tech_section=false
396408
local in_changes_section=false
397409
local tech_entries_added=false
398410
local changes_entries_added=false
399411
local existing_changes_count=0
412+
local file_ended=false
400413

401414
while IFS= read -r line || [[ -n "$line" ]]; do
402415
# Handle Active Technologies section
@@ -457,6 +470,22 @@ update_existing_agent_file() {
457470
# Post-loop check: if we're still in the Active Technologies section and haven't added new entries
458471
if [[ $in_tech_section == true ]] && [[ $tech_entries_added == false ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
459472
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
473+
tech_entries_added=true
474+
fi
475+
476+
# If sections don't exist, add them at the end of the file
477+
if [[ $has_active_technologies -eq 0 ]] && [[ ${#new_tech_entries[@]} -gt 0 ]]; then
478+
echo "" >> "$temp_file"
479+
echo "## Active Technologies" >> "$temp_file"
480+
printf '%s\n' "${new_tech_entries[@]}" >> "$temp_file"
481+
tech_entries_added=true
482+
fi
483+
484+
if [[ $has_recent_changes -eq 0 ]] && [[ -n "$new_change_entry" ]]; then
485+
echo "" >> "$temp_file"
486+
echo "## Recent Changes" >> "$temp_file"
487+
echo "$new_change_entry" >> "$temp_file"
488+
changes_entries_added=true
460489
fi
461490

462491
# Move temp file to target atomically

0 commit comments

Comments
 (0)