git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] completion: add and use the __git_get_config_subsection helper function

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 14, 2023, 01:08 UTC
Message-ID
<xmqqzfzh16qp.fsf@gitster.g>
In-Reply-To
<20231113222528.62771-1-szeder.dev@gmail.com>
SZEDER Gábor <szeder.dev@gmail.com> writes:
Show 8 quoted lines
> +# 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}

As this script is allowed bash-isms, I wondered if we can use a single pattern substitution instead of two remove pre/suffix pattern substitution, but I guess it would not work, and the above is perfectly readable.

> +		echo "$i"

As the subsection is designed to contain unbounded set of end-user controlled names, we probably should do

		printf "%s\n" "$i"

instead to protect us from interesting names (e.g. ones that begin with a dash).

> +	done
> +}

Interesting to see that we do not need to bother deduplicating the output from here.

Show 20 quoted lines
> 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
> +'
Good to see an uppercase character is used here ;-).
Thanks.
Previous: SZEDER GáborNext: SZEDER Gábor
Message 2 of 3 in “completion: add and use the __git_get_config_subsection helper function”
  1. completion: add and use the __git_get_config_subsection helper functionSZEDER Gábor, Nov 13, 2023
  2. Junio C HamanoNov 14, 2023
  3. SZEDER GáborNov 28, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.