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
12 changes: 10 additions & 2 deletions git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1042,11 +1042,19 @@ def active_branch(self) -> Head:
:raise TypeError:
If HEAD is detached.

:raise ValueError:
If HEAD points to the ``.invalid`` ref Git uses to mark refs as
incompatible with older clients.

:return:
:class:`~git.refs.head.Head` to the active branch
"""
# reveal_type(self.head.reference) # => Reference
return self.head.reference
active_branch = self.head.reference
if active_branch.name == ".invalid":
raise ValueError(
"HEAD points to 'refs/heads/.invalid', which Git uses to mark refs as incompatible with older clients"
)
return active_branch

def blame_incremental(self, rev: str | HEAD | None, file: str, **kwargs: Any) -> Iterator["BlameEntry"]:
"""Iterator for blame information for the given file at the given revision.
Expand Down
40 changes: 40 additions & 0 deletions test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,6 +962,46 @@ def test_empty_repo(self, rw_dir):

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

@with_rw_directory
def test_active_branch_raises_value_error_when_head_ref_is_invalid(self, rw_dir):
repo = Repo.init(rw_dir)
with open(osp.join(rw_dir, ".git", "HEAD"), "w") as f:
f.write("ref: refs/heads/.invalid\n")

self.assertRaisesRegex(
ValueError,
r"refs/heads/\.invalid.*older clients",
lambda: repo.active_branch,
)

@with_rw_directory
def test_empty_repo_reftable_active_branch(self, rw_dir):
git = Git(rw_dir)
try:
git.init(ref_format="reftable")
except GitCommandError as err:
if err.status == 129:
pytest.skip("git init --ref-format is not supported by this git version")
raise

repo = Repo(rw_dir)
self.assertEqual(repo.head.reference.name, ".invalid")
self.assertRaisesRegex(
ValueError,
r"refs/heads/\.invalid.*older clients",
lambda: repo.active_branch,
)

@with_rw_directory
def test_active_branch_raises_type_error_when_head_is_detached(self, rw_dir):
repo = Repo.init(rw_dir)
with open(osp.join(rw_dir, "a.txt"), "w") as f:
f.write("a")
repo.index.add(["a.txt"])
repo.index.commit("initial commit")
repo.git.checkout(repo.head.commit.hexsha)
self.assertRaisesRegex(TypeError, "detached symbolic reference", lambda: repo.active_branch)

def test_merge_base(self):
repo = self.rorepo
c1 = "f6aa8d1"
Expand Down
Loading