From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245620; b=QSG7OWXzqTJtKIBG9KnGnV1shH4di6rN9ZlWGrRrzaTd00HItpLGJ7MAWexVa1Mt9GyU gSPFd6meni1pny6u2aEJenjUMmVEUfpU0QvU5A3X9vA2J6D/NeLRM5D5Cwg0XQf0b7W0P /uJfHi/+Hn7vEdleMmdP7fdlmntZxbj2Jxm5jYloqxCJT3v54E+ubLnKoSeFooKPEZswy Sqrh/pk2/9yokrg5Dzpu2UTIOQg117Y/dlnK2/abnPWMTzweduvIstEm1JnOEFOxn6kEr ZWtKCDevdTjYhfyf4ARX+aeBBGe0uLbmSfSAOBA5LzZFEAgeKDAOhoPGIG21Sqnu7qg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245620; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:Message-Id:In-Reply-To: References:From:Date:Subject:Fcc:Content-Type: Content-Transfer-Encoding:MIME-Version:To:Cc; bh=ICvDzvc/xNV3HYr10UNVmxBDKK8ySTisUsfE2828aO4=; b=sOPWGIkf/ndkXGCBWf+vivPxwjfDuZ99Jy3M8l4L4elAky79HSKwSuoqdbTj5bBFlF48 rLCc4lgWW+ne/o8ZQsgY5VwVYtVDzJrRfPFnl8ivri4I5IlWJl7voBkkFuAVigTTp6/Ho 5M7QhL9A3u3wYKKbWnHj97z5sbMRPyO0RFnIqAKUpRZQr2WCxNZ2TsbOVwvz22WURRMEP qDDFsUJjwmGAmqyVYeCvHVpuhUV/8llfI5FBGM5hcO4xIOpf88xep3dZOi/EjQVX4RVWX u4yx7CIF1Q3GtbPEMtd50QJTslZ0+tooS49PMSS/svCE6LroktPOvtpJOwP7tqEzJfg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=H6yS+0Z0; arc=none smtp.remote-ip=74.125.82.179 Received: from mail-dy1-f179.google.com (mail-dy1-f179.google.com [74.125.82.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 32FDB606A32F for ; Wed, 2 Dec 2026 21:06:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 32FDB606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=H6yS+0Z0 Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-386d1859e1cso246167eec.0 for ; Wed, 02 Dec 2026 13:06:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796245619; x=1796850419; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=ICvDzvc/xNV3HYr10UNVmxBDKK8ySTisUsfE2828aO4=; b=H6yS+0Z03AOeUDR6V+V5IJIRZZ7QMmj7Now7/wIwyEyDH8V9QArlujWLvk2jnE9/kL rbcpmF5v3MvYMHISEhdA5zRqMTv4K1rWhFnq8KDdf0QSMaV/xvrybwkWsaL95gu9sa75 UscR4IMKzrKtWbhDSePTDD9t48dxP0a5r7zs4BGLvzk96RWMadLLJXQVcLvER7VcCkRX QuTrvnsFqgNYnanh3psURdKYcN08rCTEhxiumkzJC2Jb0KAAuBhrpJDNaroYPT+8+CcG i7ubmd5ZtfrYMIwMiglqKTwiKB0im5wOx5klgH0U0PIYLVPAF6I4z8aQfPNaSfG0Ns7U 2Igw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245619; x=1796850419; h=cc:to:mime-version:content-transfer-encoding:content-type:fcc :subject:date:from:references:in-reply-to:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ICvDzvc/xNV3HYr10UNVmxBDKK8ySTisUsfE2828aO4=; b=ZRuUGVXBfpG6lL43cqabbZFKG4xOEslN950e1DxkcNvKr4q1/yXQWiTWVTeFU2jBBD o1x5y3+fqb38DcIUxeBGsscVd1o6h97S9n1CSC3u3ZHfDp5UQ53bLP1bsFzAIBbn0hh2 41GycF9Ij4ZeO7Hs4n6PWx0TbCaT9eWTPeixcG7jTmMVx4IetNWFp8CQZEjHv+T2g7Wj 53XTzhaaXez10F6OdXZvwGPzvnWnhA8R47XFqGRr559fcqdgUbdYEUeEJtNBNfo8MNJG 3PavdEMZQwr9P1+P5roIs2+RaRbL7Ix1pzMqHP7k8q9dRhzIzNG1yIcUF1zz0BCaI4a5 AWKg== X-Gm-Message-State: APuU4Z+SI1PU6jFzv6rFeg6cjyM06Q4LsDepz2xSapbAdv3WXg2wjzGK tyOQGecB02L+fn/pGq5hy/xyOrQV+/c+H9JSby9Z06Oq1WZeAtqgAF1asZPmG06i X-Gm-Gg: AZFdTZffjp1NKLX08fU0TYbY2HBCVMDa+/GTjR8aTzt6NEajneO6sK9VVZ+224isvy8 uWXy60gG10bJxc161ucVmFRdhw8L2CegRYDJGU9pg0aajdnw8EEJoTUz6nmixiRaB1YPeUqRPAK xpX+QWdgYQbGEWX4s1H4ifyJ6HOlW8cK9v2tG/itzZj2NQrSFl0IafL9ej+db3s9CNmmczKDxCa rS1qabE9cIDP/dvFiTUVdUiGfirSAwT8qZ919LiCD76YMi8yJQQJlJJd9AZEpvm54hoYmZxe08e iZgW4zNtVIpbei8qLqdCSVR4ZyNXKal/xFAi/CYvQFNJ7x7PiQ/ALZTsObZygrtHjDgdtA0JIp/ kqlkMqT8f4479g5RDF/dQpXZMkb07Byg+Kau3WgQVeIdBkNQsKQv/mQ2MMmrOcOWeuxsGIccVX9 dOpQEGeuXWswUMmkfA3uMzsuhm3ysjkcZb7HjX266UFqMeXcR5TFHHKpSwYAwEcFRRy1R+BH+6s UpvND/forusa4vslQcCRPyxY834M7l6GETCR4wu/7VdmIDkeVG5ORba8G5uUAIiCR825wn0X7BB jXTqG/L2EyyL5kaIQhA3CyFYYdfQtfA= X-Received: by 2002:a05:7301:6782:b0:385:2ea8:aac7 with SMTP id 5a478bee46e88-3852ea8d7d4mr56505855eec.23.1796245618863; Wed, 02 Dec 2026 13:06:58 -0800 (PST) Received: from [127.0.0.1] ([20.171.22.246]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38567cafec4sm133788772eec.26.2026.12.02.13.06.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 13:06:58 -0800 (PST) Message-Id: In-Reply-To: References: From: "Yoichi NAKAYAMA via GitGitGadget" Date: Wed, 02 Dec 2026 21:06:52 +0000 Subject: [PATCH v4 4/4] submodule init: copy submodule..gitdir to config.worktree Fcc: Sent Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Mailing-List: git@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 To: git@vger.kernel.org Cc: Yoichi NAKAYAMA , Yoichi NAKAYAMA , Yoichi NAKAYAMA From: Yoichi NAKAYAMA When extensions.worktreeConfig is enabled, an existing `submodule..gitdir` setting in the repository config may have been written by 'git submodule init' in another worktree. Reusing that setting would cause the current worktree to use the submodule gitdir of the other worktree. 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. Signed-off-by: Yoichi NAKAYAMA --- Documentation/config/submodule.adoc | 5 +- builtin/submodule--helper.c | 113 ++++++++++++++++++--- t/t7425-submodule-gitdir-path-extension.sh | 102 ++++++++++++++++++- 3 files changed, 204 insertions(+), 16 deletions(-) diff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc index 6931888b71..e6b2bb4d3f 100644 --- a/Documentation/config/submodule.adoc +++ b/Documentation/config/submodule.adoc @@ -68,8 +68,9 @@ submodule..gitdir:: has no effect. When enabled, this config becomes the single source of truth for submodule gitdir paths and Git will error if it is missing. When `extensions.worktreeConfig` is enabled, 'git submodule init' stores - this setting in each worktree's config. See linkgit:git-config[1] for - details. + this setting in each worktree's config. If this setting already exists + in the repository config, copy the value only if it belongs to the + current worktree. See linkgit:git-config[1] for details. submodule.active:: A repeated field which contains a pathspec used to match against a diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 34783c71d1..66b30c7a39 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -480,10 +480,44 @@ struct init_cb { }; #define INIT_CB_INIT { 0 } +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_realpath_forgiving(&submodule, submodule.buf, 0)) { + strbuf_release(&submodule); + return false; + } + + super = real_pathdup(repo_get_git_dir(the_repository), 1); + matches = starts_with(relative_path(submodule.buf, super, &tmp), + "modules/"); + + strbuf_release(&submodule); + strbuf_release(&tmp); + free(super); + return matches; +} + +static const char submodule_mismatch_warning[] = N_( +"config '%s' exists but was not copied to the worktree config because " +"it does not point to a gitdir for the current worktree." +); + static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, - const char *submodule_name) + const char *submodule_name, + struct worktree *wt) { - const char *value; + char *value = NULL; char *key; if (validate_submodule_git_dir(gitdir_path->buf, submodule_name)) @@ -491,18 +525,65 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, key = xstrfmt("submodule.%s.gitdir", submodule_name); - /* Nothing to do if the config already exists. */ - if (!repo_config_get_string_tmp(the_repository, key, &value)) { - free(key); - return 0; + if (the_repository->repository_format_worktree_config) { + struct config_set cs = { 0 }; + const char *filename = worktree_git_path(wt, "config.worktree"); + char *tmp; + + git_configset_init(&cs); + git_configset_add_file(&cs, filename); + + /* + * Nothing to do if the config already exists in the config set + * for the current worktree. + */ + if (!git_configset_get(&cs, key)) { + git_configset_clear(&cs); + free(key); + return 0; + } + + git_configset_clear(&cs); + + /* + * If the worktree config does not have this key, check the + * effective value from lower-priority config scopes and copy + * it to the worktree config if it belongs to the current + * worktree. + */ + if (!repo_config_get_string(the_repository, key, &tmp)) { + bool matches = + submodule_git_dir_matches_current_worktree(tmp); + + if (!matches || + validate_submodule_git_dir(tmp, submodule_name)) { + if (!matches) + warning(_(submodule_mismatch_warning), + key); + free(tmp); + } else { + value = tmp; + } + } + } else { + const char *value_; + + /* Nothing to do if the config already exists. */ + if (!repo_config_get_string_tmp(the_repository, key, &value_)) { + free(key); + return 0; + } } - if (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) { + if (repo_config_set_worktree_gently(the_repository, key, + value ? value : gitdir_path->buf)) { free(key); + free(value); return -1; } free(key); + free(value); return 0; } @@ -526,7 +607,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr(&gitdir_path, submodule_name); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ @@ -534,7 +616,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */ @@ -542,7 +625,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 2.3: Try some derived gitdir names, see if one sticks */ @@ -552,7 +636,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved); strbuf_addch(&gitdir_path, c); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; strbuf_reset(&gitdir_path); @@ -560,7 +645,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved); strbuf_addch(&gitdir_path, c); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; } @@ -577,7 +663,8 @@ static void create_default_gitdir_config(const char *submodule_name) strbuf_addstr(&gitdir_path, modules_dir); strbuf_addstr(&gitdir_path, hex_name_hash); if (!validate_and_set_submodule_gitdir(&gitdir_path, - submodule_name)) + submodule_name, + wt)) goto cleanup; /* Case 3: nothing worked, error out */ diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh index 590febcafd..854ddb9edd 100755 --- a/t/t7425-submodule-gitdir-path-extension.sh +++ b/t/t7425-submodule-gitdir-path-extension.sh @@ -338,7 +338,49 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo ) ' -test_expect_success 'git submodule init make gitdir relative' ' +test_expect_success 'git submodule init inherits related repository config for main' ' + test_when_finished "rm -rf repo-clone-init-main" && + git clone upstream repo-clone-init-main && + ( + cd repo-clone-init-main && + git config core.repositoryformatversion 1 && + git config extensions.submodulePathConfig true && + git submodule init && + git config submodule.sub1.gitdir >actual && + echo ".git/modules/sub1" >expect && + test_cmp expect actual && + git config submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && + test_cmp expect actual && + + # Modify values so that they can be distinguished from the default + git config submodule.sub1.gitdir .git/modules/sub1_custom && + git config submodule.sub2.gitdir .git/modules/sub2_custom && + + git config extensions.worktreeConfig true && + git submodule init 2>err && + test_must_be_empty err && + git config --worktree submodule.sub1.gitdir >actual && + echo ".git/modules/sub1_custom" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo ".git/modules/sub2_custom" >expect && + test_cmp expect actual && + + git worktree add wt && + cd wt && + git submodule init 2>err && + test_grep "does not point to a gitdir for the current worktree" err && + git config --worktree submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual + ) +' + +test_expect_success 'git submodule init inherits related repository config for worktree' ' test_when_finished "rm -rf repo-clone-init-wt" && git clone upstream repo-clone-init-wt && ( @@ -353,6 +395,64 @@ test_expect_success 'git submodule init make gitdir relative' ' test_cmp expect actual && git config submodule.sub2.gitdir >actual && echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual && + + # Modify values so that they can be distinguished from the default + git config submodule.sub1.gitdir ../.git/worktrees/wt/modules/sub1_custom && + # Use an absolute path to simulate a value written by older Git + git config submodule.sub2.gitdir "$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom" && + + git config extensions.worktreeConfig true && + git submodule init 2>err && + test_must_be_empty err && + git config --worktree submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1_custom" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo "$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom" >expect && + test_cmp expect actual && + + cd .. && + git submodule init 2>err && + test_grep "does not point to a gitdir for the current worktree" err && + git config --worktree submodule.sub1.gitdir >actual && + echo ".git/modules/sub1" >expect && + test_cmp expect actual && + git config --worktree submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && + test_cmp expect actual + ) +' + +test_expect_success SYMLINKS 'git submodule init inherits related repository config by resolving symlinks' ' + test_when_finished "rm -rf repo-clone-init-symlink" && + git clone upstream repo-clone-init-symlink && + ( + cd repo-clone-init-symlink && + git config core.repositoryformatversion 1 && + git config extensions.submodulePathConfig true && + git submodule init && + git config submodule.sub1.gitdir >actual && + echo ".git/modules/sub1" >expect && + test_cmp expect actual && + git config submodule.sub2.gitdir >actual && + echo ".git/modules/sub2" >expect && + test_cmp expect actual && + + ln -s ../repo-clone-init-symlink link && + git config submodule.sub2.gitdir "$(pwd -P)/link/.git/modules/sub2" && + git config extensions.worktreeConfig true && + + GIT_DIR=link/.git git submodule init sub1 2>err && + test_must_be_empty err && + git config --worktree submodule.sub1.gitdir >actual && + git config --local submodule.sub1.gitdir >expect && + test_cmp expect actual && + + git submodule init sub2 2>err && + test_must_be_empty err && + git config --worktree submodule.sub2.gitdir >actual && + git config --local submodule.sub2.gitdir >expect && test_cmp expect actual ) ' -- gitgitgadget