Skip to content

Commit 4d0c400

Browse files
committed
Add repo option to autoupdate
1 parent 0ec3b55 commit 4d0c400

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def _write_new_config_file(path, output):
106106
f.write(to_write)
107107

108108

109-
def autoupdate(runner, tags_only):
109+
def autoupdate(runner, tags_only, repo=None):
110110
"""Auto-update the pre-commit config to the latest versions of repos."""
111111
migrate_config(runner, quiet=True)
112112
retv = 0
@@ -116,6 +116,10 @@ def autoupdate(runner, tags_only):
116116
input_config = load_config(runner.config_file_path)
117117

118118
for repo_config in input_config['repos']:
119+
# Skip any repo_configs that aren't the specified repo
120+
if repo and repo != repo_config['repo']:
121+
continue
122+
119123
if is_local_repo(repo_config) or is_meta_repo(repo_config):
120124
output_repos.append(repo_config)
121125
continue

pre_commit/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ def main(argv=None):
167167
'tagged version (the default behavior).'
168168
),
169169
)
170+
autoupdate_parser.add_argument(
171+
'--repo', nargs=1, default=None,
172+
help=(
173+
'Repository to update the hooks of.'
174+
),
175+
)
170176

171177
migrate_config_parser = subparsers.add_parser(
172178
'migrate-config',
@@ -245,7 +251,11 @@ def main(argv=None):
245251
elif args.command == 'autoupdate':
246252
if args.tags_only:
247253
logger.warning('--tags-only is the default')
248-
return autoupdate(runner, tags_only=not args.bleeding_edge)
254+
return autoupdate(
255+
runner,
256+
tags_only=not args.bleeding_edge,
257+
repo=args.repo,
258+
)
249259
elif args.command == 'migrate-config':
250260
return migrate_config(runner)
251261
elif args.command == 'run':

tests/commands/autoupdate_test.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,11 @@ def test_autoupdate_out_of_date_repo(
114114
)
115115
write_config('.', config)
116116

117+
runner = Runner('.', C.CONFIG_FILE)
117118
before = open(C.CONFIG_FILE).read()
118-
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
119+
repo_name = 'file://{}'.format(out_of_date_repo.path)
120+
# It will update the repo, because the name matches
121+
ret = autoupdate(runner, tags_only=False, repo=repo_name)
119122
after = open(C.CONFIG_FILE).read()
120123
assert ret == 0
121124
assert before != after
@@ -124,6 +127,24 @@ def test_autoupdate_out_of_date_repo(
124127
assert out_of_date_repo.head_sha in after
125128

126129

130+
def test_autoupdate_out_of_date_repo_wrong_repo_name(
131+
out_of_date_repo, in_tmpdir, mock_out_store_directory,
132+
):
133+
# Write out the config
134+
config = make_config_from_repo(
135+
out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False,
136+
)
137+
write_config('.', config)
138+
139+
runner = Runner('.', C.CONFIG_FILE)
140+
before = open(C.CONFIG_FILE).read()
141+
# It will not update it, because the name doesn't match
142+
ret = autoupdate(runner, tags_only=False, repo='wrong_repo_name')
143+
after = open(C.CONFIG_FILE).read()
144+
assert ret == 0
145+
assert before == after
146+
147+
127148
def test_does_not_reformat(
128149
out_of_date_repo, mock_out_store_directory, in_tmpdir,
129150
):

0 commit comments

Comments
 (0)