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>
This commit is contained in:
Ævar Arnfjörð Bjarmason 2021-11-25 23:52:21 +01:00 committed by Junio C Hamano
parent 2b7098936c
commit 7f14609e29
6 changed files with 31 additions and 53 deletions

View File

@ -430,7 +430,6 @@ static int write_tar_filter_archive(const struct archiver *ar,
{ {
struct strbuf cmd = STRBUF_INIT; struct strbuf cmd = STRBUF_INIT;
struct child_process filter = CHILD_PROCESS_INIT; struct child_process filter = CHILD_PROCESS_INIT;
const char *argv[2];
int r; int r;
if (!ar->data) if (!ar->data)
@ -440,14 +439,12 @@ static int write_tar_filter_archive(const struct archiver *ar,
if (args->compression_level >= 0) if (args->compression_level >= 0)
strbuf_addf(&cmd, " -%d", args->compression_level); strbuf_addf(&cmd, " -%d", args->compression_level);
argv[0] = cmd.buf; strvec_push(&filter.args, cmd.buf);
argv[1] = NULL;
filter.argv = argv;
filter.use_shell = 1; filter.use_shell = 1;
filter.in = -1; filter.in = -1;
if (start_command(&filter) < 0) if (start_command(&filter) < 0)
die_errno(_("unable to start '%s' filter"), argv[0]); die_errno(_("unable to start '%s' filter"), cmd.buf);
close(1); close(1);
if (dup2(filter.in, 1) < 0) if (dup2(filter.in, 1) < 0)
die_errno(_("unable to redirect descriptor")); die_errno(_("unable to redirect descriptor"));
@ -457,7 +454,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
close(1); close(1);
if (finish_command(&filter) != 0) if (finish_command(&filter) != 0)
die(_("'%s' filter reported error"), argv[0]); die(_("'%s' filter reported error"), cmd.buf);
strbuf_release(&cmd); strbuf_release(&cmd);
return r; return r;

View File

@ -812,16 +812,13 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
{ {
struct child_process proc = CHILD_PROCESS_INIT; struct child_process proc = CHILD_PROCESS_INIT;
struct async muxer; struct async muxer;
const char *argv[2];
int code; int code;
const char *hook_path = find_hook(hook_name);
argv[0] = find_hook(hook_name); if (!hook_path)
if (!argv[0])
return 0; return 0;
argv[1] = NULL; strvec_push(&proc.args, hook_path);
proc.argv = argv;
proc.in = -1; proc.in = -1;
proc.stdout_to_stderr = 1; proc.stdout_to_stderr = 1;
proc.trace2_hook_name = hook_name; proc.trace2_hook_name = hook_name;
@ -943,23 +940,21 @@ static int run_receive_hook(struct command *commands,
static int run_update_hook(struct command *cmd) static int run_update_hook(struct command *cmd)
{ {
const char *argv[5];
struct child_process proc = CHILD_PROCESS_INIT; struct child_process proc = CHILD_PROCESS_INIT;
int code; int code;
const char *hook_path = find_hook("update");
argv[0] = find_hook("update"); if (!hook_path)
if (!argv[0])
return 0; return 0;
argv[1] = cmd->ref_name; strvec_push(&proc.args, hook_path);
argv[2] = oid_to_hex(&cmd->old_oid); strvec_push(&proc.args, cmd->ref_name);
argv[3] = oid_to_hex(&cmd->new_oid); strvec_push(&proc.args, oid_to_hex(&cmd->old_oid));
argv[4] = NULL; strvec_push(&proc.args, oid_to_hex(&cmd->new_oid));
proc.no_stdin = 1; proc.no_stdin = 1;
proc.stdout_to_stderr = 1; proc.stdout_to_stderr = 1;
proc.err = use_sideband ? -1 : 0; proc.err = use_sideband ? -1 : 0;
proc.argv = argv;
proc.trace2_hook_name = "update"; proc.trace2_hook_name = "update";
code = start_command(&proc); code = start_command(&proc);
@ -1117,22 +1112,20 @@ static int run_proc_receive_hook(struct command *commands,
struct child_process proc = CHILD_PROCESS_INIT; struct child_process proc = CHILD_PROCESS_INIT;
struct async muxer; struct async muxer;
struct command *cmd; struct command *cmd;
const char *argv[2];
struct packet_reader reader; struct packet_reader reader;
struct strbuf cap = STRBUF_INIT; struct strbuf cap = STRBUF_INIT;
struct strbuf errmsg = STRBUF_INIT; struct strbuf errmsg = STRBUF_INIT;
int hook_use_push_options = 0; int hook_use_push_options = 0;
int version = 0; int version = 0;
int code; int code;
const char *hook_path = find_hook("proc-receive");
argv[0] = find_hook("proc-receive"); if (!hook_path) {
if (!argv[0]) {
rp_error("cannot find hook 'proc-receive'"); rp_error("cannot find hook 'proc-receive'");
return -1; return -1;
} }
argv[1] = NULL;
proc.argv = argv; strvec_push(&proc.args, hook_path);
proc.in = -1; proc.in = -1;
proc.out = -1; proc.out = -1;
proc.trace2_hook_name = "proc-receive"; proc.trace2_hook_name = "proc-receive";

View File

@ -326,22 +326,18 @@ static int run_access_hook(struct daemon_service *service, const char *dir,
{ {
struct child_process child = CHILD_PROCESS_INIT; struct child_process child = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
const char *argv[8];
const char **arg = argv;
char *eol; char *eol;
int seen_errors = 0; int seen_errors = 0;
*arg++ = access_hook; strvec_push(&child.args, access_hook);
*arg++ = service->name; strvec_push(&child.args, service->name);
*arg++ = path; strvec_push(&child.args, path);
*arg++ = hi->hostname.buf; strvec_push(&child.args, hi->hostname.buf);
*arg++ = get_canon_hostname(hi); strvec_push(&child.args, get_canon_hostname(hi));
*arg++ = get_ip_address(hi); strvec_push(&child.args, get_ip_address(hi));
*arg++ = hi->tcp_port.buf; strvec_push(&child.args, hi->tcp_port.buf);
*arg = NULL;
child.use_shell = 1; child.use_shell = 1;
child.argv = argv;
child.no_stdin = 1; child.no_stdin = 1;
child.no_stderr = 1; child.no_stderr = 1;
child.out = -1; child.out = -1;

8
diff.c
View File

@ -6921,19 +6921,15 @@ static char *run_textconv(struct repository *r,
size_t *outsize) size_t *outsize)
{ {
struct diff_tempfile *temp; struct diff_tempfile *temp;
const char *argv[3];
const char **arg = argv;
struct child_process child = CHILD_PROCESS_INIT; struct child_process child = CHILD_PROCESS_INIT;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
int err = 0; int err = 0;
temp = prepare_temp_file(r, spec->path, spec); temp = prepare_temp_file(r, spec->path, spec);
*arg++ = pgm; strvec_push(&child.args, pgm);
*arg++ = temp->name; strvec_push(&child.args, temp->name);
*arg = NULL;
child.use_shell = 1; child.use_shell = 1;
child.argv = argv;
child.out = -1; child.out = -1;
if (start_command(&child)) { if (start_command(&child)) {
remove_tempfile(); remove_tempfile();

View File

@ -8,15 +8,12 @@
static char *do_askpass(const char *cmd, const char *prompt) static char *do_askpass(const char *cmd, const char *prompt)
{ {
struct child_process pass = CHILD_PROCESS_INIT; struct child_process pass = CHILD_PROCESS_INIT;
const char *args[3];
static struct strbuf buffer = STRBUF_INIT; static struct strbuf buffer = STRBUF_INIT;
int err = 0; int err = 0;
args[0] = cmd; strvec_push(&pass.args, cmd);
args[1] = prompt; strvec_push(&pass.args, prompt);
args[2] = NULL;
pass.argv = args;
pass.out = -1; pass.out = -1;
if (start_command(&pass)) if (start_command(&pass))

View File

@ -1204,16 +1204,15 @@ static int run_pre_push_hook(struct transport *transport,
struct ref *r; struct ref *r;
struct child_process proc = CHILD_PROCESS_INIT; struct child_process proc = CHILD_PROCESS_INIT;
struct strbuf buf; struct strbuf buf;
const char *argv[4]; const char *hook_path = find_hook("pre-push");
if (!(argv[0] = find_hook("pre-push"))) if (!hook_path)
return 0; return 0;
argv[1] = transport->remote->name; strvec_push(&proc.args, hook_path);
argv[2] = transport->url; strvec_push(&proc.args, transport->remote->name);
argv[3] = NULL; strvec_push(&proc.args, transport->url);
proc.argv = argv;
proc.in = -1; proc.in = -1;
proc.trace2_hook_name = "pre-push"; proc.trace2_hook_name = "pre-push";