From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795903566; b=vheZlB6PXXyWmBiuiPCZdqG3vodXmGxnXQ6XG586dhnz9UVjWL7hx5rGazHelYfR+Hsk B+C3RpnnLKpfhPZi4qAb1h+vz4ZmISNYmZPY7hxszMBdLpJKWr7HmaCZ7SXHQjb1gOFag pUz/1ZltzjOd92eu3NBz/RhcQ0H1g6y2+61xWIkJ7SsM6xBj89kEjjJq67BZmlICe7cys PAlpr48YDgJjTEo+GEHYnYV/krW+Wsg7kpKRfJqvzYToHsaBfyD0CQ5D2INT1khAQKbz1 mamJtEAOfwFP58WTyW1ehzVHgZdNa1gfU5N3k1mzbhy94t+EBWrDIGEaQKSco+2Z8LA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795903566; 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=grZpi3U0B5PIYmW4dYBcodv6GkUL2Zfugo/G9xo6LlA=; b=4tF96JTgLcmRu/CMnvaPThoSJxv4SUXadDZetON5jQFAJXSNx6sjCALXZukahb7CRQfE 35zEu4kPV4G5CupAAM/L0Updk4pjLkkb3o+Nu8O0iWnGLBeSlDKr27IaPsdeCMZ2eZIcf AqfZ6mFLReGkKA7N+yhyoAOQfZn2m3mfLLFeqp3JM9IfoF1Qd2NK15GTOGz2SybtFPZVn eEbcQNZ1PbenUG/Lxb+iDW7kNESsoFzG2S9cYO6KqsN1+WLp00vD1w7DVxvK+jo6XsiAV 6kbX96aGTL4Jr3upC8dpahowynDe5Oq/lmE8cL+0geejsvB+J/n4SwcaUcygNsU9qtA== 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=J4s8l6wI; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (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 06D68606A329 for ; Sat, 28 Nov 2026 22:06:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 06D68606A329 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=J4s8l6wI Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-386d1859e1cso3404738eec.0 for ; Sat, 28 Nov 2026 14:06:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795903565; x=1796508365; 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=grZpi3U0B5PIYmW4dYBcodv6GkUL2Zfugo/G9xo6LlA=; b=J4s8l6wIHNVxGoYk0bEk7d2N8StgCZPN8wGVNYYcYa5Odedgh2DUsNKTG+ExZQCZWg 3R2j7p54q2EPlylDk9jGk6e/G3qvUGQb7HBh6AM8XVv7FFcEp11jYpL4Rn2QCUNUp5p5 MdKScsaBYKMIJDahLN3gI8MtSNMYFUIxOaix9YIKaJ9fnxKhmgaKZ6pVcEjWnEBhikQE db47TlBQGAxL3/oTrYECBA/PgFB9eLkdorRF9cU6DTgYI3x8c1t6w5lOdK8N2QbQ7vuW G4KFQG/jsPmJp6c+j5cByeU1ZfBbVZBZ2PlKRLiJA4MuzfMG/o+8lwp/tm/+106bvxAX YwDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795903565; x=1796508365; 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=grZpi3U0B5PIYmW4dYBcodv6GkUL2Zfugo/G9xo6LlA=; b=EACAJwvSPmDIOmc8+9fl9KcJzNxN0zzqApvagFQDzeExTBzg9BaZCbTaVtT/ODA/zf PARNZe4HFEyYf65Z7UtC/fslSUZDLMsOfmZ530wd+/vvxP+JU0D8uV5ydkIF292NRbs7 FnAg6sXvXXrD0ccwBc0plR9k1vtjkpW/EMKdMWxYe/EbkLwLARWg7aUSX7d5AvKVqe6n s6UhlZs2YDq9rZZNehxCH00FGMxz45D71RWAZQlmmxH5i3w7MvZUuzPDVP0kcOllcUMw VjxLlk6mh92znu8dwjonTW0kQyEHaCcNoiLpQx4pBoj2T4XgqAhT0T0m3hd6FvD7MUH5 6tFA== X-Gm-Message-State: APuU4Z8ZRE4Yqn1/r5h389Gi788HQyDI7Gm3KkB+xU2tU1k9numyK69y 5GUaYMkYVqeW0G8O3xglFS2S6AXCLrd9P3mCrtEfmos2VR5yw4AR75H0stICR6vc X-Gm-Gg: AZFdTZdizHGDvKmTecMpEQFkoPO3d3elIHbRLe/yhuVD+ZdUPcRdNcvaSgJv5HlZkHX IYGc+9u5k3bCLra1n9sGEco985pOkM1+fVH1TLZpu7D1rEG50JTC4VqK+fAUTrDRdU2aG8TOe0h UqMDhiewYFPGCwgAEfYIoEpWrUhfV5IwiDu8eDyqrZij2P8NylL90RGTIxXpZMJgQ5UxSg2c/4X yj3/2q2RzlUKzh0lFy1JPgNWAZzwdPYLlH/xED4yLkGAV5mYshX7yd78g2eehPySc34WR+9dmo2 Cd7b6TtNmRF0dfKWqiAuu4xMmLmAOXxIS1Z13V76hawNZbTinxeNcZu738WhKECUJcndxDwrQrU nFAvaXkuHOplXmTXRv7dLLBsP+kjb56BB7aC9vj3wg3gawGawqdBqmJAyhxpcXxvVZXGJvWyw6j jvNWTPAwqpKS6mYa2JUBeL56MrhQUX6bU9vkLGOLW1WTqF3fjQYUZFP8HLtWMUYazwnLU+gWHid nyR0qqIm9COgYsSC22Poj0mW6ch1uOO9DVssxsCJYF2H8A/pCgTG358wOh/cuX+pPD1jXtOliIY KoVSSqEdrpDMZqgf1c7SQsP0Kxdiy1gAkQ== X-Received: by 2002:a05:693c:25cc:b0:369:f17d:efb with SMTP id 5a478bee46e88-384b3e77699mr40500895eec.10.1795903564815; Sat, 28 Nov 2026 14:06:04 -0800 (PST) Received: from [127.0.0.1] ([52.160.138.198]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38566cd40c5sm103661647eec.10.2026.11.28.14.06.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 14:06:04 -0800 (PST) Message-Id: <3e2c1612988ea1bc74b94a2cfc17e24dfd41ca3e.1795903557.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Yoichi NAKAYAMA via GitGitGadget" Date: Sat, 28 Nov 2026 22:05:57 +0000 Subject: [PATCH v3 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 | 69 ++++++++++++- 3 files changed, 171 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..8e2dfe204c 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -480,10 +480,45 @@ 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_normalize_path(&submodule)) { + 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. If you want " +"to keep using this setting, copy it to the worktree config." +); + 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 +526,64 @@ 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..06d67c654e 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,31 @@ 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 ) ' -- gitgitgadget