git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] bash: __git_find_subcommand function
@ 2008-03-06 14:58 SZEDER Gábor
  2008-03-08  2:23 ` Shawn O. Pearce
  0 siblings, 1 reply; 3+ messages in thread
From: SZEDER Gábor @ 2008-03-06 14:58 UTC (permalink / raw)
  To: git; +Cc: spearce, SZEDER Gábor

This function takes one argument: a string containing all subcommands
separated by spaces.  The function searches through the command line
whether a subcommand is already present.  If a subcommand is found, it
will be printed to standard output.

This enables us to remove code duplications from completion
functions for commands having subcommands.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
This function does not return the index of the subcommand found on the
command line, which was in the $c variable previously.  However, $c was
only used in if statements, like:
	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
		__gitcomp "cmd1 cmd2 cmd3"
	fi
To my understanding the only(?) purpose of those if statements was to
prevent subcommands appearing again on the list of possible completions,
when there was one already on the command line.  But [ -z $command ] is
sufficient to detect those cases, so we can actually omit
[ $c -eq $COMP_CWORD ].  Is it right, or am I missing something?

Note, that some of the patches I sent out yesterday are in conflict with
these changes, namely:
* [PATCH 1/2] bash: add missing 'git stash save' subcommand
  is in conflict,
* [PATCH 2/2] bash: complete 'git stash' subcommands only once
  should be dropped,
* [PATCH 1/2] bash: add 'git svn' subcommands
  should be updated, and
* [PATCH 2/2] bash: add 'git svn' options
  should be updated (depends on the previous one).
I think that if this patch will get merged, then it should be merged
before those mentioned above.  It just doesn't make much sense e.g. to
merge 'complete 'git stash' subcommands only once' and then basically
revert it.  In this case, of course, I will send the updated patches.

 contrib/completion/git-completion.bash |   70 ++++++++++++++------------------
 1 files changed, 31 insertions(+), 39 deletions(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 49e6df0..f9e29be 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -420,6 +420,22 @@ __git_aliased_command ()
 	done
 }
 
+__git_find_subcommand ()
+{
+	local word subcommand c=1
+
+	while [ $c -lt $COMP_CWORD ]; do
+		word="${COMP_WORDS[c]}"
+		for subcommand in $1; do
+			if [ "$subcommand" = "$word" ]; then
+				echo "$subcommand"
+				return
+			fi
+		done
+		c=$((++c))
+	done
+}
+
 __git_whitespacelist="nowarn warn error error-all strip"
 
 _git_am ()
@@ -477,24 +493,13 @@ _git_add ()
 
 _git_bisect ()
 {
-	local i c=1 command
-	while [ $c -lt $COMP_CWORD ]; do
-		i="${COMP_WORDS[c]}"
-		case "$i" in
-		start|bad|good|reset|visualize|replay|log)
-			command="$i"
-			break
-			;;
-		esac
-		c=$((++c))
-	done
-
-	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
-		__gitcomp "start bad good reset visualize replay log"
+	local subcommands="start bad good reset visualize replay log"
+	local subcommand="$(__git_find_subcommand "$subcommands")"
+	if [ -z "$subcommand" ]; then
 		return
 	fi
 
-	case "$command" in
+	case "$subcommand" in
 	bad|good|reset)
 		__gitcomp "$(__git_refs)"
 		;;
@@ -1025,21 +1030,13 @@ _git_config ()
 
 _git_remote ()
 {
-	local i c=1 command
-	while [ $c -lt $COMP_CWORD ]; do
-		i="${COMP_WORDS[c]}"
-		case "$i" in
-		add|rm|show|prune|update) command="$i"; break ;;
-		esac
-		c=$((++c))
-	done
-
-	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
-		__gitcomp "add rm show prune update"
+	local subcommands="add rm show prune update"
+	local subcommand="$(__git_find_subcommand "$subcommands")"
+	if [ -z "$subcommand" ]; then
 		return
 	fi
 
-	case "$command" in
+	case "$subcommand" in
 	rm|show|prune)
 		__gitcomp "$(__git_remotes)"
 		;;
@@ -1113,28 +1110,23 @@ _git_show ()
 
 _git_stash ()
 {
-	__gitcomp 'list show apply clear'
+	local subcommands='list show apply clear'
+	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
+		__gitcomp "$subcommands"
+	fi
 }
 
 _git_submodule ()
 {
-	local i c=1 command
-	while [ $c -lt $COMP_CWORD ]; do
-		i="${COMP_WORDS[c]}"
-		case "$i" in
-		add|status|init|update) command="$i"; break ;;
-		esac
-		c=$((++c))
-	done

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

* Re: [PATCH] bash: __git_find_subcommand function
  2008-03-06 14:58 [PATCH] bash: __git_find_subcommand function SZEDER Gábor
@ 2008-03-08  2:23 ` Shawn O. Pearce
  2008-03-10 14:51   ` SZEDER Gábor
  0 siblings, 1 reply; 3+ messages in thread
From: Shawn O. Pearce @ 2008-03-08  2:23 UTC (permalink / raw)
  To: SZEDER Gábor; +Cc: git

SZEDER Gbor <szeder@ira.uka.de> wrote:
> This enables us to remove code duplications from completion
> functions for commands having subcommands.
> 
> Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
> ---
> This function does not return the index of the subcommand found on the
> command line, which was in the $c variable previously.  However, $c was
> only used in if statements, like:
> 	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
> 		__gitcomp "cmd1 cmd2 cmd3"
> 	fi
> To my understanding the only(?) purpose of those if statements was to
> prevent subcommands appearing again on the list of possible completions,
> when there was one already on the command line.  But [ -z $command ] is
> sufficient to detect those cases, so we can actually omit
> [ $c -eq $COMP_CWORD ].  Is it right, or am I missing something?

Yup, you understood it correctly.  You found a cluster of "copy and
paste programming" and I am happy to see it cleaned up.  Thanks.

-- 
Shawn.

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

* Re: [PATCH] bash: __git_find_subcommand function
  2008-03-08  2:23 ` Shawn O. Pearce
@ 2008-03-10 14:51   ` SZEDER Gábor
  0 siblings, 0 replies; 3+ messages in thread
From: SZEDER Gábor @ 2008-03-10 14:51 UTC (permalink / raw)
  To: Shawn O. Pearce; +Cc: git

On Fri, Mar 07, 2008 at 09:23:42PM -0500, Shawn O. Pearce wrote:
> Yup, you understood it correctly.  You found a cluster of "copy and
> paste programming" and I am happy to see it cleaned up.  Thanks.
Thanks.  I will send out the updated patches soon.

Gábor

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

end of thread, other threads:[~2008-03-10 14:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-03-06 14:58 [PATCH] bash: __git_find_subcommand function SZEDER Gábor
2008-03-08  2:23 ` Shawn O. Pearce
2008-03-10 14:51   ` 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).