Skip to content

Commit b9e5184

Browse files
committed
Introduce .pre-commit-hooks.yaml as a replacement for hooks.yaml
1 parent b90a598 commit b9e5184

File tree

32 files changed

+107
-21
lines changed

32 files changed

+107
-21
lines changed

.pre-commit-hooks.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
- id: validate_config
2+
name: Validate Pre-Commit Config
3+
description: This validator validates a pre-commit hooks config file
4+
entry: pre-commit-validate-config
5+
language: python
6+
files: ^\.pre-commit-config\.yaml$
7+
- id: validate_manifest
8+
name: Validate Pre-Commit Manifest
9+
description: This validator validates a pre-commit hooks manifest file
10+
entry: pre-commit-validate-manifest
11+
language: python
12+
files: ^(\.pre-commit-hooks\.yaml|hooks\.yaml)$

hooks.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
description: This validator validates a pre-commit hooks config file
44
entry: pre-commit-validate-config
55
language: python
6-
files: ^\.pre-commit-config.yaml$
6+
files: ^\.pre-commit-config\.yaml$
77
- id: validate_manifest
88
name: Validate Pre-Commit Manifest
99
description: This validator validates a pre-commit hooks manifest file
1010
entry: pre-commit-validate-manifest
1111
language: python
12-
files: ^hooks.yaml$
12+
files: ^(\.pre-commit-hooks\.yaml|hooks\.yaml)$

pre_commit/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
CONFIG_FILE = '.pre-commit-config.yaml'
55

6-
MANIFEST_FILE = 'hooks.yaml'
6+
# In 0.12.0, the default file was changed to be namespaced
7+
MANIFEST_FILE = '.pre-commit-hooks.yaml'
8+
MANIFEST_FILE_LEGACY = 'hooks.yaml'
79

810
YAML_DUMP_KWARGS = {
911
'default_flow_style': False,

pre_commit/manifest.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import unicode_literals
22

3+
import logging
34
import os.path
45

56
from cached_property import cached_property
@@ -8,16 +9,33 @@
89
from pre_commit.clientlib.validate_manifest import load_manifest
910

1011

12+
logger = logging.getLogger('pre_commit')
13+
14+
1115
class Manifest(object):
12-
def __init__(self, repo_path_getter):
16+
def __init__(self, repo_path_getter, repo_url):
1317
self.repo_path_getter = repo_path_getter
18+
self.repo_url = repo_url
1419

1520
@cached_property
1621
def manifest_contents(self):
17-
manifest_path = os.path.join(
18-
self.repo_path_getter.repo_path, C.MANIFEST_FILE,
19-
)
20-
return load_manifest(manifest_path)
22+
repo_path = self.repo_path_getter.repo_path
23+
default_path = os.path.join(repo_path, C.MANIFEST_FILE)
24+
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:
28+
logger.warning(
29+
'{} uses legacy {} to provide hooks.\n'
30+
'In newer versions, this file is called {}\n'
31+
'This will work in this version of pre-commit but will be '
32+
'removed at a later time.\n'
33+
'If `pre-commit autoupdate` does not silence this warning '
34+
'consider making an issue / pull request.'.format(
35+
self.repo_url, C.MANIFEST_FILE_LEGACY, C.MANIFEST_FILE,
36+
)
37+
)
38+
return load_manifest(legacy_path)
2139

2240
@cached_property
2341
def hooks(self):

pre_commit/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def hooks(self):
104104

105105
@cached_property
106106
def manifest(self):
107-
return Manifest(self.repo_path_getter)
107+
return Manifest(self.repo_path_getter, self.repo_url)
108108

109109
@cached_property
110110
def cmd_runner(self):

testing/fixtures.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ def make_repo(tempdir_factory, repo_source):
3939

4040
@contextlib.contextmanager
4141
def modify_manifest(path):
42-
"""Modify the manifest yielded by this context to write to hooks.yaml."""
42+
"""Modify the manifest yielded by this context to write to
43+
.pre-commit-hooks.yaml.
44+
"""
4345
manifest_path = os.path.join(path, C.MANIFEST_FILE)
4446
manifest = ordered_load(io.open(manifest_path).read())
4547
yield manifest
4648
with io.open(manifest_path, 'w') as manifest_file:
4749
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
48-
cmd_output('git', 'commit', '-am', 'update hooks.yaml', cwd=path)
50+
cmd_output(
51+
'git', 'commit', '-am', 'update .pre-commit-hooks.yaml', cwd=path,
52+
)
4953

5054

5155
@contextlib.contextmanager
@@ -75,8 +79,11 @@ def config_with_local_hooks():
7579
))
7680

7781

78-
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True):
79-
manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
82+
def make_config_from_repo(
83+
repo_path, sha=None, hooks=None, check=True, legacy=False,
84+
):
85+
filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE
86+
manifest = load_manifest(os.path.join(repo_path, filename))
8087
config = OrderedDict((
8188
('repo', repo_path),
8289
('sha', sha or get_head_sha(repo_path)),

testing/resources/arbitrary_bytes_repo/hooks.yaml renamed to testing/resources/arbitrary_bytes_repo/.pre-commit-hooks.yaml

File renamed without changes.

testing/resources/arg_per_line_hooks_repo/hooks.yaml renamed to testing/resources/arg_per_line_hooks_repo/.pre-commit-hooks.yaml

File renamed without changes.
File renamed without changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
FROM cogniteev/echo
22

3-
CMD ["echo", "This is overwritten by the hooks.yaml 'entry'"]
3+
CMD ["echo", "This is overwritten by the .pre-commit-hooks.yaml 'entry'"]

0 commit comments

Comments
 (0)