From patchwork Mon Jan 31 15:00:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12730743 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 17854C433FE for ; Mon, 31 Jan 2022 15:01:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1349305AbiAaPBF (ORCPT ); Mon, 31 Jan 2022 10:01:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349248AbiAaPBD (ORCPT ); Mon, 31 Jan 2022 10:01:03 -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 32E32C06173B for ; Mon, 31 Jan 2022 07:01:03 -0800 (PST) Received: by mail-wr1-x42d.google.com with SMTP id l25so25883761wrb.13 for ; Mon, 31 Jan 2022 07:01:03 -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=/ZljNoJgVbCVB9yLzYfuE976PzYqWu1MtluAAAmo3IQ=; b=bDmCnjChXkWaPoJxXZF3wK4YjKKwFjD4p81jzsu7ejDxyCAwL8aji7w7js4sg+BApp 3gJvAG8BkmSa7aW8b1h9KRs0g7bKVNQ2i2yUciz5LlzJdPhelH1bucnZguRwqBMt2rd3 8Bv0g3yg5VChJ+tyzuVwFyFDpN7VpO2n4H3kUXo+PtiLclre+zBKeU5a/HYaro7sjUZ2 yNU0enhlzyR4jGcc2bgRq11nosiE1937NTFky6lwjhk/zNTWShGXbUhFQVjMkdE9iiZg 6t29orPSgUoKzBsFjTooxrfUcDpMf50f6owzpBC1cIZv6VcfvKBwtUZroTJu/zeC2nnn CtHA== 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=/ZljNoJgVbCVB9yLzYfuE976PzYqWu1MtluAAAmo3IQ=; b=2MFaKOc+0PSgg8aRD/9PL8CmzSo8Fi/Ms88l61y4Y6iN5upFvqEWWIJFlzp8cICb7X yAAgYN0AbKgjErx9D1E9XweufoFL/zUBK8GDNPOLHicgza3bJ6MXQLOjeKGj372haoM6 ypdphYykzI8gv3ZYv6QG75ziSk3QiTVvNLQf4kBMKZgwgruZjAc7PBUwaDUN/+elpfOn mwQUx0u8vMeUFAtm2QhWLxj4Fv1QA/vxHCbjewALOrCEKX9ii5V5THPQkb+hfGjytGB3 D7GqQnsLqZBZfgfYFX/89Rd/ZsPkgFUtn3LeyIpmUwV+EVW9KAnqnHaUw5meIxMvG3YX M2rQ== X-Gm-Message-State: AOAM533qRJ+aNPgeW+5at+ZEAtVq+4z9fd2QYdlkf2iOVgyzmHmymlw3 zBvbxXnKkd3Yxrbar6N4KHl2FSV3D4E= X-Google-Smtp-Source: ABdhPJxdHRwjLue40/J1sqn1HMvsFJih1rnQRs3SV+Em3o2mdRfWAR0SILrCVNXqomoSxEO5t92s+w== X-Received: by 2002:a5d:64e5:: with SMTP id g5mr9633508wri.345.1643641261529; Mon, 31 Jan 2022 07:01:01 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 11sm9511903wmx.5.2022.01.31.07.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 07:01:01 -0800 (PST) Message-Id: <1bd5f26271c9b1cff6c920bb91738ae903dccc61.1643641259.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 31 Jan 2022 15:00:55 +0000 Subject: [PATCH v5 1/5] 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 , 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..5999dcb2a1f 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 worktree, while other + worktrees 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 worktree'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..a48f7529fbc 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 worktree, but is of the + form `$GIT_DIR/worktrees//` for other worktrees. 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 Jan 31 15:00:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12730744 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 2BE7FC433EF for ; Mon, 31 Jan 2022 15:01:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350566AbiAaPBG (ORCPT ); Mon, 31 Jan 2022 10:01:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349242AbiAaPBE (ORCPT ); Mon, 31 Jan 2022 10:01:04 -0500 Received: from mail-wm1-x32c.google.com (mail-wm1-x32c.google.com [IPv6:2a00:1450:4864:20::32c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C029C061714 for ; Mon, 31 Jan 2022 07:01:04 -0800 (PST) Received: by mail-wm1-x32c.google.com with SMTP id k6-20020a05600c1c8600b003524656034cso6521394wms.2 for ; Mon, 31 Jan 2022 07:01:04 -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=uyMDAyf4BzO629nUhVYIK18MvIpf6a1gpCToHf8x+fw=; b=FVtpVTB7w6ab6zLJzL5NAnMLJ36CAvwRINhZyuhyTQKg8fS9nK3rg11zCl24b2cn0s +fODqKicDYUPF/Pi/8CjWzDXec2VD7ksq/h1b5zSTsyy2NDl432goZFp21/ZCdR1S7I4 ilIvJNhn0eskzV510VySBdcbHaXg3qb5VAl4c8k9/NWH/acCT0+qF0Aizyl2x7Maf7Kn XHBaNFZcMKya0YJ5vIk3AlZ9cwy1B5NTsRJofCzYcNUOL/FtNfn13WzrdOioohZEZATQ hFrsv4rwKOvTUDQJ2my2M0pCaOxWAzWXfHmUasb7Tb+xC73s3rqKU9Uz6YNOpEQoSsII EciA== 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=uyMDAyf4BzO629nUhVYIK18MvIpf6a1gpCToHf8x+fw=; b=Hf8ZnZdDIGIXJR6vs1w++JotD+tYWHgISJ/ztgUpfkuWNRf55ZMAlLaskg++jRedE6 7VK07IdSzBic0v3F7p73XYW6hVz2d0rFgSwAM6VLCWAuLdHyFZGtAU3+Lbpa1pzB+0rb bisCYdXRXIay0Nticmg6N91B4rVgmVEjNYt1eOdimh9t69osiUFd3XR8GkXFuj6QBAvj u5EVB8bgZuyXhN+dqTqKjduwKzRe20eSD8VXwecBzBUumibcBmOTG86jm+cL45ufH+UX +wWSmqUHvlKAhhsFICZxe1i41Tc6qqbpArL+LFXnIrC3OAk7OO/X8m7HRH/KdgTifUA6 9qzg== X-Gm-Message-State: AOAM532vL+pGCE9qX8wYJykkSHEjhuxj0gZK15X+GVyXic5Q3Iv6XU4O nJS5TSbxRs0jRve0dqI4agq5IAIgRt8= X-Google-Smtp-Source: ABdhPJxlee8zU2+mhUJzJFYEn5zj6jeZ/r3bCs3EB+QmeycLQIHFAciZyozJosxMhnuqbs1n5gEGvw== X-Received: by 2002:a1c:1f0c:: with SMTP id f12mr18787508wmf.44.1643641262441; Mon, 31 Jan 2022 07:01:02 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n15sm9308205wmr.26.2022.01.31.07.01.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 07:01:02 -0800 (PST) Message-Id: <2a2c350112e647510c5f7c81e831661948cfb68d.1643641259.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 31 Jan 2022 15:00:56 +0000 Subject: [PATCH v5 2/5] 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 , 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 | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ worktree.h | 21 ++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/worktree.c b/worktree.c index 6f598dcfcdf..dc4ead4c8fb 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,72 @@ 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 = xstrfmt("%s/config", r->commondir); + char *main_worktree_file = xstrfmt("%s/config.worktree", r->commondir); + + /* + * 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")); + + 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 base 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 base 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_string_tmp(&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 Jan 31 15:00:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Derrick Stolee X-Patchwork-Id: 12730745 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 0D3BCC433FE for ; Mon, 31 Jan 2022 15:01:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358664AbiAaPBH (ORCPT ); Mon, 31 Jan 2022 10:01:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43874 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1349248AbiAaPBF (ORCPT ); Mon, 31 Jan 2022 10:01:05 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00BD0C061714 for ; Mon, 31 Jan 2022 07:01:05 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id bg21-20020a05600c3c9500b0035283e7a012so4605474wmb.0 for ; Mon, 31 Jan 2022 07:01:04 -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=BnsTyVAkCegZZQW5r96Ysr4vCPa+6u8mXMlEOmOVESVWeF96ahHi/rVAXQMJw5h7Ui hoohDeCOwWyWVranslguYfaY96z/07qYMYoIbgmAmkY21jSQoMsy2phdCs9zZ3SmVj2J ohCMn58iKXdjn8kr+GcIa2TILaixa46toNxe3vTEWN6cjxHPAQSk7pFY35qkRhQtyWSo ryY8qj7zZNS6WRelFMo+mH+Ybugn/KxZAxa0arwiuqKchpyypPXUpXlxSvVHc5Tumt1q 125kZabAuejaTT4ELMejfwJDGcPP2OuWw2KMVOlXoiNgNjkG163AdjM+elMY8wV/vCg/ DqJA== 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=ONMZqb2qJx85pkkFalHbRoKtpoRkntC06tmcpHAQUGbJE54N1PlaB9sMocG3DINdrj nwVlkTLEBLPzJzMIwtKJFkr1tCcljOiohdgRej9respBm5afYEcVVymfgiNt4ohhQb5x cTlDhVBDLbJBlwJq8aelGDT9qBfxzqO9W0USrSzkvaxtDVUIeEf82P5p5H+aKYAboehe m5DqWLNDz0HcfkGNErW8wxLBlNw393dbeWry7TJzWowuQ4Mn3wp7OnUUDpt7pw/Kt+/v YTlCnU7Ct6DhsBTA25H0o9w7on57spatYzCZ68QSPDH+hzJrjb6j3qV171jtivM70cg7 gVhA== X-Gm-Message-State: AOAM533RhX9pbEeDkzRWqZMZwXQ2567XdJYO5tngrO4YnioWuahMzMx2 6KIijc1BVFfnrbtyTAmANAWSPBJ9Yz8= X-Google-Smtp-Source: ABdhPJwgnrXRK5BhwzpQSRg9BPGMjT49YMg6tASDtnWt2PbuRR/IJbif4E+aJ4sFrPb8IEUp/k+a0g== X-Received: by 2002:a05:600c:2dd7:: with SMTP id e23mr19223940wmh.65.1643641263317; Mon, 31 Jan 2022 07:01:03 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t16sm11952756wrx.79.2022.01.31.07.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 07:01:02 -0800 (PST) Message-Id: <802b28a95103396d305eaf62ea98b492b4f21871.1643641259.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 31 Jan 2022 15:00:57 +0000 Subject: [PATCH v5 3/5] 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 , 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 Jan 31 15:00: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: 12730746 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 E4130C433F5 for ; Mon, 31 Jan 2022 15:01:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359177AbiAaPBL (ORCPT ); Mon, 31 Jan 2022 10:01:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43880 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1350615AbiAaPBG (ORCPT ); Mon, 31 Jan 2022 10:01:06 -0500 Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25DD3C061714 for ; Mon, 31 Jan 2022 07:01:06 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id n12-20020a05600c3b8c00b0034eb13edb8eso55406wms.0 for ; Mon, 31 Jan 2022 07:01:06 -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=KIJthjsStQOuB75k6ZK/7i3G8fr1HBHmLXH4L6mURrw=; b=RaCMJcN/wz3ZP0FUKYmLIr7S/KpORrpaf7jDzfRWMgP/y6Zf+55tZhpS4+AhYsnZRa VpxkHbMJ7SFEkx39DY+Z5Pi8ZXjyaNr2NIRn4Jv/XXSwgnMuIFspX9HFUwYgeBdPvh36 gZsIouAf0Js4w7SVuURbbzosi26/5M5EbSFn0aD6wgf+urQCASMej2ENm2ln3H7T43Em KAV2t59kOl97wQMLqvvgfvK4VUX9ZWF+eVYSlnKxpZm8MLdbUJgpugs1id6BgOife031 N676Sxuda9x/m/ehZ1cSEtxqmdOun+qlrwzeu0uI0aI80UeQ8vadijlkRdMbjSFj7K54 dSRg== 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=KIJthjsStQOuB75k6ZK/7i3G8fr1HBHmLXH4L6mURrw=; b=3HD05/BGmKwv0FBpu3Qa2HvsQ3RvQlnnkhIgOs61dWARFAQ6GYFHATvIEQt1/1+E3M yRGUDsG2/KsrotDfobeWAH4BtYG57zCoollJHoWq2Yk/DPa4ZcChky869feMb+s3+8bw j+ueqBp92B4iO6aKj7+2+k5kT6c8LwcWDEQ4oGwEolU+ti7ng1q8G743ja5uZs62tmuq KrSDEge0HlhoDn+oONz6eOJOxTQwSZlGx70wWYMZIgTKoBMU31fSx6Lm+I7P3l8DXAJk JH3sAk4QbiwrKm2p2FGBUB4Mc5JiynWGv9nqO48zQpmuAJXpi8a5FzZkM/yokHSSMDQk Juog== X-Gm-Message-State: AOAM532ojDadKQPHosoFn20zr5uCzzBpreoJzYcek6Dn579jDRDc1htE 6yW21iDfnmiBKvB16/h32pnlN5et/Wc= X-Google-Smtp-Source: ABdhPJzzS/Jyx0gsyQqqOpXbyq04ntbO6LBkTp9Xuwqe86CopLgXJdvLw2bLxgoLUXfJRMj9ShOtwQ== X-Received: by 2002:a05:600c:1d8b:: with SMTP id p11mr27554887wms.115.1643641264406; Mon, 31 Jan 2022 07:01:04 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id m12sm14185286wrp.61.2022.01.31.07.01.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 07:01:03 -0800 (PST) Message-Id: <08b89d17ccfab93546c9e84accaea84ce93ab932.1643641259.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 31 Jan 2022 15:00:58 +0000 Subject: [PATCH v5 4/5] 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 , Derrick Stolee , Derrick Stolee Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Derrick Stolee From: Derrick Stolee The previous change added repo_config_set_worktree_gently() to assist writing config values into the config.worktree file, if enabled. An earlier change added init_worktree_config() as a helper to initialize extensions.worktreeConfig if not already enabled. Let the sparse-checkout builtin use these helpers instead of attempting to initialize the worktree config on its own. This changes behavior of 'git sparse-checkout set' in a few important ways: 1. Git will no longer upgrade the repository format, since this is not a requirement for understanding extensions.worktreeConfig. 2. If the main worktree is bare, then this command will not put the worktree in a broken state. 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 Jan 31 15:00: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: 12730747 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 9B02DC433EF for ; Mon, 31 Jan 2022 15:01:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358790AbiAaPBM (ORCPT ); Mon, 31 Jan 2022 10:01:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358760AbiAaPBH (ORCPT ); Mon, 31 Jan 2022 10:01:07 -0500 Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEFBAC06173B for ; Mon, 31 Jan 2022 07:01:06 -0800 (PST) Received: by mail-wm1-x32f.google.com with SMTP id l35-20020a05600c1d2300b0034d477271c1so9565116wms.3 for ; Mon, 31 Jan 2022 07:01:06 -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=gK4ZPfO2ivlVA1lIXUAfZArXY/3xNecB/HSAOb9Ny7g=; b=dLAkYepUFgW8rDJVmYi+1BWni2NSvoAT8yCroRjddrwpPc58qWFKG9uQk7cLb5Gr7a p41watXoF69MxLvHHxEwZ2HQ8i+rx8UNyB99W1vZ6qKNmNHxIIkZvyO9qH6FMri6/xJ8 Kt6l2V+8lMi93Xrmfleo3WgVH2r8iHJXKfp8wG65YF1QpbTuzMVI46VXFIaZLjGxt9+/ Nm0mCoIcJ52QBoIGTXayAPZ7danwQbole5ZJ6y5QZ/r30Cioxo9m9kY/EXG/KyWOWwr0 drlfX3GEZBVIRbtTXqaYeWunCLfnv+R+V9BZ7rzBQpwUaxQMwnoSZFx70pzcqqt/1Ns1 +IHQ== 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=gK4ZPfO2ivlVA1lIXUAfZArXY/3xNecB/HSAOb9Ny7g=; b=2pMoVSEzaxAIZZ4um8p4NT5mNK6d2gfJX1bFVOUREXm8tk66Ghjn94t6NujMYOd14w swXopscJvque+REb/WzVFZwEnsXV5Vh9/tsoyBTuOC+mQcIuyr3D+6UteAS3E+F6gZj4 FDB2C/QJo4fdz302QyjaMDxzIK/JOG0qph3JTurBdFH6e/T4Xkflm04X61No6jZSs+p/ fWJuVZAXz2ECf4Ggi3PlovZOO1icV6OKcXqq2IXJCkjQ0lismPd2aimlfy6FxnjzyF6h sUS3+bar1mmYc2ZELKvlqEl2PwCF3/2XkrPggrOwyiSqcBy2ONQL/ji2TkJyH/iBbLEb h+HQ== X-Gm-Message-State: AOAM533hv7diyV2IWNMDKfpGiuJ7Iw9Cwt2vt0I0chSSviF+5YBFB3KB Vl6tTrVHdqXuFdThHU2JA3GjbdVuHf4= X-Google-Smtp-Source: ABdhPJxVWKugGwLXSXRsnMo5q4Sg5cfHP+rv5ocOIHGlMP/CLj9ykZem9IlbOc6fqGAz2L+NYn0bQA== X-Received: by 2002:a05:600c:4f06:: with SMTP id l6mr5605446wmq.126.1643641265249; Mon, 31 Jan 2022 07:01:05 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id ay3sm6114151wmb.44.2022.01.31.07.01.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 31 Jan 2022 07:01:04 -0800 (PST) Message-Id: <85779dfaed39220e18129e823aff9c95ade5985b.1643641259.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Mon, 31 Jan 2022 15:00:59 +0000 Subject: [PATCH v5 5/5] 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 , 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 | 60 ++++++++++++++++++++++++++++++ t/t1091-sparse-checkout-builtin.sh | 31 +++++++++++---- t/t2400-worktree-add.sh | 46 ++++++++++++++++++++++- 3 files changed, 127 insertions(+), 10 deletions(-) diff --git a/builtin/worktree.c b/builtin/worktree.c index 2838254f7f2..dc9cd6decc8 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -335,6 +335,66 @@ 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/worktrees/%s/info/sparse-checkout", + realpath.buf, name); + + 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/worktrees/%s/config.worktree", + realpath.buf, name); + + if (file_exists(from_file)) { + struct config_set cs = { { 0 }}; + const char *str_value; + int bool_value; + + if (safe_create_leading_directories(to_file) || + copy_file(to_file, from_file, 0666)) + die(_("failed to copy worktree config from '%s' to '%s'"), + from_file, to_file); + + git_configset_init(&cs); + git_configset_add_file(&cs, from_file); + + if (!git_configset_get_bool(&cs, "core.bare", &bool_value) && + bool_value && + 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", &str_value) && + 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); + } + + 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..8b92e307318 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=repo/.git/worktrees/worktree/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..3fb5b21b943 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -165,8 +165,50 @@ 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 && + 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 && + git config --worktree core.bare true && + git config --worktree core.worktree "$(pwd)" && + git config --worktree bogus.key value && + git config --unset core.bare && + git worktree add ../there4 main && + cd ../there4 && + 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' '