Skip to content

Commit 80a0548

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae2a382 commit 80a0548

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

builtin/worktree.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static const char *worktree_basename(const char *path, int *olen)
178178
return name;
179179
}
180180

181-
static int add_worktree(const char *path, const char **child_argv,
181+
static int add_worktree(const char *path, const char *refname,
182182
const struct add_opts *opts)
183183
{
184184
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,
261261
argv_array_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
262262
memset(&cp, 0, sizeof(cp));
263263
cp.git_cmd = 1;
264-
cp.argv = child_argv;
264+
argv_array_push(&cp.args, "checkout");
265+
if (opts->force)
266+
argv_array_push(&cp.args, "--ignore-other-worktrees");
267+
if (opts->detach)
268+
argv_array_push(&cp.args, "--detach");
269+
argv_array_push(&cp.args, refname);
265270
cp.env = child_env.argv;
266271
ret = run_command(&cp);
267272
if (!ret) {
@@ -286,7 +291,6 @@ static int add(int ac, const char **av, const char *prefix)
286291
struct add_opts opts;
287292
const char *new_branch_force = NULL;
288293
const char *path, *branch;
289-
struct argv_array cmd = ARGV_ARRAY_INIT;
290294
struct option options[] = {
291295
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
292296
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -331,14 +335,7 @@ static int add(int ac, const char **av, const char *prefix)
331335
branch = opts.new_branch;
332336
}
333337

334-
argv_array_push(&cmd, "checkout");
335-
if (opts.force)
336-
argv_array_push(&cmd, "--ignore-other-worktrees");
337-
if (opts.detach)
338-
argv_array_push(&cmd, "--detach");
339-
argv_array_push(&cmd, branch);
340-
341-
return add_worktree(path, cmd.argv, &opts);
338+
return add_worktree(path, branch, &opts);
342339
}
343340

344341
int cmd_worktree(int ac, const char **av, const char *prefix)

0 commit comments

Comments
 (0)