Skip to content

Commit ddbb47f

Browse files
rscharfettaylorr
authored andcommitted
replace and remove run_command_v_opt()
Replace the remaining calls of run_command_v_opt() with run_command() calls and explict struct child_process variables. This is more verbose, but not by much overall. The code becomes more flexible, e.g. it's easy to extend to conditionally add a new argument. Then remove the now unused function and its own flag names, simplifying the run-command API. Suggested-by: Jeff King <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent ef249b3 commit ddbb47f

File tree

13 files changed

+92
-75
lines changed

13 files changed

+92
-75
lines changed

bisect.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,11 +737,12 @@ enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
737737
update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
738738
UPDATE_REFS_DIE_ON_ERR);
739739
} else {
740-
const char *argv_checkout[] = {
741-
"checkout", "-q", oid_to_hex(bisect_rev), "--", NULL
742-
};
740+
struct child_process cmd = CHILD_PROCESS_INIT;
743741

744-
if (run_command_v_opt(argv_checkout, RUN_GIT_CMD))
742+
cmd.git_cmd = 1;
743+
strvec_pushl(&cmd.args, "checkout", "-q",
744+
oid_to_hex(bisect_rev), "--", NULL);
745+
if (run_command(&cmd))
745746
/*
746747
* Errors in `run_command()` itself, signaled by res < 0,
747748
* and errors in the child process, signaled by res > 0

builtin/am.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,11 +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[] = {
2191-
"show", oid_to_hex(&state->orig_commit), "--", NULL
2192-
};
2190+
struct child_process cmd = CHILD_PROCESS_INIT;
21932191

2194-
return run_command_v_opt(av, RUN_GIT_CMD);
2192+
strvec_pushl(&cmd.args, "show", oid_to_hex(&state->orig_commit),
2193+
"--", NULL);
2194+
cmd.git_cmd = 1;
2195+
return run_command(&cmd);
21952196
}
21962197

21972198
switch (sub_mode) {

builtin/bisect--helper.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -764,10 +764,12 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, const char **a
764764
strbuf_read_file(&start_head, git_path_bisect_start(), 0);
765765
strbuf_trim(&start_head);
766766
if (!no_checkout) {
767-
const char *argv[] = { "checkout", start_head.buf,
768-
"--", NULL };
767+
struct child_process cmd = CHILD_PROCESS_INIT;
769768

770-
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)) {
771773
res = error(_("checking out '%s' failed."
772774
" Try 'git bisect start "
773775
"<valid-branch>'."),
@@ -1141,9 +1143,12 @@ static int get_first_good(const char *refname UNUSED,
11411143

11421144
static int do_bisect_run(const char *command)
11431145
{
1144-
const char *argv[] = { command, NULL };
1146+
struct child_process cmd = CHILD_PROCESS_INIT;
1147+
11451148
printf(_("running %s\n"), command);
1146-
return run_command_v_opt(argv, RUN_USING_SHELL);
1149+
cmd.use_shell = 1;
1150+
strvec_push(&cmd.args, command);
1151+
return run_command(&cmd);
11471152
}
11481153

11491154
static int verify_good(const struct bisect_terms *terms, const char *command)

builtin/clone.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,15 @@ static void write_refspec_config(const char *src_ref_prefix,
865865

866866
static void dissociate_from_references(void)
867867
{
868-
static const char* argv[] = { "repack", "-a", "-d", NULL };
869868
char *alternates = git_pathdup("objects/info/alternates");
870869

871870
if (!access(alternates, F_OK)) {
872-
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))
873877
die(_("cannot repack to clean up"));
874878
if (unlink(alternates) && errno != ENOENT)
875879
die_errno(_("cannot unlink temporary alternates file"));

builtin/difftool.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ static int difftool_config(const char *var, const char *value, void *cb)
4444

4545
static int print_tool_help(void)
4646
{
47-
const char *argv[] = { "mergetool", "--tool-help=diff", NULL };
48-
return run_command_v_opt(argv, RUN_GIT_CMD);
47+
struct child_process cmd = CHILD_PROCESS_INIT;
48+
49+
cmd.git_cmd = 1;
50+
strvec_pushl(&cmd.args, "mergetool", "--tool-help=diff", NULL);
51+
return run_command(&cmd);
4952
}
5053

5154
static int parse_index_info(char *p, int *mode1, int *mode2,

builtin/fetch.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1965,14 +1965,17 @@ static int fetch_multiple(struct string_list *list, int max_children)
19651965
} else
19661966
for (i = 0; i < list->nr; i++) {
19671967
const char *name = list->items[i].string;
1968-
strvec_push(&argv, name);
1968+
struct child_process cmd = CHILD_PROCESS_INIT;
1969+
1970+
strvec_pushv(&cmd.args, argv.v);
1971+
strvec_push(&cmd.args, name);
19691972
if (verbosity >= 0)
19701973
printf(_("Fetching %s\n"), name);
1971-
if (run_command_v_opt(argv.v, RUN_GIT_CMD)) {
1974+
cmd.git_cmd = 1;
1975+
if (run_command(&cmd)) {
19721976
error(_("could not fetch %s"), name);
19731977
result = 1;
19741978
}
1975-
strvec_pop(&argv);
19761979
}
19771980

19781981
strvec_clear(&argv);

builtin/gc.c

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ static void gc_config(void)
167167
struct maintenance_run_opts;
168168
static int maintenance_task_pack_refs(MAYBE_UNUSED struct maintenance_run_opts *opts)
169169
{
170-
const char *argv[] = { "pack-refs", "--all", "--prune", NULL };
170+
struct child_process cmd = CHILD_PROCESS_INIT;
171171

172-
return run_command_v_opt(argv, RUN_GIT_CMD);
172+
cmd.git_cmd = 1;
173+
strvec_pushl(&cmd.args, "pack-refs", "--all", "--prune", NULL);
174+
return run_command(&cmd);
173175
}
174176

175177
static int too_many_loose_objects(void)
@@ -535,8 +537,14 @@ static void gc_before_repack(void)
535537
if (pack_refs && maintenance_task_pack_refs(NULL))
536538
die(FAILED_RUN, "pack-refs");
537539

538-
if (prune_reflogs && run_command_v_opt(reflog.v, RUN_GIT_CMD))
539-
die(FAILED_RUN, reflog.v[0]);
540+
if (prune_reflogs) {
541+
struct child_process cmd = CHILD_PROCESS_INIT;
542+
543+
cmd.git_cmd = 1;
544+
strvec_pushv(&cmd.args, reflog.v);
545+
if (run_command(&cmd))
546+
die(FAILED_RUN, reflog.v[0]);
547+
}
540548
}
541549

542550
int cmd_gc(int argc, const char **argv, const char *prefix)
@@ -550,6 +558,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
550558
int daemonized = 0;
551559
int keep_largest_pack = -1;
552560
timestamp_t dummy;
561+
struct child_process rerere_cmd = CHILD_PROCESS_INIT;
553562

554563
struct option builtin_gc_options[] = {
555564
OPT__QUIET(&quiet, N_("suppress progress reporting")),
@@ -671,30 +680,44 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
671680
gc_before_repack();
672681

673682
if (!repository_format_precious_objects) {
674-
if (run_command_v_opt(repack.v,
675-
RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE))
683+
struct child_process repack_cmd = CHILD_PROCESS_INIT;
684+
685+
repack_cmd.git_cmd = 1;
686+
repack_cmd.close_object_store = 1;
687+
strvec_pushv(&repack_cmd.args, repack.v);
688+
if (run_command(&repack_cmd))
676689
die(FAILED_RUN, repack.v[0]);
677690

678691
if (prune_expire) {
692+
struct child_process prune_cmd = CHILD_PROCESS_INIT;
693+
679694
/* run `git prune` even if using cruft packs */
680695
strvec_push(&prune, prune_expire);
681696
if (quiet)
682697
strvec_push(&prune, "--no-progress");
683698
if (has_promisor_remote())
684699
strvec_push(&prune,
685700
"--exclude-promisor-objects");
686-
if (run_command_v_opt(prune.v, RUN_GIT_CMD))
701+
prune_cmd.git_cmd = 1;
702+
strvec_pushv(&prune_cmd.args, prune.v);
703+
if (run_command(&prune_cmd))
687704
die(FAILED_RUN, prune.v[0]);
688705
}
689706
}
690707

