refactor: improve file handling and error reporting in Coder class

This commit is contained in:
Paul Gauthier 2024-08-31 08:15:02 -07:00 committed by Paul Gauthier (aider)
parent b6b4fc6fab
commit 01964ca294
2 changed files with 17 additions and 12 deletions

View file

@ -349,20 +349,23 @@ class Coder:
for fname in fnames:
fname = Path(fname)
if not fname.exists():
self.io.tool_output(f"Creating empty file {fname}")
fname.parent.mkdir(parents=True, exist_ok=True)
utils.touch_file(fname)
if not fname.is_file():
raise ValueError(f"{fname} is not a file")
fname = str(fname.resolve())
if self.repo and self.repo.ignored_file(fname):
self.io.tool_error(f"Skipping {fname} that matches aiderignore spec.")
continue
if not fname.exists():
if utils.touch_file(fname):
self.io.tool_output(f"Creating empty file {fname}")
else:
self.io.tool_error(f"Can not create {fname}, skipping.")
continue
if not fname.is_file():
self.io.tool_error(f"Skipping {fname} that is not a normal file.")
continue
fname = str(fname.resolve())
self.abs_fnames.add(fname)
self.check_added_files()
@ -1677,8 +1680,9 @@ class Coder:
return
if not self.dry_run:
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
utils.touch_file(Path(full_path))
if not utils.touch_file(full_path):
self.io.tool_error(f"Unable to create {path}, skipping edits.")
return
# Seems unlikely that we needed to create the file, but it was
# actually already part of the repo.

View file

@ -292,6 +292,7 @@ def format_tokens(count):
def touch_file(fname):
fname = Path(fname)
try:
fname.parent.mkdir(parents=True, exist_ok=True)
fname.touch()