@@ -1007,8 +1007,7 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps,
if (files->selected[i])
strvec_push(&args,
files->items.items[i].string);
- res = run_command_v_opt(args.v, 0);
- strvec_clear(&args);
+ res = run_command_sv_opt(&args, 0);
}
putchar('\n');
@@ -240,7 +240,7 @@ static int refresh(int verbose, const struct pathspec *pathspec)
int run_add_interactive(const char *revision, const char *patch_mode,
const struct pathspec *pathspec)
{
- int status, i;
+ int i;
struct strvec argv = STRVEC_INIT;
int use_builtin_add_i =
git_env_bool("GIT_TEST_ADD_I_USE_BUILTIN", -1);
@@ -282,9 +282,7 @@ int run_add_interactive(const char *revision, const char *patch_mode,
/* pass original pathspec, to be re-parsed */
strvec_push(&argv, pathspec->items[i].original);
- status = run_command_v_opt(argv.v, RUN_GIT_CMD);
- strvec_clear(&argv);
- return status;
+ return run_command_sv_opt(&argv, RUN_GIT_CMD);
}
int interactive_add(const char **argv, const char *prefix, int patch)
@@ -756,8 +756,7 @@ static int checkout(int submodule_progress, int filter_submodules)
"--single-branch" :
"--no-single-branch");
- err = run_command_v_opt(args.v, RUN_GIT_CMD);
- strvec_clear(&args);
+ return run_command_sv_opt(&args, RUN_GIT_CMD);
}
return err;
@@ -1899,7 +1899,6 @@ static int is_schtasks_available(void)
static int schtasks_remove_task(enum schedule_priority schedule)
{
const char *cmd = "schtasks";
- int result;
struct strvec args = STRVEC_INIT;
const char *frequency = get_frequency(schedule);
@@ -1909,9 +1908,7 @@ static int schtasks_remove_task(enum schedule_priority schedule)
strvec_pushf(&args, SCHTASKS_NAME_FMT, frequency);
strvec_pushl(&args, "/f", NULL);
- result = run_command_v_opt(args.v, 0);
- strvec_clear(&args);
- return result;
+ return run_command_sv_opt(&args, 0);
}
static int schtasks_remove_tasks(void)
@@ -516,7 +516,6 @@ static void parse_repo_refspecs(int argc, const char **argv, const char **repo,
static int run_fetch(const char *repo, const char **refspecs)
{
struct strvec args = STRVEC_INIT;
- int ret;
strvec_pushl(&args, "fetch", "--update-head-ok", NULL);
@@ -582,9 +581,7 @@ static int run_fetch(const char *repo, const char **refspecs)
strvec_pushv(&args, refspecs);
} else if (*refspecs)
BUG("refspecs without repo?");
- ret = run_command_v_opt(args.v, RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE);
- strvec_clear(&args);
- return ret;
+ return run_command_sv_opt(&args, RUN_GIT_CMD | RUN_CLOSE_OBJECT_STORE);
}
/**
@@ -653,7 +650,6 @@ static int update_submodules(void)
*/
static int run_merge(void)
{
- int ret;
struct strvec args = STRVEC_INIT;
strvec_pushl(&args, "merge", NULL);
@@ -696,9 +692,7 @@ static int run_merge(void)
strvec_push(&args, "--allow-unrelated-histories");
strvec_push(&args, "FETCH_HEAD");
- ret = run_command_v_opt(args.v, RUN_GIT_CMD);
- strvec_clear(&args);
- return ret;
+ return run_command_sv_opt(&args, RUN_GIT_CMD);
}
/**
@@ -879,7 +873,6 @@ static int get_rebase_newbase_and_upstream(struct object_id *newbase,
static int run_rebase(const struct object_id *newbase,
const struct object_id *upstream)
{
- int ret;
struct strvec args = STRVEC_INIT;
strvec_push(&args, "rebase");
@@ -913,9 +906,7 @@ static int run_rebase(const struct object_id *newbase,
strvec_push(&args, oid_to_hex(upstream));
- ret = run_command_v_opt(args.v, RUN_GIT_CMD);
- strvec_clear(&args);
- return ret;
+ return run_command_sv_opt(&args, RUN_GIT_CMD);
}
static int get_can_ff(struct object_id *orig_head,
@@ -1508,7 +1508,6 @@ static int update(int argc, const char **argv, const char *prefix)
};
struct strvec fetch_argv = STRVEC_INIT;
int default_defined = 0;
- int retval;
argc = parse_options(argc, argv, prefix, options,
builtin_remote_update_usage,
@@ -1534,9 +1533,7 @@ static int update(int argc, const char **argv, const char *prefix)
}
}
- retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD);
- strvec_clear(&fetch_argv);
- return retval;
+ return run_command_sv_opt(&fetch_argv, RUN_GIT_CMD);
}
static int remove_all_fetch_refspecs(const char *key)
@@ -33,8 +33,7 @@ int try_merge_command(struct repository *r,
for (j = remotes; j; j = j->next)
strvec_push(&args, merge_argument(j->item));
- ret = run_command_v_opt(args.v, RUN_GIT_CMD);
- strvec_clear(&args);
+ ret = run_command_sv_opt(&args, RUN_GIT_CMD);
discard_index(r->index);
if (repo_read_index(r) < 0)
@@ -151,7 +151,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, run_command_l_opt, run_command_v_opt, run_command_sv_opt,
* child_process_clear do the following:
*
* - If a system call failed, errno is set and -1 is returned. A diagnostic
@@ -262,6 +262,24 @@ int run_command_v_opt(const char **argv, int opt);
LAST_ARG_MUST_BE_NULL
int run_command_l_opt(int opt, ...);
+/**
+ * The run_command_sv_opt() function is a wrapper for
+ * run_command_v_opt(). It takes a "struct strvec *args" which
+ * similarly to run_command() (but not run_command_sv_opt()) will be
+ * strvec_clear()'d before returning.
+ *
+ * Use it for the common case of constructing a "struct strvec" for a
+ * one-shot run_command_v_opt() invocation.
+ */
+RESULT_MUST_BE_USED
+static inline int run_command_sv_opt(struct strvec *args, int opt)
+{
+ int ret = run_command_v_opt(args->v, opt);
+
+ strvec_clear(args);
+ return ret;
+}
+
/**
* Execute the given command, sending "in" to its stdin, and capturing its
* stdout and stderr in the "out" and "err" strbufs. Any of the three may
@@ -72,7 +72,6 @@ static int run_git(const char *arg, ...)
struct strvec argv = STRVEC_INIT;
va_list args;
const char *p;
- int res;
va_start(args, arg);
strvec_push(&argv, arg);
@@ -80,10 +79,7 @@ static int run_git(const char *arg, ...)
strvec_push(&argv, p);
va_end(args);
- res = run_command_v_opt(argv.v, RUN_GIT_CMD);
-
- strvec_clear(&argv);
- return res;
+ return run_command_sv_opt(&argv, RUN_GIT_CMD);
}
struct scalar_config {
@@ -3183,7 +3183,6 @@ static int rollback_is_safe(void)
static int reset_merge(const struct object_id *oid)
{
- int ret;
struct strvec argv = STRVEC_INIT;
strvec_pushl(&argv, "reset", "--merge", NULL);
@@ -3191,10 +3190,7 @@ static int reset_merge(const struct object_id *oid)
if (!is_null_oid(oid))
strvec_push(&argv, oid_to_hex(oid));
- ret = run_command_v_opt(argv.v, RUN_GIT_CMD);
- strvec_clear(&argv);
-
- return ret;
+ return run_command_sv_opt(&argv, RUN_GIT_CMD);
}
static int rollback_single_pick(struct repository *r)
@@ -4866,7 +4862,6 @@ static int pick_commits(struct repository *r,
static int continue_single_pick(struct repository *r, struct replay_opts *opts)
{
struct strvec argv = STRVEC_INIT;
- int ret;
if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") &&
!refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD"))
@@ -4887,9 +4882,7 @@ static int continue_single_pick(struct repository *r, struct replay_opts *opts)
*/
strvec_pushl(&argv, "--no-edit", "--cleanup=strip", NULL);
- ret = run_command_v_opt(argv.v, RUN_GIT_CMD);
- strvec_clear(&argv);
- return ret;
+ return run_command_sv_opt(&argv, RUN_GIT_CMD);
}
static int commit_staged_changes(struct repository *r,
Add a run_command_sv_opt() convenience wrapper for run_command_v_opt(), as noted in the API documentation this is for the common case of wanting to construct a "struct strvec" to pass to run_command_v_opt(), and as it's a one-shot to strvec_clear() it afterwards. Let's convert those API users that were using a "ret" variable to carry over to "return" after a "strvec_clear()" to use this new function instead. Let's leave aside the user in "builtin/bisect--helper.c"'s bisect_visualize(). There's an outstanding topic that's extensively modifying it. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> --- add-interactive.c | 3 +-- builtin/add.c | 6 ++---- builtin/clone.c | 3 +-- builtin/gc.c | 5 +---- builtin/pull.c | 15 +++------------ builtin/remote.c | 5 +---- merge.c | 3 +-- run-command.h | 20 +++++++++++++++++++- scalar.c | 6 +----- sequencer.c | 11 ++--------- 10 files changed, 32 insertions(+), 45 deletions(-)