As we'll see in subsequent commits most of the run_command_v_opt_*() API users are a bad fit for what we'd like to do with that API. The ideal use-case for it is something where we already have an "argv", and don't want the one-shot semantics of run_command(). In the case of run_command_v_opt_cd_env() there was only user of it, and that user was just accumulating complexity by not using run_command() directly. By not having to maintain its own "argv" and "env" it doesn't have to strvec_clear() them both, which in the case of run_command() will be done by child_process_clear() before it returns. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@xxxxxxxxx> --- diff.c | 26 ++++++++++++-------------- run-command.c | 7 +------ run-command.h | 3 +-- tmp-objdir.h | 6 ++++-- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/diff.c b/diff.c index 648f6717a55..392530016fa 100644 --- a/diff.c +++ b/diff.c @@ -4278,35 +4278,33 @@ static void run_external_diff(const char *pgm, const char *xfrm_msg, struct diff_options *o) { - struct strvec argv = STRVEC_INIT; - struct strvec env = STRVEC_INIT; struct diff_queue_struct *q = &diff_queued_diff; + struct child_process cmd = CHILD_PROCESS_INIT; - strvec_push(&argv, pgm); - strvec_push(&argv, name); + strvec_push(&cmd.args, pgm); + strvec_push(&cmd.args, name); if (one && two) { - add_external_diff_name(o->repo, &argv, name, one); + add_external_diff_name(o->repo, &cmd.args, name, one); if (!other) - add_external_diff_name(o->repo, &argv, name, two); + add_external_diff_name(o->repo, &cmd.args, name, two); else { - add_external_diff_name(o->repo, &argv, other, two); - strvec_push(&argv, other); - strvec_push(&argv, xfrm_msg); + add_external_diff_name(o->repo, &cmd.args, other, two); + strvec_push(&cmd.args, other); + strvec_push(&cmd.args, xfrm_msg); } } - strvec_pushf(&env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); - strvec_pushf(&env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); + strvec_pushf(&cmd.env, "GIT_DIFF_PATH_COUNTER=%d", ++o->diff_path_counter); + strvec_pushf(&cmd.env, "GIT_DIFF_PATH_TOTAL=%d", q->nr); diff_free_filespec_data(one); diff_free_filespec_data(two); - if (run_command_v_opt_cd_env(argv.v, RUN_USING_SHELL, NULL, env.v)) + cmd.use_shell = 1; + if (run_command(&cmd)) die(_("external diff died, stopping at %s"), name); remove_tempfile(); - strvec_clear(&argv); - strvec_clear(&env); } static int similarity_index(struct diff_filepair *p) diff --git a/run-command.c b/run-command.c index f9a0b13f62f..25a978fb027 100644 --- a/run-command.c +++ b/run-command.c @@ -1038,7 +1038,7 @@ static int run_command_v_opt_1(struct child_process *cmd, int opt) int run_command_v_opt(const char **argv, int opt) { - return run_command_v_opt_cd_env(argv, opt, NULL, NULL); + return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, NULL); } int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class) @@ -1046,11 +1046,6 @@ int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class) return run_command_v_opt_cd_env_tr2(argv, opt, NULL, NULL, tr2_class); } -int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env) -{ - return run_command_v_opt_cd_env_tr2(argv, opt, dir, env, NULL); -} - int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, const char *const *env, const char *tr2_class) { diff --git a/run-command.h b/run-command.h index 0af01bd9b67..2574d46cb70 100644 --- a/run-command.h +++ b/run-command.h @@ -152,7 +152,7 @@ struct child_process { /** * The functions: child_process_init, start_command, finish_command, * run_command, run_command_l_opt, run_command_v_opt, - * run_command_v_opt_cd_env, child_process_clear do the following: + * child_process_clear do the following: * * - If a system call failed, errno is set and -1 is returned. A diagnostic * is printed. @@ -263,7 +263,6 @@ int run_command_v_opt_tr2(const char **argv, int opt, const char *tr2_class); * env (the environment) is to be formatted like environ: "VAR=VALUE". * To unset an environment variable use just "VAR". */ -int run_command_v_opt_cd_env(const char **argv, int opt, const char *dir, const char *const *env); int run_command_v_opt_cd_env_tr2(const char **argv, int opt, const char *dir, const char *const *env, const char *tr2_class); diff --git a/tmp-objdir.h b/tmp-objdir.h index 76efc7edee5..c96aa77396d 100644 --- a/tmp-objdir.h +++ b/tmp-objdir.h @@ -10,9 +10,11 @@ * * Example: * + * struct child_process cmd = CHILD_PROCESS_INIT; + * ... * struct tmp_objdir *t = tmp_objdir_create("incoming"); - * if (!run_command_v_opt_cd_env(cmd, 0, NULL, tmp_objdir_env(t)) && - * !tmp_objdir_migrate(t)) + * strvec_pushl(&cmd.env, tmp_objdir_env(t)); + * if (!run_command(&cmd) && !tmp_objdir_migrate(t)) * printf("success!\n"); * else * die("failed...tmp_objdir will clean up for us"); -- 2.38.0.1091.gf9d18265e59