worktree: teach 'add' to respect --force for registered but missing path

For safety, "git worktree add <path>" will refuse to add a new worktree at <path> if <path> is already associated with a worktree entry, even if <path> is missing (for instance, has been deleted or resides on non-mounted removable media or network share). The typical way to re-create a worktree at <path> in such a situation is either to prune all "broken" entries ("git worktree prune") or to selectively remove the worktree entry manually ("git worktree remove <path>"). However, neither of these approaches ("prune" nor "remove") is especially convenient, and they may be unsuitable for scripting when a tool merely wants to re-use a worktree if it exists or create it from scratch if it doesn't (much as a tool might use "mkdir -p" to re-use or create a directory). Therefore, teach 'add' to respect --force as a convenient way to re-use a path already associated with a worktree entry if the path is non-existent. For a locked worktree, require --force to be specified twice. 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 e19831c94f91fd410fe001c0372b9c88b40d335b
3 files changed +26 -5
Documentation/git-worktree.txt
+6 -2
@@ -120,8 +120,12 @@ OPTIONS
120 --force::
121 By default, `add` refuses to create a new working tree when
122 `<commit-ish>` is a branch name and is already checked out by
123 - another working tree and `remove` refuses to remove an unclean
124 - working tree. This option overrides these safeguards.
123 + another working tree, or if `<path>` is already assigned to some
124 + working tree but is missing (for instance, if `<path>` was deleted
125 + manually). This option overrides these safeguards. To add a missing but
126 + locked working tree path, specify `--force` twice.
127 ++
128 +`remove` refuses to remove an unclean working tree unless `--force` is used.
129
130 -b <new-branch>::
131 -B <new-branch>::
builtin/worktree.c
+8 -2
@@ -241,10 +241,16 @@ static void validate_worktree_add(const char *path, const struct add_opts *opts)
241 goto done;
242
243 locked = !!is_worktree_locked(wt);
244 + if ((!locked && opts->force) || (locked && opts->force > 1)) {
245 + if (delete_git_dir(wt->id))
246 + die(_("unable to re-add worktree '%s'"), path);
247 + goto done;
248 + }
249 +
250 if (locked)
245 - die(_("'%s' is a missing but locked worktree;\nuse 'unlock' and 'prune' or 'remove' to clear"), path);
251 + die(_("'%s' is a missing but locked worktree;\nuse 'add -f -f' to override, or 'unlock' and 'prune' or 'remove' to clear"), path);
252 else
247 - die(_("'%s' is a missing but already registered worktree;\nuse 'prune' or 'remove' to clear"), path);
253 + die(_("'%s' is a missing but already registered worktree;\nuse 'add -f' to override, or 'prune' or 'remove' to clear"), path);
254
255 done:
256 free_worktrees(worktrees);
t/t2025-worktree-add.sh
+12 -1
@@ -556,7 +556,18 @@ test_expect_success '"add" an existing but missing worktree' '
556 git worktree add --detach pneu &&
557 test_must_fail git worktree add --detach pneu &&
558 rm -fr pneu &&
559 - test_must_fail git worktree add --detach pneu
559 + test_must_fail git worktree add --detach pneu &&
560 + git worktree add --force --detach pneu
561 +'
562 +
563 +test_expect_success '"add" an existing locked but missing worktree' '
564 + git worktree add --detach gnoo &&
565 + git worktree lock gnoo &&
566 + test_when_finished "git worktree unlock gnoo || :" &&
567 + rm -fr gnoo &&
568 + test_must_fail git worktree add --detach gnoo &&
569 + test_must_fail git worktree add --force --detach gnoo &&
570 + git worktree add --force --force --detach gnoo
571 '
572
573 test_done