# [PATCH 3/3] worktree: add --recurse-submodules flag to worktree add

1 messages from 2026-04-16 to 2026-04-16. Participants: Jimmy Aguilar Mena.
Thread: https://gitlist.dev/t/65498

## Jimmy Aguilar Mena, 2026-04-16 16:36

Subject: [PATCH 3/3] worktree: add --recurse-submodules flag to worktree add
Message-ID: <aeEP2lMfN9_qCTm1@RTX>

```
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.

Signed-off-by: Jimmy Aguilar Mena <kratsbinovish@gmail.com>
---
  t/t2405-worktree-submodule.sh | 24 +++++++++++++++++++++++-
  1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
index 11018f37c7..5ffe0206dd 100755
--- a/t/t2405-worktree-submodule.sh
+++ b/t/t2405-worktree-submodule.sh
@@ -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



```
