Skip to content

Commit c418f2b

Browse files
committed
Implement no-dependency system and script hook types. Closes #39.
1 parent 02660f7 commit c418f2b

File tree

14 files changed

+86
-52
lines changed

14 files changed

+86
-52
lines changed

example_hooks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
name: My Bash Based Hook
2424
description: This is a hook that uses grep to validate some stuff
2525
entry: ./my_grep_based_hook.sh
26+
language: script
2627
expected_return_value: 1

pre_commit/clientlib/validate_manifest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import pre_commit.constants as C
88
from pre_commit.clientlib.validate_base import get_validator
9+
from pre_commit.languages.all import all_languages
910
from pre_commit.util import entry
1011

1112

@@ -25,7 +26,7 @@ class InvalidManifestError(ValueError): pass
2526
'language': {'type': 'string'},
2627
'expected_return_value': {'type': 'number'},
2728
},
28-
'required': ['id', 'name', 'entry'],
29+
'required': ['id', 'name', 'entry', 'language'],
2930
},
3031
}
3132

@@ -35,13 +36,13 @@ def additional_manifest_check(obj):
3536
language = hook_config.get('language')
3637

3738
if language is not None and not any(
38-
language.startswith(lang) for lang in C.SUPPORTED_LANGUAGES
39+
language.startswith(lang) for lang in all_languages
3940
):
4041
raise InvalidManifestError(
4142
'Expected language {0} for {1} to start with one of {2!r}'.format(
4243
hook_config['id'],
4344
hook_config['language'],
44-
C.SUPPORTED_LANGUAGES,
45+
all_languages,
4546
)
4647
)
4748

pre_commit/constants.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,6 @@
55

66
MANIFEST_FILE = 'hooks.yaml'
77

8-
SUPPORTED_LANGUAGES = set([
9-
'python',
10-
'ruby',
11-
'node',
12-
])
13-
14-
158
YAML_DUMP_KWARGS = {
169
'default_flow_style': False,
1710
'indent': 4,

pre_commit/languages/all.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pre_commit.languages import node
33
from pre_commit.languages import python
44
from pre_commit.languages import ruby
5+
from pre_commit.languages import script
6+
from pre_commit.languages import system
57

68
# A language implements the following two functions in its module:
79
#
@@ -29,4 +31,9 @@
2931
'node': node,
3032
'python': python,
3133
'ruby': ruby,
34+
'script': script,
35+
'system': system,
3236
}
37+
38+
39+
all_languages = languages.keys()

pre_commit/languages/script.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def install_environment(repo_cmd_runner):
3+
"""Installation for script type is a noop."""
4+
pass
5+
6+
7+
def run_hook(repo_cmd_runner, hook, file_args):
8+
return repo_cmd_runner.run(
9+
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook.get('args', []),
10+
# TODO: this is duplicated in pre_commit/languages/helpers.py
11+
stdin='\n'.join(list(file_args) + ['']),
12+
retcode=None,
13+
)

pre_commit/languages/system.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def install_environment(repo_cmd_runner):
3+
"""Installation for system type is a noop."""
4+
pass
5+
6+
7+
def run_hook(repo_cmd_runner, hook, file_args):
8+
return repo_cmd_runner.run(
9+
['xargs', hook['entry']] + hook.get('args', []),
10+
# TODO: this is duplicated in pre_commit/languages/helpers.py
11+
stdin='\n'.join(list(file_args) + ['']),
12+
retcode=None,
13+
)

pre_commit/repository.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ def install(self, cmd_runner):
8282
"""
8383
self.require_created()
8484
repo_cmd_runner = self.get_cmd_runner(cmd_runner)
85-
for language in C.SUPPORTED_LANGUAGES:
86-
if language in self.languages:
87-
languages[language].install_environment(repo_cmd_runner)
85+
for language in self.languages:
86+
languages[language].install_environment(repo_cmd_runner)
8887

8988
@contextlib.contextmanager
9089
def in_checkout(self):
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
- id: prints_cwd
22
name: Prints Cwd
3-
entry: prints_cwd
4-
language: python
3+
entry: pwd
4+
language: system
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env bash
2+
3+
echo $@
4+
echo 'Hello World'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- id: bash_hook
2+
name: Bash hook
3+
entry: bin/hook.sh
4+
language: script

0 commit comments

Comments
 (0)