completion: add new __git_complete helper
[gitweb.git] / contrib / completion / git-completion.bash
index 0acbdda3b8af423fc4911af4d337658a4841c899..d60bb8ac8f62d44cafea3282867be158c6b68cbb 100755 (executable)
@@ -94,9 +94,10 @@ __gitdir ()
 __git_ps1_show_upstream ()
 {
        local key value
-       local svn_remote=() svn_url_pattern count n
+       local svn_remote svn_url_pattern count n
        local upstream=git legacy="" verbose=""
 
+       svn_remote=()
        # get some config options from git-config
        local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
        while read -r key value; do
@@ -137,7 +138,7 @@ __git_ps1_show_upstream ()
                        svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
                        svn_upstream=${svn_upstream%@*}
                        local n_stop="${#svn_remote[@]}"
-                       for ((n=1; n <= n_stop; ++n)); do
+                       for ((n=1; n <= n_stop; n++)); do
                                svn_upstream=${svn_upstream#${svn_remote[$n]}}
                        done
 
@@ -166,10 +167,8 @@ __git_ps1_show_upstream ()
                        for commit in $commits
                        do
                                case "$commit" in
-                               "<"*) let ++behind
-                                       ;;
-                               *)    let ++ahead
-                                       ;;
+                               "<"*) ((behind++)) ;;
+                               *)    ((ahead++))  ;;
                                esac
                        done
                        count="$behind  $ahead"
@@ -305,16 +304,16 @@ __git_ps1 ()
        fi
 }
 
-# __gitcomp_1 requires 2 arguments
 __gitcomp_1 ()
 {
-       local c IFS=' '$'\t'$'\n'
+       local c IFS=$' \t\n'
        for c in $1; do
-               case "$c$2" in
-               --*=*) printf %s$'\n' "$c$2" ;;
-               *.)    printf %s$'\n' "$c$2" ;;
-               *)     printf %s$'\n' "$c$2 " ;;
+               c="$c$2"
+               case $c in
+               --*=*|*.) ;;
+               *) c="$c " ;;
                esac
+               printf '%s\n' "$c"
        done
 }
 
@@ -726,6 +725,9 @@ __git_complete_remote_or_refspec ()
 {
        local cur_="$cur" cmd="${words[1]}"
        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
+       if [ "$cmd" = "remote" ]; then
+               ((c++))
+       fi
        while [ $c -lt $cword ]; do
                i="${words[c]}"
                case "$i" in
@@ -743,7 +745,7 @@ __git_complete_remote_or_refspec ()
                -*) ;;
                *) remote="$i"; break ;;
                esac
-               c=$((++c))
+               ((c++))
        done
        if [ -z "$remote" ]; then
                __gitcomp_nl "$(__git_remotes)"
@@ -776,7 +778,7 @@ __git_complete_remote_or_refspec ()
                        __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
                fi
                ;;
-       pull)
+       pull|remote)
                if [ $lhs = 1 ]; then
                        __gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
                else
@@ -983,7 +985,7 @@ __git_find_on_cmdline ()
                                return
                        fi
                done
-               c=$((++c))
+               ((c++))
        done
 }
 
@@ -994,7 +996,7 @@ __git_has_doubledash ()
                if [ "--" = "${words[c]}" ]; then
                        return 0
                fi
-               c=$((++c))
+               ((c++))
        done
        return 1
 }
@@ -1117,7 +1119,7 @@ _git_branch ()
                -d|-m)  only_local_ref="y" ;;
                -r)     has_r="y" ;;
                esac
-               c=$((++c))
+               ((c++))
        done
 
        case "$cur" in
@@ -1656,7 +1658,7 @@ _git_notes ()
                __gitcomp '--ref'
                ;;
        ,*)
-               case "${words[cword-1]}" in
+               case "$prev" in
                --ref)
                        __gitcomp_nl "$(__git_refs)"
                        ;;
@@ -1682,7 +1684,7 @@ _git_notes ()
        prune,*)
                ;;
        *)
-               case "${words[cword-1]}" in
+               case "$prev" in
                -m|-F)
                        ;;
                *)
@@ -2091,6 +2093,7 @@ _git_config ()
                core.whitespace
                core.worktree
                diff.autorefreshindex
+               diff.statGraphWidth
                diff.external
                diff.ignoreSubmodules
                diff.mnemonicprefix
