completion: simplify inner 'case' pattern in __gitcomp()
[gitweb.git] / contrib / completion / git-completion.bash
index 8c6b610a2447cd1ec7c2dff0f9dbd09f6a024f57..fc437bf3eb0e3f937764513cda9aa428702aae25 100644 (file)
@@ -37,7 +37,8 @@
 #   GIT_COMPLETION_CHECKOUT_NO_GUESS
 #
 #     When set to "1", do not include "DWIM" suggestions in git-checkout
-#     completion (e.g., completing "foo" when "origin/foo" exists).
+#     and git-switch completion (e.g., completing "foo" when "origin/foo"
+#     exists).
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -339,7 +340,7 @@ __gitcomp ()
                        c="$c${4-}"
                        if [[ $c == "$cur_"* ]]; then
                                case $c in
-                               --*=*|*.) ;;
+                               --*=|*.) ;;
                                *) c="$c " ;;
                                esac
                                COMPREPLY[i++]="${2-}$c"
@@ -359,7 +360,7 @@ __gitcomp ()
                        c="$c${4-}"
                        if [[ $c == "$cur_"* ]]; then
                                case $c in
-                               --*=*|*.) ;;
+                               --*=|*.) ;;
                                *) c="$c " ;;
                                esac
                                COMPREPLY[i++]="${2-}$c"
@@ -523,7 +524,7 @@ __git_index_files ()
                        # Even when a directory name itself does not contain
                        # any special characters, it will still be quoted if
                        # any of its (stripped) trailing path components do.
-                       # Because of this we may have seen the same direcory
+                       # Because of this we may have seen the same directory
                        # both quoted and unquoted.
                        if (p in paths)
                                # We have seen the same directory unquoted,
@@ -2160,6 +2161,44 @@ _git_status ()
        __git_complete_index_file "$complete_opt"
 }
 
+_git_switch ()
+{
+       case "$cur" in
+       --conflict=*)
+               __gitcomp "diff3 merge" "" "${cur##--conflict=}"
+               ;;
+       --*)
+               __gitcomp_builtin switch
+               ;;
+       *)
+               # check if --track, --no-track, or --no-guess was specified
+               # if so, disable DWIM mode
+               local track_opt="--track" only_local_ref=n
+               if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
+                  [ -n "$(__git_find_on_cmdline "--track --no-track --no-guess")" ]; then
+                       track_opt=''
+               fi
+               # explicit --guess enables DWIM mode regardless of
+               # $GIT_COMPLETION_CHECKOUT_NO_GUESS
+               if [ -n "$(__git_find_on_cmdline "--guess")" ]; then
+                       track_opt='--track'
+               fi
+               if [ -z "$(__git_find_on_cmdline "-d --detach")" ]; then
+                       only_local_ref=y
+               else
+                       # --guess --detach is invalid combination, no
+                       # dwim will be done when --detach is specified
+                       track_opt=
+               fi
+               if [ $only_local_ref = y -a -z "$track_opt" ]; then
+                       __gitcomp_direct "$(__git_heads "" "$cur" " ")"
+               else
+                       __git_complete_refs $track_opt
+               fi
+               ;;
+       esac
+}
+
 __git_config_get_set_variables ()
 {
        local prevword word config_file= c=$cword
@@ -2186,7 +2225,7 @@ __git_config_vars=
 __git_compute_config_vars ()
 {
        test -n "$__git_config_vars" ||
-       __git_config_vars="$(git help --config-for-completion | sort | uniq)"
+       __git_config_vars="$(git help --config-for-completion | sort -u)"
 }
 
 _git_config ()
@@ -2238,11 +2277,6 @@ _git_config ()
                __gitcomp "$__git_merge_strategies"
                return
                ;;
-       color.branch|color.diff|color.interactive|\
-       color.showbranch|color.status|color.ui)
-               __gitcomp "always never auto"
-               return
-               ;;
        color.pager)
                __gitcomp "false true"
                return
@@ -2254,6 +2288,10 @@ _git_config ()
                        "
                return
                ;;
+       color.*)
+               __gitcomp "false true always never auto"
+               return
+               ;;
        diff.submodule)
                __gitcomp "$__git_diff_submodule_formats"
                return
@@ -2360,7 +2398,15 @@ _git_config ()
                ;;
        *)
                __git_compute_config_vars
-               __gitcomp "$(echo "$__git_config_vars" | sed 's/\.[^ ]*/./g')"
+               __gitcomp "$(echo "$__git_config_vars" |
+                               awk -F . '{
+                                       sections[$1] = 1
+                               }
+                               END {
+                                       for (s in sections)
+                                               print s "."
+                               }
+                               ')"
        esac
 }
 
@@ -2458,6 +2504,21 @@ _git_reset ()
        __git_complete_refs
 }
 
+_git_restore ()
+{
+       case "$cur" in
+       --conflict=*)
+               __gitcomp "diff3 merge" "" "${cur##--conflict=}"
+               ;;
+       --source=*)
+               __git_complete_refs --cur="${cur##--source=}"
+               ;;
+       --*)
+               __gitcomp_builtin restore
+               ;;
+       esac
+}
+
 __git_revert_inprogress_options="--continue --quit --abort"
 
 _git_revert ()