Skip to content

Commit 0a27f73

Browse files
codexByron
authored andcommitted
fix: resolve active_branch correctly for reftable refs
reviewed-by: Sebastian Thiel <sebastian.thiel@icloud.com>
1 parent 5780812 commit 0a27f73

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

git/repo/base.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,13 @@ 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+
active_branch = self.head.reference
1049+
if active_branch.name == ".invalid":
1050+
raise ValueError(
1051+
"HEAD points to 'refs/heads/.invalid', which Git uses to mark refs as incompatible "
1052+
"with older clients"
1053+
)
1054+
return active_branch
10501055

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

test/test_repo.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,48 @@ 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_active_branch_raises_value_error_when_head_ref_is_invalid(self, rw_dir):
967+
repo = Repo.init(rw_dir)
968+
with open(osp.join(rw_dir, ".git", "HEAD"), "w") as f:
969+
f.write("ref: refs/heads/.invalid\n")
970+
971+
self.assertRaisesRegex(
972+
ValueError,
973+
r"refs/heads/\.invalid.*older clients",
974+
lambda: repo.active_branch,
975+
)
976+
977+
@with_rw_directory
978+
def test_active_branch_raises_value_error_for_reftable_repo(self, rw_dir):
979+
git = Git(rw_dir)
980+
try:
981+
git.init(ref_format="reftable")
982+
except GitCommandError as err:
983+
if err.status == 129:
984+
pytest.skip("git init --ref-format is not supported by this git version")
985+
raise
986+
987+
with open(osp.join(rw_dir, ".git", "HEAD")) as f:
988+
self.assertEqual(f.read(), "ref: refs/heads/.invalid\n")
989+
990+
repo = Repo(rw_dir)
991+
self.assertRaisesRegex(
992+
ValueError,
993+
r"refs/heads/\.invalid.*older clients",
994+
lambda: repo.active_branch,
995+
)
996+
997+
@with_rw_directory
998+
def test_active_branch_raises_type_error_when_head_is_detached(self, rw_dir):
999+
repo = Repo.init(rw_dir)
1000+
with open(osp.join(rw_dir, "a.txt"), "w") as f:
1001+
f.write("a")
1002+
repo.index.add(["a.txt"])
1003+
repo.index.commit("initial commit")
1004+
repo.git.checkout(repo.head.commit.hexsha)
1005+
self.assertRaisesRegex(TypeError, "detached symbolic reference", lambda: repo.active_branch)
1006+
9651007
def test_merge_base(self):
9661008
repo = self.rorepo
9671009
c1 = "f6aa8d1"

0 commit comments

Comments
 (0)