4 #include "spawn-pipe.h"
8 static const char *argv_exec_path
;
10 static const char *builtin_exec_path(void)
21 len
= strlen(_pgmptr
);
25 p
= ep
= xmalloc(len
+1);
28 /* copy program name, turn '\\' into '/', skip last part */
30 if (*q
== '\\' || *q
== '/') {
39 ep
[0] = '.', ep
[1] = '\0';
44 void git_set_argv_exec_path(const char *exec_path
)
46 argv_exec_path
= exec_path
;
50 /* Returns the highest-priority, location to look for git programs. */
51 const char *git_exec_path(void)
56 return argv_exec_path
;
58 env
= getenv(EXEC_PATH_ENVIRONMENT
);
63 return builtin_exec_path();
66 static void add_path(struct strbuf
*out
, const char *path
)
69 if (is_absolute_path(path
))
70 strbuf_addstr(out
, path
);
72 strbuf_addstr(out
, make_absolute_path(path
));
75 strbuf_addch(out
, ';');
77 strbuf_addch(out
, ':');
82 void setup_path(const char *cmd_path
)
84 const char *old_path
= getenv("PATH");
85 struct strbuf new_path
;
87 strbuf_init(&new_path
, 0);
89 add_path(&new_path
, argv_exec_path
);
90 add_path(&new_path
, getenv(EXEC_PATH_ENVIRONMENT
));
91 add_path(&new_path
, builtin_exec_path());
92 add_path(&new_path
, cmd_path
);
95 strbuf_addstr(&new_path
, old_path
);
97 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
99 setenv("PATH", new_path
.buf
, 1);
101 strbuf_release(&new_path
);
104 int execv_git_cmd(const char **argv
)
109 strbuf_init(&cmd
, 0);
110 strbuf_addf(&cmd
, "git-%s", argv
[0]);
113 * argv[0] must be the git command, but the argv array
114 * belongs to the caller, and may be reused in
115 * subsequent loop iterations. Save argv[0] and
116 * restore it on error.
121 trace_argv_printf(argv
, -1, "trace: exec:");
123 /* execvp() can only ever return if it fails */
124 execvp(cmd
.buf
, (char **)argv
);
126 trace_printf("trace: exec failed: %s\n", strerror(errno
));
130 strbuf_release(&cmd
);
136 int execl_git_cmd(const char *cmd
,...)
139 const char *argv
[MAX_ARGS
+ 1];
143 va_start(param
, cmd
);
146 while (argc
< MAX_ARGS
) {
147 arg
= argv
[argc
++] = va_arg(param
, char *);
152 if (MAX_ARGS
<= argc
)
153 return error("too many args to run %s", cmd
);
156 return execv_git_cmd(argv
);
159 int spawnv_git_cmd(const char **argv
, int pin
[2], int pout
[2])
165 strbuf_init(&cmd
, 0);
166 strbuf_addf(&cmd
, "git-%s", argv
[0]);
168 /* argv[0] must be the git command, but the argv array
169 * belongs to the caller. Save argv[0] and
176 trace_argv_printf(argv
, -1, "trace: exec:");
178 pid
= spawnvpe_pipe(cmd
.buf
, argv
, environ
,