]> git.ipfire.org Git - thirdparty/git.git/blame - run-command.c
git-reset: Let -q hush "locally modified" messages
[thirdparty/git.git] / run-command.c
CommitLineData
b1bf95bb
JW
1#include "cache.h"
2#include "run-command.h"
77cb17e9 3#include "exec_cmd.h"
b1bf95bb 4
9dc09c76
SP
5static inline void close_pair(int fd[2])
6{
7 close(fd[0]);
8 close(fd[1]);
9}
10
e4507ae8
SP
11static inline void dup_devnull(int to)
12{
13 int fd = open("/dev/null", O_RDWR);
14 dup2(fd, to);
15 close(fd);
16}
17
ebcb5d16 18int start_command(struct child_process *cmd)
b1bf95bb 19{
f3b33f1d
JS
20 int need_in, need_out, need_err;
21 int fdin[2], fdout[2], fderr[2];
4919bf03 22
c20181e3
JS
23 /*
24 * In case of errors we must keep the promise to close FDs
25 * that have been passed in via ->in and ->out.
26 */
27
e4507ae8 28 need_in = !cmd->no_stdin && cmd->in < 0;
4919bf03 29 if (need_in) {
c20181e3
JS
30 if (pipe(fdin) < 0) {
31 if (cmd->out > 0)
32 close(cmd->out);
4919bf03 33 return -ERR_RUN_COMMAND_PIPE;
c20181e3 34 }
4919bf03 35 cmd->in = fdin[1];
4919bf03
SP
36 }
37
e4507ae8
SP
38 need_out = !cmd->no_stdout
39 && !cmd->stdout_to_stderr
40 && cmd->out < 0;
f4bba25b
SP
41 if (need_out) {
42 if (pipe(fdout) < 0) {
43 if (need_in)
44 close_pair(fdin);
c20181e3
JS
45 else if (cmd->in)
46 close(cmd->in);
f4bba25b
SP
47 return -ERR_RUN_COMMAND_PIPE;
48 }
49 cmd->out = fdout[0];
f4bba25b
SP
50 }
51
b73a4397 52 need_err = !cmd->no_stderr && cmd->err < 0;
f3b33f1d
JS
53 if (need_err) {
54 if (pipe(fderr) < 0) {
55 if (need_in)
56 close_pair(fdin);
c20181e3
JS
57 else if (cmd->in)
58 close(cmd->in);
f3b33f1d
JS
59 if (need_out)
60 close_pair(fdout);
c20181e3
JS
61 else if (cmd->out)
62 close(cmd->out);
f3b33f1d
JS
63 return -ERR_RUN_COMMAND_PIPE;
64 }
65 cmd->err = fderr[0];
66 }
67
8852f5d7
JS
68 trace_argv_printf(cmd->argv, "trace: run_command:");
69
ba26f296 70#ifndef __MINGW32__
ebcb5d16 71 cmd->pid = fork();
ebcb5d16 72 if (!cmd->pid) {
e4507ae8
SP
73 if (cmd->no_stdin)
74 dup_devnull(0);
75 else if (need_in) {
4919bf03 76 dup2(fdin[0], 0);
9dc09c76 77 close_pair(fdin);
4919bf03
SP
78 } else if (cmd->in) {
79 dup2(cmd->in, 0);
80 close(cmd->in);
95d3c4f5 81 }
4919bf03 82
ce2cf27a
CC
83 if (cmd->no_stderr)
84 dup_devnull(2);
85 else if (need_err) {
86 dup2(fderr[1], 2);
87 close_pair(fderr);
88 }
89
e4507ae8
SP
90 if (cmd->no_stdout)
91 dup_devnull(1);
92 else if (cmd->stdout_to_stderr)
cd83c74c 93 dup2(2, 1);
f4bba25b
SP
94 else if (need_out) {
95 dup2(fdout[1], 1);
96 close_pair(fdout);
97 } else if (cmd->out > 1) {
98 dup2(cmd->out, 1);
99 close(cmd->out);
100 }
101
1568fea0
AR
102 if (cmd->dir && chdir(cmd->dir))
103 die("exec %s: cd to %s failed (%s)", cmd->argv[0],
104 cmd->dir, strerror(errno));
ee493148 105 if (cmd->env) {
3427b375
AR
106 for (; *cmd->env; cmd->env++) {
107 if (strchr(*cmd->env, '='))
108 putenv((char*)*cmd->env);
109 else
110 unsetenv(*cmd->env);
111 }
ee493148 112 }
f1000898
SP
113 if (cmd->git_cmd) {
114 execv_git_cmd(cmd->argv);
77cb17e9 115 } else {
f1000898 116 execvp(cmd->argv[0], (char *const*) cmd->argv);
128aed68 117 }
f1000898 118 die("exec %s failed.", cmd->argv[0]);
b1bf95bb 119 }
ba26f296
JS
120#else
121 int s0 = -1, s1 = -1, s2 = -1; /* backups of stdin, stdout, stderr */
122 const char *sargv0 = cmd->argv[0];
123 char **env = environ;
124 struct strbuf git_cmd;
125
126 if (cmd->no_stdin) {
127 s0 = dup(0);
128 dup_devnull(0);
129 } else if (need_in) {
130 s0 = dup(0);
131 dup2(fdin[0], 0);
132 } else if (cmd->in) {
133 s0 = dup(0);
134 dup2(cmd->in, 0);
135 }
136
137 if (cmd->no_stderr) {
138 s2 = dup(2);
139 dup_devnull(2);
140 } else if (need_err) {
141 s2 = dup(2);
142 dup2(fderr[1], 2);
143 }
144
145 if (cmd->no_stdout) {
146 s1 = dup(1);
147 dup_devnull(1);
148 } else if (cmd->stdout_to_stderr) {
149 s1 = dup(1);
150 dup2(2, 1);
151 } else if (need_out) {
152 s1 = dup(1);
153 dup2(fdout[1], 1);
154 } else if (cmd->out > 1) {
155 s1 = dup(1);
156 dup2(cmd->out, 1);
157 }
158
159 if (cmd->dir)
160 die("chdir in start_command() not implemented");
161 if (cmd->env) {
162 env = copy_environ();
163 for (; *cmd->env; cmd->env++)
164 env = env_setenv(env, *cmd->env);
165 }
166
167 if (cmd->git_cmd) {
168 strbuf_init(&git_cmd, 0);
169 strbuf_addf(&git_cmd, "git-%s", cmd->argv[0]);
170 cmd->argv[0] = git_cmd.buf;
171 }
172
7e5d7768 173 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
ba26f296
JS
174
175 if (cmd->env)
176 free_environ(env);
177 if (cmd->git_cmd)
178 strbuf_release(&git_cmd);
179
180 cmd->argv[0] = sargv0;
181 if (s0 >= 0)
182 dup2(s0, 0), close(s0);
183 if (s1 >= 0)
184 dup2(s1, 1), close(s1);
185 if (s2 >= 0)
186 dup2(s2, 2), close(s2);
187#endif
188
189 if (cmd->pid < 0) {
190 if (need_in)
191 close_pair(fdin);
192 else if (cmd->in)
193 close(cmd->in);
194 if (need_out)
195 close_pair(fdout);
196 else if (cmd->out)
197 close(cmd->out);
198 if (need_err)
199 close_pair(fderr);
200 return -ERR_RUN_COMMAND_FORK;
201 }
4919bf03
SP
202
203 if (need_in)
204 close(fdin[0]);
205 else if (cmd->in)
206 close(cmd->in);
207
f4bba25b
SP
208 if (need_out)
209 close(fdout[1]);
c20181e3 210 else if (cmd->out)
f4bba25b
SP
211 close(cmd->out);
212
f3b33f1d
JS
213 if (need_err)
214 close(fderr[1]);
215
ebcb5d16
SP
216 return 0;
217}
218
2d22c208 219static int wait_or_whine(pid_t pid)
ebcb5d16 220{
b1bf95bb
JW
221 for (;;) {
222 int status, code;
2d22c208 223 pid_t waiting = waitpid(pid, &status, 0);
b1bf95bb 224
6f002f98 225 if (waiting < 0) {
b1bf95bb
JW
226 if (errno == EINTR)
227 continue;
6f002f98 228 error("waitpid failed (%s)", strerror(errno));
b1bf95bb
JW
229 return -ERR_RUN_COMMAND_WAITPID;
230 }
2d22c208 231 if (waiting != pid)
b1bf95bb
JW
232 return -ERR_RUN_COMMAND_WAITPID_WRONG_PID;
233 if (WIFSIGNALED(status))
234 return -ERR_RUN_COMMAND_WAITPID_SIGNAL;
235
236 if (!WIFEXITED(status))
237 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
238 code = WEXITSTATUS(status);
239 if (code)
240 return -code;
241 return 0;
242 }
243}
f1000898 244
2d22c208
JS
245int finish_command(struct child_process *cmd)
246{
2d22c208
JS
247 return wait_or_whine(cmd->pid);
248}
249
ebcb5d16
SP
250int run_command(struct child_process *cmd)
251{
252 int code = start_command(cmd);
253 if (code)
254 return code;
255 return finish_command(cmd);
256}
257
1568fea0 258static void prepare_run_command_v_opt(struct child_process *cmd,
ee493148
AR
259 const char **argv,
260 int opt)
1568fea0
AR
261{
262 memset(cmd, 0, sizeof(*cmd));
263 cmd->argv = argv;
264 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
265 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
266 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
267}
268
f1000898
SP
269int run_command_v_opt(const char **argv, int opt)
270{
271 struct child_process cmd;
1568fea0
AR
272 prepare_run_command_v_opt(&cmd, argv, opt);
273 return run_command(&cmd);
274}
275
276int run_command_v_opt_cd(const char **argv, int opt, const char *dir)
277{
278 struct child_process cmd;
279 prepare_run_command_v_opt(&cmd, argv, opt);
280 cmd.dir = dir;
f1000898
SP
281 return run_command(&cmd);
282}
ee493148
AR
283
284int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env)
285{
286 struct child_process cmd;
287 prepare_run_command_v_opt(&cmd, argv, opt);
288 cmd.dir = dir;
289 cmd.env = env;
290 return run_command(&cmd);
291}
2d22c208 292
618ebe9f
JS
293#ifdef __MINGW32__
294static __stdcall unsigned run_thread(void *data)
295{
296 struct async *async = data;
297 return async->proc(async->fd_for_proc, async->data);
298}
299#endif
300
2d22c208
JS
301int start_async(struct async *async)
302{
303 int pipe_out[2];
304
305 if (pipe(pipe_out) < 0)
306 return error("cannot create pipe: %s", strerror(errno));
618ebe9f 307 async->out = pipe_out[0];
2d22c208 308
618ebe9f 309#ifndef __MINGW32__
2d22c208
JS
310 async->pid = fork();
311 if (async->pid < 0) {
312 error("fork (async) failed: %s", strerror(errno));
313 close_pair(pipe_out);
314 return -1;
315 }
316 if (!async->pid) {
317 close(pipe_out[0]);
318 exit(!!async->proc(pipe_out[1], async->data));
319 }
2d22c208 320 close(pipe_out[1]);
618ebe9f
JS
321#else
322 async->fd_for_proc = pipe_out[1];
323 async->tid = (HANDLE) _beginthreadex(NULL, 0, run_thread, async, 0, NULL);
324 if (!async->tid) {
325 error("cannot create thread: %s", strerror(errno));
326 close_pair(pipe_out);
327 return -1;
328 }
329#endif
2d22c208
JS
330 return 0;
331}
332
333int finish_async(struct async *async)
334{
618ebe9f 335#ifndef __MINGW32__
2d22c208
JS
336 int ret = 0;
337
338 if (wait_or_whine(async->pid))
339 ret = error("waitpid (async) failed");
618ebe9f
JS
340#else
341 DWORD ret = 0;
342 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
343 ret = error("waiting for thread failed: %lu", GetLastError());
344 else if (!GetExitCodeThread(async->tid, &ret))
345 ret = error("cannot get thread exit code: %lu", GetLastError());
346 CloseHandle(async->tid);
347#endif
2d22c208
JS
348 return ret;
349}