feat: Add git_ignored_file method to check files against gitignore

This commit is contained in:
Paul Gauthier 2024-11-13 13:12:25 -08:00 committed by Paul Gauthier (aider)
parent ea1a4ecdc6
commit 218623be28
3 changed files with 17 additions and 0 deletions

View file

@ -355,6 +355,9 @@ class Coder:
for fname in fnames: for fname in fnames:
fname = Path(fname) fname = Path(fname)
if self.repo and self.repo.git_ignored_file(fname):
self.io.tool_warning(f"Skipping {fname} that matches gitignore spec.")
if self.repo and self.repo.ignored_file(fname): if self.repo and self.repo.ignored_file(fname):
self.io.tool_warning(f"Skipping {fname} that matches aiderignore spec.") self.io.tool_warning(f"Skipping {fname} that matches aiderignore spec.")
continue continue
@ -1779,6 +1782,10 @@ class Coder:
self.check_for_dirty_commit(path) self.check_for_dirty_commit(path)
return True return True
if self.repo and self.repo.git_ignored_file(path):
self.io.tool_warning(f"Skipping edits to {path} that matches gitignore spec.")
return
if not Path(full_path).exists(): if not Path(full_path).exists():
if not self.io.confirm_ask("Create new file?", subject=path): if not self.io.confirm_ask("Create new file?", subject=path):
self.io.tool_output(f"Skipping edits to {path}") self.io.tool_output(f"Skipping edits to {path}")

View file

@ -737,6 +737,10 @@ class Commands:
for matched_file in sorted(all_matched_files): for matched_file in sorted(all_matched_files):
abs_file_path = self.coder.abs_root_path(matched_file) abs_file_path = self.coder.abs_root_path(matched_file)
if self.coder.repo.git_ignored_file(matched_file):
self.io.tool_error(f"Can't add {matched_file} which is in gitignore")
continue
if not abs_file_path.startswith(self.coder.root) and not is_image_file(matched_file): if not abs_file_path.startswith(self.coder.root) and not is_image_file(matched_file):
self.io.tool_error( self.io.tool_error(
f"Can not add {abs_file_path}, which is not within {self.coder.root}" f"Can not add {abs_file_path}, which is not within {self.coder.root}"

View file

@ -331,6 +331,12 @@ class GitRepo:
lines, lines,
) )
def git_ignored_file(self, path):
if not self.repo:
return
if self.repo.ignored(path):
return True
def ignored_file(self, fname): def ignored_file(self, fname):
self.refresh_aider_ignore() self.refresh_aider_ignore()