blob: 5d6a1247b4a1955dffae2b52da064a6eb489d83b [file] [log] [blame]
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05001#include "cache.h"
2#include "exec_cmd.h"
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +02003#include "quote.h"
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05004#define MAX_ARGS 32
5
6extern char **environ;
7static const char *builtin_exec_path = GIT_EXEC_PATH;
David Rientjes96f1e582006-08-15 10:23:48 -07008static const char *current_exec_path;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -05009
10void git_set_exec_path(const char *exec_path)
11{
12 current_exec_path = exec_path;
13}
14
15
16/* Returns the highest-priority, location to look for git programs. */
Timo Hirvonen962554c2006-02-26 17:13:46 +020017const char *git_exec_path(void)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050018{
19 const char *env;
20
21 if (current_exec_path)
22 return current_exec_path;
23
24 env = getenv("GIT_EXEC_PATH");
Dmitry V. Levin2b601622006-05-29 04:34:34 +040025 if (env && *env) {
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050026 return env;
27 }
28
29 return builtin_exec_path;
30}
31
32
Junio C Hamano9201c702006-03-05 02:47:29 -080033int execv_git_cmd(const char **argv)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050034{
35 char git_command[PATH_MAX + 1];
Dmitry V. Levind6859902006-05-30 18:58:52 +040036 int i;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050037 const char *paths[] = { current_exec_path,
38 getenv("GIT_EXEC_PATH"),
39 builtin_exec_path };
40
Junio C Hamanob4f2a6a2006-03-09 11:58:05 -080041 for (i = 0; i < ARRAY_SIZE(paths); ++i) {
Dmitry V. Levind6859902006-05-30 18:58:52 +040042 size_t len;
43 int rc;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050044 const char *exec_dir = paths[i];
Junio C Hamano9201c702006-03-05 02:47:29 -080045 const char *tmp;
46
Dmitry V. Levin2b601622006-05-29 04:34:34 +040047 if (!exec_dir || !*exec_dir) continue;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050048
49 if (*exec_dir != '/') {
50 if (!getcwd(git_command, sizeof(git_command))) {
51 fprintf(stderr, "git: cannot determine "
Dmitry V. Levind6859902006-05-30 18:58:52 +040052 "current directory: %s\n",
53 strerror(errno));
54 break;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050055 }
56 len = strlen(git_command);
57
58 /* Trivial cleanup */
59 while (!strncmp(exec_dir, "./", 2)) {
60 exec_dir += 2;
61 while (*exec_dir == '/')
62 exec_dir++;
63 }
Dmitry V. Levind6859902006-05-30 18:58:52 +040064
65 rc = snprintf(git_command + len,
66 sizeof(git_command) - len, "/%s",
67 exec_dir);
68 if (rc < 0 || rc >= sizeof(git_command) - len) {
69 fprintf(stderr, "git: command name given "
70 "is too long.\n");
71 break;
72 }
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050073 } else {
Dmitry V. Levind6859902006-05-30 18:58:52 +040074 if (strlen(exec_dir) + 1 > sizeof(git_command)) {
75 fprintf(stderr, "git: command name given "
76 "is too long.\n");
77 break;
78 }
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050079 strcpy(git_command, exec_dir);
80 }
81
82 len = strlen(git_command);
Dmitry V. Levind6859902006-05-30 18:58:52 +040083 rc = snprintf(git_command + len, sizeof(git_command) - len,
84 "/git-%s", argv[0]);
85 if (rc < 0 || rc >= sizeof(git_command) - len) {
Michal Ostrowski77cb17e2006-01-10 21:12:17 -050086 fprintf(stderr,
87 "git: command name given is too long.\n");
88 break;
89 }
90
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.
95 */
96
97 tmp = argv[0];
98 argv[0] = git_command;
99
Christian Couder7cf67202006-08-31 08:42:11 +0200100 trace_argv_printf(argv, -1, "trace: exec:");
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +0200101
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500102 /* execve() can only ever return if it fails */
Junio C Hamano9201c702006-03-05 02:47:29 -0800103 execve(git_command, (char **)argv, environ);
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500104
Christian Couder7cf67202006-08-31 08:42:11 +0200105 trace_printf("trace: exec failed: %s\n", strerror(errno));
Matthias Lederhofer575ba9d2006-06-25 15:56:18 +0200106
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500107 argv[0] = tmp;
108 }
109 return -1;
110
111}
112
113
Junio C Hamano9201c702006-03-05 02:47:29 -0800114int execl_git_cmd(const char *cmd,...)
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500115{
116 int argc;
Junio C Hamano9201c702006-03-05 02:47:29 -0800117 const char *argv[MAX_ARGS + 1];
118 const char *arg;
Michal Ostrowski77cb17e2006-01-10 21:12:17 -0500119 va_list param;
120
121 va_start(param, cmd);
122 argv[0] = cmd;
123 argc = 1;
124 while (argc < MAX_ARGS) {
125 arg = argv[argc++] = va_arg(param, char *);
126 if (!arg)
127 break;
128 }
129 va_end(param);
130 if (MAX_ARGS <= argc)
131 return error("too many args to run %s", cmd);
132
133 argv[argc] = NULL;
134 return execv_git_cmd(argv);
135}