Skip to contentSkip to conversation

worktree: add --recurse-submodules flag to worktree add

PATCH In review Jimmy Aguilar Mena 1 participant 1 message Apr 16

lore
Share & tools
lore
#0 lore raw

PATCH 3/3 worktree: add --recurse-submodules flag to worktree add

Add two new tests to t2405-worktree-submodule.sh:

1. "worktree add --recurse-submodules initializes submodules": verifies that the submodule working tree is populated and that "git diff --submodule" shows the expected commit difference after --recurse-submodules is used.

2. "submodule in --recurse-submodules worktree uses per-worktree gitdir":
verifies the isolation guarantee -- the submodule gitdir for the linked worktree lives under $GIT_COMMON_DIR/worktrees/<id>/modules/, not under $GIT_COMMON_DIR/modules/, and the .git pointer file inside the working tree references that per-worktree location. This ensures that removing the linked worktree also removes its submodule gitdirs, and that the main worktree's submodule gitdir is unaffected.

Also rename the existing test_expect_failure to clarify that it covers the case without --recurse-submodules.

---
 t/t2405-worktree-submodule.sh | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)
t/t2405-worktree-submodule.sh +23 -1
index 11018f37c7..5ffe0206dd 100755
@@ -34,11 +34,33 @@ test_expect_success 'add superproject worktree' '
 	git -C main worktree add "$base_path/worktree" "$rev1_hash_main"
 '
 
-test_expect_failure 'submodule is checked out just after worktree add' '
+test_expect_failure 'submodule is checked out just after worktree add (without flag)' '
 	git -C worktree diff --submodule main"^!" >out &&
 	grep "file1 updated" out
 '
 
+test_expect_success 'worktree add --recurse-submodules initializes submodules' '
+	git -C main worktree add --recurse-submodules \
+		"$base_path/worktree-recurse" "$rev1_hash_main" &&
+	git -C worktree-recurse diff --submodule main"^!" >out &&
+	grep "file1 updated" out
+'
+
+test_expect_success 'submodule in --recurse-submodules worktree uses per-worktree gitdir' '
+	# The per-worktree submodule gitdir must live under the worktree entry,
+	# not under $GIT_COMMON_DIR/modules/, so it is cleaned up with the
+	# worktree and does not disturb the main worktree submodule.
+	sub_gitdir="$base_path/main/.git/worktrees/worktree-recurse/modules/sub" &&
+	test -d "$sub_gitdir" &&
+	# .git pointer in the working tree must reference the per-worktree gitdir
+	echo "gitdir: ../../main/.git/worktrees/worktree-recurse/modules/sub" \
+		>expect-gitfile &&
+	cat "$base_path/worktree-recurse/sub/.git" >actual-gitfile &&
+	test_cmp expect-gitfile actual-gitfile &&
+	# The working tree is populated (test_commit creates <name>.t files)
+	test -f "$base_path/worktree-recurse/sub/file1.t"
+'
+
 test_expect_success 'add superproject worktree and initialize submodules' '
 	git -C main worktree add "$base_path/worktree-submodule-update" "$rev1_hash_main" &&
 	git -C worktree-submodule-update submodule update