use only the $PATH for exec'ing git commands
We need to correctly set up $PATH for non-c based git commands. Since we already do this, we can just use that $PATH and execvp, instead of looping over the paths with execve. This patch adds a setup_path() function to exec_cmd.c, which sets the $PATH order correctly for our search order. execv_git_cmd() is stripped down to setting up argv and calling execvp(). git.c's main() only only needs to call setup_path(). Signed-off-by: Scott R Parish <srp@srparish.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Scott R Parish committed
Oct 28, 2007 at 04:17 UTC
511707d42b3b3e57d9623493092590546ffeae80
3 files changed
+61
-105
exec_cmd.c
+53
-69
@@ -29,85 +29,69 @@ const char *git_exec_path(void)
29
return builtin_exec_path;
30
}
31
32
+static void add_path(struct strbuf *out, const char *path)
33
+{
34
+ if (path && *path) {
35
+ if (is_absolute_path(path))
36
+ strbuf_addstr(out, path);
37
+ else
38
+ strbuf_addstr(out, make_absolute_path(path));
39
+
40
+ strbuf_addch(out, ':');
41
+ }
42
+}
43
+
44
+void setup_path(const char *cmd_path)
45
+{
46
+ const char *old_path = getenv("PATH");
47
+ struct strbuf new_path;
48
+
49
+ strbuf_init(&new_path, 0);
50
+
51
+ add_path(&new_path, argv_exec_path);
52
+ add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
53
+ add_path(&new_path, builtin_exec_path);
54
+ add_path(&new_path, cmd_path);
55
+
56
+ if (old_path)
57
+ strbuf_addstr(&new_path, old_path);
58
+ else
59
+ strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
60
+
61
+ setenv("PATH", new_path.buf, 1);
62
+
63
+ strbuf_release(&new_path);
64
+}
65
66
int execv_git_cmd(const char **argv)
67
{
35
- char git_command[PATH_MAX + 1];
36
- int i;
37
- const char *paths[] = { argv_exec_path,
38
- getenv(EXEC_PATH_ENVIRONMENT),
39
- builtin_exec_path };
40
-
41
- for (i = 0; i < ARRAY_SIZE(paths); ++i) {
42
- size_t len;
43
- int rc;
44
- const char *exec_dir = paths[i];
45
- const char *tmp;
46
-
47
- if (!exec_dir || !*exec_dir) continue;
48
-
49
- if (*exec_dir != '/') {
50
- if (!getcwd(git_command, sizeof(git_command))) {
51
- fprintf(stderr, "git: cannot determine "
52
- "current directory: %s\n",
53
- strerror(errno));
54
- break;
55
- }
56
- len = strlen(git_command);
57
-
58
- /* Trivial cleanup */
59
- while (!prefixcmp(exec_dir, "./")) {
60
- exec_dir += 2;
61
- while (*exec_dir == '/')
62
- exec_dir++;
63
- }
64
-
65
- rc = snprintf(git_command + len,
66
- sizeof(git_command) - len, "/%s",
67
- exec_dir);
68
- if (rc < 0 || rc >= sizeof(git_command) - len) {
69
- fprintf(stderr, "git: command name given "
70
- "is too long.\n");
71
- break;
72
- }
73
- } else {
74
- if (strlen(exec_dir) + 1 > sizeof(git_command)) {
75
- fprintf(stderr, "git: command name given "
76
- "is too long.\n");
77
- break;
78
- }
79
- strcpy(git_command, exec_dir);
80
- }
81
-
82
- len = strlen(git_command);
83
- rc = snprintf(git_command + len, sizeof(git_command) - len,
84
- "/git-%s", argv[0]);
85
- if (rc < 0 || rc >= sizeof(git_command) - len) {
86
- fprintf(stderr,
87
- "git: command name given is too long.\n");
88
- break;
89
- }
68
+ struct strbuf cmd;
69
+ const char *tmp;
70
91
- /* argv[0] must be the git command, but the argv array
92
- * belongs to the caller, and my be reused in
93
- * subsequent loop iterations. Save argv[0] and
94
- * restore it on error.
95
- */
71
+ strbuf_init(&cmd, 0);
72
+ strbuf_addf(&cmd, "git-%s", argv[0]);
73
97
- tmp = argv[0];
98
- argv[0] = git_command;
74
+ /*
75
+ * argv[0] must be the git command, but the argv array
76
+ * belongs to the caller, and may be reused in
77
+ * subsequent loop iterations. Save argv[0] and
78
+ * restore it on error.
79
+ */
80
+ tmp = argv[0];
81
+ argv[0] = cmd.buf;
82
100
- trace_argv_printf(argv, -1, "trace: exec:");
83
+ trace_argv_printf(argv, -1, "trace: exec:");
84
102
- /* execve() can only ever return if it fails */
103
- execve(git_command, (char **)argv, environ);
85
+ /* execvp() can only ever return if it fails */
86
+ execvp(cmd.buf, (char **)argv);
87
105
- trace_printf("trace: exec failed: %s\n", strerror(errno));
88
+ trace_printf("trace: exec failed: %s\n", strerror(errno));
89
107
- argv[0] = tmp;
108
- }
109
- return -1;
90
+ argv[0] = tmp;
91
92
+ strbuf_release(&cmd);
93
+
94
+ return -1;
95
}
96
97
exec_cmd.h
+1
@@ -3,6 +3,7 @@
3
4
extern void git_set_argv_exec_path(const char *exec_path);
5
extern const char* git_exec_path(void);
6
+extern void setup_path(const char *);
7
extern int execv_git_cmd(const char **argv); /* NULL terminated */
8
extern int execl_git_cmd(const char *cmd, ...);
9
git.c
+7
-36
@@ -6,28 +6,6 @@
6
const char git_usage_string[] =
7
"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate|--no-pager] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]";
8
9
-static void prepend_to_path(const char *dir, int len)
10
-{
11
- const char *old_path = getenv("PATH");
12
- char *path;
13
- int path_len = len;
14
-
15
- if (!old_path)
16
- old_path = "/usr/local/bin:/usr/bin:/bin";
17
-
18
- path_len = len + strlen(old_path) + 1;
19
-
20
- path = xmalloc(path_len + 1);
21
-
22
- memcpy(path, dir, len);
23
- path[len] = ':';
24
- memcpy(path + len + 1, old_path, path_len - len);
25
-
26
- setenv("PATH", path, 1);
27
-
28
- free(path);
29
-}
30
-
9
static int handle_options(const char*** argv, int* argc, int* envchanged)
10
{
11
int handled = 0;
@@ -408,7 +386,7 @@ int main(int argc, const char **argv)
386
{
387
const char *cmd = argv[0] ? argv[0] : "git-help";
388
char *slash = strrchr(cmd, '/');
411
- const char *exec_path = NULL;
389
+ const char *cmd_path = NULL;
390
int done_alias = 0;
391
392
/*
@@ -418,10 +396,7 @@ int main(int argc, const char **argv)
396
*/
397
if (slash) {
398
*slash++ = 0;
421
- if (*cmd == '/')
422
- exec_path = cmd;
423
- else
424
- exec_path = xstrdup(make_absolute_path(cmd));
399
+ cmd_path = cmd;
400
cmd = slash;
401
}
402
@@ -458,16 +433,12 @@ int main(int argc, const char **argv)
433
cmd = argv[0];
434
435
/*
461
- * We execute external git command via execv_git_cmd(),
462
- * which looks at "--exec-path" option, GIT_EXEC_PATH
463
- * environment, and $(gitexecdir) in Makefile while built,
464
- * in this order. For scripted commands, we prepend
465
- * the value of the exec_path variable to the PATH.
436
+ * We use PATH to find git commands, but we prepend some higher
437
+ * precidence paths: the "--exec-path" option, the GIT_EXEC_PATH
438
+ * environment, and the $(gitexecdir) from the Makefile at build
439
+ * time.
440
*/
467
- if (exec_path)
468
- prepend_to_path(exec_path, strlen(exec_path));
469
- exec_path = git_exec_path();
470
- prepend_to_path(exec_path, strlen(exec_path));
441
+ setup_path(cmd_path);
442
443
while (1) {
444
/* See if it's an internal command */