worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

[Discussion] Recommended way to handle many submodules in worktrunk worktrees? Considering nested git worktrees

Open est7 opened this issue 6 months ago • 3 comments

Context

We use worktrunk on a large Gradle-based project with 25 git submodules. ~9 are libraries we maintain (read access); ~14 are private to other teams and we substitute prebuilt artifacts via Gradle's dependencySubstitution. The standard flow — wt switch --create followed by git submodule update --init — takes several minutes per new worktree because each submodule is re-cloned from the remote.

Pattern we're considering

Instead of re-initializing, use nested git worktree add: for each permitted submodule, run git worktree add $WT/$sub <commit> from inside $BASE/$sub. This produces a second worktree of the submodule, rooted at the parent worktree's submodule path — same mechanism that makes parent-repo worktrees instant. No network, no object duplication.

Start commit defaults to the parent worktree's index entry for that submodule (git rev-parse HEAD:$sub), which keeps parent's git status clean.

Rough numbers from a one-off test

Standard submodule update --init Nested worktree
9-submodule init minutes (network-bound) ~2s
Extra disk for .git data several GB ~0 (gitfiles only)
Full Gradle build + install works works

Tradeoffs we've already spotted

  • wt remove needs --force because detaching the nested submodule worktrees first leaves parent's git status with D entries.
  • git submodule deinit / git submodule absorbgitdirs become dangerous in this workflow — they would manipulate the shared git dir that base and all worktrees rely on.

What we'd like input on

Before committing to this pattern, we'd appreciate hearing:

  1. Has anyone shipped nested-worktree-as-submodule in a real workflow? Any non-obvious failure modes (git upgrades, gc/prune on shared git dirs, IDE integrations)?
  2. Is there a reason the worktrunk team has steered away from documenting this? Closed issues #1194 / #1196 / #777 / #1069 all focus on standard git submodule semantics; we couldn't find any nested-worktree mention.
  3. What's the recommended way to handle many submodules in worktrunk worktrees today? If the standard update --init flow is considered acceptable despite the speed cost, we'd like to hear why — there may be safety properties we're under-valuing.

Mostly looking for guidance from anyone who's been here before. Happy to go with the standard approach if there's a strong reason to.

est7 avatar May 27 '26 02:05 est7

Thanks for the detailed write-up — this is a usage/design question rather than a bug report, so leaving the substantive call to a maintainer.

What I could verify from the codebase: worktrunk's submodule handling today is mostly defensive — Worktree::has_initialized_submodules (see src/git/repository/working_tree.rs) is used to force git worktree remove --force when needed (#1196), and wt push/wt merge pin --recurse-submodules=no to avoid the failure mode in #1604. I didn't find any built-in for initializing submodules in a freshly-created worktree, nor any documented "recommended pattern" for many-submodule workflows in docs/content/tips-patterns.md or the FAQ.

So as far as worktrunk goes today, post-create submodule setup is left to the user — a post-switch hook running whatever git submodule update/git worktree add recipe fits the repo is the natural place for it. Whether nested git worktree add is safe to recommend as the pattern is the part worth a maintainer's judgment.

A maintainer will take a closer look.

worktrunk-bot avatar May 27 '26 03:05 worktrunk-bot

Interesting case @est7 !

I admittedly don't personally use submodules, and so the submodule features I've added have been at the request of others.

Your approach sounds great tbh. I'll let others contribute if they have ideas

I guess this should be possible to define in a script, and so a hook or alias, if you wanted to have a go implementing that and see how it does, that could be a good way of exploring the space (I think it would be premautre to start by implementing in worktrunk until someone had some experience with the pattern, but I'd be happy to implement something like this if we're confident it's a good pattern...)

max-sixty avatar May 27 '26 16:05 max-sixty

I love the "worktree as submodule" idea, but my guess is that implementing it would be pretty tricky. I think all the git submodule commands expect to have real git repos, not worktrees. I'm not sure that an external tool like wt can help there.

A midway improvement would be to copy the .git folder of each submodule from the "root" worktree into the checkout. Not instantaneous, but a lot faster than using the network. Possible that a combination of symbolic linking some directories while copying others could make it almost as fast as a "real" worktree.

nedtwigg avatar May 29 '26 15:05 nedtwigg