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=1795903562; b=P5P3fTh0YrQhr9d2VYHArN/s6dPLaTAT4duwebVhyQR7l3wGt04s5F0B7oibqLD1iR8G S9KZnHU4EdKO4z6cDJMc8IT8c3HqdnXRkA1RP9UqyKEpw2VhFZroxX7fpqKrXIa8sdeL3 CGJGuW+83P13QY35RBLwFFyCqmE2PxXDeNSA4FMly1qZFu5HpkrfMb2v/82WPLFK+mdiO TXdl095kguBhjpxS9n4o66Vo0MxhA4vicfehNZvQlW6VSXm8QIuCigvXV+I0EKOcqStvt FI6YiTyPuLqdDP91nZ3YH7KdwS5EatnZYenCltHcNo5rT8Nf8tB7PB0DXe9Mmvbidpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795903562; 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=h4f0NBxO/DUFgwZ4q2rYUsYNyGOB2ffVHtS6RSL8QkM=; b=iHinyfrQYrp0m0by/njWD59ksKWpj3hShXKKd+Av65nDwB3gteniKiR3wijWYmMLQp7u zQa5h1RI9L9+BOrCBxGtMklUVHrU+OFZ0E20SU4/G/TfTBsqhe6qsC3ZrWYykxGW2LhH/ YW0URhqKed4IANmwui7RwAlCkIw0dhrqrukstz8OJQV/L/LjDZCILZTRo1O+5zpTiqsdk MwBjs9E2iiC0QQAu+7O+b4rKQPR3Al7co6Mt/dZn6/DUc7qUc4JP5u1oXOSNttBUhOj8U e8qCtNvBVwFW7XKx/ggn1M/TkYtS2JHQpw9V88psuabvYGzlUWO3zQyNf+MYpGfv3jg== 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=goyhJTQb; 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 A6E15606A327 for ; Sat, 28 Nov 2026 22:06:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A6E15606A327 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=goyhJTQb Received: by mail-dy1-f179.google.com with SMTP id 5a478bee46e88-386b8f2f671so5497380eec.1 for ; Sat, 28 Nov 2026 14:06:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795903561; x=1796508361; 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=h4f0NBxO/DUFgwZ4q2rYUsYNyGOB2ffVHtS6RSL8QkM=; b=goyhJTQbx8b+aFiAQdWObAsB5wAhIcDbz0IagUWSRwa+1yfbC7eLuDxzI2tTMmEpGs pxd5XnAE4OAF2BkxdJQMnIG/gqjIMbopGrT4+1ejow/jVXGT773X70frJ0gJnnDbu+WK iCH1gSUC9A1Wv8wXKNCKJUHZE8QJ3eSz75c6dXnl69+e8iBGe+NN2ZSydGaxlHhENpBG VGtI+2EnXk99j+7A2B38Dqa7IT7XqJyzn2/RidWmvgaVfj1WcYcayHZH4s0YYLWrakr2 TYSTxgE4N0GC050xzSb3s7or4HtB3MvCMc3DbyksHjGPh2BivLU6ttdIRaxOxvVj2BwY jvaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795903561; x=1796508361; 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=h4f0NBxO/DUFgwZ4q2rYUsYNyGOB2ffVHtS6RSL8QkM=; b=1GIduTCaFLB5j/gPz7rByEH4kVlsf7k2atI338oHb5brPtKcWXNl29OJWaYD0lij/B 9b4e1hrzyJGl5sUC29jQ9aOGxa2pghU+oRUsZlj0M9MNNRybRarIhNJ0zZiIlkECg2+G 0+uuw4lZ+1s+CbzvTLyd8t/AyIWNCwnW09h7OAdUMqLiU+gE2Nuecfq5T5KEVo1L32mi 6lZPx6EGgr6/YOobxyARV+U/4ZY3wTYmglhVf9v8l4XpB3/UQKkLG7UtY0+RAkyB/ho8 cegbu97FsyrH2v7dY2IUQx8YdvcAxFyi0YwR7/mgD8pLLZy6g5D/gWoJ427tTwIYEiFq hRSA== X-Gm-Message-State: APuU4Z/Ijj0cW36idvPspeqb8F0DDXIr3Tr+BQQR50Y5mBqPcr/aQDNE rjnHv5isAJ2fxryaHkLxjQkkZ53xTQ/BaJhdj2dXHIu1b5UvbixIVKKc2wHVjGfZ X-Gm-Gg: AZFdTZcuePU5AJ7CWI3nS5alAceH0+bnsaUXdjjwJDku0LhtovHEmSwXxdZBtj2vYho jnxKunk6vgtfrqS7zPQpYvWwTCtwJtB6zYI8B9AVtJiCVe2B5bc4Pug4g76WF2UQfFXaNT1hmYo Sv0q415e8qmdywskMQCRhk4TlK7xRSlb8vFPvmvtOsWjHb8Ay7Q3LdXOPIDGe8RhvS+lOZCQLQN lPPzK1dTSx6LEToPZJkaxTsuIapfbrEfOIF10noJppMMyXBMEDCRfIEtyRPfaQETnCcdz1fEDzo DlLNhnMbq5Xp2sKXWK22CREyM8eJB98yiXZhmcI+5cCOSepJikyaQpJtByzog6lUQIRmNay58AV 9v97CNH2ABMvgdoS8S8uaLJsvGC77fm5WQNvtSDQaz8vQeEHHx9rcFaGOOZv2WRziRhWgDh4pLj GoIVe9+LFdecW1F/h8Iq6bKuZgo3zDqUviy03t20QEeCs13Fh/I8jvu1HMpOewFFg7z6WuZHB0M Ocgqev1OfLU0FS3SajbteTBEjxMkSRgYR1ODVHTpI5Fd7SvN2Wi/nR81e/4zWF7Xucii71qpxvO yze/YuloUBiKIwR5PxhSYA9+hPMB72PsgQ== X-Received: by 2002:a05:7300:6da7:b0:36c:3b8a:8090 with SMTP id 5a478bee46e88-3856c9a302amr14467296eec.2.1795903560476; Sat, 28 Nov 2026 14:06:00 -0800 (PST) Received: from [127.0.0.1] ([52.160.138.198]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-38566cd46c5sm106489697eec.11.2026.11.28.14.05.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 14:06:00 -0800 (PST) Message-Id: <6ed1297d8fcfa5627f679b9aa282977466b14c76.1795903557.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Yoichi NAKAYAMA via GitGitGadget" Date: Sat, 28 Nov 2026 22:05:54 +0000 Subject: [PATCH v3 1/4] submodule init: make submodule..gitdir relative for worktrees 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 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 | 70 ++++++++++++++-------- t/t7425-submodule-gitdir-path-extension.sh | 19 ++++++ 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 461b682d0d..c20540497b 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -37,6 +37,7 @@ #include "wildmatch.h" #include "strbuf.h" #include "url.h" +#include "worktree.h" #define OPT_QUIET (1 << 0) #define OPT_CACHED (1 << 1) @@ -507,50 +508,62 @@ static int validate_and_set_submodule_gitdir(struct strbuf *gitdir_path, static void create_default_gitdir_config(const char *submodule_name) { + struct worktree *wt = get_current_worktree(the_repository); + 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; + 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; - } + 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/"); + 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)) { - strbuf_release(&gitdir_path); - return; - } + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 2.2: Try extended uppercase URI (RFC3986) encoding, to fix case-folding */ strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + 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)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 2.3: Try some derived gitdir names, see if one sticks */ for (char c = '0'; c <= '9'; c++) { strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); 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)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/"); + strbuf_addstr(&gitdir_path, modules_dir); 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)) - return; + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; } /* Case 2.4: If all the above failed, try a hash of the name as a last resort */ @@ -563,17 +576,22 @@ static void create_default_gitdir_config(const char *submodule_name) git_hash_final(raw_name_hash, &ctx); hash_to_hex_algop_r(hex_name_hash, raw_name_hash, the_hash_algo); strbuf_reset(&gitdir_path); - repo_git_path_append(the_repository, &gitdir_path, "modules/%s", hex_name_hash); - if (!validate_and_set_submodule_gitdir(&gitdir_path, submodule_name)) { - strbuf_release(&gitdir_path); - return; - } + strbuf_addstr(&gitdir_path, modules_dir); + strbuf_addstr(&gitdir_path, hex_name_hash); + if (!validate_and_set_submodule_gitdir(&gitdir_path, + submodule_name)) + goto cleanup; /* Case 3: nothing worked, error out */ die(_("failed to set a valid default config for 'submodule.%s.gitdir'. " "Please ensure it is set, for example by running something like: " - "'git config submodule.%s.gitdir .git/modules/%s'"), - submodule_name, submodule_name, submodule_name); + "'git config submodule.%s.gitdir %s%s'"), + submodule_name, submodule_name, modules_dir, submodule_name); + +cleanup: + strbuf_release(&gitdir_path); + free(modules_dir); + free_worktree(wt); } static void init_submodule(const char *path, const char *prefix, diff --git a/t/t7425-submodule-gitdir-path-extension.sh b/t/t7425-submodule-gitdir-path-extension.sh index ea86ecf7ee..02123b8468 100755 --- a/t/t7425-submodule-gitdir-path-extension.sh +++ b/t/t7425-submodule-gitdir-path-extension.sh @@ -279,6 +279,25 @@ test_expect_success '`git clone --recurse-submodules` respects init.defaultSubmo ) ' +test_expect_success 'git submodule init make gitdir relative' ' + test_when_finished "rm -rf repo-clone-init-wt" && + git clone upstream repo-clone-init-wt && + ( + cd repo-clone-init-wt && + git config core.repositoryformatversion 1 && + git config extensions.submodulePathConfig true && + git worktree add wt && + cd wt && + git submodule init && + git config submodule.sub1.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub1" >expect && + test_cmp expect actual && + git config submodule.sub2.gitdir >actual && + echo "../.git/worktrees/wt/modules/sub2" >expect && + test_cmp expect actual + ) +' + test_expect_success 'submodule--helper migrates legacy modules' ' ( cd upstream && -- gitgitgadget