Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org,  Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>
Subject: Re: [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree
Date: Tue, 24 Nov 2026 12:44:02 -0800	[thread overview]
Message-ID: <xmqqo6benenh.fsf@gitster.g> (raw)
In-Reply-To: <b5d203d0147715dc2b38f13be427ef8d2138321d.1795273853.git.gitgitgadget@gmail.com> (Yoichi NAKAYAMA via GitGitGadget's message of "Sat, 21 Nov 2026 15:10:53 +0000")

"Yoichi NAKAYAMA via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Make 'git submodule init' copy an existing setting to the worktree
> config only if it belongs to the current worktree; otherwise, generate
> a new setting.
>
> Warn when an existing setting does not match the current worktree and
> is not copied to the worktree config.

This makes the transition to extensions.worktreeConfig much safer for
existing repositories, and the new warning plus test cases covering both
main-worktree and linked-worktree migration look solid.

> +static bool submodule_git_dir_matches_current_worktree(const char *gitdir)
> +{
> +	struct strbuf tmp = STRBUF_INIT;
> +	struct strbuf submodule = STRBUF_INIT;
> +	char *super;
> +	bool matches;
> +
> +	if (is_absolute_path(gitdir))
> +		strbuf_addstr(&submodule, gitdir);
> +	else if (!repo_worktree_path_append(the_repository, &submodule,
> +					    "%s", gitdir))
> +		return false;
> +
> +	if (strbuf_normalize_path(&submodule)) {
> +		strbuf_release(&submodule);
> +		return false;
> +	}
> +
> +	super = absolute_pathdup(repo_get_git_dir(the_repository));
> +	matches = starts_with(relative_path(submodule.buf, super, &tmp),
> +			      "modules/");

Isn't there a subtle symlink / normalization mismatch here?

repo_worktree_path_append() uses 'the_repository->worktree', which is
canonicalized via real_pathdup() during repository setup.  In contrast,
absolute_pathdup(repo_get_git_dir(the_repository)) uses 'PWD' (if
matching 'getcwd()') without resolving symlinks when the gitdir is
relative, and copies an explicit absolute GIT_DIR verbatim without
resolving symlinks or normalizing '.' / '..'.

While 'git submodule' normally runs cd_to_toplevel() (resetting PWD)
when GIT_DIR is unset, running 'GIT_DIR=.git git submodule init' from a
symlinked path to the worktree (or 'GIT_DIR=.git/../.git', or invoking
'git submodule--helper init' directly via a symlink) causes 'super' and
'submodule' to diverge, triggering a false mismatch warning and
generating a new gitdir instead of copying the existing one, no?

Using real_pathdup(repo_get_git_dir(the_repository), 1) for 'super' (and
strbuf_realpath_forgiving() for 'submodule' in case an existing absolute
'gitdir' config contains symlinks) keeps both sides canonicalized, which
might make it safer (though the resulting path may become uglier, as it
often is the reason why people use symlinks that are more comfortable to
the eyes and "cd" to a directory through them).

  reply	other threads:[~2026-11-24 20:44 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-12 12:03 [PATCH 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-14 15:27   ` Yoichi NAKAYAMA
2026-11-14 15:47     ` Yoichi NAKAYAMA
2026-11-12 12:03 ` [PATCH 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-12 12:03 ` [PATCH 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10 ` [PATCH v2 0/3] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-21 15:10   ` [PATCH v2 1/3] submodule: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-24 19:44     ` Junio C Hamano
2026-11-27  5:04       ` Yoichi NAKAYAMA
2026-11-27  5:23         ` Junio C Hamano
2026-11-27  6:15           ` Yoichi NAKAYAMA
2026-11-21 15:10   ` [PATCH v2 2/3] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:14     ` Junio C Hamano
2026-11-27 12:35       ` Yoichi NAKAYAMA
2026-11-21 15:10   ` [PATCH v2 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-24 20:44     ` Junio C Hamano [this message]
2026-11-28  0:14       ` Yoichi NAKAYAMA
2026-11-28 22:05 ` [PATCH v3 0/4] submodule: support worktree-specific configuration Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 1/4] submodule init: make submodule.<name>.gitdir relative for worktrees Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 2/4] submodule init: resolve symlinks in submodule.<name>.gitdir Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 3/4] submodule: store settings in worktree config when enabled Yoichi NAKAYAMA via GitGitGadget
2026-11-28 22:05   ` [PATCH v3 4/4] submodule init: copy submodule.<name>.gitdir to config.worktree Yoichi NAKAYAMA via GitGitGadget
2026-11-29  8:32     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqqo6benenh.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=yoichi.nakayama@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox