fix usage without vcs repo

This commit is contained in:
Manuel Barkhau 2018-12-21 19:22:10 +01:00
parent 481a6625ae
commit 658ef57478

View file

@ -26,24 +26,26 @@ log = logging.getLogger("pycalver.vcs")
VCS_SUBCOMMANDS_BY_NAME = { VCS_SUBCOMMANDS_BY_NAME = {
'git': { 'git': {
'is_usable': "git rev-parse --git-dir", 'is_usable' : "git rev-parse --git-dir",
'fetch' : "git fetch", 'fetch' : "git fetch",
'ls_tags' : "git tag --list v*", 'ls_tags' : "git tag --list v*",
'status' : "git status --porcelain", 'status' : "git status --porcelain",
'add_path' : "git add --update {path}", 'add_path' : "git add --update {path}",
'commit' : "git commit --file {path}", 'commit' : "git commit --file {path}",
'tag' : "git tag --annotate {tag} --message {tag}", 'tag' : "git tag --annotate {tag} --message {tag}",
'push_tag' : "git push origin {tag}", 'push_tag' : "git push origin {tag}",
'show_remotes': "git config --get remote.origin.url",
}, },
'hg': { 'hg': {
'is_usable': "hg root", 'is_usable' : "hg root",
'fetch' : "hg pull", 'fetch' : "hg pull",
'ls_tags' : "hg tags", 'ls_tags' : "hg tags",
'status' : "hg status -mard", 'status' : "hg status -mard",
'add_path' : "hg add {path}", 'add_path' : "hg add {path}",
'commit' : "hg commit --logfile", 'commit' : "hg commit --logfile",
'tag' : "hg tag {tag} --message {tag}", 'tag' : "hg tag {tag} --message {tag}",
'push_tag' : "hg push {tag}", 'push_tag' : "hg push {tag}",
'show_remotes': "hg paths",
}, },
} }
@ -71,6 +73,9 @@ class VCS:
@property @property
def is_usable(self) -> bool: def is_usable(self) -> bool:
"""Detect availability of subcommand.""" """Detect availability of subcommand."""
if not os.path.exists(f".{self.name}"):
return False
cmd = self.subcommands['is_usable'].split() cmd = self.subcommands['is_usable'].split()
try: try:
@ -82,9 +87,20 @@ class VCS:
return False return False
raise raise
@property
def has_remote(self) -> bool:
try:
output = self('show_remotes')
if output.strip() == "":
return False
return True
except Exception:
return False
def fetch(self) -> None: def fetch(self) -> None:
"""Fetch updates from remote origin.""" """Fetch updates from remote origin."""
self('fetch') if self.has_remote:
self('fetch')
def status(self) -> typ.List[str]: def status(self) -> typ.List[str]:
"""Get status lines.""" """Get status lines."""
@ -130,7 +146,8 @@ class VCS:
def push(self, tag_name: str) -> None: def push(self, tag_name: str) -> None:
"""Push changes to origin.""" """Push changes to origin."""
self('push_tag', tag=tag_name) if self.has_remote:
self('push_tag', tag=tag_name)
def __repr__(self) -> str: def __repr__(self) -> str:
"""Generate string representation.""" """Generate string representation."""