Skip to contentSkip to conversation

bash: support pretty format aliases

PATCH In review SZEDER Gábor 2 participants 8 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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=', 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.

---
 contrib/completion/git-completion.bash |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
contrib/completion/git-completion.bash +10 -1
index 6756990..3022213 100755
@@ -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
 		;;
Signature · 1 line
1.7.3.1.148.g2fffa
#1 lore raw
Quoted 7 lines · SZEDER Gábor

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?

#2 lore raw

PATCH v2 1/1 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.

---

Quoted 7 lines · Jonathan Nieder

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(-)
contrib/completion/git-completion.bash +17 -4
index 6756990..4d54c32 100755
@@ -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
 		;;
Signature · 1 line
1.7.3.1.148.g2fffa
#3 lore raw
Quoted 14 lines · SZEDER Gábor
contrib/completion/git-completion.bash +6 -0
index 6756990..4d54c32 100755
@@ -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.*)

contrib/completion/git-completion.bash __git_compute_porcelain_commands () · line 768

Quoted 10 lines
contrib/completion/git-completion.bash +7 -0
+			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?

#4 lore raw

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

---
 contrib/completion/git-completion.bash |   28 +++++++++++-----------------
 1 files changed, 11 insertions(+), 17 deletions(-)
contrib/completion/git-completion.bash +11 -17
index f83f019..97a3d8c 100755
@@ -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
Signature · 1 line
1.7.3.1.151.g3779c
#5 lore raw

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

Quoted 2 lines · SZEDER Gábor

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

Good point.

contrib/completion/git-completion.bash __git_remotes () · line 455

Quoted 12 lines
contrib/completion/git-completion.bash +1 -4
@@ -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

contrib/completion/git-completion.bash __git_compute_porcelain_commands () · line 755

Quoted 13 lines
contrib/completion/git-completion.bash +5 -3
@@ -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
#6 lore raw

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

Hi Jonathan,

Quoted 22 lines · Jonathan Nieder
Quoted 2 lines · SZEDER Gábor

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

Good point.

contrib/completion/git-completion.bash __git_remotes () · line 455

Quoted 12 lines
contrib/completion/git-completion.bash +1 -4
@@ -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

Quoted 2 lines
	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?).

Quoted 29 lines

contrib/completion/git-completion.bash __git_compute_porcelain_commands () · line 755

Quoted 13 lines
contrib/completion/git-completion.bash +5 -3
@@ -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)" ...
	)
Quoted 7 lines

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

#7 lore raw

PATCH 2/2 bash: support pretty format aliases

In reply to SZEDER Gábor

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.

---
 contrib/completion/git-completion.bash |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
contrib/completion/git-completion.bash +4 -0
index 97a3d8c..a4b6f78 100755
@@ -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
 		;;
Signature · 1 line
1.7.3.1.151.g3779c