7 static const char *argv_exec_path
;
9 static const char *builtin_exec_path(void)
20 len
= strlen(_pgmptr
);
24 p
= ep
= xmalloc(len
+1);
27 /* copy program name, turn '\\' into '/', skip last part */
29 if (*q
== '\\' || *q
== '/') {
38 ep
[0] = '.', ep
[1] = '\0';
43 const char *system_path(const char *path
)
45 if (!is_absolute_path(path
)) {
46 struct strbuf d
= STRBUF_INIT
;
47 strbuf_addf(&d
, "%s/%s", git_exec_path(), path
);
48 path
= strbuf_detach(&d
, NULL
);
53 void git_set_argv_exec_path(const char *exec_path
)
55 argv_exec_path
= exec_path
;
59 /* Returns the highest-priority, location to look for git programs. */
60 const char *git_exec_path(void)
65 return argv_exec_path
;
67 env
= getenv(EXEC_PATH_ENVIRONMENT
);
72 return builtin_exec_path();
75 static void add_path(struct strbuf
*out
, const char *path
)
78 if (is_absolute_path(path
))
79 strbuf_addstr(out
, path
);
81 strbuf_addstr(out
, make_absolute_path(path
));
83 strbuf_addch(out
, PATH_SEP
);
87 void setup_path(const char *cmd_path
)
89 const char *old_path
= getenv("PATH");
90 struct strbuf new_path
;
92 strbuf_init(&new_path
, 0);
94 add_path(&new_path
, argv_exec_path
);
95 add_path(&new_path
, getenv(EXEC_PATH_ENVIRONMENT
));
96 add_path(&new_path
, builtin_exec_path());
97 add_path(&new_path
, cmd_path
);
100 strbuf_addstr(&new_path
, old_path
);
102 strbuf_addstr(&new_path
, "/usr/local/bin:/usr/bin:/bin");
104 setenv("PATH", new_path
.buf
, 1);
106 strbuf_release(&new_path
);
109 int execv_git_cmd(const char **argv
)
114 for (argc
= 0; argv
[argc
]; argc
++)
115 ; /* just counting */
116 nargv
= xmalloc(sizeof(*nargv
) * (argc
+ 2));
119 for (argc
= 0; argv
[argc
]; argc
++)
120 nargv
[argc
+ 1] = argv
[argc
];
121 nargv
[argc
+ 1] = NULL
;
122 trace_argv_printf(nargv
, "trace: exec:");
124 /* execvp() can only ever return if it fails */
125 execvp("git", (char **)nargv
);
127 trace_printf("trace: exec failed: %s\n", strerror(errno
));
134 int execl_git_cmd(const char *cmd
,...)
137 const char *argv
[MAX_ARGS
+ 1];
141 va_start(param
, cmd
);
144 while (argc
< MAX_ARGS
) {
145 arg
= argv
[argc
++] = va_arg(param
, char *);
150 if (MAX_ARGS
<= argc
)
151 return error("too many args to run %s", cmd
);
154 return execv_git_cmd(argv
);