Skip to content

Commit 483bbd4

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d318027 commit 483bbd4

File tree

5 files changed

+20
-6
lines changed

5 files changed

+20
-6
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ produces in the caller in order to process it.
1313
Functions
1414
---------
1515

16+
`child_process_init`
17+
18+
Initialize a struct child_process variable.
19+
1620
`start_command`::
1721

1822
Start a sub-process. Takes a pointer to a `struct child_process`
@@ -96,8 +100,8 @@ command to run in a sub-process.
96100

97101
The caller:
98102

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;
101105
2. initializes the members;
102106
3. calls start_command();
103107
4. processes the data;

connect.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,8 @@ static struct child_process *git_proxy_connect(int fd[2], char *host)
537537

538538
get_host_and_port(&host, &port);
539539

540-
proxy = xcalloc(1, sizeof(*proxy));
540+
proxy = xmalloc(sizeof(*proxy));
541+
child_process_init(proxy);
541542
argv_array_push(&proxy->args, git_proxy_command);
542543
argv_array_push(&proxy->args, host);
543544
argv_array_push(&proxy->args, port);
@@ -694,7 +695,8 @@ struct child_process *git_connect(int fd[2], const char *url,
694695
target_host, 0);
695696
free(target_host);
696697
} else {
697-
conn = xcalloc(1, sizeof(*conn));
698+
conn = xmalloc(sizeof(*conn));
699+
child_process_init(conn);
698700

699701
strbuf_addstr(&cmd, prog);
700702
strbuf_addch(&cmd, ' ');

run-command.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
# define SHELL_PATH "/bin/sh"
99
#endif
1010

11+
void child_process_init(struct child_process *child)
12+
{
13+
memset(child, 0, sizeof(*child));
14+
argv_array_init(&child->args);
15+
}
16+
1117
struct child_to_clean {
1218
pid_t pid;
1319
struct child_to_clean *next;

run-command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct child_process {
4545
};
4646

4747
#define CHILD_PROCESS_INIT { NULL, ARGV_ARRAY_INIT }
48+
void child_process_init(struct child_process *);
4849

4950
int start_command(struct child_process *);
5051
int finish_command(struct child_process *);

transport-helper.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static struct child_process *get_helper(struct transport *transport)
118118
if (data->helper)
119119
return data->helper;
120120

121-
helper = xcalloc(1, sizeof(*helper));
121+
helper = xmalloc(sizeof(*helper));
122+
child_process_init(helper);
122123
helper->in = -1;
123124
helper->out = -1;
124125
helper->err = 0;
@@ -395,7 +396,7 @@ static int get_importer(struct transport *transport, struct child_process *fasti
395396
struct child_process *helper = get_helper(transport);
396397
struct helper_data *data = transport->data;
397398
int cat_blob_fd, code;
398-
memset(fastimport, 0, sizeof(*fastimport));
399+
child_process_init(fastimport);
399400
fastimport->in = helper->out;
400401
argv_array_push(&fastimport->args, "fast-import");
401402
argv_array_push(&fastimport->args, debug ? "--stats" : "--quiet");

0 commit comments

Comments
 (0)