Skip to content

Commit

Permalink
fix: handle bad patches better
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 10, 2024
1 parent 45cfbac commit 771734c
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions gptme/tools/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,23 @@ def execute_patch(
Applies the patch.
"""
fn = " ".join(args)
assert fn, "No filename provided"
if not fn:
yield Message("system", "No path provided")
return

path = Path(fn).expanduser()
if not path.exists():
raise ValueError(f"file not found: {fn}")
yield Message("system", f"File not found: {fn}")
return

patches = Patch.from_codeblock(code)
patches_str = "\n\n".join(p.diff_minimal() for p in patches)
print_preview(patches_str, lang="diff")
try:
patches = Patch.from_codeblock(code)
patches_str = "\n\n".join(p.diff_minimal() for p in patches)
except ValueError as e:
yield Message("system", f"Patch failed: {e.args[0]}")
return

print_preview(patches_str, lang="diff")
if ask:
# TODO: display minimal patches
confirm = ask_execute(f"Apply patch to {fn}?")
Expand Down

0 comments on commit 771734c

Please sign in to comment.