mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-24 06:15:00 +00:00
Handle diffs on a detached head #520
This commit is contained in:
parent
5c10944054
commit
c2b8b2355d
2 changed files with 40 additions and 4 deletions
|
@ -137,11 +137,17 @@ class GitRepo:
|
|||
|
||||
def get_diffs(self, fnames=None):
|
||||
# We always want diffs of index and working dir
|
||||
|
||||
current_branch_has_commits = False
|
||||
try:
|
||||
commits = self.repo.iter_commits(self.repo.active_branch)
|
||||
current_branch_has_commits = any(commits)
|
||||
except git.exc.GitCommandError:
|
||||
current_branch_has_commits = False
|
||||
active_branch = self.repo.active_branch
|
||||
try:
|
||||
commits = self.repo.iter_commits(active_branch)
|
||||
current_branch_has_commits = any(commits)
|
||||
except git.exc.GitCommandError:
|
||||
pass
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
if not fnames:
|
||||
fnames = []
|
||||
|
|
|
@ -53,6 +53,36 @@ class TestRepo(unittest.TestCase):
|
|||
self.assertIn("index", diffs)
|
||||
self.assertIn("workingdir", diffs)
|
||||
|
||||
def test_diffs_detached_head(self):
|
||||
with GitTemporaryDirectory():
|
||||
repo = git.Repo()
|
||||
fname = Path("foo.txt")
|
||||
fname.touch()
|
||||
repo.git.add(str(fname))
|
||||
repo.git.commit("-m", "foo")
|
||||
|
||||
fname2 = Path("bar.txt")
|
||||
fname2.touch()
|
||||
repo.git.add(str(fname2))
|
||||
repo.git.commit("-m", "bar")
|
||||
|
||||
fname3 = Path("baz.txt")
|
||||
fname3.touch()
|
||||
repo.git.add(str(fname3))
|
||||
repo.git.commit("-m", "baz")
|
||||
|
||||
repo.git.checkout("HEAD^")
|
||||
|
||||
fname.write_text("index\n")
|
||||
repo.git.add(str(fname))
|
||||
|
||||
fname2.write_text("workingdir\n")
|
||||
|
||||
git_repo = GitRepo(InputOutput(), None, ".")
|
||||
diffs = git_repo.get_diffs()
|
||||
self.assertIn("index", diffs)
|
||||
self.assertIn("workingdir", diffs)
|
||||
|
||||
def test_diffs_between_commits(self):
|
||||
with GitTemporaryDirectory():
|
||||
repo = git.Repo()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue