From patchwork Tue Feb 15 08:32:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746694 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 B87A8C433F5 for ; Tue, 15 Feb 2022 08:32:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234322AbiBOIck (ORCPT ); Tue, 15 Feb 2022 03:32:40 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233799AbiBOIch (ORCPT ); Tue, 15 Feb 2022 03:32:37 -0500 Received: from mail-wr1-x434.google.com (mail-wr1-x434.google.com [IPv6:2a00:1450:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BD72AADFEE for ; Tue, 15 Feb 2022 00:32:27 -0800 (PST) Received: by mail-wr1-x434.google.com with SMTP id q7so30644656wrc.13 for ; Tue, 15 Feb 2022 00:32:27 -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=dZbtwX0GLSsu8C3WqwPHVHg7l/tECBP6cyoIzbFydJU=; b=GAtPpS6qwWsIsBtVHJDqI7/MTRZ1b/DSPLilyQpi9AWu1kdGxbsgQg1yiCs5N9C6LX bwlwMg12tL5GUESQTzl6ylSPPsZv3EgHz2lWIfcWKJgNnwnTmLfzk18wlihiPjxM5Z0G EHoVtXhZaCiDaTmxYnRxOIX4sPZdL0h9Ncn1yYk8hEfw44Fc2shVfshnbDs37ZowaLJJ uigG20Hclnt9ODqDVzsee+tFH3S3SPyce+kGU50MRRuppCAo2g0E0O+Ya0iadub1Y0bs 9Abf7CkJQoSLdutTNqtMdT9u2PapcL8nSJTImrm15b91PB7K03hDQCrt3iv53V++POVm lh2w== 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=dZbtwX0GLSsu8C3WqwPHVHg7l/tECBP6cyoIzbFydJU=; b=O1gWCQuIxtNQLyg/+1+e9C1niiItvy0JnEsJPSCtTCUFrkrHMzR0pQjBccbZpKKhPl 8v/fddyevq31VY7Am9bMist/wJ5Vhh9sZzdxAVf6MyDN3L+huTx/yBR6gSfTgFk7o8lq KExFfAzvIdX4NIE15aQcaXSRWXJBss1mgyG1BTloqsOQMe3WAdWsnknlFHWWybC5SGbO 0RC9GM54eBPosTVHktgADLX3rh9EB6F7LNQ4/sUg9j6r7jsjl6eT9xN3A1gd60J+ZdMG 6uoJDofC+neiffv1HzUQH83AJFcPsPJc4Srpl90QJFOBTAMJJsTB0G1i+aC/JgSThPru lj4w== X-Gm-Message-State: AOAM533fMq6vkG3v2byhRoHdxodJHJDfDsZ5RX6MA9SpJlCBoRVoOjJ6 cqeY7duvSD56SiiaJ5eamWk5iGbngHQ= X-Google-Smtp-Source: ABdhPJwEuwlgEy3lmn4CW2X5lzHLVPqpw7ZbT92aoDF2tMLxt8f3QB6mkJEjFAU6e7mams54kR91Xg== X-Received: by 2002:a05:6000:108a:: with SMTP id y10mr2207451wrw.464.1644913946123; Tue, 15 Feb 2022 00:32:26 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id t187sm14327721wma.22.2022.02.15.00.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:25 -0800 (PST) Message-Id: <5536fe6498e460e217a74c743f92c42b1615f9e5.1644913943.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:18 +0000 Subject: [PATCH v2 1/6] 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. 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 Tue Feb 15 08:32:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746695 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 E5376C433EF for ; Tue, 15 Feb 2022 08:32:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235465AbiBOIcm (ORCPT ); Tue, 15 Feb 2022 03:32:42 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233884AbiBOIch (ORCPT ); Tue, 15 Feb 2022 03:32:37 -0500 Received: from mail-wm1-x336.google.com (mail-wm1-x336.google.com [IPv6:2a00:1450:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 959D2C3309 for ; Tue, 15 Feb 2022 00:32:28 -0800 (PST) Received: by mail-wm1-x336.google.com with SMTP id m126-20020a1ca384000000b0037bb8e379feso903376wme.5 for ; Tue, 15 Feb 2022 00:32:28 -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=YIwZ1zfPp3II5K+wfNAzshh5OSylkxFuME6uVSGIRF4=; b=i7K8FnMCEPah4pqhbToU8yLwKL4lp7o5Wnl/mn/oXHzLDqSfUPAUc/I85Sl81cPht1 k+CKZOaEeqV2UiN1n7tUr4QM4vpN6eG/PokLQe5XXK5Zw9NeLzDKRZDXG71b+5hLJCYV TxnqnZpy9xzTzB5RDct9ORIKS7IFne1sexFC+N43WVxkKkjCVJ05kBCG2d4i6ycJMPj3 Z2LZkR44sh9/ddNgXDL64EpdLftqqHoPYzBzfJeJ7gzZDeGI+RfCDYWsZLy0MCgol1x4 DXCqItqqnu8CaFM9Nnjkd5FpnuFwOkau017BXiVPzi8D8Y7ONfCeP/wB/6QdMWW+sVs5 k7CQ== 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=YIwZ1zfPp3II5K+wfNAzshh5OSylkxFuME6uVSGIRF4=; b=nQyESKlmGgiA57COUWeKfl7iUQ5gaYWI3qif4qXZ0KFUVsRYaxkpZwwlbU4tBMwFCn 1P50vp027x/W5AX92ZXs+126YGUixtHxTv/m1YlohKxhWWY7dUOZv9cyJX3LnTYLXAJx dIJ5eFlCu/UekPXQ/xSlQ/P8+yxJ5Yv6StCSqVNjQOye3XmT7KqAcnLrssINMcBJcL5a DP6nDeKEQIsd+VjJquvAUiZ5xMfwOtuU1CH8i99e42vJYTJ1rdfo48nSDYrlSKNtpR5x ymeyEWYDk8cQiPu5M+DbdW/HMV3QglYSjPDQwrp5ljEdTXpGmXcQ36bSjcmUjDaxvVMl fHjA== X-Gm-Message-State: AOAM5332B01+6A/6iE0hGCvJBUhpAxEEMTbS8ouadsgfzJSMziD8Df6A 7axh3G1ZDb3oQwcjiiPZ09h24l6rfTw= X-Google-Smtp-Source: ABdhPJzorLbk6FDq6Z79gZHdW+8duqnTUdzXWaJvGpdE7YWS4nUChDcP5oTMM3QWkd/MuIprTOXItA== X-Received: by 2002:a1c:f403:: with SMTP id z3mr2121528wma.83.1644913946989; Tue, 15 Feb 2022 00:32:26 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id n2sm4980456wmq.6.2022.02.15.00.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:26 -0800 (PST) Message-Id: <9edad872e0d986c8f9e18dae58e86e826c97237b.1644913943.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:19 +0000 Subject: [PATCH v2 2/6] 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. 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 Tue Feb 15 08:32:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746696 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 CD4C7C433F5 for ; Tue, 15 Feb 2022 08:32:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235467AbiBOIcy (ORCPT ); Tue, 15 Feb 2022 03:32:54 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234106AbiBOIcl (ORCPT ); Tue, 15 Feb 2022 03:32:41 -0500 Received: from mail-wr1-x431.google.com (mail-wr1-x431.google.com [IPv6:2a00:1450:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B4B69A94EE for ; Tue, 15 Feb 2022 00:32:29 -0800 (PST) Received: by mail-wr1-x431.google.com with SMTP id w11so30754057wra.4 for ; Tue, 15 Feb 2022 00:32:29 -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=s5kIJ+O1UzcsJfrsoFb4Y6UDPypo9HQBG7KiiqSk1GY=; b=jjetzlqnhdFI6TFXIXtcN1X35rV1szyfh/G/Fd37C6dSMV20L+cAL9ns1cr9Qv5L4U pGfPnUW+3KSoMG9LijxfO9OcD20Ck94mmt0U0UgwHw0eQZU5owBbK9cpbGFeAEWJXvOg riPMAiDLofAwWd8cp8nPS72+0qU6J07Yb6eKyOd8qS+j6jhT+AhTcBd2KKEEBVNAdxbB ml3Ad15OvYz7LEWxyh7j9lsvbA3RZxm8XxnQULlQx06HNc00WTYGdBBb1EqFtVbtCnm5 UzZpYjx+D/uczu+Cg4iVxYy5VTizIY5OsyLA8v31Bx6ZgJ0vOAPcvAOFEnRAIuj5WCUZ Gnvg== 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=s5kIJ+O1UzcsJfrsoFb4Y6UDPypo9HQBG7KiiqSk1GY=; b=6e3Kn2CDak44TUHn9NVQ1Tn+SPhVq/WfjI3IaV2dFXmJnRTZsTlIll0shotUG7DCOE IbYvUdYAFa40b86oxEoGKIcB7xg8e05LHF/OILRo0/bEV7i0kMdYTmwS7J0sGFKTcFRe E0EXSTUh/gso8T1ukoreqJvvZedBRKGSUe6kzFkg+GLxT7oG68dw0o2R2+tVzaaB4lZS nDJIo6qLFjqjWUgY9J1jlupwzJXHA5HbSrCSTtCGaUCHLCaaBTHyI21Non1JIIFcqLa4 V98heohmEwPDH79yKUIi8BeESsJCviKAdKNHht6kTqeoBrZI9jLW/QapNbdl9m3r7jPV mP+A== X-Gm-Message-State: AOAM532G2j5sOR5/WBU+uzEZZzrT5Gmk+4p7hkP/2rMCXKwwMgFA/XSh 9ckGch81krrlr1672bOIlQjEndpJsM4= X-Google-Smtp-Source: ABdhPJzJhDmukeB0d0gXHT/Xl6nyThsEtgz0DZvFnIxpILfzV024/szkY8irXXL7mcJ6fe6Nn7HOyQ== X-Received: by 2002:adf:e112:: with SMTP id t18mr2315590wrz.411.1644913948062; Tue, 15 Feb 2022 00:32:28 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id r11sm22838560wrt.28.2022.02.15.00.32.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:27 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:20 +0000 Subject: [PATCH v2 3/6] 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 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 Tue Feb 15 08:32:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746697 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 075E7C433FE for ; Tue, 15 Feb 2022 08:32:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235472AbiBOIcz (ORCPT ); Tue, 15 Feb 2022 03:32:55 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233799AbiBOIcl (ORCPT ); Tue, 15 Feb 2022 03:32:41 -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 74CE9ADFEE for ; Tue, 15 Feb 2022 00:32:30 -0800 (PST) Received: by mail-wr1-x436.google.com with SMTP id v12so30797032wrv.2 for ; Tue, 15 Feb 2022 00:32:30 -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=A2xj2nIYxCEAnSBh0YLeCKK8z39CVzBA89hr84UYs4I=; b=NT2jm/Tx2tqgQERz1hAO/CFvDqYYATS3W/std/KjkT0JLOE7OJAb6Gr1DSvOhxr5Lu t0JXAV3fb5q1Cq1c39VHusBsCdxl4FW1mmbpiEb8Q0LBiku42MdU0xM+9RWJQkFXHOUn WCbVHFa3Ue8LAwPkjwOdZPRqiwCo02VPvhFkjiLzjyCA1mpKhSZsgZ8LsuvH6yiag4c9 WvMzinaTD9yy9xCqEhoX8BY/WJ3wJ9q3GpB6HE+CSjmghJrj8AgqYcpER6z+0zb69AN9 aU6hNhJiMJHmtWj4l2eAUKYp6TK9AR8IzZkRCeuPV9/M78n8f4yb62JNZGVFYAaKZsOZ xWig== 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=A2xj2nIYxCEAnSBh0YLeCKK8z39CVzBA89hr84UYs4I=; b=L9RzHTRYpjQlyJNz7qwnhlNNFDwVEQTX6q6l5+flAD2K9NNAcLZNqHDZyG92QyHcU4 S8nPQfaXLvbJvTfwE6CL8kSEzNm3Ubckrh2Je5MyipGOmov3z31AZaRh7rNuZVJsoqVI 6SsMfX/H3h09QY/y4tOLpiLhYeosIuzDeoqJAqM8FO7fVJhbT7sdAHuaHfHEPoXdYRYO 8FMlCKcSMMqSTY59+IMJ2UL3wR8iY2yP31ABcLfH43sttzDwKYZXJ9PYqV+2h/XkBOz0 PqD6iy2lus7EcL95xNeRELcW1voIF0ZrkX+bAHxBXvVHarlVfbzTllPuCFFJZFchJ3Bl ldGg== X-Gm-Message-State: AOAM532DYb6rQXAoyFn9jM2j2MNdnGgYM88d+P0r3V61Rt2t0FD+sjN1 coG6FGjTA3Pvx7KlMhMuqW4XUd7yHQM= X-Google-Smtp-Source: ABdhPJynHlqlSpqKQ5Ly5DpFNDbll2m+zov+HP21RsqWbhsw2Hlw1HPHbokDPuyiImubPhZ0KJbs0Q== X-Received: by 2002:a05:6000:1683:: with SMTP id y3mr2294894wrd.222.1644913948885; Tue, 15 Feb 2022 00:32:28 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id j10sm8705426wmq.20.2022.02.15.00.32.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:28 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:21 +0000 Subject: [PATCH v2 4/6] 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. 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 Tue Feb 15 08:32:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746698 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 64D3DC433EF for ; Tue, 15 Feb 2022 08:32:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235477AbiBOIc4 (ORCPT ); Tue, 15 Feb 2022 03:32:56 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:37368 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234569AbiBOIcl (ORCPT ); Tue, 15 Feb 2022 03:32:41 -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 49A9DC3309 for ; Tue, 15 Feb 2022 00:32:31 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id k1so30740890wrd.8 for ; Tue, 15 Feb 2022 00:32:31 -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=dHb1rCKERi19T6nYLYN6+lO0wE8S9YzSIrHxyIls5Uo=; b=ghaH3arehw06Xoip3gy1y29DS5XT3QdFEuG3ict4sIl54I8hRisQfDe+3RHNqO2oOw 2xXx6CNaIPXrEfSJ/6jDN7ITAe66xJFy2qhKt/5BHTfSdVyZrByuIV/QMPb1Qkl9/E0y GJwAMUolELMB69c2yVGQ3NXlihUAH6S/Fz+aInBWVnW6EBebYqJt3PBcXnXs2Q5zm1wY FfBGihcUvCKMS+lhLUELf3Uu0b8I9ARZJHSKFZMZ2MxQiU1cjePTYF0H7njLN8xATYDz G+CU0lVEPJLI9tiMCtFAzEEmB5KXHeQGXeJaMPxDTqvGo8qdb94FBWbJAD2sC/96t1dp 88sw== 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=dHb1rCKERi19T6nYLYN6+lO0wE8S9YzSIrHxyIls5Uo=; b=Z01+KBTaQuXh/CcHbdC2A7zbxnqKWEyuK4rEMzNZGPjeRrnY9zjwmW06ftneOCRRn8 NdREJUB3baoamv5+4TbUofcYoGQ77bXUEu9WG1HNNVLDE2jEqDFi+7mNfOoNYQ+X3Y7x ga0IwYstIr+I164dJpsBVur6lah6ZrxgV5FO9tZbs60jl5Q/Lyl/ROAF7UnMNIpN67M8 N6r6K3TqO3vdVsVta2XcVbFyxO6XdRqUoF+pJcuAVyTAP/ZtWLvjdT2I5vb/ARI5U5/W hSdDaICsn/TfrmgetBNFHzvPQxyBdOS1yZ1n2jbhTA9cWp1oB0+pWI4xpwEPlBoxY1Mp MJ6A== X-Gm-Message-State: AOAM532YaKXiIkXWQ0hGA9rOzBeEfGyBS2fwGz0z/wQiTx/Mew2X9t00 kqfrrwWHBPJ3vg5EwLpl82K7SiBl2lY= X-Google-Smtp-Source: ABdhPJxW1HRLV2mW2rziiW/t34mgifxjugNKurzbA3fMffOxM5yzQYQyFH2a+x6aAJ/10O3a317p1A== X-Received: by 2002:a5d:58c8:: with SMTP id o8mr2183222wrf.233.1644913949734; Tue, 15 Feb 2022 00:32:29 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id 7sm13869391wrb.43.2022.02.15.00.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:29 -0800 (PST) Message-Id: In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:22 +0000 Subject: [PATCH v2 5/6] sparse-checkout: reject non-cone-mode patterns starting with a '#' 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, in cone mode any specified directories will be transformed into appropriate patterns. In non-cone mode, the non-option arguments are treated as patterns. Since .git/info/sparse-checkout will ignore any patterns starting with a '#' (they are just gitignore patterns), if the user passes an argument starting with a '#' to sparse-checkout add/set in non-cone mode, it would just be treated as a comment and ignored. Error out in such cases, informing the user that they need to backslash escape it. Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 5 +++++ t/t1091-sparse-checkout-builtin.sh | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 64583fa704f..74d64ada9f9 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -710,6 +710,11 @@ 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(_("paths beginning with a '#' must be preceeded by a backslash")); + 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..3c811724d5d 100755 --- a/t/t1091-sparse-checkout-builtin.sh +++ b/t/t1091-sparse-checkout-builtin.sh @@ -853,4 +853,10 @@ test_expect_success 'by default, non-cone mode will warn on individual files' ' grep "pass a leading slash before paths.*if you want a single file" warning ' +test_expect_success 'paths starting with hash must be escaped in non-cone mode' ' + test_must_fail git -C repo sparse-checkout set --no-cone "#funny-path" 2>error && + + grep "paths beginning.*#.*must be preceeded by a backslash" error +' + test_done From patchwork Tue Feb 15 08:32:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 12746699 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 AE3B5C433F5 for ; Tue, 15 Feb 2022 08:32:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235484AbiBOIc6 (ORCPT ); Tue, 15 Feb 2022 03:32:58 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:38408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235468AbiBOIcw (ORCPT ); Tue, 15 Feb 2022 03:32:52 -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 6A86FC3313 for ; Tue, 15 Feb 2022 00:32:32 -0800 (PST) Received: by mail-wr1-x42e.google.com with SMTP id q7so30644922wrc.13 for ; Tue, 15 Feb 2022 00:32:32 -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=ZH+9Cxv/6UJbd1oWmz8Bh/o11mstupjqCb9ozi3qAfo=; b=hj/PKsHTvb+Bq6sJWH4Iz6evIXl84smJdAwhng02PORXXMgyoeYVljCD4QcILZUbdO AbLxB+zd5n0zF9B+nL+hUg2EnuZaaHMDl5pGe8aWOKypUKZyOJ4VVAVdFJzrwsvJaT1y Kk6NF6DVUZhL1EMFaCu89hrEZomQRccwYUkqiJ4P4wfVPPUlhxVVKyfvjUbqKEwHvsu9 MJ0IdbfVg/X0/pbJm7FHOqOBd912A2/ALHIrbyoVcwwNpWopjPIUpxiFxOb3s3rJufR5 BGsjE1DHL+d5S1uRO7gLRs5ym4GWCzf/h4HxCl4FXM+oSZFnF1fNsUQsG73vHoPvRQt8 VT/Q== 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=ZH+9Cxv/6UJbd1oWmz8Bh/o11mstupjqCb9ozi3qAfo=; b=L6HURZUw3oQkq5WnLM7Aeutw8pP/lHF5GnRfDhVt5N+HiCC4kH5oBUU7Zo3qq5JQ+U tKPqswZAePwsKH1MIKueyz/wsPvlIRV1uIHgeqp5QIS+uSiOu3f7FqaKrTv6G6EZjzBO aIfO9pSuYrWh+v0Vr2119TaUJ8VccU3mWlgZqGl0LPW30Yjpnmgsxr/m2AP/xl48U6vJ HcgCEVEr+3S18E/BUtyJUgw51oeh8aRjFUYp1lnfe7duVKlF/hOg2E8Z3c8vv8bcvwyz RGx9ru4u4dL4UqdLyoKqa8lGBjKiGig7h9FRetlDDK2PxwsGf1I7XKlyecTIuU7Exz43 R3Gg== X-Gm-Message-State: AOAM532MWbKS9FchCXxrxKT1zvAhVOwtvcyrms2sGNHVPZCl/Es3IPHU u+GxYzcrSTMX/t7le0nn0DcjPi17pdk= X-Google-Smtp-Source: ABdhPJzeOtvco1Dt3on0THjMLXNLJzrVRWWzm3HrhGWYRqBDkRhjq4YQOPeh2epywrEKesPML764rQ== X-Received: by 2002:adf:cd0f:: with SMTP id w15mr2164868wrm.335.1644913950481; Tue, 15 Feb 2022 00:32:30 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id b16sm17128780wmj.32.2022.02.15.00.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 00:32:30 -0800 (PST) Message-Id: <286c22e5ecdd1e19cc4ca53126b2616085de6133.1644913943.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Tue, 15 Feb 2022 08:32:23 +0000 Subject: [PATCH v2 6/6] 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. Signed-off-by: Elijah Newren --- builtin/sparse-checkout.c | 16 ++++++++++++++-- t/t1091-sparse-checkout-builtin.sh | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/builtin/sparse-checkout.c b/builtin/sparse-checkout.c index 74d64ada9f9..a30c77e7a1a 100644 --- a/builtin/sparse-checkout.c +++ b/builtin/sparse-checkout.c @@ -710,10 +710,22 @@ static void sanitize_paths(int argc, const char **argv, if (skip_checks) return; - if (!core_sparse_checkout_cone) - for (i = 0; i < argc; i++) + for (i = 0; i < argc; i++) { + if (core_sparse_checkout_cone) { + 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 (strchr(argv[i], '*') || + strchr(argv[i], '?') || + strchr(argv[i], '[') || + strchr(argv[i], ']')) + die(_("specify directories rather than patterns. If your directory really has any of '*?[]' in it, pass --skip-checks")); + } else { if (argv[i][0] == '#') die(_("paths beginning with a '#' must be preceeded by a backslash")); + } + } for (i = 0; i < argc; i++) { struct cache_entry *ce; diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh index 3c811724d5d..72157fc6f1f 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 && /* !/*/