Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 758faa4

Browse files
committed
Autoupdate to tags when available
1 parent d845ec6 commit 758faa4

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pre_commit.logging_handler import LoggingHandler
1616
from pre_commit.ordereddict import OrderedDict
1717
from pre_commit.repository import Repository
18+
from pre_commit.util import CalledProcessError
1819
from pre_commit.util import cmd_output
1920
from pre_commit.util import cwd
2021

@@ -38,15 +39,20 @@ def _update_repository(repo_config, runner):
3839

3940
with cwd(repo.repo_path_getter.repo_path):
4041
cmd_output('git', 'fetch')
41-
head_sha = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
42+
try:
43+
rev = cmd_output(
44+
'git', 'describe', 'origin/master', '--tags', '--exact',
45+
)[1].strip()
46+
except CalledProcessError:
47+
rev = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
4248

4349
# Don't bother trying to update if our sha is the same
44-
if head_sha == repo_config['sha']:
50+
if rev == repo_config['sha']:
4551
return repo_config
4652

4753
# Construct a new config with the head sha
4854
new_config = OrderedDict(repo_config)
49-
new_config['sha'] = head_sha
55+
new_config['sha'] = rev
5056
new_repo = Repository.create(new_config, runner.store)
5157

5258
# See if any of our hooks were deleted with the new commits

tests/commands/autoupdate_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ def test_autoupdate_out_of_date_repo(
9696
assert out_of_date_repo.head_sha in after
9797

9898

99+
@pytest.yield_fixture
100+
def tagged_repo(out_of_date_repo):
101+
with cwd(out_of_date_repo.path):
102+
cmd_output('git', 'tag', 'v1.2.3')
103+
yield out_of_date_repo
104+
105+
106+
def test_autoupdate_tagged_repo(
107+
tagged_repo, in_tmpdir, mock_out_store_directory,
108+
):
109+
config = make_config_from_repo(
110+
tagged_repo.path, sha=tagged_repo.original_sha,
111+
)
112+
write_config('.', config)
113+
114+
ret = autoupdate(Runner('.'))
115+
assert ret == 0
116+
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
117+
118+
99119
@pytest.yield_fixture
100120
def hook_disappearing_repo(tempdir_factory):
101121
path = make_repo(tempdir_factory, 'python_hooks_repo')

0 commit comments

Comments
 (0)