diff options
author | Jeff King <peff@peff.net> | 2009-12-30 05:55:36 -0500 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2010-01-05 23:41:50 -0800 |
commit | f445644fd28d31a828731a618e9a9c79be89efd2 (patch) | |
tree | 19f58a2063483bbe12e84f606145f267fa5b2a40 /run-command.c | |
parent | ac0ba18df0c58decfb128336bab51a172c77abc0 (diff) | |
download | git-f445644fd28d31a828731a618e9a9c79be89efd2.tar.gz |
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>
Diffstat (limited to 'run-command.c')
-rw-r--r-- | run-command.c | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/run-command.c b/run-command.c index b2bdcfda3d..47ced570bd 100644 --- a/run-command.c +++ b/run-command.c @@ -28,15 +28,17 @@ static const char **prepare_shell_cmd(const char **argv) if (argc < 1) die("BUG: shell command is empty"); - nargv[nargc++] = "sh"; - nargv[nargc++] = "-c"; - - if (argc < 2) - nargv[nargc++] = argv[0]; - else { - struct strbuf arg0 = STRBUF_INIT; - strbuf_addf(&arg0, "%s \"$@\"", argv[0]); - nargv[nargc++] = strbuf_detach(&arg0, NULL); + if (strcspn(argv[0], "|&;<>()$`\\\"' \t\n*?[#~=%") != strlen(argv[0])) { + nargv[nargc++] = "sh"; + nargv[nargc++] = "-c"; + + if (argc < 2) + nargv[nargc++] = argv[0]; + else { + struct strbuf arg0 = STRBUF_INIT; + strbuf_addf(&arg0, "%s \"$@\"", argv[0]); + nargv[nargc++] = strbuf_detach(&arg0, NULL); + } } for (argc = 0; argv[argc]; argc++) |