691708
if (prune_worktrees_expire) {
709+
struct child_process prune_worktrees_cmd = CHILD_PROCESS_INIT;
710+
692711
strvec_push(&prune_worktrees, prune_worktrees_expire);
693-
if (run_command_v_opt(prune_worktrees.v, RUN_GIT_CMD))
712+
prune_worktrees_cmd.git_cmd = 1;
713+
strvec_pushv(&prune_worktrees_cmd.args, prune_worktrees.v);
714+
if (run_command(&prune_worktrees_cmd))
694715
die(FAILED_RUN, prune_worktrees.v[0]);
695716
}
696717

697-
if (run_command_v_opt(rerere.v, RUN_GIT_CMD))
718+
rerere_cmd.git_cmd = 1;
719+
strvec_pushv(&rerere_cmd.args, rerere.v);
720+
if (run_command(&rerere_cmd))
698721
die(FAILED_RUN, rerere.v[0]);
699722

700723
report_garbage = report_pack_garbage;

builtin/merge-index.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ static int merge_entry(int pos, const char *path)
1212
const char *arguments[] = { pgm, "", "", "", path, "", "", "", NULL };
1313
char hexbuf[4][GIT_MAX_HEXSZ + 1];
1414
char ownbuf[4][60];
15+
struct child_process cmd = CHILD_PROCESS_INIT;
1516

