From patchwork Mon Feb 7 21:32:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737905 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03427C433FE for ; Mon, 7 Feb 2022 21:33:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240952AbiBGVdK (ORCPT ); Mon, 7 Feb 2022 16:33:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53620 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240717AbiBGVdJ (ORCPT ); Mon, 7 Feb 2022 16:33:09 -0500 Received: from mail-wm1-x32e.google.com (mail-wm1-x32e.google.com [IPv6:2a00:1450:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 001C6C0612A4 for ; Mon, 7 Feb 2022 13:33:07 -0800 (PST) Received: by mail-wm1-x32e.google.com with SMTP id l123-20020a1c2581000000b0037b9d960079so240972wml.0 for ; Mon, 07 Feb 2022 13:33:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=KICq9YcJdV4ejpfTBK+yosGiFs85PyzWOYs09u9Tdqg=; b=EmVa1tMtiBMjww2peoMjJPodqLdoOzTDcx3rKNqUFEO8SNbr1t5/5G6i87yUV1YYdk QGRTgXZR1eX+Vs40qJy7WGh/MVrTCVSXX5eHnS7kMHyF6zW5+RatF8WHsY5LF4q+cLFc oKPzxfBNd19njLtXDPggtBbaI47BnaSQ4QD8QRxOhM4N4XwOvwMYZwaVQAB/uV3XjwuY EmWIUlhIZ6pEqz6137oJJl5wv2leRptz6tkhtIoNwenIcn2UR9Oi3RbbDyNz+B6vBqH7 RvXC20+EFGaJxjXZvhrTnJAeQXFUWvQX9d89874NCIZD1LPt2XvDaBv1THPS3k2Wk8+P py2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=KICq9YcJdV4ejpfTBK+yosGiFs85PyzWOYs09u9Tdqg=; b=gfvmg7kluQq4A3jZn8a3seS3kIlu6nXursTOiPfHSOYlmro79Y7fKcpd7ElojE7If2 bM++CRSkLkTEsmg6frKz4lP8esPEAKSbh5i7oI8sBwnzNrYZm4niZogAjH1zmOUSA+oc +53O0ZS+gyJkm/dIF8BfAzp05TkillHWGNcFqZUVt29QkRTdR1gGMpopiM7MNx/Lul57 mLffZ4UkGVsegAOnjpmjV06WqgIgUpL2HJ7Uyg2eD21hAtbbsIVa+nGm2qgmwLDSbJ8L YjCxFR4kvJHPWxdTxZIVjSfy1DEy61232PhkKu5Be8NPlQlTShM+dUFxFV+1vKPo+MsR o4LQ== X-Gm-Message-State: AOAM531KBxVKr58eSUkaUk1lPxtxQeIRlGjXqFVpmIDtyxOjXeq/FY0F 6j7qFsQJXP7hDaYeJzBT1UtaPEFikV4= X-Google-Smtp-Source: ABdhPJzPxoKcJJ+hLYrY74zqJcScxVqNsdih0vpJOwNcpJMhZoSkyr/SKaxPj4urRhDj5mqTWAXiHA== X-Received: by 2002:a1c:a552:: with SMTP id o79mr668920wme.40.1644269586346; Mon, 07 Feb 2022 13:33:06 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id w6sm9325850wrp.51.2022.02.07.13.33.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:05 -0800 (PST) Message-Id: <0260ff6cac0c76c6d66187d77defef1edd4c6fb5.1644269583.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 07 Feb 2022 21:32:58 +0000 Subject: [PATCH v6 1/6] Documentation: add extensions.worktreeConfig details Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The extensions.worktreeConfig extension was added in 58b284a (worktree: add per-worktree config files, 2018-10-21) and was somewhat documented in Documentation/git-config.txt. However, the extensions.worktreeConfig value was not specified further in the list of possible config keys. The location of the config.worktree file is not specified, and there are some precautions that should be mentioned clearly, but are only mentioned in git-worktree.txt. Expand the documentation to help users discover the complexities of extensions.worktreeConfig by adding details and cross links in these locations (relative to Documentation/): - config/extensions.txt - git-config.txt - git-worktree.txt The updates focus on items such as * $GIT_DIR/config.worktree takes precedence over $GIT_COMMON_DIR/config. * The core.worktree and core.bare=true settings are incorrect to have in the common config file when extensions.worktreeConfig is enabled. * The sparse-checkout settings core.sparseCheckout[Cone] are recommended to be set in the worktree config. As documented in 11664196ac ("Revert "check_repository_format_gently(): refuse extensions for old repositories"", 2020-07-15), this extension must be considered regardless of the repository format version for historical reasons. A future change will update references to extensions.worktreeConfig within git-sparse-checkout.txt, but a behavior change is needed before making those updates. Helped-by: Elijah Newren Signed-off-by: Derrick Stolee --- Documentation/config/extensions.txt | 31 +++++++++++++++++++++++++++++ Documentation/git-config.txt | 8 ++++++-- Documentation/git-worktree.txt | 11 +++++++--- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/Documentation/config/extensions.txt b/Documentation/config/extensions.txt index 4e23d73cdca..bccaec7a963 100644 --- a/Documentation/config/extensions.txt +++ b/Documentation/config/extensions.txt @@ -6,3 +6,34 @@ extensions.objectFormat:: Note that this setting should only be set by linkgit:git-init[1] or linkgit:git-clone[1]. Trying to change it after initialization will not work and will produce hard-to-diagnose issues. + +extensions.worktreeConfig:: + If enabled, then worktrees will load config settings from the + `$GIT_DIR/config.worktree` file in addition to the + `$GIT_COMMON_DIR/config` file. Note that `$GIT_COMMON_DIR` and + `$GIT_DIR` are the same for the main working tree, while other + working trees have `$GIT_DIR` equal to + `$GIT_COMMON_DIR/worktrees//`. The settings in the + `config.worktree` file will override settings from any other + config files. ++ +When enabling `extensions.worktreeConfig`, you must be careful to move +certain values from the common config file to the main working tree's +`config.worktree` file, if present: ++ +* `core.worktree` must be moved from `$GIT_COMMON_DIR/config` to + `$GIT_COMMON_DIR/config.worktree`. +* If `core.bare` is true, then it must be moved from `$GIT_COMMON_DIR/config` + to `$GIT_COMMON_DIR/config.worktree`. ++ +It may also be beneficial to adjust the locations of `core.sparseCheckout` +and `core.sparseCheckoutCone` depending on your desire for customizable +sparse-checkout settings for each worktree. By default, the `git +sparse-checkout` builtin enables `extensions.worktreeConfig`, assigns +these config values on a per-worktree basis, and uses the +`$GIT_DIR/info/sparse-checkout` file to specify the sparsity for each +worktree independently. See linkgit:git-sparse-checkout[1] for more +details. ++ +For historical reasons, `extensions.worktreeConfig` is respected +regardless of the `core.repositoryFormatVersion` setting. diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index 2285effb363..bdcfd94b642 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -141,9 +141,13 @@ from all available files. See also <>. --worktree:: - Similar to `--local` except that `.git/config.worktree` is + Similar to `--local` except that `$GIT_DIR/config.worktree` is read from or written to if `extensions.worktreeConfig` is - present. If not it's the same as `--local`. + enabled. If not it's the same as `--local`. Note that `$GIT_DIR` + is equal to `$GIT_COMMON_DIR` for the main working tree, but is of + the form `$GIT_DIR/worktrees//` for other working trees. See + linkgit:git-worktree[1] to learn how to enable + `extensions.worktreeConfig`. -f :: --file :: diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt index 9e862fbcf79..b8d53c48303 100644 --- a/Documentation/git-worktree.txt +++ b/Documentation/git-worktree.txt @@ -286,8 +286,8 @@ CONFIGURATION FILE ------------------ By default, the repository `config` file is shared across all working trees. If the config variables `core.bare` or `core.worktree` are -already present in the config file, they will be applied to the main -working trees only. +present in the common config file and `extensions.worktreeConfig` is +disabled, then they will be applied to the main working tree only. In order to have configuration specific to working trees, you can turn on the `worktreeConfig` extension, e.g.: @@ -307,11 +307,16 @@ them to the `config.worktree` of the main working tree. You may also take this opportunity to review and move other configuration that you do not want to share to all working trees: - - `core.worktree` and `core.bare` should never be shared + - `core.worktree` should never be shared. + + - `core.bare` should not be shared if the value is `core.bare=true`. - `core.sparseCheckout` is recommended per working tree, unless you are sure you always use sparse checkout for all working trees. +See the documentation of `extensions.worktreeConfig` in +linkgit:git-config[1] for more details. + DETAILS ------- Each linked working tree has a private sub-directory in the repository's From patchwork Mon Feb 7 21:32:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737906 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4D419C433F5 for ; Mon, 7 Feb 2022 21:33:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241065AbiBGVdN (ORCPT ); Mon, 7 Feb 2022 16:33:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240896AbiBGVdJ (ORCPT ); Mon, 7 Feb 2022 16:33:09 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D09DDC061A73 for ; Mon, 7 Feb 2022 13:33:08 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id m14so27098062wrg.12 for ; Mon, 07 Feb 2022 13:33:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=D3bNaELIYHVf+nC7o47wgpqMZkoLpv+hq4KgoP47X2w=; b=NCeBzv/iRYFDJfCqRMDJbFvRL/uP3DXjHzAsurjoJxcQDdgNcOu2owOpCmQASBrZt0 /k5+mVy5vbPP90mFvKzpuLUOUAzOWZbJWmImSE2/khD1VPFOxnTQIIKTpD2w7HeRm+IQ 9HRN1OYH0a0HH7H83qA0tfPe84Oedg/pu6z+ImSZQeMsyAGGYyLQ9Nn3b/eQ2xxuaMyW 0l+hnFxqKqOPe6VuYGwuy7RW0u9eKUlXWh+ITvmnCcghgWgTHa18sEAdB4nXENJrFNIu MExbBIvYtoBxCieyxKBouprJelBU+wIGY9Z764xCMm3/LbYrkAXrGNqv1D54mC6t7IDZ Ra4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=D3bNaELIYHVf+nC7o47wgpqMZkoLpv+hq4KgoP47X2w=; b=8QXd+gMtooDYTWYdq037rlrfzQiJZPZh+ra3nywTHNVDpbj20PMWil+82kvc+NebCQ gsDWETwDHZtbtMmMYqKS8EkqXsfHX8aVzJRr88PxP+xZWZG0l7yRygG2GuwFT37gNCjR GDrWZy2SwmZyPuDwVVNhHeR6qiPIkYzwPpWhre0C0HlKuyMo36q5HPA5N5SgnxyZrp00 lRjyeoiGjPK84XwTASu0G6W2m0ZyJqIRKN67QGiPvk2jOraPMXtIHCTboSfn2+jqRbgI fKsP4TROqIvI5yyj3j9Etv64bBq2VL/IznNFZv+IPU8+KoJiKYFJwALEFXzG5s2DNNPa 8mGw== X-Gm-Message-State: AOAM532hcgKddwAiHOwkzazQxDlXv5XkFOwS9bN6hVKLF5sGBiSh3aaN XS38xoH+4x17FNLxUNc2TZ+rRSc5RCI= X-Google-Smtp-Source: ABdhPJxZmUFqpwVSTWO8XyMpJq6WmL0Dy3SNvlTyH/WqMBzPJVjMm/Mk3BOZWJ4eJfZe5BrKhZZvTw== X-Received: by 2002:a05:6000:4:: with SMTP id h4mr1031713wrx.336.1644269587185; Mon, 07 Feb 2022 13:33:07 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c13sm11080607wrv.24.2022.02.07.13.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:06 -0800 (PST) Message-Id: <5d0cc242d92c68bf239f9e17eab9c80ec6b2d469.1644269583.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 07 Feb 2022 21:32:59 +0000 Subject: [PATCH v6 2/6] worktree: create init_worktree_config() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee Upgrading a repository to use extensions.worktreeConfig is non-trivial. There are several steps involved, including moving some config settings from the common config file to the main worktree's config.worktree file. The previous change updated the documentation with all of these details. Commands such as 'git sparse-checkout set' upgrade the repository to use extensions.worktreeConfig without following these steps, causing some user pain in some special cases. Create a helper method, init_worktree_config(), that will be used in a later change to fix this behavior within 'git sparse-checkout set'. The method is carefully documented in worktree.h. Note that we do _not_ upgrade the repository format version to 1 during this process. The worktree config extension must be considered by Git and third-party tools even if core.repositoryFormatVersion is 0 for historical reasons documented in 11664196ac ("Revert "check_repository_format_gently(): refuse extensions for old repositories"", 2020-07-15). This is a special case for this extension, and newer extensions (such as extensions.objectFormat) still need to upgrade the repository format version. Signed-off-by: Derrick Stolee --- worktree.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ worktree.h | 21 ++++++++++++++++ 2 files changed, 94 insertions(+) diff --git a/worktree.c b/worktree.c index 6f598dcfcdf..5292c94b3d9 100644 --- a/worktree.c +++ b/worktree.c @@ -5,6 +5,7 @@ #include "worktree.h" #include "dir.h" #include "wt-status.h" +#include "config.h" void free_worktrees(struct worktree **worktrees) { @@ -826,3 +827,75 @@ int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, *wtpath = path; return 0; } + +static int move_config_setting(const char *key, const char *value, + const char *from_file, const char *to_file) +{ + if (git_config_set_in_file_gently(to_file, key, value)) + return error(_("unable to set %s in '%s'"), key, to_file); + if (git_config_set_in_file_gently(from_file, key, NULL)) + return error(_("unable to unset %s in '%s'"), key, from_file); + return 0; +} + +int init_worktree_config(struct repository *r) +{ + int res = 0; + int bare = 0; + struct config_set cs = { { 0 } }; + const char *core_worktree; + char *common_config_file; + char *main_worktree_file; + + /* + * If the extension is already enabled, then we can skip the + * upgrade process. + */ + if (repository_format_worktree_config) + return 0; + if ((res = git_config_set_gently("extensions.worktreeConfig", "true"))) + return error(_("failed to set extensions.worktreeConfig setting")); + + common_config_file = xstrfmt("%s/config", r->commondir); + main_worktree_file = xstrfmt("%s/config.worktree", r->commondir); + + git_configset_init(&cs); + git_configset_add_file(&cs, common_config_file); + + /* + * If core.bare is true in the common config file, then we need to + * move it to the main worktree's config file or it will break all + * worktrees. If it is false, then leave it in place because it + * _could_ be negating a global core.bare=true. + */ + if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) { + if ((res = move_config_setting("core.bare", "true", + common_config_file, + main_worktree_file))) + goto cleanup; + } + /* + * If core.worktree is set, then the main worktree is located + * somewhere different than the parent of the common Git dir. + * Relocate that value to avoid breaking all worktrees with this + * upgrade to worktree config. + */ + if (!git_configset_get_value(&cs, "core.worktree", &core_worktree)) { + if ((res = move_config_setting("core.worktree", core_worktree, + common_config_file, + main_worktree_file))) + goto cleanup; + } + + /* + * Ensure that we use worktree config for the remaining lifetime + * of the current process. + */ + repository_format_worktree_config = 1; + +cleanup: + git_configset_clear(&cs); + free(common_config_file); + free(main_worktree_file); + return res; +} diff --git a/worktree.h b/worktree.h index 9e06fcbdf3d..e9e839926b0 100644 --- a/worktree.h +++ b/worktree.h @@ -183,4 +183,25 @@ void strbuf_worktree_ref(const struct worktree *wt, struct strbuf *sb, const char *refname); +/** + * Enable worktree config for the first time. This will make the following + * adjustments: + * + * 1. Add extensions.worktreeConfig=true in the common config file. + * + * 2. If the common config file has a core.worktree value, then that value + * is moved to the main worktree's config.worktree file. + * + * 3. If the common config file has a core.bare enabled, then that value + * is moved to the main worktree's config.worktree file. + * + * If extensions.worktreeConfig is already true, then this method + * terminates early without any of the above steps. The existing config + * arrangement is assumed to be intentional. + * + * Returns 0 on success. Reports an error message and returns non-zero + * if any of these steps fail. + */ +int init_worktree_config(struct repository *r); + #endif From patchwork Mon Feb 7 21:33:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737907 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5B210C433EF for ; Mon, 7 Feb 2022 21:33:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241099AbiBGVdQ (ORCPT ); Mon, 7 Feb 2022 16:33:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240951AbiBGVdK (ORCPT ); Mon, 7 Feb 2022 16:33:10 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8FF7AC061355 for ; Mon, 7 Feb 2022 13:33:09 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id i15so4768479wrb.3 for ; Mon, 07 Feb 2022 13:33:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=INwATnL1xRLYdZVmyzbjAkZIOCTDRoDVPXqO0Q4F5YA=; b=otauxVbNhdpRSbB2iGrJwCh+O74dZOFGyR27yFTt++VGRhRRW80+pYt97kfat9oxbG r3+mWYw9KXNt9xpAB6yJycdaae+MemsDvlSfkK8c6KKhnlNf0b4mj6LJyI5ki+pURfs8 SUTOelorQFGUddbrFg+JFWEgkUV8g8xeGurz5ogjU0q1PQw9t3yVj3SarH7pWWn93byW 1ljEwLNmyot0RG3yy7ncEyRdTo/aqkEkXg9+rl8Nw4ZBEmwR5Oo/jMdcohfjEnEJtY0k 5hzDAdxJ3c/oHEMiWwpH+zO7jfJXILnCGN6neOuKUIkyW+p3i0nLHrI+SdJSnuSqVcAw GzxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=INwATnL1xRLYdZVmyzbjAkZIOCTDRoDVPXqO0Q4F5YA=; b=4MIMrD/7AKN1P/Ykjvmz9+XY4npgXLXmAIwIXVRtPmahv4mTIGm9GvdoiSJcJCKOYe fZVsbPReBpYN1QMJ/63URIBfmbtFC4jW3Qr5rV5aXfEp3DDR0WJYFA744qqomNoW7EKH i6bKdhAWGWk204it94pmJNaYI2iDyNe7R0cGwO0j1U9ryMPDIRCaHMzFaWHI8nZ20cIK cruAcR0XABv0VJvKFAzGAfFyY03UjBKFcqTBvjSMYQUJixtQB6oNklUB0KoUXVuM4XbN TwzWBqkLvMMtRIAzjOewpoui+b8NuiuwvtBFWHrCgaGYE7ZCL+hZcKRm80ugU73IRpq5 in2g== X-Gm-Message-State: AOAM533tE1TrEVTRR7aUO4YuwzUpXunZiJOOAR1TJ775m+FDDflaMZPx EX+wRXTYFl+cJSHgmONY+/p6sm7EwVI= X-Google-Smtp-Source: ABdhPJyV3wF+DB93WOIAQawhLsK3038oBxMBdVMcmaeglOhmm86geUmY2+L8z6PWja62fsGqhItG4A== X-Received: by 2002:a05:6000:156d:: with SMTP id 13mr1048131wrz.700.1644269588020; Mon, 07 Feb 2022 13:33:08 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m6sm12480388wrw.54.2022.02.07.13.33.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:07 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 07 Feb 2022 21:33:00 +0000 Subject: [PATCH v6 3/6] config: add repo_config_set_worktree_gently() Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee Some config settings, such as those for sparse-checkout, are likely intended to only apply to one worktree at a time. To make this write easier, add a new config API method, repo_config_set_worktree_gently(). This method will attempt to write to the worktree-specific config, but will instead write to the common config file if worktree config is not enabled. The next change will introduce a consumer of this method. Signed-off-by: Derrick Stolee --- config.c | 35 ++++++++++++++++++++++++++++++++--- config.h | 8 ++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/config.c b/config.c index 2bffa8d4a01..1a03ced1a54 100644 --- a/config.c +++ b/config.c @@ -21,6 +21,7 @@ #include "dir.h" #include "color.h" #include "refs.h" +#include "worktree.h" struct config_source { struct config_source *prev; @@ -2884,6 +2885,20 @@ int git_config_set_gently(const char *key, const char *value) return git_config_set_multivar_gently(key, value, NULL, 0); } +int repo_config_set_worktree_gently(struct repository *r, + const char *key, const char *value) +{ + /* Only use worktree-specific config if it is is already enabled. */ + if (repository_format_worktree_config) { + char *file = repo_git_path(r, "config.worktree"); + int ret = git_config_set_multivar_in_file_gently( + file, key, value, NULL, 0); + free(file); + return ret; + } + return repo_config_set_multivar_gently(r, key, value, NULL, 0); +} + void git_config_set(const char *key, const char *value) { git_config_set_multivar(key, value, NULL, 0); @@ -3181,14 +3196,28 @@ void git_config_set_multivar_in_file(const char *config_filename, int git_config_set_multivar_gently(const char *key, const char *value, const char *value_pattern, unsigned flags) { - return git_config_set_multivar_in_file_gently(NULL, key, value, value_pattern, - flags); + return repo_config_set_multivar_gently(the_repository, key, value, + value_pattern, flags); +} + +int repo_config_set_multivar_gently(struct repository *r, const char *key, + const char *value, + const char *value_pattern, unsigned flags) +{ + char *file = repo_git_path(r, "config"); + int res = git_config_set_multivar_in_file_gently(file, + key, value, + value_pattern, + flags); + free(file); + return res; } void git_config_set_multivar(const char *key, const char *value, const char *value_pattern, unsigned flags) { - git_config_set_multivar_in_file(NULL, key, value, value_pattern, + git_config_set_multivar_in_file(git_path("config"), + key, value, value_pattern, flags); } diff --git a/config.h b/config.h index f119de01309..1d98ad269bd 100644 --- a/config.h +++ b/config.h @@ -253,6 +253,13 @@ void git_config_set_in_file(const char *, const char *, const char *); int git_config_set_gently(const char *, const char *); +/** + * Write a config value that should apply to the current worktree. If + * extensions.worktreeConfig is enabled, then the write will happen in the + * current worktree's config. Otherwise, write to the common config file. + */ +int repo_config_set_worktree_gently(struct repository *, const char *, const char *); + /** * write config values to `.git/config`, takes a key/value pair as parameter. */ @@ -281,6 +288,7 @@ int git_config_parse_key(const char *, char **, size_t *); int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned); void git_config_set_multivar(const char *, const char *, const char *, unsigned); +int repo_config_set_multivar_gently(struct repository *, const char *, const char *, const char *, unsigned); int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned); /** From patchwork Mon Feb 7 21:33:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737908 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4011DC433EF for ; Mon, 7 Feb 2022 21:33:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241388AbiBGVdW (ORCPT ); Mon, 7 Feb 2022 16:33:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240971AbiBGVdL (ORCPT ); Mon, 7 Feb 2022 16:33:11 -0500 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81B20C0612A4 for ; Mon, 7 Feb 2022 13:33:10 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id e3so13207931wra.0 for ; Mon, 07 Feb 2022 13:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=ABpkPLaMlQSyuii/h6Kzk3rFjsx7ver4X6LdDJV17Rs=; b=lhE+10Xf5jeqIjFVCjp4xDh/t0myigWeLFo0dgvyCaLImr7ApgJmhZEmAAN+4q7kfD UOhYlqb64kJzENaIjrcO045T1gZBksh9IxcQ82wGu0bap1oXTaXMoaQyqu1lRFk0A5ON nZ8eqIJqzYhF1Rj0rpZcaLXhtd2fxC8boCQPK/fKtdCU4e7Wo4vYHZlNrZLqzKCiHurZ 3sMQzmKGJLUFKdfUGKl+vK6t3YebwG9zNnttUUVIJaV3GimxFtayz5xMwy/Zdqv33+EH i8G/wj0CVlIBBZs4to/X2aw/8UVENLMkZYQoz98TkqLWW6P+Gsz84JjKpguxp1DJAP3w cTeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=ABpkPLaMlQSyuii/h6Kzk3rFjsx7ver4X6LdDJV17Rs=; b=S6x81Yac4vfNxLy80ZcAjOFXUNJ9rv5HTlJDF8Lt/jZ1aXy3KgoIWJaRNdzGPVZVj4 Dp/EYtHRMW37tkKjV71H7FflRECRuLQc0/wfcEHTkQSNqFxwaLskStk61Py3uiuLApC1 a4kkbS48PKqObFCA2A1owMKiV5xRtf2qC6P6k1X5cUqc1rsCDd0Gs4QrX7jVw5WSBDU7 jTT8RhN3ELcHhv65LP4X5JJ6kE/WushgutaEuO4yVxjtt63ka8aXHDw1AP2uQ0LEU75G ErIGWwwAvegpZbdMm2Buo0WSe1VVmcBv3Nd35AJBMgtnLfcVfL9j5VehroLgQSRYwvHw 1wNw== X-Gm-Message-State: AOAM531rUgjV+kRoibMq0fPIxpAyghTW9Gn6F1AZm01ZZVwMpPAlbErY 3IU405Ddl2d6voz9+rDTCh0QGplAdRs= X-Google-Smtp-Source: ABdhPJzVEB5ILes54ZxMvWUK4DTBBnyGDm0u9EPrUmTgmGPf2mXA5ohGiEI4rZ+cVZbEgot4JrP3iA== X-Received: by 2002:adf:f3d2:: with SMTP id g18mr1099890wrp.484.1644269588852; Mon, 07 Feb 2022 13:33:08 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c13sm11080647wrv.24.2022.02.07.13.33.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:08 -0800 (PST) Message-Id: <5b5924eab498c40542f42989fb31f568cb5371b2.1644269583.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 07 Feb 2022 21:33:01 +0000 Subject: [PATCH v6 4/6] sparse-checkout: set worktree-config correctly Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee `git sparse-checkout set/init` enables worktree-specific configuration[*] by setting extensions.worktreeConfig=true, but neglects to perform the additional necessary bookkeeping of relocating `core.bare=true` and `core.worktree` from $GIT_COMMON_DIR/config to $GIT_COMMON_DIR/config.worktree, as documented in git-worktree.txt. As a result of this oversight, these settings, which are nonsensical for secondary worktrees, can cause Git commands to incorrectly consider a worktree bare (in the case of `core.bare`) or operate on the wrong worktree (in the case of `core.worktree`). Fix this problem by taking advantage of the recently-added init_worktree_config() which enables `extensions.worktreeConfig` and takes care of necessary bookkeeping. While at it, for backward-compatibility reasons, also stop upgrading the repository format to "1" since doing so is (unintentionally) not required to take advantage of `extensions.worktreeConfig`, as explained by 11664196ac ("Revert "check_repository_format_gently(): refuse extensions for old repositories"", 2020-07-15). [*] The main reason to use worktree-specific config for the sparse-checkout builtin was to avoid enabling sparse-checkout patterns in one and causing a loss of files in another. If a worktree does not have a sparse-checkout patterns file, then the sparse-checkout logic will not kick in on that worktree. Reported-by: Sean Allred Helped-by: Eric Sunshine Signed-off-by: Derrick Stolee --- Documentation/git-sparse-checkout.txt | 16 +++++++++++---- builtin/sparse-checkout.c | 28 +++++++++++++-------------- sparse-index.c | 10 +++------- t/t1091-sparse-checkout-builtin.sh | 4 ++-- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Documentation/git-sparse-checkout.txt b/Documentation/git-sparse-checkout.txt index b81dbe06543..94dad137b96 100644 --- a/Documentation/git-sparse-checkout.txt +++ b/Documentation/git-sparse-checkout.txt @@ -31,13 +31,21 @@ COMMANDS Describe the patterns in the sparse-checkout file. 'set':: - Enable the necessary config settings - (extensions.worktreeConfig, core.sparseCheckout, - core.sparseCheckoutCone) if they are not already enabled, and - write a set of patterns to the sparse-checkout file from the + Enable the necessary sparse-checkout config settings + (`core.sparseCheckout`, `core.sparseCheckoutCone`, and + `index.sparse`) if they are not already set to the desired values, + and write a set of patterns to the sparse-checkout file from the list of arguments following the 'set' subcommand. Update the working directory to match the new patterns. + +To ensure that adjusting the sparse-checkout settings within a worktree +does not alter the sparse-checkout settings in other worktrees, the 'set' +subcommand will upgrade your repository config to use worktree-specific +config if not already present. The sparsity defined by the arguments to +the 'set' subcommand are stored in the worktree-specific sparse-checkout +file. See linkgit:git-worktree[1] and the documentation of +`extensions.worktreeConfig` in linkgit:git-config[1] for more details. ++ When the `--stdin` option is provided, the patterns are read from standard in as a newline-delimited list instead of from the arguments. + diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 679c1070368..314c8d61f80 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -15,6 +15,7 @@ #include "wt-status.h" #include "quote.h" #include "sparse-index.h" +#include "worktree.h" static const char *empty_base = ""; @@ -359,26 +360,23 @@ enum sparse_checkout_mode { static int set_config(enum sparse_checkout_mode mode) { - const char *config_path; - - if (upgrade_repository_format(1) < 0) - die(_("unable to upgrade repository format to enable worktreeConfig")); - if (git_config_set_gently("extensions.worktreeConfig", "true")) { - error(_("failed to set extensions.worktreeConfig setting")); + /* Update to use worktree config, if not already. */ + if (init_worktree_config(the_repository)) { + error(_("failed to initialize worktree config")); return 1; } - config_path = git_path("config.worktree"); - git_config_set_in_file_gently(config_path, - "core.sparseCheckout", - mode ? "true" : NULL); - - git_config_set_in_file_gently(config_path, - "core.sparseCheckoutCone", - mode == MODE_CONE_PATTERNS ? "true" : NULL); + if (repo_config_set_worktree_gently(the_repository, + "core.sparseCheckout", + mode ? "true" : "false") || + repo_config_set_worktree_gently(the_repository, + "core.sparseCheckoutCone", + mode == MODE_CONE_PATTERNS ? + "true" : "false")) + return 1; if (mode == MODE_NO_PATTERNS) - set_sparse_index_config(the_repository, 0); + return set_sparse_index_config(the_repository, 0); return 0; } diff --git a/sparse-index.c b/sparse-index.c index a1d505d50e9..e93609999e0 100644 --- a/sparse-index.c +++ b/sparse-index.c @@ -99,13 +99,9 @@ static int convert_to_sparse_rec(struct index_state *istate, int set_sparse_index_config(struct repository *repo, int enable) { - int res; - char *config_path = repo_git_path(repo, "config.worktree"); - res = git_config_set_in_file_gently(config_path, - "index.sparse", - enable ? "true" : NULL); - free(config_path); - + int res = repo_config_set_worktree_gently(repo, + "index.sparse", + enable ? "true" : "false"); prepare_repo_settings(repo); repo->settings.sparse_index = enable; return res; diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index 42776984fe7..be6ea4ffe33 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -117,7 +117,7 @@ test_expect_success 'switching to cone mode with non-cone mode patterns' ' cd bad-patterns && git sparse-checkout init && git sparse-checkout add dir && - git config core.sparseCheckoutCone true && + git config --worktree core.sparseCheckoutCone true && test_must_fail git sparse-checkout add dir 2>err && grep "existing sparse-checkout patterns do not use cone mode" err ) @@ -256,7 +256,7 @@ test_expect_success 'sparse-index enabled and disabled' ' test_cmp expect actual && git -C repo config --list >config && - ! grep index.sparse config + test_cmp_config -C repo false index.sparse ) ' From patchwork Mon Feb 7 21:33:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737909 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9812BC433F5 for ; Mon, 7 Feb 2022 21:33:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241069AbiBGVdX (ORCPT ); Mon, 7 Feb 2022 16:33:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53684 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241014AbiBGVdM (ORCPT ); Mon, 7 Feb 2022 16:33:12 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D6D6C061A73 for ; Mon, 7 Feb 2022 13:33:11 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id m14so27098227wrg.12 for ; Mon, 07 Feb 2022 13:33:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=gNGD5q/SjVzOkwpQKi5dugiQ1BOgAonIAEOI/Pa9RFI=; b=Gg7vcgDm8oWbbXm7c94te7cBlvygJj52sBjyH4SAO7Vmr0FD60PGqDdnSFIxHbqGWy 423LUzUpMSgwRWl5PAIwOQZKmj2q0SphucxjVu3kkDYMkPGBqs610IHayvbR8ZTGURwt VQdQ8iet0rXEzn56k18HKGXEsQ15LGpjuf0HhH3HXIBLjNwZxn6FhZvOCloH/s2K/Lnt NdNdmIFaJzvwAtbmXmcyKuqHkRUKa65LhR6ZXWAB4dBN3YRkigeOLbsrgUSiNBPqyjVC dR9DnSjHouvNvEYmUaxtcsxhYnO68AmEsXSpn+m8fN1mw00kfQKfXTA99U0KIQvzRl5F 1Q8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=gNGD5q/SjVzOkwpQKi5dugiQ1BOgAonIAEOI/Pa9RFI=; b=nGSg6UKDHpKU1aEtyDoCiLwweg505LrG4MwZKrh9PPHdl6o8c3Tgko8NYyA7MEozYJ geTPw+LJDg4EH5AG4BoyCgRIBUGmvvI5QmnfgxKnbfMy2wjKc9otfCF8FaqYrVGMdCvV 4HyZgVN2zHLi/QC5LudnvlYuH9YzXOl41tcSp22AT50NM66YD0zutlSgK/CEbdXulMVL kblpmiW7OFKKLtK6axspGXacIWMM8xwBg7QXeC5q/9FStJoZClsAnOozfX8QE0HyJ95R wFFi6Im823tQ0p7jvQQLpCPJHdCLpQrWwUYIZ7xYWxO2iJTsWrM4ai/tR2THJbcOt2rg nD1Q== X-Gm-Message-State: AOAM533pictaCzaFEX9ngXtrnFuQ1DnDANGAaxjYgIxbMkFIcvGBcxo+ FKs8IIJqoeuPSZJw0Kl4IKMLRVosOxQ= X-Google-Smtp-Source: ABdhPJzJ2pah1TEs7FSWPOOCLoY2Dnpib+yBBgmSKgTQnO0zqL593dO7yKloVh/A52wHgDCdV0+cGQ== X-Received: by 2002:a5d:6850:: with SMTP id o16mr1057237wrw.344.1644269589681; Mon, 07 Feb 2022 13:33:09 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id v3sm10661407wru.15.2022.02.07.13.33.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:09 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 07 Feb 2022 21:33:02 +0000 Subject: [PATCH v6 5/6] worktree: copy sparse-checkout patterns and config on add Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee When adding a new worktree, it is reasonable to expect that we want to use the current set of sparse-checkout settings for that new worktree. This is particularly important for repositories where the worktree would become too large to be useful. This is even more important when using partial clone as well, since we want to avoid downloading the missing blobs for files that should not be written to the new worktree. The only way to create such a worktree without this intermediate step of expanding the full worktree is to copy the sparse-checkout patterns and config settings during 'git worktree add'. Each worktree has its own sparse-checkout patterns, and the default behavior when the sparse-checkout file is missing is to include all paths at HEAD. Thus, we need to have patterns from somewhere, they might as well be the current worktree's patterns. These are then modified independently in the future. In addition to the sparse-checkout file, copy the worktree config file if worktree config is enabled and the file exists. This will copy over any important settings to ensure the new worktree behaves the same as the current one. The only exception we must continue to make is that core.bare and core.worktree should become unset in the worktree's config file. Signed-off-by: Derrick Stolee --- builtin/worktree.c | 63 ++++++++++++++++++++++++++++++ t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++---- t/t2400-worktree-add.sh | 58 ++++++++++++++++++++++++++- 3 files changed, 142 insertions(+), 10 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 2838254f7f2..c6eb636329a 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -335,6 +335,69 @@ static int add_worktree(const char *path, const char *refname, strbuf_addf(&sb, "%s/commondir", sb_repo.buf); write_file(sb.buf, "../.."); + /* + * If the current worktree has sparse-checkout enabled, then copy + * the sparse-checkout patterns from the current worktree. + */ + if (core_apply_sparse_checkout) { + char *from_file = git_pathdup("info/sparse-checkout"); + char *to_file = xstrfmt("%s/info/sparse-checkout", + sb_repo.buf); + + if (file_exists(from_file)) { + if (safe_create_leading_directories(to_file) || + copy_file(to_file, from_file, 0666)) + error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"), + from_file, to_file); + } + + free(from_file); + free(to_file); + } + + /* + * If we are using worktree config, then copy all current config + * values from the current worktree into the new one, that way the + * new worktree behaves the same as this one. + */ + if (repository_format_worktree_config) { + char *from_file = git_pathdup("config.worktree"); + char *to_file = xstrfmt("%s/config.worktree", + sb_repo.buf); + + if (file_exists(from_file)) { + struct config_set cs = { { 0 } }; + const char *core_worktree; + int bare; + + if (safe_create_leading_directories(to_file) || + copy_file(to_file, from_file, 0666)) { + error(_("failed to copy worktree config from '%s' to '%s'"), + from_file, to_file); + goto worktree_copy_cleanup; + } + + git_configset_init(&cs); + git_configset_add_file(&cs, from_file); + + if (!git_configset_get_bool(&cs, "core.bare", &bare) && + bare && + git_config_set_multivar_in_file_gently( + to_file, "core.bare", NULL, "true", 0)) + error(_("failed to unset 'core.bare' in '%s'"), to_file); + if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) && + git_config_set_in_file_gently(to_file, + "core.worktree", NULL)) + error(_("failed to unset 'core.worktree' in '%s'"), to_file); + + git_configset_clear(&cs); + } + +worktree_copy_cleanup: + free(from_file); + free(to_file); + } + strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); cp.git_cmd = 1; diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index be6ea4ffe33..8a757b43e6c 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -146,9 +146,9 @@ test_expect_success 'interaction with clone --no-checkout (unborn index)' ' ' test_expect_success 'set enables config' ' - git init empty-config && + git init worktree-config && ( - cd empty-config && + cd worktree-config && test_commit test file && test_path_is_missing .git/config.worktree && git sparse-checkout set nothing && @@ -201,6 +201,21 @@ test_expect_success 'add to sparse-checkout' ' check_files repo "a folder1 folder2" ' +test_expect_success 'worktree: add copies sparse-checkout patterns' ' + cat repo/.git/info/sparse-checkout >old && + test_when_finished cp old repo/.git/info/sparse-checkout && + test_when_finished git -C repo worktree remove ../worktree && + git -C repo sparse-checkout set --no-cone "/*" && + git -C repo worktree add --quiet ../worktree 2>err && + test_must_be_empty err && + new="$(git -C worktree rev-parse --git-path info/sparse-checkout)" && + test_path_is_file "$new" && + test_cmp repo/.git/info/sparse-checkout "$new" && + git -C worktree sparse-checkout set --cone && + test_cmp_config -C worktree true core.sparseCheckoutCone && + test_must_fail git -C repo core.sparseCheckoutCone +' + test_expect_success 'cone mode: match patterns' ' git -C repo config --worktree core.sparseCheckoutCone true && rm -rf repo/a repo/folder1 repo/folder2 && @@ -520,13 +535,13 @@ test_expect_success 'interaction with submodules' ' ' test_expect_success 'different sparse-checkouts with worktrees' ' + git -C repo sparse-checkout set --cone deep folder1 && git -C repo worktree add --detach ../worktree && - check_files worktree "a deep folder1 folder2" && - git -C worktree sparse-checkout init --cone && - git -C repo sparse-checkout set folder1 && - git -C worktree sparse-checkout set deep/deeper1 && - check_files repo a folder1 && - check_files worktree a deep + check_files worktree "a deep folder1" && + git -C repo sparse-checkout set --cone folder1 && + git -C worktree sparse-checkout set --cone deep/deeper1 && + check_files repo "a folder1" && + check_files worktree "a deep" ' test_expect_success 'set using filename keeps file on-disk' ' diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 37ad79470fb..43139af08fc 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -165,8 +165,62 @@ test_expect_success '"add" default branch of a bare repo' ' ( git clone --bare . bare2 && cd bare2 && - git worktree add ../there3 main - ) + git worktree add ../there3 main && + cd ../there3 && + # Simple check that a Git command does not + # immediately fail with the current setup + git status + ) && + cat >expect <<-EOF && + init.t + EOF + ls there3 >actual && + test_cmp expect actual +' + +test_expect_success '"add" to bare repo with worktree config' ' + ( + git clone --bare . bare3 && + cd bare3 && + git config extensions.worktreeconfig true && + + # Add config values that are erroneous to have in + # a config.worktree file outside of the main + # working tree, to check that Git filters them out + # when copying config during "git worktree add". + git config --worktree core.bare true && + git config --worktree core.worktree "$(pwd)" && + + # We want to check that bogus.key is copied + git config --worktree bogus.key value && + git config --unset core.bare && + git worktree add ../there4 main && + cd ../there4 && + + # Simple check that a Git command does not + # immediately fail with the current setup + git status && + git worktree add --detach ../there5 && + cd ../there5 && + git status + ) && + + # the worktree has the arbitrary value copied. + test_cmp_config -C there4 value bogus.key && + test_cmp_config -C there5 value bogus.key && + + # however, core.bare and core.worktree were removed. + test_must_fail git -C there4 config core.bare && + test_must_fail git -C there4 config core.worktree && + + cat >expect <<-EOF && + init.t + EOF + + ls there4 >actual && + test_cmp expect actual && + ls there5 >actual && + test_cmp expect actual ' test_expect_success 'checkout with grafts' ' From patchwork Mon Feb 7 21:33:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12737910 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id EA633C433FE for ; Mon, 7 Feb 2022 21:33:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241141AbiBGVdZ (ORCPT ); Mon, 7 Feb 2022 16:33:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53688 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241044AbiBGVdM (ORCPT ); Mon, 7 Feb 2022 16:33:12 -0500 Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFBCEC043181 for ; Mon, 7 Feb 2022 13:33:11 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id f17so27210689wrx.1 for ; Mon, 07 Feb 2022 13:33:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:in-reply-to:references:from:date:subject:fcc :content-transfer-encoding:mime-version:to:cc; bh=L70Bj04IYq30hwhq9Ie3sb3mr5ltQTCSdsjilrUOVnQ=; b=jlQRlW8XYAtY3MACIC2oaLWnXD3nVGKN81w6SGo3fPsp80LGAHOVopndtRffB5FGIz CpIRIPeFeTYgCT3bK58ORzkyKLlfKt+HwfvedMlX3Ow7SLeD34/4HbcK4l0qrqPWbyQI OoAojKY8SAZNh7aa/vLotYLCx5gcXsIDCmH5AOFa6zp5Et6yz+Rz7Mn6F7pjDBD/1geo vf4t+5NsO+pzABFBk/5gMhEFwVNPVRiX6klZX5tkR1E4EoYLavgWoLa5XdylTcuJuGZb IAnep2u5BfETHzEplrZMGJEJVAX7w4aYS1sFYDXvHC3Pip58eQqMj6a/3nXAOH4/k721 SksA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:in-reply-to:references:from:date :subject:fcc:content-transfer-encoding:mime-version:to:cc; bh=L70Bj04IYq30hwhq9Ie3sb3mr5ltQTCSdsjilrUOVnQ=; b=0fsAUDny98lZ4A6Fiv8aRIraapHWYjZWE4drWqfrRgNV+93bv3p4r4pj4gXgJuhzpa daUNTn5R0mzED8EQXtFdprngCERGTHax2xclIHd/96SyzKxWXYF+Q2uDYy8nNKxftBsX CtxdXDxjHwnzA/dN4uniwxjzQ/RcCvFZO/1wzszUotykxu22tHG5zLfg+D42/ekDSKdu Wpz2nWNWZ2OQpQHvK9D34+Y0Lp1h61fwDwf+KZ/ZoRH9fE0K7ZTvR3i0kVbQJrxU3x/x esPGiHcvBpicCbTfBYMuDudA4PWMVRDt4EWnleEfojAULdVzJikJisBmc+vwFWCcIofr tQzg== X-Gm-Message-State: AOAM531iC0boKYBxbV4zEf/JW4pK6eR3r2c9OAVqGz0m95xSRyf+46lH DPYvEEmTVmUfPC8+XrZ++AEGlhPI7IE= X-Google-Smtp-Source: ABdhPJwHwcBaoi0r75tyfCs4F9fC5xMnHNtHbVqylNW8Gd+xMdZE4h/Uw/I0e8C/Wo1Ev2eiiNeCoQ== X-Received: by 2002:adf:a2da:: with SMTP id t26mr1062127wra.290.1644269590475; Mon, 07 Feb 2022 13:33:10 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id y3sm12015440wry.109.2022.02.07.13.33.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Feb 2022 13:33:09 -0800 (PST) Message-Id: In-Reply-To: References: Date: Mon, 07 Feb 2022 21:33:03 +0000 Subject: [PATCH v6 6/6] config: make git_configset_get_string_tmp() private Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: stolee@gmail.com, sunshine@sunshineco.com, allred.sean@gmail.com, gitster@pobox.com, Elijah Newren , Bagas Sanjaya , =?utf-8?q?Jean-No=C3=ABl?= AVILA , derrickstolee@github.com, Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee This method was created in f1de981e8 (config: fix leaks from git_config_get_string_const(), 2020-08-14) but its only use was in the repo_config_get_string_tmp() method, also declared in config.h and implemented in config.c. Since this is otherwise unused and is a very similar implementation to git_configset_get_value(), let's remove this declaration. Signed-off-by: Derrick Stolee --- config.c | 4 ++-- config.h | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/config.c b/config.c index 1a03ced1a54..870b22dd2ff 100644 --- a/config.c +++ b/config.c @@ -2179,8 +2179,8 @@ int git_configset_get_string(struct config_set *cs, const char *key, char **dest return 1; } -int git_configset_get_string_tmp(struct config_set *cs, const char *key, - const char **dest) +static int git_configset_get_string_tmp(struct config_set *cs, const char *key, + const char **dest) { const char *value; if (!git_configset_get_value(cs, key, &value)) { diff --git a/config.h b/config.h index 1d98ad269bd..184aef1eca4 100644 --- a/config.h +++ b/config.h @@ -494,7 +494,6 @@ void git_configset_clear(struct config_set *cs); int git_configset_get_value(struct config_set *cs, const char *key, const char **dest); int git_configset_get_string(struct config_set *cs, const char *key, char **dest); -int git_configset_get_string_tmp(struct config_set *cs, const char *key, const char **dest); int git_configset_get_int(struct config_set *cs, const char *key, int *dest); int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest); int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);