Skip to content

Commit 94a1392

Browse files
committed
sub-process: use gentle handshake to avoid die() on startup failure
When the configured subprocess command contains shell metacharacters (such as a space), prepare_shell_cmd() wraps it in "sh -c <cmd>". The shell itself always starts successfully, so start_command() returns zero even if the tool inside does not exist. The subsequent handshake then reads from a dead pipe and calls die() via the non-gentle packet_read_line(), killing the parent process instead of letting it handle the error. Before this change, a missing filter process at a path containing spaces produces a confusing error: $ git -c filter.myfilter.process="/path with space/tool" \ -c filter.myfilter.required=true add file.txt /path with space/tool: line 1: /path: No such file or directory fatal: the remote end hung up unexpectedly After this change, the proper error is reported: $ git ... add file.txt /path with space/tool: line 1: /path: No such file or directory error: could not read from subprocess '/path with space/tool' error: initialization for subprocess '/path with space/tool' failed fatal: file.txt: clean filter 'myfilter' failed Switch the subprocess handshake from the dying packet_read_line() to packet_read_line_gently() so that a process that exits during startup produces an error return instead of killing the caller. This affects any subprocess consumer whose command path contains spaces. On Windows this routinely happens because programs live under "C:/Program Files/...", and MSYS2 path conversion can rewrite absolute paths to include that prefix. On POSIX it triggers whenever the configured path naturally contains a space or other metacharacter. convert.c (filter.<driver>.process, used by git-lfs and custom clean/smudge filters) is the primary affected consumer. Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
1 parent 29bd7ed commit 94a1392

2 files changed

Lines changed: 37 additions & 6 deletions

File tree

sub-process.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,24 @@ static int handshake_version(struct child_process *process,
132132
if (packet_flush_gently(process->in))
133133
return error("Could not write flush packet");
134134

135-
if (!(line = packet_read_line(process->out, NULL)) ||
136-
!skip_prefix(line, welcome_prefix, &p) ||
135+
if (packet_read_line_gently(process->out, NULL, &line) < 0)
136+
return error("could not read from subprocess '%s'",
137+
process->args.v[0]);
138+
if (!line || !skip_prefix(line, welcome_prefix, &p) ||
137139
strcmp(p, "-server"))
138140
return error("Unexpected line '%s', expected %s-server",
139141
line ? line : "<flush packet>", welcome_prefix);
140-
if (!(line = packet_read_line(process->out, NULL)) ||
141-
!skip_prefix(line, "version=", &p) ||
142+
if (packet_read_line_gently(process->out, NULL, &line) < 0)
143+
return error("could not read from subprocess '%s'",
144+
process->args.v[0]);
145+
if (!line || !skip_prefix(line, "version=", &p) ||
142146
strtol_i(p, 10, chosen_version))
143147
return error("Unexpected line '%s', expected version",
144148
line ? line : "<flush packet>");
145-
if ((line = packet_read_line(process->out, NULL)))
149+
if (packet_read_line_gently(process->out, NULL, &line) < 0)
150+
return error("could not read from subprocess '%s'",
151+
process->args.v[0]);
152+
if (line)
146153
return error("Unexpected line '%s', expected flush", line);
147154

148155
/* Check to make sure that the version received is supported */
@@ -171,8 +178,15 @@ static int handshake_capabilities(struct child_process *process,
171178
if (packet_flush_gently(process->in))
172179
return error("Could not write flush packet");
173180

174-
while ((line = packet_read_line(process->out, NULL))) {
181+
for (;;) {
175182
const char *p;
183+
int len = packet_read_line_gently(process->out, NULL, &line);
184+
185+
if (len < 0)
186+
return error("could not read capabilities from subprocess '%s'",
187+
process->args.v[0]);
188+
if (!line)
189+
break;
176190
if (!skip_prefix(line, "capability=", &p))
177191
continue;
178192

t/t0021-conversion.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,23 @@ test_expect_success 'invalid process filter must fail (and not hang!)' '
857857
)
858858
'
859859

860+
test_expect_success 'missing process filter with space in path does not die' '
861+
test_config_global filter.protocol.process "/non existent/tool" &&
862+
test_config_global filter.protocol.required true &&
863+
rm -rf repo &&
864+
mkdir repo &&
865+
(
866+
cd repo &&
867+
git init &&
868+
869+
echo "*.r filter=protocol" >.gitattributes &&
870+
871+
cp "$TEST_ROOT/test.o" test.r &&
872+
test_must_fail git add . 2>git-stderr.log &&
873+
test_grep "clean filter.*protocol.*failed" git-stderr.log
874+
)
875+
'
876+
860877
test_expect_success 'delayed checkout in process filter' '
861878
test_config_global filter.a.process "test-tool rot13-filter --log=a.log clean smudge delay" &&
862879
test_config_global filter.a.required true &&

0 commit comments

Comments
 (0)