Subject: Re: [PATCH 1/2] bash: add helper function to get config variables for completion

From: Jonathan Nieder <[email-masked]>
Date: Thu, 14 Oct 2010 12:15:07 -0500

SZEDER Gábor wrote:

> Currently there are three completion functions that perform similar
> queries to 'git config' to get config variable names.

Good point.

> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -451,10 +451,7 @@ __git_remotes ()
>  		echo ${i#$d/remotes/}
>  	done
>  	[ "$ngoff" ] && shopt -u nullglob
> -	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
> -		i="${i#remote.}"
> -		echo "${i/.url*/}"
> -	done
> +	__git_get_config_variables "remote" "url"
>  }

Ok, so __git_get_config_variables $category $var means something like

	git config --get-regexp '$category[.].*[.]$var' |
	cut -d. -f2

> @@ -750,14 +747,16 @@ __git_compute_porcelain_commands ()
>  	: ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
>  }
>  
> -__git_aliases ()
> +# returns all config variables within a given section with an optional
> +# suffix, with both the section name and the suffix removed
> +__git_get_config_variables ()
>  {
> -	local i IFS=$'\n'
> -	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
> +	local section="$1" suffix="${2-}" i IFS=$'\n'
> +	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "$section\..*${suffix:+\.$suffix}" 2>/dev/null); do

Would it be possible to shorten this line?  e.g.

	for i in $(
		git --git-dir="$(__gitdir)" ...
	); do

or

	while read -r setting
	do
		...
	done < <(
		git --git-dir="$(__gitdir)" ...
	)

or

	local ... IFS=$'\n'
	set -- $(git ... )
	for i do
		...
	done