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 <l.s.r@web.de> Signed-off-by: Taylor Blau <me@ttaylorr.com>
René Scharfe committed
Oct 30, 2022 at 12:50 UTC
4120294cbf8e434c1de408434842d570eba0e25d
6 files changed
+44
-44
builtin/difftool.c
+9
-8
@@ -360,8 +360,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
360
struct pair_entry *entry;
361
struct index_state wtindex;
362
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;
365
struct hashmap wt_modified, tmp_modified;
366
int indices_loaded = 0;
367
@@ -563,16 +563,17 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
563
}
564
565
strbuf_setlen(&ldir, ldir_len);
566
- helper_argv[1] = ldir.buf;
566
strbuf_setlen(&rdir, rdir_len);
568
- helper_argv[2] = rdir.buf;
567
568
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;
573
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);
577
578
/* TODO: audit for interaction with sparse-index. */
579
ensure_full_index(&wtindex);
builtin/merge.c
+12
-20
@@ -347,33 +347,25 @@ out:
347
348
static void read_empty(const struct object_id *oid)
349
{
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;
355
360
- if (run_command_v_opt(args, RUN_GIT_CMD))
356
+ if (run_command(&cmd))
357
die(_("read-tree failed"));
358
}
359
360
static void reset_hard(const struct object_id *oid)
361
{
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;
367
376
- if (run_command_v_opt(args, RUN_GIT_CMD))
368
+ if (run_command(&cmd))
369
die(_("read-tree failed"));
370
}
371
builtin/remote.c
+8
-6
@@ -92,13 +92,15 @@ static int verbose;
92
93
static int fetch_remote(const char *name)
94
{
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;
102
printf_ln(_("Updating %s"), name);
101
- if (run_command_v_opt(argv, RUN_GIT_CMD))
103
+ if (run_command(&cmd))
104
return error(_("Could not fetch %s"), name);
105
return 0;
106
}
compat/mingw.c
+7
-4
@@ -196,16 +196,19 @@ static int read_yes_no_answer(void)
196
static int ask_yes_no_if_possible(const char *format, ...)
197
{
198
char question[4096];
199
- const char *retry_hook[] = { NULL, NULL, NULL };
199
+ const char *retry_hook;
200
va_list args;
201
202
va_start(args, format);
203
vsnprintf(question, sizeof(question), format, args);
204
va_end(args);
205
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);
212
}
213
214
if (!isatty(_fileno(stdin)) || !isatty(_fileno(stderr)))
ll-merge.c
+4
-3
@@ -193,7 +193,7 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
193
struct strbuf cmd = STRBUF_INIT;
194
struct strbuf_expand_dict_entry dict[6];
195
struct strbuf path_sq = STRBUF_INIT;
196
- const char *args[] = { NULL, NULL };
196
+ struct child_process child = CHILD_PROCESS_INIT;
197
int status, fd, i;
198
struct stat st;
199
enum ll_merge_result ret;
@@ -219,8 +219,9 @@ static enum ll_merge_result ll_ext_merge(const struct ll_merge_driver *fn,
219
220
strbuf_expand(&cmd, fn->cmdline, strbuf_expand_dict_cb, &dict);
221
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);
225
fd = open(temp[1], O_RDONLY);
226
if (fd < 0)
227
goto bad;
t/helper/test-fake-ssh.c
+4
-3
@@ -8,7 +8,7 @@ int cmd_main(int argc, const char **argv)
8
struct strbuf buf = STRBUF_INIT;
9
FILE *f;
10
int i;
11
- const char *child_argv[] = { NULL, NULL };
11
+ struct child_process cmd = CHILD_PROCESS_INIT;
12
13
/* First, print all parameters into $TRASH_DIRECTORY/ssh-output */
14
if (!trash_directory)
@@ -25,6 +25,7 @@ int cmd_main(int argc, const char **argv)
25
/* Now, evaluate the *last* parameter */
26
if (argc < 2)
27
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);
31
}