Skip to content

Commit d37382c

Browse files
authored
Merge pull request #285 from jamwil/git-ops
Fix apparent bug when running the `GitDiff` tool
2 parents 85bae17 + 40917cc commit d37382c

5 files changed

Lines changed: 95 additions & 6 deletions

File tree

aider/repo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,17 @@ def get_diffs(self, fnames=None):
417417
except ANY_GIT_ERROR as err:
418418
self.io.tool_error(f"Unable to diff: {err}")
419419

420-
def diff_commits(self, pretty, from_commit, to_commit):
420+
def diff_commits(self, pretty, from_commit, to_commit=None):
421421
args = []
422422
if pretty:
423423
args += ["--color"]
424424
else:
425425
args += ["--color=never"]
426426

427-
args += [from_commit, to_commit]
427+
if to_commit is not None:
428+
args += [from_commit, to_commit]
429+
else:
430+
args += [from_commit]
428431
diffs = self.repo.git.diff(*args, stdout_as_string=False).decode(
429432
self.io.encoding, "replace"
430433
)

aider/tools/git_branch.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ def execute(
113113
args.extend(["--format", format])
114114

115115
# Execute git command
116-
result = coder.repo.repo.git.branch(*args)
116+
result = coder.repo.repo.git.branch(*args).strip()
117117

118118
# If no result and show_current was used, get current branch directly
119119
if not result and show_current:
120-
current_branch = coder.repo.repo.active_branch.name
121-
return current_branch
120+
try:
121+
head = coder.repo.repo.head
122+
if head.is_detached:
123+
return "HEAD (detached)"
124+
return coder.repo.repo.active_branch.name
125+
except ANY_GIT_ERROR:
126+
return "No current branch found."
122127

123128
return result if result else "No branches found matching the criteria."
124129

aider/tools/git_diff.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def execute(cls, coder, branch=None):
3636

3737
try:
3838
if branch:
39-
diff = coder.repo.diff_commits(False, branch, "HEAD")
39+
# Diff working tree against the requested branch/commit
40+
diff = coder.repo.diff_commits(False, branch, None)
4041
else:
4142
diff = coder.repo.diff_commits(False, "HEAD", None)
4243

tests/tools/test_git_branch.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from pathlib import Path
2+
from types import SimpleNamespace
3+
4+
import git
5+
6+
from aider.io import InputOutput
7+
from aider.repo import GitRepo
8+
from aider.tools import git_branch
9+
from aider.utils import GitTemporaryDirectory
10+
11+
12+
def _make_repo():
13+
repo = git.Repo()
14+
repo.config_writer().set_value("commit", "gpgsign", "false").release()
15+
return repo
16+
17+
18+
def test_gitbranch_show_current_returns_branch_name():
19+
with GitTemporaryDirectory():
20+
repo = _make_repo()
21+
Path("file.txt").write_text("content\n")
22+
repo.git.add("file.txt")
23+
repo.git.commit("-m", "init")
24+
repo.git.checkout("-b", "feature")
25+
26+
io = InputOutput()
27+
git_repo = GitRepo(io, None, ".")
28+
coder = SimpleNamespace(repo=git_repo, io=io)
29+
30+
result = git_branch.Tool.execute(coder, show_current=True)
31+
32+
assert result.strip() == "feature"
33+
34+
35+
def test_gitbranch_show_current_handles_detached_head():
36+
with GitTemporaryDirectory():
37+
repo = _make_repo()
38+
Path("file.txt").write_text("content\n")
39+
repo.git.add("file.txt")
40+
repo.git.commit("-m", "init")
41+
42+
commit_sha = repo.head.commit.hexsha
43+
repo.git.checkout(commit_sha)
44+
45+
io = InputOutput()
46+
git_repo = GitRepo(io, None, ".")
47+
coder = SimpleNamespace(repo=git_repo, io=io)
48+
49+
result = git_branch.Tool.execute(coder, show_current=True)
50+
51+
assert result.strip() == "HEAD (detached)"

tests/tools/test_git_diff.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from pathlib import Path
2+
from types import SimpleNamespace
3+
4+
import git
5+
6+
from aider.io import InputOutput
7+
from aider.repo import GitRepo
8+
from aider.tools import git_diff
9+
from aider.utils import GitTemporaryDirectory
10+
11+
12+
def test_gitdiff_head_argument_includes_working_tree_changes():
13+
with GitTemporaryDirectory():
14+
repo = git.Repo()
15+
fname = Path("example.txt")
16+
fname.write_text("original\n")
17+
repo.git.add(str(fname))
18+
repo.config_writer().set_value("commit", "gpgsign", "false").release()
19+
repo.git.commit("-m", "initial")
20+
21+
fname.write_text("updated\n")
22+
23+
io = InputOutput()
24+
git_repo = GitRepo(io, None, ".")
25+
coder = SimpleNamespace(repo=git_repo, io=io)
26+
27+
result = git_diff.Tool.execute(coder, branch="HEAD")
28+
29+
assert "updated" in result

0 commit comments

Comments
 (0)