contrib / completion / git-completion.bashon commit bash completion: add basic support for git-reflog (057f327)
   1#!bash
   2#
   3# bash completion support for core Git.
   4#
   5# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
   6# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
   7# Distributed under the GNU General Public License, version 2.0.
   8#
   9# The contained completion routines provide support for completing:
  10#
  11#    *) local and remote branch names
  12#    *) local and remote tag names
  13#    *) .git/remotes file names
  14#    *) git 'subcommands'
  15#    *) tree paths within 'ref:path/to/file' expressions
  16#    *) common --long-options
  17#
  18# To use these routines:
  19#
  20#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
  21#    2) Added the following line to your .bashrc:
  22#        source ~/.git-completion.sh
  23#
  24#       Or, add the following lines to your .zshrc:
  25#        autoload bashcompinit
  26#        bashcompinit
  27#        source ~/.git-completion.sh
  28#
  29#    3) Consider changing your PS1 to also show the current branch:
  30#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  31#
  32#       The argument to __git_ps1 will be displayed only if you
  33#       are currently in a git repository.  The %s token will be
  34#       the name of the current branch.
  35#
  36#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
  37#       value, unstaged (*) and staged (+) changes will be shown next
  38#       to the branch name.  You can configure this per-repository
  39#       with the bash.showDirtyState variable, which defaults to true
  40#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
  41#
  42#       You can also see if currently something is stashed, by setting
  43#       GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
  44#       then a '$' will be shown next to the branch name.
  45#
  46#       If you would like to see if there're untracked files, then you can
  47#       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
  48#       untracked files, then a '%' will be shown next to the branch name.
  49#
  50#       If you would like to see the difference between HEAD and its
  51#       upstream, set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates
  52#       you are behind, ">" indicates you are ahead, and "<>"
  53#       indicates you have diverged.  You can further control
  54#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
  55#       list of values:
  56#           verbose       show number of commits ahead/behind (+/-) upstream
  57#           legacy        don't use the '--count' option available in recent
  58#                         versions of git-rev-list
  59#           git           always compare HEAD to @{upstream}
  60#           svn           always compare HEAD to your SVN upstream
  61#       By default, __git_ps1 will compare HEAD to your SVN upstream
  62#       if it can find one, or @{upstream} otherwise.  Once you have
  63#       set GIT_PS1_SHOWUPSTREAM, you can override it on a
  64#       per-repository basis by setting the bash.showUpstream config
  65#       variable.
  66#
  67#
  68# To submit patches:
  69#
  70#    *) Read Documentation/SubmittingPatches
  71#    *) Send all patches to the current maintainer:
  72#
  73#       "Shawn O. Pearce" <spearce@spearce.org>
  74#
  75#    *) Always CC the Git mailing list:
  76#
  77#       git@vger.kernel.org
  78#
  79
  80case "$COMP_WORDBREAKS" in
  81*:*) : great ;;
  82*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  83esac
  84
  85# __gitdir accepts 0 or 1 arguments (i.e., location)
  86# returns location of .git repo
  87__gitdir ()
  88{
  89        if [ -z "${1-}" ]; then
  90                if [ -n "${__git_dir-}" ]; then
  91                        echo "$__git_dir"
  92                elif [ -d .git ]; then
  93                        echo .git
  94                else
  95                        git rev-parse --git-dir 2>/dev/null
  96                fi
  97        elif [ -d "$1/.git" ]; then
  98                echo "$1/.git"
  99        else
 100                echo "$1"
 101        fi
 102}
 103
 104# stores the divergence from upstream in $p
 105# used by GIT_PS1_SHOWUPSTREAM
 106__git_ps1_show_upstream ()
 107{
 108        local key value
 109        local svn_remote=() svn_url_pattern count n
 110        local upstream=git legacy="" verbose=""
 111
 112        # get some config options from git-config
 113        while read key value; do
 114                case "$key" in
 115                bash.showupstream)
 116                        GIT_PS1_SHOWUPSTREAM="$value"
 117                        if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
 118                                p=""
 119                                return
 120                        fi
 121                        ;;
 122                svn-remote.*.url)
 123                        svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
 124                        svn_url_pattern+="\\|$value"
 125                        upstream=svn+git # default upstream is SVN if available, else git
 126                        ;;
 127                esac
 128        done < <(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')
 129
 130        # parse configuration values
 131        for option in ${GIT_PS1_SHOWUPSTREAM}; do
 132                case "$option" in
 133                git|svn) upstream="$option" ;;
 134                verbose) verbose=1 ;;
 135                legacy)  legacy=1  ;;
 136                esac
 137        done
 138
 139        # Find our upstream
 140        case "$upstream" in
 141        git)    upstream="@{upstream}" ;;
 142        svn*)
 143                # get the upstream from the "git-svn-id: ..." in a commit message
 144                # (git-svn uses essentially the same procedure internally)
 145                local svn_upstream=($(git log --first-parent -1 \
 146                                        --grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
 147                if [[ 0 -ne ${#svn_upstream[@]} ]]; then
 148                        svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
 149                        svn_upstream=${svn_upstream%@*}
 150                        local n_stop="${#svn_remote[@]}"
 151                        for ((n=1; n <= n_stop; ++n)); do
 152                                svn_upstream=${svn_upstream#${svn_remote[$n]}}
 153                        done
 154
 155                        if [[ -z "$svn_upstream" ]]; then
 156                                # default branch name for checkouts with no layout:
 157                                upstream=${GIT_SVN_ID:-git-svn}
 158                        else
 159                                upstream=${svn_upstream#/}
 160                        fi
 161                elif [[ "svn+git" = "$upstream" ]]; then
 162                        upstream="@{upstream}"
 163                fi
 164                ;;
 165        esac
 166
 167        # Find how many commits we are ahead/behind our upstream
 168        if [[ -z "$legacy" ]]; then
 169                count="$(git rev-list --count --left-right \
 170                                "$upstream"...HEAD 2>/dev/null)"
 171        else
 172                # produce equivalent output to --count for older versions of git
 173                local commits
 174                if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
 175                then
 176                        local commit behind=0 ahead=0
 177                        for commit in $commits
 178                        do
 179                                case "$commit" in
 180                                "<"*) let ++behind
 181                                        ;;
 182                                *)    let ++ahead
 183                                        ;;
 184                                esac
 185                        done
 186                        count="$behind  $ahead"
 187                else
 188                        count=""
 189                fi
 190        fi
 191
 192        # calculate the result
 193        if [[ -z "$verbose" ]]; then
 194                case "$count" in
 195                "") # no upstream
 196                        p="" ;;
 197                "0      0") # equal to upstream
 198                        p="=" ;;
 199                "0      "*) # ahead of upstream
 200                        p=">" ;;
 201                *"      0") # behind upstream
 202                        p="<" ;;
 203                *)          # diverged from upstream
 204                        p="<>" ;;
 205                esac
 206        else
 207                case "$count" in
 208                "") # no upstream
 209                        p="" ;;
 210                "0      0") # equal to upstream
 211                        p=" u=" ;;
 212                "0      "*) # ahead of upstream
 213                        p=" u+${count#0 }" ;;
 214                *"      0") # behind upstream
 215                        p=" u-${count%  0}" ;;
 216                *)          # diverged from upstream
 217                        p=" u+${count#* }-${count%      *}" ;;
 218                esac
 219        fi
 220
 221}
 222
 223
 224# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
 225# returns text to add to bash PS1 prompt (includes branch name)
 226__git_ps1 ()
 227{
 228        local g="$(__gitdir)"
 229        if [ -n "$g" ]; then
 230                local r=""
 231                local b=""
 232                if [ -f "$g/rebase-merge/interactive" ]; then
 233                        r="|REBASE-i"
 234                        b="$(cat "$g/rebase-merge/head-name")"
 235                elif [ -d "$g/rebase-merge" ]; then
 236                        r="|REBASE-m"
 237                        b="$(cat "$g/rebase-merge/head-name")"
 238                else
 239                        if [ -d "$g/rebase-apply" ]; then
 240                                if [ -f "$g/rebase-apply/rebasing" ]; then
 241                                        r="|REBASE"
 242                                elif [ -f "$g/rebase-apply/applying" ]; then
 243                                        r="|AM"
 244                                else
 245                                        r="|AM/REBASE"
 246                                fi
 247                        elif [ -f "$g/MERGE_HEAD" ]; then
 248                                r="|MERGING"
 249                        elif [ -f "$g/BISECT_LOG" ]; then
 250                                r="|BISECTING"
 251                        fi
 252
 253                        b="$(git symbolic-ref HEAD 2>/dev/null)" || {
 254
 255                                b="$(
 256                                case "${GIT_PS1_DESCRIBE_STYLE-}" in
 257                                (contains)
 258                                        git describe --contains HEAD ;;
 259                                (branch)
 260                                        git describe --contains --all HEAD ;;
 261                                (describe)
 262                                        git describe HEAD ;;
 263                                (* | default)
 264                                        git describe --tags --exact-match HEAD ;;
 265                                esac 2>/dev/null)" ||
 266
 267                                b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
 268                                b="unknown"
 269                                b="($b)"
 270                        }
 271                fi
 272
 273                local w=""
 274                local i=""
 275                local s=""
 276                local u=""
 277                local c=""
 278                local p=""
 279
 280                if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 281                        if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
 282                                c="BARE:"
 283                        else
 284                                b="GIT_DIR!"
 285                        fi
 286                elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
 287                        if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
 288                                if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
 289                                        git diff --no-ext-diff --quiet --exit-code || w="*"
 290                                        if git rev-parse --quiet --verify HEAD >/dev/null; then
 291                                                git diff-index --cached --quiet HEAD -- || i="+"
 292                                        else
 293                                                i="#"
 294                                        fi
 295                                fi
 296                        fi
 297                        if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 298                                git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 299                        fi
 300
 301                        if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
 302                           if [ -n "$(git ls-files --others --exclude-standard)" ]; then
 303                              u="%"
 304                           fi
 305                        fi
 306
 307                        if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
 308                                __git_ps1_show_upstream
 309                        fi
 310                fi
 311
 312                local f="$w$i$s$u"
 313                printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
 314        fi
 315}
 316
 317# __gitcomp_1 requires 2 arguments
 318__gitcomp_1 ()
 319{
 320        local c IFS=' '$'\t'$'\n'
 321        for c in $1; do
 322                case "$c$2" in
 323                --*=*) printf %s$'\n' "$c$2" ;;
 324                *.)    printf %s$'\n' "$c$2" ;;
 325                *)     printf %s$'\n' "$c$2 " ;;
 326                esac
 327        done
 328}
 329
 330# __gitcomp accepts 1, 2, 3, or 4 arguments
 331# generates completion reply with compgen
 332__gitcomp ()
 333{
 334        local cur="${COMP_WORDS[COMP_CWORD]}"
 335        if [ $# -gt 2 ]; then
 336                cur="$3"
 337        fi
 338        case "$cur" in
 339        --*=)
 340                COMPREPLY=()
 341                ;;
 342        *)
 343                local IFS=$'\n'
 344                COMPREPLY=($(compgen -P "${2-}" \
 345                        -W "$(__gitcomp_1 "${1-}" "${4-}")" \
 346                        -- "$cur"))
 347                ;;
 348        esac
 349}
 350
 351# __git_heads accepts 0 or 1 arguments (to pass to __gitdir)
 352__git_heads ()
 353{
 354        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 355        if [ -d "$dir" ]; then
 356                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 357                        refs/heads
 358                return
 359        fi
 360        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 361                case "$is_hash,$i" in
 362                y,*) is_hash=n ;;
 363                n,*^{}) is_hash=y ;;
 364                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 365                n,*) is_hash=y; echo "$i" ;;
 366                esac
 367        done
 368}
 369
 370# __git_tags accepts 0 or 1 arguments (to pass to __gitdir)
 371__git_tags ()
 372{
 373        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 374        if [ -d "$dir" ]; then
 375                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 376                        refs/tags
 377                return
 378        fi
 379        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 380                case "$is_hash,$i" in
 381                y,*) is_hash=n ;;
 382                n,*^{}) is_hash=y ;;
 383                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 384                n,*) is_hash=y; echo "$i" ;;
 385                esac
 386        done
 387}
 388
 389# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
 390# presence of 2nd argument means use the guess heuristic employed
 391# by checkout for tracking branches
 392__git_refs ()
 393{
 394        local i is_hash=y dir="$(__gitdir "${1-}")" track="${2-}"
 395        local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 396        if [ -d "$dir" ]; then
 397                case "$cur" in
 398                refs|refs/*)
 399                        format="refname"
 400                        refs="${cur%/*}"
 401                        track=""
 402                        ;;
 403                *)
 404                        for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
 405                                if [ -e "$dir/$i" ]; then echo $i; fi
 406                        done
 407                        format="refname:short"
 408                        refs="refs/tags refs/heads refs/remotes"
 409                        ;;
 410                esac
 411                git --git-dir="$dir" for-each-ref --format="%($format)" \
 412                        $refs
 413                if [ -n "$track" ]; then
 414                        # employ the heuristic used by git checkout
 415                        # Try to find a remote branch that matches the completion word
 416                        # but only output if the branch name is unique
 417                        local ref entry
 418                        git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
 419                                "refs/remotes/" | \
 420                        while read entry; do
 421                                eval "$entry"
 422                                ref="${ref#*/}"
 423                                if [[ "$ref" == "$cur"* ]]; then
 424                                        echo "$ref"
 425                                fi
 426                        done | uniq -u
 427                fi
 428                return
 429        fi
 430        for i in $(git ls-remote "$dir" 2>/dev/null); do
 431                case "$is_hash,$i" in
 432                y,*) is_hash=n ;;
 433                n,*^{}) is_hash=y ;;
 434                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 435                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 436                n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
 437                n,*) is_hash=y; echo "$i" ;;
 438                esac
 439        done
 440}
 441
 442# __git_refs2 requires 1 argument (to pass to __git_refs)
 443__git_refs2 ()
 444{
 445        local i
 446        for i in $(__git_refs "$1"); do
 447                echo "$i:$i"
 448        done
 449}
 450
 451# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 452__git_refs_remotes ()
 453{
 454        local cmd i is_hash=y
 455        for i in $(git ls-remote "$1" 2>/dev/null); do
 456                case "$is_hash,$i" in
 457                n,refs/heads/*)
 458                        is_hash=y
 459                        echo "$i:refs/remotes/$1/${i#refs/heads/}"
 460                        ;;
 461                y,*) is_hash=n ;;
 462                n,*^{}) is_hash=y ;;
 463                n,refs/tags/*) is_hash=y;;
 464                n,*) is_hash=y; ;;
 465                esac
 466        done
 467}
 468
 469__git_remotes ()
 470{
 471        local i ngoff IFS=$'\n' d="$(__gitdir)"
 472        shopt -q nullglob || ngoff=1
 473        shopt -s nullglob
 474        for i in "$d/remotes"/*; do
 475                echo ${i#$d/remotes/}
 476        done
 477        [ "$ngoff" ] && shopt -u nullglob
 478        for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
 479                i="${i#remote.}"
 480                echo "${i/.url*/}"
 481        done
 482}
 483
 484__git_list_merge_strategies ()
 485{
 486        git merge -s help 2>&1 |
 487        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 488                s/\.$//
 489                s/.*://
 490                s/^[    ]*//
 491                s/[     ]*$//
 492                p
 493        }'
 494}
 495
 496__git_merge_strategies=
 497# 'git merge -s help' (and thus detection of the merge strategy
 498# list) fails, unfortunately, if run outside of any git working
 499# tree.  __git_merge_strategies is set to the empty string in
 500# that case, and the detection will be repeated the next time it
 501# is needed.
 502__git_compute_merge_strategies ()
 503{
 504        : ${__git_merge_strategies:=$(__git_list_merge_strategies)}
 505}
 506
 507__git_complete_file ()
 508{
 509        local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
 510        case "$cur" in
 511        ?*:*)
 512                ref="${cur%%:*}"
 513                cur="${cur#*:}"
 514                case "$cur" in
 515                ?*/*)
 516                        pfx="${cur%/*}"
 517                        cur="${cur##*/}"
 518                        ls="$ref:$pfx"
 519                        pfx="$pfx/"
 520                        ;;
 521                *)
 522                        ls="$ref"
 523                        ;;
 524            esac
 525
 526                case "$COMP_WORDBREAKS" in
 527                *:*) : great ;;
 528                *)   pfx="$ref:$pfx" ;;
 529                esac
 530
 531                local IFS=$'\n'
 532                COMPREPLY=($(compgen -P "$pfx" \
 533                        -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
 534                                | sed '/^100... blob /{
 535                                           s,^.*        ,,
 536                                           s,$, ,
 537                                       }
 538                                       /^120000 blob /{
 539                                           s,^.*        ,,
 540                                           s,$, ,
 541                                       }
 542                                       /^040000 tree /{
 543                                           s,^.*        ,,
 544                                           s,$,/,
 545                                       }
 546                                       s/^.*    //')" \
 547                        -- "$cur"))
 548                ;;
 549        *)
 550                __gitcomp "$(__git_refs)"
 551                ;;
 552        esac
 553}
 554
 555__git_complete_revlist ()
 556{
 557        local pfx cur="${COMP_WORDS[COMP_CWORD]}"
 558        case "$cur" in
 559        *...*)
 560                pfx="${cur%...*}..."
 561                cur="${cur#*...}"
 562                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 563                ;;
 564        *..*)
 565                pfx="${cur%..*}.."
 566                cur="${cur#*..}"
 567                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 568                ;;
 569        *)
 570                __gitcomp "$(__git_refs)"
 571                ;;
 572        esac
 573}
 574
 575__git_complete_remote_or_refspec ()
 576{
 577        local cmd="${COMP_WORDS[1]}"
 578        local cur="${COMP_WORDS[COMP_CWORD]}"
 579        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
 580        while [ $c -lt $COMP_CWORD ]; do
 581                i="${COMP_WORDS[c]}"
 582                case "$i" in
 583                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
 584                --all)
 585                        case "$cmd" in
 586                        push) no_complete_refspec=1 ;;
 587                        fetch)
 588                                COMPREPLY=()
 589                                return
 590                                ;;
 591                        *) ;;
 592                        esac
 593                        ;;
 594                -*) ;;
 595                *) remote="$i"; break ;;
 596                esac
 597                c=$((++c))
 598        done
 599        if [ -z "$remote" ]; then
 600                __gitcomp "$(__git_remotes)"
 601                return
 602        fi
 603        if [ $no_complete_refspec = 1 ]; then
 604                COMPREPLY=()
 605                return
 606        fi
 607        [ "$remote" = "." ] && remote=
 608        case "$cur" in
 609        *:*)
 610                case "$COMP_WORDBREAKS" in
 611                *:*) : great ;;
 612                *)   pfx="${cur%%:*}:" ;;
 613                esac
 614                cur="${cur#*:}"
 615                lhs=0
 616                ;;
 617        +*)
 618                pfx="+"
 619                cur="${cur#+}"
 620                ;;
 621        esac
 622        case "$cmd" in
 623        fetch)
 624                if [ $lhs = 1 ]; then
 625                        __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
 626                else
 627                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 628                fi
 629                ;;
 630        pull)
 631                if [ $lhs = 1 ]; then
 632                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 633                else
 634                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 635                fi
 636                ;;
 637        push)
 638                if [ $lhs = 1 ]; then
 639                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 640                else
 641                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 642                fi
 643                ;;
 644        esac
 645}
 646
 647__git_complete_strategy ()
 648{
 649        __git_compute_merge_strategies
 650        case "${COMP_WORDS[COMP_CWORD-1]}" in
 651        -s|--strategy)
 652                __gitcomp "$__git_merge_strategies"
 653                return 0
 654        esac
 655        local cur="${COMP_WORDS[COMP_CWORD]}"
 656        case "$cur" in
 657        --strategy=*)
 658                __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
 659                return 0
 660                ;;
 661        esac
 662        return 1
 663}
 664
 665__git_list_all_commands ()
 666{
 667        local i IFS=" "$'\n'
 668        for i in $(git help -a|egrep '^  [a-zA-Z0-9]')
 669        do
 670                case $i in
 671                *--*)             : helper pattern;;
 672                *) echo $i;;
 673                esac
 674        done
 675}
 676
 677__git_all_commands=
 678__git_compute_all_commands ()
 679{
 680        : ${__git_all_commands:=$(__git_list_all_commands)}
 681}
 682
 683__git_list_porcelain_commands ()
 684{
 685        local i IFS=" "$'\n'
 686        __git_compute_all_commands
 687        for i in "help" $__git_all_commands
 688        do
 689                case $i in
 690                *--*)             : helper pattern;;
 691                applymbox)        : ask gittus;;
 692                applypatch)       : ask gittus;;
 693                archimport)       : import;;
 694                cat-file)         : plumbing;;
 695                check-attr)       : plumbing;;
 696                check-ref-format) : plumbing;;
 697                checkout-index)   : plumbing;;
 698                commit-tree)      : plumbing;;
 699                count-objects)    : infrequent;;
 700                cvsexportcommit)  : export;;
 701                cvsimport)        : import;;
 702                cvsserver)        : daemon;;
 703                daemon)           : daemon;;
 704                diff-files)       : plumbing;;
 705                diff-index)       : plumbing;;
 706                diff-tree)        : plumbing;;
 707                fast-import)      : import;;
 708                fast-export)      : export;;
 709                fsck-objects)     : plumbing;;
 710                fetch-pack)       : plumbing;;
 711                fmt-merge-msg)    : plumbing;;
 712                for-each-ref)     : plumbing;;
 713                hash-object)      : plumbing;;
 714                http-*)           : transport;;
 715                index-pack)       : plumbing;;
 716                init-db)          : deprecated;;
 717                local-fetch)      : plumbing;;
 718                lost-found)       : infrequent;;
 719                ls-files)         : plumbing;;
 720                ls-remote)        : plumbing;;
 721                ls-tree)          : plumbing;;
 722                mailinfo)         : plumbing;;
 723                mailsplit)        : plumbing;;
 724                merge-*)          : plumbing;;
 725                mktree)           : plumbing;;
 726                mktag)            : plumbing;;
 727                pack-objects)     : plumbing;;
 728                pack-redundant)   : plumbing;;
 729                pack-refs)        : plumbing;;
 730                parse-remote)     : plumbing;;
 731                patch-id)         : plumbing;;
 732                peek-remote)      : plumbing;;
 733                prune)            : plumbing;;
 734                prune-packed)     : plumbing;;
 735                quiltimport)      : import;;
 736                read-tree)        : plumbing;;
 737                receive-pack)     : plumbing;;
 738                remote-*)         : transport;;
 739                repo-config)      : deprecated;;
 740                rerere)           : plumbing;;
 741                rev-list)         : plumbing;;
 742                rev-parse)        : plumbing;;
 743                runstatus)        : plumbing;;
 744                sh-setup)         : internal;;
 745                shell)            : daemon;;
 746                show-ref)         : plumbing;;
 747                send-pack)        : plumbing;;
 748                show-index)       : plumbing;;
 749                ssh-*)            : transport;;
 750                stripspace)       : plumbing;;
 751                symbolic-ref)     : plumbing;;
 752                tar-tree)         : deprecated;;
 753                unpack-file)      : plumbing;;
 754                unpack-objects)   : plumbing;;
 755                update-index)     : plumbing;;
 756                update-ref)       : plumbing;;
 757                update-server-info) : daemon;;
 758                upload-archive)   : plumbing;;
 759                upload-pack)      : plumbing;;
 760                write-tree)       : plumbing;;
 761                var)              : infrequent;;
 762                verify-pack)      : infrequent;;
 763                verify-tag)       : plumbing;;
 764                *) echo $i;;
 765                esac
 766        done
 767}
 768
 769__git_porcelain_commands=
 770__git_compute_porcelain_commands ()
 771{
 772        __git_compute_all_commands
 773        : ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
 774}
 775
 776__git_pretty_aliases ()
 777{
 778        local i IFS=$'\n'
 779        for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
 780                case "$i" in
 781                pretty.*)
 782                        i="${i#pretty.}"
 783                        echo "${i/ */}"
 784                        ;;
 785                esac
 786        done
 787}
 788
 789__git_aliases ()
 790{
 791        local i IFS=$'\n'
 792        for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
 793                case "$i" in
 794                alias.*)
 795                        i="${i#alias.}"
 796                        echo "${i/ */}"
 797                        ;;
 798                esac
 799        done
 800}
 801
 802# __git_aliased_command requires 1 argument
 803__git_aliased_command ()
 804{
 805        local word cmdline=$(git --git-dir="$(__gitdir)" \
 806                config --get "alias.$1")
 807        for word in $cmdline; do
 808                case "$word" in
 809                \!gitk|gitk)
 810                        echo "gitk"
 811                        return
 812                        ;;
 813                \!*)    : shell command alias ;;
 814                -*)     : option ;;
 815                *=*)    : setting env ;;
 816                git)    : git itself ;;
 817                *)
 818                        echo "$word"
 819                        return
 820                esac
 821        done
 822}
 823
 824# __git_find_on_cmdline requires 1 argument
 825__git_find_on_cmdline ()
 826{
 827        local word subcommand c=1
 828
 829        while [ $c -lt $COMP_CWORD ]; do
 830                word="${COMP_WORDS[c]}"
 831                for subcommand in $1; do
 832                        if [ "$subcommand" = "$word" ]; then
 833                                echo "$subcommand"
 834                                return
 835                        fi
 836                done
 837                c=$((++c))
 838        done
 839}
 840
 841__git_has_doubledash ()
 842{
 843        local c=1
 844        while [ $c -lt $COMP_CWORD ]; do
 845                if [ "--" = "${COMP_WORDS[c]}" ]; then
 846                        return 0
 847                fi
 848                c=$((++c))
 849        done
 850        return 1
 851}
 852
 853__git_whitespacelist="nowarn warn error error-all fix"
 854
 855_git_am ()
 856{
 857        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
 858        if [ -d "$dir"/rebase-apply ]; then
 859                __gitcomp "--skip --continue --resolved --abort"
 860                return
 861        fi
 862        case "$cur" in
 863        --whitespace=*)
 864                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 865                return
 866                ;;
 867        --*)
 868                __gitcomp "
 869                        --3way --committer-date-is-author-date --ignore-date
 870                        --ignore-whitespace --ignore-space-change
 871                        --interactive --keep --no-utf8 --signoff --utf8
 872                        --whitespace= --scissors
 873                        "
 874                return
 875        esac
 876        COMPREPLY=()
 877}
 878
 879_git_apply ()
 880{
 881        local cur="${COMP_WORDS[COMP_CWORD]}"
 882        case "$cur" in
 883        --whitespace=*)
 884                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 885                return
 886                ;;
 887        --*)
 888                __gitcomp "
 889                        --stat --numstat --summary --check --index
 890                        --cached --index-info --reverse --reject --unidiff-zero
 891                        --apply --no-add --exclude=
 892                        --ignore-whitespace --ignore-space-change
 893                        --whitespace= --inaccurate-eof --verbose
 894                        "
 895                return
 896        esac
 897        COMPREPLY=()
 898}
 899
 900_git_add ()
 901{
 902        __git_has_doubledash && return
 903
 904        local cur="${COMP_WORDS[COMP_CWORD]}"
 905        case "$cur" in
 906        --*)
 907                __gitcomp "
 908                        --interactive --refresh --patch --update --dry-run
 909                        --ignore-errors --intent-to-add
 910                        "
 911                return
 912        esac
 913        COMPREPLY=()
 914}
 915
 916_git_archive ()
 917{
 918        local cur="${COMP_WORDS[COMP_CWORD]}"
 919        case "$cur" in
 920        --format=*)
 921                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
 922                return
 923                ;;
 924        --remote=*)
 925                __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
 926                return
 927                ;;
 928        --*)
 929                __gitcomp "
 930                        --format= --list --verbose
 931                        --prefix= --remote= --exec=
 932                        "
 933                return
 934                ;;
 935        esac
 936        __git_complete_file
 937}
 938
 939_git_bisect ()
 940{
 941        __git_has_doubledash && return
 942
 943        local subcommands="start bad good skip reset visualize replay log run"
 944        local subcommand="$(__git_find_on_cmdline "$subcommands")"
 945        if [ -z "$subcommand" ]; then
 946                if [ -f "$(__gitdir)"/BISECT_START ]; then
 947                        __gitcomp "$subcommands"
 948                else
 949                        __gitcomp "replay start"
 950                fi
 951                return
 952        fi
 953
 954        case "$subcommand" in
 955        bad|good|reset|skip|start)
 956                __gitcomp "$(__git_refs)"
 957                ;;
 958        *)
 959                COMPREPLY=()
 960                ;;
 961        esac
 962}
 963
 964_git_branch ()
 965{
 966        local i c=1 only_local_ref="n" has_r="n"
 967
 968        while [ $c -lt $COMP_CWORD ]; do
 969                i="${COMP_WORDS[c]}"
 970                case "$i" in
 971                -d|-m)  only_local_ref="y" ;;
 972                -r)     has_r="y" ;;
 973                esac
 974                c=$((++c))
 975        done
 976
 977        case "${COMP_WORDS[COMP_CWORD]}" in
 978        --*)
 979                __gitcomp "
 980                        --color --no-color --verbose --abbrev= --no-abbrev
 981                        --track --no-track --contains --merged --no-merged
 982                        --set-upstream
 983                        "
 984                ;;
 985        *)
 986                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 987                        __gitcomp "$(__git_heads)"
 988                else
 989                        __gitcomp "$(__git_refs)"
 990                fi
 991                ;;
 992        esac
 993}
 994
 995_git_bundle ()
 996{
 997        local cmd="${COMP_WORDS[2]}"
 998        case "$COMP_CWORD" in
 999        2)
