Skip to content

Commit

Permalink
fix: fix uncaught assert when patching missing file (fixes #39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 17, 2023
1 parent 0a0f7ca commit e34fb2c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions gptme/tools/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
Inspired by aider.
"""
import re
from pathlib import Path
from collections.abc import Generator
from pathlib import Path

from ..message import Message
from ..util import ask_execute
Expand Down Expand Up @@ -67,8 +67,12 @@ def apply(codeblock: str, content: str) -> str:
def apply_file(codeblock, filename):
codeblock = codeblock.strip()
_patch, filename = codeblock.splitlines()[0].split()
assert _patch == "```patch"
assert Path(filename).exists()
if not _patch == "```patch":
raise ValueError(
"invalid patch, codeblock is missing leading ```patch", codeblock
)
if not Path(filename).exists():
raise FileNotFoundError(filename)

with open(filename) as f:
content = f.read()
Expand All @@ -94,5 +98,5 @@ def execute_patch(codeblock: str, fn: str, ask: bool) -> Generator[Message, None
try:
apply_file(codeblock, fn)
yield Message("system", "Patch applied")
except ValueError as e:
except (ValueError, FileNotFoundError) as e:
yield Message("system", f"Patch failed: {e.args[0]}")

0 comments on commit e34fb2c

Please sign in to comment.