Skip to content

Commit 6c52dec

Browse files
CopilotByron
andcommitted
fix: resolve active_branch correctly for reftable refs
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 5e61e41 commit 6c52dec

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

git/repo/base.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,12 @@ def active_branch(self) -> Head:
10451045
:return:
10461046
:class:`~git.refs.head.Head` to the active branch
10471047
"""
1048-
# reveal_type(self.head.reference) # => Reference
1049-
return self.head.reference
1048+
try:
1049+
return Head(self, self.git.symbolic_ref("HEAD").strip())
1050+
except GitCommandError as err:
1051+
if err.status == 1 or "not a symbolic ref" in str(err).lower():
1052+
raise TypeError("HEAD is a detached symbolic reference as it points to a commit") from err
1053+
raise
10501054

10511055
def blame_incremental(self, rev: str | HEAD | None, file: str, **kwargs: Any) -> Iterator["BlameEntry"]:
10521056
"""Iterator for blame information for the given file at the given revision.

test/test_repo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,20 @@ def test_empty_repo(self, rw_dir):
962962

963963
assert "BAD MESSAGE" not in contents, "log is corrupt"
964964

965+
@with_rw_directory
966+
def test_empty_repo_reftable_active_branch(self, rw_dir):
967+
git = Git(rw_dir)
968+
try:
969+
git.init(ref_format="reftable")
970+
except GitCommandError as err:
971+
if err.status == 129:
972+
pytest.skip("git init --ref-format is not supported by this git version")
973+
raise
974+
975+
repo = Repo(rw_dir)
976+
self.assertEqual(repo.active_branch.name, "master")
977+
assert not repo.active_branch.is_valid(), "Branch is yet to be born"
978+
965979
def test_merge_base(self):
966980
repo = self.rorepo
967981
c1 = "f6aa8d1"

0 commit comments

Comments
 (0)