Skip to content

Commit

Permalink
fix: support line removal in patch tool (#267)
Browse files Browse the repository at this point in the history
The patch tool now properly handles empty content between ======= and >>>>>>> UPDATED,
allowing for line removal in patches. This is done by:
- Splitting patch content in multiple steps
- Special handling of the case where UPDATED marker follows immediately after DIVIDER
- Maintaining compatibility with existing patch formats
  • Loading branch information
ErikBjare authored Nov 22, 2024
1 parent 84e5ab6 commit 7c51573
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions gptme/tools/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,26 @@ def _from_codeblock(cls, codeblock: str) -> Generator["Patch", None, None]:
if ORIGINAL not in patch: # pragma: no cover
raise ValueError(f"invalid patch, no `{ORIGINAL.strip()}`", patch)

parts = re.split(
f"{re.escape(ORIGINAL)}|{re.escape(DIVIDER)}|{re.escape(UPDATED)}",
patch,
# First split on ORIGINAL to get the content after it
_, after_original = re.split(re.escape(ORIGINAL), patch, maxsplit=1)

# Then split on DIVIDER to get the original content
if DIVIDER not in after_original: # pragma: no cover
raise ValueError("invalid patch format: missing =======")
original, after_divider = re.split(
re.escape(DIVIDER), after_original, maxsplit=1
)
if len(parts) != 4: # pragma: no cover
raise ValueError("invalid patch format")

_, original, modified, _ = parts
# Finally split on UPDATED to get the modified content
# Use UPDATED[1:] to ignore the leading newline in the marker,
# allowing us to detect truly empty content between ======= and >>>>>>> UPDATED
if after_divider.startswith(UPDATED[1:]):
# Special case: empty content followed immediately by UPDATED marker
modified = ""
else:
if UPDATED not in after_divider: # pragma: no cover
raise ValueError("invalid patch format: missing >>>>>>> UPDATED")
modified, _ = re.split(re.escape(UPDATED), after_divider, maxsplit=1)
yield Patch(original, modified)

@classmethod
Expand Down

0 comments on commit 7c51573

Please sign in to comment.