From d720bfe459789e16bb201f86e6647b7a31206d23 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 2 Nov 2023 15:50:30 -0700 Subject: [PATCH] Properly test if /added file is already in the repo #314 --- aider/commands.py | 3 ++- tests/test_commands.py | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index f780e5120..79a1b33eb 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -305,6 +305,7 @@ class Commands: for matched_file in all_matched_files: abs_file_path = self.coder.abs_root_path(matched_file) + rel_path = self.coder.get_rel_fname(matched_file) if not abs_file_path.startswith(self.coder.root): self.io.tool_error( @@ -312,7 +313,7 @@ class Commands: ) continue - if self.coder.repo and matched_file not in git_files: + if self.coder.repo and rel_path not in git_files: try: self.coder.repo.repo.git.add(abs_file_path) git_added.append(matched_file) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3957df8ef..7ff801ba8 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -432,3 +432,29 @@ class TestCommands(TestCase): # Assert that foo.txt has been `git add` but not `git commit` added_files = repo.git.diff("--cached", "--name-only").split() self.assertIn("foo.txt", added_files) + + def test_cmd_add_existing_with_dirty_repo(self): + with GitTemporaryDirectory(): + repo = git.Repo() + + files = ["one.txt", "two.txt"] + for fname in files: + Path(fname).touch() + repo.git.add(fname) + repo.git.commit("-m", "initial") + + commit = repo.head.commit.hexsha + + # leave a dirty `git rm` + repo.git.rm("one.txt") + + io = InputOutput(pretty=False, yes=True) + from aider.coders import Coder + + coder = Coder.create(models.GPT35, None, io) + commands = Commands(io, coder) + + # There's no reason this /add should trigger a commit + commands.cmd_add("two.txt") + + self.assertEqual(commit, repo.head.commit.hexsha)