worktree: add --force option

By default, "git worktree add" refuses to create a new worktree when the requested branch is already checked out elsewhere. Add a --force option to override this safeguard. 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 f43254440d56da1fd52216cbebd5c285b20985e6
2 files changed +12 -2
Documentation/git-worktree.txt
+7 -1
@@ -9,7 +9,7 @@ git-worktree - Manage multiple worktrees
9 SYNOPSIS
10 --------
11 [verse]
12 -'git worktree add' <path> <branch>
12 +'git worktree add' [-f] <path> <branch>
13 'git worktree prune' [-n] [-v] [--expire <expire>]
14
15 DESCRIPTION
@@ -58,6 +58,12 @@ Prune working tree information in $GIT_DIR/worktrees.
58 OPTIONS
59 -------
60
61 +-f::
62 +--force::
63 + By default, `add` refuses to create a new worktree when `<branch>`
64 + is already checked out by another worktree. This option overrides
65 + that safeguard.
66 +
67 -n::
68 --dry-run::
69 With `prune`, do not remove anything; just report what it would
builtin/worktree.c
+5 -1
@@ -6,7 +6,7 @@
6 #include "run-command.h"
7
8 static const char * const worktree_usage[] = {
9 - N_("git worktree add <path> <branch>"),
9 + N_("git worktree add [<options>] <path> <branch>"),
10 N_("git worktree prune [<options>]"),
11 NULL
12 };
@@ -125,9 +125,11 @@ 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;
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_END()
134 };
135
@@ -140,6 +142,8 @@ static int add(int ac, const char **av, const char *prefix)
142
143 argv_array_push(&cmd, "checkout");
144 argv_array_pushl(&cmd, "--to", path, NULL);
145 + if (force)
146 + argv_array_push(&cmd, "--ignore-other-worktrees");
147 argv_array_push(&cmd, branch);
148
149 memset(&c, 0, sizeof(c));