Skip to content

Commit be4ac3b

Browse files
committed
Merge branch 'rs/no-more-run-command-v'
Simplify the run-command API. * rs/no-more-run-command-v: replace and remove run_command_v_opt() replace and remove run_command_v_opt_cd_env_tr2() replace and remove run_command_v_opt_tr2() replace and remove run_command_v_opt_cd_env() use child_process members "args" and "env" directly use child_process member "args" instead of string array variable sequencer: simplify building argument list in do_exec() bisect--helper: factor out do_bisect_run() bisect: simplify building "checkout" argument list am: simplify building "show" argument list run-command: fix return value comment merge: remove always-the-same "verbose" arguments
2 parents 3e9303d + ddbb47f commit be4ac3b

27 files changed

+346
-383
lines changed

add-interactive.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -997,18 +997,17 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
997997
count = list_and_choose(s, files, opts);
998998
opts->flags = 0;
999999
if (count > 0) {
1000-
struct strvec args = STRVEC_INIT;
1000+
struct child_process cmd = CHILD_PROCESS_INIT;
10011001

1002-
strvec_pushl(&args, "git", "diff", "-p", "--cached",
1002+
strvec_pushl(&cmd.args, "git", "diff", "-p", "--cached",
10031003
oid_to_hex(!is_initial ? &oid :
10041004
s->r->hash_algo->empty_tree),
10051005
"--", NULL);
10061006
for (i = 0; i < files->items.nr; i++)
10071007
if (files->selected[i])
1008-
strvec_push(&args,
1008+
strvec_push(&cmd.args,
10091009
files->items.items[i].string);
1010-
res = run_command_v_opt(args.v, 0);
1011-
strvec_clear(&args);
1010+
res = run_command(&cmd);
10121011
}
10131012

10141013
putchar('\n');

bisect.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ static struct oid_array skipped_revs;
2222

2323
static struct object_id *current_bad_oid;
2424

25-
static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
26-
2725
static const char *term_bad;
2826
static const char *term_good;
2927

@@ -729,20 +727,22 @@ static int is_expected_rev(const struct object_id *oid)
729727
enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
730728
int no_checkout)
731729
{
732-
char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
733730
struct commit *commit;
734731
struct pretty_print_context pp = {0};
735732
struct strbuf commit_msg = STRBUF_INIT;
736733

737-
oid_to_hex_r(bisect_rev_hex, bisect_rev);
738734
update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
739735

740-
argv_checkout[2] = bisect_rev_hex;
741736
if (no_checkout) {
742737
update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
743738
UPDATE_REFS_DIE_ON_ERR);
744739
} else {
745-
if (run_command_v_opt(argv_checkout, RUN_GIT_CMD))
740+
struct child_process cmd = CHILD_PROCESS_INIT;
741+
742+
cmd.git_cmd = 1;
743+
strvec_pushl(&cmd.args, "checkout", "-q",
744+
oid_to_hex(bisect_rev), "--", NULL);
745+
if (run_command(&cmd))
746746
/*
747747
* Errors in `run_command()` itself, signaled by res < 0,
748748
* and errors in the child process, signaled by res > 0

builtin/add.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ static int refresh(int verbose, const struct pathspec *pathspec)
240240
int run_add_interactive(const char *revision, const char *patch_mode,
241241
const struct pathspec *pathspec)
242242
{
243-
int status, i;
244-
struct strvec argv = STRVEC_INIT;
243+
int i;
244+
struct child_process cmd = CHILD_PROCESS_INIT;
245245
int use_builtin_add_i =
246246
git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
247247

@@ -272,19 +272,18 @@ int run_add_interactive(const char *revision, const char *patch_mode,
272272
return !!run_add_p(the_repository, mode, revision, pathspec);
273273
}
274274

275-
strvec_push(&argv, "add--interactive");
275+
strvec_push(&cmd.args, "add--interactive");
276276
if (patch_mode)
277-
strvec_push(&argv, patch_mode);
277+
strvec_push(&cmd.args, patch_mode);
278278
if (revision)
279-
strvec_push(&argv, revision);
280-
strvec_push(&argv, "--");
279+
strvec_push(&cmd.args, revision);
280+
strvec_push(&cmd.args, "--");
281281
for (i = 0; i < pathspec->nr; i++)
282282
/* pass original pathspec, to be re-parsed */
283-
strvec_push(&argv, pathspec->items[i].original);
283+
strvec_push(&cmd.args, pathspec->items[i].original);
284284

285-
status = run_command_v_opt(argv.v, RUN_GIT_CMD);
286-
strvec_clear(&argv);
287-
return status;
285+
cmd.git_cmd = 1;
286+
return run_command(&cmd);
288287
}
289288

290289
int interactive_add(const char **argv, const char *prefix, int patch)

builtin/am.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,14 +2187,12 @@ static int show_patch(struct am_state *state, enum show_patch_type sub_mode)
21872187
int len;
21882188

21892189
if (!is_null_oid(&state->orig_commit)) {
2190-
const char *av[4] = { "show", NULL, "--", NULL };
2191-
char *new_oid_str;
2192-
int ret;
2190+
struct child_process cmd = CHILD_PROCESS_INIT;
21932191

2194-
av[1] = new_oid_str = xstrdup(oid_to_hex(&state->orig_commit));
2195-
ret = run_command_v_opt(av, RUN_GIT_CMD);
2196-
free(new_oid_str);
2197-
return ret;
2192+
strvec_pushl(&cmd.args, "show", oid_to_hex(&state->orig_commit),
2193+
"--", NULL);
2194+
cmd.git_cmd = 1;
2195+
return run_command(&cmd);
21982196
}
21992197

22002198
switch (sub_mode) {

builtin/bisect--helper.c

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -220,18 +220,17 @@ static int bisect_reset(const char *commit)
220220
}
221221

222222
if (!ref_exists("BISECT_HEAD")) {
223-
struct strvec argv = STRVEC_INIT;
223+
struct child_process cmd = CHILD_PROCESS_INIT;
224224

225-
strvec_pushl(&argv, "checkout", branch.buf, "--", NULL);
226-
if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
225+
cmd.git_cmd = 1;
226+
strvec_pushl(&cmd.args, "checkout", branch.buf, "--", NULL);
227+
if (run_command(&cmd)) {
227228
error(_("could not check out original"
228229
" HEAD '%s'. Try 'git bisect"
229230
" reset <commit>'."), branch.buf);
230231
strbuf_release(&branch);
231-
strvec_clear(&argv);
232232
return -1;
233233
}
234-
strvec_clear(&argv);
235234
}
236235

237236
strbuf_release(&branch);
@@ -765,10 +764,12 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, const char **a
765764
strbuf_read_file(&start_head, git_path_bisect_start(), 0);
766765
strbuf_trim(&start_head);
767766
if (!no_checkout) {
768-
const char *argv[] = { "checkout", start_head.buf,
769-
"--", NULL };
767+
struct child_process cmd = CHILD_PROCESS_INIT;
770768

771-
if (run_command_v_opt(argv, RUN_GIT_CMD)) {
769+
cmd.git_cmd = 1;
770+
strvec_pushl(&cmd.args, "checkout", start_head.buf,
771+
"--", NULL);
772+
if (run_command(&cmd)) {
772773
res = error(_("checking out '%s' failed."
773774
" Try 'git bisect start "
774775
"<valid-branch>'."),
@@ -1098,40 +1099,38 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
10981099

10991100
static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
11001101
{
1101-
struct strvec args = STRVEC_INIT;
1102-
int flags = RUN_COMMAND_NO_STDIN, res = 0;
1102+
struct child_process cmd = CHILD_PROCESS_INIT;
11031103
struct strbuf sb = STRBUF_INIT;
11041104

11051105
if (bisect_next_check(terms, NULL) != 0)
11061106
return BISECT_FAILED;
11071107

1108+
cmd.no_stdin = 1;
11081109
if (!argc) {
11091110
if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
11101111
getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk")) {
1111-
strvec_push(&args, "gitk");
1112+
strvec_push(&cmd.args, "gitk");
11121113
} else {
1113-
strvec_push(&args, "log");
1114-
flags |= RUN_GIT_CMD;
1114+
strvec_push(&cmd.args, "log");
1115+
cmd.git_cmd = 1;
11151116
}
11161117
} else {
11171118
if (argv[0][0] == '-') {
1118-
strvec_push(&args, "log");
1119-
flags |= RUN_GIT_CMD;
1119+
strvec_push(&cmd.args, "log");
1120+
cmd.git_cmd = 1;
11201121
} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
1121-
flags |= RUN_GIT_CMD;
1122+
cmd.git_cmd = 1;
11221123

1123-
strvec_pushv(&args, argv);
1124+
strvec_pushv(&cmd.args, argv);
11241125
}
11251126

1126-
strvec_pushl(&args, "--bisect", "--", NULL);
1127+
strvec_pushl(&cmd.args, "--bisect", "--", NULL);
11271128

11281129
strbuf_read_file(&sb, git_path_bisect_names(), 0);
1129-
sq_dequote_to_strvec(sb.buf, &args);
1130+
sq_dequote_to_strvec(sb.buf, &cmd.args);
11301131
strbuf_release(&sb);
11311132

1132-
res = run_command_v_opt(args.v, flags);
1133-
strvec_clear(&args);
1134-
return res;
1133+
return run_command(&cmd);
11351134
}
11361135

11371136
static int get_first_good(const char *refname UNUSED,
@@ -1142,8 +1141,17 @@ static int get_first_good(const char *refname UNUSED,
11421141
return 1;
11431142
}
11441143

1145-
static int verify_good(const struct bisect_terms *terms,
1146-
const char **quoted_argv)
1144+
static int do_bisect_run(const char *command)
1145+
{
1146+
struct child_process cmd = CHILD_PROCESS_INIT;
1147+
1148+
printf(_("running %s\n"), command);
1149+
cmd.use_shell = 1;
1150+
strvec_push(&cmd.args, command);
1151+
return run_command(&cmd);
1152+
}
1153+
1154+
static int verify_good(const struct bisect_terms *terms, const char *command)
11471155
{
11481156
int rc;
11491157
enum bisect_error res;
@@ -1163,8 +1171,7 @@ static int verify_good(const struct bisect_terms *terms,
11631171
if (res != BISECT_OK)
11641172
return -1;
11651173

1166-
printf(_("running %s\n"), quoted_argv[0]);
1167-
rc = run_command_v_opt(quoted_argv, RUN_USING_SHELL);
1174+
rc = do_bisect_run(command);
11681175

11691176
res = bisect_checkout(&current_rev, no_checkout);
11701177
if (res != BISECT_OK)
@@ -1177,7 +1184,6 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
11771184
{
11781185
int res = BISECT_OK;
11791186
struct strbuf command = STRBUF_INIT;
1180-
struct strvec run_args = STRVEC_INIT;
11811187
const char *new_state;
11821188
int temporary_stdout_fd, saved_stdout;
11831189
int is_first_run = 1;
@@ -1192,11 +1198,8 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
11921198
return BISECT_FAILED;
11931199
}
11941200

1195-
strvec_push(&run_args, command.buf);
1196-
11971201
while (1) {
1198-
printf(_("running %s\n"), command.buf);
1199-
res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
1202+
res = do_bisect_run(command.buf);
12001203

12011204
/*
12021205
* Exit code 126 and 127 can either come from the shell
@@ -1206,7 +1209,7 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
12061209
* missing or non-executable script.
12071210
*/
12081211
if (is_first_run && (res == 126 || res == 127)) {
1209-
int rc = verify_good(terms, run_args.v);
1212+
int rc = verify_good(terms, command.buf);
12101213
is_first_run = 0;
12111214
if (rc < 0) {
12121215
error(_("unable to verify '%s' on good"
@@ -1273,7 +1276,6 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
12731276
}
12741277

12751278
strbuf_release(&command);
1276-
strvec_clear(&run_args);
12771279
return res;
12781280
}
12791281

builtin/clone.c

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -653,22 +653,22 @@ static void update_head(const struct ref *our, const struct ref *remote,
653653

654654
static int git_sparse_checkout_init(const char *repo)
655655
{
656-
struct strvec argv = STRVEC_INIT;
656+
struct child_process cmd = CHILD_PROCESS_INIT;
657657
int result = 0;
658-
strvec_pushl(&argv, "-C", repo, "sparse-checkout", "set", NULL);
658+
strvec_pushl(&cmd.args, "-C", repo, "sparse-checkout", "set", NULL);
659659

660660
/*
661661
* We must apply the setting in the current process
662662
* for the later checkout to use the sparse-checkout file.
663663
*/
664664
core_apply_sparse_checkout = 1;
665665

666-
if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
666+
cmd.git_cmd = 1;
667+
if (run_command(&cmd)) {
667668
error(_("failed to initialize sparse-checkout"));
668669
result = 1;
669670
}
670671

671-
strvec_clear(&argv);
672672
return result;
673673
}
674674

@@ -733,37 +733,38 @@ static int checkout(int submodule_progress, int filter_submodules)
733733
oid_to_hex(&oid), "1", NULL);
734734

735735
if (!err && (option_recurse_submodules.nr > 0)) {
736-
struct strvec args = STRVEC_INIT;
737-
strvec_pushl(&args, "submodule", "update", "--require-init", "--recursive", NULL);
736+
struct child_process cmd = CHILD_PROCESS_INIT;
737+
strvec_pushl(&cmd.args, "submodule", "update", "--require-init",
738+
"--recursive", NULL);
738739

739740
if (option_shallow_submodules == 1)
740-
strvec_push(&args, "--depth=1");
741+
strvec_push(&cmd.args, "--depth=1");
741742

742743
if (max_jobs != -1)
743-
strvec_pushf(&args, "--jobs=%d", max_jobs);
744+
strvec_pushf(&cmd.args, "--jobs=%d", max_jobs);
744745

745746
if (submodule_progress)
746-
strvec_push(&args, "--progress");
747+
strvec_push(&cmd.args, "--progress");
747748

748749
if (option_verbosity < 0)
749-
strvec_push(&args, "--quiet");
750+
strvec_push(&cmd.args, "--quiet");
750751

751752
if (option_remote_submodules) {
752-
strvec_push(&args, "--remote");
753-
strvec_push(&args, "--no-fetch");
753+
strvec_push(&cmd.args, "--remote");
754+
strvec_push(&cmd.args, "--no-fetch");
754755
}
755756

756757
if (filter_submodules && filter_options.choice)
757-
strvec_pushf(&args, "--filter=%s",
758+
strvec_pushf(&cmd.args, "--filter=%s",
758759
expand_list_objects_filter_spec(&filter_options));
759760

760761
if (option_single_branch >= 0)
761-
strvec_push(&args, option_single_branch ?
762+
strvec_push(&cmd.args, option_single_branch ?
762763
"--single-branch" :
763764
"--no-single-branch");
764765

765-
err = run_command_v_opt(args.v, RUN_GIT_CMD);
766-
strvec_clear(&args);
766+
cmd.git_cmd = 1;
767+
err = run_command(&cmd);
767768
}
768769

769770
return err;
@@ -864,11 +865,15 @@ static void write_refspec_config(const char *src_ref_prefix,
864865

865866
static void dissociate_from_references(void)
866867
{
867-
static const char* argv[] = { "repack", "-a", "-d", NULL };
868868
char *alternates = git_pathdup("objects/info/alternates");
869869

870870
if (!access(alternates, F_OK)) {
871-
if (run_command_v_opt(argv, RUN_GIT_CMD|RUN_COMMAND_NO_STDIN))
871+
struct child_process cmd = CHILD_PROCESS_INIT;
872+
873+
cmd.git_cmd = 1;
874+
cmd.no_stdin = 1;
875+
strvec_pushl(&cmd.args, "repack", "-a", "-d", NULL);
876+
if (run_command(&cmd))
872877
die(_("cannot repack to clean up"));
873878
if (unlink(alternates) && errno != ENOENT)
874879
die_errno(_("cannot unlink temporary alternates file"));

0 commit comments

Comments
 (0)