bash: refactor searching for subcommands on the command line

This patch adds the __git_find_subcommand function, which takes one
argument: a string containing all subcommands separated by spaces.  The
function searches through the command line whether a subcommand is
already present.  The first found subcommand 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>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
SZEDER Gábor 2008-03-10 16:02:23 +01:00 committed by Shawn O. Pearce
parent 1d17b22ebf
commit 3ff1320d4b

View File

@ -428,6 +428,22 @@ __git_aliased_command ()
done 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_whitespacelist="nowarn warn error error-all strip"
_git_am () _git_am ()
@ -485,24 +501,14 @@ _git_add ()
_git_bisect () _git_bisect ()
{ {
local i c=1 command local subcommands="start bad good reset visualize replay log"
while [ $c -lt $COMP_CWORD ]; do local subcommand="$(__git_find_subcommand "$subcommands")"
i="${COMP_WORDS[c]}" if [ -z "$subcommand" ]; then
case "$i" in __gitcomp "$subcommands"
start|bad|good|reset|visualize|replay|log)
command="$i"
break
;;
esac
c=$((++c))
done
if [ -z "$command" ]; then
__gitcomp "start bad good reset visualize replay log"
return return
fi fi
case "$command" in case "$subcommand" in
bad|good|reset) bad|good|reset)
__gitcomp "$(__git_refs)" __gitcomp "$(__git_refs)"
;; ;;
@ -1033,21 +1039,13 @@ _git_config ()
_git_remote () _git_remote ()
{ {
local i c=1 command local subcommands="add rm show prune update"
while [ $c -lt $COMP_CWORD ]; do local subcommand="$(__git_find_subcommand "$subcommands")"
i="${COMP_WORDS[c]}" if [ -z "$subcommand" ]; then
case "$i" in
add|rm|show|prune|update) command="$i"; break ;;
esac
c=$((++c))
done
if [ -z "$command" ]; then
__gitcomp "add rm show prune update"
return return
fi fi
case "$command" in case "$subcommand" in
rm|show|prune) rm|show|prune)
__gitcomp "$(__git_remotes)" __gitcomp "$(__git_remotes)"
;; ;;
@ -1121,28 +1119,23 @@ _git_show ()
_git_stash () _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 () _git_submodule ()
{ {
local i c=1 command local subcommands="add status init update"
while [ $c -lt $COMP_CWORD ]; do if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
i="${COMP_WORDS[c]}"
case "$i" in
add|status|init|update) command="$i"; break ;;
esac
c=$((++c))
done
if [ -z "$command" ]; then
local cur="${COMP_WORDS[COMP_CWORD]}" local cur="${COMP_WORDS[COMP_CWORD]}"
case "$cur" in case "$cur" in
--*) --*)
__gitcomp "--quiet --cached" __gitcomp "--quiet --cached"
;; ;;
*) *)
__gitcomp "add status init update" __gitcomp "$subcommands"
;; ;;
esac esac
return return