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 json
import os import os
import shlex import shlex
@ -220,12 +219,22 @@ class Commands:
if partial.lower() in fname.lower(): if partial.lower() in fname.lower():
yield Completion(fname, start_position=-len(partial)) 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): def cmd_add(self, args):
"Add matching files to the chat session using glob patterns" "Add matching files to the chat session using glob patterns"
added_fnames = [] added_fnames = []
for word in args.split(): 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 not matched_files:
if any(char in word for char in "*?[]"): if any(char in word for char in "*?[]"):
@ -289,11 +298,8 @@ class Commands:
self.coder.abs_fnames = set() self.coder.abs_fnames = set()
for word in args.split(): for word in args.split():
matched_files = [ matched_files = self.glob_filtered_to_repo(word)
file
for file in self.coder.abs_fnames
if glob.fnmatch.fnmatch(os.path.basename(file), word)
]
if not matched_files: if not matched_files:
self.io.tool_error(f"No files matched '{word}'") self.io.tool_error(f"No files matched '{word}'")

View file

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