path: root/builtin/worktree.c
diff options
authorEric Sunshine <>2015-07-17 23:00:12 (GMT)
committerJunio C Hamano <>2015-07-20 18:29:52 (GMT)
commit80a0548f6c12f43e9bd62e13eacb033f05e2b001 (patch)
tree05cf608fee704b88993928bcc684fab7d27fd960 /builtin/worktree.c
parentae2a38271f778522ceb9182b94e0024a816e3338 (diff)
worktree: add_worktree: construct worktree-population command locally
The caller of add_worktree() provides it with a command to invoke to populate the new worktree. This was a useful abstraction during the conversion of "git checkout --to" functionality to "git worktree add" since git-checkout and git-worktree constructed the population command differently. However, now that "git checkout --to" has been retired, and add_worktree() has access to the options given to "worktree add", this extra indirection is no longer useful and makes the code a bit convoluted. Moreover, the eventual goal is for git-worktree to make setting of HEAD and worktree population distinct operations, whereas they are currently conflated into a single git-checkout invocation. As such, add_worktree() will eventually invoke other commands in addition to the worktree population command, so it will be doing command construction itself anyhow. Therefore, relocate construction of the worktree population command from add() to add_worktree(). Signed-off-by: Eric Sunshine <> Signed-off-by: Junio C Hamano <>
Diffstat (limited to 'builtin/worktree.c')
1 files changed, 8 insertions, 11 deletions
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 7bd6f17..da76eb7 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -178,7 +178,7 @@ static const char *worktree_basename(const char *path, int *olen)
return name;
-static int add_worktree(const char *path, const char **child_argv,
+static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts)
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
@@ -261,7 +261,12 @@ static int add_worktree(const char *path, const char **child_argv,
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
memset(&cp, 0, sizeof(cp));
cp.git_cmd = 1;
- cp.argv = child_argv;
+ argv_array_push(&cp.args, "checkout");
+ if (opts->force)
+ argv_array_push(&cp.args, "--ignore-other-worktrees");
+ if (opts->detach)
+ argv_array_push(&cp.args, "--detach");
+ argv_array_push(&cp.args, refname);
cp.env = child_env.argv;
ret = run_command(&cp);
if (!ret) {
@@ -286,7 +291,6 @@ static int add(int ac, const char **av, const char *prefix)
struct add_opts opts;
const char *new_branch_force = NULL;
const char *path, *branch;
- struct argv_array cmd = ARGV_ARRAY_INIT;
struct option options[] = {
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -331,14 +335,7 @@ static int add(int ac, const char **av, const char *prefix)
branch = opts.new_branch;
- argv_array_push(&cmd, "checkout");
- if (opts.force)
- argv_array_push(&cmd, "--ignore-other-worktrees");
- if (opts.detach)
- argv_array_push(&cmd, "--detach");
- argv_array_push(&cmd, branch);
- return add_worktree(path, cmd.argv, &opts);
+ return add_worktree(path, branch, &opts);
int cmd_worktree(int ac, const char **av, const char *prefix)