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

Skip to content

Commit cf1fa70

Browse files
authored
Merge pull request #480 from pre-commit/futureproof
Make autoupdate slightly more future proof
2 parents 308f2cb + 8d589a5 commit cf1fa70

3 files changed

Lines changed: 33 additions & 4 deletions

File tree

pre_commit/commands/autoupdate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def _update_repo(repo_config, runner, tags_only):
5454
new_repo = Repository.create(new_config, runner.store)
5555

5656
# See if any of our hooks were deleted with the new commits
57-
hooks = {hook_id for hook_id, _ in repo.hooks}
57+
hooks = {hook['id'] for hook in repo.repo_config['hooks']}
5858
hooks_missing = hooks - (hooks & set(new_repo.manifest.hooks))
5959
if hooks_missing:
6060
raise RepositoryCannotBeUpdatedError(

pre_commit/manifest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ def manifest_contents(self):
2222
repo_path = self.repo_path_getter.repo_path
2323
default_path = os.path.join(repo_path, C.MANIFEST_FILE)
2424
legacy_path = os.path.join(repo_path, C.MANIFEST_FILE_LEGACY)
25-
if os.path.exists(default_path):
26-
return load_manifest(default_path)
27-
else:
25+
if os.path.exists(legacy_path) and not os.path.exists(default_path):
2826
logger.warning(
2927
'{} uses legacy {} to provide hooks.\n'
3028
'In newer versions, this file is called {}\n'
@@ -36,6 +34,8 @@ def manifest_contents(self):
3634
)
3735
)
3836
return load_manifest(legacy_path)
37+
else:
38+
return load_manifest(default_path)
3939

4040
@cached_property
4141
def hooks(self):

tests/commands/autoupdate_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ def test_autoupdate_up_to_date_repo(
5151
assert before == after
5252

5353

54+
def test_autoupdate_old_revision_broken(
55+
tempdir_factory, in_tmpdir, mock_out_store_directory,
56+
):
57+
"""In $FUTURE_VERSION, hooks.yaml will no longer be supported. This
58+
asserts that when that day comes, pre-commit will be able to autoupdate
59+
despite not being able to read hooks.yaml in that repository.
60+
"""
61+
path = make_repo(tempdir_factory, 'python_hooks_repo')
62+
config = make_config_from_repo(path, check=False)
63+
64+
with cwd(path):
65+
cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml')
66+
cmd_output('git', 'commit', '-m', 'simulate old repo')
67+
# Assume this is the revision the user's old repository was at
68+
rev = get_head_sha(path)
69+
cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE)
70+
cmd_output('git', 'commit', '-m', 'move hooks file')
71+
update_rev = get_head_sha(path)
72+
73+
config['sha'] = rev
74+
write_config('.', config)
75+
before = open(C.CONFIG_FILE).read()
76+
ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False)
77+
after = open(C.CONFIG_FILE).read()
78+
assert ret == 0
79+
assert before != after
80+
assert update_rev in after
81+
82+
5483
@pytest.yield_fixture
5584
def out_of_date_repo(tempdir_factory):
5685
path = make_repo(tempdir_factory, 'python_hooks_repo')

0 commit comments

Comments
 (0)