1617
if (pos >= active_nr)
1718
die("git merge-index: %s not in the cache", path);
@@ -31,7 +32,8 @@ static int merge_entry(int pos, const char *path)
3132
if (!found)
3233
die("git merge-index: %s not in the cache", path);
3334

34-
if (run_command_v_opt(arguments, 0)) {
35+
strvec_pushv(&cmd.args, arguments);
36+
if (run_command(&cmd)) {
3537
if (one_shot)
3638
err++;
3739
else {

run-command.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,21 +1004,6 @@ int run_command(struct child_process *cmd)
10041004
return finish_command(cmd);
10051005
}
10061006

1007-
int run_command_v_opt(const char **argv, int opt)
1008-
{
1009-
struct child_process cmd = CHILD_PROCESS_INIT;
1010-
strvec_pushv(&cmd.args, argv);
1011-
cmd.no_stdin = opt & RUN_COMMAND_NO_STDIN ? 1 : 0;
1012-
cmd.git_cmd = opt & RUN_GIT_CMD ? 1 : 0;
1013-
cmd.stdout_to_stderr = opt & RUN_COMMAND_STDOUT_TO_STDERR ? 1 : 0;
1014-
cmd.silent_exec_failure = opt & RUN_SILENT_EXEC_FAILURE ? 1 : 0;
1015-
cmd.use_shell = opt & RUN_USING_SHELL ? 1 : 0;
1016-
cmd.clean_on_exit = opt & RUN_CLEAN_ON_EXIT ? 1 : 0;
1017-
cmd.wait_after_clean = opt & RUN_WAIT_AFTER_CLEAN ? 1 : 0;
1018-
cmd.close_object_store = opt & RUN_CLOSE_OBJECT_STORE ? 1 : 0;
1019-
return run_command(&cmd);
1020-
}
1021-
10221007
#ifndef NO_PTHREADS
10231008
static pthread_t main_thread;
10241009
static int main_thread_set;

run-command.h

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ struct child_process {
150150
}
151151

152152
/**
153-
* The functions: start_command, finish_command, run_command,
154-
* run_command_v_opt do the following:
153+
* The functions: start_command, finish_command, run_command do the following:
155154
*
156155
* - If a system call failed, errno is set and -1 is returned. A diagnostic
157156
* is printed.
@@ -223,26 +222,6 @@ int run_command(struct child_process *);
223222
*/
224223
int run_auto_maintenance(int quiet);
225224

226-
#define RUN_COMMAND_NO_STDIN (1<<0)
227-
#define RUN_GIT_CMD (1<<1)
228-
#define RUN_COMMAND_STDOUT_TO_STDERR (1<<2)
229-
#define RUN_SILENT_EXEC_FAILURE (1<<3)
230-
#define RUN_USING_SHELL (1<<4)
231-
#define RUN_CLEAN_ON_EXIT (1<<5)
232-
#define RUN_WAIT_AFTER_CLEAN (1<<6)
233-
#define RUN_CLOSE_OBJECT_STORE (1<<7)
234-
235-
/**
236-
* Convenience function that encapsulates a sequence of
237-
* start_command() followed by finish_command(). The argument argv
238-
* specifies the program and its arguments. The argument opt is zero
239-
* or more of the flags `RUN_COMMAND_NO_STDIN`, `RUN_GIT_CMD`,
240-
* `RUN_COMMAND_STDOUT_TO_STDERR`, or `RUN_SILENT_EXEC_FAILURE`
241-
* that correspond to the members .no_stdin, .git_cmd,
242-
* .stdout_to_stderr, .silent_exec_failure of `struct child_process`.
243-
*/
244-
int run_command_v_opt(const char **argv, int opt);
245-
246225
/**
247226
* Execute the given command, sending "in" to its stdin, and capturing its
248227
* stdout and stderr in the "out" and "err" strbufs. Any of the three may

0 commit comments

Comments
 (0)