mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-21 21:04:59 +00:00
better tests, small cleanups
This commit is contained in:
parent
95fb55b9d9
commit
752e47a886
3 changed files with 64 additions and 12 deletions
|
@ -727,7 +727,9 @@ class Coder:
|
||||||
def allowed_to_edit(self, path):
|
def allowed_to_edit(self, path):
|
||||||
full_path = self.abs_root_path(path)
|
full_path = self.abs_root_path(path)
|
||||||
if self.repo:
|
if self.repo:
|
||||||
is_in_repo = self.repo.path_in_repo(path)
|
need_to_add = not self.repo.path_in_repo(path)
|
||||||
|
else:
|
||||||
|
need_to_add = False
|
||||||
|
|
||||||
if full_path in self.abs_fnames:
|
if full_path in self.abs_fnames:
|
||||||
self.check_for_dirty_commit(path)
|
self.check_for_dirty_commit(path)
|
||||||
|
@ -742,7 +744,10 @@ class Coder:
|
||||||
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
|
Path(full_path).parent.mkdir(parents=True, exist_ok=True)
|
||||||
Path(full_path).touch()
|
Path(full_path).touch()
|
||||||
|
|
||||||
if self.repo and not is_in_repo:
|
# Seems unlikely that we needed to create the file, but it was
|
||||||
|
# actually already part of the repo.
|
||||||
|
# But let's handle this obscure corner case anyway.
|
||||||
|
if need_to_add:
|
||||||
self.repo.repo.git.add(full_path)
|
self.repo.repo.git.add(full_path)
|
||||||
|
|
||||||
self.abs_fnames.add(full_path)
|
self.abs_fnames.add(full_path)
|
||||||
|
@ -754,7 +759,7 @@ class Coder:
|
||||||
self.io.tool_error(f"Skipping edits to {path}")
|
self.io.tool_error(f"Skipping edits to {path}")
|
||||||
return
|
return
|
||||||
|
|
||||||
if self.repo and not is_in_repo:
|
if need_to_add:
|
||||||
self.repo.repo.git.add(full_path)
|
self.repo.repo.git.add(full_path)
|
||||||
|
|
||||||
self.abs_fnames.add(full_path)
|
self.abs_fnames.add(full_path)
|
||||||
|
@ -780,7 +785,8 @@ class Coder:
|
||||||
if allowed:
|
if allowed:
|
||||||
res.append(edit)
|
res.append(edit)
|
||||||
|
|
||||||
self.dirty_commit()
|
fnames = [edit[0] for edit in res]
|
||||||
|
self.dirty_commit(fnames)
|
||||||
self.need_commit_before_edits = False
|
self.need_commit_before_edits = False
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
@ -880,14 +886,14 @@ class Coder:
|
||||||
self.io.tool_output("No changes made to git tracked files.")
|
self.io.tool_output("No changes made to git tracked files.")
|
||||||
return self.gpt_prompts.files_content_gpt_no_edits
|
return self.gpt_prompts.files_content_gpt_no_edits
|
||||||
|
|
||||||
def dirty_commit(self):
|
def dirty_commit(self, fnames):
|
||||||
if not self.need_commit_before_edits:
|
if not self.need_commit_before_edits:
|
||||||
return
|
return
|
||||||
if not self.dirty_commits:
|
if not self.dirty_commits:
|
||||||
return
|
return
|
||||||
if not self.repo:
|
if not self.repo:
|
||||||
return
|
return
|
||||||
self.repo.commit()
|
self.repo.commit(fnames=fnames)
|
||||||
|
|
||||||
# files changed, move cur messages back behind the files messages
|
# files changed, move cur messages back behind the files messages
|
||||||
self.move_back_cur_messages(self.gpt_prompts.files_content_local_edits)
|
self.move_back_cur_messages(self.gpt_prompts.files_content_local_edits)
|
||||||
|
|
|
@ -59,7 +59,9 @@ class GitRepo:
|
||||||
diff_args = []
|
diff_args = []
|
||||||
if fnames:
|
if fnames:
|
||||||
diff_args += ["--"] + list(fnames)
|
diff_args += ["--"] + list(fnames)
|
||||||
|
dump(diff_args)
|
||||||
diffs = self.get_diffs(False, *diff_args)
|
diffs = self.get_diffs(False, *diff_args)
|
||||||
|
dump(diffs)
|
||||||
commit_message = self.get_commit_message(diffs, context)
|
commit_message = self.get_commit_message(diffs, context)
|
||||||
|
|
||||||
if not commit_message:
|
if not commit_message:
|
||||||
|
@ -134,6 +136,7 @@ class GitRepo:
|
||||||
if args:
|
if args:
|
||||||
if pretty:
|
if pretty:
|
||||||
args = ["--color"] + args
|
args = ["--color"] + args
|
||||||
|
dump(args)
|
||||||
return self.repo.git.diff(*args)
|
return self.repo.git.diff(*args)
|
||||||
|
|
||||||
# otherwise, we always want diffs of index and working dir
|
# otherwise, we always want diffs of index and working dir
|
||||||
|
|
|
@ -474,9 +474,16 @@ TWO
|
||||||
fname = Path("file.txt")
|
fname = Path("file.txt")
|
||||||
fname.write_text("one\n")
|
fname.write_text("one\n")
|
||||||
repo.git.add(str(fname))
|
repo.git.add(str(fname))
|
||||||
|
|
||||||
|
fname2 = Path("other.txt")
|
||||||
|
fname2.write_text("other\n")
|
||||||
|
repo.git.add(str(fname2))
|
||||||
|
|
||||||
repo.git.commit("-m", "new")
|
repo.git.commit("-m", "new")
|
||||||
|
|
||||||
|
# dirty
|
||||||
fname.write_text("two\n")
|
fname.write_text("two\n")
|
||||||
|
fname2.write_text("OTHER\n")
|
||||||
|
|
||||||
io = InputOutput(yes=True)
|
io = InputOutput(yes=True)
|
||||||
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname)])
|
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname)])
|
||||||
|
@ -495,23 +502,59 @@ three
|
||||||
"""
|
"""
|
||||||
coder.partial_response_function_call = dict()
|
coder.partial_response_function_call = dict()
|
||||||
|
|
||||||
|
saved_diffs = []
|
||||||
|
|
||||||
|
def mock_get_commit_message(diffs, context):
|
||||||
|
saved_diffs.append(diffs)
|
||||||
|
return "commit message"
|
||||||
|
|
||||||
|
coder.repo.get_commit_message = MagicMock(side_effect=mock_get_commit_message)
|
||||||
coder.send = MagicMock(side_effect=mock_send)
|
coder.send = MagicMock(side_effect=mock_send)
|
||||||
coder.repo.get_commit_message = MagicMock()
|
|
||||||
coder.repo.get_commit_message.return_value = "commit message"
|
|
||||||
|
|
||||||
coder.run(with_message="hi")
|
coder.run(with_message="hi")
|
||||||
|
|
||||||
|
print("=" * 20)
|
||||||
|
print(repo.git.log(["-p"]))
|
||||||
|
print("=" * 20)
|
||||||
|
|
||||||
content = fname.read_text()
|
content = fname.read_text()
|
||||||
self.assertEqual(content, "three\n")
|
self.assertEqual(content, "three\n")
|
||||||
|
|
||||||
num_commits = len(list(repo.iter_commits(repo.active_branch.name)))
|
num_commits = len(list(repo.iter_commits(repo.active_branch.name)))
|
||||||
self.assertEqual(num_commits, 3)
|
self.assertEqual(num_commits, 3)
|
||||||
|
|
||||||
self.assertIn("two", repo.git.diff(["HEAD~1", "HEAD"]))
|
diff = repo.git.diff(["HEAD~2", "HEAD~1"])
|
||||||
self.assertIn("three", repo.git.diff(["HEAD~1", "HEAD"]))
|
dump(diff)
|
||||||
|
self.assertIn("one", diff)
|
||||||
|
self.assertIn("two", diff)
|
||||||
|
self.assertNotIn("three", diff)
|
||||||
|
self.assertNotIn("other", diff)
|
||||||
|
self.assertNotIn("OTHER", diff)
|
||||||
|
|
||||||
self.assertIn("one", repo.git.diff(["HEAD~2", "HEAD~1"]))
|
dump(saved_diffs)
|
||||||
self.assertIn("two", repo.git.diff(["HEAD~2", "HEAD~1"]))
|
diff = saved_diffs[0]
|
||||||
|
self.assertIn("one", diff)
|
||||||
|
self.assertIn("two", diff)
|
||||||
|
self.assertNotIn("three", diff)
|
||||||
|
self.assertNotIn("other", diff)
|
||||||
|
self.assertNotIn("OTHER", diff)
|
||||||
|
|
||||||
|
diff = repo.git.diff(["HEAD~1", "HEAD"])
|
||||||
|
self.assertNotIn("one", diff)
|
||||||
|
self.assertIn("two", diff)
|
||||||
|
self.assertIn("three", diff)
|
||||||
|
self.assertNotIn("other", diff)
|
||||||
|
self.assertNotIn("OTHER", diff)
|
||||||
|
|
||||||
|
diff = saved_diffs[1]
|
||||||
|
self.assertNotIn("one", diff)
|
||||||
|
self.assertIn("two", diff)
|
||||||
|
self.assertIn("three", diff)
|
||||||
|
self.assertNotIn("other", diff)
|
||||||
|
self.assertNotIn("OTHER", diff)
|
||||||
|
|
||||||
|
dump(saved_diffs)
|
||||||
|
self.assertEqual(len(saved_diffs), 2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue