diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 8f7900e1f..ebc6d4241 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -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() diff --git a/aider/coders/editblock_coder.py b/aider/coders/editblock_coder.py index 0bdc17f78..c96423dfb 100644 --- a/aider/coders/editblock_coder.py +++ b/aider/coders/editblock_coder.py @@ -1,5 +1,4 @@ import math -import os import re from difflib import SequenceMatcher from pathlib import Path @@ -24,38 +23,13 @@ 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}") - 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) - + full_path = self.allowed_to_edit(path) + if not full_path: + continue if do_replace(full_path, original, updated, self.dry_run): edited.add(path) - else: - self.io.tool_error(f"Failed to apply edit to {path}") + continue + self.io.tool_error(f"Failed to apply edit to {path}") return edited