worktree: add --detach option

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 a --detach option to detach HEAD in the new worktree. 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 39ecb27436296361371d3caadcc9bc4a4e0b556d
2 files changed +9 -2
Documentation/git-worktree.txt
+5 -1
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git worktree add' [-f] <path> <branch>
12 +'git worktree add' [-f] [--detach] <path> <branch>
13 'git worktree prune' [-n] [-v] [--expire <expire>]
14
15 DESCRIPTION
@@ -64,6 +64,10 @@ OPTIONS
64 is already checked out by another worktree. This option overrides
65 that safeguard.
66
67 +--detach::
68 + With `add`, detach HEAD in the new worktree. See "DETACHED HEAD" in
69 + linkgit:git-checkout[1].
70 +
71 -n::
72 --dry-run::
73 With `prune`, do not remove anything; just report what it would
builtin/worktree.c
+4 -1
@@ -125,11 +125,12 @@ static int prune(int ac, const char **av, const char *prefix)
125 static int add(int ac, const char **av, const char *prefix)
126 {
127 struct child_process c;
128 - int force = 0;
128 + int force = 0, detach = 0;
129 const char *path, *branch;
130 struct argv_array cmd = ARGV_ARRAY_INIT;
131 struct option options[] = {
132 OPT__FORCE(&force, N_("checkout <branch> even if already checked out in other worktree")),
133 + OPT_BOOL(0, "detach", &detach, N_("detach HEAD at named commit")),
134 OPT_END()
135 };
136
@@ -144,6 +145,8 @@ static int add(int ac, const char **av, const char *prefix)
145 argv_array_pushl(&cmd, "--to", path, NULL);
146 if (force)
147 argv_array_push(&cmd, "--ignore-other-worktrees");
148 + if (detach)
149 + argv_array_push(&cmd, "--detach");
150 argv_array_push(&cmd, branch);
151
152 memset(&c, 0, sizeof(c));