Skip to content

Commit dd7be0d

Browse files
committed
fix: added patch previews
1 parent df9d83c commit dd7be0d

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

gptme/tools/patch.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pathlib import Path
1010

1111
from ..message import Message
12-
from ..util import ask_execute
12+
from ..util import ask_execute, print_preview
1313
from .base import ToolSpec, ToolUse
1414

1515

@@ -76,17 +76,18 @@ def diff_minimal(self, strip_context=False) -> str:
7676
# TODO: show this when previewing the patch
7777
# TODO: replace previous patches with the minimal version
7878

79-
diff = difflib.unified_diff(
80-
self.original.splitlines(),
81-
self.updated.splitlines(),
82-
lineterm="",
83-
fromfile="original",
84-
tofile="updated",
85-
)
86-
diff = list(diff)[3:]
79+
diff = list(
80+
difflib.unified_diff(
81+
self.original.splitlines(),
82+
self.updated.splitlines(),
83+
lineterm="",
84+
fromfile="original",
85+
tofile="updated",
86+
)
87+
)[3:]
8788
if strip_context:
8889
# find first and last lines with changes
89-
markers = [l[0] for l in diff]
90+
markers = [line[0] for line in diff]
9091
start = min(
9192
markers.index("+") if "+" in markers else len(markers),
9293
markers.index("-") if "-" in markers else len(markers),
@@ -95,6 +96,7 @@ def diff_minimal(self, strip_context=False) -> str:
9596
markers[::-1].index("+") if "+" in markers else len(markers),
9697
markers[::-1].index("-") if "-" in markers else len(markers),
9798
)
99+
len(diff) - start - end
98100
diff = diff[start : len(diff) - end]
99101
return "\n".join(diff)
100102

@@ -170,7 +172,13 @@ def execute_patch(
170172
path = Path(fn).expanduser()
171173
if not path.exists():
172174
raise ValueError(f"file not found: {fn}")
175+
176+
patches = Patch.from_codeblock(code)
177+
patches_str = "\n\n".join(p.diff_minimal() for p in patches)
178+
print_preview(patches_str, lang="diff")
179+
173180
if ask:
181+
# TODO: display minimal patches
174182
confirm = ask_execute(f"Apply patch to {fn}?")
175183
if not confirm:
176184
print("Patch not applied")
@@ -186,13 +194,13 @@ def execute_patch(
186194
f.write(patched_content)
187195

188196
# Compare token counts
189-
patch_tokens = len(code)
190-
full_file_tokens = len(patched_content)
197+
patch_len = len(code)
198+
full_file_len = len(patched_content)
191199

192200
warnings = []
193-
if full_file_tokens < patch_tokens:
201+
if 1000 < full_file_len < patch_len:
194202
warnings.append(
195-
"Note: The patch was larger than the file. In the future, try writing smaller patches or use the save tool instead."
203+
"Note: The patch was big and larger than the file. In the future, try writing smaller patches or use the save tool instead."
196204
)
197205
warnings_str = ("\n".join(warnings) + "\n") if warnings else ""
198206

gptme/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def epoch_to_age(epoch):
130130
def print_preview(code: str, lang: str): # pragma: no cover
131131
print()
132132
print("[bold white]Preview[/bold white]")
133-
print(Syntax(code.strip(), lang))
133+
print(Syntax(code.strip("\n"), lang))
134134
print()
135135

136136

0 commit comments

Comments
 (0)