Skip to content

Commit b9391bd

Browse files
committed
pygit: implement active_branch
1 parent 9b78a14 commit b9391bd

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

scmrepo/git/backend/pygit2.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,14 @@ def is_dirty(self, untracked_files: bool = False) -> bool:
251251
raise NotImplementedError
252252

253253
def active_branch(self) -> str:
254-
raise NotImplementedError
254+
if self.repo.head_is_detached:
255+
raise SCMError("No active branch (detached HEAD)")
256+
if self.repo.head_is_unborn:
257+
# if HEAD points to a nonexistent branch we still return the
258+
# branch name (without "refs/heads/" prefix) to match gitpython's
259+
# behavior
260+
return self.repo.references["HEAD"].target[11:]
261+
return self.repo.head.shorthand
255262

256263
def list_branches(self) -> Iterable[str]:
257264
raise NotImplementedError

0 commit comments

Comments
 (0)