fix #171: parse remote (remove hardcoded 'origin')

This commit is contained in:
Manuel Barkhau 2022-03-12 00:21:27 +00:00
parent 25b6d39c03
commit 9be1186601

View file

@ -16,6 +16,7 @@ mercurial, then the git terms are used. For example "fetch"
""" """
import os import os
import re
import sys import sys
import shlex import shlex
import typing as typ import typing as typ
@ -28,6 +29,19 @@ from . import config
logger = logging.getLogger("bumpver.vcs") logger = logging.getLogger("bumpver.vcs")
BRANCH_PATTERN = r"""
(?P<is_current>\*)?
\s+
(?P<branch>[\S]+)
\s+
[0-9a-f]+
\s+
\[(?P<remote>[^/]+)/[^\]]+\]
"""
BRANCH_RE = re.compile(BRANCH_PATTERN, flags=re.VERBOSE)
VCS_SUBCOMMANDS_BY_NAME = { VCS_SUBCOMMANDS_BY_NAME = {
'git': { 'git': {
'is_usable' : "git rev-parse --git-dir", 'is_usable' : "git rev-parse --git-dir",
@ -37,8 +51,9 @@ VCS_SUBCOMMANDS_BY_NAME = {
'add_path' : "git add --update {path}", 'add_path' : "git add --update {path}",
'commit' : "git commit --message '{message}'", 'commit' : "git commit --message '{message}'",
'tag' : "git tag --annotate {tag} --message {tag}", 'tag' : "git tag --annotate {tag} --message {tag}",
'push_tag' : "git push origin --follow-tags {tag} HEAD", 'push_tag' : "git push {remote} --follow-tags {tag} HEAD",
'show_remotes': "git config --get remote.origin.url", 'show_remotes': "git config --get remote.origin.url",
'ls_branches' : "git branch -vv",
}, },
'hg': { 'hg': {
'is_usable' : "hg root", 'is_usable' : "hg root",
@ -98,21 +113,28 @@ class VCSAPI:
else: else:
raise raise
@property def get_remote(self) -> typ.Optional[str]:
def has_remote(self) -> bool:
# pylint:disable=broad-except; Not sure how to anticipate all cases. # pylint:disable=broad-except; Not sure how to anticipate all cases.
try: try:
if self.name == 'git':
output = self('ls_branches')
for match in BRANCH_RE.finditer(output):
branch_info = match.groupdict()
if branch_info['is_current']:
return branch_info['remote']
output = self('show_remotes') output = self('show_remotes')
if output.strip() == "": if output.strip() == "":
return False return False
else: else:
return True return True
except Exception: except Exception:
return False return None
def fetch(self) -> None: def fetch(self) -> None:
"""Fetch updates from remote origin.""" """Fetch updates from remote origin."""
if self.has_remote: if self.get_remote():
self('fetch') self('fetch')
def status(self, required_files: typ.Set[str]) -> typ.List[str]: def status(self, required_files: typ.Set[str]) -> typ.List[str]:
@ -175,8 +197,9 @@ class VCSAPI:
def push(self, tag_name: str) -> None: def push(self, tag_name: str) -> None:
"""Push changes to origin.""" """Push changes to origin."""
if self.has_remote: remote = self.get_remote()
self('push_tag', tag=tag_name) if remote:
self('push_tag', tag=tag_name, remote=remote)
def __repr__(self) -> str: def __repr__(self) -> str:
"""Generate string representation.""" """Generate string representation."""