Skip to content

Commit d318027

Browse files
rscharfegitster
authored andcommitted
run-command: introduce CHILD_PROCESS_INIT
Most struct child_process variables are cleared using memset first after declaration. Provide a macro, CHILD_PROCESS_INIT, that can be used to initialize them statically instead. That's shorter, doesn't require a function call and is slightly more readable (especially given that we already have STRBUF_INIT, ARGV_ARRAY_INIT etc.). Helped-by: Johannes Sixt <[email protected]> Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6c4ab27 commit d318027

40 files changed

+60
-107
lines changed

Documentation/technical/api-run-command.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ command to run in a sub-process.
9696

9797
The caller:
9898

99-
1. allocates and clears (memset(&chld, 0, sizeof(chld));) a
100-
struct child_process variable;
99+
1. allocates and clears (memset(&chld, 0, sizeof(chld)); or
100+
using CHILD_PROCESS_INIT) a struct child_process variable;
101101
2. initializes the members;
102102
3. calls start_command();
103103
4. processes the data;

archive-tar.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
395395
struct archiver_args *args)
396396
{
397397
struct strbuf cmd = STRBUF_INIT;
398-
struct child_process filter;
398+
struct child_process filter = CHILD_PROCESS_INIT;
399399
const char *argv[2];
400400
int r;
401401

@@ -406,7 +406,6 @@ static int write_tar_filter_archive(const struct archiver *ar,
406406
if (args->compression_level >= 0)
407407
strbuf_addf(&cmd, " -%d", args->compression_level);
408408

409-
memset(&filter, 0, sizeof(filter));
410409
argv[0] = cmd.buf;
411410
argv[1] = NULL;
412411
filter.argv = argv;

builtin/add.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
180180
char *file = git_pathdup("ADD_EDIT.patch");
181181
const char *apply_argv[] = { "apply", "--recount", "--cached",
182182
NULL, NULL };
183-
struct child_process child;
183+
struct child_process child = CHILD_PROCESS_INIT;
184184
struct rev_info rev;
185185
int out;
186186
struct stat st;
@@ -214,7 +214,6 @@ static int edit_patch(int argc, const char **argv, const char *prefix)
214214
if (!st.st_size)
215215
die(_("Empty patch. Aborted."));
216216

217-
memset(&child, 0, sizeof(child));
218217
child.git_cmd = 1;
219218
child.argv = apply_argv;
220219
if (run_command(&child))

builtin/commit.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,7 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
15081508
{
15091509
/* oldsha1 SP newsha1 LF NUL */
15101510
static char buf[2*40 + 3];
1511-
struct child_process proc;
1511+
struct child_process proc = CHILD_PROCESS_INIT;
15121512
const char *argv[3];
15131513
int code;
15141514
size_t n;
@@ -1520,7 +1520,6 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
15201520
argv[1] = "amend";
15211521
argv[2] = NULL;
15221522

1523-
memset(&proc, 0, sizeof(proc));
15241523
proc.argv = argv;
15251524
proc.in = -1;
15261525
proc.stdout_to_stderr = 1;

builtin/help.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ static const char *get_man_viewer_info(const char *name)
7979
static int check_emacsclient_version(void)
8080
{
8181
struct strbuf buffer = STRBUF_INIT;
82-
struct child_process ec_process;
82+
struct child_process ec_process = CHILD_PROCESS_INIT;
8383
const char *argv_ec[] = { "emacsclient", "--version", NULL };
8484
int version;
8585

8686
/* emacsclient prints its version number on stderr */
87-
memset(&ec_process, 0, sizeof(ec_process));
8887
ec_process.argv = argv_ec;
8988
ec_process.err = -1;
9089
ec_process.stdout_to_stderr = 1;

builtin/merge.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,10 @@ static void drop_save(void)
237237
static int save_state(unsigned char *stash)
238238
{
239239
int len;
240-
struct child_process cp;
240+
struct child_process cp = CHILD_PROCESS_INIT;
241241
struct strbuf buffer = STRBUF_INIT;
242242
const char *argv[] = {"stash", "create", NULL};
243243

244-
memset(&cp, 0, sizeof(cp));
245244
cp.argv = argv;
246245
cp.out = -1;
247246
cp.git_cmd = 1;

builtin/notes.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,11 @@ static void write_commented_object(int fd, const unsigned char *object)
122122
{
123123
const char *show_args[5] =
124124
{"show", "--stat", "--no-notes", sha1_to_hex(object), NULL};
125-
struct child_process show;
125+
struct child_process show = CHILD_PROCESS_INIT;
126126
struct strbuf buf = STRBUF_INIT;
127127
struct strbuf cbuf = STRBUF_INIT;
128128

129129
/* Invoke "git show --stat --no-notes $object" */
130-
memset(&show, 0, sizeof(show));
131130
show.argv = show_args;
132131
show.no_stdin = 1;
133132
show.out = -1;

builtin/receive-pack.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ static int copy_to_sideband(int in, int out, void *arg)
255255
typedef int (*feed_fn)(void *, const char **, size_t *);
256256
static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
257257
{
258-
struct child_process proc;
258+
struct child_process proc = CHILD_PROCESS_INIT;
259259
struct async muxer;
260260
const char *argv[2];
261261
int code;
@@ -266,7 +266,6 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_sta
266266

267267
argv[1] = NULL;
268268

269-
memset(&proc, 0, sizeof(proc));
270269
proc.argv = argv;
271270
proc.in = -1;
272271
proc.stdout_to_stderr = 1;
@@ -350,7 +349,7 @@ static int run_receive_hook(struct command *commands, const char *hook_name,
350349
static int run_update_hook(struct command *cmd)
351350
{
352351
const char *argv[5];
353-
struct child_process proc;
352+
struct child_process proc = CHILD_PROCESS_INIT;
354353
int code;
355354

356355
argv[0] = find_hook("update");
@@ -362,7 +361,6 @@ static int run_update_hook(struct command *cmd)
362361
argv[3] = sha1_to_hex(cmd->new_sha1);
363362
argv[4] = NULL;
364363

365-
memset(&proc, 0, sizeof(proc));
366364
proc.no_stdin = 1;
367365
proc.stdout_to_stderr = 1;
368366
proc.err = use_sideband ? -1 : 0;
@@ -598,7 +596,7 @@ static void run_update_post_hook(struct command *commands)
598596
struct command *cmd;
599597
int argc;
600598
const char **argv;
601-
struct child_process proc;
599+
struct child_process proc = CHILD_PROCESS_INIT;
602600
char *hook;
603601

604602
hook = find_hook("post-update");
@@ -621,7 +619,6 @@ static void run_update_post_hook(struct command *commands)
621619
}
622620
argv[argc] = NULL;
623621

624-
memset(&proc, 0, sizeof(proc));
625622
proc.no_stdin = 1;
626623
proc.stdout_to_stderr = 1;
627624
proc.err = use_sideband ? -1 : 0;
@@ -911,7 +908,7 @@ static const char *unpack(int err_fd, struct shallow_info *si)
911908
const char *hdr_err;
912909
int status;
913910
char hdr_arg[38];
914-
struct child_process child;
911+
struct child_process child = CHILD_PROCESS_INIT;
915912
int fsck_objects = (receive_fsck_objects >= 0
916913
? receive_fsck_objects
917914
: transfer_fsck_objects >= 0
@@ -933,7 +930,6 @@ static const char *unpack(int err_fd, struct shallow_info *si)
933930
argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
934931
}
935932

936-
memset(&child, 0, sizeof(child));
937933
if (ntohl(hdr.hdr_entries) < unpack_limit) {
938934
argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
939935
if (quiet)

builtin/remote-ext.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ static void send_git_request(int stdin_fd, const char *serv, const char *repo,
179179
static int run_child(const char *arg, const char *service)
180180
{
181181
int r;
182-
struct child_process child;
182+
struct child_process child = CHILD_PROCESS_INIT;
183183

184-
memset(&child, 0, sizeof(child));
185184
child.in = -1;
186185
child.out = -1;
187186
child.err = 0;

builtin/repack.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
133133
{".idx"},
134134
{".bitmap", 1},
135135
};
136-
struct child_process cmd;
136+
struct child_process cmd = CHILD_PROCESS_INIT;
137137
struct string_list_item *item;
138138
struct argv_array cmd_args = ARGV_ARRAY_INIT;
139139
struct string_list names = STRING_LIST_INIT_DUP;
@@ -250,7 +250,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
250250

251251
argv_array_push(&cmd_args, packtmp);
252252

253-
memset(&cmd, 0, sizeof(cmd));
254253
cmd.argv = cmd_args.argv;
255254
cmd.git_cmd = 1;
256255
cmd.out = -1;

0 commit comments

Comments
 (0)