Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions aider/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,17 @@ def get_diffs(self, fnames=None):
except ANY_GIT_ERROR as err:
self.io.tool_error(f"Unable to diff: {err}")

def diff_commits(self, pretty, from_commit, to_commit):
def diff_commits(self, pretty, from_commit, to_commit=None):
args = []
if pretty:
args += ["--color"]
else:
args += ["--color=never"]

args += [from_commit, to_commit]
if to_commit is not None:
args += [from_commit, to_commit]
else:
args += [from_commit]
diffs = self.repo.git.diff(*args, stdout_as_string=False).decode(
self.io.encoding, "replace"
)
Expand Down
11 changes: 8 additions & 3 deletions aider/tools/git_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,17 @@ def execute(
args.extend(["--format", format])

# Execute git command
result = coder.repo.repo.git.branch(*args)
result = coder.repo.repo.git.branch(*args).strip()

# If no result and show_current was used, get current branch directly
if not result and show_current:
current_branch = coder.repo.repo.active_branch.name
return current_branch
try:
head = coder.repo.repo.head
if head.is_detached:
return "HEAD (detached)"
return coder.repo.repo.active_branch.name
except ANY_GIT_ERROR:
return "No current branch found."

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

Expand Down
3 changes: 2 additions & 1 deletion aider/tools/git_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def execute(cls, coder, branch=None):

try:
if branch:
diff = coder.repo.diff_commits(False, branch, "HEAD")
# Diff working tree against the requested branch/commit
diff = coder.repo.diff_commits(False, branch, None)
else:
diff = coder.repo.diff_commits(False, "HEAD", None)

Expand Down
51 changes: 51 additions & 0 deletions tests/tools/test_git_branch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from pathlib import Path
from types import SimpleNamespace

import git

from aider.io import InputOutput
from aider.repo import GitRepo
from aider.tools import git_branch
from aider.utils import GitTemporaryDirectory


def _make_repo():
repo = git.Repo()
repo.config_writer().set_value("commit", "gpgsign", "false").release()
return repo


def test_gitbranch_show_current_returns_branch_name():
with GitTemporaryDirectory():
repo = _make_repo()
Path("file.txt").write_text("content\n")
repo.git.add("file.txt")
repo.git.commit("-m", "init")
repo.git.checkout("-b", "feature")

io = InputOutput()
git_repo = GitRepo(io, None, ".")
coder = SimpleNamespace(repo=git_repo, io=io)

result = git_branch.Tool.execute(coder, show_current=True)

assert result.strip() == "feature"


def test_gitbranch_show_current_handles_detached_head():
with GitTemporaryDirectory():
repo = _make_repo()
Path("file.txt").write_text("content\n")
repo.git.add("file.txt")
repo.git.commit("-m", "init")

commit_sha = repo.head.commit.hexsha
repo.git.checkout(commit_sha)

io = InputOutput()
git_repo = GitRepo(io, None, ".")
coder = SimpleNamespace(repo=git_repo, io=io)

result = git_branch.Tool.execute(coder, show_current=True)

assert result.strip() == "HEAD (detached)"
29 changes: 29 additions & 0 deletions tests/tools/test_git_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from pathlib import Path
from types import SimpleNamespace

import git

from aider.io import InputOutput
from aider.repo import GitRepo
from aider.tools import git_diff
from aider.utils import GitTemporaryDirectory


def test_gitdiff_head_argument_includes_working_tree_changes():
with GitTemporaryDirectory():
repo = git.Repo()
fname = Path("example.txt")
fname.write_text("original\n")
repo.git.add(str(fname))
repo.config_writer().set_value("commit", "gpgsign", "false").release()
repo.git.commit("-m", "initial")

fname.write_text("updated\n")

io = InputOutput()
git_repo = GitRepo(io, None, ".")
coder = SimpleNamespace(repo=git_repo, io=io)

result = git_diff.Tool.execute(coder, branch="HEAD")

assert "updated" in result