From: "David Mandelberg via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Jacob Keller <jacob.keller@gmail.com>,
Junio C Hamano <gitster@pobox.com>,
David Mandelberg <david@mandelberg.org>,
David Mandelberg <david@mandelberg.org>
Subject: [PATCH 2/3] completion: add helper to count path components
Date: Sun, 02 Mar 2025 07:45:24 +0000 [thread overview]
Message-ID: <4fab893d9fddd65b20d280edfb8cdf6ed8bfa295.1740901525.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1901.git.git.1740901525.gitgitgadget@gmail.com>
From: David Mandelberg <david@mandelberg.org>
A follow-up commit will use this with for-each-ref to strip the right
number of path components from refnames.
Signed-off-by: David Mandelberg <david@mandelberg.org>
---
contrib/completion/git-completion.bash | 11 +++++++++++
t/t9902-completion.sh | 26 ++++++++++++++++++++++++++
2 files changed, 37 insertions(+)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index cb10f818a81..17c044f7d60 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -246,6 +246,17 @@ __git_escape_fnmatch ()
printf '%s\n' "$s"
}
+# Prints the number of slash-separated components in a path.
+# 1: Path to count components of.
+__git_count_path_components ()
+{
+ local path="$1"
+ local relative="${path#/}"
+ relative="${relative%/}"
+ local slashes="/${relative//[^\/]}"
+ echo "${#slashes}"
+}
+
# The following function is based on code from:
#
# bash_completion - programmable completion functions for bash 3.2+
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index c5e91622876..0219408358c 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -462,6 +462,32 @@ test_expect_success '__git_escape_fnmatch' '
'
+test_expect_success '__git_count_path_components - no slashes' '
+ echo 1 >expected &&
+ __git_count_path_components a >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_count_path_components - relative' '
+ echo 3 >expected &&
+ __git_count_path_components a/b/c >"$actual" &&
+ test_cmp expected "$actual"
+
+'
+
+test_expect_success '__git_count_path_components - absolute' '
+ echo 3 >expected &&
+ __git_count_path_components /a/b/c >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_count_path_components - trailing slash' '
+ echo 3 >expected &&
+ __git_count_path_components a/b/c/ >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+
test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
sed -e "s/Z$//g" >expected <<-EOF &&
with-trailing-space Z
--
gitgitgadget
next prev parent reply other threads:[~2025-03-02 7:45 UTC|newest]
Thread overview: 44+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-02 7:45 [PATCH 0/3] completion: fix bugs with slashes in remote names David Mandelberg via GitGitGadget
2025-03-02 7:45 ` [PATCH 1/3] completion: add helper to escape strings for fnmatch David Mandelberg via GitGitGadget
2025-03-02 7:45 ` David Mandelberg via GitGitGadget [this message]
2025-03-02 7:45 ` [PATCH 3/3] completion: fix bugs with slashes in remote names David Mandelberg via GitGitGadget
2025-03-02 14:17 ` Phillip Wood
2025-03-02 20:34 ` David Mandelberg
2025-03-03 16:36 ` phillip.wood123
2025-03-03 19:19 ` David Mandelberg
2025-03-03 19:43 ` Todd Zullinger
2025-03-06 20:26 ` D. Ben Knoble
2025-03-06 20:24 ` D. Ben Knoble
2025-03-06 20:38 ` Junio C Hamano
2025-03-07 10:34 ` phillip.wood123
2025-03-07 21:46 ` David Mandelberg
2025-03-13 17:40 ` D. Ben Knoble
2025-03-13 20:19 ` David Mandelberg
2025-03-03 20:29 ` Junio C Hamano
2025-03-03 19:33 ` SZEDER Gábor
2025-03-03 19:49 ` David Mandelberg
2025-03-06 20:46 ` Junio C Hamano
2025-03-07 20:11 ` David Mandelberg
2025-03-07 20:57 ` Junio C Hamano
2025-03-07 21:38 ` David Mandelberg
2025-03-07 21:45 ` Junio C Hamano
2025-03-05 0:07 ` [PATCH v2 0/2] " David Mandelberg
2025-03-05 0:08 ` [PATCH v2 1/2] completion: add helper to count path components David Mandelberg
2025-03-05 0:09 ` [PATCH v2 2/2] completion: fix bugs with slashes in remote names David Mandelberg
2025-03-05 20:50 ` David Mandelberg
2025-03-06 16:35 ` Phillip Wood
2025-03-06 17:12 ` David Mandelberg
2025-03-06 17:39 ` Phillip Wood
2025-03-14 19:40 ` [PATCH v3 0/2] " David Mandelberg
2025-03-14 19:41 ` [PATCH v3 1/2] completion: add helper to count path components David Mandelberg
2025-03-14 19:43 ` [PATCH v3 2/2] completion: fix bugs with slashes in remote names David Mandelberg
2025-03-14 22:18 ` Junio C Hamano
2025-03-14 22:37 ` David Mandelberg
2025-03-14 23:23 ` Junio C Hamano
2025-03-18 17:02 ` Phillip Wood
2025-03-18 17:27 ` Junio C Hamano
2025-03-23 21:04 ` [PATCH v4 0/2] " David Mandelberg
2025-03-23 21:05 ` [PATCH v4 1/2] completion: add helper to count path components David Mandelberg
2025-03-23 21:06 ` [PATCH v4 2/2] completion: fix bugs with slashes in remote names David Mandelberg
2025-04-08 14:18 ` [PATCH v4 0/2] " Phillip Wood
2025-04-08 18:55 ` David Mandelberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4fab893d9fddd65b20d280edfb8cdf6ed8bfa295.1740901525.git.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=david@mandelberg.org \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jacob.keller@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).