{"thread":{"id":"66833","subject":"[PATCH 0/3] submodule: support worktree-specific configuration","startedAt":"2026-11-12T12:03:02Z","lastAt":"2026-11-14T15:47:12Z","messageCount":6,"participants":["Yoichi NAKAYAMA via GitGitGadget","Yoichi NAKAYAMA"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"558286","messageId":"pull.2306.git.1794484985.gitgitgadget@gmail.com","threadId":"66833","inReplyTo":null,"subject":"[PATCH 0/3] submodule: support worktree-specific configuration","fromName":"Yoichi NAKAYAMA via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-12T12:03:02Z","receivedAt":"2026-11-12T12:03:02Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"When extensions.worktreeConfig is enabled, this series stores submodule\nsettings that can differ between worktrees in the worktree config.\n\nThe changes include:\n\n * [1/3] Keep submodule.<name>.gitdir relative for linked worktrees.\n * [2/3] Store submodule settings in the worktree config when\n   extensions.worktreeConfig is enabled.\n * [3/3] Make git submodule init copy an existing gitdir setting to the\n   current worktree config only when it belongs to that worktree.\n\nYoichi NAKAYAMA (3):\n  submodule: make submodule.<name>.gitdir relative for worktrees\n  submodule: store settings in worktree config when enabled\n  submodule init: copy submodule.<name>.gitdir to config.worktree\n\n Documentation/config/submodule.adoc        |   3 +-\n builtin/submodule--helper.c                | 197 +++++++++++++++------\n builtin/worktree.c                         |  23 +++\n t/t2405-worktree-submodule.sh              | 129 ++++++++++++++\n t/t7425-submodule-gitdir-path-extension.sh | 131 ++++++++++++++\n 5 files changed, 431 insertions(+), 52 deletions(-)\n\n\nbase-commit: fbd2d1e765da4d2931cadc4c62d3ca5d5b590e7e\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-2306%2Fyoichi%2Fsubmodule-with-worktree-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-2306/yoichi/submodule-with-worktree-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/2306\n-- \ngitgitgadget\n\n"},{"id":"558287","messageId":"f9adc6c85c4b098a0df8cf18783d1969a68fc048.1794484985.git.gitgitgadget@gmail.com","threadId":"66833","inReplyTo":"pull.2306.git.1794484985.gitgitgadget@gmail.com","subject":"[PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees","fromName":"Yoichi NAKAYAMA via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-12T12:03:03Z","receivedAt":"2026-11-12T12:03:03Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n\nFor a linked worktree, always keep `submodule.<name>.gitdir` relative\nto ensure it continues to work when the worktree is moved along with\nthe main worktree. (This benefit also requires\nworktree.useRelativePaths to be set to true.)\n\nSigned-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n---\n builtin/submodule--helper.c                | 68 +++++++++++++---------\n t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++\n 2 files changed, 61 insertions(+), 26 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 461b682d0d..56d7720e93 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -37,6 +37,7 @@\n #include \"wildmatch.h\"\n #include \"strbuf.h\"\n #include \"url.h\"\n+#include \"worktree.h\"\n \n #define OPT_QUIET (1 << 0)\n #define OPT_CACHED (1 << 1)\n@@ -507,50 +508,60 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n \n static void create_default_gitdir_config(const char *submodule_name)\n {\n+\tstruct worktree *wt = get_current_worktree(the_repository);\n+\tconst char *modules_dir;\n+\tstruct strbuf tmp = STRBUF_INIT;\n \tstruct strbuf gitdir_path = STRBUF_INIT;\n \tstruct git_hash_ctx ctx;\n \tchar hex_name_hash[GIT_MAX_HEXSZ + 1], header[128];\n \tunsigned char raw_name_hash[GIT_MAX_RAWSZ];\n \tint header_len;\n \n-\t/* Case 1: try the plain module name */\n-\trepo_git_path_append(the_repository, &gitdir_path, \"modules/%s\", submodule_name);\n-\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {\n-\t\tstrbuf_release(&gitdir_path);\n-\t\treturn;\n+\tmodules_dir = worktree_git_path(wt, \"modules/\");\n+\tif (is_absolute_path(modules_dir)) {\n+\t\tmodules_dir = relative_path(modules_dir, wt->path, &tmp);\n \t}\n \n+\t/* Case 1: try the plain module name */\n+\tstrbuf_addstr(&gitdir_path, modules_dir);\n+\tstrbuf_addstr(&gitdir_path, submodule_name);\n+\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t       submodule_name))\n+\t\tgoto cleanup;\n+\n \t/* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */\n \tstrbuf_reset(&gitdir_path);\n-\trepo_git_path_append(the_repository, &gitdir_path, \"modules/\");\n+\tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);\n-\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {\n-\t\tstrbuf_release(&gitdir_path);\n-\t\treturn;\n-\t}\n+\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t       submodule_name))\n+\t\tgoto cleanup;\n \n \t/* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */\n \tstrbuf_reset(&gitdir_path);\n-\trepo_git_path_append(the_repository, &gitdir_path, \"modules/\");\n+\tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);\n-\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))\n-\t\treturn;\n+\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t       submodule_name))\n+\t\tgoto cleanup;\n \n \t/* Case 2.3: Try some derived gitdir names, see if one sticks */\n \tfor (char c = '0'; c <= '9'; c++) {\n \t\tstrbuf_reset(&gitdir_path);\n-\t\trepo_git_path_append(the_repository, &gitdir_path, \"modules/\");\n+\t\tstrbuf_addstr(&gitdir_path, modules_dir);\n \t\tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);\n \t\tstrbuf_addch(&gitdir_path, c);\n-\t\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))\n-\t\t\treturn;\n+\t\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t\t       submodule_name))\n+\t\t\tgoto cleanup;\n \n \t\tstrbuf_reset(&gitdir_path);\n-\t\trepo_git_path_append(the_repository, &gitdir_path, \"modules/\");\n+\t\tstrbuf_addstr(&gitdir_path, modules_dir);\n \t\tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);\n \t\tstrbuf_addch(&gitdir_path, c);\n-\t\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name))\n-\t\t\treturn;\n+\t\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t\t       submodule_name))\n+\t\t\tgoto cleanup;\n \t}\n \n \t/* Case 2.4: If all the above failed, try a hash of the name as a last resort */\n@@ -563,17 +574,22 @@ static void create_default_gitdir_config(const char *submodule_name)\n \tgit_hash_final(raw_name_hash, &ctx);\n \thash_to_hex_algop_r(hex_name_hash, raw_name_hash, the_hash_algo);\n \tstrbuf_reset(&gitdir_path);\n-\trepo_git_path_append(the_repository, &gitdir_path, \"modules/%s\", hex_name_hash);\n-\tif (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {\n-\t\tstrbuf_release(&gitdir_path);\n-\t\treturn;\n-\t}\n+\tstrbuf_addstr(&gitdir_path, modules_dir);\n+\tstrbuf_addstr(&gitdir_path, hex_name_hash);\n+\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n+\t\t\t\t\t       submodule_name))\n+\t\tgoto cleanup;\n \n \t/* Case 3: nothing worked, error out */\n \tdie(_(\"failed to set a valid default config for 'submodule.%s.gitdir'. \"\n \t      \"Please ensure it is set, for example by running something like: \"\n-\t      \"'git config submodule.%s.gitdir .git/modules/%s'\"),\n-\t    submodule_name, submodule_name, submodule_name);\n+\t      \"'git config submodule.%s.gitdir %s%s'\"),\n+\t    submodule_name, submodule_name, modules_dir, submodule_name);\n+\n+cleanup:\n+\tstrbuf_release(&gitdir_path);\n+\tstrbuf_release(&tmp);\n+\tfree_worktree(wt);\n }\n \n static void init_submodule(const char *path, const char *prefix,\ndiff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh\nindex ea86ecf7ee..02123b8468 100755\n--- a/t/t7425-submodule-gitdir-path-extension.sh\n+++ b/t/t7425-submodule-gitdir-path-extension.sh\n@@ -279,6 +279,25 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo\n \t)\n '\n \n+test_expect_success 'git submodule init make gitdir relative' '\n+\ttest_when_finished \"rm -rf repo-clone-init-wt\" &&\n+\tgit clone upstream repo-clone-init-wt &&\n+\t(\n+\t\tcd repo-clone-init-wt &&\n+\t\tgit config core.repositoryformatversion 1 &&\n+\t\tgit config extensions.submodulePathConfig true &&\n+\t\tgit worktree add wt &&\n+\t\tcd wt &&\n+\t\tgit submodule init &&\n+\t\tgit config submodule.sub1.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub1\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config submodule.sub2.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub2\" >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'submodule--helper migrates legacy modules' '\n \t(\n \t\tcd upstream &&\n-- \ngitgitgadget\n\n\n"},{"id":"558288","messageId":"14b156bc42c4d6bcfd662d43f07231a3b62f46ef.1794484985.git.gitgitgadget@gmail.com","threadId":"66833","inReplyTo":"pull.2306.git.1794484985.gitgitgadget@gmail.com","subject":"[PATCH 2/3] submodule: store settings in worktree config when enabled","fromName":"Yoichi NAKAYAMA via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-12T12:03:04Z","receivedAt":"2026-11-12T12:03:04Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n\nSubmodule URLs can differ between branches. For example, one branch\nmay use the upstream repository while another uses a fork for the same\nsubmodule. When these branches are checked out in separate worktrees,\nstoring `submodule.<name>.url` in the shared repository config\nprevents each worktree from using its own URL.\n\nStore submodule settings such as `submodule.<name>.active` and\n`submodule.<name>.url` in the worktree config when\n`extensions.worktreeConfig` is enabled.\n\nSettings shared between worktrees can remain in the repository\nconfig. When such a setting exists there, 'git submodule deinit' sets\n`submodule.<name>.active` to false in the worktree config to\nunregister the submodule.\n\nUnlike other settings, `submodule.<name>.gitdir` should not be shared\nbetween worktrees, so exclude it from the config copied by 'git\nworktree add'.\n\nSigned-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n---\n Documentation/config/submodule.adoc        |   3 +-\n builtin/submodule--helper.c                |  43 ++++---\n builtin/worktree.c                         |  23 ++++\n t/t2405-worktree-submodule.sh              | 129 +++++++++++++++++++++\n t/t7425-submodule-gitdir-path-extension.sh |  49 ++++++++\n 5 files changed, 227 insertions(+), 20 deletions(-)\n\ndiff --git a/Documentation/config/submodule.adoc b/Documentation/config/submodule.adoc\nindex 4c424017b4..1e294147db 100644\n--- a/Documentation/config/submodule.adoc\n+++ b/Documentation/config/submodule.adoc\n@@ -58,7 +58,8 @@ submodule.<name>.gitdir::\n \trespected when `extensions.submodulePathConfig` is enabled, otherwise it\n \thas no effect. When enabled, this config becomes the single source of\n \ttruth for submodule gitdir paths and Git will error if it is missing.\n-\tSee linkgit:git-config[1] for details.\n+\tWhen `extensions.worktreeConfig` is enabled, this setting is stored in\n+\teach worktree's config. See linkgit:git-config[1] for details.\n \n submodule.active::\n \tA repeated field which contains a pathspec used to match against a\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 56d7720e93..f249582ca6 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -497,7 +497,7 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n \t\treturn 0;\n \t}\n \n-\tif (repo_config_set_gently(the_repository, key, gitdir_path->buf)) {\n+\tif (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) {\n \t\tfree(key);\n \t\treturn -1;\n \t}\n@@ -609,15 +609,10 @@ static void init_submodule(const char *path, const char *prefix,\n \t\tdie(_(\"No url found for submodule path '%s' in .gitmodules\"),\n \t\t\tdisplaypath);\n \n-\t/*\n-\t * NEEDSWORK: In a multi-working-tree world, this needs to be\n-\t * set in the per-worktree config.\n-\t *\n-\t * Set active flag for the submodule being initialized\n-\t */\n+\t/* Set active flag for the submodule being initialized */\n \tif (!is_submodule_active(the_repository, path)) {\n \t\tstrbuf_addf(&sb, \"submodule.%s.active\", sub->name);\n-\t\trepo_config_set_gently(the_repository, sb.buf, \"true\");\n+\t\trepo_config_set_worktree_gently(the_repository, sb.buf, \"true\");\n \t\tstrbuf_reset(&sb);\n \t}\n \n@@ -643,7 +638,7 @@ static void init_submodule(const char *path, const char *prefix,\n \t\t\tfree(oldurl);\n \t\t}\n \n-\t\tif (repo_config_set_gently(the_repository, sb.buf, url))\n+\t\tif (repo_config_set_worktree_gently(the_repository, sb.buf, url))\n \t\t\tdie(_(\"Failed to register url for submodule path '%s'\"),\n \t\t\t    displaypath);\n \t\tif (!(flags & OPT_QUIET))\n@@ -665,7 +660,7 @@ static void init_submodule(const char *path, const char *prefix,\n \t\t\tupd = submodule_update_type_to_string(sub->update_strategy.type);\n \t\t}\n \n-\t\tif (repo_config_set_gently(the_repository, sb.buf, upd))\n+\t\tif (repo_config_set_worktree_gently(the_repository, sb.buf, upd))\n \t\t\tdie(_(\"Failed to register update mode for submodule path '%s'\"), displaypath);\n \t}\n \n@@ -1491,7 +1486,7 @@ static void sync_submodule(const char *path, const char *prefix,\n \n \tstrbuf_reset(&sb);\n \tstrbuf_addf(&sb, \"submodule.%s.url\", sub->name);\n-\tif (repo_config_set_gently(the_repository, sb.buf, super_config_url))\n+\tif (repo_config_set_worktree_gently(the_repository, sb.buf, super_config_url))\n \t\tdie(_(\"failed to register url for submodule path '%s'\"),\n \t\t      displaypath);\n \n@@ -1670,17 +1665,31 @@ static void deinit_submodule(const char *path, const char *prefix,\n \n \t/* remove the .git/config entries (unless the user already did it) */\n \tif (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {\n+\t\tchar *file = the_repository->repository_format_worktree_config ?\n+\t\t  repo_git_path(the_repository, \"config.worktree\") :\n+\t\t  repo_git_path(the_repository, \"config\");\n \t\tchar *sub_key = xstrfmt(\"submodule.%s\", sub->name);\n \n \t\t/*\n \t\t * remove the whole section so we have a clean state when\n \t\t * the user later decides to init this submodule again\n \t\t */\n-\t\trepo_config_rename_section_in_file(the_repository, NULL, sub_key, NULL);\n+\t\trepo_config_rename_section_in_file(the_repository, file, sub_key, NULL);\n \t\tif (!(flags & OPT_QUIET))\n \t\t\tprintf(_(\"Submodule '%s' (%s) unregistered for path '%s'\\n\"),\n \t\t\t\t sub->name, sub->url, displaypath);\n+\t\tfree(file);\n \t\tfree(sub_key);\n+\n+\t\tif (the_repository->repository_format_worktree_config) {\n+\t\t\tchar *key = xstrfmt(\"submodule.%s.active\", sub->name);\n+\n+\t\t\trepo_config_clear(the_repository);\n+\t\t\tif (is_submodule_active(the_repository, sub->name))\n+\t\t\t\trepo_config_set_worktree_gently(the_repository,\n+\t\t\t\t\t\t\t\tkey, \"false\");\n+\t\t\tfree(key);\n+\t\t}\n \t}\n \n cleanup:\n@@ -3580,7 +3589,7 @@ static void configure_added_submodule(struct add_data *add_data)\n \tint matched = 0;\n \n \tkey = xstrfmt(\"submodule.%s.url\", add_data->sm_name);\n-\trepo_config_set_gently(the_repository, key, add_data->realrepo);\n+\trepo_config_set_worktree_gently(the_repository, key, add_data->realrepo);\n \tfree(key);\n \n \tadd_submod.git_cmd = 1;\n@@ -3610,10 +3619,6 @@ static void configure_added_submodule(struct add_data *add_data)\n \tif (run_command(&add_gitmodules))\n \t\tdie(_(\"Failed to register submodule '%s'\"), add_data->sm_path);\n \n-\t/*\n-\t * NEEDSWORK: In a multi-working-tree world this needs to be\n-\t * set in the per-worktree config.\n-\t */\n \t/*\n \t * NEEDSWORK: In the longer run, we need to get rid of this\n \t * pattern of querying \"submodule.active\" before calling\n@@ -3627,7 +3632,7 @@ static void configure_added_submodule(struct add_data *add_data)\n \t\t * current configured pathspec, set the submodule's active flag\n \t\t */\n \t\tkey = xstrfmt(\"submodule.%s.active\", add_data->sm_name);\n-\t\trepo_config_set_gently(the_repository, key, \"true\");\n+\t\trepo_config_set_worktree_gently(the_repository, key, \"true\");\n \t\tfree(key);\n \t} else {\n \t\tfor (size_t i = 0; i < values->nr; i++) {\n@@ -3639,7 +3644,7 @@ static void configure_added_submodule(struct add_data *add_data)\n \t\t}\n \t\tif (!matched) { /* no pattern matched -> force-enable */\n \t\t\tkey = xstrfmt(\"submodule.%s.active\", add_data->sm_name);\n-\t\t\trepo_config_set_gently(the_repository, key, \"true\");\n+\t\t\trepo_config_set_worktree_gently(the_repository, key, \"true\");\n \t\t\tfree(key);\n \t\t}\n \t}\ndiff --git a/builtin/worktree.c b/builtin/worktree.c\nindex 77ecd0f71f..ef03ef979a 100644\n--- a/builtin/worktree.c\n+++ b/builtin/worktree.c\n@@ -363,6 +363,9 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)\n \tif (file_exists(from_file)) {\n \t\tstruct config_set cs = { { 0 } };\n \t\tint bare;\n+\t\tstruct string_list keys = STRING_LIST_INIT_DUP;\n+\t\tstruct string_list_item *item;\n+\t\tint i;\n \n \t\tif (safe_create_leading_directories(the_repository, to_file) ||\n \t\t\tcopy_file(the_repository, to_file, from_file, 0666)) {\n@@ -386,6 +389,26 @@ static void copy_filtered_worktree_config(const char *worktree_git_dir)\n \t\t\terror(_(\"failed to unset '%s' in '%s'\"),\n \t\t\t\t\"core.worktree\", to_file);\n \n+\t\t/* unset \"submodule.*.gitdir\" */\n+\t\tfor (i = 0; i < cs.list.nr; i++) {\n+\t\t\tconst char *var = cs.list.items[i].e->key;\n+\t\t\tconst char *subsection, *key;\n+\t\t\tsize_t subsection_len;\n+\n+\t\t\tif (!parse_config_key(var, \"submodule\",\n+\t\t\t\t\t      &subsection, &subsection_len, &key) &&\n+\t\t\t    subsection && !strcmp(key, \"gitdir\"))\n+\t\t\t\tstring_list_insert(&keys, var);\n+\t\t}\n+\t\tfor_each_string_list_item(item, &keys) {\n+\t\t\tif (repo_config_set_in_file_gently(the_repository,\n+\t\t\t\t\t\t\t   to_file, item->string,\n+\t\t\t\t\t\t\t   NULL, NULL))\n+\t\t\t\terror(_(\"failed to unset '%s' in '%s'\"),\n+\t\t\t\t      item->string, to_file);\n+\t\t}\n+\t\tstring_list_clear(&keys, 0);\n+\n \t\tgit_configset_clear(&cs);\n \t}\n \ndiff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh\nindex f5c94d490b..d46d565ad6 100755\n--- a/t/t2405-worktree-submodule.sh\n+++ b/t/t2405-worktree-submodule.sh\n@@ -91,4 +91,133 @@ test_expect_success 'unsetting core.worktree does not prevent running commands d\n \tgit -C main/.git/worktrees/checkout-recurse/modules/sub log\n '\n \n+test_expect_success 'submodule config in worktree config' '\n+\ttest_when_finished \"rm -rf wt-config\" &&\n+\tgit clone \"$base_path/origin/main\" wt-config &&\n+\t(\n+\t\tcd wt-config &&\n+\t\tgit config extensions.worktreeConfig true &&\n+\t\tgit worktree add wt &&\n+\t\tcat .git/config >.git/expect-config &&\n+\n+\t\tgit submodule update --init &&\n+\t\ttest_cmp .git/expect-config .git/config &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit submodule deinit sub &&\n+\t\ttest_cmp .git/expect-config .git/config &&\n+\t\ttest_must_be_empty .git/config.worktree &&\n+\n+\t\tgit submodule add ../sub sub2 &&\n+\t\ttest_cmp .git/expect-config .git/config &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub2.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub2.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit config --file=.gitmodules submodule.sub2.url new_url &&\n+\t\tgit submodule sync &&\n+\t\ttest_cmp .git/expect-config .git/config &&\n+\t\techo \"new_url\" >expect &&\n+\t\tgit config --worktree submodule.sub2.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcd wt &&\n+\n+\t\tgit submodule update --init &&\n+\t\ttest_cmp ../.git/expect-config ../.git/config &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit submodule deinit sub &&\n+\t\ttest_cmp ../.git/expect-config ../.git/config &&\n+\t\ttest_must_be_empty ../.git/worktrees/wt/config.worktree &&\n+\n+\t\tgit submodule add ../sub sub2 &&\n+\t\ttest_cmp ../.git/expect-config ../.git/config &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub2.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub2.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit config --file=.gitmodules submodule.sub2.url new_url &&\n+\t\tgit submodule sync &&\n+\t\ttest_cmp ../.git/expect-config ../.git/config &&\n+\t\techo \"new_url\" >expect &&\n+\t\tgit config --worktree submodule.sub2.url >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'deinit overwrites active config' '\n+\ttest_when_finished \"rm -rf repo-wt-config\" &&\n+\tgit clone \"$base_path/origin/main\" repo-wt-config &&\n+\t(\n+\t\tcd repo-wt-config &&\n+\t\tgit submodule update --init &&\n+\t\tgit config extensions.worktreeConfig true &&\n+\t\tgit worktree add wt &&\n+\t\tcat .git/config >.git/expect-config &&\n+\n+\t\tgit submodule deinit sub &&\n+\t\ttest_cmp .git/expect-config .git/config &&\n+\t\techo \"false\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcd wt &&\n+\n+\t\tgit submodule deinit sub &&\n+\t\ttest_cmp ../.git/expect-config ../.git/config &&\n+\t\techo \"false\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'do not copy gitdir in config.worktree' '\n+\ttest_when_finished \"rm -rf copy-wt-config\" &&\n+\tgit clone \"$base_path/origin/main\" copy-wt-config &&\n+\t(\n+\t\tcd copy-wt-config &&\n+\t\tgit config core.repositoryformatversion 1 &&\n+\t\tgit config extensions.submodulePathConfig true &&\n+\t\tgit config extensions.worktreeConfig true &&\n+\n+\t\tgit submodule update --init &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \".git/modules/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub.gitdir >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit worktree add wt &&\n+\t\tcd wt &&\n+\t\techo \"true\" >expect &&\n+\t\tgit config --worktree submodule.sub.active >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\techo \"$base_path/origin/sub\" >expect &&\n+\t\tgit config --worktree submodule.sub.url >actual &&\n+\t\ttest_cmp expect actual &&\n+\t\ttest_must_fail git config --worktree submodule.sub.gitdir\n+\t)\n+'\n+\n test_done\ndiff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh\nindex 02123b8468..3bae4c4ef1 100755\n--- a/t/t7425-submodule-gitdir-path-extension.sh\n+++ b/t/t7425-submodule-gitdir-path-extension.sh\n@@ -78,6 +78,55 @@ test_expect_success 'gitdir config path is relative for both absolute and relati\n \t)\n '\n \n+test_expect_success 'gitdir for the main worktree is stored in worktree config' '\n+\ttest_when_finished \"rm -rf relative-cfg-path-test-main\" &&\n+\tgit init -b main relative-cfg-path-test-main &&\n+\t(\n+\t\tcd relative-cfg-path-test-main &&\n+\t\tgit config core.repositoryformatversion 1 &&\n+\t\tgit config extensions.submodulePathConfig true &&\n+\t\tgit config extensions.worktreeConfig true &&\n+\n+\t\t# Test with absolute URL\n+\t\tgit submodule add \"$TRASH_DIRECTORY/new-sub\" sub-abs &&\n+\t\tgit config --worktree submodule.sub-abs.gitdir >actual &&\n+\t\techo \".git/modules/sub-abs\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# Test with relative URL\n+\t\tgit submodule add ../new-sub sub-rel &&\n+\t\tgit config --worktree submodule.sub-rel.gitdir >actual &&\n+\t\techo \".git/modules/sub-rel\" >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'gitdir for a linked worktree is stored in worktree config' '\n+\ttest_when_finished \"rm -rf relative-cfg-path-test-wt\" &&\n+\tgit init -b main relative-cfg-path-test-wt &&\n+\t(\n+\t\tcd relative-cfg-path-test-wt &&\n+\t\tgit config core.repositoryformatversion 1 &&\n+\t\tgit config extensions.submodulePathConfig true &&\n+\t\tgit config extensions.worktreeConfig true &&\n+\n+\t\tgit worktree add wt &&\n+\t\tcd wt &&\n+\n+\t\t# Test with absolute URL\n+\t\tgit submodule add \"$TRASH_DIRECTORY/new-sub\" sub-abs &&\n+\t\tgit config --worktree submodule.sub-abs.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub-abs\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# Test with relative URL\n+\t\tgit submodule add ../../new-sub sub-rel &&\n+\t\tgit config --worktree submodule.sub-rel.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub-rel\" >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'clone from repo with both legacy and new-style submodules' '\n \tgit clone --recurse-submodules main cloned-non-extension &&\n \t(\n-- \ngitgitgadget\n\n\n"},{"id":"558289","messageId":"cdf089b6355440e6515723a47aacbcc1605ab3f0.1794484985.git.gitgitgadget@gmail.com","threadId":"66833","inReplyTo":"pull.2306.git.1794484985.gitgitgadget@gmail.com","subject":"[PATCH 3/3] submodule init: copy submodule.<name>.gitdir to config.worktree","fromName":"Yoichi NAKAYAMA via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-12T12:03:05Z","receivedAt":"2026-11-12T12:03:05Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n\nIf a repository already has gitdir settings in its repository config\nwhen extensions.worktreeConfig is enabled, those settings belong to a\nparticular worktree. Make 'git submodule init' copy an existing\nsetting to the worktree config if it belongs to the current worktree;\notherwise, generate a new setting.\n\nSigned-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n---\n builtin/submodule--helper.c                | 100 ++++++++++++++++++---\n t/t7425-submodule-gitdir-path-extension.sh |  65 +++++++++++++-\n 2 files changed, 151 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex f249582ca6..f34a22238f 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -480,10 +480,41 @@ struct init_cb {\n };\n #define INIT_CB_INIT { 0 }\n \n+static int validate_submodule_git_dir_with_worktree(const char *gitdir)\n+{\n+\tstruct strbuf tmp = STRBUF_INIT;\n+\tstruct strbuf submodule = STRBUF_INIT;\n+\tchar *super;\n+\tint ret = 1;\n+\n+\tif (is_absolute_path(gitdir))\n+\t\tstrbuf_addstr(&submodule, gitdir);\n+\telse if (!repo_worktree_path_append(the_repository, &submodule,\n+\t\t\t\t\t    \"%s\", gitdir))\n+\t\treturn -1;\n+\n+\tif (strbuf_normalize_path(&submodule)) {\n+\t\tstrbuf_release(&submodule);\n+\t\treturn -1;\n+\t}\n+\n+\tsuper = absolute_pathdup(repo_get_git_dir(the_repository));\n+\n+\tif (starts_with(relative_path(submodule.buf, super, &tmp), \"modules/\"))\n+\t\tret = 0;\n+\n+\tstrbuf_release(&submodule);\n+\tstrbuf_release(&tmp);\n+\tfree(super);\n+\treturn ret;\n+}\n+\n static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n-\t\t\t\t\t     const char *submodule_name)\n+\t\t\t\t\t     const char *submodule_name,\n+\t\t\t\t\t     struct worktree *wt)\n {\n-\tconst char *value;\n+\tconst char *value = gitdir_path->buf;\n+\tchar *value_to_free = NULL;\n \tchar *key;\n \n \tif (validate_submodule_git_dir(gitdir_path->buf, submodule_name))\n@@ -491,18 +522,55 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n \n \tkey = xstrfmt(\"submodule.%s.gitdir\", submodule_name);\n \n-\t/* Nothing to do if the config already exists. */\n-\tif (!repo_config_get_string_tmp(the_repository, key, &value)) {\n-\t\tfree(key);\n-\t\treturn 0;\n+\tif (the_repository->repository_format_worktree_config) {\n+\t\tstruct config_set cs = { 0 };\n+\t\tconst char *filename = worktree_git_path(wt, \"config.worktree\");\n+\t\tchar *tmp;\n+\n+\t\tgit_configset_init(&cs);\n+\t\tgit_configset_add_file(&cs, filename);\n+\n+\t\t/*\n+\t\t * Nothing to do if the config already exists in the config set\n+\t\t * for the current worktree.\n+\t\t */\n+\t\tif (!git_configset_get(&cs, key)) {\n+\t\t\tgit_configset_clear(&cs);\n+\t\t\tfree(key);\n+\t\t\treturn 0;\n+\t\t}\n+\n+\t\tgit_configset_clear(&cs);\n+\n+\t\t/*\n+\t\t * If the worktree config does not have this key, check the\n+\t\t * effective value from lower-priority config scopes and copy\n+\t\t * it to the worktree config if it belongs to the current\n+\t\t * worktree.\n+\t\t */\n+\t\tif (!repo_config_get_string(the_repository, key, &tmp)) {\n+\t\t\tif (validate_submodule_git_dir_with_worktree(tmp) ||\n+\t\t\t    validate_submodule_git_dir(tmp, submodule_name))\n+\t\t\t\tfree(tmp);\n+\t\t\telse\n+\t\t\t\tvalue = value_to_free = tmp;\n+\t\t}\n+\t} else {\n+\t\t/* Nothing to do if the config already exists. */\n+\t\tif (!repo_config_get_string_tmp(the_repository, key, &value)) {\n+\t\t\tfree(key);\n+\t\t\treturn 0;\n+\t\t}\n \t}\n \n-\tif (repo_config_set_worktree_gently(the_repository, key, gitdir_path->buf)) {\n+\tif (repo_config_set_worktree_gently(the_repository, key, value)) {\n \t\tfree(key);\n+\t\tfree(value_to_free);\n \t\treturn -1;\n \t}\n \n \tfree(key);\n+\tfree(value_to_free);\n \treturn 0;\n }\n \n@@ -526,7 +594,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr(&gitdir_path, submodule_name);\n \tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t       wt))\n \t\tgoto cleanup;\n \n \t/* Case 2.1: Try URI-safe (RFC3986) encoding first, this fixes nested gitdirs */\n@@ -534,7 +603,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);\n \tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t       wt))\n \t\tgoto cleanup;\n \n \t/* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */\n@@ -542,7 +612,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);\n \tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t       wt))\n \t\tgoto cleanup;\n \n \t/* Case 2.3: Try some derived gitdir names, see if one sticks */\n@@ -552,7 +623,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \t\tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_rfc3986_unreserved);\n \t\tstrbuf_addch(&gitdir_path, c);\n \t\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t\t       wt))\n \t\t\tgoto cleanup;\n \n \t\tstrbuf_reset(&gitdir_path);\n@@ -560,7 +632,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \t\tstrbuf_addstr_urlencode(&gitdir_path, submodule_name, is_casefolding_rfc3986_unreserved);\n \t\tstrbuf_addch(&gitdir_path, c);\n \t\tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t\t       wt))\n \t\t\tgoto cleanup;\n \t}\n \n@@ -577,7 +650,8 @@ static void create_default_gitdir_config(const char *submodule_name)\n \tstrbuf_addstr(&gitdir_path, modules_dir);\n \tstrbuf_addstr(&gitdir_path, hex_name_hash);\n \tif (!validate_and_set_submodule_gitdir(&gitdir_path,\n-\t\t\t\t\t       submodule_name))\n+\t\t\t\t\t       submodule_name,\n+\t\t\t\t\t       wt))\n \t\tgoto cleanup;\n \n \t/* Case 3: nothing worked, error out */\ndiff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh\nindex 3bae4c4ef1..36aac0f578 100755\n--- a/t/t7425-submodule-gitdir-path-extension.sh\n+++ b/t/t7425-submodule-gitdir-path-extension.sh\n@@ -328,7 +328,47 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo\n \t)\n '\n \n-test_expect_success 'git submodule init make gitdir relative' '\n+test_expect_success 'git submodule init inherits related repository config for main' '\n+\ttest_when_finished \"rm -rf repo-clone-init-main\" &&\n+\tgit clone upstream repo-clone-init-main &&\n+\t(\n+\t\tcd repo-clone-init-main &&\n+\t\tgit config core.repositoryformatversion 1 &&\n+\t\tgit config extensions.submodulePathConfig true &&\n+\t\tgit submodule init &&\n+\t\tgit config submodule.sub1.gitdir >actual &&\n+\t\techo \".git/modules/sub1\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config submodule.sub2.gitdir >actual &&\n+\t\techo \".git/modules/sub2\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# Modify values so that they can be distinguished from the default\n+\t\tgit config submodule.sub1.gitdir .git/modules/sub1_custom &&\n+\t\tgit config submodule.sub2.gitdir .git/modules/sub2_custom &&\n+\n+\t\tgit config extensions.worktreeConfig true &&\n+\t\tgit submodule init &&\n+\t\tgit config --worktree submodule.sub1.gitdir >actual &&\n+\t\techo \".git/modules/sub1_custom\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config --worktree submodule.sub2.gitdir >actual &&\n+\t\techo \".git/modules/sub2_custom\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit worktree add wt &&\n+\t\tcd wt &&\n+\t\tgit submodule init &&\n+\t\tgit config --worktree submodule.sub1.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub1\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config --worktree submodule.sub2.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub2\" >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'git submodule init inherits related repository config for worktree' '\n \ttest_when_finished \"rm -rf repo-clone-init-wt\" &&\n \tgit clone upstream repo-clone-init-wt &&\n \t(\n@@ -343,6 +383,29 @@ test_expect_success 'git submodule init make gitdir relative' '\n \t\ttest_cmp expect actual &&\n \t\tgit config submodule.sub2.gitdir >actual &&\n \t\techo \"../.git/worktrees/wt/modules/sub2\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\t# Modify values so that they can be distinguished from the default\n+\t\tgit config submodule.sub1.gitdir ../.git/worktrees/wt/modules/sub1_custom &&\n+\t\t# Use an absolute path to simulate a value written by older Git\n+\t\tgit config submodule.sub2.gitdir \"$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom\" &&\n+\n+\t\tgit config extensions.worktreeConfig true &&\n+\t\tgit submodule init &&\n+\t\tgit config --worktree submodule.sub1.gitdir >actual &&\n+\t\techo \"../.git/worktrees/wt/modules/sub1_custom\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config --worktree submodule.sub2.gitdir >actual &&\n+\t\techo \"$(cd .. && pwd -P)/.git/worktrees/wt/modules/sub2_custom\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tcd .. &&\n+\t\tgit submodule init &&\n+\t\tgit config --worktree submodule.sub1.gitdir >actual &&\n+\t\techo \".git/modules/sub1\" >expect &&\n+\t\ttest_cmp expect actual &&\n+\t\tgit config --worktree submodule.sub2.gitdir >actual &&\n+\t\techo \".git/modules/sub2\" >expect &&\n \t\ttest_cmp expect actual\n \t)\n '\n-- \ngitgitgadget\n\n"},{"id":"558456","messageId":"CAF5D8-sJkCzL85boqNDnuU+wWthMSB37h5SJOny4hDUfe8y1rg@mail.gmail.com","threadId":"66833","inReplyTo":"f9adc6c85c4b098a0df8cf18783d1969a68fc048.1794484985.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees","fromName":"Yoichi NAKAYAMA","fromEmail":"yoichi.nakayama@gmail.com","sentAt":"2026-11-14T15:27:36Z","receivedAt":"2026-11-14T15:27:36Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"On Thu, Nov 12, 2026 at 9:03 PM Yoichi NAKAYAMA via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>\n> From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n>\n> For a linked worktree, always keep `submodule.<name>.gitdir` relative\n> to ensure it continues to work when the worktree is moved along with\n> the main worktree. (This benefit also requires\n> worktree.useRelativePaths to be set to true.)\n>\n> Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n> ---\n>  builtin/submodule--helper.c                | 68 +++++++++++++---------\n>  t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++\n>  2 files changed, 61 insertions(+), 26 deletions(-)\n>\n> diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\n> index 461b682d0d..56d7720e93 100644\n> --- a/builtin/submodule--helper.c\n> +++ b/builtin/submodule--helper.c\n> @@ -37,6 +37,7 @@\n>  #include \"wildmatch.h\"\n>  #include \"strbuf.h\"\n>  #include \"url.h\"\n> +#include \"worktree.h\"\n>\n>  #define OPT_QUIET (1 << 0)\n>  #define OPT_CACHED (1 << 1)\n> @@ -507,50 +508,60 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n>\n>  static void create_default_gitdir_config(const char *submodule_name)\n>  {\n> +       struct worktree *wt = get_current_worktree(the_repository);\n> +       const char *modules_dir;\n> +       struct strbuf tmp = STRBUF_INIT;\n>         struct strbuf gitdir_path = STRBUF_INIT;\n>         struct git_hash_ctx ctx;\n>         char hex_name_hash[GIT_MAX_HEXSZ + 1], header[128];\n>         unsigned char raw_name_hash[GIT_MAX_RAWSZ];\n>         int header_len;\n>\n> -       /* Case 1: try the plain module name */\n> -       repo_git_path_append(the_repository, &gitdir_path, \"modules/%s\", submodule_name);\n> -       if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {\n> -               strbuf_release(&gitdir_path);\n> -               return;\n> +       modules_dir = worktree_git_path(wt, \"modules/\");\n> +       if (is_absolute_path(modules_dir)) {\n> +               modules_dir = relative_path(modules_dir, wt->path, &tmp);\n>         }\n\nI noticed that worktree_git_path() returns a pointer to a static buffer managed\nby get_pathname(). I'll copy the result into tmp immediately, before calling\nother functions that might overwrite the buffer:\n\n    modules_dir = worktree_git_path(wt, \"modules/\");\n    if (is_absolute_path(modules_dir))\n        relative_path(modules_dir, wt->path, &tmp);\n    else\n        strbuf_addstr(&tmp, modules_dir);\n    modules_dir = tmp.buf;\n\nThanks,\n-- \nYoichi NAKAYAMA\n\n"},{"id":"558460","messageId":"CAF5D8-uCq26c9G+R7uzoepY28aN0H6d+XL5DYBc5_cp0pF35vw@mail.gmail.com","threadId":"66833","inReplyTo":"CAF5D8-sJkCzL85boqNDnuU+wWthMSB37h5SJOny4hDUfe8y1rg@mail.gmail.com","subject":"Re: [PATCH 1/3] submodule: make submodule.<name>.gitdir relative for worktrees","fromName":"Yoichi NAKAYAMA","fromEmail":"yoichi.nakayama@gmail.com","sentAt":"2026-11-14T15:47:12Z","receivedAt":"2026-11-14T15:47:12Z","isPatch":true,"sender":{"key":"yoichi.nakayama@gmail.com","avatar":"https://avatars.githubusercontent.com/u/107537?v=4"},"body":"On Sun, Nov 15, 2026 at 12:27 AM Yoichi NAKAYAMA\n<yoichi.nakayama@gmail.com> wrote:\n>\n> On Thu, Nov 12, 2026 at 9:03 PM Yoichi NAKAYAMA via GitGitGadget\n> <gitgitgadget@gmail.com> wrote:\n> >\n> > From: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n> >\n> > For a linked worktree, always keep `submodule.<name>.gitdir` relative\n> > to ensure it continues to work when the worktree is moved along with\n> > the main worktree. (This benefit also requires\n> > worktree.useRelativePaths to be set to true.)\n> >\n> > Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com>\n> > ---\n> >  builtin/submodule--helper.c                | 68 +++++++++++++---------\n> >  t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++\n> >  2 files changed, 61 insertions(+), 26 deletions(-)\n> >\n> > diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\n> > index 461b682d0d..56d7720e93 100644\n> > --- a/builtin/submodule--helper.c\n> > +++ b/builtin/submodule--helper.c\n> > @@ -37,6 +37,7 @@\n> >  #include \"wildmatch.h\"\n> >  #include \"strbuf.h\"\n> >  #include \"url.h\"\n> > +#include \"worktree.h\"\n> >\n> >  #define OPT_QUIET (1 << 0)\n> >  #define OPT_CACHED (1 << 1)\n> > @@ -507,50 +508,60 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path,\n> >\n> >  static void create_default_gitdir_config(const char *submodule_name)\n> >  {\n> > +       struct worktree *wt = get_current_worktree(the_repository);\n> > +       const char *modules_dir;\n> > +       struct strbuf tmp = STRBUF_INIT;\n> >         struct strbuf gitdir_path = STRBUF_INIT;\n> >         struct git_hash_ctx ctx;\n> >         char hex_name_hash[GIT_MAX_HEXSZ + 1], header[128];\n> >         unsigned char raw_name_hash[GIT_MAX_RAWSZ];\n> >         int header_len;\n> >\n> > -       /* Case 1: try the plain module name */\n> > -       repo_git_path_append(the_repository, &gitdir_path, \"modules/%s\", submodule_name);\n> > -       if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) {\n> > -               strbuf_release(&gitdir_path);\n> > -               return;\n> > +       modules_dir = worktree_git_path(wt, \"modules/\");\n> > +       if (is_absolute_path(modules_dir)) {\n> > +               modules_dir = relative_path(modules_dir, wt->path, &tmp);\n> >         }\n>\n> I noticed that worktree_git_path() returns a pointer to a static buffer managed\n> by get_pathname(). I'll copy the result into tmp immediately, before calling\n> other functions that might overwrite the buffer:\n>\n>     modules_dir = worktree_git_path(wt, \"modules/\");\n>     if (is_absolute_path(modules_dir))\n>         relative_path(modules_dir, wt->path, &tmp);\n>     else\n>         strbuf_addstr(&tmp, modules_dir);\n>     modules_dir = tmp.buf;\n\nCorrection: relative_path() does not always store the result in tmp.\nThe code should be:\n\n    modules_dir = worktree_git_path(wt, \"modules/\");\n    if (is_absolute_path(modules_dir)) {\n        modules_dir = relative_path(modules_dir, wt->path, &tmp);\n    } else {\n        strbuf_addstr(&tmp, modules_dir);\n        modules_dir = tmp.buf;\n    }\n\nThanks,\n-- \nYoichi NAKAYAMA\n\n"}]}