run-command API users: use strvec_push(), not argv construction

Change a pattern of hardcoding an "argv" array size, populating it and assigning to the "argv" member of "struct child_process" to instead use "strvec_push()" to add data to the "args" member. As noted in the preceding commit this moves us further towards being able to remove the "argv" member in a subsequent commit These callers could have used strvec_pushl(), but moving to strvec_push() makes the diff easier to read, and keeps the arguments aligned as before. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Nov 25, 2021 at 23:52 UTC 7f14609e296d5737f34607d4c3e40bb1b063ef04
6 files changed +31 -53
archive-tar.c
+3 -6
@@ -430,7 +430,6 @@ static int write_tar_filter_archive(const struct archiver *ar,
430 {
431 struct strbuf cmd = STRBUF_INIT;
432 struct child_process filter = CHILD_PROCESS_INIT;
433 - const char *argv[2];
433 int r;
434
435 if (!ar->data)
@@ -440,14 +439,12 @@ static int write_tar_filter_archive(const struct archiver *ar,
439 if (args->compression_level >= 0)
440 strbuf_addf(&cmd, " -%d", args->compression_level);
441
443 - argv[0] = cmd.buf;
444 - argv[1] = NULL;
445 - filter.argv = argv;
442 + strvec_push(&filter.args, cmd.buf);
443 filter.use_shell = 1;
444 filter.in = -1;
445
446 if (start_command(&filter) < 0)
450 - die_errno(_("unable to start '%s' filter"), argv[0]);
447 + die_errno(_("unable to start '%s' filter"), cmd.buf);
448 close(1);
449 if (dup2(filter.in, 1) < 0)
450 die_errno(_("unable to redirect descriptor"));
@@ -457,7 +454,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
454
455 close(1);
456 if (finish_command(&filter) != 0)
460 - die(_("'%s' filter reported error"), argv[0]);
457 + die(_("'%s' filter reported error"), cmd.buf);
458
459 strbuf_release(&cmd);
460 return r;
builtin/receive-pack.c
+12 -19
@@ -812,16 +812,13 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
812 {
813 struct child_process proc = CHILD_PROCESS_INIT;
814 struct async muxer;
815 - const char *argv[2];
815 int code;
816 + const char *hook_path = find_hook(hook_name);
817
818 - argv[0] = find_hook(hook_name);
819 - if (!argv[0])
818 + if (!hook_path)
819 return 0;
820
822 - argv[1] = NULL;
823 -
824 - proc.argv = argv;
821 + strvec_push(&proc.args, hook_path);
822 proc.in = -1;
823 proc.stdout_to_stderr = 1;
824 proc.trace2_hook_name = hook_name;
@@ -943,23 +940,21 @@ static int run_receive_hook(struct command *commands,
940
941 static int run_update_hook(struct command *cmd)
942 {
946 - const char *argv[5];
943 struct child_process proc = CHILD_PROCESS_INIT;
944 int code;
945 + const char *hook_path = find_hook("update");
946
950 - argv[0] = find_hook("update");
951 - if (!argv[0])
947 + if (!hook_path)
948 return 0;
949
954 - argv[1] = cmd->ref_name;
955 - argv[2] = oid_to_hex(&cmd->old_oid);
956 - argv[3] = oid_to_hex(&cmd->new_oid);
957 - argv[4] = NULL;
950 + strvec_push(&proc.args, hook_path);
951 + strvec_push(&proc.args, cmd->ref_name);
952 + strvec_push(&proc.args, oid_to_hex(&cmd->old_oid));
953 + strvec_push(&proc.args, oid_to_hex(&cmd->new_oid));
954
955 proc.no_stdin = 1;
956 proc.stdout_to_stderr = 1;
957 proc.err = use_sideband ? -1 : 0;
962 - proc.argv = argv;
958 proc.trace2_hook_name = "update";
959
960 code = start_command(&proc);
@@ -1117,22 +1112,20 @@ static int run_proc_receive_hook(struct command *commands,
1112 struct child_process proc = CHILD_PROCESS_INIT;
1113 struct async muxer;
1114 struct command *cmd;
1120 - const char *argv[2];
1115 struct packet_reader reader;
1116 struct strbuf cap = STRBUF_INIT;
1117 struct strbuf errmsg = STRBUF_INIT;
1118 int hook_use_push_options = 0;
1119 int version = 0;
1120 int code;
1121 + const char *hook_path = find_hook("proc-receive");
1122
1128 - argv[0] = find_hook("proc-receive");
1129 - if (!argv[0]) {
1123 + if (!hook_path) {
1124 rp_error("cannot find hook 'proc-receive'");
1125 return -1;
1126 }
1133 - argv[1] = NULL;
1127
1135 - proc.argv = argv;
1128 + strvec_push(&proc.args, hook_path);
1129 proc.in = -1;
1130 proc.out = -1;
1131 proc.trace2_hook_name = "proc-receive";
daemon.c
+7 -11
@@ -326,22 +326,18 @@ static int run_access_hook(struct daemon_service *service, const char *dir,
326 {
327 struct child_process child = CHILD_PROCESS_INIT;
328 struct strbuf buf = STRBUF_INIT;
329 - const char *argv[8];
330 - const char **arg = argv;
329 char *eol;
330 int seen_errors = 0;
331
334 - *arg++ = access_hook;
335 - *arg++ = service->name;
336 - *arg++ = path;
337 - *arg++ = hi->hostname.buf;
338 - *arg++ = get_canon_hostname(hi);
339 - *arg++ = get_ip_address(hi);
340 - *arg++ = hi->tcp_port.buf;
341 - *arg = NULL;
332 + strvec_push(&child.args, access_hook);
333 + strvec_push(&child.args, service->name);
334 + strvec_push(&child.args, path);
335 + strvec_push(&child.args, hi->hostname.buf);
336 + strvec_push(&child.args, get_canon_hostname(hi));
337 + strvec_push(&child.args, get_ip_address(hi));
338 + strvec_push(&child.args, hi->tcp_port.buf);
339
340 child.use_shell = 1;
344 - child.argv = argv;
341 child.no_stdin = 1;
342 child.no_stderr = 1;
343 child.out = -1;
diff.c
+2 -6
@@ -6921,19 +6921,15 @@ static char *run_textconv(struct repository *r,
6921 size_t *outsize)
6922 {
6923 struct diff_tempfile *temp;
6924 - const char *argv[3];
6925 - const char **arg = argv;
6924 struct child_process child = CHILD_PROCESS_INIT;
6925 struct strbuf buf = STRBUF_INIT;
6926 int err = 0;
6927
6928 temp = prepare_temp_file(r, spec->path, spec);
6931 - *arg++ = pgm;
6932 - *arg++ = temp->name;
6933 - *arg = NULL;
6929 + strvec_push(&child.args, pgm);
6930 + strvec_push(&child.args, temp->name);
6931
6932 child.use_shell = 1;
6936 - child.argv = argv;
6933 child.out = -1;
6934 if (start_command(&child)) {
6935 remove_tempfile();
prompt.c
+2 -5
@@ -8,15 +8,12 @@
8 static char *do_askpass(const char *cmd, const char *prompt)
9 {
10 struct child_process pass = CHILD_PROCESS_INIT;
11 - const char *args[3];
11 static struct strbuf buffer = STRBUF_INIT;
12 int err = 0;
13
15 - args[0] = cmd;
16 - args[1] = prompt;
17 - args[2] = NULL;
14 + strvec_push(&pass.args, cmd);
15 + strvec_push(&pass.args, prompt);
16
19 - pass.argv = args;
17 pass.out = -1;
18
19 if (start_command(&pass))
transport.c
+5 -6
@@ -1204,16 +1204,15 @@ static int run_pre_push_hook(struct transport *transport,
1204 struct ref *r;
1205 struct child_process proc = CHILD_PROCESS_INIT;
1206 struct strbuf buf;
1207 - const char *argv[4];
1207 + const char *hook_path = find_hook("pre-push");
1208
1209 - if (!(argv[0] = find_hook("pre-push")))
1209 + if (!hook_path)
1210 return 0;
1211
1212 - argv[1] = transport->remote->name;
1213 - argv[2] = transport->url;
1214 - argv[3] = NULL;
1212 + strvec_push(&proc.args, hook_path);
1213 + strvec_push(&proc.args, transport->remote->name);
1214 + strvec_push(&proc.args, transport->url);
1215
1216 - proc.argv = argv;
1216 proc.in = -1;
1217 proc.trace2_hook_name = "pre-push";
1218