From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245616; b=DDxfSoaYLBjnSnSVTbRVBQdwcPCQ0r2CYHGITP+ON7W5DhbBKZWHzvufk7Ry7QGLBvM/ a3rOWHVa/e4b2b5GH/NWmXHdQb4UIOHbOQOMu8SOFV2PPd2aJVtFkr6VKuoO37mTF7gx5 /XxKjSva8rD2/6YNqLr4LOcvhWL9oVIaMd+akJp84MRNXWn3mAuEBXDHWILZ3ZBMWUMPK Grfr3wv0T/IdInUQQIg2NDkUZdqsGw5Su9UeGfoBif0qEZs3VmX+hNdToHqsVCvPnZlNW EI/UdDSzPTR0n81ld0jomzQ3e6iX8GY2qO8tI6YiA14mJ1h6bVx35B+wlbYfLXn/rYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245616; 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=1x5M/06UPY8XhfNL7rlH/X2TB/rDDq/rpbwIoLXT5UoJAwpy9Nx22j8mpsASy/e8eaqR +Wv1PgLApYV9QmYbAF3VA/rp+be3ql1744tdPJVBqtcvg6DcKDOBYe9Tqh0q+ZZ/v4hIN Bsr8SVWh8qjEk+QMLxfalpfr6iG9p+aVTNpbVKUoE47AukTyRHpsPQwyPEKpKKxiCsiuF X1DTvmJy5UC65mnC4NWXAtFYQucCBxPO84bBN94fbCTNx30C9owr5uMB+drD/tbWrhrGk 5OrYj8imAD4xKkehTqKDahX/gAz+8H4sVqoQXGv/uIo5MnsIO3I45VqgC4KEJCIUR4w== 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="tCOR/fJ4"; arc=none smtp.remote-ip=74.125.82.41 Received: from mail-dl1-f41.google.com (mail-dl1-f41.google.com [74.125.82.41]) (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 376ED606A334 for ; Wed, 2 Dec 2026 21:06:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 376ED606A334 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=tCOR/fJ4 Received: by mail-dl1-f41.google.com with SMTP id a92af1059eb24-1b5bbedcdbeso3873919c88.0 for ; Wed, 02 Dec 2026 13:06:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796245615; x=1796850415; 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=tCOR/fJ41PfIJQwZfkZ2veyqRG3SgkgL1yZZav9jsoHHLoNMUd+rxfP9wLKAsIJO4C NPfOR78LcHUPXBI9jFMl/MauKVgz5BIAzt2vbsS7ZkGfwWiuyrknAmkDlp0c47sIVijB aF0aUBElPx/ppSQO1hKPqi8LWWPwhk4zbUoMTaeIOZgzzagpDaO0TkhCNkcikPT/cJYZ stt8GsOBofJx3jj4gRMPt7wxqbHs4xfoFMqyj2lEJfNOplcSmZehDExigcXne5a9+d8I jJPnAgI10ksxr4a3pvI7+B078hZqYRwL6HP/ysmVCgk49NS2ij29Ea2ALvViFxrFENI4 x2Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245615; x=1796850415; 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=ZEoQU4PtODzXV8jipe1o57qcCuD86/zdKSUMynz/XLLrRlsQ2hRgWY+Wt++Dgq8/Jj 85ucy/ZjTnvvJYqLYm8uBW5lEukTjTg1ZJdx354qIGBHWw4NfwzW1zopNmp6VQwOhKrE 4aAetRwxSh/b4lJUG1CEy8xkIdkEpboiNcU5FYB73VO+lIZ3VDPZTDgyrLScf+e6IDUf 8rcWlMQyOuHU2I7q7PgYXTXJm4JkPZi1FoAmr6TQnVridVUrsJ5LZAGZqPzFDu1K8wSz CgezSXZZ11J7Ilmqco1Dk8D6Sch49PDarZ2gVeNwFKEETK6HluATLBd5UNsSzR0UYLXX waXQ== X-Gm-Message-State: APuU4Z8bFJ3Ix0kYs/KcH1Nq2ljyHn9QBy1G6DHqs1PkO9pVfZa3bgFD e5mrxGvrSDuVH1Yh2skQF8BT0PyDfDpKQ09a3fQm38peIbZn2QnxdPLNflpCyQ== X-Gm-Gg: AZFdTZd0GbBn85KDUdLtE/Y8q1DOpyz3cPePyvWK62PfEloguch6XXhZOdK+Tab3rtd EPGKoKmsotnX+MYD/1wjhDqIzD0CLwqComZ+bwPfkinTTuFudwKKkm+baJBeTVIi4qgS1fH+7z6 OAgWryzoNZFVWnVow9ZmD7lslsW1PYYxnGAfqLewD4lAyt9xmedRitRU8QrVYap2StUnn2Zc6v5 v7EXjGdkYpzQnHDGXXoI2zwEZz5pWkOJ2oNVAaJ88QgusV629H343XJ91pFWGD4XmYw4JOhDzRV fmiK5EtWa6u4vAKNf6GkXD0yvsbr5hGh87m56VTi4GcGnzk5M1uvuGB3PRfUHLkdhqOhQTe6V4g pJ3wl6ETm8WSd/9lAUAY1NhcYDGo9iZDUxutn88IZOTIEIfs6OE47J1GxI1Gb4R1K3bxM+X2LsS QB13Kls4ZrXEfHXnikmAdc62vG4aJ5SvxqGGNZsyG/es+5kx5yQUWoGN6XR2iYxAQUwYCxH5CG6 otZzbKCsQJhrlqbSPc3lOSc9cDu8umAEqBYlHNOMGXhy14YalOKIcFOfpbP+/eAB8XhoTs32cD8 yZ2hJyb4lTx4mMQFlrepYPfz3rX5a2U= X-Received: by 2002:a05:701b:4585:10b0:1b5:8f22:9803 with SMTP id a92af1059eb24-1b58f229b6bmr20504899c88.1.1796245615058; Wed, 02 Dec 2026 13:06:55 -0800 (PST) Received: from [127.0.0.1] ([20.171.22.246]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-1b55ff0e4e6sm87379894c88.11.2026.12.02.13.06.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 13:06:54 -0800 (PST) Message-Id: <6ed1297d8fcfa5627f679b9aa282977466b14c76.1796245612.git.gitgitgadget@gmail.com> In-Reply-To: References: From: "Yoichi NAKAYAMA via GitGitGadget" Date: Wed, 02 Dec 2026 21:06:49 +0000 Subject: [PATCH v4 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