worktree: teach 'move' to override lock when --force given twice
authorEric Sunshine <sunshine@sunshineco.com>
Tue, 28 Aug 2018 21:20:24 +0000 (17:20 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Aug 2018 16:28:02 +0000 (09:28 -0700)
For consistency with "add -f -f", which allows a missing but locked
worktree path to be re-used, allow "move -f -f" to override a lock,
as well, as a convenience.

Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-worktree.txt
builtin/worktree.c
t/t2028-worktree-move.sh
index 8537692f05ca7055078f2dff088680a44e6ade51..d08b8d8e4f94839ccd729592a9f462b6a98282cc 100644 (file)
@@ -125,6 +125,9 @@ OPTIONS
        manually). This option overrides these safeguards. To add a missing but
        locked working tree path, specify `--force` twice.
 +
+`move` refuses to move a locked working tree unless `--force` is specified
+twice.
++
 `remove` refuses to remove an unclean working tree unless `--force` is used.
 
 -b <new-branch>::
index 3eb2f89b0f181909a16189de725d50c3bc351e4c..354a6c0eb599c094c7912a9601eb20c5aed0e8e1 100644 (file)
@@ -740,13 +740,17 @@ static void validate_no_submodules(const struct worktree *wt)
 
 static int move_worktree(int ac, const char **av, const char *prefix)
 {
+       int force = 0;
        struct option options[] = {
+               OPT__FORCE(&force,
+                        N_("force move even if worktree is dirty or locked"),
+                        PARSE_OPT_NOCOMPLETE),
                OPT_END()
        };
        struct worktree **worktrees, *wt;
        struct strbuf dst = STRBUF_INIT;
        struct strbuf errmsg = STRBUF_INIT;
-       const char *reason;
+       const char *reason = NULL;
        char *path;
 
        ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
@@ -777,12 +781,13 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 
        validate_no_submodules(wt);
 
-       reason = is_worktree_locked(wt);
+       if (force < 2)
+               reason = is_worktree_locked(wt);
        if (reason) {
                if (*reason)
-                       die(_("cannot move a locked working tree, lock reason: %s"),
+                       die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
                            reason);
-               die(_("cannot move a locked working tree"));
+               die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
        }
        if (validate_worktree(wt, &errmsg, 0))
                die(_("validation failed, cannot move working tree: %s"),
index 60aba7c41ad3424b9a6cd1aba36360f90bd480b3..9756ede8f1527de7a92a44470d2c949b6e5d663d 100755 (executable)
@@ -98,6 +98,20 @@ test_expect_success 'move worktree to another dir' '
        test_cmp expected2 actual2
 '
 
+test_expect_success 'move locked worktree (force)' '
+       test_when_finished "
+               git worktree unlock flump || :
+               git worktree remove flump || :
+               git worktree unlock ploof || :
+               git worktree remove ploof || :
+               " &&
+       git worktree add --detach flump &&
+       git worktree lock flump &&
+       test_must_fail git worktree move flump ploof" &&
+       test_must_fail git worktree move --force flump ploof" &&
+       git worktree move --force --force flump ploof
+'
+
 test_expect_success 'remove main worktree' '
        test_must_fail git worktree remove .
 '