From patchwork Wed Nov 17 00:56:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emily Shaffer X-Patchwork-Id: 12623379 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56D3CC433EF for ; Wed, 17 Nov 2021 00:57:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3138661BFB for ; Wed, 17 Nov 2021 00:57:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229868AbhKQBAI (ORCPT ); Tue, 16 Nov 2021 20:00:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229543AbhKQBAG (ORCPT ); Tue, 16 Nov 2021 20:00:06 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E8B5C061570 for ; Tue, 16 Nov 2021 16:57:09 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id lt10-20020a17090b354a00b001a649326aedso2151161pjb.5 for ; Tue, 16 Nov 2021 16:57:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=DfCP7FsPguIce0YnHHzzqxlq868zO7M1fd9aVh/VHdk=; b=CmGrqqVlSbR59wEFAOeGY2iXTIp5stKA+uBVnmnFcxHOA1dQRih31bd5JCRb3irCWW gQNx23sbH7ot2umwGL/8B5jBWsFQvosJ/cO1pDVvqiV+7GQgThBmN1J9G1uo70Q4YdAA oybzLpFZrjvY6PNMRE6OFnpLPqWtP6rT3Zf0USYCwAhG6ct/0A4daMAv8Azg0WVmDw5x RquPhUIXFTxQYSASZ2fw+l7GLnRisr5NyiQb6R7DTqkIMedJaCoA2/Uh4jpz7L/yt6uD EdGgE7r75JT1IPWWMnI2HSw9Wpmmzdj/R1DxfpfRgy8SXrTbkWZFhA/G2wvz6lfYZjLF zE2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=DfCP7FsPguIce0YnHHzzqxlq868zO7M1fd9aVh/VHdk=; b=N4721QgShkdoAGpXhnlB4YYNQUzFnbrGo967LAC/ueNfYISzfzQ0862MORNVBzwUj8 qpphYT05piouTt1bHMh4Sa+bMYNdz4rRm7xkpJrgwmCYdH6Fd6jBx6dXg8fnTEUNdeE/ oY6ROcYH9gwQ2RY3Jqo2BGuLV9VY/QDWkGSg0T/jhOqV7sseNzgMU6vEhy/+ltVaL+G+ VK4Tqbj8tSyesU5YDWKyCPU7PF43rfn2zDzU/l6/3LM3ldYZv+1TqIZ+IqA8qlMEGiXE pJ8xEkYwHtJv0D3v9QgxxvHNVJJEvv5WYu1ksMDYrbVmHof3qREocoiG+HMMLW7xaHcB GKeA== X-Gm-Message-State: AOAM5312TqndPiHBCOvAQ7oh6pMCpbkaZBD8vRNKVYG5b0b3OsbLzmG1 ajdOvjtj6HjZT4JEzFuxh46NIfRD0FoePv/6H5zVbTD35gak0sDqw4BR21GC/ITH+hugUn2ZjVh G4/kLMMGLtHKCShhjuM48bNEXaB48mNjUkaREu4T6gIRYqCpebaU3CULvrZoDVloTgyaeN8zSYw == X-Google-Smtp-Source: ABdhPJwy7Clv9O9RUUCcI24FkVN7JHkc8J3XQwFF1rUOlq/GnNt4vd/wnal4vj1OspYvlef32+i0E5WqWlY32UTIQVw= X-Received: from podkayne.svl.corp.google.com ([2620:15c:2ce:200:205d:7edd:a3da:3608]) (user=emilyshaffer job=sendgmr) by 2002:a17:90a:390c:: with SMTP id y12mr165293pjb.0.1637110628371; Tue, 16 Nov 2021 16:57:08 -0800 (PST) Date: Tue, 16 Nov 2021 16:56:57 -0800 In-Reply-To: <20211117005701.371808-1-emilyshaffer@google.com> Message-Id: <20211117005701.371808-2-emilyshaffer@google.com> Mime-Version: 1.0 References: <20211117005701.371808-1-emilyshaffer@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v6 1/5] t7400-submodule-basic: modernize inspect() helper From: Emily Shaffer To: git@vger.kernel.org Cc: Emily Shaffer Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Since the inspect() helper in the submodule-basic test suite was written, 'git -C ' was added. By using -C, we no longer need a reference to the base directory for the test. This simplifies callsites, and will make the addition of other arguments in later patches more readable. Signed-off-by: Emily Shaffer --- t/t7400-submodule-basic.sh | 40 +++++++++++++++----------------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh index cb1b8e35db..d69a5c0032 100755 --- a/t/t7400-submodule-basic.sh +++ b/t/t7400-submodule-basic.sh @@ -107,23 +107,15 @@ test_expect_success 'setup - repository to add submodules to' ' # generates, which will expand symbolic links. submodurl=$(pwd -P) -listbranches() { - git for-each-ref --format='%(refname)' 'refs/heads/*' -} - inspect() { - dir=$1 && - dotdot="${2:-..}" && - - ( - cd "$dir" && - listbranches >"$dotdot/heads" && - { git symbolic-ref HEAD || :; } >"$dotdot/head" && - git rev-parse HEAD >"$dotdot/head-sha1" && - git update-index --refresh && - git diff-files --exit-code && - git clean -n -d -x >"$dotdot/untracked" - ) + sub_dir=$1 && + + git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads && + { git -C "$sub_dir" symbolic-ref HEAD || :; } >head && + git -C "$sub_dir" rev-parse HEAD >head-sha1 && + git -C "$sub_dir" update-index --refresh && + git -C "$sub_dir" diff-files --exit-code && + git -C "$sub_dir" clean -n -d -x >untracked } test_expect_success 'submodule add' ' @@ -146,7 +138,7 @@ test_expect_success 'submodule add' ' ) && rm -f heads head untracked && - inspect addtest/submod ../.. && + inspect addtest/submod && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -248,7 +240,7 @@ test_expect_success 'submodule add --branch' ' ) && rm -f heads head untracked && - inspect addtest/submod-branch ../.. && + inspect addtest/submod-branch && test_cmp expect-heads heads && test_cmp expect-head head && test_must_be_empty untracked @@ -264,7 +256,7 @@ test_expect_success 'submodule add with ./ in path' ' ) && rm -f heads head untracked && - inspect addtest/dotsubmod/frotz ../../.. && + inspect addtest/dotsubmod/frotz && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -280,7 +272,7 @@ test_expect_success 'submodule add with /././ in path' ' ) && rm -f heads head untracked && - inspect addtest/dotslashdotsubmod/frotz ../../.. && + inspect addtest/dotslashdotsubmod/frotz && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -296,7 +288,7 @@ test_expect_success 'submodule add with // in path' ' ) && rm -f heads head untracked && - inspect addtest/slashslashsubmod/frotz ../../.. && + inspect addtest/slashslashsubmod/frotz && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -312,7 +304,7 @@ test_expect_success 'submodule add with /.. in path' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod ../.. && + inspect addtest/realsubmod && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -328,7 +320,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod2 ../.. && + inspect addtest/realsubmod2 && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -359,7 +351,7 @@ test_expect_success 'submodule add in subdirectory' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod3 ../.. && + inspect addtest/realsubmod3 && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked From patchwork Wed Nov 17 00:56:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emily Shaffer X-Patchwork-Id: 12623381 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C0825C433F5 for ; Wed, 17 Nov 2021 00:57:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A8D866128E for ; Wed, 17 Nov 2021 00:57:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229883AbhKQBAJ (ORCPT ); Tue, 16 Nov 2021 20:00:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229862AbhKQBAI (ORCPT ); Tue, 16 Nov 2021 20:00:08 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 37589C061570 for ; Tue, 16 Nov 2021 16:57:11 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id m78-20020a252651000000b005c1f44d3c7bso1199737ybm.22 for ; Tue, 16 Nov 2021 16:57:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=XlwPBwXJVVPEQqpM3hOUg8HbBOJ4cEXjwakSA6/E4Ms=; b=TqeHAASC0K3F54amrPWb6HqIkPC+WytGg8VAB+H5vJJ6t3CY8Z8oYEKqCWWs99i/0H W0wTw9YNzvIBMGGKpci4MD39W7NARY4tm2zxOsa9UqxKIbT4sNPE2AxLAPKcGKHqViFw AvSi30q9gGB9kx0o5/9mLm74O3Z1o9CaqV/jDtcjrSUqnn+LkwBZ3j3qDfjCT2leGhgR K2MFTliaBYKtpJzXhDQ6T4pxdbjGQlgQrKrqn3gmwy/GPNeMTy7KT2NVWvwoa8hiJgZS GwUIrJN8XDNgIP3s8w58QvpVwMUBhUR8AXGiNTP4eQzzAXBysGVDIa+VQVF7GY8SFhgT 88SQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=XlwPBwXJVVPEQqpM3hOUg8HbBOJ4cEXjwakSA6/E4Ms=; b=0eneL/Nl+V8WqV4YmABEAQfrC3+IotB78dPII7+76zN8HO4hU+mimuyvJek4/Dnf7F UgfEdle0jFEYpYOr6VR0f8pdNCzOjJk4J7OpifiHEDJW3NE8S0ObxJNgXEVhDhNdEak9 +XT4xJeQhpq2ckqkAohmdw4mS8MkaFnqFkRk1Zejvuts3LERCMaW1WOTx0A0NAUUjf1S ztIpKCBg3uQOBF4o4D+KLxRry5x3rHbN62cPm/MPxIwLDw6VpoQQoaob4Nir7CMSh8+B ZAe8FH3xNEmqrkGV03J9s+F4nNytuVmx+7A/k+ePqLZ7VoX0EYLjfZOwkE2wZ9zwTNkg 6Myg== X-Gm-Message-State: AOAM532sv9FrSU9vWbIzlVDbdqmMkc3NdgJhgpnSWnJ4t0o1OciHfDeL 0QA7ksRvB48cjcIBgHhJvN0GbFX91D9Is2+duzcnI0lhaiOV1+kvXszd3KGjz+ZEw9q9X0sxALe 27nK3/o+R2zD6q2VGf7dL52z6CCYM4jO4F6L9uf0RH0AtzEyFtQCjF7K/We9+WZWw4bhL9Xs61g == X-Google-Smtp-Source: ABdhPJzuT5XH4ixYAs2/t8PyGCTgjeSI/YaPGgrUi3aaBRdcPjXY2xxgKebY1fvHoDAc5Dnnov0bT5HnU9oIUl8PCHk= X-Received: from podkayne.svl.corp.google.com ([2620:15c:2ce:200:205d:7edd:a3da:3608]) (user=emilyshaffer job=sendgmr) by 2002:a05:6902:4d3:: with SMTP id v19mr12708150ybs.500.1637110630411; Tue, 16 Nov 2021 16:57:10 -0800 (PST) Date: Tue, 16 Nov 2021 16:56:58 -0800 In-Reply-To: <20211117005701.371808-1-emilyshaffer@google.com> Message-Id: <20211117005701.371808-3-emilyshaffer@google.com> Mime-Version: 1.0 References: <20211117005701.371808-1-emilyshaffer@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v6 2/5] introduce submodule.superprojectGitDir record From: Emily Shaffer To: git@vger.kernel.org Cc: Emily Shaffer , Junio C Hamano Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Teach submodules a reference to their superproject's gitdir. This allows us to A) know that we're running from a submodule, and B) have a shortcut to the superproject's vitals, for example, configs. By using a relative path instead of an absolute path, we can move the superproject directory around on the filesystem without breaking the submodule's pointer. And by using the path from gitdir to gitdir, we can move the submodule within the superproject's tree structure without breaking the submodule's pointer, too. Finally, by pointing at the superproject's worktree gitdir (if it exists), we ensure that we can tell which worktree contains our submodule. Since this hint value is only introduced during new submodule creation via `git submodule add`, though, there is more work to do to allow the record to be created at other times. Once this new config is reliably in place, we can use it to know definitively that we are working in a submodule, and to know which superproject we are a submodule of. This allows us to do some value-added behavior, like letting "git status" print additional info about the submodule's status in relation to its superproject, or like letting the superproject and submodule share an additional config file separate from either one's local config. Signed-off-by: Emily Shaffer Helped-by: Junio C Hamano --- Documentation/config/submodule.txt | 12 +++++++++++ builtin/submodule--helper.c | 11 ++++++++++ t/t7400-submodule-basic.sh | 32 ++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Documentation/config/submodule.txt b/Documentation/config/submodule.txt index ee454f8126..61d975745e 100644 --- a/Documentation/config/submodule.txt +++ b/Documentation/config/submodule.txt @@ -91,3 +91,15 @@ submodule.alternateErrorStrategy:: `ignore`, `info`, `die`. Default is `die`. Note that if set to `ignore` or `info`, and if there is an error with the computed alternate, the clone proceeds as if no alternate was specified. + +submodule.superprojectGitDir:: + The relative path from the submodule's gitdir to its superproject's + gitdir. When Git is run in a repository, it usually makes no + difference whether this repository is standalone or a submodule, but if + this configuration variable is present, additional behavior may be + possible, such as "git status" printing additional information about + this submodule's status with respect to its superproject. This config + should only be present in projects which are submodules, but is not + guaranteed to be present in every submodule, so only optional + value-added behavior should be linked to it. It is set automatically + during submodule creation. diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index e630f0c730..24f0ef2a78 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -1838,6 +1838,17 @@ static int clone_submodule(struct module_clone_data *clone_data) git_config_set_in_file(p, "submodule.alternateErrorStrategy", error_strategy); + /* + * Set the path from submodule's new gitdir to superproject's gitdir. + * The latter may be a worktree gitdir. However, it is not possible for + * the submodule to have a worktree-specific gitdir or config at clone + * time, because "extensions.worktreeConfig" is only valid when set in + * the local gitconfig, which the brand new submodule does not have yet. + */ + git_config_set_in_file(p, "submodule.superprojectGitDir", + relative_path(absolute_path(get_git_dir()), + sm_gitdir, &sb)); + free(sm_alternate); free(error_strategy); diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh index d69a5c0032..3d146491df 100755 --- a/t/t7400-submodule-basic.sh +++ b/t/t7400-submodule-basic.sh @@ -109,12 +109,24 @@ submodurl=$(pwd -P) inspect() { sub_dir=$1 && + super_dir=$2 && git -C "$sub_dir" for-each-ref --format='%(refname)' 'refs/heads/*' >heads && { git -C "$sub_dir" symbolic-ref HEAD || :; } >head && git -C "$sub_dir" rev-parse HEAD >head-sha1 && git -C "$sub_dir" update-index --refresh && git -C "$sub_dir" diff-files --exit-code && + + # Ensure that submodule.superprojectGitDir contains the path from the + # submodule's gitdir to the superproject's gitdir. + + super_abs_gitdir=$(git -C "$super_dir" rev-parse --absolute-git-dir) && + sub_abs_gitdir=$(git -C "$sub_dir" rev-parse --absolute-git-dir) && + + [ "$(git -C "$sub_dir" config --get submodule.superprojectGitDir)" = \ + "$(test-tool path-utils relative_path "$super_abs_gitdir" \ + "$sub_abs_gitdir")" ] && + git -C "$sub_dir" clean -n -d -x >untracked } @@ -138,7 +150,7 @@ test_expect_success 'submodule add' ' ) && rm -f heads head untracked && - inspect addtest/submod && + inspect addtest/submod addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -240,7 +252,7 @@ test_expect_success 'submodule add --branch' ' ) && rm -f heads head untracked && - inspect addtest/submod-branch && + inspect addtest/submod-branch addtest && test_cmp expect-heads heads && test_cmp expect-head head && test_must_be_empty untracked @@ -256,7 +268,7 @@ test_expect_success 'submodule add with ./ in path' ' ) && rm -f heads head untracked && - inspect addtest/dotsubmod/frotz && + inspect addtest/dotsubmod/frotz addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -272,7 +284,7 @@ test_expect_success 'submodule add with /././ in path' ' ) && rm -f heads head untracked && - inspect addtest/dotslashdotsubmod/frotz && + inspect addtest/dotslashdotsubmod/frotz addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -288,7 +300,7 @@ test_expect_success 'submodule add with // in path' ' ) && rm -f heads head untracked && - inspect addtest/slashslashsubmod/frotz && + inspect addtest/slashslashsubmod/frotz addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -304,7 +316,7 @@ test_expect_success 'submodule add with /.. in path' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod && + inspect addtest/realsubmod addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -320,7 +332,7 @@ test_expect_success 'submodule add with ./, /.. and // in path' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod2 && + inspect addtest/realsubmod2 addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -351,7 +363,7 @@ test_expect_success 'submodule add in subdirectory' ' ) && rm -f heads head untracked && - inspect addtest/realsubmod3 && + inspect addtest/realsubmod3 addtest && test_cmp expect heads && test_cmp expect head && test_must_be_empty untracked @@ -492,7 +504,7 @@ test_expect_success 'update should work when path is an empty dir' ' git submodule update -q >update.out && test_must_be_empty update.out && - inspect init && + inspect init . && test_cmp expect head-sha1 ' @@ -551,7 +563,7 @@ test_expect_success 'update should checkout rev1' ' echo "$rev1" >expect && git submodule update init && - inspect init && + inspect init . && test_cmp expect head-sha1 ' From patchwork Wed Nov 17 00:56:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emily Shaffer X-Patchwork-Id: 12623383 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 44E75C433EF for ; Wed, 17 Nov 2021 00:57:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2D44961BFA for ; Wed, 17 Nov 2021 00:57:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229958AbhKQBAL (ORCPT ); Tue, 16 Nov 2021 20:00:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229906AbhKQBAK (ORCPT ); Tue, 16 Nov 2021 20:00:10 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA3B9C061570 for ; Tue, 16 Nov 2021 16:57:12 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id w7-20020aa78587000000b004a28bb92381so614899pfn.22 for ; Tue, 16 Nov 2021 16:57:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=rSinrMgceDslMqmJ+qn19OGG4hWxi78pYt4JlcuHWfE=; b=Z3+wndM3sBdGVWtyvN5MglRMV9feBXzSRWCG0/68SDiLB7yMYgAa8UDOv2gtB53TVi Lhh6NYAZlz7MkXcMZHlqklJ2T6BUZfwxlmYlEsqHPitk0Brd4rX84G9wSXnLsuoEypH5 /Bac20bLnz+c4wuOG0++i+83K1o3K8/vbl6lLpIB9khKEvMx/28gXst+9yEcuGvv6BWL 7NpvGX6tdLzbwXjWwD5sw46BnmNmahL/NsPjPHlLMnkCiL5KYR2gEBCH31XplCM0uANC mP2lkDBownaJkX3GwaeGOMlvmQe+p5M2gMw1o3qYWZwJQF4yk/8g0gPQxoYTOT0cM0/K 8cDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=rSinrMgceDslMqmJ+qn19OGG4hWxi78pYt4JlcuHWfE=; b=fCybcE+JaVawT3ufgi7efuqBc7Qa4fiv2ZN7bsjdaR4c/zdVhT5t70dY1R23pSzY5O IUmRe1HUUTR/nE/B+wdF2D8X4S69dxzYKej/MdNjGbcFagt1WxVep0RFLvBWvcMn5Are FiDyNQzBcI1qSBVQx2AjMnXKb3nFm9Wq90lGaKQuqGg3tv738SMCJ4HVR6SdxNSc2QPU LI30D1og9xgb/S2ecZCYaGIJSVBOP7peJhHbUBnjkCavw8az/NBSMzANa8x76aq7CVau ucGhYAPSX8TCc+RFiT9yp0fdlqQS2Gv81oa1ljzQWypYzg32F3NMqxLv7W6ve6sQSCd8 5XvQ== X-Gm-Message-State: AOAM5321qpwze7mDwOkeB42OdALTDE67jM4wWCaLvOJz3KnITJWeoIza rFzO7yuoFqa+gRMPYagIYIWC4y9MhCYjINPsn7vNSpMYOolpf3eBgdSAIIC8VYxS6hJUoZm4uVJ XF203KUUUlynA0l2CVXuZ/89dCb9jfAeiMF82fG9IOqCdgbs/fJKNEeyeOw2j9bZBoHUPPoYh1A == X-Google-Smtp-Source: ABdhPJyUDtApRb6ZQUx+mJXv+ohkIVXc1h0uoqBvxxyeiKfUY8VtdkDxOmTeFooc06vyUqInCvEDb0SrCtjzLa35TNg= X-Received: from podkayne.svl.corp.google.com ([2620:15c:2ce:200:205d:7edd:a3da:3608]) (user=emilyshaffer job=sendgmr) by 2002:a17:90b:1b4b:: with SMTP id nv11mr4348277pjb.131.1637110632346; Tue, 16 Nov 2021 16:57:12 -0800 (PST) Date: Tue, 16 Nov 2021 16:56:59 -0800 In-Reply-To: <20211117005701.371808-1-emilyshaffer@google.com> Message-Id: <20211117005701.371808-4-emilyshaffer@google.com> Mime-Version: 1.0 References: <20211117005701.371808-1-emilyshaffer@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v6 3/5] submodule: record superproject gitdir during absorbgitdirs From: Emily Shaffer To: git@vger.kernel.org Cc: Emily Shaffer Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Already during 'git submodule add' we record a pointer to the superproject's gitdir. However, this doesn't help brand-new submodules created with 'git init' and later absorbed with 'git submodule absorbgitdirs'. Let's start adding that pointer during 'git submodule absorbgitdirs' too. Signed-off-by: Emily Shaffer --- submodule.c | 23 +++++++++ t/t7412-submodule-absorbgitdirs.sh | 82 +++++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/submodule.c b/submodule.c index c689070524..d7395c7551 100644 --- a/submodule.c +++ b/submodule.c @@ -2097,6 +2097,9 @@ static void relocate_single_git_dir_into_superproject(const char *path) char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL; struct strbuf new_gitdir = STRBUF_INIT; const struct submodule *sub; + struct config_set sub_cs; + struct strbuf config_path = STRBUF_INIT, sb = STRBUF_INIT; + int tmp; if (submodule_uses_worktrees(path)) die(_("relocate_gitdir for submodule '%s' with " @@ -2127,6 +2130,26 @@ static void relocate_single_git_dir_into_superproject(const char *path) relocate_gitdir(path, real_old_git_dir, real_new_git_dir); + /* + * Note location of superproject's gitdir. Because the submodule already + * has a gitdir and local config, we can store this pointer from + * worktree config to worktree config, if the submodule has + * extensions.worktreeConfig set. + */ + strbuf_addf(&config_path, "%s/config", real_new_git_dir); + git_configset_init(&sub_cs); + git_configset_add_file(&sub_cs, config_path.buf); + /* return 0 indicates config was found - we have a worktree config */ + if (!git_configset_get_bool(&sub_cs, "extensions.worktreeConfig", &tmp)) + strbuf_addstr(&config_path, ".worktree"); + + git_config_set_in_file(config_path.buf, "submodule.superprojectGitdir", + relative_path(absolute_path(get_git_dir()), + real_new_git_dir, &sb)); + + git_configset_clear(&sub_cs); + strbuf_release(&config_path); + strbuf_release(&sb); free(old_git_dir); free(real_old_git_dir); free(real_new_git_dir); diff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh index 1cfa150768..5753f90268 100755 --- a/t/t7412-submodule-absorbgitdirs.sh +++ b/t/t7412-submodule-absorbgitdirs.sh @@ -30,7 +30,17 @@ test_expect_success 'absorb the git dir' ' git status >actual.1 && git -C sub1 rev-parse HEAD >actual.2 && test_cmp expect.1 actual.1 && - test_cmp expect.2 actual.2 + test_cmp expect.2 actual.2 && + + # make sure the submodule cached the superproject gitdir correctly + submodule_gitdir="$(git -C sub1 rev-parse --path-format=absolute --git-common-dir)" && + superproject_gitdir="$(git rev-parse --path-format=absolute --git-common-dir)" && + + test-tool path-utils relative_path "$superproject_gitdir" \ + "$submodule_gitdir" >expect && + git -C sub1 config submodule.superprojectGitDir >actual && + + test_cmp expect actual ' test_expect_success 'absorbing does not fail for deinitialized submodules' ' @@ -61,7 +71,16 @@ test_expect_success 'absorb the git dir in a nested submodule' ' git status >actual.1 && git -C sub1/nested rev-parse HEAD >actual.2 && test_cmp expect.1 actual.1 && - test_cmp expect.2 actual.2 + test_cmp expect.2 actual.2 && + + sub1_gitdir="$(git -C sub1 rev-parse --path-format=absolute --git-common-dir)" && + sub1_nested_gitdir="$(git -C sub1/nested rev-parse --path-format=absolute --git-common-dir)" && + + test-tool path-utils relative_path "$sub1_gitdir" "$sub1_nested_gitdir" \ + >expect && + git -C sub1/nested config submodule.superprojectGitDir >actual && + + test_cmp expect actual ' test_expect_success 're-setup nested submodule' ' @@ -130,4 +149,63 @@ test_expect_success 'absorbing fails for a submodule with multiple worktrees' ' test_i18ngrep "not supported" error ' +test_expect_success 'absorbgitdirs works when called from a superproject worktree' ' + # set up a worktree of the superproject + git worktree add wt && + ( + cd wt && + + # create a new unembedded git dir + git init sub4 && + test_commit -C sub4 first && + git submodule add ./sub4 && + test_tick && + + # absorb the git dir + git submodule absorbgitdirs sub4 && + + # make sure the submodule noted the superproject gitdir correctly + submodule_gitdir="$(git -C sub4 rev-parse --absolute-git-dir)" && + superproject_gitdir="$(git rev-parse --absolute-git-dir)" && + + test-tool path-utils relative_path "$superproject_gitdir" \ + "$submodule_gitdir" >expect && + git -C sub4 config submodule.superprojectGitDir >actual && + + test_cmp expect actual + ) +' + +test_expect_success 'absorbgitdirs works with a submodule with worktree config' ' + # reuse the worktree of the superproject + ( + cd wt && + + # create a new unembedded git dir + git init sub5 && + test_commit -C sub5 first && + git submodule add ./sub5 && + test_tick && + + # turn on worktree configs for submodule + git -C sub5 config extensions.worktreeConfig true && + + # absorb the git dir + git submodule absorbgitdirs sub5 && + + # make sure the submodule noted the superproject gitdir correctly + submodule_gitdir="$(git -C sub5 rev-parse --absolute-git-dir)" && + superproject_gitdir="$(git rev-parse --absolute-git-dir)" && + + test-tool path-utils relative_path "$superproject_gitdir" \ + "$submodule_gitdir" >expect && + git -C sub5 config submodule.superprojectGitDir >actual && + + test_cmp expect actual && + + # make sure the config went into the submodule config.worktree + test_file_not_empty "$submodule_gitdir/config.worktree" + ) +' + test_done From patchwork Wed Nov 17 00:57:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emily Shaffer X-Patchwork-Id: 12623385 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1BDAC433EF for ; Wed, 17 Nov 2021 00:57:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE2C46128E for ; Wed, 17 Nov 2021 00:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230126AbhKQBAP (ORCPT ); Tue, 16 Nov 2021 20:00:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229983AbhKQBAM (ORCPT ); Tue, 16 Nov 2021 20:00:12 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BECEDC061746 for ; Tue, 16 Nov 2021 16:57:14 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id lj10-20020a17090b344a00b001a653d07ad8so2157191pjb.3 for ; Tue, 16 Nov 2021 16:57:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=V3qykwMN4wHWktrVL99TQAqGyHQLsaQkqRKTO9qhMYM=; b=tV4DkTnNsD3XeaDQjRE/h1A+QO8srrHwDpaNv9hOMUddgQeYD2KDns6FctKYoGTPS4 uFzEiMhhj5hiA5jdz3mJvh8pIyZnCXS+F6dYrvJ20ytyrGfH0Qc9m7AeVpsVZFu6NjWN 4Mm4YKbcTe123a8Pmtjg2sAuuNhTmt1OjzeeVszVecnyOJqmL+h4+HB4/oZ0hQp2Ocma quAfVQA5xnvwAy1nW4+4JCbt9CjDMDWGWTZEWSftgfzpovp9sZRM+mDI/PSILWNBzDcg kpxAI9SbQN23HoXyb50UvN5CRhUcFg34eYzQ93dOUcOo4Zx4faQce0NiKL4vs/hovOFy Eb3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=V3qykwMN4wHWktrVL99TQAqGyHQLsaQkqRKTO9qhMYM=; b=4B6P8lOe1vC3xl3uMTwxqIKtjSCMHlQwLBRkXq5H7S2fPk7s90w6KNthRH0pm3tpIv VZ+D1m5n2anvft5u6yOO60T7Pl7RpYtiV/04X+dCMP9L41DVIFsVBqARgqL23BwTLxYt 3OqVZjwz+R/MBfjde2pKjpsoRe71nNVXsYYdYS71/yoiv3XsvlkG+FMmTgiDfxe45nYZ mTEM6sdn+VlgaaCRKsd7X4BZ/NE3bEIl3Ok/3iPmSVM19hhoPgeMqx8qIrru+qeyg5g5 4GZkA1UT3SsFbgxZlO8hUsKeV7KnbiMOoonexjouswRR6F39ptbJ3lQF6FU8F6bQUSyF B4lw== X-Gm-Message-State: AOAM531bLWezf/HobJSm86f2zFpFtqPv8ujaPhReOZlRsNID5o0c42i1 wtE+u/Xxaabimc9F+zzxdurf9pLkfh6jPY9YCPXg7YK7uAfhnJQp/Wh5tXaoJvR46KcCygriFMw +UuXocCfk/3ZEM1KGgKF0U8uQhShQjMuiabyWvMMUCfMAHE31Z3gmGBFBkXO14o1zzGnHGkHPlg == X-Google-Smtp-Source: ABdhPJwCR6zJmuPNKyTubsx6fItNPNK1qeHLYB5HIFCn84TSHes4Da+YxMA7lrfJe7Al0bulJp+a+Lf1Vvh2UzB2Anc= X-Received: from podkayne.svl.corp.google.com ([2620:15c:2ce:200:205d:7edd:a3da:3608]) (user=emilyshaffer job=sendgmr) by 2002:a17:90a:bf0b:: with SMTP id c11mr4197208pjs.208.1637110634205; Tue, 16 Nov 2021 16:57:14 -0800 (PST) Date: Tue, 16 Nov 2021 16:57:00 -0800 In-Reply-To: <20211117005701.371808-1-emilyshaffer@google.com> Message-Id: <20211117005701.371808-5-emilyshaffer@google.com> Mime-Version: 1.0 References: <20211117005701.371808-1-emilyshaffer@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v6 4/5] submodule: record superproject gitdir during 'update' From: Emily Shaffer To: git@vger.kernel.org Cc: Emily Shaffer Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org A recorded path to the superproject's gitdir might be added during 'git submodule add', but in some cases - like submodules which were created before 'git submodule add' learned to record that info - it might be useful to update the hint. Let's do it during 'git submodule update', when we already have a handle to the superproject while calling operations on the submodules. Signed-off-by: Emily Shaffer --- git-submodule.sh | 15 +++++++++++++++ t/t7406-submodule-update.sh | 27 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/git-submodule.sh b/git-submodule.sh index 652861aa66..7c247bee7f 100755 --- a/git-submodule.sh +++ b/git-submodule.sh @@ -449,6 +449,21 @@ cmd_update() ;; esac + # Store a poitner to the superproject's gitdir. This may have + # changed, unless it's a fresh clone. Write to worktree if + # applicable, and point to superproject's worktree gitdir if + # applicable. + if test -z "$just_cloned" + then + sm_gitdir="$(git -C "$sm_path" rev-parse --absolute-git-dir)" + relative_gitdir="$(git rev-parse --path-format=relative \ + --prefix "${sm_gitdir}" \ + --git-dir)" + + git -C "$sm_path" config --worktree \ + submodule.superprojectgitdir "$relative_gitdir" + fi + if test -n "$recursive" then ( diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh index 11cccbb333..b42a339982 100755 --- a/t/t7406-submodule-update.sh +++ b/t/t7406-submodule-update.sh @@ -1061,4 +1061,31 @@ test_expect_success 'submodule update --quiet passes quietness to fetch with a s ) ' +test_expect_success 'submodule update adds superproject gitdir to older repos' ' + (cd super && + git -C submodule config --unset submodule.superprojectGitdir && + git submodule update && + test-tool path-utils relative_path \ + "$(git rev-parse --absolute-git-dir)" \ + "$(git -C submodule rev-parse --absolute-git-dir)" >expect && + git -C submodule config submodule.superprojectGitdir >actual && + test_cmp expect actual + ) +' + +test_expect_success 'submodule update uses config.worktree if applicable' ' + (cd super && + git -C submodule config --unset submodule.superprojectGitDir && + git -C submodule config extensions.worktreeConfig true && + git submodule update && + test-tool path-utils relative_path \ + "$(git rev-parse --absolute-git-dir)" \ + "$(git -C submodule rev-parse --absolute-git-dir)" >expect && + git -C submodule config submodule.superprojectGitdir >actual && + test_cmp expect actual && + + test_file_not_empty "$(git -C submodule rev-parse --absolute-git-dir)/config.worktree" + ) +' + test_done From patchwork Wed Nov 17 00:57:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emily Shaffer X-Patchwork-Id: 12623387 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 52187C433FE for ; Wed, 17 Nov 2021 00:57:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2971661BFA for ; Wed, 17 Nov 2021 00:57:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230141AbhKQBAQ (ORCPT ); Tue, 16 Nov 2021 20:00:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41888 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230033AbhKQBAN (ORCPT ); Tue, 16 Nov 2021 20:00:13 -0500 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 699BAC061570 for ; Tue, 16 Nov 2021 16:57:16 -0800 (PST) Received: by mail-pf1-x449.google.com with SMTP id w7-20020aa78587000000b004a28bb92381so615091pfn.22 for ; Tue, 16 Nov 2021 16:57:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=11Xxzf5yVdB60QZKKeSbKLqofXA3z/c5/STQ0C+V378=; b=UyrYQRBrJmhESIIOwZvr71LdprK/psLxjqiClP/kMHzVmceuK7q2QwNNETxz6NSbaL kHJCJSOZ6WW3igGu3AJfzjf18Pok3QzOx08wyWPxbfJJcmlShPnIaBbWmQH653K861MK NcFFnOF4CVGyJa/Ko27fIuPJ/VKdMYDmp5cWyjWAyBbfTW2WdYvujcKSwWmEXO3yRon5 Y/NVtuUgJhbgIhhAHI7tRPT7QIMCmOP8Y4y6Jgwj3Xq4WCA9XImuYsLyjYMhFaBXAD3I /fZky8dVEVx2kOFOmbNr+tj7q3bXGXwBjguCrcXn01x8XkCKS2T3U+aPbIie9JA938J+ qvJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=11Xxzf5yVdB60QZKKeSbKLqofXA3z/c5/STQ0C+V378=; b=j3MsURGK38sAZqsaAKMQS3mbgmimn1/wIjo1v6gwvn6KTpVwmN1WjXaB/52FLH2mKk omUWJ79KY32a7yTfzYVZ0W2FPuWLEPqR47eEycjVo1deC+8TDyhH0wrxDNZGKaT4nDKU JwkTvQF0FlyuXxRrvYEteixwUcr/glfKF5QxknKuQUnr8XcKy4kRrTVNQkvJRkArfejJ IRfzMauTCBcFVNb6GFnOhcbBSZEg3/50QM+QV8TGIrJpFYfeUvsB5ttnmCNx9g8NCanc dlfNwlCH/8LZI5ZexwuHVTisH1HK2UqB7KNsVe9pmaf7zcWD7LIw/tqcEJYOZK9PJEEG pmHQ== X-Gm-Message-State: AOAM532c4gBmwJuHs+D3Usr0SmDz283sfdRahmwVCPWy6JOwHU7BPect 3LzmpzZySSpibQtgykN7ZfHe5Va2ZvtQGj9HYDfUnszkqXA8DtNK/bHMts5ESms9B8LkMnkXNNO jjzNP7guLKKJOrqdWaSQtJfDAiygBDvPmmMAlrJFqpjt6WZI/sidpw7FGhUEeWb0ct+JLSC3XXQ == X-Google-Smtp-Source: ABdhPJx/GVHgTrm/xWBzYj0zIcvs2gVhVjTHU7tE0FqFNio4XlW19Mby/nETToEF3GZ101cp/L7R38rPm7XiQTdXhAA= X-Received: from podkayne.svl.corp.google.com ([2620:15c:2ce:200:205d:7edd:a3da:3608]) (user=emilyshaffer job=sendgmr) by 2002:a17:903:283:b0:142:1243:d879 with SMTP id j3-20020a170903028300b001421243d879mr50936962plr.61.1637110635864; Tue, 16 Nov 2021 16:57:15 -0800 (PST) Date: Tue, 16 Nov 2021 16:57:01 -0800 In-Reply-To: <20211117005701.371808-1-emilyshaffer@google.com> Message-Id: <20211117005701.371808-6-emilyshaffer@google.com> Mime-Version: 1.0 References: <20211117005701.371808-1-emilyshaffer@google.com> X-Mailer: git-send-email 2.34.0.rc1.387.gb447b232ab-goog Subject: [PATCH v6 5/5] submodule: use config to find superproject worktree From: Emily Shaffer To: git@vger.kernel.org Cc: Emily Shaffer Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Now that submodule.superprojectGitDir is being treated as the point of truth for whether a repo is a submodule or not, let's use it in `git rev-parse --show-superproject-working-tree`. Signed-off-by: Emily Shaffer --- This commit may be more of an RFC - to demonstrate what life looks like if we use submodule.superprojectGitDir as the source of truth. But since 'git rev-parse --show-superproject-working-tree' is used in a lot of scripts in the wild[1], I'm not so sure it's a great example. To be honest, I'd prefer to die("Try running 'git submodule update'") here, but I don't think that's very script-friendly. However, falling back on the old implementation kind of undermines the idea of treating submodule.superprojectGitDir as the point of truth. One thought I did have was to put that error message in builtin/rev-parse.c instead, and print it to stderr (per usual with user-visible messages) so it doesn't interfere with scripts, but gives a hint for debugging. Another thought - captured by the NEEDSWORK in the diff - was that we could "heal" by adding that config after we know the worktree of the superproject. Or, it could be that it won't be a problem for a long time, as anybody running 'git submodule update' will eventually have that config specified - that's why I included the traces, so we could try and get an understanding of how long repos remain in this state where they have submodules but nobody ran 'git submodule update'. But that will only give me visibility into submodule users at Google, who we expect to be making a lot of workflow changes soon, anyway. 1: https://github.com/search?q=%22--show-superproject-working-tree%22&type=code --- submodule.c | 85 +++++++++++++++++++++++++++++++++++++++++++- t/t1500-rev-parse.sh | 9 +++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/submodule.c b/submodule.c index d7395c7551..ad95cdda07 100644 --- a/submodule.c +++ b/submodule.c @@ -2236,7 +2236,7 @@ void absorb_git_dir_into_superproject(const char *path, } } -int get_superproject_working_tree(struct strbuf *buf) +static int get_superproject_working_tree_from_fs(struct strbuf *buf) { struct child_process cp = CHILD_PROCESS_INIT; struct strbuf sb = STRBUF_INIT; @@ -2320,6 +2320,89 @@ int get_superproject_working_tree(struct strbuf *buf) return ret; } +int get_superproject_working_tree(struct strbuf *buf) +{ + char *super_gitdir = NULL; + const char *cwd = xgetcwd(); + struct child_process cp = CHILD_PROCESS_INIT; + struct strbuf absolute_super_gitdir = STRBUF_INIT; + struct strbuf out = STRBUF_INIT; + struct string_list lines = STRING_LIST_INIT_NODUP; + struct string_list_item *it = NULL; + const char *wt_prefix = "worktree "; + int rc = 0; + + + /* Do we know we have a superproject? */ + if (git_config_get_string("submodule.superprojectgitdir", &super_gitdir)) + goto fallback; + + strbuf_addf(&absolute_super_gitdir, "%s/%s", get_git_dir(), super_gitdir); + + /* + * NEEDSWORK: This is a child process call because worktree.c still + * relies heavily on the_repository. If we can make worktree.c work with + * a repository object - or, better yet, a gitdir path alone - then we + * can drop the child process and ask worktree.c directly. + * + * Alternatively, if 'git worktree' learns a way to say 'the worktree + * associated with this gitdir' instead of 'all worktrees', that would + * be clearer because we could skip the foreach below. + */ + + /* Get the output of `git worktree list` from that superproject */ + prepare_other_repo_env(&cp.env_array, absolute_super_gitdir.buf); + strvec_pushl(&cp.args, "-C", absolute_super_gitdir.buf, "worktree", "list", + "--porcelain", NULL); + + cp.git_cmd = 1; + if (capture_command(&cp, &out, 0) || + !string_list_split_in_place(&lines, out.buf, '\n', -1)) + die("submodule.superprojectGitDir is stale; run 'git submodule " + "update' from the superproject."); + + for_each_string_list_item(it, &lines) { + char *trimmed; + /* + * Lines containing worktree dirs look like + * 'worktree /some/path' + */ + if (strncmp(it->string, wt_prefix, strlen(wt_prefix))) + continue; + trimmed = it->string + strlen(wt_prefix); + + /* + * '/some/path/to/sub' is a prefix of '/some/path' - that's our + * worktree + */ + if (!strncmp(cwd, trimmed, strlen(trimmed))) { + strbuf_addstr(buf, trimmed); + rc = 1; + goto cleanup; + } + } + +fallback: + /* + * Because a submodule may have been created before + * submodule.superprojectGitDir was introduced, fall back on checking + * whether ../ is the superproject. + */ + trace2_data_intmax("submodule", the_repository, + "get_superproject_wt/config_missing", rc); + rc = get_superproject_working_tree_from_fs(buf); + + /* + * NEEDSWORK: Is it possible to teach a submodule the path to its + * superproject when this happens? + */ + +cleanup: + string_list_clear(&lines, 0); + strbuf_release(&out); + return rc; +} + /* * Put the gitdir for a submodule (given relative to the main * repository worktree) into `buf`, or return -1 on error. diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index 1c2df08333..e569910289 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -247,6 +247,15 @@ test_expect_success 'showing the superproject correctly' ' test_cmp expect out ' +test_expect_success 'show the superproject correctly without superprojectGitDir' ' + # repos created before submodule.superprojectGitDir was introduced which + # have not been `git submodule update`-ed lately must not break + git -C super/dir/sub config --unset submodule.superprojectGitDir && + echo $(pwd)/super >expect && + git -C super/dir/sub rev-parse --show-superproject-working-tree >out && + test_cmp expect out +' + # at least one external project depends on this behavior: test_expect_success 'rev-parse --since= unsqueezed ordering' ' x1=--since=1970-01-01T00:00:01Z &&