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>
SZEDER Gábor committed
Mar 10, 2008 at 16:02 UTC
3ff1320d4be164e35a685d8d00b6f44084be76e0
1 file changed
+32
-39
contrib/completion/git-completion.bash
+32
-39
@@ -428,6 +428,22 @@ __git_aliased_command ()
428
done
429
}
430
431
+__git_find_subcommand ()
432
+{
433
+ local word subcommand c=1
434
+
435
+ while [ $c -lt $COMP_CWORD ]; do
436
+ word="${COMP_WORDS[c]}"
437
+ for subcommand in $1; do
438
+ if [ "$subcommand" = "$word" ]; then
439
+ echo "$subcommand"
440
+ return
441
+ fi
442
+ done
443
+ c=$((++c))
444
+ done
445
+}
446
+
447
__git_whitespacelist="nowarn warn error error-all strip"
448
449
_git_am ()
@@ -485,24 +501,14 @@ _git_add ()
501
502
_git_bisect ()
503
{
488
- local i c=1 command
489
- while [ $c -lt $COMP_CWORD ]; do
490
- i="${COMP_WORDS[c]}"
491
- case "$i" in
492
- start|bad|good|reset|visualize|replay|log)
493
- command="$i"
494
- break
495
- ;;
496
- esac
497
- c=$((++c))
498
- done
499
-
500
- if [ -z "$command" ]; then
501
- __gitcomp "start bad good reset visualize replay log"
504
+ local subcommands="start bad good reset visualize replay log"
505
+ local subcommand="$(__git_find_subcommand "$subcommands")"
506
+ if [ -z "$subcommand" ]; then
507
+ __gitcomp "$subcommands"
508
return
509
fi
510
505
- case "$command" in
511
+ case "$subcommand" in
512
bad|good|reset)
513
__gitcomp "$(__git_refs)"
514
;;
@@ -1033,21 +1039,13 @@ _git_config ()
1039
1040
_git_remote ()
1041
{
1036
- local i c=1 command
1037
- while [ $c -lt $COMP_CWORD ]; do
1038
- i="${COMP_WORDS[c]}"
1039
- case "$i" in
1040
- add|rm|show|prune|update) command="$i"; break ;;
1041
- esac
1042
- c=$((++c))
1043
- done
1044
-
1045
- if [ -z "$command" ]; then
1046
- __gitcomp "add rm show prune update"
1042
+ local subcommands="add rm show prune update"
1043
+ local subcommand="$(__git_find_subcommand "$subcommands")"
1044
+ if [ -z "$subcommand" ]; then
1045
return
1046
fi
1047
1050
- case "$command" in
1048
+ case "$subcommand" in
1049
rm|show|prune)
1050
__gitcomp "$(__git_remotes)"
1051
;;
@@ -1121,28 +1119,23 @@ _git_show ()
1119
1120
_git_stash ()
1121
{
1124
- __gitcomp 'list show apply clear'
1122
+ local subcommands='list show apply clear'
1123
+ if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1124
+ __gitcomp "$subcommands"
1125
+ fi
1126
}
1127
1128
_git_submodule ()
1129
{
1129
- local i c=1 command
1130
- while [ $c -lt $COMP_CWORD ]; do
1131
- i="${COMP_WORDS[c]}"
1132
- case "$i" in
1133
- add|status|init|update) command="$i"; break ;;
1134
- esac
1135
- c=$((++c))
1136
- done
1137
-
1138
- if [ -z "$command" ]; then
1130
+ local subcommands="add status init update"
1131
+ if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1132
local cur="${COMP_WORDS[COMP_CWORD]}"
1133
case "$cur" in
1134
--*)
1135
__gitcomp "--quiet --cached"
1136
;;
1137
*)
1145
- __gitcomp "add status init update"
1138
+ __gitcomp "$subcommands"
1139
;;
1140
esac
1141
return