Skip to content

Commit 2ec7a34

Browse files
committed
Fix filenames with spaces in them.
1 parent 061ac81 commit 2ec7a34

File tree

8 files changed

+25
-7
lines changed

8 files changed

+25
-7
lines changed

pre_commit/languages/helpers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33

44
def file_args_to_stdin(file_args):
5-
return '\n'.join(list(file_args) + [''])
5+
return '\0'.join(list(file_args) + [''])
66

77

88
def run_hook(env, hook, file_args):
99
return env.run(
10-
' '.join(['xargs', hook['entry']] + hook['args']),
10+
' '.join(['xargs', '-0', hook['entry']] + hook['args']),
1111
stdin=file_args_to_stdin(file_args),
1212
retcode=None,
1313
)

pre_commit/languages/pcre.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run_hook(repo_cmd_runner, hook, file_args):
1616
# For PCRE the entry is the regular expression to match
1717
return repo_cmd_runner.run(
1818
[
19-
'xargs', 'sh', '-c',
19+
'xargs', '-0', 'sh', '-c',
2020
# Grep usually returns 0 for matches, and nonzero for non-matches
2121
# so we flip it here.
2222
'! grep -H -n -P {0} $@'.format(shell_escape(hook['entry'])),

pre_commit/languages/script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def install_environment(repo_cmd_runner, version='default'):
1313

1414
def run_hook(repo_cmd_runner, hook, file_args):
1515
return repo_cmd_runner.run(
16-
['xargs', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
16+
['xargs', '-0', '{{prefix}}{0}'.format(hook['entry'])] + hook['args'],
1717
# TODO: this is duplicated in pre_commit/languages/helpers.py
1818
stdin=file_args_to_stdin(file_args),
1919
retcode=None,

pre_commit/languages/system.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def install_environment(repo_cmd_runner, version='default'):
1515

1616
def run_hook(repo_cmd_runner, hook, file_args):
1717
return repo_cmd_runner.run(
18-
['xargs'] + shlex.split(hook['entry']) + hook['args'],
18+
['xargs', '-0'] + shlex.split(hook['entry']) + hook['args'],
1919
stdin=file_args_to_stdin(file_args),
2020
retcode=None,
2121
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
for i in "$@"; do
4+
echo "arg: $i"
5+
done
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: arg-per-line
2+
name: Args per line hook
3+
entry: bin/hook.sh
4+
language: script
5+
files: ''

tests/languages/helpers_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def test_file_args_to_stdin_empty():
99

1010

1111
def test_file_args_to_stdin_some():
12-
assert file_args_to_stdin(['foo', 'bar']) == 'foo\nbar\n'
12+
assert file_args_to_stdin(['foo', 'bar']) == 'foo\0bar\0'
1313

1414

1515
def test_file_args_to_stdin_tuple():
16-
assert file_args_to_stdin(('foo', 'bar')) == 'foo\nbar\n'
16+
assert file_args_to_stdin(('foo', 'bar')) == 'foo\0bar\0'

tests/repository_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ def test_run_a_script_hook(tmpdir_factory, store):
102102
)
103103

104104

105+
@pytest.mark.integration
106+
def test_run_hook_with_spaced_args(tmpdir_factory, store):
107+
_test_hook_repo(
108+
tmpdir_factory, store, 'arg_per_line_hooks_repo',
109+
'arg-per-line', ['foo bar', 'baz'], 'arg: foo bar\narg: baz\n',
110+
)
111+
112+
105113
@pytest.mark.integration
106114
def test_pcre_hook_no_match(tmpdir_factory, store):
107115
path = git_dir(tmpdir_factory)

0 commit comments

Comments
 (0)