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 <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 28, 2018 at 17:20 UTC f4143101cbb26d189f63f2d29875f4acc07b2730
3 files changed +17 -5
Documentation/git-worktree.txt
+1
@@ -129,6 +129,7 @@ OPTIONS
129 twice.
130 +
131 `remove` refuses to remove an unclean working tree unless `--force` is used.
132 +To remove a locked working tree, specify `--force` twice.
133
134 -b <new-branch>::
135 -B <new-branch>::
builtin/worktree.c
+6 -5
@@ -875,13 +875,13 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
875 int force = 0;
876 struct option options[] = {
877 OPT__FORCE(&force,
878 - N_("force removing even if the worktree is dirty"),
878 + N_("force removal even if worktree is dirty or locked"),
879 PARSE_OPT_NOCOMPLETE),
880 OPT_END()
881 };
882 struct worktree **worktrees, *wt;
883 struct strbuf errmsg = STRBUF_INIT;
884 - const char *reason;
884 + const char *reason = NULL;
885 int ret = 0;
886
887 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)
894 die(_("'%s' is not a working tree"), av[0]);
895 if (is_main_worktree(wt))
896 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);
899 if (reason) {
900 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"),
902 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"));
904 }
905 if (validate_worktree(wt, &errmsg, WT_VALIDATE_WORKTREE_MISSING_OK))
906 die(_("validation failed, cannot remove working tree: %s"),
t/t2028-worktree-move.sh
+10
@@ -163,4 +163,14 @@ test_expect_success 'proper error when worktree not found' '
163 done
164 '
165
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 +
176 test_done