Skip to content

Commit bbfa27c

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 bbfa27c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

git/repo/base.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,11 +1042,20 @@ def active_branch(self) -> Head:
10421042
:raise TypeError:
10431043
If HEAD is detached.
10441044
1045+
:raise ValueError:
1046+
If HEAD points to the ``.invalid`` ref Git uses to mark refs as
1047+
incompatible with older clients.
1048+
10451049
:return:
10461050
:class:`~git.refs.head.Head` to the active branch
10471051
"""
1048-
# reveal_type(self.head.reference) # => Reference
1049-
return self.head.reference
1052+
active_branch = self.head.reference
1053+
if active_branch.name == ".invalid":
1054+
raise ValueError(
1055+
"HEAD points to 'refs/heads/.invalid', which Git uses to mark refs as incompatible "
1056+
"with older clients"
1057+
)
1058+
return active_branch
10501059

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

test/test_repo.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,46 @@ 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_empty_repo_reftable_active_branch(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+
repo = Repo(rw_dir)
988+
self.assertEqual(repo.head.reference.name, ".invalid")
989+
self.assertRaisesRegex(
990+
ValueError,
991+
r"refs/heads/\.invalid.*older clients",
992+
lambda: repo.active_branch,
993+
)
994+
995+
@with_rw_directory
996+
def test_active_branch_raises_type_error_when_head_is_detached(self, rw_dir):
997+
repo = Repo.init(rw_dir)
998+
with open(osp.join(rw_dir, "a.txt"), "w") as f:
999+
f.write("a")
1000+
repo.index.add(["a.txt"])
1001+
repo.index.commit("initial commit")
1002+
repo.git.checkout(repo.head.commit.hexsha)
1003+
self.assertRaisesRegex(TypeError, "detached symbolic reference", lambda: repo.active_branch)
1004+
9651005
def test_merge_base(self):
9661006
repo = self.rorepo
9671007
c1 = "f6aa8d1"

0 commit comments

Comments
 (0)