Skip to content

Commit c7bdb67

Browse files
committed
Fix backtick parsing in issue-fix-mode.sh and improve error handling for diff processing
1 parent dae2869 commit c7bdb67

1 file changed

Lines changed: 33 additions & 23 deletions

File tree

scripts/issue-fix-mode.sh

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ for FILE_PATH in $FILES_TO_MODIFY; do
314314

315315
# Check if we need to create a new file
316316
if [ ! -f "$FILE_PATH" ]; then
317-
# Get the content between the first code block markers
317+
# Get the content between the first code block markers - using single quotes for robustness
318318
NEW_CONTENT=$(echo "$FILE_BLOCK" | sed -n '/```/,/```/p' | sed '1d;$d')
319319

320320
# Create directory if it doesn't exist
@@ -324,34 +324,44 @@ for FILE_PATH in $FILES_TO_MODIFY; do
324324
echo "$NEW_CONTENT" > "$FILE_PATH"
325325
echo "Created new file: $FILE_PATH"
326326
else
327-
# Extract diff blocks
328-
DIFF_BLOCKS=$(echo "$FILE_BLOCK" | grep -A100 "^```diff" | sed -n '/^```diff/,/^```/p' | sed '1d;$d')
327+
# Extract diff blocks - using a more robust approach to handle backticks
328+
DIFF_BLOCKS=$(echo "$FILE_BLOCK" | grep -A100 '```diff' | sed -n '/```diff/,/```/p' | sed '1d;$d')
329329

330330
# Create a temporary file for the new content
331331
TEMP_FILE=$(mktemp)
332332
cat "$FILE_PATH" > "$TEMP_FILE"
333333

334-
# Process each diff block
335-
echo "$DIFF_BLOCKS" | while IFS= read -r LINE; do
336-
if [[ "$LINE" == -* ]]; then
337-
# Remove line (strip the leading -)
338-
REMOVE_LINE="${LINE:1}"
339-
sed -i.bak "s|$REMOVE_LINE||g" "$TEMP_FILE"
340-
elif [[ "$LINE" == +* ]]; then
341-
# Add line (strip the leading +)
342-
ADD_LINE="${LINE:1}"
343-
# Find the line above where we need to add
344-
PREV_LINE=$(echo "$DIFF_BLOCKS" | grep -B1 "\\+$ADD_LINE" | head -n1)
345-
if [[ "$PREV_LINE" == -* ]]; then
346-
# Replace the removed line with the added line
347-
PREV_LINE="${PREV_LINE:1}"
348-
sed -i.bak "s|$PREV_LINE|$ADD_LINE|g" "$TEMP_FILE"
349-
else
350-
# Just append the line for now (this is simplistic)
351-
echo "$ADD_LINE" >> "$TEMP_FILE"
334+
# Check if we have any diff blocks to process
335+
if [ -z "$DIFF_BLOCKS" ]; then
336+
echo "Warning: No diff blocks found in Claude's response. Proceeding without changes."
337+
else
338+
echo "Found diff blocks, processing changes..."
339+
# Process each diff block
340+
echo "$DIFF_BLOCKS" | while IFS= read -r LINE; do
341+
if [[ "$LINE" == -* ]]; then
342+
# Remove line (strip the leading -)
343+
REMOVE_LINE="${LINE:1}"
344+
echo "Removing: $REMOVE_LINE"
345+
sed -i.bak "s|$REMOVE_LINE||g" "$TEMP_FILE"
346+
elif [[ "$LINE" == +* ]]; then
347+
# Add line (strip the leading +)
348+
ADD_LINE="${LINE:1}"
349+
echo "Adding: $ADD_LINE"
350+
# Find the line above where we need to add
351+
PREV_LINE=$(echo "$DIFF_BLOCKS" | grep -B1 "\\+$ADD_LINE" | head -n1)
352+
if [[ "$PREV_LINE" == -* ]]; then
353+
# Replace the removed line with the added line
354+
PREV_LINE="${PREV_LINE:1}"
355+
echo "Replacing: $PREV_LINE with: $ADD_LINE"
356+
sed -i.bak "s|$PREV_LINE|$ADD_LINE|g" "$TEMP_FILE"
357+
else
358+
# Just append the line for now (this is simplistic)
359+
echo "Appending: $ADD_LINE"
360+
echo "$ADD_LINE" >> "$TEMP_FILE"
361+
fi
352362
fi
353-
fi
354-
done
363+
done
364+
fi
355365

356366
# Move the modified content back to the original file
357367
mv "$TEMP_FILE" "$FILE_PATH"

0 commit comments

Comments
 (0)