threads / patch / 25412

patchbash: support pretty format aliases

Subject: [PATCH] bash: support pretty format aliases

## tl;dr

8 messages between Oct 10, 2010 and Oct 20, 2010. Diffs are folded; open one to read it.

replies: 7people: 2as markdown or json

SZEDER Gábor· Oct 10, 2010, 21:34 UTC · lore

Users can have their own pretty format aliases since 8028184 (pretty: add aliases for pretty formats, 2010-05-02), so let's offer those after '--pretty=', too.

Similar to the completion of aliases, this will invoke 'git config' each time pretty aliases needs to be completed, so changes in pretty.* configuration will be reflected immediately.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
Show changes to contrib/completion/git-completion.bash +10 −1
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6756990..3022213 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1368,7 +1368,16 @@ _git_log ()
 	fi
 	case "$cur" in
 	--pretty=*)
-		__gitcomp "$__git_log_pretty_formats
+		local pretty_aliases
+		for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
+			case "$i" in
+			pretty.*)
+				i="${i#pretty.}"
+				pretty_aliases="$pretty_aliases ${i/ */}"
+				;;
+			esac
+		done
+		__gitcomp "$__git_log_pretty_formats $pretty_aliases
 			" "" "${cur##--pretty=}"
 		return
 		;;
-- 
1.7.3.1.148.g2fffa
Jonathan Nieder· Oct 10, 2010, 21:44 UTC · re: SZEDER Gábor · lore

Re: [PATCH] bash: support pretty format aliases

SZEDER Gábor wrote:
Show 7 quoted lines
> Users can have their own pretty format aliases since 8028184 (pretty:
> add aliases for pretty formats, 2010-05-02), so let's offer those
> after '--pretty=', too.
> 
> Similar to the completion of aliases, this will invoke 'git config'
> each time pretty aliases needs to be completed, so changes in pretty.*
> configuration will be reflected immediately.
Does this apply to
	git log --format=
	git show --pretty=
	git show --format=
too?
SZEDER Gábor· Oct 10, 2010, 22:06 UTC · re: Jonathan Nieder · lore

[PATCH v2] bash: support pretty format aliases

Users can have their own pretty format aliases since 8028184 (pretty: add aliases for pretty formats, 2010-05-02), so let's offer those after '--pretty=' and '--format=' for 'log' and 'show', too.

Similar to the completion of aliases, this will invoke 'git config' each time pretty aliases needs to be completed, so changes in pretty.* configuration will be reflected immediately.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
On Sun, Oct 10, 2010 at 04:44:11PM -0500, Jonathan Nieder wrote:
Show 7 quoted lines
> Does this apply to
> 
> 	git log --format=
> 	git show --pretty=
> 	git show --format=
> 
> too?
Yes ;)
 contrib/completion/git-completion.bash |   21 +++++++++++++++++----
 1 files changed, 17 insertions(+), 4 deletions(-)
Show changes to contrib/completion/git-completion.bash +17 −4
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 6756990..4d54c32 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -750,6 +750,19 @@ __git_compute_porcelain_commands ()
 	: ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
 }
 
+__git_pretty_aliases ()
+{
+	local i IFS=$'\n'
+	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
+		case "$i" in
+		pretty.*)
+			i="${i#pretty.}"
+			echo "${i/ */}"
+			;;
+		esac
+	done
+}
+
 __git_aliases ()
 {
 	local i IFS=$'\n'
@@ -1368,12 +1381,12 @@ _git_log ()
 	fi
 	case "$cur" in
 	--pretty=*)
-		__gitcomp "$__git_log_pretty_formats
+		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
 			" "" "${cur##--pretty=}"
 		return
 		;;
 	--format=*)
-		__gitcomp "$__git_log_pretty_formats
+		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
 			" "" "${cur##--format=}"
 		return
 		;;
@@ -2100,12 +2113,12 @@ _git_show ()
 	local cur="${COMP_WORDS[COMP_CWORD]}"
 	case "$cur" in
 	--pretty=*)
-		__gitcomp "$__git_log_pretty_formats
+		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
 			" "" "${cur##--pretty=}"
 		return
 		;;
 	--format=*)
