7 static const char *builtin_exec_path
= GIT_EXEC_PATH
;
8 static const char *current_exec_path
= NULL
;
10 void git_set_exec_path(const char *exec_path
)
12 current_exec_path
= exec_path
;
16 /* Returns the highest-priority, location to look for git programs. */
17 const char *git_exec_path(void)
21 if (current_exec_path
)
22 return current_exec_path
;
24 env
= getenv("GIT_EXEC_PATH");
29 return builtin_exec_path
;
33 int execv_git_cmd(const char **argv
)
35 char git_command
[PATH_MAX
+ 1];
37 const char *paths
[] = { current_exec_path
,
38 getenv("GIT_EXEC_PATH"),
41 for (i
= 0; i
< ARRAY_SIZE(paths
); ++i
) {
44 const char *exec_dir
= paths
[i
];
47 if (!exec_dir
|| !*exec_dir
) continue;
49 if (*exec_dir
!= '/') {
50 if (!getcwd(git_command
, sizeof(git_command
))) {
51 fprintf(stderr
, "git: cannot determine "
52 "current directory: %s\n",
56 len
= strlen(git_command
);
59 while (!strncmp(exec_dir
, "./", 2)) {
61 while (*exec_dir
== '/')
65 rc
= snprintf(git_command
+ len
,
66 sizeof(git_command
) - len
, "/%s",
68 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
69 fprintf(stderr
, "git: command name given "
74 if (strlen(exec_dir
) + 1 > sizeof(git_command
)) {
75 fprintf(stderr
, "git: command name given "
79 strcpy(git_command
, exec_dir
);
82 len
= strlen(git_command
);
83 rc
= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
85 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
87 "git: command name given is too long.\n");
91 /* argv[0] must be the git command, but the argv array
92 * belongs to the caller, and my be reused in
93 * subsequent loop iterations. Save argv[0] and
94 * restore it on error.
98 argv
[0] = git_command
;
100 if (getenv("GIT_TRACE")) {
101 const char **p
= argv
;
102 fputs("trace: exec:", stderr
);
105 sq_quote_print(stderr
, *p
);
112 /* execve() can only ever return if it fails */
113 execve(git_command
, (char **)argv
, environ
);
115 if (getenv("GIT_TRACE")) {
116 fprintf(stderr
, "trace: exec failed: %s\n",
128 int execl_git_cmd(const char *cmd
,...)
131 const char *argv
[MAX_ARGS
+ 1];
135 va_start(param
, cmd
);
138 while (argc
< MAX_ARGS
) {
139 arg
= argv
[argc
++] = va_arg(param
, char *);
144 if (MAX_ARGS
<= argc
)
145 return error("too many args to run %s", cmd
);
148 return execv_git_cmd(argv
);