7 static const char *argv_exec_path
;
8 static const char *argv0_path
;
10 const char *system_path(const char *path
)
12 static const char *prefix
;
14 if (is_absolute_path(path
)) {
20 assert(is_absolute_path(argv0_path
));
23 const char *strip
[] = {
30 for (s
= strip
; *s
; s
++) {
31 const char *sargv
= argv0_path
+ strlen(argv0_path
);
32 const char *ss
= *s
+ strlen(*s
);
33 while (argv0_path
< sargv
&& *s
< ss
35 (is_dir_sep(*sargv
) && is_dir_sep(*ss
)))) {
40 struct strbuf d
= STRBUF_INIT
;
41 /* We also skip the trailing directory separator. */
42 assert(sargv
- argv0_path
- 1 >= 0);
43 strbuf_add(&d
, argv0_path
, sargv
- argv0_path
- 1);
44 prefix
= strbuf_detach(&d
, NULL
);
52 fprintf(stderr
, "RUNTIME_PREFIX requested, "
53 "but prefix computation failed. "
54 "Using static fallback '%s'.\n", prefix
);
60 struct strbuf d
= STRBUF_INIT
;
61 strbuf_addf(&d
, "%s/%s", prefix
, path
);
62 path
= strbuf_detach(&d
, NULL
);
66 const char *git_extract_argv0_path(const char *argv0
)
68 const char *slash
= argv0
+ strlen(argv0
);
72 while (slash
>= argv0
&& !is_dir_sep(*slash
));
75 argv0_path
= xstrndup(argv0
, slash
- argv0
);
82 void git_set_argv_exec_path(const char *exec_path
)
84 argv_exec_path
= exec_path
;
88 /* Returns the highest-priority, location to look for git programs. */
89 const char *git_exec_path(void)
94 return argv_exec_path
;
96 env
= getenv(EXEC_PATH_ENVIRONMENT
);
101 return system_path(GIT_EXEC_PATH
);
104 static void add_path(struct strbuf
*out
, const char *path
)
107 if (is_absolute_path(path
))
108 strbuf_addstr(out
, path
);
110 strbuf_addstr(out
, make_nonrelative_path(path
));
112 strbuf_addch(out
, PATH_SEP
);
116 void setup_path(void)
118 const char *old_path
= getenv("PATH");
119 struct strbuf new_path
= STRBUF_INIT
;
121 add_path(&new_path
, git_exec_path());
122 add_path(&new_path
, argv0_path
);
125 strbuf_addstr(&new_path
, old_path
);
127 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
129 setenv("PATH", new_path
.buf
, 1);
131 strbuf_release(&new_path
);
134 const char **prepare_git_cmd(const char **argv
)
139 for (argc
= 0; argv
[argc
]; argc
++)
140 ; /* just counting */
141 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 2));
144 for (argc
= 0; argv
[argc
]; argc
++)
145 nargv
[argc
+ 1] = argv
[argc
];
146 nargv
[argc
+ 1] = NULL
;
150 int execv_git_cmd(const char **argv
) {
151 const char **nargv
= prepare_git_cmd(argv
);
152 trace_argv_printf(nargv
, "trace: exec:");
154 /* execvp() can only ever return if it fails */
155 execvp("git", (char **)nargv
);
157 trace_printf("trace: exec failed: %s\n", strerror(errno
));
164 int execl_git_cmd(const char *cmd
,...)
167 const char *argv
[MAX_ARGS
+ 1];
171 va_start(param
, cmd
);
174 while (argc
< MAX_ARGS
) {
175 arg
= argv
[argc
++] = va_arg(param
, char *);
180 if (MAX_ARGS
<= argc
)
181 return error("too many args to run %s", cmd
);
184 return execv_git_cmd(argv
);