Skip to content

worktree: add --recurse-submodules support to git worktree add

Jimmy Aguilar Mena git 1 version In review

Get series with b4
b4 am aeEMU-ohKz2tnSWq@RTX

Versions newest first

  1. v1 Latest3 patches 4 repliesIn review

Changes since the RFC from the cover letter

- Replaced the shell-script cp -al prototype with a native C implementation in builtin/worktree.c and builtin/submodule--helper.c.
- Per-worktree gitdir isolation is now handled by the existing submodule_name_to_gitdir() path; no extra plumbing is needed.
- Added t2405 tests verifying both behaviour and gitdir placement.

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3worktree: add --recurse-submodules flag to worktree addbuiltin/worktree.c+23 -00 replies
2/3worktree: add --recurse-submodules flag to worktree addbuiltin/submodule--helper.c+54 -01 reply
3/3worktree: add --recurse-submodules flag to worktree addt/t2405-worktree-submodule.sh+23 -10 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 0/3] worktree: add --recurse-submodules support to git worktree add

This series implements the native --recurse-submodules flag for "git worktree add" discussed in the earlier RFC thread.

The approach follows Phillip Wood's and Junio's feedback: each linked worktree gets its own per-worktree submodule gitdir under $GIT_COMMON_DIR/worktrees/<id>/modules/<name>/, so HEAD, refs, and the index are independent per worktree while pack files and loose objects are shared via hardlinks. The gitdir isolation is the same model git worktree already uses for the superproject.

Patch 1 adds the --recurse-submodules flag to builtin/worktree.c and calls "git submodule update --init --recursive" from within the new worktree after checkout.

Patch 2 teaches clone_submodule() in builtin/submodule--helper.c to detect when the main worktree already has the submodule cloned and reuse it via "git clone --local --no-checkout --separate-git-dir" instead of fetching from the remote URL. This avoids redundant network access and disk use: the objects are already present locally.

Patch 3 adds tests to t2405-worktree-submodule.sh covering both the happy path and the gitdir-isolation invariant.

Cleanup is automatic: submodule gitdirs under worktrees/<id>/modules/ are removed when "git worktree remove" calls remove_dir_recursively() on the worktree entry, exactly as with the superproject's per-worktree state.

Changes since the RFC:
- Replaced the shell-script cp -al prototype with a native C implementation in builtin/worktree.c and builtin/submodule--helper.c.
- Per-worktree gitdir isolation is now handled by the existing submodule_name_to_gitdir() path; no extra plumbing is needed.
- Added t2405 tests verifying both behaviour and gitdir placement.

Jimmy Aguilar Mena (3):
worktree: add --recurse-submodules flag to worktree add
submodule--helper: reuse main-worktree gitdir in linked worktrees
t2405: add tests for worktree add --recurse-submodules

 builtin/submodule--helper.c   | 54 +++++++++++++++++++++++++++++++++++
 builtin/worktree.c            | 23 +++++++++++++++
 t/t2405-worktree-submodule.sh | 24 +++++++++++++++-
 3 files changed, 100 insertions(+), 1 deletion(-)