Skip to content

Commit

Permalink
git: add active_branch_remote for looking up active tracking-branch
Browse files Browse the repository at this point in the history
remote
  • Loading branch information
pmrowla committed Nov 7, 2023
1 parent 5807114 commit f388e71
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/scmrepo/git/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def is_dirty(self, untracked_files: bool = False) -> bool:
def active_branch(self) -> str:
pass

@abstractmethod
def active_branch_remote(self) -> str:
"""Return the fetch remote name for the current branch."""

@abstractmethod
def list_branches(self) -> Iterable[str]:
pass
Expand Down
3 changes: 3 additions & 0 deletions src/scmrepo/git/backend/dulwich/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,9 @@ def is_dirty(self, untracked_files: bool = False) -> bool:
def active_branch(self) -> str:
raise NotImplementedError

def active_branch_remote(self) -> str:
raise NotImplementedError

def list_branches(self) -> Iterable[str]:
base = "refs/heads/"
return sorted(ref[len(base) :] for ref in self.iter_refs(base))
Expand Down
6 changes: 6 additions & 0 deletions src/scmrepo/git/backend/gitpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ def active_branch(self):
except TypeError as exc:
raise SCMError("No active branch") from exc

def active_branch_remote(self) -> str:
try:
return self.repo.active_branch.tracking_branch()
except (TypeError, ValueError) as exc:
raise SCMError("No active branch tracking remote") from exc

def list_branches(self):
return [h.name for h in self.repo.heads]

Expand Down
9 changes: 9 additions & 0 deletions src/scmrepo/git/backend/pygit2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,15 @@ def active_branch(self) -> str:
return self.repo.references["HEAD"].target[11:]
return self.repo.head.shorthand

def active_branch_remote(self) -> str:
try:
upstream = self.repo.branches[self.active_branch()].upstream
if upstream:
return upstream.remote_name
except (KeyError, ValueError):
pass
raise SCMError("No active branch tracking remote")

def list_branches(self) -> Iterable[str]:
base = "refs/heads/"
return sorted(ref[len(base) :] for ref in self.iter_refs(base))
Expand Down

0 comments on commit f388e71

Please sign in to comment.