4 #include "spawn-pipe.h"
8 static const char *builtin_exec_path
= GIT_EXEC_PATH
;
9 static const char *current_exec_path
;
11 void git_set_exec_path(const char *exec_path
)
13 current_exec_path
= exec_path
;
17 /* Returns the highest-priority, location to look for git programs. */
18 const char *git_exec_path(void)
22 if (current_exec_path
)
23 return current_exec_path
;
25 env
= getenv(EXEC_PATH_ENVIRONMENT
);
30 return builtin_exec_path
;
34 int execv_git_cmd(const char **argv
)
36 char git_command
[PATH_MAX
+ 1];
38 const char *paths
[] = { current_exec_path
,
39 getenv(EXEC_PATH_ENVIRONMENT
),
42 for (i
= 0; i
< ARRAY_SIZE(paths
); ++i
) {
45 const char *exec_dir
= paths
[i
];
48 if (!exec_dir
|| !*exec_dir
) continue;
51 if (*exec_dir
!= '/' && exec_dir
[1] != ':') {
53 if (*exec_dir
!= '/') {
55 if (!getcwd(git_command
, sizeof(git_command
))) {
56 fprintf(stderr
, "git: cannot determine "
57 "current directory: %s\n",
61 len
= strlen(git_command
);
64 while (!prefixcmp(exec_dir
, "./")) {
66 while (*exec_dir
== '/')
70 rc
= snprintf(git_command
+ len
,
71 sizeof(git_command
) - len
, "/%s",
73 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
74 fprintf(stderr
, "git: command name given "
79 if (strlen(exec_dir
) + 1 > sizeof(git_command
)) {
80 fprintf(stderr
, "git: command name given "
84 strcpy(git_command
, exec_dir
);
87 len
= strlen(git_command
);
88 rc
= snprintf(git_command
+ len
, sizeof(git_command
) - len
,
90 if (rc
< 0 || rc
>= sizeof(git_command
) - len
) {
92 "git: command name given is too long.\n");
96 /* argv[0] must be the git command, but the argv array
97 * belongs to the caller, and my be reused in
98 * subsequent loop iterations. Save argv[0] and
99 * restore it on error.
103 argv
[0] = git_command
;
105 trace_argv_printf(argv
, -1, "trace: exec:");
107 /* execve() can only ever return if it fails */
108 execve(git_command
, (char **)argv
, environ
);
110 trace_printf("trace: exec failed: %s\n", strerror(errno
));
119 int execl_git_cmd(const char *cmd
,...)
122 const char *argv
[MAX_ARGS
+ 1];
126 va_start(param
, cmd
);
129 while (argc
< MAX_ARGS
) {
130 arg
= argv
[argc
++] = va_arg(param
, char *);
135 if (MAX_ARGS
<= argc
)
136 return error("too many args to run %s", cmd
);
139 return execv_git_cmd(argv
);
142 int spawnv_git_cmd(const char **argv
, int pin
[2], int pout
[2])
147 const char *paths
[] = { current_exec_path
,
148 getenv(EXEC_PATH_ENVIRONMENT
),
150 char p
[3][PATH_MAX
+ 1];
151 char *usedpaths
[4], **up
= usedpaths
;
154 for (i
= 0; i
< ARRAY_SIZE(paths
); ++i
) {
156 const char *exec_dir
= paths
[i
];
158 if (!exec_dir
|| !*exec_dir
) continue;
161 if (*exec_dir
!= '/' && exec_dir
[1] != ':') {
163 if (*exec_dir
!= '/') {
165 if (!getcwd(p
[i
], sizeof(p
[i
]))) {
166 fprintf(stderr
, "git: cannot determine "
167 "current directory: %s\n",
173 /* Trivial cleanup */
174 while (!strncmp(exec_dir
, "./", 2)) {
176 while (*exec_dir
== '/')
180 rc
= snprintf(p
[i
] + len
,
181 sizeof(p
[i
]) - len
, "/%s",
183 if (rc
< 0 || rc
>= sizeof(p
[i
]) - len
) {
184 fprintf(stderr
, "git: command name given "
189 if (strlen(exec_dir
) + 1 > sizeof(p
[i
])) {
190 fprintf(stderr
, "git: command name given "
194 strcpy(p
[i
], exec_dir
);
200 rc
= snprintf(cmd
, sizeof(cmd
), "git-%s", argv
[0]);
201 if (rc
< 0 || rc
>= sizeof(cmd
)) {
203 "git: command name given is too long.\n");
207 /* argv[0] must be the git command, but the argv array
208 * belongs to the caller. Save argv[0] and
215 trace_argv_printf(argv
, -1, "trace: exec:");
217 pid
= spawnvppe_pipe(cmd
, argv
, environ
, usedpaths
,