Skip to content

Commit 209b623

Browse files
committed
fix: improved errors for invalid patches
1 parent becab4d commit 209b623

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

gptme/tools/patch.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
```
5555
""".strip()
5656

57+
ORIGINAL = "\n<<<<<<< ORIGINAL\n"
58+
DIVIDER = "\n=======\n"
59+
UPDATED = ">>>>>>> UPDATED\n"
60+
5761

5862
def apply(codeblock: str, content: str) -> str:
5963
"""
@@ -64,11 +68,17 @@ def apply(codeblock: str, content: str) -> str:
6468
codeblock = codeblock.strip()
6569

6670
# get the original and modified chunks
67-
original = re.split("\n<<<<<<< ORIGINAL\n", codeblock)[1]
68-
original, modified = re.split("\n=======\n", original)
69-
if ">>>>>>> UPDATED\n" not in modified: # pragma: no cover
70-
raise ValueError("invalid patch", codeblock)
71-
modified = re.split(">>>>>>> UPDATED\n", modified)[0].rstrip("\n")
71+
if ORIGINAL not in codeblock: # pragma: no cover
72+
raise ValueError(f"invalid patch, no `{ORIGINAL.strip()}`", codeblock)
73+
original = re.split(ORIGINAL, codeblock)[1]
74+
75+
if DIVIDER not in original: # pragma: no cover
76+
raise ValueError(f"invalid patch, no `{DIVIDER.strip()}`", codeblock)
77+
original, modified = re.split(DIVIDER, original)
78+
79+
if UPDATED not in modified: # pragma: no cover
80+
raise ValueError(f"invalid patch, no `{UPDATED.strip()}`", codeblock)
81+
modified = re.split(UPDATED, modified)[0].rstrip("\n")
7282

7383
# TODO: maybe allow modified chunk to contain "// ..." to refer to chunks in the original,
7484
# and then replace these with the original chunks?

0 commit comments

Comments
 (0)