Documentation/git-pull.txt: Add subtitles above included option files
[gitweb.git] / run-command.c
index 30c2b3dd8826acd3b50ab18e8efb4ff473fd7cf2..cf2d8f7fae1356e50736cb9d599625df79738a2a 100644 (file)
@@ -19,7 +19,7 @@ int start_command(struct child_process *cmd)
 {
        int need_in, need_out, need_err;
        int fdin[2], fdout[2], fderr[2];
-       int failed_errno;
+       int failed_errno = failed_errno;
 
        /*
         * In case of errors we must keep the promise to close FDs
@@ -75,7 +75,7 @@ int start_command(struct child_process *cmd)
 
        trace_argv_printf(cmd->argv, "trace: run_command:");
 
-#ifndef __MINGW32__
+#ifndef WIN32
        fflush(NULL);
        cmd->pid = fork();
        if (!cmd->pid) {
@@ -109,8 +109,8 @@ int start_command(struct child_process *cmd)
                }
 
                if (cmd->dir && chdir(cmd->dir))
-                       die("exec %s: cd to %s failed (%s)", cmd->argv[0],
-                           cmd->dir, strerror(errno));
+                       die_errno("exec '%s': cd to '%s' failed", cmd->argv[0],
+                           cmd->dir);
                if (cmd->env) {
                        for (; *cmd->env; cmd->env++) {
                                if (strchr(*cmd->env, '='))
@@ -134,6 +134,7 @@ int start_command(struct child_process *cmd)
                error("cannot fork() for %s: %s", cmd->argv[0],
                        strerror(failed_errno = errno));
 #else
+{
        int s0 = -1, s1 = -1, s2 = -1;  /* backups of stdin, stdout, stderr */
        const char **sargv = cmd->argv;
        char **env = environ;
@@ -173,11 +174,8 @@ int start_command(struct child_process *cmd)
 
        if (cmd->dir)
                die("chdir in start_command() not implemented");
-       if (cmd->env) {
-               env = copy_environ();
-               for (; *cmd->env; cmd->env++)
-                       env = env_setenv(env, *cmd->env);
-       }
+       if (cmd->env)
+               env = make_augmented_environ(cmd->env);
 
        if (cmd->git_cmd) {
                cmd->argv = prepare_git_cmd(cmd->argv);
@@ -185,7 +183,7 @@ int start_command(struct child_process *cmd)
 
        cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
        failed_errno = errno;
-       if (cmd->pid < 0 && errno != ENOENT)
+       if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
                error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
 
        if (cmd->env)
@@ -200,6 +198,7 @@ int start_command(struct child_process *cmd)
                dup2(s1, 1), close(s1);
        if (s2 >= 0)
                dup2(s2, 2), close(s2);
+}
 #endif
 
        if (cmd->pid < 0) {
@@ -233,7 +232,7 @@ int start_command(struct child_process *cmd)
        return 0;
 }
 
-static int wait_or_whine(pid_t pid, const char *argv0)
+static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
 {
        int status, code = -1;
        pid_t waiting;
@@ -264,6 +263,9 @@ static int wait_or_whine(pid_t pid, const char *argv0)
                if (code == 127) {
                        code = -1;
                        failed_errno = ENOENT;
+                       if (!silent_exec_failure)
+                               error("cannot run %s: %s", argv0,
+                                       strerror(ENOENT));
                }
        } else {
                error("waitpid is confused (%s)", argv0);
@@ -274,7 +276,7 @@ static int wait_or_whine(pid_t pid, const char *argv0)
 
 int finish_command(struct child_process *cmd)
 {
-       return wait_or_whine(cmd->pid, cmd->argv[0]);
+       return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
 }
 
 int run_command(struct child_process *cmd)
@@ -294,6 +296,7 @@ static void prepare_run_command_v_opt(struct child_process *cmd,
        cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
        cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
        cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
+       cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
 }
 
 int run_command_v_opt(const char **argv, int opt)
@@ -312,8 +315,8 @@ int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const
        return run_command(&cmd);
 }
 
-#ifdef __MINGW32__
-static __stdcall unsigned run_thread(void *data)
+#ifdef WIN32
+static unsigned __stdcall run_thread(void *data)
 {
        struct async *async = data;
        return async->proc(async->fd_for_proc, async->data);
@@ -328,7 +331,7 @@ int start_async(struct async *async)
                return error("cannot create pipe: %s", strerror(errno));
        async->out = pipe_out[0];
 
-#ifndef __MINGW32__
+#ifndef WIN32
        /* Flush stdio before fork() to avoid cloning buffers */
        fflush(NULL);
 
@@ -357,8 +360,8 @@ int start_async(struct async *async)
 
 int finish_async(struct async *async)
 {
-#ifndef __MINGW32__
-       int ret = wait_or_whine(async->pid, "child process");
+#ifndef WIN32
+       int ret = wait_or_whine(async->pid, "child process", 0);
 #else
        DWORD ret = 0;
        if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)