Skip to content

Commit 4120294

Browse files
rscharfettaylorr
authored andcommitted
use child_process member "args" instead of string array variable
Use run_command() with a struct child_process variable and populate its "args" member directly instead of building a string array and passing it to run_command_v_opt(). This avoids the use of magic index numbers and makes simplifies the possible addition of more arguments in the future. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 242aa33 commit 4120294

File tree

6 files changed

+44
-44
lines changed

6 files changed

+44
-44
lines changed

builtin/difftool.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
360360
struct pair_entry *entry;
361361
struct index_state wtindex;
362362
struct checkout lstate, rstate;
363-
int flags = RUN_GIT_CMD, err = 0;
364-
const char *helper_argv[] = { "difftool--helper", NULL, NULL, NULL };
363+
int err = 0;
364+
struct child_process cmd = CHILD_PROCESS_INIT;
365365
struct hashmap wt_modified, tmp_modified;
366366
int indices_loaded = 0;
367367

@@ -563,16 +563,17 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
563563
}
564564

565565
strbuf_setlen(&ldir, ldir_len);
566-
helper_argv[1] = ldir.buf;
567566
strbuf_setlen(&rdir, rdir_len);
568-
helper_argv[2] = rdir.buf;
569567

570568
if (extcmd) {
571-
helper_argv[0] = extcmd;
572-
flags = 0;
573-
} else
569+
strvec_push(&cmd.args, extcmd);
570+
} else {
571+
strvec_push(&cmd.args, "difftool--helper");
572+
cmd.git_cmd = 1;
574573
setenv("GIT_DIFFTOOL_DIRDIFF", "true", 1);
575-
ret = run_command_v_opt(helper_argv, flags);
574+
}
575+
strvec_pushl(&cmd.args, ldir.buf, rdir.buf, NULL);
576+
ret = run_command(&cmd);
576577

577578
/* TODO: audit for interaction with sparse-index. */
578579
ensure_full_index(&wtindex);

builtin/merge.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -347,33 +347,25 @@ static int save_state(struct object_id *stash)
347347

348348
static void read_empty(const struct object_id *oid)
349349
{
350-
int i = 0;
351-
const char *args[7];
352-
353-
args[i++] = "read-tree";
354-
args[i++] = "-m";
355-
args[i++] = "-u";
356-
args[i++] = empty_tree_oid_hex();
357-
args[i++] = oid_to_hex(oid);
358-
args[i] = NULL;
350+
struct child_process cmd = CHILD_PROCESS_INIT;
351+
352+
strvec_pushl(&cmd.args, "read-tree", "-m", "-u", empty_tree_oid_hex(),
353+
oid_to_hex(oid), NULL);
354+
cmd.git_cmd = 1;
359355

360-
if (run_command_v_opt(args, RUN_GIT_CMD))
356+
if (run_command(&cmd))
361357
die(_("read-tree failed"));
362358
}
363359

364360
static void reset_hard(const struct object_id *oid)
365361
{
366-
int i = 0;
367-
const char *args[6];
368-
369-
args[i++] = "read-tree";
370-
args[i++] = "-v";
371-
args[i++] = "--reset";
372-
args[i++] = "-u";
373-
args[i++] = oid_to_hex(oid);
374-
args[i] = NULL;
362+
struct child_process cmd = CHILD_PROCESS_INIT;
363+
364+
strvec_pushl(&cmd.args, "read-tree", "-v", "--reset", "-u",
365+
oid_to_hex(oid), NULL);
366+
cmd.git_cmd = 1;
375367

376-
if (run_command_v_opt(args, RUN_GIT_CMD))
368+
if (run_command(&cmd))
377369
die(_("read-tree failed"));
378370
}
379371

builtin/remote.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,15 @@ static int verbose;
9292

9393
static int fetch_remote(const char *name)
9494
{
95-
const char *argv[] = { "fetch", name, NULL, NULL };
96-
if (verbose) {
97-
argv[1] = "-v";
98-
argv[2] = name;
99-
}
95+
struct child_process cmd = CHILD_PROCESS_INIT;
96+
97+
strvec_push(&cmd.args, "fetch");
98+
if (verbose)
99+
strvec_push(&cmd.args, "-v");
100+
strvec_push(&cmd.args, name);
101+
cmd.git_cmd = 1;
100102
printf_ln(_("Updating %s"), name);
101-
if (run_command_v_opt(argv, RUN_GIT_CMD))
103+
if (run_command(&cmd))
102104
return error(_("Could not fetch %s"), name);
103105
return 0;
104106
}

compat/mingw.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,16 +196,19 @@ static int read_yes_no_answer(void)
196196
static int ask_yes_no_if_possible(const char *format, ...)
197197
{
198198
char question[4096];
199-
const char *retry_hook[] = { NULL, NULL, NULL };
199+
const char *retry_hook;
200200
va_list args;
201201

202202
va_start(args, format);
203203
vsnprintf(question, sizeof(question), format, args);
204204
va_end(args);
205205

206-
if ((retry_hook[0] = mingw_getenv("GIT_ASK_YESNO"))) {
207-
retry_hook[1] = question;
208-
return !run_command_v_opt(retry_hook, 0);
206+
retry_hook = mingw_getenv("GIT_ASK_YESNO");
207+
if (retry_hook) {
208+
struct child_process cmd = CHILD_PROCESS_INIT;
209+
210+
strvec_pushl(&cmd.args, retry_hook, question, NULL);
211+
return !run_command(&cmd);
209212
}
210213

211214
if (!isatty(_fileno(stdin)) || !isatty(_fileno(stderr)))

ll-merge.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
193193
struct strbuf cmd = STRBUF_INIT;
194194
struct strbuf_expand_dict_entry dict[6];
195195
struct strbuf path_sq = STRBUF_INIT;
196-
const char *args[] = { NULL, NULL };
196+
struct child_process child = CHILD_PROCESS_INIT;
197197
int status, fd, i;
198198
struct stat st;
199199
enum ll_merge_result ret;
@@ -219,8 +219,9 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
219219

220220
strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
221221

222-
args[0] = cmd.buf;
223-
status = run_command_v_opt(args, RUN_USING_SHELL);
222+
child.use_shell = 1;
223+
strvec_push(&child.args, cmd.buf);
224+
status = run_command(&child);
224225
fd = open(temp[1], O_RDONLY);
225226
if (fd < 0)
226227
goto bad;

t/helper/test-fake-ssh.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ int cmd_main(int argc, const char **argv)
88
struct strbuf buf = STRBUF_INIT;
99
FILE *f;
1010
int i;
11-
const char *child_argv[] = { NULL, NULL };
11+
struct child_process cmd = CHILD_PROCESS_INIT;
1212

1313
/* First, print all parameters into $TRASH_DIRECTORY/ssh-output */
1414
if (!trash_directory)
@@ -25,6 +25,7 @@ int cmd_main(int argc, const char **argv)
2525
/* Now, evaluate the *last* parameter */
2626
if (argc < 2)
2727
return 0;
28-
child_argv[0] = argv[argc - 1];
29-
return run_command_v_opt(child_argv, RUN_USING_SHELL);
28+
cmd.use_shell = 1;
29+
strvec_push(&cmd.args, argv[argc - 1]);
30+
return run_command(&cmd);
3031
}

0 commit comments

Comments
 (0)