1000                __gitcomp "create list-heads verify unbundle"
1001                ;;
1002        3)
1003                # looking for a file
1004                ;;
1005        *)
1006                case "$cmd" in
1007                        create)
1008                                __git_complete_revlist
1009                        ;;
1010                esac
1011                ;;
1012        esac
1013}
1014
1015_git_checkout ()
1016{
1017        __git_has_doubledash && return
1018
1019        local cur="${COMP_WORDS[COMP_CWORD]}"
1020        case "$cur" in
1021        --conflict=*)
1022                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1023                ;;
1024        --*)
1025                __gitcomp "
1026                        --quiet --ours --theirs --track --no-track --merge
1027                        --conflict= --orphan --patch
1028                        "
1029                ;;
1030        *)
1031                # check if --track, --no-track, or --no-guess was specified
1032                # if so, disable DWIM mode
1033                local flags="--track --no-track --no-guess" track=1
1034                if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1035                        track=''
1036                fi
1037                __gitcomp "$(__git_refs '' $track)"
1038                ;;
1039        esac
1040}
1041
1042_git_cherry ()
1043{
1044        __gitcomp "$(__git_refs)"
1045}
1046
1047_git_cherry_pick ()
1048{
1049        local cur="${COMP_WORDS[COMP_CWORD]}"
1050        case "$cur" in
1051        --*)
1052                __gitcomp "--edit --no-commit"
1053                ;;
1054        *)
1055                __gitcomp "$(__git_refs)"
1056                ;;
1057        esac
1058}
1059
1060_git_clean ()
1061{
1062        __git_has_doubledash && return
1063
1064        local cur="${COMP_WORDS[COMP_CWORD]}"
1065        case "$cur" in
1066        --*)
1067                __gitcomp "--dry-run --quiet"
1068                return
1069                ;;
1070        esac
1071        COMPREPLY=()
1072}
1073
1074_git_clone ()
1075{
1076        local cur="${COMP_WORDS[COMP_CWORD]}"
1077        case "$cur" in
1078        --*)
1079                __gitcomp "
1080                        --local
1081                        --no-hardlinks
1082                        --shared
1083                        --reference
1084                        --quiet
1085                        --no-checkout
1086                        --bare
1087                        --mirror
1088                        --origin
1089                        --upload-pack
1090                        --template=
1091                        --depth
1092                        "
1093                return
1094                ;;
1095        esac
1096        COMPREPLY=()
1097}
1098
1099_git_commit ()
1100{
1101        __git_has_doubledash && return
1102
1103        local cur="${COMP_WORDS[COMP_CWORD]}"
1104        case "$cur" in
1105        --cleanup=*)
1106                __gitcomp "default strip verbatim whitespace
1107                        " "" "${cur##--cleanup=}"
1108                return
1109                ;;
1110        --reuse-message=*)
1111                __gitcomp "$(__git_refs)" "" "${cur##--reuse-message=}"
1112                return
1113                ;;
1114        --reedit-message=*)
1115                __gitcomp "$(__git_refs)" "" "${cur##--reedit-message=}"
1116                return
1117                ;;
1118        --untracked-files=*)
1119                __gitcomp "all no normal" "" "${cur##--untracked-files=}"
1120                return
1121                ;;
1122        --*)
1123                __gitcomp "
1124                        --all --author= --signoff --verify --no-verify
1125                        --edit --amend --include --only --interactive
1126                        --dry-run --reuse-message= --reedit-message=
1127                        --reset-author --file= --message= --template=
1128                        --cleanup= --untracked-files --untracked-files=
1129                        --verbose --quiet
1130                        "
1131                return
1132        esac
1133        COMPREPLY=()
1134}
1135
1136_git_describe ()
1137{
1138        local cur="${COMP_WORDS[COMP_CWORD]}"
1139        case "$cur" in
1140        --*)
1141                __gitcomp "
1142                        --all --tags --contains --abbrev= --candidates=
1143                        --exact-match --debug --long --match --always
1144                        "
1145                return
1146        esac
1147        __gitcomp "$(__git_refs)"
1148}
1149
1150__git_diff_common_options="--stat --numstat --shortstat --summary
1151                        --patch-with-stat --name-only --name-status --color
1152                        --no-color --color-words --no-renames --check
1153                        --full-index --binary --abbrev --diff-filter=
1154                        --find-copies-harder
1155                        --text --ignore-space-at-eol --ignore-space-change
1156                        --ignore-all-space --exit-code --quiet --ext-diff
1157                        --no-ext-diff
1158                        --no-prefix --src-prefix= --dst-prefix=
1159                        --inter-hunk-context=
1160                        --patience
1161                        --raw
1162                        --dirstat --dirstat= --dirstat-by-file
1163                        --dirstat-by-file= --cumulative
1164"
1165
1166_git_diff ()
1167{
1168        __git_has_doubledash && return
1169
1170        local cur="${COMP_WORDS[COMP_CWORD]}"
1171        case "$cur" in
1172        --*)
1173                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1174                        --base --ours --theirs --no-index
1175                        $__git_diff_common_options
1176                        "
1177                return
1178                ;;
1179        esac
1180        __git_complete_file
1181}
1182
1183__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
1184                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge
1185"
1186
1187_git_difftool ()
1188{
1189        __git_has_doubledash && return
1190
1191        local cur="${COMP_WORDS[COMP_CWORD]}"
1192        case "$cur" in
1193        --tool=*)
1194                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1195                return
1196                ;;
1197        --*)
1198                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1199                        --base --ours --theirs
1200                        --no-renames --diff-filter= --find-copies-harder
1201                        --relative --ignore-submodules
1202                        --tool="
1203                return
1204                ;;
1205        esac
1206        __git_complete_file
1207}
1208
1209__git_fetch_options="
1210        --quiet --verbose --append --upload-pack --force --keep --depth=
1211        --tags --no-tags --all --prune --dry-run
1212"
1213
1214_git_fetch ()
1215{
1216        local cur="${COMP_WORDS[COMP_CWORD]}"
1217        case "$cur" in
1218        --*)
1219                __gitcomp "$__git_fetch_options"
1220                return
1221                ;;
1222        esac
1223        __git_complete_remote_or_refspec
1224}
1225
1226_git_format_patch ()
1227{
1228        local cur="${COMP_WORDS[COMP_CWORD]}"
1229        case "$cur" in
1230        --thread=*)
1231                __gitcomp "
1232                        deep shallow
1233                        " "" "${cur##--thread=}"
1234                return
1235                ;;
1236        --*)
1237                __gitcomp "
1238                        --stdout --attach --no-attach --thread --thread=
1239                        --output-directory
1240                        --numbered --start-number
1241                        --numbered-files
1242                        --keep-subject
1243                        --signoff --signature --no-signature
1244                        --in-reply-to= --cc=
1245                        --full-index --binary
1246                        --not --all
1247                        --cover-letter
1248                        --no-prefix --src-prefix= --dst-prefix=
1249                        --inline --suffix= --ignore-if-in-upstream
1250                        --subject-prefix=
1251                        "
1252                return
1253                ;;
1254        esac
1255        __git_complete_revlist
1256}
1257
1258_git_fsck ()
1259{
1260        local cur="${COMP_WORDS[COMP_CWORD]}"
1261        case "$cur" in
1262        --*)
1263                __gitcomp "
1264                        --tags --root --unreachable --cache --no-reflogs --full
1265                        --strict --verbose --lost-found
1266                        "
1267                return
1268                ;;
1269        esac
1270        COMPREPLY=()
1271}
1272
1273_git_gc ()
1274{
1275        local cur="${COMP_WORDS[COMP_CWORD]}"
1276        case "$cur" in
1277        --*)
1278                __gitcomp "--prune --aggressive"
1279                return
1280                ;;
1281        esac
1282        COMPREPLY=()
1283}
1284
1285_git_gitk ()
1286{
1287        _gitk
1288}
1289
1290_git_grep ()
1291{
1292        __git_has_doubledash && return
1293
1294        local cur="${COMP_WORDS[COMP_CWORD]}"
1295        case "$cur" in
1296        --*)
1297                __gitcomp "
1298                        --cached
1299                        --text --ignore-case --word-regexp --invert-match
1300                        --full-name
1301                        --extended-regexp --basic-regexp --fixed-strings
1302                        --files-with-matches --name-only
1303                        --files-without-match
1304                        --max-depth
1305                        --count
1306                        --and --or --not --all-match
1307                        "
1308                return
1309                ;;
1310        esac
1311
1312        __gitcomp "$(__git_refs)"
1313}
1314
1315_git_help ()
1316{
1317        local cur="${COMP_WORDS[COMP_CWORD]}"
1318        case "$cur" in
1319        --*)
1320                __gitcomp "--all --info --man --web"
1321                return
1322                ;;
1323        esac
1324        __git_compute_all_commands
1325        __gitcomp "$__git_all_commands
1326                attributes cli core-tutorial cvs-migration
1327                diffcore gitk glossary hooks ignore modules
1328                repository-layout tutorial tutorial-2
1329                workflows
1330                "
1331}
1332
1333_git_init ()
1334{
1335        local cur="${COMP_WORDS[COMP_CWORD]}"
1336        case "$cur" in
1337        --shared=*)
1338                __gitcomp "
1339                        false true umask group all world everybody
1340                        " "" "${cur##--shared=}"
1341                return
1342                ;;
1343        --*)
1344                __gitcomp "--quiet --bare --template= --shared --shared="
1345                return
1346                ;;
1347        esac
1348        COMPREPLY=()
1349}
1350
1351_git_ls_files ()
1352{
1353        __git_has_doubledash && return
1354
1355        local cur="${COMP_WORDS[COMP_CWORD]}"
1356        case "$cur" in
1357        --*)
1358                __gitcomp "--cached --deleted --modified --others --ignored
1359                        --stage --directory --no-empty-directory --unmerged
1360                        --killed --exclude= --exclude-from=
1361                        --exclude-per-directory= --exclude-standard
1362                        --error-unmatch --with-tree= --full-name
1363                        --abbrev --ignored --exclude-per-directory
1364                        "
1365                return
1366                ;;
1367        esac
1368        COMPREPLY=()
1369}
1370
1371_git_ls_remote ()
1372{
1373        __gitcomp "$(__git_remotes)"
1374}
1375
1376_git_ls_tree ()
1377{
1378        __git_complete_file
1379}
1380
1381# Options that go well for log, shortlog and gitk
1382__git_log_common_options="
1383        --not --all
1384        --branches --tags --remotes
1385        --first-parent --merges --no-merges
1386        --max-count=
1387        --max-age= --since= --after=
1388        --min-age= --until= --before=
1389"
1390# Options that go well for log and gitk (not shortlog)
1391__git_log_gitk_options="
1392        --dense --sparse --full-history
1393        --simplify-merges --simplify-by-decoration
1394        --left-right
1395"
1396# Options that go well for log and shortlog (not gitk)
1397__git_log_shortlog_options="
1398        --author= --committer= --grep=
1399        --all-match
1400"
1401
1402__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1403__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1404
1405_git_log ()
1406{
1407        __git_has_doubledash && return
1408
1409        local cur="${COMP_WORDS[COMP_CWORD]}"
1410        local g="$(git rev-parse --git-dir 2>/dev/null)"
1411        local merge=""
1412        if [ -f "$g/MERGE_HEAD" ]; then
1413                merge="--merge"
1414        fi
1415        case "$cur" in
1416        --pretty=*)
1417                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1418                        " "" "${cur##--pretty=}"
1419                return
1420                ;;
1421        --format=*)
1422                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1423                        " "" "${cur##--format=}"
1424                return
1425                ;;
1426        --date=*)
1427                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1428                return
1429                ;;
1430        --decorate=*)
1431                __gitcomp "long short" "" "${cur##--decorate=}"
1432                return
1433                ;;
1434        --*)
1435                __gitcomp "
1436                        $__git_log_common_options
1437                        $__git_log_shortlog_options
1438                        $__git_log_gitk_options
1439                        --root --topo-order --date-order --reverse
1440                        --follow --full-diff
1441                        --abbrev-commit --abbrev=
1442                        --relative-date --date=
1443                        --pretty= --format= --oneline
1444                        --cherry-pick
1445                        --graph
1446                        --decorate --decorate=
1447                        --walk-reflogs
1448                        --parents --children
1449                        $merge
1450                        $__git_diff_common_options
1451                        --pickaxe-all --pickaxe-regex
1452                        "
1453                return
1454                ;;
1455        esac
1456        __git_complete_revlist
1457}
1458
1459__git_merge_options="
1460        --no-commit --no-stat --log --no-log --squash --strategy
1461        --commit --stat --no-squash --ff --no-ff --ff-only
1462"
1463
1464_git_merge ()
1465{
1466        __git_complete_strategy && return
1467
1468        local cur="${COMP_WORDS[COMP_CWORD]}"
1469        case "$cur" in
1470        --*)
1471                __gitcomp "$__git_merge_options"
1472                return
1473        esac
1474        __gitcomp "$(__git_refs)"
1475}
1476
1477_git_mergetool ()
1478{
1479        local cur="${COMP_WORDS[COMP_CWORD]}"
1480        case "$cur" in
1481        --tool=*)
1482                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1483                return
1484                ;;
1485        --*)
1486                __gitcomp "--tool="
1487                return
1488                ;;
1489        esac
1490        COMPREPLY=()
1491}
1492
1493_git_merge_base ()
1494{
1495        __gitcomp "$(__git_refs)"
1496}
1497
1498_git_mv ()
1499{
1500        local cur="${COMP_WORDS[COMP_CWORD]}"
1501        case "$cur" in
1502        --*)
1503                __gitcomp "--dry-run"
1504                return
1505                ;;
1506        esac
1507        COMPREPLY=()
1508}
1509
1510_git_name_rev ()
1511{
1512        __gitcomp "--tags --all --stdin"
1513}
1514
1515_git_notes ()
1516{
1517        local subcommands='add append copy edit list prune remove show'
1518        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1519        local cur="${COMP_WORDS[COMP_CWORD]}"
1520
1521        case "$subcommand,$cur" in
1522        ,--*)
1523                __gitcomp '--ref'
1524                ;;
1525        ,*)
1526                case "${COMP_WORDS[COMP_CWORD-1]}" in
1527                --ref)
1528                        __gitcomp "$(__git_refs)"
1529                        ;;
1530                *)
1531                        __gitcomp "$subcommands --ref"
1532                        ;;
1533                esac
1534                ;;
1535        add,--reuse-message=*|append,--reuse-message=*)
1536                __gitcomp "$(__git_refs)" "" "${cur##--reuse-message=}"
1537                ;;
1538        add,--reedit-message=*|append,--reedit-message=*)
1539                __gitcomp "$(__git_refs)" "" "${cur##--reedit-message=}"
1540                ;;
1541        add,--*|append,--*)
1542                __gitcomp '--file= --message= --reedit-message=
1543                                --reuse-message='
1544                ;;
1545        copy,--*)
1546                __gitcomp '--stdin'
1547                ;;
1548        prune,--*)
1549                __gitcomp '--dry-run --verbose'
1550                ;;
1551        prune,*)
1552                ;;
1553        *)
1554                case "${COMP_WORDS[COMP_CWORD-1]}" in
1555                -m|-F)
1556                        ;;
1557                *)
1558                        __gitcomp "$(__git_refs)"
1559                        ;;
1560                esac
1561                ;;
1562        esac
1563}
1564
1565_git_pull ()
1566{
1567        __git_complete_strategy && return
1568
1569        local cur="${COMP_WORDS[COMP_CWORD]}"
1570        case "$cur" in
1571        --*)
1572                __gitcomp "
1573                        --rebase --no-rebase
1574                        $__git_merge_options
1575                        $__git_fetch_options
1576                "
1577                return
1578                ;;
1579        esac
1580        __git_complete_remote_or_refspec
1581}
1582
1583_git_push ()
1584{
1585        local cur="${COMP_WORDS[COMP_CWORD]}"
1586        case "${COMP_WORDS[COMP_CWORD-1]}" in
1587        --repo)
1588                __gitcomp "$(__git_remotes)"
1589                return
1590        esac
1591        case "$cur" in
1592        --repo=*)
1593                __gitcomp "$(__git_remotes)" "" "${cur##--repo=}"
1594                return
1595                ;;
1596        --*)
1597                __gitcomp "
1598                        --all --mirror --tags --dry-run --force --verbose
1599                        --receive-pack= --repo=
1600                "
1601                return
1602                ;;
1603        esac
1604        __git_complete_remote_or_refspec
1605}
1606
1607_git_rebase ()
1608{
1609        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1610        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1611                __gitcomp "--continue --skip --abort"
1612                return
1613        fi
1614        __git_complete_strategy && return
1615        case "$cur" in
1616        --whitespace=*)
1617                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1618                return
1619                ;;
1620        --*)
1621                __gitcomp "
1622                        --onto --merge --strategy --interactive
1623                        --preserve-merges --stat --no-stat
1624                        --committer-date-is-author-date --ignore-date
1625                        --ignore-whitespace --whitespace=
1626                        --autosquash
1627                        "
1628
1629                return
1630        esac
1631        __gitcomp "$(__git_refs)"
1632}
1633
1634_git_reflog ()
1635{
1636        local subcommands="show delete expire"
1637        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1638
1639        if [ -z "$subcommand" ]; then
1640                __gitcomp "$subcommands"
1641        else
1642                __gitcomp "$(__git_refs)"
1643        fi
1644}
1645
1646__git_send_email_confirm_options="always never auto cc compose"
1647__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1648
1649_git_send_email ()
1650{
1651        local cur="${COMP_WORDS[COMP_CWORD]}"
1652        case "$cur" in
1653        --confirm=*)
1654                __gitcomp "
1655                        $__git_send_email_confirm_options
1656                        " "" "${cur##--confirm=}"
1657                return
1658                ;;
1659        --suppress-cc=*)
1660                __gitcomp "
1661                        $__git_send_email_suppresscc_options
1662                        " "" "${cur##--suppress-cc=}"
1663
1664                return
1665                ;;
1666        --smtp-encryption=*)
1667                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1668                return
1669                ;;
1670        --*)
1671                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1672                        --compose --confirm= --dry-run --envelope-sender
1673                        --from --identity
1674                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1675                        --no-suppress-from --no-thread --quiet
1676                        --signed-off-by-cc --smtp-pass --smtp-server
1677                        --smtp-server-port --smtp-encryption= --smtp-user
1678                        --subject --suppress-cc= --suppress-from --thread --to
1679                        --validate --no-validate"
1680                return
1681                ;;
1682        esac
1683        COMPREPLY=()
1684}
1685
1686_git_stage ()
1687{
1688        _git_add
1689}
1690
1691__git_config_get_set_variables ()
1692{
1693        local prevword word config_file= c=$COMP_CWORD
1694        while [ $c -gt 1 ]; do
1695                word="${COMP_WORDS[c]}"
1696                case "$word" in
1697                --global|--system|--file=*)
1698                        config_file="$word"
1699                        break
1700                        ;;
1701                -f|--file)
1702                        config_file="$word $prevword"
1703                        break
1704                        ;;
1705                esac
1706                prevword=$word
1707                c=$((--c))
1708        done
1709
1710        git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1711        while read line
1712        do
1713                case "$line" in
1714                *.*=*)
1715                        echo "${line/=*/}"
1716                        ;;
1717                esac
1718        done
1719}
1720
1721_git_config ()
1722{
1723        local cur="${COMP_WORDS[COMP_CWORD]}"
1724        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1725        case "$prv" in
1726        branch.*.remote)
1727                __gitcomp "$(__git_remotes)"
1728                return
1729                ;;
1730        branch.*.merge)
1731                __gitcomp "$(__git_refs)"
1732                return
1733                ;;
1734        remote.*.fetch)
1735                local remote="${prv#remote.}"
1736                remote="${remote%.fetch}"
1737                __gitcomp "$(__git_refs_remotes "$remote")"
1738                return
1739                ;;
1740        remote.*.push)
1741                local remote="${prv#remote.}"
1742                remote="${remote%.push}"
1743                __gitcomp "$(git --git-dir="$(__gitdir)" \
1744                        for-each-ref --format='%(refname):%(refname)' \
1745                        refs/heads)"
1746                return
1747                ;;
1748        pull.twohead|pull.octopus)
1749                __git_compute_merge_strategies
1750                __gitcomp "$__git_merge_strategies"
1751                return
1752                ;;
1753        color.branch|color.diff|color.interactive|\
1754        color.showbranch|color.status|color.ui)
1755                __gitcomp "always never auto"
1756                return
1757                ;;
1758        color.pager)
1759                __gitcomp "false true"
1760                return
1761                ;;
1762        color.*.*)
1763                __gitcomp "
1764                        normal black red green yellow blue magenta cyan white
1765                        bold dim ul blink reverse
1766                        "
1767                return
1768                ;;
1769        help.format)
1770                __gitcomp "man info web html"
1771                return
1772                ;;
1773        log.date)
1774                __gitcomp "$__git_log_date_formats"
1775                return
1776                ;;
1777        sendemail.aliasesfiletype)
1778                __gitcomp "mutt mailrc pine elm gnus"
1779                return
1780                ;;
1781        sendemail.confirm)
1782                __gitcomp "$__git_send_email_confirm_options"
1783                return
1784                ;;
1785        sendemail.suppresscc)
1786                __gitcomp "$__git_send_email_suppresscc_options"
1787                return
1788                ;;
1789        --get|--get-all|--unset|--unset-all)
1790                __gitcomp "$(__git_config_get_set_variables)"
1791                return
1792                ;;
1793        *.*)
1794                COMPREPLY=()
1795                return
1796                ;;
1797        esac
1798        case "$cur" in
1799        --*)
1800                __gitcomp "
1801                        --global --system --file=
1802                        --list --replace-all
1803                        --get --get-all --get-regexp
1804                        --add --unset --unset-all
1805                        --remove-section --rename-section
1806                        "
1807                return
1808                ;;
1809        branch.*.*)
1810                local pfx="${cur%.*}."
1811                cur="${cur##*.}"
1812                __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur"
1813                return
1814                ;;
1815        branch.*)
1816                local pfx="${cur%.*}."
1817                cur="${cur#*.}"
1818                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1819                return
1820                ;;
1821        guitool.*.*)
1822                local pfx="${cur%.*}."
1823                cur="${cur##*.}"
1824                __gitcomp "
1825                        argprompt cmd confirm needsfile noconsole norescan
1826                        prompt revprompt revunmerged title
1827                        " "$pfx" "$cur"
1828                return
1829                ;;
1830        difftool.*.*)
1831                local pfx="${cur%.*}."
1832                cur="${cur##*.}"
1833                __gitcomp "cmd path" "$pfx" "$cur"
1834                return
1835                ;;
1836        man.*.*)
1837                local pfx="${cur%.*}."
1838                cur="${cur##*.}"
1839                __gitcomp "cmd path" "$pfx" "$cur"
1840                return
1841                ;;
1842        mergetool.*.*)
1843                local pfx="${cur%.*}."
1844                cur="${cur##*.}"
1845                __gitcomp "cmd path trustExitCode" "$pfx" "$cur"
1846                return
1847                ;;
1848        pager.*)
1849                local pfx="${cur%.*}."
1850                cur="${cur#*.}"
1851                __git_compute_all_commands
1852                __gitcomp "$__git_all_commands" "$pfx" "$cur"
1853                return
1854                ;;
1855        remote.*.*)
1856                local pfx="${cur%.*}."
1857                cur="${cur##*.}"
1858                __gitcomp "
1859                        url proxy fetch push mirror skipDefaultUpdate
1860                        receivepack uploadpack tagopt pushurl
1861                        " "$pfx" "$cur"
1862                return
1863                ;;
1864        remote.*)
1865                local pfx="${cur%.*}."
1866                cur="${cur#*.}"
1867                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1868                return
1869                ;;
1870        url.*.*)
1871                local pfx="${cur%.*}."
1872                cur="${cur##*.}"
1873                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur"
1874                return
1875                ;;
1876        esac
1877        __gitcomp "
1878                add.ignore-errors
1879                alias.
1880                apply.ignorewhitespace
1881                apply.whitespace
1882                branch.autosetupmerge
1883                branch.autosetuprebase
1884                clean.requireForce
1885                color.branch
1886                color.branch.current
1887                color.branch.local
1888                color.branch.plain
1889                color.branch.remote
1890                color.diff
1891                color.diff.commit
1892                color.diff.frag
1893                color.diff.meta
1894                color.diff.new
1895                color.diff.old
1896                color.diff.plain
1897                color.diff.whitespace
1898                color.grep
1899                color.grep.external
1900                color.grep.match
1901                color.interactive
1902                color.interactive.header
1903                color.interactive.help
1904                color.interactive.prompt
1905                color.pager
1906                color.showbranch
1907                color.status
1908                color.status.added
1909                color.status.changed
1910                color.status.header
1911                color.status.nobranch
1912                color.status.untracked
1913                color.status.updated
1914                color.ui
1915                commit.template
1916                core.autocrlf
1917                core.bare
1918                core.compression
1919                core.createObject
1920                core.deltaBaseCacheLimit
1921                core.editor
1922                core.excludesfile
1923                core.fileMode
1924                core.fsyncobjectfiles
1925                core.gitProxy
1926                core.ignoreCygwinFSTricks
1927                core.ignoreStat
1928                core.logAllRefUpdates
1929                core.loosecompression
1930                core.packedGitLimit
1931                core.packedGitWindowSize
1932                core.pager
1933                core.preferSymlinkRefs
1934                core.preloadindex
1935                core.quotepath
1936                core.repositoryFormatVersion
1937                core.safecrlf
1938                core.sharedRepository
1939                core.symlinks
1940                core.trustctime
1941                core.warnAmbiguousRefs
1942                core.whitespace
1943                core.worktree
1944                diff.autorefreshindex
1945                diff.external
1946                diff.mnemonicprefix
1947                diff.renameLimit
1948                diff.renameLimit.
1949                diff.renames
1950                diff.suppressBlankEmpty
1951                diff.tool
1952                diff.wordRegex
1953                difftool.
1954                difftool.prompt
1955                fetch.unpackLimit
1956                format.attach
1957                format.cc
1958                format.headers
1959                format.numbered
1960                format.pretty
1961                format.signature
1962                format.signoff
1963                format.subjectprefix
1964                format.suffix
1965                format.thread
1966                gc.aggressiveWindow
1967                gc.auto
1968                gc.autopacklimit
1969                gc.packrefs
1970                gc.pruneexpire
1971                gc.reflogexpire
1972                gc.reflogexpireunreachable
1973                gc.rerereresolved
1974                gc.rerereunresolved
1975                gitcvs.allbinary
1976                gitcvs.commitmsgannotation
1977                gitcvs.dbTableNamePrefix
1978                gitcvs.dbdriver
1979                gitcvs.dbname
1980                gitcvs.dbpass
1981                gitcvs.dbuser
1982                gitcvs.enabled
1983                gitcvs.logfile
1984                gitcvs.usecrlfattr
1985                guitool.
1986                gui.blamehistoryctx
1987                gui.commitmsgwidth
1988                gui.copyblamethreshold
1989                gui.diffcontext
1990                gui.encoding
1991                gui.fastcopyblame
1992                gui.matchtrackingbranch
1993                gui.newbranchtemplate
1994                gui.pruneduringfetch
1995                gui.spellingdictionary
1996                gui.trustmtime
1997                help.autocorrect
1998                help.browser
1999                help.format
2000                http.lowSpeedLimit
2001                http.lowSpeedTime
2002                http.maxRequests
2003                http.noEPSV
2004                http.proxy
2005                http.sslCAInfo
2006                http.sslCAPath
2007                http.sslCert
2008                http.sslKey
2009                http.sslVerify
2010                i18n.commitEncoding
2011                i18n.logOutputEncoding
2012                imap.folder
2013                imap.host
2014                imap.pass
2015                imap.port
2016                imap.preformattedHTML
2017                imap.sslverify
2018                imap.tunnel
2019                imap.user
2020                instaweb.browser
2021                instaweb.httpd
2022                instaweb.local
2023                instaweb.modulepath
2024                instaweb.port
2025                interactive.singlekey
2026                log.date
2027                log.showroot
2028                mailmap.file
2029                man.
2030                man.viewer
2031                merge.conflictstyle
2032                merge.log
2033                merge.renameLimit
2034                merge.stat
2035                merge.tool
2036                merge.verbosity
2037                mergetool.
2038                mergetool.keepBackup
2039                mergetool.prompt
2040                pack.compression
2041                pack.deltaCacheLimit
2042                pack.deltaCacheSize
2043                pack.depth
2044                pack.indexVersion
2045                pack.packSizeLimit
2046                pack.threads
2047                pack.window
2048                pack.windowMemory
2049                pager.
2050                pull.octopus
2051                pull.twohead
2052                push.default
2053                rebase.stat
2054                receive.denyCurrentBranch
2055                receive.denyDeletes
2056                receive.denyNonFastForwards
2057                receive.fsckObjects
2058                receive.unpackLimit
2059                repack.usedeltabaseoffset
2060                rerere.autoupdate
2061                rerere.enabled
2062                sendemail.aliasesfile
2063                sendemail.aliasesfiletype
2064                sendemail.bcc
2065                sendemail.cc
2066                sendemail.cccmd
2067                sendemail.chainreplyto
2068                sendemail.confirm
2069                sendemail.envelopesender
2070                sendemail.multiedit
2071                sendemail.signedoffbycc
2072                sendemail.smtpencryption
2073                sendemail.smtppass
2074                sendemail.smtpserver
2075                sendemail.smtpserverport
2076                sendemail.smtpuser
2077                sendemail.suppresscc
2078                sendemail.suppressfrom
2079                sendemail.thread
2080                sendemail.to
2081                sendemail.validate
2082                showbranch.default
2083                status.relativePaths
2084                status.showUntrackedFiles
2085                tar.umask
2086                transfer.unpackLimit
2087                url.
2088                user.email
2089                user.name
2090                user.signingkey
2091                web.browser
2092                branch. remote.
2093        "
2094}
2095
2096_git_remote ()
2097{
2098        local subcommands="add rename rm show prune update set-head"
2099        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2100        if [ -z "$subcommand" ]; then
2101                __gitcomp "$subcommands"
2102                return
2103        fi
2104
2105        case "$subcommand" in
2106        rename|rm|show|prune)
2107                __gitcomp "$(__git_remotes)"
2108                ;;
2109        update)
2110                local i c='' IFS=$'\n'
2111                for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
2112                        i="${i#remotes.}"
2113                        c="$c ${i/ */}"
2114                done
2115                __gitcomp "$c"
2116                ;;
2117        *)
2118                COMPREPLY=()
2119                ;;
2120        esac
2121}
2122
2123_git_replace ()
2124{
2125        __gitcomp "$(__git_refs)"
2126}
2127
2128_git_reset ()
2129{
2130        __git_has_doubledash && return
2131
2132        local cur="${COMP_WORDS[COMP_CWORD]}"
2133        case "$cur" in
2134        --*)
2135                __gitcomp "--merge --mixed --hard --soft --patch"
2136                return
2137                ;;
2138        esac
2139        __gitcomp "$(__git_refs)"
2140}
2141
2142_git_revert ()
2143{
2144        local cur="${COMP_WORDS[COMP_CWORD]}"
2145        case "$cur" in
2146        --*)
2147                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
2148                return
2149                ;;
2150        esac
2151        __gitcomp "$(__git_refs)"
2152}
2153
2154_git_rm ()
2155{
2156        __git_has_doubledash && return
2157
2158        local cur="${COMP_WORDS[COMP_CWORD]}"
2159        case "$cur" in
2160        --*)
2161                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2162                return
2163                ;;
2164        esac
2165        COMPREPLY=()
2166}
2167
2168_git_shortlog ()
2169{
2170        __git_has_doubledash && return
2171
2172        local cur="${COMP_WORDS[COMP_CWORD]}"
2173        case "$cur" in
2174        --*)
2175                __gitcomp "
2176                        $__git_log_common_options
2177                        $__git_log_shortlog_options
2178                        --numbered --summary
2179                        "
2180                return
2181                ;;
2182        esac
2183        __git_complete_revlist
2184}
2185
2186_git_show ()
2187{
2188        __git_has_doubledash && return
2189
2190        local cur="${COMP_WORDS[COMP_CWORD]}"
2191        case "$cur" in
2192        --pretty=*)
2193                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2194                        " "" "${cur##--pretty=}"
2195                return
2196                ;;
2197        --format=*)
2198                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2199                        " "" "${cur##--format=}"
2200                return
2201                ;;
2202        --*)
2203                __gitcomp "--pretty= --format= --abbrev-commit --oneline
2204                        $__git_diff_common_options
2205                        "
2206                return
2207                ;;
2208        esac
2209        __git_complete_file
2210}
2211
2212_git_show_branch ()
2213{
2214        local cur="${COMP_WORDS[COMP_CWORD]}"
2215        case "$cur" in
2216        --*)
2217                __gitcomp "
2218                        --all --remotes --topo-order --current --more=
2219                        --list --independent --merge-base --no-name
2220                        --color --no-color
2221                        --sha1-name --sparse --topics --reflog
2222                        "
2223                return
2224                ;;
2225        esac
2226        __git_complete_revlist
2227}
2228
2229_git_stash ()
2230{
2231        local cur="${COMP_WORDS[COMP_CWORD]}"
2232        local save_opts='--keep-index --no-keep-index --quiet --patch'
2233        local subcommands='save list show apply clear drop pop create branch'
2234        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2235        if [ -z "$subcommand" ]; then
2236                case "$cur" in
2237                --*)
2238                        __gitcomp "$save_opts"
2239                        ;;
2240                *)
2241                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2242                                __gitcomp "$subcommands"
2243                        else
2244                                COMPREPLY=()
2245                        fi
2246                        ;;
2247                esac
2248        else
2249                case "$subcommand,$cur" in
2250                save,--*)
2251                        __gitcomp "$save_opts"
2252                        ;;
2253                apply,--*|pop,--*)
2254                        __gitcomp "--index --quiet"
2255                        ;;
2256                show,--*|drop,--*|branch,--*)
2257                        COMPREPLY=()
2258                        ;;
2259                show,*|apply,*|drop,*|pop,*|branch,*)
2260                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
2261                                        | sed -n -e 's/:.*//p')"
2262                        ;;
2263                *)
2264                        COMPREPLY=()
2265                        ;;
2266                esac
2267        fi
2268}
2269
2270_git_submodule ()
2271{
2272        __git_has_doubledash && return
2273
2274        local subcommands="add status init update summary foreach sync"
2275        if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2276                local cur="${COMP_WORDS[COMP_CWORD]}"
2277                case "$cur" in
2278                --*)
2279                        __gitcomp "--quiet --cached"
2280                        ;;
2281                *)
2282                        __gitcomp "$subcommands"
2283                        ;;
2284                esac
2285                return
2286        fi
2287}
2288
2289_git_svn ()
2290{
2291        local subcommands="
2292                init fetch clone rebase dcommit log find-rev
2293                set-tree commit-diff info create-ignore propget
2294                proplist show-ignore show-externals branch tag blame
2295                migrate mkdirs reset gc
2296                "
2297        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2298        if [ -z "$subcommand" ]; then
2299                __gitcomp "$subcommands"
2300        else
2301                local remote_opts="--username= --config-dir= --no-auth-cache"
2302                local fc_opts="
2303                        --follow-parent --authors-file= --repack=
2304                        --no-metadata --use-svm-props --use-svnsync-props
2305                        --log-window-size= --no-checkout --quiet
2306                        --repack-flags --use-log-author --localtime
2307                        --ignore-paths= $remote_opts
2308                        "
2309                local init_opts="
2310                        --template= --shared= --trunk= --tags=
2311                        --branches= --stdlayout --minimize-url
2312                        --no-metadata --use-svm-props --use-svnsync-props
2313                        --rewrite-root= --prefix= --use-log-author
2314                        --add-author-from $remote_opts
2315                        "
2316                local cmt_opts="
2317                        --edit --rmdir --find-copies-harder --copy-similarity=
2318                        "
2319
2320                local cur="${COMP_WORDS[COMP_CWORD]}"
2321                case "$subcommand,$cur" in
2322                fetch,--*)
2323                        __gitcomp "--revision= --fetch-all $fc_opts"
2324                        ;;
2325                clone,--*)
2326                        __gitcomp "--revision= $fc_opts $init_opts"
2327                        ;;
2328                init,--*)
2329                        __gitcomp "$init_opts"
2330                        ;;
2331                dcommit,--*)
2332                        __gitcomp "
2333                                --merge --strategy= --verbose --dry-run
2334                                --fetch-all --no-rebase --commit-url
2335                                --revision $cmt_opts $fc_opts
2336                                "
2337                        ;;
2338                set-tree,--*)
2339                        __gitcomp "--stdin $cmt_opts $fc_opts"
2340                        ;;
2341                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2342                show-externals,--*|mkdirs,--*)
2343                        __gitcomp "--revision="
2344                        ;;
2345                log,--*)
2346                        __gitcomp "
2347                                --limit= --revision= --verbose --incremental
2348                                --oneline --show-commit --non-recursive
2349                                --authors-file= --color
2350                                "
2351                        ;;
2352                rebase,--*)
2353                        __gitcomp "
2354                                --merge --verbose --strategy= --local
2355                                --fetch-all --dry-run $fc_opts
2356                                "
2357                        ;;
2358                commit-diff,--*)
2359                        __gitcomp "--message= --file= --revision= $cmt_opts"
2360                        ;;
2361                info,--*)
2362                        __gitcomp "--url"
2363                        ;;
2364                branch,--*)
2365                        __gitcomp "--dry-run --message --tag"
2366                        ;;
2367                tag,--*)
2368                        __gitcomp "--dry-run --message"
2369                        ;;
2370                blame,--*)
2371                        __gitcomp "--git-format"
2372                        ;;
2373                migrate,--*)
2374                        __gitcomp "
2375                                --config-dir= --ignore-paths= --minimize
2376                                --no-auth-cache --username=
2377                                "
2378                        ;;
2379                reset,--*)
2380                        __gitcomp "--revision= --parent"
2381                        ;;
2382                *)
2383                        COMPREPLY=()
2384                        ;;
2385                esac
2386        fi
2387}
2388
2389_git_tag ()
2390{
2391        local i c=1 f=0
2392        while [ $c -lt $COMP_CWORD ]; do
2393                i="${COMP_WORDS[c]}"
2394                case "$i" in
2395                -d|-v)
2396                        __gitcomp "$(__git_tags)"
2397                        return
2398                        ;;
2399                -f)
2400                        f=1
2401                        ;;
2402                esac
2403                c=$((++c))
2404        done
2405
2406        case "${COMP_WORDS[COMP_CWORD-1]}" in
2407        -m|-F)
2408                COMPREPLY=()
2409                ;;
2410        -*|tag)
2411                if [ $f = 1 ]; then
2412                        __gitcomp "$(__git_tags)"
2413                else
2414                        COMPREPLY=()
2415                fi
2416                ;;
2417        *)
2418                __gitcomp "$(__git_refs)"
2419                ;;
2420        esac
2421}
2422
2423_git_whatchanged ()
2424{
2425        _git_log
2426}
2427
2428_git ()
2429{
2430        local i c=1 command __git_dir
2431
2432        if [[ -n ${ZSH_VERSION-} ]]; then
2433                emulate -L bash
2434                setopt KSH_TYPESET
2435        fi
2436
2437        while [ $c -lt $COMP_CWORD ]; do
2438                i="${COMP_WORDS[c]}"
2439                case "$i" in
2440                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2441                --bare)      __git_dir="." ;;
2442                --version|-p|--paginate) ;;
2443                --help) command="help"; break ;;
2444                *) command="$i"; break ;;
2445                esac
2446                c=$((++c))
2447        done
2448
2449        if [ -z "$command" ]; then
2450                case "${COMP_WORDS[COMP_CWORD]}" in
2451                --*)   __gitcomp "
2452                        --paginate
2453                        --no-pager
2454                        --git-dir=
2455                        --bare
2456                        --version
2457                        --exec-path
2458                        --html-path
2459                        --work-tree=
2460                        --help
2461                        "
2462                        ;;
2463                *)     __git_compute_porcelain_commands
2464                       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2465                esac
2466                return
2467        fi
2468
2469        local completion_func="_git_${command//-/_}"
2470        declare -f $completion_func >/dev/null && $completion_func && return
2471
2472        local expansion=$(__git_aliased_command "$command")
2473        if [ -n "$expansion" ]; then
2474                completion_func="_git_${expansion//-/_}"
2475                declare -f $completion_func >/dev/null && $completion_func
2476        fi
2477}
2478
2479_gitk ()
2480{
2481        if [[ -n ${ZSH_VERSION-} ]]; then
2482                emulate -L bash
2483                setopt KSH_TYPESET
2484        fi
2485
2486        __git_has_doubledash && return
2487
2488        local cur="${COMP_WORDS[COMP_CWORD]}"
2489        local g="$(__gitdir)"
2490        local merge=""
2491        if [ -f "$g/MERGE_HEAD" ]; then
2492                merge="--merge"
2493        fi
2494        case "$cur" in
2495        --*)
2496                __gitcomp "
2497                        $__git_log_common_options
2498                        $__git_log_gitk_options
2499                        $merge
2500                        "
2501                return
2502                ;;
2503        esac
2504        __git_complete_revlist
2505}
2506
2507complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
2508        || complete -o default -o nospace -F _git git
2509complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
2510        || complete -o default -o nospace -F _gitk gitk
2511
2512# The following are necessary only for Cygwin, and only are needed
2513# when the user has tab-completed the executable name and consequently
2514# included the '.exe' suffix.
2515#
2516if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2517complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
2518        || complete -o default -o nospace -F _git git.exe
2519fi
2520
2521if [[ -n ${ZSH_VERSION-} ]]; then
2522        shopt () {
2523                local option
2524                if [ $# -ne 2 ]; then
2525                        echo "USAGE: $0 (-q|-s|-u) <option>" >&2
2526                        return 1
2527                fi
2528                case "$2" in
2529                nullglob)
2530                        option="$2"
2531                        ;;
2532                *)
2533                        echo "$0: invalid option: $2" >&2
2534                        return 1
2535                esac
2536                case "$1" in
2537                -q)     setopt | grep -q "$option" ;;
2538                -u)     unsetopt "$option" ;;
2539                -s)     setopt "$option" ;;
2540                *)
2541                        echo "$0: invalid flag: $1" >&2
2542                        return 1
2543                esac
2544        }
2545fi