Skip to content

Commit c284243

Browse files
sunshinecogitster
authored andcommitted
worktree: make branch creation distinct from worktree population
git-worktree currently conflates branch creation, setting of HEAD in the new worktree, and worktree population into a single sub-invocation of git-checkout, which requires git-checkout to be specially aware that it is operating in a newly-created worktree. The goal is to free git-checkout of that special knowledge, and to do so, git-worktree will eventually perform those operations separately. Thus, as a first step, rather than piggybacking on git-checkout's -b/-B ability to create a new branch at checkout time, make git-worktree responsible for branch creation itself. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c94257 commit c284243

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

builtin/worktree.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,23 @@ static int add(int ac, const char **av, const char *prefix)
314314
opts.new_branch = xstrndup(s, n);
315315
}
316316

317+
if (opts.new_branch) {
318+
struct child_process cp;
319+
memset(&cp, 0, sizeof(cp));
320+
cp.git_cmd = 1;
321+
argv_array_push(&cp.args, "branch");
322+
if (opts.force_new_branch)
323+
argv_array_push(&cp.args, "--force");
324+
argv_array_push(&cp.args, opts.new_branch);
325+
argv_array_push(&cp.args, branch);
326+
if (run_command(&cp))
327+
return -1;
328+
branch = opts.new_branch;
329+
}
330+
317331
argv_array_push(&cmd, "checkout");
318332
if (opts.force)
319333
argv_array_push(&cmd, "--ignore-other-worktrees");
320-
if (opts.new_branch)
321-
argv_array_pushl(&cmd, opts.force_new_branch ? "-B" : "-b",
322-
opts.new_branch, NULL);
323334
if (opts.detach)
324335
argv_array_push(&cmd, "--detach");
325336
argv_array_push(&cmd, branch);

0 commit comments

Comments
 (0)