Skip to content

Commit 68a6b3a

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e19831c commit 68a6b3a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

Documentation/git-worktree.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ OPTIONS
125125
manually). This option overrides these safeguards. To add a missing but
126126
locked working tree path, specify `--force` twice.
127127
+
128+
`move` refuses to move a locked working tree unless `--force` is specified
129+
twice.
130+
+
128131
`remove` refuses to remove an unclean working tree unless `--force` is used.
129132

130133
-b <new-branch>::

builtin/worktree.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -740,13 +740,17 @@ static void validate_no_submodules(const struct worktree *wt)
740740

741741
static int move_worktree(int ac, const char **av, const char *prefix)
742742
{
743+
int force = 0;
743744
struct option options[] = {
745+
OPT__FORCE(&force,
746+
N_("force move even if worktree is dirty or locked"),
747+
PARSE_OPT_NOCOMPLETE),
744748
OPT_END()
745749
};
746750
struct worktree **worktrees, *wt;
747751
struct strbuf dst = STRBUF_INIT;
748752
struct strbuf errmsg = STRBUF_INIT;
749-
const char *reason;
753+
const char *reason = NULL;
750754
char *path;
751755

752756
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)
777781

778782
validate_no_submodules(wt);
779783

780-
reason = is_worktree_locked(wt);
784+
if (force < 2)
785+
reason = is_worktree_locked(wt);
781786
if (reason) {
782787
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"),
784789
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"));
786791
}
787792
if (validate_worktree(wt, &errmsg, 0))
788793
die(_("validation failed, cannot move working tree: %s"),

t/t2028-worktree-move.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ test_expect_success 'move worktree to another dir' '
9898
test_cmp expected2 actual2
9999
'
100100

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+
101115
test_expect_success 'remove main worktree' '
102116
test_must_fail git worktree remove .
103117
'

0 commit comments

Comments
 (0)