git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] completion: add and use the __git_get_config_subsection helper function
@ 2023-11-13 22:25 SZEDER Gábor
  2023-11-14  1:08 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: SZEDER Gábor @ 2023-11-13 22:25 UTC (permalink / raw)
  To: git; +Cc: Philippe Blain, Junio C Hamano, SZEDER Gábor

Our Bash completion script recently learned to complete configured
trailer key aliases for 'git config --trailer=<TAB>', but the helper
function extracting the key alias from 'git config's output, i.e. the
subsection from 'trailer.*.key', ended up more complex than necessary,
with considerable overhead from executing four external processes in a
pipe.

Replace those commands in the pipe with a simple shell loop using only
a pair of parameter expansions and a builtin 'echo', which is easier
to understand and should perform better (I assume that users don't
have that many subsections in any particular section to make the
processing with an external process (let alone four) worth it).

And while at it, let's extract this loop into a generic
__git_get_config_subsections() helper function, as it might be useful
elsewhere in the future as well (at the moment it isn't, AFAICT).

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
---
 contrib/completion/git-completion.bash | 14 +++++++++++++-
 t/t9902-completion.sh                  | 13 +++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 13a39ebd2e..34bbb66f85 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1148,6 +1148,18 @@ __git_get_config_variables ()
 	done
 }
 
+# Lists all subsections in the given section which contain the given
+# config variable, with the section and variable names removed.
+__git_get_config_subsections ()
+{
+	local section="$1" var="$2" i IFS=$'\n'
+	for i in $(__git config --name-only --get-regexp "^$section\..*\.$var$"); do
+		i=${i#$section.}
+		i=${i%.$var}
+		echo "$i"
+	done
+}
+
 __git_pretty_aliases ()
 {
 	__git_get_config_variables "pretty"
@@ -1681,7 +1693,7 @@ __git_untracked_file_modes="all no normal"
 
 __git_trailer_tokens ()
 {
-	__git config --name-only --get-regexp '^trailer\..*\.key$' | cut -d. -f 2- | rev | cut -d. -f2- | rev
+	__git_get_config_subsections trailer key
 }
 
 _git_commit ()
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index a7c3b4eb63..11ed83d0ed 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -2130,6 +2130,19 @@ test_expect_success '__git_get_config_variables' '
 	test_cmp expect actual
 '
 
+test_expect_success '__git_get_config_subsections' '
+	cat >expect <<-\EOF &&
+	subsection-1
+	SubSection-2
+	sub.section.3
+	EOF
+	test_config interesting.subsection-1.name good &&
+	test_config Interesting.SubSection-2.Name good &&
+	test_config interesting.sub.section.3.name good &&
+	__git_get_config_subsections interesting name >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success '__git_pretty_aliases' '
 	cat >expect <<-EOF &&
 	author
-- 
2.43.0.rc1.528.g8f9d60d041


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-28 12:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-11-13 22:25 [PATCH] completion: add and use the __git_get_config_subsection helper function SZEDER Gábor
2023-11-14  1:08 ` Junio C Hamano
2023-11-28 12:40   ` SZEDER Gábor

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).