run-command: introduce child_process_init()
Add a helper function for initializing those struct child_process variables for which the macro CHILD_PROCESS_INIT can't be used. Suggested-by: Jeff King <peff@peff.net> Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Aug 19, 2014 at 21:10 UTC
483bbd4e4ce8a5c717cd47d732893317a14c965a
5 files changed
+20
-6
Documentation/technical/api-run-command.txt
+6
-2
@@ -13,6 +13,10 @@ produces in the caller in order to process it.
13
Functions
14
---------
15
16
+`child_process_init`
17
+
18
+ Initialize a struct child_process variable.
19
+
20
`start_command`::
21
22
Start a sub-process. Takes a pointer to a `struct child_process`
@@ -96,8 +100,8 @@ command to run in a sub-process.
100
101
The caller:
102
99
-1. allocates and clears (memset(&chld, 0, sizeof(chld)); or
100
- using CHILD_PROCESS_INIT) a struct child_process variable;
103
+1. allocates and clears (using child_process_init() or
104
+ CHILD_PROCESS_INIT) a struct child_process variable;
105
2. initializes the members;
106
3. calls start_command();
107
4. processes the data;
connect.c
+4
-2
@@ -537,7 +537,8 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
537
538
get_host_and_port(&host, &port);
539
540
- proxy = xcalloc(1, sizeof(*proxy));
540
+ proxy = xmalloc(sizeof(*proxy));
541
+ child_process_init(proxy);
542
argv_array_push(&proxy->args, git_proxy_command);
543
argv_array_push(&proxy->args, host);
544
argv_array_push(&proxy->args, port);
@@ -694,7 +695,8 @@ struct child_process *git_connect(int fd[2], const char *url,
695
target_host, 0);
696
free(target_host);
697
} else {
697
- conn = xcalloc(1, sizeof(*conn));
698
+ conn = xmalloc(sizeof(*conn));
699
+ child_process_init(conn);
700
701
strbuf_addstr(&cmd, prog);
702
strbuf_addch(&cmd, ' ');
run-command.c
+6
@@ -8,6 +8,12 @@
8
# define SHELL_PATH "/bin/sh"
9
#endif
10
11
+void child_process_init(struct child_process *child)
12
+{
13
+ memset(child, 0, sizeof(*child));
14
+ argv_array_init(&child->args);
15
+}
16
+
17
struct child_to_clean {
18
pid_t pid;
19
struct child_to_clean *next;
run-command.h
+1
@@ -45,6 +45,7 @@ struct child_process {
45
};
46
47
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT }
48
+void child_process_init(struct child_process *);
49
50
int start_command(struct child_process *);
51
int finish_command(struct child_process *);
transport-helper.c
+3
-2
@@ -118,7 +118,8 @@ static struct child_process *get_helper(struct transport *transport)
118
if (data->helper)
119
return data->helper;
120
121
- helper = xcalloc(1, sizeof(*helper));
121
+ helper = xmalloc(sizeof(*helper));
122
+ child_process_init(helper);
123
helper->in = -1;
124
helper->out = -1;
125
helper->err = 0;
@@ -395,7 +396,7 @@ static int get_importer(struct transport *transport, struct child_process *fasti
396
struct child_process *helper = get_helper(transport);
397
struct helper_data *data = transport->data;
398
int cat_blob_fd, code;
398
- memset(fastimport, 0, sizeof(*fastimport));
399
+ child_process_init(fastimport);
400
fastimport->in = helper->out;
401
argv_array_push(&fastimport->args, "fast-import");
402
argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");