-		__gitcomp "$__git_log_pretty_formats
+		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
 			" "" "${cur##--format=}"
 		return
 		;;
-- 
1.7.3.1.148.g2fffa
SZEDER Gábor· Oct 14, 2010, 08:55 UTC · re: SZEDER Gábor · lore

Re: [PATCH v2] bash: support pretty format aliases

On Mon, Oct 11, 2010 at 12:06:22AM +0200, SZEDER Gábor wrote:
Show 24 quoted lines
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 6756990..4d54c32 100755
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -750,6 +750,19 @@ __git_compute_porcelain_commands ()
>  	: ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
>  }
>  
> +__git_pretty_aliases ()
> +{
> +	local i IFS=$'\n'
> +	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
> +		case "$i" in
> +		pretty.*)
> +			i="${i#pretty.}"
> +			echo "${i/ */}"
> +			;;
> +		esac
> +	done
> +}
> +
>  __git_aliases ()
>  {
>  	local i IFS=$'\n'
 
If you look at this new __git_pretty_aliases() function and the old
__git_aliases(), then you'll see that it's quite a code duplication.
So, how about the following two patches instead?
SZEDER Gábor· Oct 14, 2010, 08:58 UTC · re: SZEDER Gábor · lore

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

Currently there are three completion functions that perform similar queries to 'git config' to get config variable names. These are the completion of aliases, remotes, and remote groups for 'git remote update'. Since the following patch is about to add yet another similar 'git config'-querying completion function to support pretty aliases, it's time to introduce a unified helper function first to avoid redundant code.

We took care that the resulting helper function still copes well with newlines in config variable values and that it works with 'set -u' (see commits e0d7805 (completion: fix alias listings with newlines, 2009-10-08) and 25a31f8 (bash-completion: Support running when set -u is enabled, 2009-01-15) for details).

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |   28 +++++++++++-----------------
 1 files changed, 11 insertions(+), 17 deletions(-)
Show changes to contrib/completion/git-completion.bash +11 −17
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index f83f019..97a3d8c 100755
--- 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"
 }
 
 __git_list_merge_strategies ()
@@ -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
 		case "$i" in
-		alias.*)
-			i="${i#alias.}"
-			echo "${i/ */}"
+		$section.*)
+			i="${i#$section.}"
+			echo "${i/${suffix:+.$suffix} */}"
 			;;
 		esac
 	done
@@ -2017,12 +2016,7 @@ _git_remote ()
 		__gitcomp "$(__git_remotes)"
 		;;
 	update)
-		local i c='' IFS=$'\n'
-		for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
-			i="${i#remotes.}"
-			c="$c ${i/ */}"
-		done
-		__gitcomp "$c"
+		__gitcomp "$(__git_get_config_variables "remotes")"
 		;;
 	*)
 		COMPREPLY=()
@@ -2366,7 +2360,7 @@ _git ()
 			"
 			;;
 		*)     __git_compute_porcelain_commands
-		       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
+		       __gitcomp "$__git_porcelain_commands $(__git_get_config_variables "alias")" ;;
 		esac
 		return
 	fi
-- 
1.7.3.1.151.g3779c
Jonathan Nieder· Oct 14, 2010, 17:15 UTC · re: SZEDER Gábor · lore

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

SZEDER Gábor wrote:
> Currently there are three completion functions that perform similar
> queries to 'git config' to get config variable names.
Good point.
Show 12 quoted lines
> --- 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
Show 13 quoted lines
> @@ -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
SZEDER Gábor· Oct 20, 2010, 22:29 UTC · re: Jonathan Nieder · lore

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

Hi Jonathan,
On Thu, Oct 14, 2010 at 12:15:07PM -0500, Jonathan Nieder wrote:
Show 24 quoted lines
> 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

Almost. Considering the current invocations of __git_get_config_variables() introduced in this patch, yes, they do the same. But "cut -d. -f2" will behave differently when $category contains a dot, or when neither $category nor $var contain a dot, but the config variable contains more than two (does git have any such config variables?).

Show 36 quoted lines
> > @@ -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

Well, yes, of course. But the original line was already too long, and neither of your proposals in itself would make it short enough to fit 80 characters. Besides, the latter two changes the loop itself, not just what the body of the loop does and what it is looping on.

Maybe we could just split the line in two in the middle, like 
	for i in $(git --git-dir="$(__gitdir)" config --get-regexp \
		$section\..*${suffix:+\.$suffix}" 2>/dev/null); do
Still doesn't look pretty, but maybe a bit better.

Best, Gábor

SZEDER Gábor· Oct 14, 2010, 08:58 UTC · re: SZEDER Gábor · lore

[PATCH 2/2] bash: support pretty format aliases

Users can have their own pretty format aliases since 8028184 (pretty: add aliases for pretty formats, 2010-05-02), so let's offer those after '--pretty=' and '--format=' for 'log' and 'show', too.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
Show changes to contrib/completion/git-completion.bash +4 −0
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 97a3d8c..a4b6f78 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1368,11 +1368,13 @@ _git_log ()
 	case "$cur" in
 	--pretty=*)
 		__gitcomp "$__git_log_pretty_formats
+			$(__git_get_config_variables "pretty")
 			" "" "${cur##--pretty=}"
 		return
 		;;
 	--format=*)
 		__gitcomp "$__git_log_pretty_formats
+			$(__git_get_config_variables "pretty")
 			" "" "${cur##--format=}"
 		return
 		;;
@@ -2095,11 +2097,13 @@ _git_show ()
 	case "$cur" in
 	--pretty=*)
 		__gitcomp "$__git_log_pretty_formats
+			$(__git_get_config_variables "pretty")
 			" "" "${cur##--pretty=}"
 		return
 		;;
 	--format=*)
 		__gitcomp "$__git_log_pretty_formats
+			$(__git_get_config_variables "pretty")
 			" "" "${cur##--format=}"
 		return
 		;;
-- 
1.7.3.1.151.g3779c

← back to recent threads