Git development
 help / color / mirror / Atom feed
* [PATCH] t9902: Instruct git-completion.bash about a test mode
@ 2013-01-24 21:20 Jean-Noël AVILA
  2013-01-24 21:46 ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Jean-Noël AVILA @ 2013-01-24 21:20 UTC (permalink / raw)
  To: git

In test mode, git completion should propose commands only if they
belong to the list of authorized commands.

Signed-off-by: Jean-Noel Avila <jn.avila@free.fr>
---

Better show some code than try to explain. Here is what I mean by
"filter the output git help -a". 

 contrib/completion/git-completion.bash | 16 +++++++++++++++-
 t/t9902-completion.sh                  |  4 ++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 14dd5e7..6490553 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -531,6 +531,20 @@ __git_complete_strategy ()
 	return 1
 }
 
+if test -z "$AUTHORIZED_CMD_LIST"; then
+	__git_cmdlist ()
+	{ 
+		echo $1;
+	}
+else
+	__git_cmdlist ()
+	{ 
+		if [[ " $AUTHORIZED_CMD_LIST " =~ " $1 " ]] ; then
+				echo $1
+		fi
+	}
+fi
+
 __git_list_all_commands ()
 {
 	local i IFS=" "$'\n'
@@ -538,7 +552,7 @@ __git_list_all_commands ()
 	do
 		case $i in
 		*--*)             : helper pattern;;
-		*) echo $i;;
+		*) __git_cmdlist $i;;
 		esac
 	done
 }
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
index 3cd53f8..5e7d81e 100755
--- a/t/t9902-completion.sh
+++ b/t/t9902-completion.sh
@@ -12,8 +12,8 @@ complete ()
 	# do nothing
 	return 0
 }
-
-. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
+AUTHORIZED_CMD_LIST=" checkout show add filter-branch ls-files send-email describe"
+. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" 
 
 # We don't need this function to actually join words or do anything special.
 # Also, it's cleaner to avoid touching bash's internal completion variables.
-- 
1.8.1.1.271.g02f55e6

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

* Re: [PATCH] t9902: Instruct git-completion.bash about a test mode
  2013-01-24 21:20 [PATCH] t9902: Instruct git-completion.bash about a test mode Jean-Noël AVILA
@ 2013-01-24 21:46 ` Junio C Hamano
  2013-01-24 22:04   ` Jean-Noël AVILA
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2013-01-24 21:46 UTC (permalink / raw)
  To: Jean-Noël AVILA; +Cc: git, Jeff King

"Jean-Noël AVILA" <jn.avila@free.fr> writes:

> In test mode, git completion should propose commands only if they
> belong to the list of authorized commands.
>
> Signed-off-by: Jean-Noel Avila <jn.avila@free.fr>
> ---
>
> Better show some code than try to explain. Here is what I mean by
> "filter the output git help -a". 

Why do you have to make an extra shell function call for each and
every possible Git subcommand to slow down everybody's work when not
in testing mode?

Comparing it with Peff's suggestion, it is fairly clear which one we
should pick, I think.



>  contrib/completion/git-completion.bash | 16 +++++++++++++++-
>  t/t9902-completion.sh                  |  4 ++--
>  2 files changed, 17 insertions(+), 3 deletions(-)
>
> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> index 14dd5e7..6490553 100644
> --- a/contrib/completion/git-completion.bash
> +++ b/contrib/completion/git-completion.bash
> @@ -531,6 +531,20 @@ __git_complete_strategy ()
>  	return 1
>  }
>  
> +if test -z "$AUTHORIZED_CMD_LIST"; then
> +	__git_cmdlist ()
> +	{ 
> +		echo $1;
> +	}
> +else
> +	__git_cmdlist ()
> +	{ 
> +		if [[ " $AUTHORIZED_CMD_LIST " =~ " $1 " ]] ; then
> +				echo $1
> +		fi
> +	}
> +fi
> +
>  __git_list_all_commands ()
>  {
>  	local i IFS=" "$'\n'
> @@ -538,7 +552,7 @@ __git_list_all_commands ()
>  	do
>  		case $i in
>  		*--*)             : helper pattern;;
> -		*) echo $i;;
> +		*) __git_cmdlist $i;;
>  		esac
>  	done
>  }
> diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> index 3cd53f8..5e7d81e 100755
> --- a/t/t9902-completion.sh
> +++ b/t/t9902-completion.sh
> @@ -12,8 +12,8 @@ complete ()
>  	# do nothing
>  	return 0
>  }
> -
> -. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
> +AUTHORIZED_CMD_LIST=" checkout show add filter-branch ls-files send-email describe"
> +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" 
>  
>  # We don't need this function to actually join words or do anything special.
>  # Also, it's cleaner to avoid touching bash's internal completion variables.

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

