git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] completion: bash: improve alias loop detection
@ 2020-11-12 22:34 Felipe Contreras
  2020-11-17 18:56 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Felipe Contreras @ 2020-11-12 22:34 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, SZEDER Gábor, Felipe Contreras

It is possible for the name of an alias to end with the name of another
alias, in which case the code will incorrectly detect a loop.

We can fix that by adding an extra space between words.

Suggested-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 contrib/completion/git-completion.bash | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index ce0dc1e0f8..cd2a02bafd 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -1121,16 +1121,17 @@ __git_pretty_aliases ()
 # __git_aliased_command requires 1 argument
 __git_aliased_command ()
 {
-	local cur=$1 list word cmdline
+	local cur=$1 last list word cmdline
 
 	while [[ -n "$cur" ]]; do
-		if [[ "$list" == *"$cur "* ]]; then
+		if [[ "$list" == *" $cur "* ]]; then
 			# loop detected
 			return
 		fi
 
 		cmdline=$(__git config --get "alias.$cur")
-		list="$cur $list"
+		list=" $cur $list"
+		last=$cur
 		cur=
 
 		for word in $cmdline; do
@@ -1154,7 +1155,7 @@ __git_aliased_command ()
 		done
 	done
 
-	cur="${list%% *}"
+	cur=$last
 	if [[ "$cur" != "$1" ]]; then
 		echo "$cur"
 	fi
-- 
2.29.2


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

* Re: [PATCH] completion: bash: improve alias loop detection
  2020-11-12 22:34 [PATCH] completion: bash: improve alias loop detection Felipe Contreras
@ 2020-11-17 18:56 ` Junio C Hamano
  2020-11-17 19:52   ` Felipe Contreras
  0 siblings, 1 reply; 3+ messages in thread
From: Junio C Hamano @ 2020-11-17 18:56 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: git, SZEDER Gábor

Felipe Contreras <felipe.contreras@gmail.com> writes:

> It is possible for the name of an alias to end with the name of another
> alias, in which case the code will incorrectly detect a loop.
>
> We can fix that by adding an extra space between words.
>
> Suggested-by: SZEDER Gábor <szeder.dev@gmail.com>
> Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> ---
>  contrib/completion/git-completion.bash | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)

OK.  An extra local variable would not hurt and makes the code
easier to read.

Will queue.


> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index ce0dc1e0f8..cd2a02bafd 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -1121,16 +1121,17 @@ __git_pretty_aliases ()
>  # __git_aliased_command requires 1 argument
>  __git_aliased_command ()
>  {
> -	local cur=$1 list word cmdline
> +	local cur=$1 last list word cmdline
>  
>  	while [[ -n "$cur" ]]; do
> -		if [[ "$list" == *"$cur "* ]]; then
> +		if [[ "$list" == *" $cur "* ]]; then
>  			# loop detected
>  			return
>  		fi
>  
>  		cmdline=$(__git config --get "alias.$cur")
> -		list="$cur $list"
> +		list=" $cur $list"
> +		last=$cur
>  		cur=
>  
>  		for word in $cmdline; do
> @@ -1154,7 +1155,7 @@ __git_aliased_command ()
>  		done
>  	done
>  
> -	cur="${list%% *}"
> +	cur=$last
>  	if [[ "$cur" != "$1" ]]; then
>  		echo "$cur"
>  	fi

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

* Re: [PATCH] completion: bash: improve alias loop detection
  2020-11-17 18:56 ` Junio C Hamano
@ 2020-11-17 19:52   ` Felipe Contreras
  0 siblings, 0 replies; 3+ messages in thread
From: Felipe Contreras @ 2020-11-17 19:52 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Git, SZEDER Gábor

On Tue, Nov 17, 2020 at 12:57 PM Junio C Hamano <gitster@pobox.com> wrote:
>
> Felipe Contreras <felipe.contreras@gmail.com> writes:
>
> > It is possible for the name of an alias to end with the name of another
> > alias, in which case the code will incorrectly detect a loop.
> >
> > We can fix that by adding an extra space between words.
> >
> > Suggested-by: SZEDER Gábor <szeder.dev@gmail.com>
> > Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
> > ---
> >  contrib/completion/git-completion.bash | 9 +++++----
> >  1 file changed, 5 insertions(+), 4 deletions(-)
>
> OK.  An extra local variable would not hurt and makes the code
> easier to read.

It was there on v2. Which now may be a more reasonable sequence.

-- 
Felipe Contreras

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

end of thread, other threads:[~2020-11-17 19:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-12 22:34 [PATCH] completion: bash: improve alias loop detection Felipe Contreras
2020-11-17 18:56 ` Junio C Hamano
2020-11-17 19:52   ` Felipe Contreras

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 NNTP newsgroup(s).