From patchwork Thu Feb 17 06:54:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12749474 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 AD457C433FE for ; Thu, 17 Feb 2022 06:54:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235822AbiBQGzI (ORCPT ); Thu, 17 Feb 2022 01:55:08 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235801AbiBQGzH (ORCPT ); Thu, 17 Feb 2022 01:55:07 -0500 Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B57CB27F296 for ; Wed, 16 Feb 2022 22:54:53 -0800 (PST) Received: by mail-wm1-x32d.google.com with SMTP id az26-20020a05600c601a00b0037c078db59cso3239400wmb.4 for ; Wed, 16 Feb 2022 22:54:53 -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=XS/de9nZ8Iv+oNlhfd/f9n9uf/JZJYBvZ/8IR+hcM2U=; b=WLNTc6HC6hFFBtSfxFI0KQTfZeoe3TNJB1XAmlO3qBPx5HiXbLBu9VOOMtdkhcVyZ2 zwyYfhJTUxgOURt3Si9t7vz776Q6Sajjj9EWD5oGwJ8BVY7n0qBOkCrHB/KkAIUmW2If SvJNgvMywm2cqjyuIMHBrDr7iVOtWIABY/rTINbYTQHbzRXnfuq45uCsvk7i8FjvdRVv 0eEUlztzgqwf6M4/nEXQZOv/Y2QuaN7XoZIbPe2A/Zxng7xJ/MZ76cyT5U9t5o5ut0E6 jmRV4E138RWsGubHJS897L41xKicJVXVPyHSVaW6wvLJVWlkgpMrJkUBYkzWoo/++O9t uPMw== 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=XS/de9nZ8Iv+oNlhfd/f9n9uf/JZJYBvZ/8IR+hcM2U=; b=X/sJEyF2S9h6FU/TT/a04XZPCMzawtlqRdMhDnE7S3YeHQlS3RxhN94+NUaPiM0xk1 LBxeviwDAcgM3KpY7V73qd/C/UGmxTSc5IPVrH53TIp3B53wTYJnX8p0aKCuF+M3YWvL aDADi1ndioNzLycyaGJjLsrcbPj82Gi0EqJOMt9oEyUHqEqoWgKPz1GLhTtniPQCKZpN vC4T/aufvKn2VP+tBXaXTrsJ4+WUHJnvJ0pQxEyqIivMKe/UP1t23fYGC8Ika0x40229 y6AEdSNj9ENiD1ExRmH9/tBeDeD2WhrSTEe+vIJRAjdAK7bBI0u5zcBBbEy3NLnMTqEt 3FRA== X-Gm-Message-State: AOAM530iG7zq31uJt043NBrJgYWjEnmAO0gMdc+WwDYXhRuZBmQkXJDH J+TMT2eCxpTsdeaqZ2c6LicqQc6VGKo= X-Google-Smtp-Source: ABdhPJz0v6zfY/1++GfP24nL1jEKxYtcisyd74iHsI9WfRJ6o3CQetFPnW/oZMs6b0iuOag96dnsWQ== X-Received: by 2002:a7b:ce12:0:b0:37b:b39d:8d73 with SMTP id m18-20020a7bce12000000b0037bb39d8d73mr4566573wmc.168.1645080892199; Wed, 16 Feb 2022 22:54:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z10sm216107wmi.31.2022.02.16.22.54.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 22:54:51 -0800 (PST) Message-Id: <5215b7f7179824468da9476891833ff4cdb28404.1645080889.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 17 Feb 2022 06:54:45 +0000 Subject: [PATCH v4 1/5] sparse-checkout: correct reapply's handling of options Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Victoria Dye , Derrick Stolee , Lessley Dennington , Derrick Stolee , Elijah Newren , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren Commit 4e256731d6 ("sparse-checkout: enable reapply to take --[no-]{cone,sparse-index}", 2021-12-14) made it so that reapply could take additional options but added no tests. Tests would have shown that the feature doesn't work because the initial values are set AFTER parsing the command line options instead of before. Add a test and set the initial value at the appropriate time. Reviewed-by: Derrick Stolee Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 6 +++--- t/t1091-sparse-checkout-builtin.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index a311483a7d2..fcd574f5fc1 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -789,15 +789,15 @@ static int sparse_checkout_reapply(int argc, const char **argv) if (!core_apply_sparse_checkout) die(_("must be in a sparse-checkout to reapply sparsity patterns")); + reapply_opts.cone_mode = -1; + reapply_opts.sparse_index = -1; + argc = parse_options(argc, argv, NULL, builtin_sparse_checkout_reapply_options, builtin_sparse_checkout_reapply_usage, 0); repo_read_index(the_repository); - reapply_opts.cone_mode = -1; - reapply_opts.sparse_index = -1; - if (update_modes(&reapply_opts.cone_mode, &reapply_opts.sparse_index)) return 1; diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index 3592d124424..ce5e7c19efa 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -495,6 +495,34 @@ test_expect_failure 'sparse-checkout reapply' ' git -C tweak sparse-checkout disable ' +test_expect_success 'reapply can handle config options' ' + git -C repo sparse-checkout init --cone --no-sparse-index && + git -C repo config --worktree --list >actual && + cat >expect <<-\EOF && + core.sparsecheckout=true + core.sparsecheckoutcone=true + EOF + test_cmp expect actual && + + git -C repo sparse-checkout reapply --no-cone --no-sparse-index && + git -C repo config --worktree --list >actual && + cat >expect <<-\EOF && + core.sparsecheckout=true + EOF + test_cmp expect actual && + + git -C repo sparse-checkout reapply --cone --sparse-index && + git -C repo config --worktree --list >actual && + cat >expect <<-\EOF && + core.sparsecheckout=true + core.sparsecheckoutcone=true + index.sparse=true + EOF + test_cmp expect actual && + + git -C repo sparse-checkout disable +' + test_expect_success 'cone mode: set with core.ignoreCase=true' ' rm repo/.git/info/sparse-checkout && git -C repo sparse-checkout init --cone && From patchwork Thu Feb 17 06:54:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12749475 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 27DA2C433EF for ; Thu, 17 Feb 2022 06:55:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235838AbiBQGzM (ORCPT ); Thu, 17 Feb 2022 01:55:12 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235820AbiBQGzI (ORCPT ); Thu, 17 Feb 2022 01:55:08 -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 6F3DC27F295 for ; Wed, 16 Feb 2022 22:54:54 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id x5so2389499wrg.13 for ; Wed, 16 Feb 2022 22:54:54 -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=7YuP0Nx5zpadGi0XMIQi9hW1TFbpV+oEB6DRVtErdrw=; b=FV+jM2cQmf/sjbU1iKGULuGUBk5lhGuEdUdtq2Prry91my7axW7I+ZZ9iiya/MqQ+I cAohdOS04A2i6mi4JoDsemRdS+vx55M+ryH/UHJ+7r39vbMDyhxmPyxtx0HMWSp7CYEJ 3JrLX1uPTbl6qIkHa8CS34o6PQdT95QD7d1fkgXyLufBquGPv5TqfbgijZVB5ogCJ+Cl BX38kfNo97fAxmA2cz/hbzOgVhV1YrZUGE3eKwW7gx13c66e7DRv9U7wK5Zy/hSCuEAw aVaEdObuh83BmBZZFqs6bINgZF+2nqCLEn3/+dpQXtBawxtgHFewayFhcZP4JnaaYvUA P+Ew== 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=7YuP0Nx5zpadGi0XMIQi9hW1TFbpV+oEB6DRVtErdrw=; b=vNEd1DqN+2Y7ItaJgQq2rKEM1Z1rZMpVTqLp51hAbnb+hH7FitdgOM4UtYRAeJxq0t 9sgEBwq3ryeZ6MO1atn8jCLZy1hPo0cyx+L7T+HGeFal+yxvc3yHNJMd28GN/5hkEDRk kAJOf28l9kIa0snADliAipGLxJwJtWo6Sn4yFhLpBa0HuzuxX0hOkcwxHa3eaEAnsP2d y7KWv4ktwwTT6Cd7x7yI5CY8DEUyZ8IfdQ0s8UViIiZnhr2L28zm6cPUHauiMRuDSZ64 wJQf1gGOMw3ClPd8QP4YZueVGEt5F+etmovzF+gMMLaDLFicVYM29XAamRDQFazLvCYh KLKA== X-Gm-Message-State: AOAM533Kz1WWS+EXrv6VG7XsHeDIZJZZwImdL5hZ8Z7KXRfA5Lzx9vmf 8QJayXXfBQYVFNQG2ukntixMnd4TVfE= X-Google-Smtp-Source: ABdhPJzSMz/3vO7h5zkSagc7LhGOCF5z7DZ5mAntD+qKKNcUn7qnNgjnP65P039ia/OC4O46ksRpdA== X-Received: by 2002:adf:e8c5:0:b0:1e4:7c8a:21a7 with SMTP id k5-20020adfe8c5000000b001e47c8a21a7mr1151877wrn.516.1645080892936; Wed, 16 Feb 2022 22:54:52 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 3sm35131989wrz.86.2022.02.16.22.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 22:54:52 -0800 (PST) Message-Id: <0c2ab523e74be6f39b82c8fc9c73b4ac57f9963e.1645080889.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 17 Feb 2022 06:54:46 +0000 Subject: [PATCH v4 2/5] sparse-checkout: correctly set non-cone mode when expected Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Victoria Dye , Derrick Stolee , Lessley Dennington , Derrick Stolee , Elijah Newren , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren commit f2e3a218e8 ("sparse-checkout: enable `set` to initialize sparse-checkout mode", 2021-12-14) made the `set` command able to initialize sparse-checkout mode, but it also had to function when sparse-checkout mode was already setup and the user just wanted to change the sparsity paths. So, if the user passed --cone or --no-cone, then we should override the current setting, but if they didn't pass either, we should use whatever the current cone mode setting is. Unfortunately, there was a small error in the logic in that it would not set the in-memory cone mode value (core_sparse_checkout_one) when --no-cone was specified, but since it did set the config setting on disk, any subsequent git invocation would correctly get non-cone mode. As such, the error did not previously matter. However, a subsequent commit will add some logic that depends on core_sparse_checkout_cone being set to the correct mode, so make sure it is set consistently with the config values we will be writing to disk. Reviewed-by: Derrick Stolee Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index fcd574f5fc1..fb85a1459c3 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -403,6 +403,7 @@ static int update_modes(int *cone_mode, int *sparse_index) core_sparse_checkout_cone = 1; } else { mode = MODE_ALL_PATTERNS; + core_sparse_checkout_cone = 0; } if (record_mode && set_config(mode)) return 1; From patchwork Thu Feb 17 06:54:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12749476 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 18ED9C433F5 for ; Thu, 17 Feb 2022 06:55:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235839AbiBQGzN (ORCPT ); Thu, 17 Feb 2022 01:55:13 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235827AbiBQGzJ (ORCPT ); Thu, 17 Feb 2022 01:55:09 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BA0A27F296 for ; Wed, 16 Feb 2022 22:54:55 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id f3so7072761wrh.7 for ; Wed, 16 Feb 2022 22:54:55 -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=f0OZN1ajAW18gNUvDd6eMnhicKRqBDk+zjM5HmxM5As=; b=RB/cNRMfxgffy0lDfglWWvfEdTqYizHv9xDfGDPkpOuZyJO5fc4jxM9NXish7lDd0Z RWlXjSAAAfWlQwvdfjWmk5Y0ua3FRcUTLd3UPMG2PPHhs3NCSXbupdpcM08Xt0Y3WJsW 4FW4Pa70IRe6gdCudE9qoWoyY7m1QZnGHJGZTCPDwhEuAH6XNWSmiYUw5jBZhTkhgdVy bSwc8SJb+YDUzVtuvNMuXzu7bfupNI8WCqJcprLEHHdl4XYpCZDZ/BuhXhDaXGBOil9q k017tpoo5BePTEXqneM8715v2QtJUXpsUX8RzWyngM5iJigh4Bl7rzcZ9bm8nokWBh3e 6ghQ== 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=f0OZN1ajAW18gNUvDd6eMnhicKRqBDk+zjM5HmxM5As=; b=WFguUuuVlx5PpfhPHlZQjSjXYHQ/d5sDIDqBxVqq/U6vobj/3QXqDGl2RShsBZXCIL fld3Jvhe0M7MdWW+pKDHCtE98YPVFRpmdhxzoDGUEXCOprhiU10wcm8CNwtwJ6tK+7OF 4eH+ShGI+i/IyvDOpM0Imk73mcMJ+0mRP1jtPruKOPM5e3yRbqJZOTSZz5RtDJpb8IWj /wxg2R3wXTtQaAkhSAePBiVp4SbnHRSh6UTHgHS+gQjEskgIodcG3rapBzHHj+mrzLwZ oamIgTSMytcGb9CuL7JStct7TilddF1q1bZdAY9qdIJvgEBM3opd3VE3UT8G/SPeWYME nzfA== X-Gm-Message-State: AOAM533Sg1ZHirEeOwV/xGBlgZUEO0XnaMNwGG5QNNS2Q2vuve/wlzVx 1wtbBUbRpZGuG4yidFRhAVKpQiJHt8w= X-Google-Smtp-Source: ABdhPJwEfj01rjGPiCcZGgCtKDTXNpHzwlhzG77E5oo6idk0CEbMpU3tLbEvDJZJLmrwhJ5KaRPP6w== X-Received: by 2002:a05:6000:3ce:b0:1e4:b2ba:e3e3 with SMTP id b14-20020a05600003ce00b001e4b2bae3e3mr1145281wrg.204.1645080893750; Wed, 16 Feb 2022 22:54:53 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id u7sm29489657wrq.112.2022.02.16.22.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 22:54:53 -0800 (PST) Message-Id: In-Reply-To: References: Date: Thu, 17 Feb 2022 06:54:47 +0000 Subject: [PATCH v4 3/5] sparse-checkout: pay attention to prefix for {set, add} Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Victoria Dye , Derrick Stolee , Lessley Dennington , Derrick Stolee , Elijah Newren , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren In cone mode, non-option arguments to set & add are clearly paths, and as such, we should pay attention to prefix. In non-cone mode, it is not clear that folks intend to provide paths since the inputs are gitignore-style patterns. Paying attention to prefix would prevent folks from doing things like git sparse-checkout add /.gitattributes git sparse-checkout add '/toplevel-dir/*' In fact, the former will result in fatal: '/.gitattributes' is outside repository... while the later will result in fatal: Invalid path '/toplevel-dir': No such file or directory despite the fact that both are valid gitignore-style patterns that would select real files if added to the sparse-checkout file. This might lead people to just use the path without the leading slash, potentially resulting in them grabbing files with the same name throughout the directory hierarchy contrary to their expectations. See also [1] and [2]. Adding prefix seems to just be fraught with error; so for now simply throw an error in non-cone mode when sparse-checkout set/add are run from a subdirectory. [1] https://lore.kernel.org/git/e1934710-e228-adc4-d37c-f706883bd27c@gmail.com/ [2] https://lore.kernel.org/git/CABPp-BHXZ-XLxY0a3wCATfdq=6-EjW62RzbxKAoFPeXfJswD2w@mail.gmail.com/ Helped-by: Junio Hamano Reviewed-by: Derrick Stolee Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 25 ++++++++++++++++++ t/t1091-sparse-checkout-builtin.sh | 41 ++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index fb85a1459c3..a2192a69219 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -684,6 +684,27 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin, return result; } +static void sanitize_paths(int argc, const char **argv, const char *prefix) +{ + if (!argc) + return; + + if (prefix && *prefix) { + /* + * The args are not pathspecs, so unfortunately we + * cannot imitate how cmd_add() uses parse_pathspec(). + */ + int i; + int prefix_len = strlen(prefix); + + if (!core_sparse_checkout_cone) + die("please run from the toplevel directory in non-cone mode"); + + for (i = 0; i < argc; i++) + argv[i] = prefix_path(prefix, prefix_len, argv[i]); + } +} + static char const * const builtin_sparse_checkout_add_usage[] = { N_("git sparse-checkout add (--stdin | )"), NULL @@ -711,6 +732,8 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix) builtin_sparse_checkout_add_usage, PARSE_OPT_KEEP_UNKNOWN); + sanitize_paths(argc, argv, prefix); + return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD); } @@ -762,6 +785,8 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) if (!core_sparse_checkout_cone && argc == 0) { argv = default_patterns; argc = default_patterns_nr; + } else { + sanitize_paths(argc, argv, prefix); } return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE); diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index ce5e7c19efa..c1f86b0e02e 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -798,4 +798,45 @@ test_expect_success 'malformed cone-mode patterns' ' grep "warning: disabling cone pattern matching" err ' +test_expect_success 'set from subdir pays attention to prefix' ' + git -C repo sparse-checkout disable && + git -C repo/deep sparse-checkout set --cone deeper2 ../folder1 && + + git -C repo sparse-checkout list >actual && + + cat >expect <<-\EOF && + deep/deeper2 + folder1 + EOF + test_cmp expect actual +' + +test_expect_success 'add from subdir pays attention to prefix' ' + git -C repo sparse-checkout set --cone deep/deeper2 && + git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 && + + git -C repo sparse-checkout list >actual && + + cat >expect <<-\EOF && + deep/deeper1/deepest + deep/deeper2 + folder1 + EOF + test_cmp expect actual +' + +test_expect_success 'set from subdir in non-cone mode throws an error' ' + git -C repo sparse-checkout disable && + test_must_fail git -C repo/deep sparse-checkout set --no-cone deeper2 ../folder1 2>error && + + grep "run from the toplevel directory in non-cone mode" error +' + +test_expect_success 'set from subdir in non-cone mode throws an error' ' + git -C repo sparse-checkout set --no-cone deep/deeper2 && + test_must_fail git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 2>error && + + grep "run from the toplevel directory in non-cone mode" error +' + test_done From patchwork Thu Feb 17 06:54:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12749478 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 25D9AC433EF for ; Thu, 17 Feb 2022 06:55:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235865AbiBQGzQ (ORCPT ); Thu, 17 Feb 2022 01:55:16 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235830AbiBQGzK (ORCPT ); Thu, 17 Feb 2022 01:55:10 -0500 Received: from mail-wr1-x42e.google.com (mail-wr1-x42e.google.com [IPv6:2a00:1450:4864:20::42e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25BFD27F298 for ; Wed, 16 Feb 2022 22:54:56 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id u2so6093924wrw.1 for ; Wed, 16 Feb 2022 22:54:56 -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=JUZpigJx7CL2piKB9j7OB1j0JGFx2ejxjC82vGxYfAY=; b=XEkq18JP+nahGAmd6TUuNfCzQjKlA/Ez7KDVlE1NFaYK+bd2r9++6Q3v7tkBPBpQ2T rdLMO1HMPapcYRELlo/2FRAIo7Zk6LOGRJ/iVSbJKJ+iB+fayAjPJENzK1kWmJS9v9dA M7Grnb9ONomqnMZEdwmGYH67Uij7P1c4P6Bwni9UoLA6zYRx46W/CMkB5suLJfNaiZPl DEMc3nSSglvw2qHxgdD3HCV+pUvwjxD6GZIAFaI8q180q6GD4kNQ3vEMVgPFf+gzTKgL Wn79kpzvK2pgXvxIG4ZxgCpwsJuAYutjL2TZkH5IKvccIrr1QTXvbY1maVeL7rRdHoIa fZdQ== 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=JUZpigJx7CL2piKB9j7OB1j0JGFx2ejxjC82vGxYfAY=; b=NYwp0OPMAqbl6gUpUZbl4f00CUVGYDEWCugt6t30QOS/7XQNczLukkpo+pSVeQ77Fp vacaLmylqvYr4TzygNlx1zgp4ozYUoVVnbIb2cD6eu3Cv9fFNY2/BljyeDn7ZdY4EbX2 eorrmQgDiF1PWWX58hYPjiBkjsg2+dhwlK+qdBzuIkkm9I+WlHwUYdbUT38u+STnTdoH 5JQbJyUNFnRsNhyf+XWNIBFVxlju3slBjI9DekdMKD9u/ZEEHJjGcUBwqPSZTobCY1ax yKZ3g/jb+q1yxjJYO2xCb3nU+z0JIl3+zujdx4XjrjwPiXO9A6NBlQETWKaGUgJuBbfT dNbQ== X-Gm-Message-State: AOAM533LW4yf+KLiqZ4BoPJv68ZbS5Z6creOtZtVvWcZ0WHYdhkWCF7h CkvFxM0TLT6u2tNE6jijjFpv7O8lceU= X-Google-Smtp-Source: ABdhPJyGJcDf5r6YHESuwDv/mp8DNR+jgGObvDXeIdxrAtG1wbVurS59XBE55uq/sad6yOoP0UKVYA== X-Received: by 2002:a5d:6c62:0:b0:1e8:d8a7:f7de with SMTP id r2-20020a5d6c62000000b001e8d8a7f7demr701918wrz.38.1645080894559; Wed, 16 Feb 2022 22:54:54 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p2sm31165128wrt.101.2022.02.16.22.54.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 22:54:54 -0800 (PST) Message-Id: <1fdebc1953fcc30a6a08b3efd62dcdb31f7e4704.1645080889.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 17 Feb 2022 06:54:48 +0000 Subject: [PATCH v4 4/5] sparse-checkout: error or warn when given individual files Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Victoria Dye , Derrick Stolee , Lessley Dennington , Derrick Stolee , Elijah Newren , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren The set and add subcommands accept multiple positional arguments. The meaning of these arguments differs slightly in the two modes: Cone mode only accepts directories. If given a file, it would previously treat it as a directory, causing not just the file itself to be included but all sibling files as well -- likely against users' expectations. Throw an error if the specified path is a file in the index. Provide a --skip-checks argument to allow users to override (e.g. for the case when the given path IS a directory on another branch). Non-cone mode accepts general gitignore patterns. There are many reasons to avoid this mode, but one possible reason to use it instead of cone mode: to be able to select individual files within a directory. However, if a file is passed to set/add in non-cone mode, you won't be selecting a single file, you'll be selecting a file with the same name in any directory. Thus users will likely want to prefix any paths they specify with a leading '/' character; warn users if the patterns they specify exactly name a file because it means they are likely missing such a missing leading slash. Reviewed-by: Derrick Stolee Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 43 +++++++++++++++++++++++++----- t/t1091-sparse-checkout-builtin.sh | 16 ++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index a2192a69219..64583fa704f 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -1,4 +1,5 @@ #include "builtin.h" +#include "cache.h" #include "config.h" #include "dir.h" #include "parse-options.h" @@ -684,8 +685,11 @@ static int modify_pattern_list(int argc, const char **argv, int use_stdin, return result; } -static void sanitize_paths(int argc, const char **argv, const char *prefix) +static void sanitize_paths(int argc, const char **argv, + const char *prefix, int skip_checks) { + int i; + if (!argc) return; @@ -694,7 +698,6 @@ static void sanitize_paths(int argc, const char **argv, const char *prefix) * The args are not pathspecs, so unfortunately we * cannot imitate how cmd_add() uses parse_pathspec(). */ - int i; int prefix_len = strlen(prefix); if (!core_sparse_checkout_cone) @@ -703,20 +706,44 @@ static void sanitize_paths(int argc, const char **argv, const char *prefix) for (i = 0; i < argc; i++) argv[i] = prefix_path(prefix, prefix_len, argv[i]); } + + if (skip_checks) + return; + + for (i = 0; i < argc; i++) { + struct cache_entry *ce; + struct index_state *index = the_repository->index; + int pos = index_name_pos(index, argv[i], strlen(argv[i])); + + if (pos < 0) + continue; + ce = index->cache[pos]; + if (S_ISSPARSEDIR(ce->ce_mode)) + continue; + + if (core_sparse_checkout_cone) + die(_("\"%s\" is not a directory; to treat it as a directory anyway, rerun with --skip-checks"), argv[i]); + else + warning(_("pass a leading slash before paths such as \"%s\" if you want a single file (see NON-CONE PROBLEMS in the git-sparse-checkout manual)."), argv[i]); + } } static char const * const builtin_sparse_checkout_add_usage[] = { - N_("git sparse-checkout add (--stdin | )"), + N_("git sparse-checkout add [--skip-checks] (--stdin | )"), NULL }; static struct sparse_checkout_add_opts { + int skip_checks; int use_stdin; } add_opts; static int sparse_checkout_add(int argc, const char **argv, const char *prefix) { static struct option builtin_sparse_checkout_add_options[] = { + OPT_BOOL_F(0, "skip-checks", &add_opts.skip_checks, + N_("skip some sanity checks on the given paths that might give false positives"), + PARSE_OPT_NONEG), OPT_BOOL(0, "stdin", &add_opts.use_stdin, N_("read patterns from standard in")), OPT_END(), @@ -732,19 +759,20 @@ static int sparse_checkout_add(int argc, const char **argv, const char *prefix) builtin_sparse_checkout_add_usage, PARSE_OPT_KEEP_UNKNOWN); - sanitize_paths(argc, argv, prefix); + sanitize_paths(argc, argv, prefix, add_opts.skip_checks); return modify_pattern_list(argc, argv, add_opts.use_stdin, ADD); } static char const * const builtin_sparse_checkout_set_usage[] = { - N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] (--stdin | )"), + N_("git sparse-checkout set [--[no-]cone] [--[no-]sparse-index] [--skip-checks] (--stdin | )"), NULL }; static struct sparse_checkout_set_opts { int cone_mode; int sparse_index; + int skip_checks; int use_stdin; } set_opts; @@ -758,6 +786,9 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) N_("initialize the sparse-checkout in cone mode")), OPT_BOOL(0, "sparse-index", &set_opts.sparse_index, N_("toggle the use of a sparse index")), + OPT_BOOL_F(0, "skip-checks", &set_opts.skip_checks, + N_("skip some sanity checks on the given paths that might give false positives"), + PARSE_OPT_NONEG), OPT_BOOL_F(0, "stdin", &set_opts.use_stdin, N_("read patterns from standard in"), PARSE_OPT_NONEG), @@ -786,7 +817,7 @@ static int sparse_checkout_set(int argc, const char **argv, const char *prefix) argv = default_patterns; argc = default_patterns_nr; } else { - sanitize_paths(argc, argv, prefix); + sanitize_paths(argc, argv, prefix, set_opts.skip_checks); } return modify_pattern_list(argc, argv, set_opts.use_stdin, REPLACE); diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index c1f86b0e02e..3b39329266b 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -562,7 +562,7 @@ test_expect_success 'different sparse-checkouts with worktrees' ' ' test_expect_success 'set using filename keeps file on-disk' ' - git -C repo sparse-checkout set a deep && + git -C repo sparse-checkout set --skip-checks a deep && cat >expect <<-\EOF && /* !/*/ @@ -839,4 +839,18 @@ test_expect_success 'set from subdir in non-cone mode throws an error' ' grep "run from the toplevel directory in non-cone mode" error ' +test_expect_success 'by default, cone mode will error out when passed files' ' + git -C repo sparse-checkout reapply --cone && + test_must_fail git -C repo sparse-checkout add .gitignore 2>error && + + grep ".gitignore.*is not a directory" error +' + +test_expect_success 'by default, non-cone mode will warn on individual files' ' + git -C repo sparse-checkout reapply --no-cone && + git -C repo sparse-checkout add .gitignore 2>warning && + + grep "pass a leading slash before paths.*if you want a single file" warning +' + test_done From patchwork Thu Feb 17 06:54:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12749477 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 5FA78C433F5 for ; Thu, 17 Feb 2022 06:55:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235848AbiBQGzO (ORCPT ); Thu, 17 Feb 2022 01:55:14 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:48926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235833AbiBQGzK (ORCPT ); Thu, 17 Feb 2022 01:55:10 -0500 Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4CB727F299 for ; Wed, 16 Feb 2022 22:54:56 -0800 (PST) Received: by mail-wr1-x42a.google.com with SMTP id x5so2389649wrg.13 for ; Wed, 16 Feb 2022 22:54:56 -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=vGo4Cu0wMK+GKV1DY6HP5Ycd5t3WJLkBzJplqc6dbYU=; b=YrAKu6RQT5iKnX1UEbgtniTHsmEG/VufG4y004H6zSNcVosCaK763irCUyIfOLci/V DvQJyvKWzT+XWCj08a0NYIKWeKdCzTeZJG1sn2v09bzXNGqGSqS0jJm81rhtyHn6vxqD mZsQuJNrEFAFhVvVxLPgoTTNjXXJ+h1GUQ5fo1j5IzphqZRlL5T5zrMImpXm3wFmjVI7 AtD43y+6LGzfE0tI5cmz12k9CPbrmTySazvgq+wrQnboKyrGLNxw5H6bkbn/tmdpCT4x pZ8dsqQp0w0salH/qLQrd6bZvF3JYBg+iB7hKdFj+ibBIx+ssoQ6qZslrCQgXOK6mYxi KgSQ== 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=vGo4Cu0wMK+GKV1DY6HP5Ycd5t3WJLkBzJplqc6dbYU=; b=KSZJhK3ZfVYeNGsg3Cw7OkUgp+LHqaDRqM2sxn+gQiJDPSGvIIY79t8hRdzrS9ezve +fi91mHRctgohQYO67VKld+FksFBAixucZJhky8xgAMI54yICeyus2a6hTiFAcibVc3N Ut1gQQqKNTTOIW+Gm15AVJw1QotKBF1RhqfY+2Nj3k1kENG2PSc9iIDhBRoAynoI2LJ5 YeWZCIp5Yx6tYNh5MZbyrd9oPVJkZp7rutHS/K5aBf0uFEE5fL/1b8L+3vvtR+tNiA3r QB8cs7A1OQXlLG0T1/R7BrYstovQWGMoAS6yL+0+YgRUHxquZ9+/K67x04SFvFe+44cm ZL/Q== X-Gm-Message-State: AOAM533Z4Eu5CFvB/SKAnI7DhkRwSAcYsuDFvX20xKyppGWs3f6jwTxd sQ9Tx8yPM/ipwLH2oTFOVnBdjFUf8r8= X-Google-Smtp-Source: ABdhPJxU7s+971wSvzGmOv7SgQWzyjM7EA80TrYN9zFwEenpiOJWvzgsy2G90f4vm5BBIcQkxq5Qpw== X-Received: by 2002:a5d:4534:0:b0:1e4:9d38:2d4f with SMTP id j20-20020a5d4534000000b001e49d382d4fmr1176396wra.2.1645080895411; Wed, 16 Feb 2022 22:54:55 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id z5sm281602wmp.10.2022.02.16.22.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Feb 2022 22:54:54 -0800 (PST) Message-Id: In-Reply-To: References: Date: Thu, 17 Feb 2022 06:54:49 +0000 Subject: [PATCH v4 5/5] sparse-checkout: reject arguments in cone-mode that look like patterns Fcc: Sent MIME-Version: 1.0 To: git@vger.kernel.org Cc: Victoria Dye , Derrick Stolee , Lessley Dennington , Derrick Stolee , Elijah Newren , Elijah Newren , Elijah Newren Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org From: Elijah Newren From: Elijah Newren In sparse-checkout add/set under cone mode, the arguments passed are supposed to be directories rather than gitignore-style patterns. However, given the amount of effort spent in the manual discussing patterns, it is easy for users to assume they need to pass patterns such as /foo/* or !/bar/*/ or perhaps they really do ignore the directory rule and specify a random gitignore-style pattern like *.c To help catch such mistakes, throw an error if any of the positional arguments: * starts with any of '/!' * contains any of '*?[]' Inform users they can pass --skip-checks if they have a directory that really does have such special characters in its name. (We exclude '\' because of sparse-checkout's special handling of backslashes; see the MINGW test in t1091.46.) Reviewed-by: Derrick Stolee Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 11 +++++++++++ t/t1091-sparse-checkout-builtin.sh | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 64583fa704f..14601b8bd67 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -710,6 +710,17 @@ static void sanitize_paths(int argc, const char **argv, if (skip_checks) return; + if (core_sparse_checkout_cone) { + for (i = 0; i < argc; i++) { + if (argv[i][0] == '/') + die(_("specify directories rather than patterns (no leading slash)")); + if (argv[i][0] == '!') + die(_("specify directories rather than patterns. If your directory starts with a '!', pass --skip-checks")); + if (strpbrk(argv[i], "*?[]")) + die(_("specify directories rather than patterns. If your directory really has any of '*?[]\\' in it, pass --skip-checks")); + } + } + for (i = 0; i < argc; i++) { struct cache_entry *ce; struct index_state *index = the_repository->index; diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index 3b39329266b..421127d99c2 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -673,7 +673,7 @@ test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' ' git -C escaped reset --hard $COMMIT && check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? && git -C escaped sparse-checkout init --cone && - git -C escaped sparse-checkout set zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" && + git -C escaped sparse-checkout set --skip-checks zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" && cat >expect <<-\EOF && /* !/*/