* Re: [PATCH] t9902: Instruct git-completion.bash about a test mode
  2013-01-24 21:46 ` Junio C Hamano
@ 2013-01-24 22:04   ` Jean-Noël AVILA
  2013-01-24 22:29     ` Junio C Hamano
  0 siblings, 1 reply; 5+ messages in thread
From: Jean-Noël AVILA @ 2013-01-24 22:04 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King

Le jeudi 24 janvier 2013 22:46:13, Junio C Hamano a écrit :
> "Jean-Noël AVILA" <jn.avila@free.fr> writes:
> 
> > In test mode, git completion should propose commands only if they
> > belong to the list of authorized commands.
> >
> > Signed-off-by: Jean-Noel Avila <jn.avila@free.fr>
> > ---
> >
> > Better show some code than try to explain. Here is what I mean by
> > "filter the output git help -a". 
> 
> Why do you have to make an extra shell function call for each and
> every possible Git subcommand to slow down everybody's work when not
> in testing mode?
> 

My rational was to be sure to put the environment variable out of the 
way once the script has been sourced. I can make two alternative 
definitions of __git_list_all_commands () depending on the presence of 
$AUTHORIZED_CMD_LIST if you are worried about performance.

> Comparing it with Peff's suggestion, it is fairly clear which one we
> should pick, I think.
> 
> 
> 
> >  contrib/completion/git-completion.bash | 16 +++++++++++++++-
> >  t/t9902-completion.sh                  |  4 ++--
> >  2 files changed, 17 insertions(+), 3 deletions(-)
> >
> > diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
> > index 14dd5e7..6490553 100644
> > --- a/contrib/completion/git-completion.bash
> > +++ b/contrib/completion/git-completion.bash
> > @@ -531,6 +531,20 @@ __git_complete_strategy ()
> >  	return 1
> >  }
> >  
> > +if test -z "$AUTHORIZED_CMD_LIST"; then
> > +	__git_cmdlist ()
> > +	{ 
> > +		echo $1;
> > +	}
> > +else
> > +	__git_cmdlist ()
> > +	{ 
> > +		if [[ " $AUTHORIZED_CMD_LIST " =~ " $1 " ]] ; then
> > +				echo $1
> > +		fi
> > +	}
> > +fi
> > +
> >  __git_list_all_commands ()
> >  {
> >  	local i IFS=" "$'\n'
> > @@ -538,7 +552,7 @@ __git_list_all_commands ()
> >  	do
> >  		case $i in
> >  		*--*)             : helper pattern;;
> > -		*) echo $i;;
> > +		*) __git_cmdlist $i;;
> >  		esac
> >  	done
> >  }
> > diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
> > index 3cd53f8..5e7d81e 100755
> > --- a/t/t9902-completion.sh
> > +++ b/t/t9902-completion.sh
> > @@ -12,8 +12,8 @@ complete ()
> >  	# do nothing
> >  	return 0
> >  }
> > -
> > -. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
> > +AUTHORIZED_CMD_LIST=" checkout show add filter-branch ls-files send-email describe"
> > +. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" 
> >  
> >  # We don't need this function to actually join words or do anything special.
> >  # Also, it's cleaner to avoid touching bash's internal completion variables.
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 

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

* Re: [PATCH] t9902: Instruct git-completion.bash about a test mode
  2013-01-24 22:04   ` Jean-Noël AVILA
@ 2013-01-24 22:29     ` Junio C Hamano
  2013-01-24 22:56       ` Jean-Noël AVILA
  0 siblings, 1 reply; 5+ messages in thread
From: Junio C Hamano @ 2013-01-24 22:29 UTC (permalink / raw)
  To: Jean-Noël AVILA; +Cc: git, Jeff King

"Jean-Noël AVILA" <jn.avila@free.fr> writes:

> My rational was to be sure to put the environment variable out of the 
> way once the script has been sourced. I can make two alternative 
> definitions of __git_list_all_commands () depending on the presence of 
> $AUTHORIZED_CMD_LIST if you are worried about performance.

Actually, it does not matter, as once __git_list_all_commands is
run, its result is kept in a variable.

So Peff's

  if test -z "$FAKE_COMMAND_LIST"; then
          __git_cmdlist() {
                  git help -a | egrep '^  [a-zA-Z0-9]'
          }
  else
          __git_cmdlist() {
                  printf '%s' "$FAKE_COMMAND_LIST"
          }
  fi

would be just as simple if not simpler, does the same thing, and is
sufficient, I think.

The t9902 test is only interested in making sure that the completion
works, and we do not want "git help -a" that omits a subcommand from
its output that is not built in your particular environment to get
in the way, which will not be an issue with this approach.

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

* Re: [PATCH] t9902: Instruct git-completion.bash about a test mode
  2013-01-24 22:29     ` Junio C Hamano
@ 2013-01-24 22:56       ` Jean-Noël AVILA
  0 siblings, 0 replies; 5+ messages in thread
From: Jean-Noël AVILA @ 2013-01-24 22:56 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King

Le jeudi 24 janvier 2013 23:29:33, Junio C Hamano a écrit :
> "Jean-Noël AVILA" <jn.avila@free.fr> writes:
> 
> > My rational was to be sure to put the environment variable out of the 
> > way once the script has been sourced. I can make two alternative 
> > definitions of __git_list_all_commands () depending on the presence of 
> > $AUTHORIZED_CMD_LIST if you are worried about performance.
> 
> Actually, it does not matter, as once __git_list_all_commands is
> run, its result is kept in a variable.
> 
> So Peff's
> 
>   if test -z "$FAKE_COMMAND_LIST"; then
>           __git_cmdlist() {
>                   git help -a | egrep '^  [a-zA-Z0-9]'
>           }
>   else
>           __git_cmdlist() {
>                   printf '%s' "$FAKE_COMMAND_LIST"
>           }
>   fi
> 
> would be just as simple if not simpler, does the same thing, and is
> sufficient, I think.
> 
> The t9902 test is only interested in making sure that the completion
> works, and we do not want "git help -a" that omits a subcommand from
> its output that is not built in your particular environment to get
> in the way, which will not be an issue with this approach.
> 


Ah. I totally missed the point. I though that the requested change was
to intersect the list needed for the test with the one provided by the 
standard completion.

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

end of thread, other threads:[~2013-01-24 22:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-24 21:20 [PATCH] t9902: Instruct git-completion.bash about a test mode Jean-Noël AVILA
2013-01-24 21:46 ` Junio C Hamano
2013-01-24 22:04   ` Jean-Noël AVILA
2013-01-24 22:29     ` Junio C Hamano
2013-01-24 22:56       ` Jean-Noël AVILA

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox