Skip to content

Commit f414310

Browse files
sunshinecogitster
authored andcommitted
worktree: teach 'remove' to override lock when --force given twice
For consistency with "add -f -f" and "move -f -f" which override the lock on a worktree, allow "remove -f -f" to do so, as well, as a convenience. Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68a6b3a commit f414310

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Documentation/git-worktree.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ OPTIONS
129129
twice.
130130
+
131131
`remove` refuses to remove an unclean working tree unless `--force` is used.
132+
To remove a locked working tree, specify `--force` twice.
132133

133134
-b <new-branch>::
134135
-B <new-branch>::

builtin/worktree.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -875,13 +875,13 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
875875
int force = 0;
876876
struct option options[] = {
877877
OPT__FORCE(&force,
878-
N_("force removing even if the worktree is dirty"),
878+
N_("force removal even if worktree is dirty or locked"),
879879
PARSE_OPT_NOCOMPLETE),
880880
OPT_END()
881881
};
882882
struct worktree **worktrees, *wt;
883883
struct strbuf errmsg = STRBUF_INIT;
884-
const char *reason;
884+
const char *reason = NULL;
885885
int ret = 0;
886886

887887
ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
@@ -894,12 +894,13 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
894894
die(_("'%s' is not a working tree"), av[0]);
895895
if (is_main_worktree(wt))
896896
die(_("'%s' is a main working tree"), av[0]);
897-
reason = is_worktree_locked(wt);
897+
if (force < 2)
898+
reason = is_worktree_locked(wt);
898899
if (reason) {
899900
if (*reason)
900-
die(_("cannot remove a locked working tree, lock reason: %s"),
901+
die(_("cannot remove a locked working tree, lock reason: %s\nuse 'remove -f -f' to override or unlock first"),
901902
reason);
902-
die(_("cannot remove a locked working tree"));
903+
die(_("cannot remove a locked working tree;\nuse 'remove -f -f' to override or unlock first"));
903904
}
904905
if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
905906
die(_("validation failed, cannot remove working tree: %s"),

t/t2028-worktree-move.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,14 @@ test_expect_success 'proper error when worktree not found' '
163163
done
164164
'
165165

166+
test_expect_success 'remove locked worktree (force)' '
167+
git worktree add --detach gumby &&
168+
test_when_finished "git worktree remove gumby || :" &&
169+
git worktree lock gumby &&
170+
test_when_finished "git worktree unlock gumby || :" &&
171+
test_must_fail git worktree remove gumby &&
172+
test_must_fail git worktree remove --force gumby &&
173+
git worktree remove --force --force gumby
174+
'
175+
166176
test_done

0 commit comments

Comments
 (0)