This commit is contained in:
Paul Gauthier 2023-06-22 11:29:59 -07:00
parent 0f5264b196
commit 08764215c3
2 changed files with 34 additions and 31 deletions

View file

@ -717,6 +717,35 @@ class Coder:
def get_addable_relative_files(self):
return set(self.get_all_relative_files()) - set(self.get_inchat_relative_files())
def allowed_to_edit(self, path):
full_path = os.path.abspath(os.path.join(self.root, path))
if full_path in self.abs_fnames:
return full_path
if not Path(full_path).exists():
question = f"Allow creation of new file {path}?" # noqa: E501
else:
question = f"Allow edits to {path} which was not previously provided?" # noqa: E501
if not self.io.confirm_ask(question):
self.io.tool_error(f"Skipping edit to {path}")
return
if not Path(full_path).exists():
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
Path(full_path).touch()
self.abs_fnames.add(full_path)
# Check if the file is already in the repo
if self.repo:
tracked_files = set(self.repo.git.ls_files().splitlines())
relative_fname = self.get_rel_fname(full_path)
if relative_fname not in tracked_files and self.io.confirm_ask(f"Add {path} to git?"):
self.repo.git.add(full_path)
return full_path
def apply_updates(self):
try:
edited = self.update_files()

View file

@ -1,5 +1,4 @@
import math
import os
import re
from difflib import SequenceMatcher
from pathlib import Path
@ -24,37 +23,12 @@ class EditBlockCoder(Coder):
edited = set()
for path, original, updated in edits:
full_path = os.path.abspath(os.path.join(self.root, path))
if full_path not in self.abs_fnames:
if not Path(full_path).exists():
question = f"Allow creation of new file {path}?" # noqa: E501
else:
question = (
f"Allow edits to {path} which was not previously provided?" # noqa: E501
)
if not self.io.confirm_ask(question):
self.io.tool_error(f"Skipping edit to {path}")
full_path = self.allowed_to_edit(path)
if not full_path:
continue
if not Path(full_path).exists():
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
Path(full_path).touch()
self.abs_fnames.add(full_path)
# Check if the file is already in the repo
if self.repo:
tracked_files = set(self.repo.git.ls_files().splitlines())
relative_fname = self.get_rel_fname(full_path)
if relative_fname not in tracked_files and self.io.confirm_ask(
f"Add {path} to git?"
):
self.repo.git.add(full_path)
if do_replace(full_path, original, updated, self.dry_run):
edited.add(path)
else:
continue
self.io.tool_error(f"Failed to apply edit to {path}")
return edited