run-command: factor out child_process_clear()

Avoid duplication by moving the code to release allocated memory for arguments and environment to its own function, child_process_clear(). Export it to provide a counterpart to child_process_init(). Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Oct 24, 2015 at 14:11 UTC 2d71608ec0b6d098ed7c8c914fb7004b28349995
3 files changed +17 -6
Documentation/technical/api-run-command.txt
+7
@@ -46,6 +46,13 @@ Functions
46 The argument dir corresponds the member .dir. The argument env
47 corresponds to the member .env.
48
49 +`child_process_clear`::
50 +
51 + Release the memory associated with the struct child_process.
52 + Most users of the run-command API don't need to call this
53 + function explicitly because `start_command` invokes it on
54 + failure and `finish_command` calls it automatically already.
55 +
56 The functions above do the following:
57
58 . If a system call failed, errno is set and -1 is returned. A diagnostic
run-command.c
+9 -6
@@ -11,6 +11,12 @@ void child_process_init(struct child_process *child)
11 argv_array_init(&child->env_array);
12 }
13
14 +void child_process_clear(struct child_process *child)
15 +{
16 + argv_array_clear(&child->args);
17 + argv_array_clear(&child->env_array);
18 +}
19 +
20 struct child_to_clean {
21 pid_t pid;
22 struct child_to_clean *next;
@@ -336,8 +342,7 @@ int start_command(struct child_process *cmd)
342 fail_pipe:
343 error("cannot create %s pipe for %s: %s",
344 str, cmd->argv[0], strerror(failed_errno));
339 - argv_array_clear(&cmd->args);
340 - argv_array_clear(&cmd->env_array);
345 + child_process_clear(cmd);
346 errno = failed_errno;
347 return -1;
348 }
@@ -523,8 +528,7 @@ fail_pipe:
528 close_pair(fderr);
529 else if (cmd->err)
530 close(cmd->err);
526 - argv_array_clear(&cmd->args);
527 - argv_array_clear(&cmd->env_array);
531 + child_process_clear(cmd);
532 errno = failed_errno;
533 return -1;
534 }
@@ -550,8 +554,7 @@ fail_pipe:
554 int finish_command(struct child_process *cmd)
555 {
556 int ret = wait_or_whine(cmd->pid, cmd->argv[0]);
553 - argv_array_clear(&cmd->args);
554 - argv_array_clear(&cmd->env_array);
557 + child_process_clear(cmd);
558 return ret;
559 }
560
run-command.h
+1
@@ -47,6 +47,7 @@ struct child_process {
47
48 #define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT, ARGV_ARRAY_INIT }
49 void child_process_init(struct child_process *);
50 +void child_process_clear(struct child_process *);
51
52 int start_command(struct child_process *);
53 int finish_command(struct child_process *);