worktree: teach 'move' to override lock when --force given twice
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>
Eric Sunshine committed
Aug 28, 2018 at 17:20 UTC
68a6b3a1bd45eb1814a0c1cedfe347692c3f2ff7
3 files changed
+26
-4
Documentation/git-worktree.txt
+3
@@ -125,6 +125,9 @@ OPTIONS
125
manually). This option overrides these safeguards. To add a missing but
126
locked working tree path, specify `--force` twice.
127
+
128
+`move` refuses to move a locked working tree unless `--force` is specified
129
+twice.
130
++
131
`remove` refuses to remove an unclean working tree unless `--force` is used.
132
133
-b <new-branch>::
builtin/worktree.c
+9
-4
@@ -740,13 +740,17 @@ static void validate_no_submodules(const struct worktree *wt)
740
741
static int move_worktree(int ac, const char **av, const char *prefix)
742
{
743
+ int force = 0;
744
struct option options[] = {
745
+ OPT__FORCE(&force,
746
+ N_("force move even if worktree is dirty or locked"),
747
+ PARSE_OPT_NOCOMPLETE),
748
OPT_END()
749
};
750
struct worktree **worktrees, *wt;
751
struct strbuf dst = STRBUF_INIT;
752
struct strbuf errmsg = STRBUF_INIT;
749
- const char *reason;
753
+ const char *reason = NULL;
754
char *path;
755
756
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)
781
782
validate_no_submodules(wt);
783
780
- reason = is_worktree_locked(wt);
784
+ if (force < 2)
785
+ reason = is_worktree_locked(wt);
786
if (reason) {
787
if (*reason)
783
- die(_("cannot move a locked working tree, lock reason: %s"),
788
+ die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
789
reason);
785
- die(_("cannot move a locked working tree"));
790
+ die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
791
}
792
if (validate_worktree(wt, &errmsg, 0))
793
die(_("validation failed, cannot move working tree: %s"),
t/t2028-worktree-move.sh
+14
@@ -98,6 +98,20 @@ test_expect_success 'move worktree to another dir' '
98
test_cmp expected2 actual2
99
'
100
101
+test_expect_success 'move locked worktree (force)' '
102
+ test_when_finished "
103
+ git worktree unlock flump || :
104
+ git worktree remove flump || :
105
+ git worktree unlock ploof || :
106
+ git worktree remove ploof || :
107
+ " &&
108
+ git worktree add --detach flump &&
109
+ git worktree lock flump &&
110
+ test_must_fail git worktree move flump ploof" &&
111
+ test_must_fail git worktree move --force flump ploof" &&
112
+ git worktree move --force --force flump ploof
113
+'
114
+
115
test_expect_success 'remove main worktree' '
116
test_must_fail git worktree remove .
117
'