filter against git

This commit is contained in:
Paul Gauthier 2023-07-06 18:09:26 -07:00
parent cb2b2c5ce3
commit 6a0cff50f8
2 changed files with 23 additions and 11 deletions

View file

@ -1,4 +1,3 @@
import glob
import json
import os
import shlex
@ -220,12 +219,22 @@ class Commands:
if partial.lower() in fname.lower():
yield Completion(fname, start_position=-len(partial))
def glob_filtered_to_repo(self, pattern):
matched_files = Path(self.coder.root).glob(pattern)
# if repo, filter against it
if self.coder.repo:
git_files = self.coder.get_tracked_files()
matched_files = [fn for fn in matched_files if str(fn) in git_files]
return list(map(str, matched_files))
def cmd_add(self, args):
"Add matching files to the chat session using glob patterns"
added_fnames = []
for word in args.split():
matched_files = glob.glob(word, recursive=True)
matched_files = self.glob_filtered_to_repo(word)
if not matched_files:
if any(char in word for char in "*?[]"):
@ -289,11 +298,8 @@ class Commands:
self.coder.abs_fnames = set()
for word in args.split():
matched_files = [
file
for file in self.coder.abs_fnames
if glob.fnmatch.fnmatch(os.path.basename(file), word)
]
matched_files = self.glob_filtered_to_repo(word)
if not matched_files:
self.io.tool_error(f"No files matched '{word}'")

View file

@ -4,6 +4,7 @@ import shutil
import sys
import tempfile
from io import StringIO
from pathlib import Path
from unittest import TestCase
from aider import models
@ -86,14 +87,19 @@ class TestCommands(TestCase):
coder = Coder.create(models.GPT35, None, io, openai_api_key="deadbeef")
commands = Commands(io, coder)
with open("test1.py", "w") as f:
f.write("print('test1')")
with open("test2.py", "w") as f:
f.write("print('test2')")
subdir = Path("subdir")
subdir.mkdir()
(subdir / "subtest1.py").touch()
(subdir / "subtest2.py").touch()
Path("test1.py").touch()
Path("test2.py").touch()
# Add some files to the chat session
commands.cmd_add("*.py")
self.assertEqual(len(coder.abs_fnames), 2)
# Call the cmd_drop method with a glob pattern
commands.cmd_drop("*2.py")