From 4d0c400066d2a40fb3c9022c72e3be7ffbad0887 Mon Sep 17 00:00:00 2001 From: Kevin Hock Date: Mon, 6 Nov 2017 17:13:47 -0800 Subject: [PATCH 1/4] Add repo option to autoupdate --- pre_commit/commands/autoupdate.py | 6 +++++- pre_commit/main.py | 12 +++++++++++- tests/commands/autoupdate_test.py | 23 ++++++++++++++++++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index ca0ed5e2..ca8f9bb1 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -106,7 +106,7 @@ def _write_new_config_file(path, output): f.write(to_write) -def autoupdate(runner, tags_only): +def autoupdate(runner, tags_only, repo=None): """Auto-update the pre-commit config to the latest versions of repos.""" migrate_config(runner, quiet=True) retv = 0 @@ -116,6 +116,10 @@ def autoupdate(runner, tags_only): input_config = load_config(runner.config_file_path) for repo_config in input_config['repos']: + # Skip any repo_configs that aren't the specified repo + if repo and repo != repo_config['repo']: + continue + if is_local_repo(repo_config) or is_meta_repo(repo_config): output_repos.append(repo_config) continue diff --git a/pre_commit/main.py b/pre_commit/main.py index 1405203c..fb5dd291 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -167,6 +167,12 @@ def main(argv=None): 'tagged version (the default behavior).' ), ) + autoupdate_parser.add_argument( + '--repo', nargs=1, default=None, + help=( + 'Repository to update the hooks of.' + ), + ) migrate_config_parser = subparsers.add_parser( 'migrate-config', @@ -245,7 +251,11 @@ def main(argv=None): elif args.command == 'autoupdate': if args.tags_only: logger.warning('--tags-only is the default') - return autoupdate(runner, tags_only=not args.bleeding_edge) + return autoupdate( + runner, + tags_only=not args.bleeding_edge, + repo=args.repo, + ) elif args.command == 'migrate-config': return migrate_config(runner) elif args.command == 'run': diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 7119c6be..584400cd 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -114,8 +114,11 @@ def test_autoupdate_out_of_date_repo( ) write_config('.', config) + runner = Runner('.', C.CONFIG_FILE) before = open(C.CONFIG_FILE).read() - ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False) + repo_name = 'file://{}'.format(out_of_date_repo.path) + # It will update the repo, because the name matches + ret = autoupdate(runner, tags_only=False, repo=repo_name) after = open(C.CONFIG_FILE).read() assert ret == 0 assert before != after @@ -124,6 +127,24 @@ def test_autoupdate_out_of_date_repo( assert out_of_date_repo.head_sha in after +def test_autoupdate_out_of_date_repo_wrong_repo_name( + out_of_date_repo, in_tmpdir, mock_out_store_directory, +): + # Write out the config + config = make_config_from_repo( + out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False, + ) + write_config('.', config) + + runner = Runner('.', C.CONFIG_FILE) + before = open(C.CONFIG_FILE).read() + # It will not update it, because the name doesn't match + ret = autoupdate(runner, tags_only=False, repo='wrong_repo_name') + after = open(C.CONFIG_FILE).read() + assert ret == 0 + assert before == after + + def test_does_not_reformat( out_of_date_repo, mock_out_store_directory, in_tmpdir, ): From e5b8cb0f7050224a3d904210f46b46a5e875e54e Mon Sep 17 00:00:00 2001 From: Kevin Hock Date: Mon, 6 Nov 2017 17:50:24 -0800 Subject: [PATCH 2/4] Keep original test as is --- tests/commands/autoupdate_test.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 584400cd..53670ca2 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -114,6 +114,26 @@ def test_autoupdate_out_of_date_repo( ) write_config('.', config) + runner = Runner('.', C.CONFIG_FILE) + before = open(C.CONFIG_FILE).read() + # It will update the repo, because the name matches + ret = autoupdate(runner, tags_only=False) + after = open(C.CONFIG_FILE).read() + assert ret == 0 + assert before != after + # Make sure we don't add defaults + assert 'exclude' not in after + assert out_of_date_repo.head_sha in after + +def test_autoupdate_out_of_date_repo_with_correct_repo_name( + out_of_date_repo, in_tmpdir, mock_out_store_directory, +): + # Write out the config + config = make_config_from_repo( + out_of_date_repo.path, sha=out_of_date_repo.original_sha, check=False, + ) + write_config('.', config) + runner = Runner('.', C.CONFIG_FILE) before = open(C.CONFIG_FILE).read() repo_name = 'file://{}'.format(out_of_date_repo.path) @@ -126,8 +146,7 @@ def test_autoupdate_out_of_date_repo( assert 'exclude' not in after assert out_of_date_repo.head_sha in after - -def test_autoupdate_out_of_date_repo_wrong_repo_name( +def test_autoupdate_out_of_date_repo_with_wrong_repo_name( out_of_date_repo, in_tmpdir, mock_out_store_directory, ): # Write out the config @@ -144,7 +163,6 @@ def test_autoupdate_out_of_date_repo_wrong_repo_name( assert ret == 0 assert before == after - def test_does_not_reformat( out_of_date_repo, mock_out_store_directory, in_tmpdir, ): From 70e7d9c5c4bd71b91c5e6de256a731af214e8bd1 Mon Sep 17 00:00:00 2001 From: Kevin Hock Date: Mon, 6 Nov 2017 17:55:43 -0800 Subject: [PATCH 3/4] Keep original test as is, for real --- tests/commands/autoupdate_test.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index 53670ca2..e2618880 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -114,10 +114,8 @@ def test_autoupdate_out_of_date_repo( ) write_config('.', config) - runner = Runner('.', C.CONFIG_FILE) before = open(C.CONFIG_FILE).read() - # It will update the repo, because the name matches - ret = autoupdate(runner, tags_only=False) + ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False) after = open(C.CONFIG_FILE).read() assert ret == 0 assert before != after @@ -137,7 +135,6 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name( runner = Runner('.', C.CONFIG_FILE) before = open(C.CONFIG_FILE).read() repo_name = 'file://{}'.format(out_of_date_repo.path) - # It will update the repo, because the name matches ret = autoupdate(runner, tags_only=False, repo=repo_name) after = open(C.CONFIG_FILE).read() assert ret == 0 From fccb4e69350b8574ec34da5252b5f81da68e344b Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Mon, 6 Nov 2017 18:14:59 -0800 Subject: [PATCH 4/4] Minor fixes --- pre_commit/main.py | 5 +---- tests/commands/autoupdate_test.py | 5 +++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pre_commit/main.py b/pre_commit/main.py index fb5dd291..4c9202ad 100644 --- a/pre_commit/main.py +++ b/pre_commit/main.py @@ -168,10 +168,7 @@ def main(argv=None): ), ) autoupdate_parser.add_argument( - '--repo', nargs=1, default=None, - help=( - 'Repository to update the hooks of.' - ), + '--repo', help='Only update this repository.', ) migrate_config_parser = subparsers.add_parser( diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py index e2618880..c78af1fb 100644 --- a/tests/commands/autoupdate_test.py +++ b/tests/commands/autoupdate_test.py @@ -123,6 +123,7 @@ def test_autoupdate_out_of_date_repo( assert 'exclude' not in after assert out_of_date_repo.head_sha in after + def test_autoupdate_out_of_date_repo_with_correct_repo_name( out_of_date_repo, in_tmpdir, mock_out_store_directory, ): @@ -139,10 +140,9 @@ def test_autoupdate_out_of_date_repo_with_correct_repo_name( after = open(C.CONFIG_FILE).read() assert ret == 0 assert before != after - # Make sure we don't add defaults - assert 'exclude' not in after assert out_of_date_repo.head_sha in after + def test_autoupdate_out_of_date_repo_with_wrong_repo_name( out_of_date_repo, in_tmpdir, mock_out_store_directory, ): @@ -160,6 +160,7 @@ def test_autoupdate_out_of_date_repo_with_wrong_repo_name( assert ret == 0 assert before == after + def test_does_not_reformat( out_of_date_repo, mock_out_store_directory, in_tmpdir, ):