@matheusbsbs / matheus-bagliano / commits / f445644fd2
Fork 0

run-command: optimize out useless shell calls

If there are no metacharacters in the program to be run, we can just skip running the shell entirely and directly exec the program. The metacharacter test is pulled verbatim from launch_editor, which already implements this optimization. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Dec 30, 2009 at 05:55 UTC f445644fd28d31a828731a618e9a9c79be89efd2
1 file changed +11 -9
run-command.c
+11 -9
@@ -28,15 +28,17 @@ static const char **prepare_shell_cmd(const char **argv)
28 if (argc < 1)
29 die("BUG: shell command is empty");
30
31 - nargv[nargc++] = "sh";
32 - nargv[nargc++] = "-c";
33 -
34 - if (argc < 2)
35 - nargv[nargc++] = argv[0];
36 - else {
37 - struct strbuf arg0 = STRBUF_INIT;
38 - strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
39 - nargv[nargc++] = strbuf_detach(&arg0, NULL);
31 + if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) {
32 + nargv[nargc++] = "sh";
33 + nargv[nargc++] = "-c";
34 +
35 + if (argc < 2)
36 + nargv[nargc++] = argv[0];
37 + else {
38 + struct strbuf arg0 = STRBUF_INIT;
39 + strbuf_addf(&arg0, "%s \"$@\"", argv[0]);
40 + nargv[nargc++] = strbuf_detach(&arg0, NULL);
41 + }
42 }
43
44 for (argc = 0; argv[argc]; argc++)