Skip to content

Commit 7f14609

Browse files
avargitster
authored andcommitted
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>
1 parent 2b70989 commit 7f14609

File tree

6 files changed

+31
-53
lines changed

6 files changed

+31
-53
lines changed

archive-tar.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,6 @@ static int write_tar_filter_archive(const struct archiver *ar,
430430
{
431431
struct strbuf cmd = STRBUF_INIT;
432432
struct child_process filter = CHILD_PROCESS_INIT;
433-
const char *argv[2];
434433
int r;
435434

436435
if (!ar->data)
@@ -440,14 +439,12 @@ static int write_tar_filter_archive(const struct archiver *ar,
440439
if (args->compression_level >= 0)
441440
strbuf_addf(&cmd, " -%d", args->compression_level);
442441

443-
argv[0] = cmd.buf;
444-
argv[1] = NULL;
445-
filter.argv = argv;
442+
strvec_push(&filter.args, cmd.buf);
446443
filter.use_shell = 1;
447444
filter.in = -1;
448445

449446
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);
451448
close(1);
452449
if (dup2(filter.in, 1) < 0)
453450
die_errno(_("unable to redirect descriptor"));
@@ -457,7 +454,7 @@ static int write_tar_filter_archive(const struct archiver *ar,
457454

458455
close(1);
459456
if (finish_command(&filter) != 0)
460-
die(_("'%s' filter reported error"), argv[0]);
457+
die(_("'%s' filter reported error"), cmd.buf);
461458

462459
strbuf_release(&cmd);
463460
return r;

builtin/receive-pack.c

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -812,16 +812,13 @@ static int run_and_feed_hook(const char *hook_name, feed_fn feed,
812812
{
813813
struct child_process proc = CHILD_PROCESS_INIT;
814814
struct async muxer;
815-
const char *argv[2];
816815
int code;
816+
const char *hook_path = find_hook(hook_name);
817817

818-
argv[0] = find_hook(hook_name);
819-
if (!argv[0])
818+
if (!hook_path)
820819
return 0;
821820

822-
argv[1] = NULL;
823-
824-
proc.argv = argv;
821+
strvec_push(&proc.args, hook_path);
825822
proc.in = -1;
826823
proc.stdout_to_stderr = 1;
827824
proc.trace2_hook_name = hook_name;
@@ -943,23 +940,21 @@ static int run_receive_hook(struct command *commands,
943940

944941
static int run_update_hook(struct command *cmd)
945942
{
946-
const char *argv[5];
947943
struct child_process proc = CHILD_PROCESS_INIT;
948944
int code;
945+
const char *hook_path = find_hook("update");
949946

950-
argv[0] = find_hook("update");
951-
if (!argv[0])
947+
if (!hook_path)
952948
return 0;
953949

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));
958954

959955
proc.no_stdin = 1;
960956
proc.stdout_to_stderr = 1;
961957
proc.err = use_sideband ? -1 : 0;
962-
proc.argv = argv;
963958
proc.trace2_hook_name = "update";
964959

965960
code = start_command(&proc);
@@ -1117,22 +1112,20 @@ static int run_proc_receive_hook(struct command *commands,
11171112
struct child_process proc = CHILD_PROCESS_INIT;
11181113
struct async muxer;
11191114
struct command *cmd;
1120-
const char *argv[2];
11211115
struct packet_reader reader;
11221116
struct strbuf cap = STRBUF_INIT;
11231117
struct strbuf errmsg = STRBUF_INIT;
11241118
int hook_use_push_options = 0;
11251119
int version = 0;
11261120
int code;
1121+
const char *hook_path = find_hook("proc-receive");
11271122

1128-
argv[0] = find_hook("proc-receive");
1129-
if (!argv[0]) {
1123+
if (!hook_path) {
11301124
rp_error("cannot find hook 'proc-receive'");
11311125
return -1;
11321126
}
1133-
argv[1] = NULL;
11341127

1135-
proc.argv = argv;
1128+
strvec_push(&proc.args, hook_path);
11361129
proc.in = -1;
11371130
proc.out = -1;
11381131
proc.trace2_hook_name = "proc-receive";

daemon.c

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -326,22 +326,18 @@ static int run_access_hook(struct daemon_service *service, const char *dir,
326326
{
327327
struct child_process child = CHILD_PROCESS_INIT;
328328
struct strbuf buf = STRBUF_INIT;
329-
const char *argv[8];
330-
const char **arg = argv;
331329
char *eol;
332330
int seen_errors = 0;
333331

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);
342339

343340
child.use_shell = 1;
344-
child.argv = argv;
345341
child.no_stdin = 1;
346342
child.no_stderr = 1;
347343
child.out = -1;

diff.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6921,19 +6921,15 @@ static char *run_textconv(struct repository *r,
69216921
size_t *outsize)
69226922
{
69236923
struct diff_tempfile *temp;
6924-
const char *argv[3];
6925-
const char **arg = argv;
69266924
struct child_process child = CHILD_PROCESS_INIT;
69276925
struct strbuf buf = STRBUF_INIT;
69286926
int err = 0;
69296927

69306928
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);
69346931

69356932
child.use_shell = 1;
6936-
child.argv = argv;
69376933
child.out = -1;
69386934
if (start_command(&child)) {
69396935
remove_tempfile();

prompt.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
static char *do_askpass(const char *cmd, const char *prompt)
99
{
1010
struct child_process pass = CHILD_PROCESS_INIT;
11-
const char *args[3];
1211
static struct strbuf buffer = STRBUF_INIT;
1312
int err = 0;
1413

15-
args[0] = cmd;
16-
args[1] = prompt;
17-
args[2] = NULL;
14+
strvec_push(&pass.args, cmd);
15+
strvec_push(&pass.args, prompt);
1816

19-
pass.argv = args;
2017
pass.out = -1;
2118

2219
if (start_command(&pass))

transport.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,16 +1204,15 @@ static int run_pre_push_hook(struct transport *transport,
12041204
struct ref *r;
12051205
struct child_process proc = CHILD_PROCESS_INIT;
12061206
struct strbuf buf;
1207-
const char *argv[4];
1207+
const char *hook_path = find_hook("pre-push");
12081208

1209-
if (!(argv[0] = find_hook("pre-push")))
1209+
if (!hook_path)
12101210
return 0;
12111211

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);
12151215

1216-
proc.argv = argv;
12171216
proc.in = -1;
12181217
proc.trace2_hook_name = "pre-push";
12191218

0 commit comments

Comments
 (0)