@matheusbsbs / matheus-bagliano / commits / c024beb56d
Fork 0

run_command: report failure to execute the program, but optionally don't

In the case where a program was not found, it was still the task of the caller to report an error to the user. Usually, this is an interesting case but only few callers actually reported a specific error (though many call sites report a generic error message regardless of the cause). With this change the error is reported by run_command, but since there is one call site in git.c that does not want that, an option is added to struct child_process, which is used to turn the error off. Signed-off-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Sixt committed Jul 4, 2009 at 21:26 UTC c024beb56da679839d61f352d088b9a86823233a
5 files changed +13 -13
builtin-receive-pack.c
+1 -3
@@ -125,9 +125,7 @@ static const char post_receive_hook[] = "hooks/post-receive";
125
126 static int run_status(int code, const char *cmd_name)
127 {
128 - if (code < 0 && errno == ENOENT)
129 - return error("execute of %s failed", cmd_name);
130 - else if (code > 0)
128 + if (code > 0)
129 error("%s exited with error code %d", cmd_name, code);
130 return code;
131 }
git.c
+1 -1
@@ -416,7 +416,7 @@ static void execv_dashed_external(const char **argv)
416 * if we fail because the command is not found, it is
417 * OK to return. Otherwise, we just pass along the status code.
418 */
419 - status = run_command_v_opt(argv, 0);
419 + status = run_command_v_opt(argv, RUN_SILENT_EXEC_FAILURE);
420 if (status >= 0 || errno != ENOENT)
421 exit(status);
422
run-command.c
+8 -4
@@ -185,7 +185,7 @@ fail_pipe:
185
186 cmd->pid = mingw_spawnvpe(cmd->argv[0], cmd->argv, env);
187 failed_errno = errno;
188 - if (cmd->pid < 0 && errno != ENOENT)
188 + if (cmd->pid < 0 && (!cmd->silent_exec_failure || errno != ENOENT))
189 error("cannot spawn %s: %s", cmd->argv[0], strerror(errno));
190
191 if (cmd->env)
@@ -233,7 +233,7 @@ fail_pipe:
233 return 0;
234 }
235
236 -static int wait_or_whine(pid_t pid, const char *argv0)
236 +static int wait_or_whine(pid_t pid, const char *argv0, int silent_exec_failure)
237 {
238 int status, code = -1;
239 pid_t waiting;
@@ -264,6 +264,9 @@ static int wait_or_whine(pid_t pid, const char *argv0)
264 if (code == 127) {
265 code = -1;
266 failed_errno = ENOENT;
267 + if (!silent_exec_failure)
268 + error("cannot run %s: %s", argv0,
269 + strerror(ENOENT));
270 }
271 } else {
272 error("waitpid is confused (%s)", argv0);
@@ -274,7 +277,7 @@ static int wait_or_whine(pid_t pid, const char *argv0)
277
278 int finish_command(struct child_process *cmd)
279 {
277 - return wait_or_whine(cmd->pid, cmd->argv[0]);
280 + return wait_or_whine(cmd->pid, cmd->argv[0], cmd->silent_exec_failure);
281 }
282
283 int run_command(struct child_process *cmd)
@@ -294,6 +297,7 @@ static void prepare_run_command_v_opt(struct child_process *cmd,
297 cmd->no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
298 cmd->git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
299 cmd->stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
300 + cmd->silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
301 }
302
303 int run_command_v_opt(const char **argv, int opt)
@@ -358,7 +362,7 @@ int start_async(struct async *async)
362 int finish_async(struct async *async)
363 {
364 #ifndef __MINGW32__
361 - int ret = wait_or_whine(async->pid, "child process");
365 + int ret = wait_or_whine(async->pid, "child process", 0);
366 #else
367 DWORD ret = 0;
368 if (WaitForSingleObject(async->tid, INFINITE) != WAIT_OBJECT_0)
run-command.h
+2
@@ -31,6 +31,7 @@ struct child_process {
31 unsigned no_stdout:1;
32 unsigned no_stderr:1;
33 unsigned git_cmd:1; /* if this is to be git sub-command */
34 + unsigned silent_exec_failure:1;
35 unsigned stdout_to_stderr:1;
36 void (*preexec_cb)(void);
37 };
@@ -44,6 +45,7 @@ extern int run_hook(const char *index_file, const char *name, ...);
45 #define RUN_COMMAND_NO_STDIN 1
46 #define RUN_GIT_CMD 2 /*If this is to be git sub-command */
47 #define RUN_COMMAND_STDOUT_TO_STDERR 4
48 +#define RUN_SILENT_EXEC_FAILURE 8
49 int run_command_v_opt(const char **argv, int opt);
50
51 /*
transport.c
+1 -5
@@ -396,7 +396,6 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons
396 {
397 const char **argv;
398 int argc;
399 - int err;
399
400 if (flags & TRANSPORT_PUSH_MIRROR)
401 return error("http transport does not support mirror mode");
@@ -416,10 +415,7 @@ static int curl_transport_push(struct transport *transport, int refspec_nr, cons
415 while (refspec_nr--)
416 argv[argc++] = *refspec++;
417 argv[argc] = NULL;
419 - err = run_command_v_opt(argv, RUN_GIT_CMD);
420 - if (err < 0 && errno == ENOENT)
421 - error("unable to exec %s", argv[0]);
422 - return !!err;
418 + return !!run_command_v_opt(argv, RUN_GIT_CMD);
419 }
420
421 static struct ref *get_refs_via_curl(struct transport *transport, int for_push)