worktree: add -b/-B options

One of git-worktree's roles is to populate the new worktree, much like git-checkout, and thus, for convenience, ought to support several of the same shortcuts. Toward this goal, add -b/-B options to create a new branch and check it out in the new worktree. (For brevity, only -b is mentioned in the synopsis; -B is omitted.) Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 6, 2015 at 13:30 UTC cbdf60fa183e3a2330849046846b400ef4a99c3b
2 files changed +21 -3
Documentation/git-worktree.txt
+10 -3
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git worktree add' [-f] [--detach] <path> <branch>
12 +'git worktree add' [-f] [--detach] [-b <new-branch>] <path> <branch>
13 'git worktree prune' [-n] [-v] [--expire <expire>]
14
15 DESCRIPTION
@@ -64,6 +64,14 @@ OPTIONS
64 is already checked out by another worktree. This option overrides
65 that safeguard.
66
67 +-b <new-branch>::
68 +-B <new-branch>::
69 + With `add`, create a new branch named `<new-branch>` starting at
70 + `<branch>`, and check out `<new-branch>` into the new worktree.
71 + By default, `-b` refuses to create a new branch if it already
72 + exists. `-B` overrides this safeguard, resetting `<new-branch>` to
73 + `<branch>`.
74 +
75 --detach::
76 With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in
77 linkgit:git-checkout[1].
@@ -133,8 +141,7 @@ make the emergency fix, remove it when done, and then resume your earlier
141 refactoring session.
142
143 ------------
136 -$ git branch emergency-fix master
137 -$ git worktree add ../temp emergency-fix
144 +$ git worktree add -b emergency-fix ../temp master
145 $ pushd ../temp
146 # ... hack hack hack ...
147 $ git commit -a -m 'emergency fix for boss'
builtin/worktree.c
+11
@@ -126,15 +126,22 @@ static int add(int ac, const char **av, const char *prefix)
126 {
127 struct child_process c;
128 int force = 0, detach = 0;
129 + const char *new_branch = NULL, *new_branch_force = NULL;
130 const char *path, *branch;
131 struct argv_array cmd = ARGV_ARRAY_INIT;
132 struct option options[] = {
133 OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
134 + OPT_STRING('b', NULL, &new_branch, N_("branch"),
135 + N_("create a new branch")),
136 + OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
137 + N_("create or reset a branch")),
138 OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
139 OPT_END()
140 };
141
142 ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
143 + if (new_branch && new_branch_force)
144 + die(_("-b and -B are mutually exclusive"));
145 if (ac != 2)
146 usage_with_options(worktree_usage, options);
147
@@ -145,6 +152,10 @@ static int add(int ac, const char **av, const char *prefix)
152 argv_array_pushl(&cmd, "--to", path, NULL);
153 if (force)
154 argv_array_push(&cmd, "--ignore-other-worktrees");
155 + if (new_branch)
156 + argv_array_pushl(&cmd, "-b", new_branch, NULL);
157 + if (new_branch_force)
158 + argv_array_pushl(&cmd, "-B", new_branch_force, NULL);
159 if (detach)
160 argv_array_push(&cmd, "--detach");
161 argv_array_push(&cmd, branch);