Re: [PATCH 3/3] submodule: accept a linked worktree as the submodule's git dir
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 18, 2026, 19:52 UTC
- Message-ID
- <xmqqv75t534o.fsf@gitster.g>
- In-Reply-To
- <20261118163249.1693361-4-adrian.ratiu@collabora.com>
Adrian Ratiu <adrian.ratiu@collabora.com> writes:
Show 9 quoted lines
> A submodule's working tree can be a linked worktree of the submodule's > repository, created e.g. with: > > git -C super/sub worktree add ../../wt/sub > > Its .git file then points to "super/.git/modules/sub/worktrees/<id>" > instead of "super/.git/modules/sub". t2405 already creates such a > submodule, but it doesn't test a recursive checkout, and its worktree > id happens to match the submodule name.
It is tempting to say "don't do it then", but if we can cater to the use case that is great ;-)
Show 20 quoted lines
> @@ -2132,6 +2171,7 @@ int submodule_move_head(const char *path, const char *super_prefix,
> struct child_process cp = CHILD_PROCESS_INIT;
> const struct submodule *sub;
> int *error_code_ptr, error_code;
> + int is_worktree = 0;
>
> if (!is_submodule_active(the_repository, path))
> return 0;
> @@ -2170,8 +2210,9 @@ int submodule_move_head(const char *path, const char *super_prefix,
> char *git_dir = xstrdup(read_gitfile(dotgit));
>
> free(dotgit);
> - if (validate_submodule_git_dir(git_dir,
> - sub->name) < 0) {
> + is_worktree = validate_submodule_gitfile_dir(git_dir,
> + sub->name);
> + if (is_worktree < 0) {
> if (!the_repository->repository_format_submodule_path_cfg)
> advise(_("enabling extensions.submodulePathConfig "
> "might fix the following error."));Earlier near the beginning of the function we initialized is_worktree to 0 and then only when submodule_uses_gitfile() is true, we update it with validate_submodule_gitfile_dir().
Show 12 quoted lines
> @@ -2191,7 +2232,13 @@ int submodule_move_head(const char *path, const char *super_prefix,
> submodule_reset_index(path, super_prefix);
> }
>
> - if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) {
> + /*
> + * Do not reconnect a linked worktree to the submodule's
> + * main git dir: that would take over the git dir of another
> + * working tree.
> + */
> + if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE) &&
> + !is_worktree) {If a linked-worktree submodule 'sub' contains a nested submodule 'inner' whose .git is a directory, submodule_uses_gitfile("sub") would have returned 0 (because the check is recursive), we ran absorb_git_dir_into_superproject() (which is also recursive so it would cause .git in 'inner' to be absorbed), and is_worktree is still 0 at this point, which means if() condition here is satisfied (if forced) and we will enter this block. Would it cause an issue?
I am wondering if the check for is_worktree should be moved outside the else clause, i.e. making the overall flow look more like
if (old_head) {
if (!submodule_uses_gitfile(path))
absorb_git_dir_into_superproject(path, super_prefix); /*
* Now path/.git is guaranteed to be a gitfile;
* validate and detect worktrees.
*/
...
is_worktree = validate_submodule_gitfile_dir(git_dir, sub->name);
...
}Or am I grossly misreading the code flow?
Thanks.