checkout: add --ignore-other-wortrees

Noticed-by: Mark Levedahl <mlevedahl@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 3, 2015 at 16:41 UTC 1d0fa898eaa879d2fa4785164c3261dd40fe5f89
3 files changed +18 -1
Documentation/git-checkout.txt
+6
@@ -232,6 +232,12 @@ section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
232 specific files such as HEAD, index... See "MULTIPLE WORKING
233 TREES" section for more information.
234
235 +--ignore-other-worktrees::
236 + `git checkout` refuses when the wanted ref is already checked
237 + out by another worktree. This option makes it check the ref
238 + out anyway. In other words, the ref can be held by more than one
239 + worktree.
240 +
241 <branch>::
242 Branch to checkout; if it refers to a branch (i.e., a name that,
243 when prepended with "refs/heads/", is a valid ref), then that
builtin/checkout.c
+5 -1
@@ -37,6 +37,7 @@ struct checkout_opts {
37 int writeout_stage;
38 int overwrite_ignore;
39 int ignore_skipworktree;
40 + int ignore_other_worktrees;
41
42 const char *new_branch;
43 const char *new_branch_force;
@@ -1210,7 +1211,8 @@ static int parse_branchname_arg(int argc, const char **argv,
1211 int flag;
1212 char *head_ref = resolve_refdup("HEAD", 0, sha1, &flag);
1213 if (head_ref &&
1213 - (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)))
1214 + (!(flag & REF_ISSYMREF) || strcmp(head_ref, new->path)) &&
1215 + !opts->ignore_other_worktrees)
1216 check_linked_checkouts(new);
1217 free(head_ref);
1218 }
@@ -1341,6 +1343,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
1343 N_("second guess 'git checkout no-such-branch'")),
1344 OPT_FILENAME(0, "to", &opts.new_worktree,
1345 N_("check a branch out in a separate working directory")),
1346 + OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
1347 + N_("do not check if another worktree is holding the given ref")),
1348 OPT_END(),
1349 };
1350
t/t2025-checkout-to.sh
+7
@@ -79,6 +79,13 @@ test_expect_success 'die the same branch is already checked out' '
79 )
80 '
81
82 +test_expect_success 'not die the same branch is already checked out' '
83 + (
84 + cd here &&
85 + git checkout --ignore-other-worktrees --to anothernewmaster newmaster
86 + )
87 +'
88 +
89 test_expect_success 'not die on re-checking out current branch' '
90 (
91 cd there &&