Skip to content

Commit 96174de

Browse files
committed
Make hooks specify files. Optionally allow config to override manifest.
1 parent 0ec9020 commit 96174de

File tree

25 files changed

+81
-50
lines changed

25 files changed

+81
-50
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
- repo: git@github.com:pre-commit/pre-commit-hooks
2-
sha: 59f23b7c556ce1cf66eb6dc574e10d2b4274be4b
2+
sha: 7c003425b35fff516c0ee88f4040c8c208d474bd
33
hooks:
44
- id: trailing-whitespace
55
files: \.(js|rb|md|py|sh|txt|yaml|yml)$

example_hooks.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
- id: my_hook
1616
name: My Simple Hook
1717
description: This is my simple hook that does blah
18-
entry: my-simple-hook.py
18+
entry: my-simple-hook
1919
language: python
20-
expected_return_value: 0
20+
files: \.py$
2121
- id: my_grep_based_hook
2222
name: My Bash Based Hook
2323
description: This is a hook that uses grep to validate some stuff
2424
entry: ./my_grep_based_hook.sh
2525
language: script
26+
files: \.(py|sh)$
2627
expected_return_value: 1

example_pre-commit-config.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22
sha: cd74dc150c142c3be70b24eaf0b02cae9d235f37
33
hooks:
44
- id: pyflakes
5-
files: '\.py$'
65
- id: jslint
7-
files: '\.js$'
86
- id: trim_trailing_whitespace
9-
files: '\.py$'

hooks.yaml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
- id: validate_manifest
2-
name: Validate Pre-Commit Manifest
3-
description: This validator validates a pre-commit hooks manifest file
4-
entry: validate-manifest
5-
language: python
61
- id: validate_config
72
name: Validate Pre-Commit Config
83
description: This validator validates a pre-commit hooks config file
94
entry: validate-config
105
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: validate-manifest
11+
language: python
12+
files: ^hooks.yaml$

pre_commit/clientlib/validate_config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class InvalidConfigError(ValueError):
3232
'items': {'type': 'string'},
3333
},
3434
},
35-
'required': ['id', 'files'],
35+
'required': ['id'],
3636
}
3737
}
3838
},
@@ -55,7 +55,7 @@ def try_regex(repo, hook, value, field_name):
5555
def validate_config_extra(config):
5656
for repo in config:
5757
for hook in repo['hooks']:
58-
try_regex(repo, hook['id'], hook['files'], 'files')
58+
try_regex(repo, hook['id'], hook.get('files', ''), 'files')
5959
try_regex(repo, hook['id'], hook['exclude'], 'exclude')
6060

6161

pre_commit/clientlib/validate_manifest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ class InvalidManifestError(ValueError):
2121
'entry': {'type': 'string'},
2222
'language': {'type': 'string'},
2323
'language_version': {'type': 'string', 'default': 'default'},
24+
'files': {'type': 'string'},
2425
'expected_return_value': {'type': 'number', 'default': 0},
2526
},
26-
'required': ['id', 'name', 'entry', 'language'],
27+
'required': ['id', 'name', 'entry', 'language', 'files'],
2728
},
2829
}
2930

pre_commit/repository.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def languages(self):
3838
def hooks(self):
3939
# TODO: merging in manifest dicts is a smell imo
4040
return OrderedDict(
41-
(hook['id'], dict(hook, **self.manifest.hooks[hook['id']]))
41+
(hook['id'], dict(self.manifest.hooks[hook['id']], **hook))
4242
for hook in self.repo_config['hooks']
4343
)
4444

testing/resources/consumer_repo/.pre-commit-config.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@
22
sha: bec87f6c87284ea15dbcf7801810404c8036bab4
33
hooks:
44
- id: pyflakes
5-
files: \.py$
65
- id: debug-statements
7-
files: \.py$
86
- id: trailing-whitespace
9-
files: \.(py|sh|yaml)$
107
- id: name-tests-test
11-
files: tests/.+\.py$
128
- id: end-of-file-fixer
13-
files: \.(py|sh|yaml)$
149
- repo: git@github.com:pre-commit/pre-commit
1510
sha: c62c1a3b513ab9e057e85a5e950bd7c438371076
1611
hooks:
1712
- id: validate_manifest
18-
files: ^hooks.yaml$
1913
- id: validate_config
20-
files: ^\.pre-commit-config.yaml$

testing/resources/failing_hook_repo/hooks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
name: Failing hook
33
entry: bin/hook.sh
44
language: script
5+
files: .

testing/resources/manifest_without_foo.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
name: Bar
33
entry: bar
44
language: python
5+
files: \.py$

0 commit comments

Comments
 (0)