Skip to content

Commit b97bb18

Browse files
authored
Merge pull request pre-commit#2312 from mblayman/multiple-tags
Pick a tag if multiple tags exist on a SHA.
2 parents a522507 + e8b46c1 commit b97bb18

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

pre_commit/commands/autoupdate.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def update(self, tags_only: bool, freeze: bool) -> RevInfo:
5959
except CalledProcessError:
6060
cmd = (*git_cmd, 'rev-parse', 'FETCH_HEAD')
6161
rev = cmd_output(*cmd, cwd=tmp)[1].strip()
62+
else:
63+
if tags_only:
64+
rev = git.get_best_candidate_tag(rev, tmp)
6265

6366
frozen = None
6467
if freeze:

pre_commit/git.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,18 @@ def check_for_cygwin_mismatch() -> None:
229229
f' - python {exe_type[is_cygwin_python]}\n'
230230
f' - git {exe_type[is_cygwin_git]}\n',
231231
)
232+
233+
234+
def get_best_candidate_tag(rev: str, git_repo: str) -> str:
235+
"""Get the best tag candidate.
236+
237+
Multiple tags can exist on a SHA. Sometimes a moving tag is attached
238+
to a version tag. Try to pick the tag that looks like a version.
239+
"""
240+
tags = cmd_output(
241+
'git', *NO_FS_MONITOR, 'tag', '--points-at', rev, cwd=git_repo,
242+
)[1].splitlines()
243+
for tag in tags:
244+
if '.' in tag:
245+
return tag
246+
return rev

tests/commands/autoupdate_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,24 @@ def test_rev_info_update_tags_only_does_not_pick_tip(tagged):
103103
assert new_info.rev == 'v1.2.3'
104104

105105

106+
def test_rev_info_update_tags_prefers_version_tag(tagged, out_of_date):
107+
cmd_output('git', 'tag', 'latest', cwd=out_of_date.path)
108+
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)
109+
info = RevInfo.from_config(config)
110+
new_info = info.update(tags_only=True, freeze=False)
111+
assert new_info.rev == 'v1.2.3'
112+
113+
114+
def test_rev_info_update_tags_non_version_tag(out_of_date):
115+
cmd_output('git', 'tag', 'latest', cwd=out_of_date.path)
116+
config = make_config_from_repo(
117+
out_of_date.path, rev=out_of_date.original_rev,
118+
)
119+
info = RevInfo.from_config(config)
120+
new_info = info.update(tags_only=True, freeze=False)
121+
assert new_info.rev == 'latest'
122+
123+
106124
def test_rev_info_update_freeze_tag(tagged):
107125
git_commit(cwd=tagged.path)
108126
config = make_config_from_repo(tagged.path, rev=tagged.original_rev)

0 commit comments

Comments
 (0)