git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] bash: support pretty format aliases
@ 2010-10-10 21:34 SZEDER Gábor
  2010-10-10 21:44 ` Jonathan Nieder
  0 siblings, 1 reply; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-10 21:34 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: Junio C Hamano, git, 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.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)

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

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

* Re: [PATCH] bash: support pretty format aliases
  2010-10-10 21:34 [PATCH] bash: support pretty format aliases SZEDER Gábor
@ 2010-10-10 21:44 ` Jonathan Nieder
  2010-10-10 22:06   ` [PATCH v2] " SZEDER Gábor
  0 siblings, 1 reply; 8+ messages in thread
From: Jonathan Nieder @ 2010-10-10 21:44 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Shawn O. Pearce, Junio C Hamano, git

SZEDER Gábor wrote:

> 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?

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

* [PATCH v2] bash: support pretty format aliases
  2010-10-10 21:44 ` Jonathan Nieder
@ 2010-10-10 22:06   ` SZEDER Gábor
  2010-10-14  8:55     ` SZEDER Gábor
  0 siblings, 1 reply; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-10 22:06 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Shawn O. Pearce, Junio C Hamano, git

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:
> 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(-)

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

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

* Re: [PATCH v2] bash: support pretty format aliases
  2010-10-10 22:06   ` [PATCH v2] " SZEDER Gábor
@ 2010-10-14  8:55     ` SZEDER Gábor
  2010-10-14  8:58       ` [PATCH 1/2] bash: add helper function to get config variables for completion SZEDER Gábor
  2010-10-14  8:58       ` [PATCH 2/2] bash: support pretty format aliases SZEDER Gábor
  0 siblings, 2 replies; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-14  8:55 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Jonathan Nieder, Shawn O. Pearce, git

On Mon, Oct 11, 2010 at 12:06:22AM +0200, SZEDER Gábor wrote:
> 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?

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

* [PATCH 1/2] bash: add helper function to get config variables for completion
  2010-10-14  8:55     ` SZEDER Gábor
@ 2010-10-14  8:58       ` SZEDER Gábor
  2010-10-14 17:15         ` Jonathan Nieder
  2010-10-14  8:58       ` [PATCH 2/2] bash: support pretty format aliases SZEDER Gábor
  1 sibling, 1 reply; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-14  8:58 UTC (permalink / raw)
  To: Junio C Hamano, Shawn O. Pearce; +Cc: Jonathan Nieder, git, SZEDER Gábor

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

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

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

* [PATCH 2/2] bash: support pretty format aliases
  2010-10-14  8:55     ` SZEDER Gábor
  2010-10-14  8:58       ` [PATCH 1/2] bash: add helper function to get config variables for completion SZEDER Gábor
@ 2010-10-14  8:58       ` SZEDER Gábor
  1 sibling, 0 replies; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-14  8:58 UTC (permalink / raw)
  To: Junio C Hamano, Shawn O. Pearce; +Cc: Jonathan Nieder, git, 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.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 contrib/completion/git-completion.bash |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

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

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

* Re: [PATCH 1/2] bash: add helper function to get config variables for completion
  2010-10-14  8:58       ` [PATCH 1/2] bash: add helper function to get config variables for completion SZEDER Gábor
@ 2010-10-14 17:15         ` Jonathan Nieder
  2010-10-20 22:29           ` SZEDER Gábor
  0 siblings, 1 reply; 8+ messages in thread
From: Jonathan Nieder @ 2010-10-14 17:15 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: Junio C Hamano, Shawn O. Pearce, git

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

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

* Re: [PATCH 1/2] bash: add helper function to get config variables for completion
  2010-10-14 17:15         ` Jonathan Nieder
@ 2010-10-20 22:29           ` SZEDER Gábor
  0 siblings, 0 replies; 8+ messages in thread
From: SZEDER Gábor @ 2010-10-20 22:29 UTC (permalink / raw)
  To: Jonathan Nieder; +Cc: Junio C Hamano, Shawn O. Pearce, git

Hi Jonathan,


On Thu, Oct 14, 2010 at 12:15:07PM -0500, Jonathan Nieder wrote:
> 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?).

> > @@ -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

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

end of thread, other threads:[~2010-10-20 22:29 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-10 21:34 [PATCH] bash: support pretty format aliases SZEDER Gábor
2010-10-10 21:44 ` Jonathan Nieder
2010-10-10 22:06   ` [PATCH v2] " SZEDER Gábor
2010-10-14  8:55     ` SZEDER Gábor
2010-10-14  8:58       ` [PATCH 1/2] bash: add helper function to get config variables for completion SZEDER Gábor
2010-10-14 17:15         ` Jonathan Nieder
2010-10-20 22:29           ` SZEDER Gábor
2010-10-14  8:58       ` [PATCH 2/2] bash: support pretty format aliases SZEDER Gábor

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).