Skip to content

Commit 057fc47

Browse files
CopilotByron
andcommitted
test: add regression coverage for reftable active_branch
Co-authored-by: Byron <63622+Byron@users.noreply.github.com> Agent-Logs-Url: https://github.com/gitpython-developers/GitPython/sessions/cb73f600-a42a-4400-b68c-da530d57677f
1 parent 6c52dec commit 057fc47

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

git/repo/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,9 +1046,9 @@ def active_branch(self) -> Head:
10461046
:class:`~git.refs.head.Head` to the active branch
10471047
"""
10481048
try:
1049-
return Head(self, self.git.symbolic_ref("HEAD").strip())
1049+
return Head(self, self.git.symbolic_ref("-q", "HEAD").strip())
10501050
except GitCommandError as err:
1051-
if err.status == 1 or "not a symbolic ref" in str(err).lower():
1051+
if err.status == 1:
10521052
raise TypeError("HEAD is a detached symbolic reference as it points to a commit") from err
10531053
raise
10541054

test/test_repo.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -973,9 +973,22 @@ def test_empty_repo_reftable_active_branch(self, rw_dir):
973973
raise
974974

975975
repo = Repo(rw_dir)
976-
self.assertEqual(repo.active_branch.name, "master")
976+
expected_active_branch_path = git.symbolic_ref("HEAD").strip()
977+
expected_active_branch_name = git.symbolic_ref("--short", "HEAD").strip()
978+
self.assertEqual(repo.active_branch.path, expected_active_branch_path)
979+
self.assertEqual(repo.active_branch.name, expected_active_branch_name)
977980
assert not repo.active_branch.is_valid(), "Branch is yet to be born"
978981

982+
@with_rw_directory
983+
def test_active_branch_raises_type_error_when_head_is_detached(self, rw_dir):
984+
repo = Repo.init(rw_dir)
985+
with open(osp.join(rw_dir, "a.txt"), "w") as f:
986+
f.write("a")
987+
repo.index.add(["a.txt"])
988+
repo.index.commit("initial commit")
989+
repo.git.checkout(repo.head.commit.hexsha)
990+
self.assertRaisesRegex(TypeError, "detached symbolic reference", lambda: repo.active_branch)
991+
979992
def test_merge_base(self):
980993
repo = self.rorepo
981994
c1 = "f6aa8d1"

0 commit comments

Comments
 (0)