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

Skip to content

Commit 583bc57

Browse files
authored
Merge pull request #476 from pre-commit/tags_only
Add a --tags-only option to autoupdate
2 parents 209dc07 + 52cd423 commit 583bc57

3 files changed

Lines changed: 44 additions & 20 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RepositoryCannotBeUpdatedError(RuntimeError):
2121
pass
2222

2323

24-
def _update_repository(repo_config, runner):
24+
def _update_repo(repo_config, runner, tags_only):
2525
"""Updates a repository to the tip of `master`. If the repository cannot
2626
be updated because a hook that is configured does not exist in `master`,
2727
this raises a RepositoryCannotBeUpdatedError
@@ -33,10 +33,13 @@ def _update_repository(repo_config, runner):
3333

3434
with cwd(repo.repo_path_getter.repo_path):
3535
cmd_output('git', 'fetch')
36+
tag_cmd = ('git', 'describe', 'origin/master', '--tags')
37+
if tags_only:
38+
tag_cmd += ('--abbrev=0',)
39+
else:
40+
tag_cmd += ('--exact',)
3641
try:
37-
rev = cmd_output(
38-
'git', 'describe', 'origin/master', '--tags', '--exact',
39-
)[1].strip()
42+
rev = cmd_output(*tag_cmd)[1].strip()
4043
except CalledProcessError:
4144
rev = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
4245

@@ -61,7 +64,7 @@ def _update_repository(repo_config, runner):
6164
return new_config
6265

6366

64-
def autoupdate(runner):
67+
def autoupdate(runner, tags_only):
6568
"""Auto-update the pre-commit config to the latest versions of repos."""
6669
retv = 0
6770
output_configs = []
@@ -78,7 +81,7 @@ def autoupdate(runner):
7881
continue
7982
output.write('Updating {}...'.format(repo_config['repo']))
8083
try:
81-
new_repo_config = _update_repository(repo_config, runner)
84+
new_repo_config = _update_repo(repo_config, runner, tags_only)
8285
except RepositoryCannotBeUpdatedError as error:
8386
output.write_line(error.args[0])
8487
output_configs.append(repo_config)

pre_commit/main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ def main(argv=None):
111111
)
112112
_add_color_option(autoupdate_parser)
113113
_add_config_option(autoupdate_parser)
114+
autoupdate_parser.add_argument(
115+
'--tags-only', action='store_true', help='Update to tags only.',
116+
)
114117

115118
run_parser = subparsers.add_parser('run', help='Run hooks.')
116119
_add_color_option(run_parser)
@@ -190,7 +193,7 @@ def main(argv=None):
190193
elif args.command == 'clean':
191194
return clean(runner)
192195
elif args.command == 'autoupdate':
193-
return autoupdate(runner)
196+
return autoupdate(runner, args.tags_only)
194197
elif args.command == 'run':
195198
return run(runner, args)
196199
else:

tests/commands/autoupdate_test.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pre_commit.constants as C
88
from pre_commit.clientlib.validate_config import load_config
9-
from pre_commit.commands.autoupdate import _update_repository
9+
from pre_commit.commands.autoupdate import _update_repo
1010
from pre_commit.commands.autoupdate import autoupdate
1111
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
1212
from pre_commit.ordereddict import OrderedDict
@@ -32,7 +32,7 @@ def up_to_date_repo(tempdir_factory):
3232
def test_up_to_date_repo(up_to_date_repo, runner_with_mocked_store):
3333
config = make_config_from_repo(up_to_date_repo)
3434
input_sha = config['sha']
35-
ret = _update_repository(config, runner_with_mocked_store)
35+
ret = _update_repo(config, runner_with_mocked_store, tags_only=False)
3636
assert ret['sha'] == input_sha
3737

3838

@@ -45,8 +45,7 @@ def test_autoupdate_up_to_date_repo(
4545

4646
before = open(C.CONFIG_FILE).read()
4747
assert '^$' not in before
48-
runner = Runner('.', C.CONFIG_FILE)
49-
ret = autoupdate(runner)
48+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
5049
after = open(C.CONFIG_FILE).read()
5150
assert ret == 0
5251
assert before == after
@@ -71,7 +70,7 @@ def test_out_of_date_repo(out_of_date_repo, runner_with_mocked_store):
7170
config = make_config_from_repo(
7271
out_of_date_repo.path, sha=out_of_date_repo.original_sha,
7372
)
74-
ret = _update_repository(config, runner_with_mocked_store)
73+
ret = _update_repo(config, runner_with_mocked_store, tags_only=False)
7574
assert ret['sha'] != out_of_date_repo.original_sha
7675
assert ret['sha'] == out_of_date_repo.head_sha
7776

@@ -86,8 +85,7 @@ def test_autoupdate_out_of_date_repo(
8685
write_config('.', config)
8786

8887
before = open(C.CONFIG_FILE).read()
89-
runner = Runner('.', C.CONFIG_FILE)
90-
ret = autoupdate(runner)
88+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
9189
after = open(C.CONFIG_FILE).read()
9290
assert ret == 0
9391
assert before != after
@@ -111,7 +109,28 @@ def test_autoupdate_tagged_repo(
111109
)
112110
write_config('.', config)
113111

114-
ret = autoupdate(Runner('.', C.CONFIG_FILE))
112+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
113+
assert ret == 0
114+
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
115+
116+
117+
@pytest.yield_fixture
118+
def tagged_repo_with_more_commits(tagged_repo):
119+
with cwd(tagged_repo.path):
120+
cmd_output('git', 'commit', '--allow-empty', '-m', 'commit!')
121+
yield tagged_repo
122+
123+
124+
def test_autoupdate_tags_only(
125+
tagged_repo_with_more_commits, in_tmpdir, mock_out_store_directory,
126+
):
127+
config = make_config_from_repo(
128+
tagged_repo_with_more_commits.path,
129+
sha=tagged_repo_with_more_commits.original_sha,
130+
)
131+
write_config('.', config)
132+
133+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=True)
115134
assert ret == 0
116135
assert 'v1.2.3' in open(C.CONFIG_FILE).read()
117136

@@ -141,7 +160,7 @@ def test_hook_disppearing_repo_raises(
141160
hooks=[OrderedDict((('id', 'foo'),))],
142161
)
143162
with pytest.raises(RepositoryCannotBeUpdatedError):
144-
_update_repository(config, runner_with_mocked_store)
163+
_update_repo(config, runner_with_mocked_store, tags_only=False)
145164

146165

147166
def test_autoupdate_hook_disappearing_repo(
@@ -156,8 +175,7 @@ def test_autoupdate_hook_disappearing_repo(
156175
write_config('.', config)
157176

158177
before = open(C.CONFIG_FILE).read()
159-
runner = Runner('.', C.CONFIG_FILE)
160-
ret = autoupdate(runner)
178+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
161179
after = open(C.CONFIG_FILE).read()
162180
assert ret == 1
163181
assert before == after
@@ -168,7 +186,7 @@ def test_autoupdate_local_hooks(tempdir_factory):
168186
config = config_with_local_hooks()
169187
path = add_config_to_repo(git_path, config)
170188
runner = Runner(path, C.CONFIG_FILE)
171-
assert autoupdate(runner) == 0
189+
assert autoupdate(runner, tags_only=False) == 0
172190
new_config_writen = load_config(runner.config_file_path)
173191
assert len(new_config_writen) == 1
174192
assert new_config_writen[0] == config
@@ -184,7 +202,7 @@ def test_autoupdate_local_hooks_with_out_of_date_repo(
184202
config = [local_config, stale_config]
185203
write_config('.', config)
186204
runner = Runner('.', C.CONFIG_FILE)
187-
assert autoupdate(runner) == 0
205+
assert autoupdate(runner, tags_only=False) == 0
188206
new_config_writen = load_config(runner.config_file_path)
189207
assert len(new_config_writen) == 2
190208
assert new_config_writen[0] == local_config

0 commit comments

Comments
 (0)