Add pcre type.

This commit is contained in:
Anthony Sottile
2014-06-16 21:11:00 -07:00
parent 38673941dc
commit 2cfd2818b5
11 changed files with 184 additions and 10 deletions

View File

@@ -1,6 +1,7 @@
from __future__ import unicode_literals
from pre_commit.languages import node
from pre_commit.languages import pcre
from pre_commit.languages import python
from pre_commit.languages import ruby
from pre_commit.languages import script
@@ -36,6 +37,7 @@ from pre_commit.languages import system
languages = {
'node': node,
'pcre': pcre,
'python': python,
'ruby': ruby,
'script': script,

View File

@@ -1,10 +1,14 @@
from __future__ import unicode_literals
def file_args_to_stdin(file_args):
return '\n'.join(list(file_args) + [''])
def run_hook(env, hook, file_args):
return env.run(
' '.join(['xargs', hook['entry']] + hook['args']),
stdin='\n'.join(list(file_args) + ['']),
stdin=file_args_to_stdin(file_args),
retcode=None,
)

View File

@@ -0,0 +1,27 @@
from __future__ import unicode_literals
from pre_commit.languages.helpers import file_args_to_stdin
from pre_commit.util import shell_escape
ENVIRONMENT_DIR = None
def install_environment(repo_cmd_runner, version='default'):
"""Installation for pcre type is a noop."""
raise AssertionError('Cannot install pcre repo.')
def run_hook(repo_cmd_runner, hook, file_args):
# For PCRE the entry is the regular expression to match
return repo_cmd_runner.run(
[
'xargs', 'sh', '-c',
# Grep usually returns 0 for matches, and nonzero for non-matches
# so we flip it here.
'! grep -H -n -P {0} $@'.format(shell_escape(hook['entry'])),
'--',
],
stdin=file_args_to_stdin(file_args),
retcode=None,
)

View File

@@ -1,16 +1,20 @@
from __future__ import unicode_literals
from pre_commit.languages.helpers import file_args_to_stdin
ENVIRONMENT_DIR = None
def install_environment(repo_cmd_runner, version='default'):
"""Installation for script type is a noop."""
raise AssertionError('Cannot install script repo.')
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
# TODO: this is duplicated in pre_commit/languages/helpers.py
stdin='\n'.join(list(file_args) + ['']),
stdin=file_args_to_stdin(file_args),
retcode=None,
)

View File

@@ -2,18 +2,20 @@ from __future__ import unicode_literals
import shlex
from pre_commit.languages.helpers import file_args_to_stdin
ENVIRONMENT_DIR = None
def install_environment(repo_cmd_runner, version='default'):
"""Installation for system type is a noop."""
raise AssertionError('Cannot install system repo.')
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs'] + shlex.split(hook['entry']) + hook['args'],
# TODO: this is duplicated in pre_commit/languages/helpers.py
stdin='\n'.join(list(file_args) + ['']),
stdin=file_args_to_stdin(file_args),
retcode=None,
)

View File

@@ -49,7 +49,10 @@ def clean_path_on_failure(path):
raise
# TODO: asottile.contextlib this with a forward port of nested
@contextlib.contextmanager
def noop_context():
yield
def shell_escape(arg):
return "'" + arg.replace("'", "'\"'\"'".strip()) + "'"