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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 17, 2015 at 19:00 UTC c2842439a3ab9bcab122105f3f457afb37fbc7c1
1 file changed +14 -3
builtin/worktree.c
+14 -3
@@ -314,12 +314,23 @@ static int add(int ac, const char **av, const char *prefix)
314 opts.new_branch = xstrndup(s, n);
315 }
316
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 +
331 argv_array_push(&cmd, "checkout");
332 if (opts.force)
333 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);
334 if (opts.detach)
335 argv_array_push(&cmd, "--detach");
336 argv_array_push(&cmd, branch);