Skip to content

Commit

Permalink
Fix more review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Mar 31, 2022
1 parent db80394 commit f16ac2b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
19 changes: 3 additions & 16 deletions pre_commit/commands/autoupdate.py
Expand Up @@ -33,20 +33,6 @@ class RevInfo(NamedTuple):
def from_config(cls, config: dict[str, Any]) -> RevInfo:
return cls(config['repo'], config['rev'], None)

def get_best_candidate_tag(self, rev: str, git_repo: str) -> str:
"""Get the best tag candidate.
Multiple tags can exist on a SHA. Sometimes a moving tag is attached
to a version tag. Try to pick the tag that looks like a version.
"""
tags = cmd_output(
'git', *git.NO_FS_MONITOR, 'tag', '--points-at', rev, cwd=git_repo,
)[1].split()
for tag in tags:
if '.' in tag:
return tag
return rev

def update(self, tags_only: bool, freeze: bool) -> RevInfo:
git_cmd = ('git', *git.NO_FS_MONITOR)

Expand All @@ -70,11 +56,12 @@ def update(self, tags_only: bool, freeze: bool) -> RevInfo:

try:
rev = cmd_output(*tag_cmd, cwd=tmp)[1].strip()
if tags_only:
rev = self.get_best_candidate_tag(rev, tmp)
except CalledProcessError:
cmd = (*git_cmd, 'rev-parse', 'FETCH_HEAD')
rev = cmd_output(*cmd, cwd=tmp)[1].strip()
else:
if tags_only:
rev = git.get_best_candidate_tag(rev, tmp)

frozen = None
if freeze:
Expand Down
15 changes: 15 additions & 0 deletions pre_commit/git.py
Expand Up @@ -229,3 +229,18 @@ def check_for_cygwin_mismatch() -> None:
f' - python {exe_type[is_cygwin_python]}\n'
f' - git {exe_type[is_cygwin_git]}\n',
)


def get_best_candidate_tag(rev: str, git_repo: str) -> str:
"""Get the best tag candidate.
Multiple tags can exist on a SHA. Sometimes a moving tag is attached
to a version tag. Try to pick the tag that looks like a version.
"""
tags = cmd_output(
'git', *NO_FS_MONITOR, 'tag', '--points-at', rev, cwd=git_repo,
)[1].splitlines()
for tag in tags:
if '.' in tag:
return tag
return rev

0 comments on commit f16ac2b

Please sign in to comment.