git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/9] worktree: teach 'move' to override lock when --force given twice

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 28, 2018, 21:20 UTC
Message-ID
<20180828212026.21989-8-sunshine@sunshineco.com>
In-Reply-To
<20180828212026.21989-1-sunshine@sunshineco.com>

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>
---
 Documentation/git-worktree.txt |  3 +++
 builtin/worktree.c             | 13 +++++++++----
 t/t2028-worktree-move.sh       | 14 ++++++++++++++
 3 files changed, 26 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 8537692f05..d08b8d8e4f 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -125,6 +125,9 @@ OPTIONS
 	manually). This option overrides these safeguards. To add a missing but
 	locked working tree path, specify `--force` twice.
 +
+`move` refuses to move a locked working tree unless `--force` is specified
+twice.
++
 `remove` refuses to remove an unclean working tree unless `--force` is used.
 
 -b <new-branch>::
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 3eb2f89b0f..354a6c0eb5 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -740,13 +740,17 @@ static void validate_no_submodules(const struct worktree *wt)
 
 static int move_worktree(int ac, const char **av, const char *prefix)
 {
+	int force = 0;
 	struct option options[] = {
+		OPT__FORCE(&force,
+			 N_("force move even if worktree is dirty or locked"),
+			 PARSE_OPT_NOCOMPLETE),
 		OPT_END()
 	};
 	struct worktree **worktrees, *wt;
 	struct strbuf dst = STRBUF_INIT;
 	struct strbuf errmsg = STRBUF_INIT;
-	const char *reason;
+	const char *reason = NULL;
 	char *path;
 
 	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)
 
 	validate_no_submodules(wt);
 
-	reason = is_worktree_locked(wt);
+	if (force < 2)
+		reason = is_worktree_locked(wt);
 	if (reason) {
 		if (*reason)
-			die(_("cannot move a locked working tree, lock reason: %s"),
+			die(_("cannot move a locked working tree, lock reason: %s\nuse 'move -f -f' to override or unlock first"),
 			    reason);
-		die(_("cannot move a locked working tree"));
+		die(_("cannot move a locked working tree;\nuse 'move -f -f' to override or unlock first"));
 	}
 	if (validate_worktree(wt, &errmsg, 0))
 		die(_("validation failed, cannot move working tree: %s"),
diff --git a/t/t2028-worktree-move.sh b/t/t2028-worktree-move.sh
index 60aba7c41a..9756ede8f1 100755
--- a/t/t2028-worktree-move.sh
+++ b/t/t2028-worktree-move.sh
@@ -98,6 +98,20 @@ test_expect_success 'move worktree to another dir' '
 	test_cmp expected2 actual2
 '
 
+test_expect_success 'move locked worktree (force)' '
+	test_when_finished "
+		git worktree unlock flump || :
+		git worktree remove flump || :
+		git worktree unlock ploof || :
+		git worktree remove ploof || :
+		" &&
+	git worktree add --detach flump &&
+	git worktree lock flump &&
+	test_must_fail git worktree move flump ploof" &&
+	test_must_fail git worktree move --force flump ploof" &&
+	git worktree move --force --force flump ploof
+'
+
 test_expect_success 'remove main worktree' '
 	test_must_fail git worktree remove .
 '
-- 
2.19.0.rc1.350.ge57e33dbd1
Previous: Eric SunshineNext: Jeff King
Message 15 of 28 in “worktree: fix bugs and broaden --force applicability”
  1. 0/9 worktree: fix bugs and broaden --force applicabilityEric Sunshine, Aug 28, 2018
  2. 1/9 worktree: don't die() in library function find_worktree()Eric Sunshine, Aug 28, 2018
  3. 2/9 worktree: move delete_git_dir() earlier in file for upcoming new callersEric Sunshine, Aug 28, 2018
  4. 4/9 worktree: prepare for more checks of whether path can become worktreeEric Sunshine, Aug 28, 2018
  5. 3/9 worktree: generalize delete_git_dir() to reduce code duplicationEric Sunshine, Aug 28, 2018
  6. Jeff KingAug 30, 2018
  7. Eric SunshineAug 30, 2018
  8. 6/9 worktree: teach 'add' to respect --force for registered but missing pathEric Sunshine, Aug 28, 2018
  9. Jeff KingAug 30, 2018
  10. Eric SunshineAug 30, 2018
  11. 8/9 worktree: teach 'remove' to override lock when --force given twiceEric Sunshine, Aug 28, 2018
  12. Jeff KingAug 30, 2018
  13. Eric SunshineAug 30, 2018
  14. 9/9 worktree: delete .git/worktrees if empty after 'remove'Eric Sunshine, Aug 28, 2018
  15. 7/9 worktree: teach 'move' to override lock when --force given twiceEric Sunshine, Aug 28, 2018
  16. Jeff KingAug 30, 2018
  17. Eric SunshineAug 30, 2018
  18. 5/9 worktree: disallow adding same path multiple timesEric Sunshine, Aug 28, 2018
  19. Jeff KingAug 30, 2018
  20. Eric SunshineAug 30, 2018
  21. Jeff KingAug 30, 2018
  22. Eric SunshineAug 30, 2018
  23. Eric SunshineAug 30, 2018
  24. Jeff KingAug 30, 2018
  25. Junio C HamanoAug 30, 2018
  26. Ramsay JonesAug 30, 2018
  27. Jeff KingAug 31, 2018
  28. Ramsay JonesAug 31, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.