fixed spurious "Adding to git" messages when running aider in subdir of repo

This commit is contained in:
Paul Gauthier 2023-08-17 08:32:44 -07:00
parent 6359376953
commit 6e9fbdcb6a
2 changed files with 30 additions and 3 deletions

View file

@ -50,9 +50,9 @@ class GitRepo:
self.root = utils.safe_abs_path(self.repo.working_tree_dir) self.root = utils.safe_abs_path(self.repo.working_tree_dir)
def add_new_files(self, fnames): def add_new_files(self, fnames):
cur_files = [Path(fn).resolve() for fn in self.get_tracked_files()] cur_files = [str(Path(fn).resolve()) for fn in self.get_tracked_files()]
for fname in fnames: for fname in fnames:
if Path(fname).resolve() in cur_files: if str(Path(fname).resolve()) in cur_files:
continue continue
if not Path(fname).exists(): if not Path(fname).exists():
continue continue
@ -183,7 +183,10 @@ class GitRepo:
files.extend(staged_files) files.extend(staged_files)
# convert to appropriate os.sep, since git always normalizes to / # convert to appropriate os.sep, since git always normalizes to /
res = set(str(Path(PurePosixPath(path))) for path in files) res = set(
str(Path(PurePosixPath((Path(self.root) / path).relative_to(self.root))))
for path in files
)
return res return res

View file

@ -170,3 +170,27 @@ class TestRepo(unittest.TestCase):
fnames = git_repo.get_tracked_files() fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames) self.assertIn(str(fname), fnames)
self.assertIn(str(fname2), fnames) self.assertIn(str(fname2), fnames)
def test_get_tracked_files_from_subdir(self):
with GitTemporaryDirectory():
# new repo
raw_repo = git.Repo()
# add it, but no commits at all in the raw_repo yet
fname = Path("subdir/new.txt")
fname.parent.mkdir()
fname.touch()
raw_repo.git.add(str(fname))
os.chdir(fname.parent)
git_repo = GitRepo(InputOutput(), None, None)
# better be there
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)
# commit it, better still be there
raw_repo.git.commit("-m", "new")
fnames = git_repo.get_tracked_files()
self.assertIn(str(fname), fnames)