@matheusbsbs / matheus-bagliano / commits / 5709e0363a
Fork 0

run_command: return exit code as positive value

As a general guideline, functions in git's code return zero to indicate success and negative values to indicate failure. The run_command family of functions followed this guideline. But there are actually two different kinds of failure: - failures of system calls; - non-zero exit code of the program that was run. Usually, a non-zero exit code of the program is a failure and means a failure to the caller. Except that sometimes it does not. For example, the exit code of merge programs (e.g. external merge drivers) conveys information about how the merge failed, and not all exit calls are actually failures. Furthermore, the return value of run_command is sometimes used as exit code by the caller. This change arranges that the exit code of the program is returned as a positive value, which can now be regarded as the "result" of the function. System call failures continue to be reported as negative values. 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 5709e0363a891b72eb9e9756d7fb121d9bf6a7c7
7 files changed +7 -19
builtin-merge.c
+1 -1
@@ -594,7 +594,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
594 discard_cache();
595 if (read_cache() < 0)
596 die("failed to read the cache");
597 - return -ret;
597 + return ret;
598 }
599 }
600
builtin-receive-pack.c
+2 -2
@@ -143,8 +143,8 @@ static int run_status(int code, const char *cmd_name)
143 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
144 return error("%s died strangely", cmd_name);
145 default:
146 - error("%s exited with error code %d", cmd_name, -code);
147 - return -code;
146 + error("%s exited with error code %d", cmd_name, code);
147 + return code;
148 }
149 }
150
convert.c
+1 -1
@@ -267,7 +267,7 @@ static int filter_buffer(int fd, void *data)
267
268 status = finish_command(&child_process);
269 if (status)
270 - error("external filter %s failed %d", params->cmd, -status);
270 + error("external filter %s failed %d", params->cmd, status);
271 return (write_err || status);
272 }
273
git.c
+2 -2
@@ -418,9 +418,9 @@ static void execv_dashed_external(const char **argv)
418 */
419 status = run_command_v_opt(argv, 0);
420 if (status != -ERR_RUN_COMMAND_EXEC) {
421 - if (IS_RUN_COMMAND_ERR(status))
421 + if (status < 0)
422 die("unable to run '%s'", argv[0]);
423 - exit(-status);
423 + exit(status);
424 }
425 errno = ENOENT; /* as if we called execvp */
426
ll-merge.c
-4
@@ -192,10 +192,6 @@ static int ll_ext_merge(const struct ll_merge_driver *fn,
192
193 args[2] = cmd.buf;
194 status = run_command_v_opt(args, 0);
195 - if (status < -ERR_RUN_COMMAND_FORK)
196 - ; /* failure in run-command */
197 - else
198 - status = -status;
195 fd = open(temp[1], O_RDONLY);
196 if (fd < 0)
197 goto bad;
run-command.c
+1 -8
@@ -241,14 +241,7 @@ static int wait_or_whine(pid_t pid)
241 if (!WIFEXITED(status))
242 return -ERR_RUN_COMMAND_WAITPID_NOEXIT;
243 code = WEXITSTATUS(status);
244 - switch (code) {
245 - case 127:
246 - return -ERR_RUN_COMMAND_EXEC;
247 - case 0:
248 - return 0;
249 - default:
250 - return -code;
251 - }
244 + return code == 127 ? -ERR_RUN_COMMAND_EXEC : code;
245 }
246 }
247
run-command.h
-1
@@ -10,7 +10,6 @@ enum {
10 ERR_RUN_COMMAND_WAITPID_SIGNAL,
11 ERR_RUN_COMMAND_WAITPID_NOEXIT,
12 };
13 -#define IS_RUN_COMMAND_ERR(x) (-(x) >= ERR_RUN_COMMAND_FORK)
13
14 struct child_process {
15 const char **argv;