7 static const char *argv_exec_path
;
8 static const char *argv0_path
;
10 const char *system_path(const char *path
)
13 static const char *prefix
;
15 static const char *prefix
= PREFIX
;
17 struct strbuf d
= STRBUF_INIT
;
19 if (is_absolute_path(path
))
24 assert(is_absolute_path(argv0_path
));
27 const char *strip
[] = {
34 for (s
= strip
; *s
; s
++) {
35 const char *sargv
= argv0_path
+ strlen(argv0_path
);
36 const char *ss
= *s
+ strlen(*s
);
37 while (argv0_path
< sargv
&& *s
< ss
39 (is_dir_sep(*sargv
) && is_dir_sep(*ss
)))) {
44 struct strbuf d
= STRBUF_INIT
;
45 /* We also skip the trailing directory separator. */
46 assert(sargv
- argv0_path
- 1 >= 0);
47 strbuf_add(&d
, argv0_path
, sargv
- argv0_path
- 1);
48 prefix
= strbuf_detach(&d
, NULL
);
56 fprintf(stderr
, "RUNTIME_PREFIX requested, "
57 "but prefix computation failed. "
58 "Using static fallback '%s'.\n", prefix
);
62 strbuf_addf(&d
, "%s/%s", prefix
, path
);
63 path
= strbuf_detach(&d
, NULL
);
67 const char *git_extract_argv0_path(const char *argv0
)
71 if (!argv0
|| !*argv0
)
73 slash
= argv0
+ strlen(argv0
);
75 while (argv0
<= slash
&& !is_dir_sep(*slash
))
79 argv0_path
= xstrndup(argv0
, slash
- argv0
);
86 void git_set_argv_exec_path(const char *exec_path
)
88 argv_exec_path
= exec_path
;
92 /* Returns the highest-priority, location to look for git programs. */
93 const char *git_exec_path(void)
98 return argv_exec_path
;
100 env
= getenv(EXEC_PATH_ENVIRONMENT
);
105 return system_path(GIT_EXEC_PATH
);
108 static void add_path(struct strbuf
*out
, const char *path
)
111 if (is_absolute_path(path
))
112 strbuf_addstr(out
, path
);
114 strbuf_addstr(out
, make_nonrelative_path(path
));
116 strbuf_addch(out
, PATH_SEP
);
120 void setup_path(void)
122 const char *old_path
= getenv("PATH");
123 struct strbuf new_path
= STRBUF_INIT
;
125 add_path(&new_path
, git_exec_path());
126 add_path(&new_path
, argv0_path
);
129 strbuf_addstr(&new_path
, old_path
);
131 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
133 setenv("PATH", new_path
.buf
, 1);
135 strbuf_release(&new_path
);
138 const char **prepare_git_cmd(const char **argv
)
143 for (argc
= 0; argv
[argc
]; argc
++)
144 ; /* just counting */
145 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 2));
148 for (argc
= 0; argv
[argc
]; argc
++)
149 nargv
[argc
+ 1] = argv
[argc
];
150 nargv
[argc
+ 1] = NULL
;
154 int execv_git_cmd(const char **argv
) {
155 const char **nargv
= prepare_git_cmd(argv
);
156 trace_argv_printf(nargv
, "trace: exec:");
158 /* execvp() can only ever return if it fails */
159 execvp("git", (char **)nargv
);
161 trace_printf("trace: exec failed: %s\n", strerror(errno
));
168 int execl_git_cmd(const char *cmd
,...)
171 const char *argv
[MAX_ARGS
+ 1];
175 va_start(param
, cmd
);
178 while (argc
< MAX_ARGS
) {
179 arg
= argv
[argc
++] = va_arg(param
, char *);
184 if (MAX_ARGS
<= argc
)
185 return error("too many args to run %s", cmd
);
188 return execv_git_cmd(argv
);