From patchwork Thu Nov 23 17:44:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 13466485 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FuiynoiJ" Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5676010DC for ; Thu, 23 Nov 2023 09:44:12 -0800 (PST) Received: by mail-lj1-x22e.google.com with SMTP id 38308e7fff4ca-2c8879a1570so14273261fa.1 for ; Thu, 23 Nov 2023 09:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700761450; x=1701366250; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=BhdjFcwMeFtf9KqLxqQNOAWKxKUu7DGHAtTISD+MFi0=; b=FuiynoiJD/OnfQTnCr3Y2lvMor5QmR2zwwJX/65YaEtZzIIiHCkTiNYw/9cmdDX73G fmnQ9FzdB5YmE3gSLyp1lWBYwV5oQYI5KJWt5KUfsS1Oar3TKhTwFts0wYY34ittdHK9 poTEUZVFqy/r+afYP45aUZKvCA3hpEBjdk1DzYvgiroHlB1qHA2Ur5PkDHWH7PPjw0hi /D/68v5pJq+RuQapx03/p6mkr59ys0tFOsqw1E3gyjE1B3FL6Z7M5qFbKW7vi613HLTQ dGMUOghKV0v+DWna9AHBI8K1rIakyu/47xqtOdxrtVyRyazegDn0bn7nKI2lBwtBcbfP 2Wug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700761450; x=1701366250; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BhdjFcwMeFtf9KqLxqQNOAWKxKUu7DGHAtTISD+MFi0=; b=u8j7MmitcOQVwlWBoi0GV5XMCuuOM5R7g+9eUkVG7/VdSCRrSHzIH5jrqn843IKnLr i8YVfCfvfAl3xdVx5xfE+DHgHePocpD9tHQ0GZkTBN1135ElYj/IZUf4xwdkOFEZQXTK iFSifleBLHtzV9/deRKs3gzmMXIxX0CJJ9a/numt7vZtsZiaDlJfbDeBub/O/XvwMjy3 g8QQvTLZ6Mr3+afHhApvt9m80eTVqLbTKWpODei18NNE6fE+C73T4UL+TJ1p2tBXgceo WCBQBiVp6rHlsef374kQuiRoueLvdd9I5RadsaRTTHtqK9eZzGPjgktmF1iKo64WUvIH H/ug== X-Gm-Message-State: AOJu0YwbgoFmZRzGdSD2duK5CG+y5AlTEvPW7Vo/Hj1FvuisfP7aUZPe WGXjQ+Kcx8W9w+/7bV99RXPilhFWsgE= X-Google-Smtp-Source: AGHT+IEumz3VLYpzb6zvAsqbEyl9Zz+qeon+Lz+uuUdlMiEgKqucs3fOlRnqmyq2GQut9e0RJQqBPQ== X-Received: by 2002:a2e:9d88:0:b0:2c4:fe14:a85 with SMTP id c8-20020a2e9d88000000b002c4fe140a85mr91907ljj.18.1700761449992; Thu, 23 Nov 2023 09:44:09 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id g9-20020a05600c310900b004094e565e71sm2680762wmo.23.2023.11.23.09.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 09:44:09 -0800 (PST) Message-ID: <591c7b8d73b1a93feaa749d68156a198a7e32a9c.1700761448.git.gitgitgadget@gmail.com> In-Reply-To: References: Date: Thu, 23 Nov 2023 17:44:05 +0000 Subject: [PATCH 1/4] completion: squelch stray errors in sparse-checkout completion Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Elijah Newren , Elijah Newren From: Elijah Newren From: Elijah Newren If, in the root of a project, one types git sparse-checkout set --cone ../ then an error message of the form fatal: ../: '../' is outside repository at '/home/newren/floss/git' is written to stderr, which munges the users view of their own command. Squelch such messages. Signed-off-by: Elijah Newren --- contrib/completion/git-completion.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index ba5c395d2d8..6fced40d04c 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3014,7 +3014,7 @@ __gitcomp_directories () COMPREPLY+=("$c/") _found=1 fi - done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir) + done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir 2>/dev/null) if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then # No possible further completions any deeper, so assume we're at From patchwork Thu Nov 23 17:44:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 13466486 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="aZc5+uW4" Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC5E410DE for ; Thu, 23 Nov 2023 09:44:12 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-32faea0fa1fso662119f8f.1 for ; Thu, 23 Nov 2023 09:44:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700761450; x=1701366250; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=6OqsDbCE8j+Lny46tUTWAxtnemlTWw9DECnHUnT/N4g=; b=aZc5+uW4GIx/yjJIfae9P+neHvq7G6/xGbjuD2eI3avWip00xXiW8pOg7Q5Boz/Dzu MUOznY+FBR3owDYIdEICzim/6QidKp0HoXZBKOUS6A/oIfKduSGmMyVuN/oqt0yYeec6 GX7O1YNrJE7WmaAYyRCMgv+O1Q1zJ9TRFYvDv5ga2PiekpWbmZvR7RhZ28xmwn+bZX/E IiLyfVD7lDdF0ZlEct6pWgfojtiOb4lyo+3zWcDkrV/YUJXVHHCA77+xHQWfmoKS5QB6 113SCyVit/cQG4rl8hDfRMrHSL+3G/1PgTBUGiT1791SSFFADZWijFbqkhLGxgboHB2T diQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700761450; x=1701366250; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=6OqsDbCE8j+Lny46tUTWAxtnemlTWw9DECnHUnT/N4g=; b=u/DJELw0LCWZtV6PwvS9vtO+d5QymbwuKxGwc/2E+X/VX/H6sj8DVReKUDKoB8ZhD0 IMJinpsMGqheVq71FExlfRoK9QyNlDSnfkD6lqKqkqnZ+3GwVokPVhS5pFdmFWGW4gcN Npuxuvnln37NhOF+7gNO2666uyrzuSkE82w67wMzHrMP3UIFfCPXQPcsXCaUBoQLXXzW 2HdeUeYx2WYh7tkbgnlYiiG/7YKO3BZs3bo86ACvFh+W8Tl+6hSsZjtfWboSCVHN+n7T 7pUMRWwb2B2GAaxXN1J+BSZ0SCC2x8D5XK+KpuxXNX7+mWT6/+aW7ybhDQPmNgoWXaez jx8Q== X-Gm-Message-State: AOJu0YxqApEXDha2BraW69Ui+v2rr6jLVbqbkH1Akft/oPuhhFNQn/r6 Jhk07sGTQO3tKDVb9CVRZ/+yQi0sHEs= X-Google-Smtp-Source: AGHT+IHOYyLrTRIBPLdawt3py7HjEJGFgbLTjKW+OzH/I7I+SxGryke9LD7vxG0yKcTQqmnxWX1fHw== X-Received: by 2002:a5d:4ece:0:b0:332:e2e5:46a5 with SMTP id s14-20020a5d4ece000000b00332e2e546a5mr2499680wrv.7.1700761450568; Thu, 23 Nov 2023 09:44:10 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id c9-20020adfef49000000b00331698cb263sm2243183wrp.103.2023.11.23.09.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 09:44:10 -0800 (PST) Message-ID: In-Reply-To: References: Date: Thu, 23 Nov 2023 17:44:06 +0000 Subject: [PATCH 2/4] completion: fix logic for determining whether cone mode is active Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Elijah Newren , Elijah Newren From: Elijah Newren From: Elijah Newren _git_sparse_checkout() was checking whether we were in cone mode by checking whether either: A) core.sparseCheckoutCone was "true" B) "--cone" was specified on the command line This code has 2 bugs I didn't catch in my review at the time 1) core.sparseCheckout must be "true" for core.sparseCheckoutCone to be relevant (which matters since "git sparse-checkout disable" only unsets core.sparseCheckout, not core.sparseCheckoutCone) 2) The presence of "--no-cone" should override any config setting Further, I forgot to update this logic as part of 2d95707a02 ("sparse-checkout: make --cone the default", 2022-04-22) for the new default. Update the code for the new default and make it be more careful in determining whether to complete based on cone mode or non-cone mode. Signed-off-by: Elijah Newren --- contrib/completion/git-completion.bash | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 6fced40d04c..42e9e0cba8f 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3027,6 +3027,7 @@ _git_sparse_checkout () { local subcommands="list init set disable add reapply" local subcommand="$(__git_find_on_cmdline "$subcommands")" + local using_cone=true if [ -z "$subcommand" ]; then __gitcomp "$subcommands" return @@ -3037,8 +3038,15 @@ _git_sparse_checkout () __gitcomp_builtin sparse-checkout_$subcommand "" "--" ;; set,*|add,*) - if [ "$(__git config core.sparseCheckoutCone)" == "true" ] || - [ -n "$(__git_find_on_cmdline --cone)" ]; then + if [[ "$(__git config core.sparseCheckout)" == "true" && + "$(__git config core.sparseCheckoutCone)" == "false" && + -z "$(__git_find_on_cmdline --cone)" ]]; then + using_cone=false + fi + if [[ -n "$(__git_find_on_cmdline --no-cone)" ]]; then + using_cone=false + fi + if [[ "$using_cone" == "true" ]]; then __gitcomp_directories fi esac From patchwork Thu Nov 23 17:44:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 13466487 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I7JtCEMc" 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 EC61B10CB for ; Thu, 23 Nov 2023 09:44:13 -0800 (PST) Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-4083f61322fso8386415e9.1 for ; Thu, 23 Nov 2023 09:44:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700761452; x=1701366252; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=Gk91LFUa4fhLzX3tW1H9Cp25C+W2Wc+wvRl7tqLimVQ=; b=I7JtCEMcpPeBAs7Jt1oDYTWRJ3LJBq+4XwAdiPYWVGJ5NOi2dn3uO+ChcZzeoUJMrO bRB56lfBgLYJTJofjZFBESj3bE9Fyn8HDMlRpvqicAeWZBY0Ji5T0Cjne8otISqimZd1 EnSs3r2ETBHXhdwgtCZAEGmJAK8hmmWqLDqu480yleUa+mpEGoBQ++vENcyFNuVm+LS6 W+BLTfC+qFOrMY2SXNPeuz0cWl2u6Pu+NNTrn0ZBPvNJ6aTsyBxowM09Iv3WWJZGaf6O jM58toLOEwFN74uEqaQgBVuiN/LGcYAakbnv7cYEdNVRN3Ujb5xEI8cZCLsLhku+oI4m LFnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700761452; x=1701366252; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Gk91LFUa4fhLzX3tW1H9Cp25C+W2Wc+wvRl7tqLimVQ=; b=DUATdEMzxlRuFFkebzZALl7EqbUK7htgABAhdh5U/o/1/jyyvnExa6VYGp2Hh1CYdI Nd0PFtMnJqX3h0mRddCEHMaNutjJD7xDZUM+U5jDDnBguTSZl1ii+DDWEsBh211hQbJi iF6k94olSY94K52CdPj4wZ7od/ayCKN8NulSSa7XDCG8o1RgtExlFgGQMgXUASBiCD5j y9eTIRsH6aTxTL5CYfrEu0kqX9cGD0PZos/5EVTcEvUy2rn5rWY+MoMkDQ58AKK8TicV PI1F4K1uPkOX7xw3NIajWlLlY9zRPCDXsJ2sMdDvfm0YGi9oU4uyG1JuuoKJmetEzbC7 849g== X-Gm-Message-State: AOJu0YxFgIRQVK3wBSZAOmHhcb2ChRPSacFDY8oFyyzB2d8Zfshf/W7D 30foGJ8r19fKLS1/Rj2pSbButEdgRmk= X-Google-Smtp-Source: AGHT+IF8L2IMV1iTuLU5tCWS9jYx1JkLw54r9empUxHFn2/65h/uhPfCumeXelDgVzNdFvfctH7kTQ== X-Received: by 2002:adf:f88c:0:b0:32f:e1a2:525d with SMTP id u12-20020adff88c000000b0032fe1a2525dmr117069wrp.57.1700761451747; Thu, 23 Nov 2023 09:44:11 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id dm16-20020a0560000bd000b0032d09f7a713sm2258101wrb.18.2023.11.23.09.44.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 09:44:10 -0800 (PST) Message-ID: In-Reply-To: References: Date: Thu, 23 Nov 2023 17:44:07 +0000 Subject: [PATCH 3/4] completion: avoid misleading completions in cone mode Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Elijah Newren , Elijah Newren From: Elijah Newren From: Elijah Newren The "set" and "add" subcommands of "sparse-checkout", when in cone mode, should only complete on directories. For bash_completion in general, when no completions are returned for any subcommands, it will often fall back to standard completion of files and directories as a substitute. That is not helpful here. Since we have already looked for all valid completions, if none are found then falling back to standard bash file and directory completion is at best actively misleading. In fact, there are three different ways it can be actively misleading. Add a long comment in the code about how that fallback behavior can deceive, and disable the fallback by returning a fake result as the sole completion. Signed-off-by: Elijah Newren --- contrib/completion/git-completion.bash | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 42e9e0cba8f..136faeca1e9 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3020,6 +3020,26 @@ __gitcomp_directories () # No possible further completions any deeper, so assume we're at # a leaf directory and just consider it complete __gitcomp_direct_append "$cur " + elif [[ $_found == 0 ]]; then + # No possible completions found. Avoid falling back to + # bash's default file and directory completion, because all + # valid completions have already been searched and the + # fallbacks can do nothing but mislead. In fact, they can + # mislead in three different ways: + # 1) Fallback file completion makes no sense when asking + # for directory completions, as this function does. + # 2) Fallback directory completion is bad because + # e.g. "/pro" is invalid and should NOT complete to + # "/proc". + # 3) Fallback file/directory completion only completes + # on paths that exist in the current working tree, + # i.e. which are *already* part of their + # sparse-checkout. Thus, normal file and directory + # completion is always useless for "git + # sparse-checkout add" and is also probelmatic for + # "git sparse-checkout set" unless using it to + # strictly narrow the checkout. + COMPREPLY=( "" ) fi } From patchwork Thu Nov 23 17:44:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elijah Newren X-Patchwork-Id: 13466488 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Hu46gnU9" Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A736DD7E for ; Thu, 23 Nov 2023 09:44:15 -0800 (PST) Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-50abb83866bso1404556e87.3 for ; Thu, 23 Nov 2023 09:44:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700761453; x=1701366253; darn=vger.kernel.org; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:from:to:cc:subject:date :message-id:reply-to; bh=SYf5HOvxzIIEU0VsptTaeM75T1DhYCIY3Ox5+Aluhyo=; b=Hu46gnU9T3udvyyE/OV9eNESbP6/+clL/a1sTweK6tYUVzD41Jfh/L6s9CtMsXXRKs 2kPUWlRitLzYHhN4mUiNMKqI00q0bk6GZb1fWuw78VIxuWaP/MizdaZ63PTHqPb6KJWG m58DFrUGLiMpKl/u8E+UIqkrmluib1Y4hDdG2f8YdTRq1POZ7Oqlp07VKjipmP5b5gey qHqThQAk5BGP8E1EFN+xqLcwzREEQoA1bwEHthlAaC76z+aR0XKzRAk5TbcXrPUb/DgC tYzYUEZfImoeUDIIn6BvsyD7/RzUo68niyVYbL/3v8bLJXZb5h9QVJWXjZn0N0Y1t3wd mLbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700761453; x=1701366253; h=cc:to:mime-version:content-transfer-encoding:fcc:subject:date:from :references:in-reply-to:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SYf5HOvxzIIEU0VsptTaeM75T1DhYCIY3Ox5+Aluhyo=; b=PWjMw0wynXltQ5Vk6DfdnxZnyq40dEEiG/N7628GTNM7dsbfGQqezAa3XbYEx883WZ q+4cdXSnSpsFbZYkozOU1FyhlUY7ldMPd4KFtqMgcUp43xLsB8zSskjWutOQ6v8iRIFW rrkrESE0HqYBgxQCU46Yk83lcUcMa+jp4BO3N4aFVjXvvdhVfg/meZoudplX0DdoViE0 /3dcTE/e1Ip48ZGpKGrxukljouy3eHQVqBafmrJkbe8TC79w2QL9uqz8DfZkkQVxc9kQ 0PInDC5IlrsBK+UBJy1LoN3ecEo2DAMp+1L2V0+Y1bi/0mhCf8Pr+oOaGdCe8ZddBN1a ZrZA== X-Gm-Message-State: AOJu0YxTFZWNZJ6jRITLP5haZNmQnQhY3NUUf0kA4dvLSFYGXlM39VnN fbV08ThxRqkrLaoCTDIu34XSDh9NhLQ= X-Google-Smtp-Source: AGHT+IFouscYUf1WrGpmNZMBsVFqbnirqvJQ6APY3t4l70iAZJerAp0y5hv3gQXasBFQrtIdf+7Yww== X-Received: by 2002:ac2:4466:0:b0:50a:9f35:4fc7 with SMTP id y6-20020ac24466000000b0050a9f354fc7mr3981584lfl.2.1700761453036; Thu, 23 Nov 2023 09:44:13 -0800 (PST) Received: from [127.0.0.1] ([13.74.141.28]) by smtp.gmail.com with ESMTPSA id p4-20020a05600c468400b003fc0505be19sm2684890wmo.37.2023.11.23.09.44.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Nov 2023 09:44:12 -0800 (PST) Message-ID: In-Reply-To: References: Date: Thu, 23 Nov 2023 17:44:08 +0000 Subject: [PATCH 4/4] completion: avoid user confusion in non-cone mode Fcc: Sent Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 To: git@vger.kernel.org Cc: Elijah Newren , Elijah Newren From: Elijah Newren From: Elijah Newren It is tempting to think of "files and directories" of the current directory as valid inputs to the add and set subcommands of git sparse-checkout. However, in non-cone mode, they often aren't and using them as potential completions leads to *many* forms of confusion: Issue #1. It provides the *wrong* files and directories. For git sparse-checkout add we always want to add files and directories not currently in our sparse checkout, which means we want file and directories not currently present in the current working tree. Providing the files and directories currently present is thus always wrong. For git sparse-checkout set we have a similar problem except in the subset of cases where we are trying to narrow our checkout to a strict subset of what we already have. That is not a very common scenario, especially since it often does not even happen to be true for the first use of the command; for years we required users to create a sparse-checkout via git sparse-checkout init git sparse-checkout set (or use a clone option that did the init step for you at clone time). The init command creates a minimal sparse-checkout with just the top-level directory present, meaning the set command has to be used to expand the checkout. Thus, only in a special and perhaps unusual cases would any of the suggestions from normal file and directory completion be appropriate. Issue #2: Suggesting patterns that lead to warnings is unfriendly. If the user specifies any regular file and omits the leading '/', then the sparse-checkout command will warn the user that their command is problematic and suggest they use a leading slash instead. Issue #3: Completion gets confused by leading '/', and provides wrong paths. Users often want to anchor their patterns to the toplevel of the repository, especially when listing individual files. There are a number of reasons for this, but notably even sparse-checkout encourages them to do so (as noted above). However, if users do so (via adding a leading '/' to their pattern), then bash completion will interpret the leading slash not as a request for a path at the toplevel of the repository, but as a request for a path at the root of the filesytem. That means at best that completion cannot help with such paths, and if it does find any completions, they are almost guaranteed to be wrong. Issue #4: Suggesting invalid patterns from subdirectories is unfriendly. There is no per-directory equivalent to .gitignore with sparse-checkouts. There is only a single worktree-global $GIT_DIR/info/sparse-checkout file. As such, paths to files must be specified relative to the toplevel of a repository. Providing suggestions of paths that are relative to the current working directory, as bash completion defaults to, is wrong when the current working directory is not the worktree toplevel directory. Issue #5: Paths with special characters will be interpreted incorrectly The entries in the sparse-checkout file are patterns, not paths. While most paths also qualify as patterns (though even in such cases it would be better for users to not use them directly but prefix them with a leading '/'), there are a variety of special characters that would need special escaping beyond the normal shell escaping: '*', '?', '\', '[', ']', and any leading '#' or '!'. If completion suggests any such paths, users will likely expect them to be treated as an exact path rather than as a pattern that might match some number of files other than 1. Because of the combination of the above issues, turn completion off for the `set` and `add` subcommands of `sparse-checkout` when in non-cone mode, but leave a NEEDSWORK comment specifying what could theoretically be done if someone wanted to provide completion rules that were more helpful than harmful. Signed-off-by: Elijah Newren --- contrib/completion/git-completion.bash | 61 ++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 136faeca1e9..7d460da2fab 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -3068,6 +3068,67 @@ _git_sparse_checkout () fi if [[ "$using_cone" == "true" ]]; then __gitcomp_directories + else + # NEEDSWORK: It might be useful to provide a + # completion function which: + # + # 1. Provides completions based on + # files/directories that exist in HEAD, not + # just those currently present in the working + # tree. Bash's default file and directory + # completion is totally useless for "git + # sparse-checkout add" because of this. It is + # likewise problematic for "git + # sparse-checkout set" except in those subset + # of cases when trying to narrow scope to a + # strict subset of what you already have + # checked out. + # + # 2. Always provides file/directory completions + # with a prepended leading '/', so that + # files/directories are only searched at the + # relevant level rather than throughout all + # trees in the hierarchy. Doing this also + # avoids suggesting the user run a + # sparse-checkout command that will result in + # a warning be thrown at the user. + # + # 3. Does not accidentally search the root of + # the filesystem when a path with a leading + # slash is specified. ("git sparse-checkout + # add /ho" should not complete to + # "/home" but to e.g. "/hooks" if there is a + # "hooks" in the top of the repository.) + # + # 4. Provides no completions when run from a + # subdirectory of the repository root. (If we + # did provide file/directory completions, the + # user would just get a "please run from the + # toplevel directory" error message when they + # ran it. *Further*, if the user did rerun + # the command from the toplevel, the + # completions we previously provided would + # likely be wrong as they'd be relative to the + # subdirectory rather than the repository + # root. That could lead to users getting a + # nasty surprise based on trying to use a + # command we helped them create.) + # + # 5. Provides escaped completions for any paths + # containing a '*', '?', '\', '[', ']', or + # leading '#' or '!'. (These characters might + # already be escaped to protect from the + # shell, but they need an *extra* layer of + # escaping to prevent the pattern parsing in + # Git from seeing them as special characters.) + # + # Of course, this would be a lot of work, so for now, + # just avoid the many forms of user confusion that + # could be caused by providing bad completions by + # providing a fake completion to avoid falling back to + # bash's normal file and directory completion. + + COMPREPLY=( "" ) fi esac }