]> git.ipfire.org Git - thirdparty/git.git/blobdiff - contrib/completion/git-completion.bash
completion: add __git_get_option_value helper
[thirdparty/git.git] / contrib / completion / git-completion.bash
index e3918c87e3adf32a9d7a4f0320c92c497376b9b5..14a8d0fe6e163058648df30f5ebe0a7dd6a5d4a9 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
@@ -1098,6 +1142,8 @@ _git_clone ()
        esac
 }
 
+__git_untracked_file_modes="all no normal"
+
 _git_commit ()
 {
        case "$prev" in
@@ -1119,7 +1165,7 @@ _git_commit ()
                return
                ;;
        --untracked-files=*)
-               __gitcomp "all no normal" "" "${cur##--untracked-files=}"
+               __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
                return
                ;;
        --*)