diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index b4d559c8e..d5352e5e7 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -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) @@ -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: diff --git a/pre_commit/git.py b/pre_commit/git.py index 853f4b0d0..6fff8d2a9 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -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