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 !(prefix
= strip_path_suffix(argv0_path
, GIT_EXEC_PATH
)) &&
28 !(prefix
= strip_path_suffix(argv0_path
, BINDIR
)) &&
29 !(prefix
= strip_path_suffix(argv0_path
, "git"))) {
31 fprintf(stderr
, "RUNTIME_PREFIX requested, "
32 "but prefix computation failed. "
33 "Using static fallback '%s'.\n", prefix
);
37 strbuf_addf(&d
, "%s/%s", prefix
, path
);
38 path
= strbuf_detach(&d
, NULL
);
42 const char *git_extract_argv0_path(const char *argv0
)
46 if (!argv0
|| !*argv0
)
48 slash
= argv0
+ strlen(argv0
);
50 while (argv0
<= slash
&& !is_dir_sep(*slash
))
54 argv0_path
= xstrndup(argv0
, slash
- argv0
);
61 void git_set_argv_exec_path(const char *exec_path
)
63 argv_exec_path
= exec_path
;
67 /* Returns the highest-priority, location to look for git programs. */
68 const char *git_exec_path(void)
73 return argv_exec_path
;
75 env
= getenv(EXEC_PATH_ENVIRONMENT
);
80 return system_path(GIT_EXEC_PATH
);
83 static void add_path(struct strbuf
*out
, const char *path
)
86 if (is_absolute_path(path
))
87 strbuf_addstr(out
, path
);
89 strbuf_addstr(out
, make_nonrelative_path(path
));
91 strbuf_addch(out
, PATH_SEP
);
97 const char *old_path
= getenv("PATH");
98 struct strbuf new_path
= STRBUF_INIT
;
100 add_path(&new_path
, git_exec_path());
101 add_path(&new_path
, argv0_path
);
104 strbuf_addstr(&new_path
, old_path
);
106 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
108 setenv("PATH", new_path
.buf
, 1);
110 strbuf_release(&new_path
);
113 const char **prepare_git_cmd(const char **argv
)
118 for (argc
= 0; argv
[argc
]; argc
++)
119 ; /* just counting */
120 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 2));
123 for (argc
= 0; argv
[argc
]; argc
++)
124 nargv
[argc
+ 1] = argv
[argc
];
125 nargv
[argc
+ 1] = NULL
;
129 int execv_git_cmd(const char **argv
) {
130 const char **nargv
= prepare_git_cmd(argv
);
131 trace_argv_printf(nargv
, "trace: exec:");
133 /* execvp() can only ever return if it fails */
134 execvp("git", (char **)nargv
);
136 trace_printf("trace: exec failed: %s\n", strerror(errno
));
143 int execl_git_cmd(const char *cmd
,...)
146 const char *argv
[MAX_ARGS
+ 1];
150 va_start(param
, cmd
);
153 while (argc
< MAX_ARGS
) {
154 arg
= argv
[argc
++] = va_arg(param
, char *);
159 if (MAX_ARGS
<= argc
)
160 return error("too many args to run %s", cmd
);
163 return execv_git_cmd(argv
);