@@ -2277,7 +2280,7 @@ _git_config ()
 
 _git_remote ()
 {
-       local subcommands="add rename rm show prune update set-head"
+       local subcommands="add rename rm set-head set-branches set-url show prune update"
        local subcommand="$(__git_find_on_cmdline "$subcommands")"
        if [ -z "$subcommand" ]; then
                __gitcomp "$subcommands"
@@ -2285,9 +2288,12 @@ _git_remote ()
        fi
 
        case "$subcommand" in
-       rename|rm|show|prune)
+       rename|rm|set-url|show|prune)
                __gitcomp_nl "$(__git_remotes)"
                ;;
+       set-head|set-branches)
+               __git_complete_remote_or_refspec
+               ;;
        update)
                local i c='' IFS=$'\n'
                for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
@@ -2500,7 +2506,7 @@ _git_svn ()
                        __gitcomp "
                                --merge --strategy= --verbose --dry-run
                                --fetch-all --no-rebase --commit-url
-                               --revision $cmt_opts $fc_opts
+                               --revision --interactive $cmt_opts $fc_opts
                                "
                        ;;
                set-tree,--*)
@@ -2568,7 +2574,7 @@ _git_tag ()
                        f=1
                        ;;
                esac
-               c=$((++c))
+               ((c++))
        done
 
        case "$prev" in
@@ -2597,31 +2603,17 @@ _git ()
 {
        local i c=1 command __git_dir
 
-       if [[ -n ${ZSH_VERSION-} ]]; then
-               emulate -L bash
-               setopt KSH_TYPESET
-
-               # workaround zsh's bug that leaves 'words' as a special
-               # variable in versions < 4.3.12
-               typeset -h words
-
-               # workaround zsh's bug that quotes spaces in the COMPREPLY
-               # array if IFS doesn't contain spaces.
-               typeset -h IFS
-       fi
-
-       local cur words cword prev
-       _get_comp_words_by_ref -n =: cur words cword prev
        while [ $c -lt $cword ]; do
                i="${words[c]}"
                case "$i" in
                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
                --bare)      __git_dir="." ;;
-               --version|-p|--paginate) ;;
                --help) command="help"; break ;;
+               -c) c=$((++c)) ;;
+               -*) ;;
                *) command="$i"; break ;;
                esac
-               c=$((++c))
+               ((c++))
        done
 
        if [ -z "$command" ]; then
@@ -2633,9 +2625,12 @@ _git ()
                        --bare
                        --version
                        --exec-path
+                       --exec-path=
                        --html-path
+                       --info-path
                        --work-tree=
                        --namespace=
+                       --no-replace-objects
                        --help
                        "
                        ;;
@@ -2657,22 +2652,6 @@ _git ()
 
 _gitk ()
 {
-       if [[ -n ${ZSH_VERSION-} ]]; then
-               emulate -L bash
-               setopt KSH_TYPESET
-
-               # workaround zsh's bug that leaves 'words' as a special
-               # variable in versions < 4.3.12
-               typeset -h words
-
-               # workaround zsh's bug that quotes spaces in the COMPREPLY
-               # array if IFS doesn't contain spaces.
-               typeset -h IFS
-       fi
-
-       local cur words cword prev
-       _get_comp_words_by_ref -n =: cur words cword prev
-
        __git_has_doubledash && return
 
        local g="$(__gitdir)"
@@ -2693,16 +2672,43 @@ _gitk ()
        __git_complete_revlist
 }
 
-complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
-       || complete -o default -o nospace -F _git git
-complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
-       || complete -o default -o nospace -F _gitk gitk
+__git_func_wrap ()
+{
+       if [[ -n ${ZSH_VERSION-} ]]; then
+               emulate -L bash
+               setopt KSH_TYPESET
+
+               # workaround zsh's bug that leaves 'words' as a special
+               # variable in versions < 4.3.12
+               typeset -h words
+
+               # workaround zsh's bug that quotes spaces in the COMPREPLY
+               # array if IFS doesn't contain spaces.
+               typeset -h IFS
+       fi
+       local cur words cword prev
+       _get_comp_words_by_ref -n =: cur words cword prev
+       $1
+}
+
+# Setup completion for certain functions defined above by setting common
+# variables and workarounds.
+# This is NOT a public function; use at your own risk.
+__git_complete ()
+{
+       local wrapper="__git_wrap${2}"
+       eval "$wrapper () { __git_func_wrap $2 ; }"
+       complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
+               || complete -o default -o nospace -F $wrapper $1
+}
+
+__git_complete git _git
+__git_complete gitk _gitk
 
 # The following are necessary only for Cygwin, and only are needed
 # when the user has tab-completed the executable name and consequently
 # included the '.exe' suffix.
 #
 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
-complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
-       || complete -o default -o nospace -F _git git.exe
+__git_complete git.exe _git
 fi