From 6a0cff50f8829d67b583074940123be46f95cdd9 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 6 Jul 2023 18:09:26 -0700 Subject: [PATCH] filter against git --- aider/commands.py | 20 +++++++++++++------- tests/test_commands.py | 14 ++++++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 29e2fbf15..ca75900b8 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -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}'") diff --git a/tests/test_commands.py b/tests/test_commands.py index d90772709..66026f0d6 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -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")