Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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>
  • Loading branch information...
commit f445644fd28d31a828731a618e9a9c79be89efd2 1 parent ac0ba18
Jeff King authored December 30, 2009 gitster committed January 05, 2010

Showing 1 changed file with 11 additions and 9 deletions. Show diff stats Hide diff stats

  1. 20  run-command.c
20  run-command.c
@@ -28,15 +28,17 @@ static const char **prepare_shell_cmd(const char **argv)
28 28
 	if (argc < 1)
29 29
 		die("BUG: shell command is empty");
30 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
+		}
40 42
 	}
41 43
 
42 44
 	for (argc = 0; argv[argc]; argc++)

0 notes on commit f445644

Please sign in to comment.
Something went wrong with that request. Please try again.