From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795903561; b=lYzROnvHZBcVDbGDHhkTUeXhpqfuJTXKILAyfldF3cZKa6HftIl5o+upLeDV6650QGGr uKq5S7Rt7FaDPr6S5nxhVrXa0+78P5CTMJHYoAJaeuJf2a1gioiS/T+05qWLxWBAvC+fT 34NC/jnWh4ZJ9j/J3BZr+G4EQ3CU0xq/uaGbPeGJJlTRzZa02WLs9Mz2SB7PQox/oG9x9 SGiIfeRFUqE9J7OifincLYSZ9arsQxJv7Nadq7qx6tx9fTy1ZjEJjB0zSB90EmKhtWGrg zFB7+qCnVZgrRx/5kpbNanB8/C2QDXIouxo7eTJ6c/7IzkFPhz/hsux/4l8rQxqkPGg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795903561; 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=Y2BgnUTbiYoXCwBBtyREDrFn+CW/eEzu5eGRM0ijmOQ=; b=s6FpcZ1+TBoQXKU2ncGQUVd2fgVAJZ2oE2fFYQxFdhYFr5Oonz71qVj5gz/uF/HbuFPZ UBqFTeiZHnL1LbPvUh9ijeTXFs6wZyeVQk21OWbXueZYQGCooB8R8yan1u3HoIi68w1ND erKW0Gx37JHG5KkGXWtVOR09MqdAxyt6l2Bnd1gRQEuO/JDYbjecc/kKLnxhX5AdfMOAP 9RY7IZ3q8fdr7J12USbJfxRCt/ipFqo6+U3AwPll1wSTZylH5DJ2rJBqKYZrBdGFucROn Axvahl1oVX6qSEeQO7CbtgBhehWd3eLVqK84BMvD6gCVIVzDMtDNx3VZhdkxkTh5k4A== 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=vddbLE+F; arc=none smtp.remote-ip=74.125.82.44 Received: from mail-dl1-f44.google.com (mail-dl1-f44.google.com [74.125.82.44]) (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 8940F606A321 for ; Sat, 28 Nov 2026 22:06:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8940F606A321 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=vddbLE+F Received: by mail-dl1-f44.google.com with SMTP id a92af1059eb24-1a2761c69fbso14248518c88.0 for ; Sat, 28 Nov 2026 14:06:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795903559; x=1796508359; 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=Y2BgnUTbiYoXCwBBtyREDrFn+CW/eEzu5eGRM0ijmOQ=; b=vddbLE+F4G+1FAseSumTZg5lfelf9aXscyctjhp6jWyJGehbleop7pSwoJ9fSotHEq NAZFBsfJjoe4KDwjd+5E+Ae0o0PKnsdM86GoYYIyxvOm8+Zj9W8gsdJN57Q+BWBTqHF9 FlK+wobYo70mMZCC545k/3IPbEGg6PfIMwW6HMBNBTO62Y79s6uYgcbNz98H6BO8Y0W1 xsGE11OLQtLm4Is3D7uFOZC9pZeHwsWSF5a7uC52rctHUr19hMWyE2Un8Nwp+ijTAIUD YCu0XSd6cyTMvcur4JOVWM1/8DAc7Fr4KpBTc+YFdwX6gnW8K7V0Lt3IPWcpv+GT/kF9 ageA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795903559; x=1796508359; 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=Y2BgnUTbiYoXCwBBtyREDrFn+CW/eEzu5eGRM0ijmOQ=; b=qeFITIBxza8t8XC0pc38zRjL+K35AFob6Evv1zA6bTTncDGzaqzw/OX9UAXFFYpuWj HyAYUzKrdHoXg1IB6J30L2bhqx6R7moGa9HJlaGJTZ0HVKeGJPjEv44VjNyRpK+ZZJtX jbpLp1Wg/Kg15sR96zdIq6gSsZMx9rftBL2WIIQ/i9DkFJW3UY355PIVlOKavkUFPdcZ Bj0bB6kZA2KbXrRR6cpaHl+8LKeggk8hhtMAB9XQqmP00l2a5UrtfweMgTFhCoLNyYZa XzzhFdFPqakZJv1uZXSyDaFWobTJmTXgk9rK0kS7P89CvhrgfR4pfM/BYRoICg/j+snC WZZA== X-Gm-Message-State: APuU4Z/EfUKIueljXEqDRPWBRJ37UaXOjjE30HPARTwopbEwUDgpgWxT W+3EOGDy/9R2PklBTG7VuwdXEExi8U0RpZHGAQcuRGLY4Fb5rMkrKNnJi0s7Jc+x X-Gm-Gg: AZFdTZd96+2/B8zoigOjnCzuR0DcTKC2lIEq0/cXz5NlgO/cHc2KAt8TfcGH/OMCZxu xtQ8qAkFvZis1z0fAuldietWdwvYYus9j8rkkeS/+rKYxcBvu723m5eILpL3z7znp5G+K0vHTYu B4mgbPXiBWPewf4bx3psRsPiMFFeCsMUsrpetHPR/gCygy1gwpo/kgh1p82vI15JM1wiqiARtx6 b/ddd85iXWH7TbvdEEK7+r2J5LEC7xMh/Uyy5t+23hG37u0AYnR+gpms2JUcutpUGAqy+uekP+j VZuUqVvGlNyNP88ZpLPVLr9yn05IkrB3tZFogn3bp9PpWJxpDVFCB3HUcpU0TCY+MgLtDW+xRaf ahAO0NtowdbVthMn/fFVG8qwa7iLkhvxUfszkshZW0bky8PNrA83btVJsahMiX/6uheMcYFEGAV E/HDZw7hzRkCT+F4Acox0dqN4fxK0LLtcgsYVQcoa9LK/8HGCj1WxAH4chUqiO3Am8+aspVlYQk bFYuxRbRj0qnEK6eKmTBXeQv96Mksavho/fyuP6EjTkg91bN3nghoUUItcNkyslsNr877tMBdWt L6n+t3Gse470y4RPdedy+Brfl9hbhHPdz84= X-Received: by 2002:a05:701b:4558:10b0:1b3:b4cf:8f18 with SMTP id a92af1059eb24-1b556926cfbmr29112197c88.41.1795903559219; Sat, 28 Nov 2026 14:05:59 -0800 (PST) Received: from [127.0.0.1] ([52.160.138.198]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b556829d31sm109743781c88.9.2026.11.28.14.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 14:05:58 -0800 (PST) Message-Id: In-Reply-To: References: From: "Yoichi NAKAYAMA via GitGitGadget" Date: Sat, 28 Nov 2026 22:05:53 +0000 Subject: [PATCH v3 0/4] submodule: support worktree-specific configuration 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 When using multiple worktrees, submodule settings stored in the repository config are shared between all worktrees, even when they need to differ. For example, different branches may use different URLs for the same submodule. More importantly, sharing submodule..gitdir causes submodule worktrees to share the same HEAD and index, even when the corresponding superproject worktrees record different commits for the submodule. This series makes submodule settings worktree-specific when extensions.worktreeConfig is enabled. It also keeps submodule..gitdir relative for linked worktrees, so it continues to work when the worktree is moved along with the main worktree. Included commits: * [1/4] Keep submodule..gitdir relative for linked worktrees. * [2/4] Resolve symlinks in submodule..gitdir. * [3/4] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled. * [4/4] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree. Changes in v3 ============= * [1/4] Copy the path early, as relative_path may continue to use the provided buffer * [2/4] Add new commit to resolve symlinks in submodule..gitdir value * [3/4] Handle duplicate submodule..gitdir on 'git worktree add' * [3/4] Add more explanations * [4/4] Resolve symlinks in submodule_git_dir_matches_current_worktree() Changes in v2 ============= * Update the cover letter to better explain the problems this series addresses. * [1/3] Copy the string returned by worktree_git_path() early. * [2/3] Expand the explanation in the commit message for why the gitdir config is excluded from worktree add. * [3/3] Explain the case where the repository config contains a gitdir setting for another worktree, and warn when the setting is not copied. Included commits: * [1/3] Keep submodule..gitdir relative for linked worktrees. * [2/3] Store submodule settings in the worktree config when extensions.worktreeConfig is enabled. * [3/3] Make git submodule init copy an existing gitdir setting to the current worktree config only when it belongs to that worktree. Yoichi NAKAYAMA (4): submodule init: make submodule..gitdir relative for worktrees submodule init: resolve symlinks in submodule..gitdir submodule: store settings in worktree config when enabled submodule init: copy submodule..gitdir to config.worktree Documentation/config/extensions.adoc | 9 + Documentation/config/submodule.adoc | 14 +- Documentation/gitsubmodules.adoc | 26 ++- builtin/submodule--helper.c | 211 ++++++++++++++++----- builtin/worktree.c | 23 +++ t/t2405-worktree-submodule.sh | 157 +++++++++++++++ t/t7425-submodule-gitdir-path-extension.sh | 147 +++++++++++++- 7 files changed, 530 insertions(+), 57 deletions(-) base-commit: acba8bf9753134d9bde89f94369f775838a8c5fe Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-2306%2Fyoichi%2Fsubmodule-with-worktree-v3 Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2306/yoichi/submodule-with-worktree-v3 Pull-Request: https://github.com/gitgitgadget/git/pull/2306 Range-diff vs v2: 1: 435ca42ec9 ! 1: 6ed1297d8f submodule: make submodule..gitdir relative for worktrees @@ Metadata Author: Yoichi NAKAYAMA ## Commit message ## - submodule: make submodule..gitdir relative for worktrees + submodule init: make submodule..gitdir relative for worktrees - For a linked worktree, always keep `submodule..gitdir` relative - to ensure it continues to work when the worktree is moved along with - the main worktree. (This benefit also requires - worktree.useRelativePaths to be set to true.) + Currently, repo_git_path_append() can produce an absolute path when + running in a linked worktree or when GIT_DIR is set to an absolute + path. As a result, `submodule..gitdir` can be stored as an + absolute path. + + Always store `submodule..gitdir` as a relative path to ensure + that it continues to work when the worktree is moved along with the + main worktree. The worktree must also use relative paths for this + benefit, which requires `worktree.useRelativePaths` to be set to true. Signed-off-by: Yoichi NAKAYAMA @@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct static void create_default_gitdir_config(const char *submodule_name) { + struct worktree *wt = get_current_worktree(the_repository); -+ const char *modules_dir; ++ const char *raw_modules_dir; + struct strbuf tmp = STRBUF_INIT; ++ char *modules_dir; struct strbuf gitdir_path = STRBUF_INIT; struct git_hash_ctx ctx; char hex_name_hash[GIT_MAX_HEXSZ + 1], header[128]; unsigned char raw_name_hash[GIT_MAX_RAWSZ]; int header_len; -- /* Case 1: try the plain module name */ ++ raw_modules_dir = worktree_git_path(wt, "modules/"); ++ modules_dir = xstrdup(is_absolute_path(raw_modules_dir) ? ++ relative_path(raw_modules_dir, wt->path, &tmp) : ++ raw_modules_dir); ++ strbuf_release(&tmp); ++ + /* Case 1: try the plain module name */ - repo_git_path_append(the_repository, &gitdir_path, "modules/%s", submodule_name); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) { - strbuf_release(&gitdir_path); - return; -+ modules_dir = worktree_git_path(wt, "modules/"); -+ if (is_absolute_path(modules_dir)) { -+ modules_dir = relative_path(modules_dir, wt->path, &tmp); -+ } else { -+ strbuf_addstr(&tmp, modules_dir); -+ modules_dir = tmp.buf; - } - -+ /* Case 1: try the plain module name */ +- } + strbuf_addstr(&gitdir_path, modules_dir); + strbuf_addstr(&gitdir_path, submodule_name); + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; -+ + /* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */ strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); @@ builtin/submodule--helper.c: static void create_default_gitdir_config(const char + +cleanup: + strbuf_release(&gitdir_path); -+ strbuf_release(&tmp); ++ free(modules_dir); + free_worktree(wt); } -: ---------- > 2: 594eefeceb submodule init: resolve symlinks in submodule..gitdir 2: e816484923 ! 3: f5d3f71748 submodule: store settings in worktree config when enabled @@ Commit message Signed-off-by: Yoichi NAKAYAMA + ## Documentation/config/extensions.adoc ## +@@ Documentation/config/extensions.adoc: these config values on a per-worktree basis, and uses the + worktree independently. See linkgit:git-sparse-checkout[1] for more + details. + + ++Submodule configuration is also affected by this extension. ++The `submodule..gitdir` variable must be worktree-specific, as ++each worktree must use a separate gitdir for the same submodule. ++Other submodule configuration variables may be shared between worktrees, ++but Git commands that update submodule configuration store these ++variables in the worktree-specific `config.worktree` file when this ++extension is enabled, so that each worktree can have its own submodule ++configuration. +++ + For historical reasons, this extension is respected regardless of the + `core.repositoryFormatVersion` setting. + ## Documentation/config/submodule.adoc ## -@@ Documentation/config/submodule.adoc: submodule..gitdir:: +@@ Documentation/config/submodule.adoc: submodule..url:: + set, the presence of this variable is used as a fallback to indicate + whether the submodule is of interest to git commands. + See linkgit:git-submodule[1] and linkgit:gitmodules[5] for details. ++ When `extensions.worktreeConfig` is enabled, 'git submodule init' and ++ 'git submodule sync' store this setting in each worktree's config. ++ See linkgit:git-config[1] for details. + + submodule..update:: + The method by which a submodule is updated by 'git submodule update', +@@ Documentation/config/submodule.adoc: submodule..update:: + and `pull.rebase` are more specific. It is populated by + `git submodule init` from the linkgit:gitmodules[5] file. + See description of 'update' command in linkgit:git-submodule[1]. ++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores ++ this setting in each worktree's config. See linkgit:git-config[1] for ++ details. + + submodule..branch:: + The remote branch name for a submodule, used by `git submodule +@@ Documentation/config/submodule.adoc: submodule..active:: + commands. This config option takes precedence over the + submodule.active config option. See linkgit:gitsubmodules[7] for + details. ++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores ++ this setting in each worktree's config. See linkgit:git-config[1] for ++ details. + + submodule..gitdir:: + This sets the gitdir path for submodule . This configuration is respected when `extensions.submodulePathConfig` is enabled, otherwise it 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. - See linkgit:git-config[1] for details. -+ When `extensions.worktreeConfig` is enabled, this setting is stored in -+ each worktree's config. See linkgit:git-config[1] for details. ++ When `extensions.worktreeConfig` is enabled, 'git submodule init' stores ++ this setting in each worktree's config. See linkgit:git-config[1] for ++ details. submodule.active:: A repeated field which contains a pathspec used to match against a + ## Documentation/gitsubmodules.adoc ## +@@ Documentation/gitsubmodules.adoc: check if the submodule has any changes not published to any remote. The + remotes are configured in the submodule as usual in the `$GIT_DIR/config` + file. + +- * The configuration file `$GIT_DIR/config` in the superproject. +- Git only recurses into active submodules ++ * The configuration files `$GIT_DIR/config` and, when the ++ `extensions.worktreeConfig` extension is enabled, `$GIT_DIR/config.worktree` ++ in the superproject. Git only recurses into active submodules + (see <> section below). + + + If the submodule is not yet initialized, then the configuration +@@ Documentation/gitsubmodules.adoc: If the submodule has never been initialized, this is the only place + where submodule configuration is found. It serves as the last fallback + to specify where to obtain the submodule from. + ++MULTIPLE WORKTREES ++------------------ ++ ++When using multiple worktrees, it is recommended to enable the ++`extensions.worktreeConfig` extension. Without this extension, ++submodule configuration is shared between worktrees, which can ++cause unexpected interactions. For example, if different branches ++use different URLs for a submodule, a URL configured in one ++worktree may be used by another worktree, preventing it from ++fetching the required commits. ++ ++Enabling this extension allows Git commands to store submodule ++configuration in each worktree's `config.worktree` file, keeping ++the configuration independent between worktrees. ++ + [[FORMS]] + FORMS + ----- +@@ Documentation/gitsubmodules.adoc: The directory which is supposed to be the working directory is empty instead. + + + A submodule can be deinitialized by running `git submodule deinit`. + Besides emptying the working directory, this command only modifies +-the superproject's `$GIT_DIR/config` file, so the superproject's history +-is not affected. This can be undone using `git submodule init`. ++the superproject's configuration file (`$GIT_DIR/config` or ++`$GIT_DIR/config.worktree`, depending on whether the ++`extensions.worktreeConfig` extension is enabled), so the superproject's ++history is not affected. This can be undone using `git submodule init`. + + * Deleted submodule: A submodule can be deleted by running + `git rm && git commit`. This can be undone + ## builtin/submodule--helper.c ## @@ builtin/submodule--helper.c: static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, return 0; @@ builtin/submodule--helper.c: static void deinit_submodule(const char *path, cons + char *key = xstrfmt("submodule.%s.active", sub->name); + + repo_config_clear(the_repository); -+ if (is_submodule_active(the_repository, sub->name)) ++ if (is_submodule_active(the_repository, path)) + repo_config_set_worktree_gently(the_repository, + key, "false"); + free(key); @@ builtin/worktree.c: static void copy_filtered_worktree_config(const char *worktr + string_list_insert(&keys, var); + } + for_each_string_list_item(item, &keys) { -+ if (repo_config_set_in_file_gently(the_repository, -+ to_file, item->string, -+ NULL, NULL)) ++ if (repo_config_set_multivar_in_file_gently( ++ the_repository, to_file, item->string, ++ NULL, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE)) + error(_("failed to unset '%s' in '%s'"), + item->string, to_file); + } @@ t/t2405-worktree-submodule.sh: test_expect_success 'unsetting core.worktree does + ) +' + ++test_expect_success 'deinit overwrites active config (name != path)' ' ++ test_when_finished "rm -rf repo-wt-config" && ++ git clone "$base_path/origin/main" repo-wt-config && ++ ( ++ cd repo-wt-config && ++ git submodule update --init && ++ git mv sub sub_ && ++ git commit -m "move submodule" && ++ git config extensions.worktreeConfig true && ++ git worktree add wt && ++ cat .git/config >.git/expect-config && ++ ++ git submodule deinit sub_ && ++ test_cmp .git/expect-config .git/config && ++ echo "false" >expect && ++ git config --worktree submodule.sub.active >actual && ++ test_cmp expect actual && ++ ++ cd wt && ++ ++ git submodule deinit sub_ && ++ test_cmp ../.git/expect-config ../.git/config && ++ echo "false" >expect && ++ git config --worktree submodule.sub.active >actual && ++ test_cmp expect actual ++ ) ++' ++ +test_expect_success 'do not copy gitdir in config.worktree' ' + test_when_finished "rm -rf copy-wt-config" && + git clone "$base_path/origin/main" copy-wt-config && 3: b5d203d014 ! 4: 3e2c161298 submodule init: copy submodule..gitdir to config.worktree @@ Commit message Signed-off-by: Yoichi NAKAYAMA + ## Documentation/config/submodule.adoc ## +@@ Documentation/config/submodule.adoc: 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 + ## builtin/submodule--helper.c ## @@ builtin/submodule--helper.c: struct init_cb { }; @@ builtin/submodule--helper.c: struct init_cb { + return false; + } + -+ super = absolute_pathdup(repo_get_git_dir(the_repository)); ++ super = real_pathdup(repo_get_git_dir(the_repository), 1); + matches = starts_with(relative_path(submodule.buf, super, &tmp), + "modules/"); + -- gitgitgadget