Skip to content

Commit f445644

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ac0ba18 commit f445644

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

run-command.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,17 @@ static const char **prepare_shell_cmd(const char **argv)
2828
if (argc < 1)
2929
die("BUG: shell command is empty");
3030

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+
}
4042
}
4143

4244
for (argc = 0; argv[argc]; argc++)

0 commit comments

Comments
 (0)