]> git.ipfire.org Git - thirdparty/git.git/commitdiff
sub-process: use gentle handshake to avoid die() on startup failure
authorMichael Montalbo <mmontalbo@gmail.com>
Mon, 1 Jun 2026 21:20:47 +0000 (21:20 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Jun 2026 22:48:57 +0000 (07:48 +0900)
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 greeting 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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
sub-process.c
t/t0021-conversion.sh

index 83bf0a0e82e56d5949dd439a6b51f969004eb1e7..2d5c965169727b77a1e15c879d1dc9d859bddb0d 100644 (file)
@@ -132,17 +132,24 @@ static int handshake_version(struct child_process *process,
        if (packet_flush_gently(process->in))
                return error("Could not write flush packet");
 
-       if (!(line = packet_read_line(process->out, NULL)) ||
-           !skip_prefix(line, welcome_prefix, &p) ||
+       if (packet_read_line_gently(process->out, NULL, &line) < 0)
+               return error("could not read greeting from subprocess '%s'",
+                            process->args.v[0]);
+       if (!line || !skip_prefix(line, welcome_prefix, &p) ||
            strcmp(p, "-server"))
                return error("Unexpected line '%s', expected %s-server",
                             line ? line : "<flush packet>", welcome_prefix);
-       if (!(line = packet_read_line(process->out, NULL)) ||
-           !skip_prefix(line, "version=", &p) ||
+       if (packet_read_line_gently(process->out, NULL, &line) < 0)
+               return error("could not read version from subprocess '%s'",
+                            process->args.v[0]);
+       if (!line || !skip_prefix(line, "version=", &p) ||
            strtol_i(p, 10, chosen_version))
                return error("Unexpected line '%s', expected version",
                             line ? line : "<flush packet>");
-       if ((line = packet_read_line(process->out, NULL)))
+       if (packet_read_line_gently(process->out, NULL, &line) < 0)
+               return error("could not read version flush from subprocess '%s'",
+                            process->args.v[0]);
+       if (line)
                return error("Unexpected line '%s', expected flush", line);
 
        /* Check to make sure that the version received is supported */
@@ -171,8 +178,15 @@ static int handshake_capabilities(struct child_process *process,
        if (packet_flush_gently(process->in))
                return error("Could not write flush packet");
 
-       while ((line = packet_read_line(process->out, NULL))) {
+       for (;;) {
                const char *p;
+               int len = packet_read_line_gently(process->out, NULL, &line);
+
+               if (len < 0)
+                       return error("could not read capabilities from subprocess '%s'",
+                                    process->args.v[0]);
+               if (!line)
+                       break;
                if (!skip_prefix(line, "capability=", &p))
                        continue;
 
index f0d50d769e9fc56f6147e9cc5421ba7408eb2fe6..033b00a364ee7a854c8ec6d2077bb2dfe0683a43 100755 (executable)
@@ -857,6 +857,23 @@ test_expect_success 'invalid process filter must fail (and not hang!)' '
        )
 '
 
+test_expect_success 'missing process filter with space in path does not die' '
+       test_config_global filter.protocol.process "/non existent/tool" &&
+       test_config_global filter.protocol.required true &&
+       rm -rf repo &&
+       mkdir repo &&
+       (
+               cd repo &&
+               git init &&
+
+               echo "*.r filter=protocol" >.gitattributes &&
+
+               cp "$TEST_ROOT/test.o" test.r &&
+               test_must_fail git add . 2>git-stderr.log &&
+               test_grep "clean filter.*protocol.*failed" git-stderr.log
+       )
+'
+
 test_expect_success 'delayed checkout in process filter' '
        test_config_global filter.a.process "test-tool rot13-filter --log=a.log clean smudge delay" &&
        test_config_global filter.a.required true &&