Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 131 lines (103 sloc) 2.567 kb
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
1 #include "cache.h"
2 #include "exec_cmd.h"
575ba9d @matled GIT_TRACE: show which built-in/external commands are executed
matled authored
3 #include "quote.h"
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
4 #define MAX_ARGS 32
5
6 extern char **environ;
384df83 @srp "current_exec_path" is a misleading name, use "argv_exec_path"
srp authored
7 static const char *argv_exec_path;
e1464ca Record the command invocation path early
Johannes Sixt authored
8 static const char *argv0_path;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
9
2de9de5 @sprohaska Move code interpreting path relative to exec-dir to new function system_...
sprohaska authored
10 const char *system_path(const char *path)
11 {
966c6ed Fix relative built-in paths to be relative to the command invocation
Johannes Sixt authored
12 if (!is_absolute_path(path) && argv0_path) {
2de9de5 @sprohaska Move code interpreting path relative to exec-dir to new function system_...
sprohaska authored
13 struct strbuf d = STRBUF_INIT;
966c6ed Fix relative built-in paths to be relative to the command invocation
Johannes Sixt authored
14 strbuf_addf(&d, "%s/%s", argv0_path, path);
2de9de5 @sprohaska Move code interpreting path relative to exec-dir to new function system_...
sprohaska authored
15 path = strbuf_detach(&d, NULL);
16 }
17 return path;
18 }
19
e1464ca Record the command invocation path early
Johannes Sixt authored
20 void git_set_argv0_path(const char *path)
21 {
22 argv0_path = path;
23 }
24
384df83 @srp "current_exec_path" is a misleading name, use "argv_exec_path"
srp authored
25 void git_set_argv_exec_path(const char *exec_path)
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
26 {
384df83 @srp "current_exec_path" is a misleading name, use "argv_exec_path"
srp authored
27 argv_exec_path = exec_path;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
28 }
29
30
31 /* Returns the highest-priority, location to look for git programs. */
962554c @tihirvon Use setenv(), fix warnings
tihirvon authored
32 const char *git_exec_path(void)
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
33 {
34 const char *env;
35
384df83 @srp "current_exec_path" is a misleading name, use "argv_exec_path"
srp authored
36 if (argv_exec_path)
37 return argv_exec_path;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
38
d4ebc36 Use preprocessor constants for environment variable names.
Junio C Hamano authored
39 env = getenv(EXEC_PATH_ENVIRONMENT);
2b60162 git_exec_path, execv_git_cmd: ignore empty environment variables
Dmitry V. Levin authored
40 if (env && *env) {
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
41 return env;
42 }
43
49fa65a Allow the built-in exec path to be relative to the command invocation pa...
Johannes Sixt authored
44 return system_path(GIT_EXEC_PATH);
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
45 }
46
511707d @srp use only the $PATH for exec'ing git commands
srp authored
47 static void add_path(struct strbuf *out, const char *path)
48 {
49 if (path && *path) {
50 if (is_absolute_path(path))
51 strbuf_addstr(out, path);
52 else
10c4c88 Allow add_path() to add non-existent directories to the path
Johannes Sixt authored
53 strbuf_addstr(out, make_nonrelative_path(path));
511707d @srp use only the $PATH for exec'ing git commands
srp authored
54
80ba074 Windows: Use the Windows style PATH separator ';'.
Johannes Sixt authored
55 strbuf_addch(out, PATH_SEP);
511707d @srp use only the $PATH for exec'ing git commands
srp authored
56 }
57 }
58
e1464ca Record the command invocation path early
Johannes Sixt authored
59 void setup_path(void)
511707d @srp use only the $PATH for exec'ing git commands
srp authored
60 {
61 const char *old_path = getenv("PATH");
f285a2d Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Brandon Casey authored
62 struct strbuf new_path = STRBUF_INIT;
511707d @srp use only the $PATH for exec'ing git commands
srp authored
63
64 add_path(&new_path, argv_exec_path);
65 add_path(&new_path, getenv(EXEC_PATH_ENVIRONMENT));
49fa65a Allow the built-in exec path to be relative to the command invocation pa...
Johannes Sixt authored
66 add_path(&new_path, system_path(GIT_EXEC_PATH));
e1464ca Record the command invocation path early
Johannes Sixt authored
67 add_path(&new_path, argv0_path);
511707d @srp use only the $PATH for exec'ing git commands
srp authored
68
69 if (old_path)
70 strbuf_addstr(&new_path, old_path);
71 else
72 strbuf_addstr(&new_path, "/usr/local/bin:/usr/bin:/bin");
73
74 setenv("PATH", new_path.buf, 1);
75
76 strbuf_release(&new_path);
77 }
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
78
4933e5e @sprohaska Refactor, adding prepare_git_cmd(const char **argv)
sprohaska authored
79 const char **prepare_git_cmd(const char **argv)
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
80 {
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
81 int argc;
82 const char **nargv;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
83
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
84 for (argc = 0; argv[argc]; argc++)
85 ; /* just counting */
86 nargv = xmalloc(sizeof(*nargv) * (argc + 2));
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
87
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
88 nargv[0] = "git";
89 for (argc = 0; argv[argc]; argc++)
90 nargv[argc + 1] = argv[argc];
91 nargv[argc + 1] = NULL;
4933e5e @sprohaska Refactor, adding prepare_git_cmd(const char **argv)
sprohaska authored
92 return nargv;
93 }
94
95 int execv_git_cmd(const char **argv) {
96 const char **nargv = prepare_git_cmd(argv);
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
97 trace_argv_printf(nargv, "trace: exec:");
575ba9d @matled GIT_TRACE: show which built-in/external commands are executed
matled authored
98
511707d @srp use only the $PATH for exec'ing git commands
srp authored
99 /* execvp() can only ever return if it fails */
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
100 execvp("git", (char **)nargv);
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
101
511707d @srp use only the $PATH for exec'ing git commands
srp authored
102 trace_printf("trace: exec failed: %s\n", strerror(errno));
575ba9d @matled GIT_TRACE: show which built-in/external commands are executed
matled authored
103
7550be0 @gitster Prepare execv_git_cmd() for removal of builtins from the filesystem
gitster authored
104 free(nargv);
511707d @srp use only the $PATH for exec'ing git commands
srp authored
105 return -1;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
106 }
107
108
9201c70 Const tightening.
Junio C Hamano authored
109 int execl_git_cmd(const char *cmd,...)
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
110 {
111 int argc;
9201c70 Const tightening.
Junio C Hamano authored
112 const char *argv[MAX_ARGS + 1];
113 const char *arg;
77cb17e Exec git programs without using PATH.
Michal Ostrowski authored
114 va_list param;
115
116 va_start(param, cmd);
117 argv[0] = cmd;
118 argc = 1;
119 while (argc < MAX_ARGS) {
120 arg = argv[argc++] = va_arg(param, char *);
121 if (!arg)
122 break;
123 }
124 va_end(param);
125 if (MAX_ARGS <= argc)
126 return error("too many args to run %s", cmd);
127
128 argv[argc] = NULL;
129 return execv_git_cmd(argv);
130 }
Something went wrong with that request. Please try again.