Merge pull request #130 from pre-commit/filenames_with_spaces

Fix filenames with spaces in them.
This commit is contained in:
Ken Struys
2014-06-20 14:54:06 -07:00
8 changed files with 25 additions and 7 deletions

View File

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

View File

@@ -16,7 +16,7 @@ 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',
'xargs', '-0', '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'])),

View File

@@ -13,7 +13,7 @@ def install_environment(repo_cmd_runner, version='default'):
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
['xargs', '-0', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
# TODO: this is duplicated in pre_commit/languages/helpers.py
stdin=file_args_to_stdin(file_args),
retcode=None,

View File

@@ -15,7 +15,7 @@ def install_environment(repo_cmd_runner, version='default'):
def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run(
['xargs'] + shlex.split(hook['entry']) + hook['args'],
['xargs', '-0'] + shlex.split(hook['entry']) + hook['args'],
stdin=file_args_to_stdin(file_args),
retcode=None,
)

View File

@@ -0,0 +1,5 @@
#!/usr/bin/env bash
for i in "$@"; do
echo "arg: $i"
done

View File

@@ -0,0 +1,5 @@
- id: arg-per-line
name: Args per line hook
entry: bin/hook.sh
language: script
files: ''

View File

@@ -9,8 +9,8 @@ def test_file_args_to_stdin_empty():
def test_file_args_to_stdin_some():
assert file_args_to_stdin(['foo', 'bar']) == 'foo\nbar\n'
assert file_args_to_stdin(['foo', 'bar']) == 'foo\0bar\0'
def test_file_args_to_stdin_tuple():
assert file_args_to_stdin(('foo', 'bar')) == 'foo\nbar\n'
assert file_args_to_stdin(('foo', 'bar')) == 'foo\0bar\0'

View File

@@ -102,6 +102,14 @@ def test_run_a_script_hook(tmpdir_factory, store):
)
@pytest.mark.integration
def test_run_hook_with_spaced_args(tmpdir_factory, store):
_test_hook_repo(
tmpdir_factory, store, 'arg_per_line_hooks_repo',
'arg-per-line', ['foo bar', 'baz'], 'arg: foo bar\narg: baz\n',
)
@pytest.mark.integration
def test_pcre_hook_no_match(tmpdir_factory, store):
path = git_dir(tmpdir_factory)