Fix filenames with spaces in them.

This commit is contained in:
Anthony Sottile 2014-06-20 10:20:02 -07:00
parent 061ac81682
commit 2ec7a34035
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): 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): def run_hook(env, hook, file_args):
return env.run( return env.run(
' '.join(['xargs', hook['entry']] + hook['args']), ' '.join(['xargs', '-0', hook['entry']] + hook['args']),
stdin=file_args_to_stdin(file_args), stdin=file_args_to_stdin(file_args),
retcode=None, 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 # For PCRE the entry is the regular expression to match
return repo_cmd_runner.run( return repo_cmd_runner.run(
[ [
'xargs', 'sh', '-c', 'xargs', '-0', 'sh', '-c',
# Grep usually returns 0 for matches, and nonzero for non-matches # Grep usually returns 0 for matches, and nonzero for non-matches
# so we flip it here. # so we flip it here.
'! grep -H -n -P {0} $@'.format(shell_escape(hook['entry'])), '! 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): def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run( 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 # TODO: this is duplicated in pre_commit/languages/helpers.py
stdin=file_args_to_stdin(file_args), stdin=file_args_to_stdin(file_args),
retcode=None, 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): def run_hook(repo_cmd_runner, hook, file_args):
return repo_cmd_runner.run( 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), stdin=file_args_to_stdin(file_args),
retcode=None, 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(): 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(): 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 @pytest.mark.integration
def test_pcre_hook_no_match(tmpdir_factory, store): def test_pcre_hook_no_match(tmpdir_factory, store):
path = git_dir(tmpdir_factory) path = git_dir(tmpdir_factory)