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

[PATCH v2 5/7] worktree move: refuse to move worktrees with submodules

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Feb 12, 2018, 09:49 UTC
Message-ID
<20180212094940.23834-6-pclouds@gmail.com>
In-Reply-To
<20180212094940.23834-1-pclouds@gmail.com>

Submodules contains .git files with relative paths. After a worktree move, these files need to be updated or they may point to nowhere.

This is a bandage patch to make sure "worktree move" don't break people's worktrees by accident. When .git file update code is in place, this validate_no_submodules() could be removed.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-worktree.txt |  2 +-
 builtin/worktree.c             | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index 4fa1dd3a48..e6764ee8e0 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -79,7 +79,7 @@ with `--reason`.
 move::
 
 Move a working tree to a new location. Note that the main working tree
-cannot be moved.
+or linked working trees containing submodules cannot be moved.
 
 prune::
 
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 6fe41313c9..4789cebe11 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -606,6 +606,27 @@ static int unlock_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static void validate_no_submodules(const struct worktree *wt)
+{
+	struct index_state istate = { NULL };
+	int i, found_submodules = 0;
+
+	if (read_index_from(&istate, worktree_git_path(wt, "index")) > 0) {
+		for (i = 0; i < istate.cache_nr; i++) {
+			struct cache_entry *ce = istate.cache[i];
+
+			if (S_ISGITLINK(ce->ce_mode)) {
+				found_submodules = 1;
+				break;
+			}
+		}
+	}
+	discard_index(&istate);
+
+	if (found_submodules)
+		die(_("working trees containing submodules cannot be moved"));
+}
+
 static int move_worktree(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -643,6 +664,8 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	if (file_exists(dst.buf))
 		die(_("target '%s' already exists"), dst.buf);
 
+	validate_no_submodules(wt);
+
 	reason = is_worktree_locked(wt);
 	if (reason) {
 		if (*reason)
-- 
2.16.1.399.g632f88eed1
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 40 of 49 in “nd/worktree-move reboot”
  1. 0/7 nd/worktree-move rebootNguyễn Thái Ngọc Duy, Jan 24, 2018
  2. 1/7 worktree.c: add validate_worktree()Nguyễn Thái Ngọc Duy, Jan 24, 2018
  3. 2/7 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Jan 24, 2018
  4. Eric SunshineFeb 2, 2018
  5. Duy NguyenFeb 2, 2018
  6. 3/7 worktree move: new commandNguyễn Thái Ngọc Duy, Jan 24, 2018
  7. Eric SunshineFeb 2, 2018
  8. Eric SunshineFeb 2, 2018
  9. Duy NguyenFeb 5, 2018
  10. Jeff KingFeb 6, 2018
  11. Martin ÅgrenFeb 6, 2018
  12. Duy NguyenFeb 12, 2018
  13. Martin ÅgrenFeb 12, 2018
  14. Duy NguyenFeb 13, 2018
  15. Jeff KingFeb 14, 2018
  16. Duy NguyenFeb 14, 2018
  17. Junio C HamanoFeb 14, 2018
  18. t/known-leaky: add list of known-leaky test scriptsMartin Ågren, Feb 14, 2018
  19. Jeff KingFeb 19, 2018
  20. Martin ÅgrenFeb 20, 2018
  21. Jeff KingFeb 20, 2018
  22. Junio C HamanoFeb 21, 2018
  23. Jeff KingFeb 21, 2018
  24. Kaartic SivaraamFeb 25, 2018
  25. Martin ÅgrenFeb 26, 2018
  26. 4/7 worktree move: accept destination as directoryNguyễn Thái Ngọc Duy, Jan 24, 2018
  27. Eric SunshineFeb 2, 2018
  28. 5/7 worktree move: refuse to move worktrees with submodulesNguyễn Thái Ngọc Duy, Jan 24, 2018
  29. Eric SunshineFeb 2, 2018
  30. 6/7 worktree remove: new commandNguyễn Thái Ngọc Duy, Jan 24, 2018
  31. Eric SunshineFeb 2, 2018
  32. Duy NguyenFeb 12, 2018
  33. 7/7 worktree remove: allow it when $GIT_WORK_TREE is already goneNguyễn Thái Ngọc Duy, Jan 24, 2018
  34. Eric SunshineFeb 2, 2018
  35. Junio C HamanoJan 24, 2018
  36. 0/7 nd/worktree-move rebootNguyễn Thái Ngọc Duy, Feb 12, 2018
  37. 1/7 worktree.c: add validate_worktree()Nguyễn Thái Ngọc Duy, Feb 12, 2018
  38. 2/7 worktree.c: add update_worktree_location()Nguyễn Thái Ngọc Duy, Feb 12, 2018
  39. 4/7 worktree move: accept destination as directoryNguyễn Thái Ngọc Duy, Feb 12, 2018
  40. 5/7 worktree move: refuse to move worktrees with submodulesNguyễn Thái Ngọc Duy, Feb 12, 2018
  41. 6/7 worktree remove: new commandNguyễn Thái Ngọc Duy, Feb 12, 2018
  42. 7/7 worktree remove: allow it when $GIT_WORK_TREE is already goneNguyễn Thái Ngọc Duy, Feb 12, 2018
  43. 3/7 worktree move: new commandNguyễn Thái Ngọc Duy, Feb 12, 2018
  44. t2028: fix minor error and issues in newly-added "worktree move" testsEric Sunshine, Mar 4, 2018
  45. Duy NguyenMar 5, 2018
  46. SZEDER GáborMar 5, 2018
  47. Junio C HamanoMar 5, 2018
  48. Eric SunshineMar 5, 2018
  49. Eric SunshineMar 4, 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.