Skip to content

Commit

Permalink
Properly test if /added file is already in the repo #314
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-gauthier committed Nov 2, 2023
1 parent 2609ec1 commit d720bfe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion aider/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,15 @@ def cmd_add(self, args):

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(
f"Can not add {abs_file_path}, which is not within {self.coder.root}"
)
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)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,29 @@ def test_cmd_add_no_autocommit(self):
# 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)

0 comments on commit d720bfe

Please sign in to comment.