OSDN Git Service

completion: add __git_get_option_value helper
authorThomas Braun <thomas.braun@virtuell-zuhause.de>
Fri, 10 Jun 2016 10:12:05 +0000 (12:12 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 10 Jun 2016 18:54:57 +0000 (11:54 -0700)
This function allows to search the commmand line and config
files for an option, long and short, with mandatory value.

The function would return e.g. for the command line
"git status -uno --untracked-files=all" the result
"all" regardless of the config option.

Signed-off-by: Thomas Braun <thomas.braun@virtuell-zuhause.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash

index a44e00b..14a8d0f 100644 (file)
@@ -803,6 +803,50 @@ __git_find_on_cmdline ()
        done
 }
 
+# Echo the value of an option set on the command line or config
+#
+# $1: short option name
+# $2: long option name including =
+# $3: list of possible values
+# $4: config string (optional)
+#
+# example:
+# result="$(__git_get_option_value "-d" "--do-something=" \
+#     "yes no" "core.doSomething")"
+#
+# result is then either empty (no option set) or "yes" or "no"
+#
+# __git_get_option_value requires 3 arguments
+__git_get_option_value ()
+{
+       local c short_opt long_opt val
+       local result= values config_key word
+
+       short_opt="$1"
+       long_opt="$2"
+       values="$3"
+       config_key="$4"
+
+       ((c = $cword - 1))
+       while [ $c -ge 0 ]; do
+               word="${words[c]}"
+               for val in $values; do
+                       if [ "$short_opt$val" = "$word" ] ||
+                          [ "$long_opt$val"  = "$word" ]; then
+                               result="$val"
+                               break 2
+                       fi
+               done
+               ((c--))
+       done
+
+       if [ -n "$config_key" ] && [ -z "$result" ]; then
+               result="$(git --git-dir="$(__gitdir)" config "$config_key")"
+       fi
+
+       echo "$result"
+}
+
 __git_has_doubledash ()
 {
        local c=1