056e43e4ad5410dfa42e20016e592b8825c48a41
   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#    3) You may want to make sure the git executable is available
  25#       in your PATH before this script is sourced, as some caching
  26#       is performed while the script loads.  If git isn't found
  27#       at source time then all lookups will be done on demand,
  28#       which may be slightly slower.
  29#
  30#    4) Consider changing your PS1 to also show the current branch:
  31#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  32#
  33#       The argument to __git_ps1 will be displayed only if you
  34#       are currently in a git repository.  The %s token will be
  35#       the name of the current branch.
  36#
  37#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
  38#       value, unstaged (*) and staged (+) changes will be shown next
  39#       to the branch name.  You can configure this per-repository
  40#       with the bash.showDirtyState variable, which defaults to true
  41#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
  42#
  43# To submit patches:
  44#
  45#    *) Read Documentation/SubmittingPatches
  46#    *) Send all patches to the current maintainer:
  47#
  48#       "Shawn O. Pearce" <spearce@spearce.org>
  49#
  50#    *) Always CC the Git mailing list:
  51#
  52#       git@vger.kernel.org
  53#
  54
  55case "$COMP_WORDBREAKS" in
  56*:*) : great ;;
  57*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  58esac
  59
  60# __gitdir accepts 0 or 1 arguments (i.e., location)
  61# returns location of .git repo
  62__gitdir ()
  63{
  64        if [ -z "${1-}" ]; then
  65                if [ -n "${__git_dir-}" ]; then
  66                        echo "$__git_dir"
  67                elif [ -d .git ]; then
  68                        echo .git
  69                else
  70                        git rev-parse --git-dir 2>/dev/null
  71                fi
  72        elif [ -d "$1/.git" ]; then
  73                echo "$1/.git"
  74        else
  75                echo "$1"
  76        fi
  77}
  78
  79# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
  80# returns text to add to bash PS1 prompt (includes branch name)
  81__git_ps1 ()
  82{
  83        local g="$(__gitdir)"
  84        if [ -n "$g" ]; then
  85                local r
  86                local b
  87                if [ -d "$g/rebase-apply" ]; then
  88                        if [ -f "$g/rebase-apply/rebasing" ]; then
  89                                r="|REBASE"
  90                elif [ -f "$g/rebase-apply/applying" ]; then
  91                                r="|AM"
  92                        else
  93                                r="|AM/REBASE"
  94                        fi
  95                        b="$(git symbolic-ref HEAD 2>/dev/null)"
  96                elif [ -f "$g/rebase-merge/interactive" ]; then
  97                        r="|REBASE-i"
  98                        b="$(cat "$g/rebase-merge/head-name")"
  99                elif [ -d "$g/rebase-merge" ]; then
 100                        r="|REBASE-m"
 101                        b="$(cat "$g/rebase-merge/head-name")"
 102                elif [ -f "$g/MERGE_HEAD" ]; then
 103                        r="|MERGING"
 104                        b="$(git symbolic-ref HEAD 2>/dev/null)"
 105                else
 106                        if [ -f "$g/BISECT_LOG" ]; then
 107                                r="|BISECTING"
 108                        fi
 109                        if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
 110                                if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
 111                                        if [ -r "$g/HEAD" ]; then
 112                                                b="$(cut -c1-7 "$g/HEAD")..."
 113                                        fi
 114                                fi
 115                        fi
 116                fi
 117
 118                local w
 119                local i
 120                local c
 121
 122                if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 123                        if [ "true" = "$(git config --bool core.bare 2>/dev/null)" ]; then
 124                                c="BARE:"
 125                        else
 126                                b="GIT_DIR!"
 127                        fi
 128                elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
 129                        if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
 130                                if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
 131                                        git diff --no-ext-diff --ignore-submodules \
 132                                                --quiet --exit-code || w="*"
 133                                        if git rev-parse --quiet --verify HEAD >/dev/null; then
 134                                                git diff-index --cached --quiet \
 135                                                        --ignore-submodules HEAD -- || i="+"
 136                                        else
 137                                                i="#"
 138                                        fi
 139                                fi
 140                        fi
 141                fi
 142
 143                if [ -n "$b" ]; then
 144                        if [ -n "${1-}" ]; then
 145                                printf "$1" "$c${b##refs/heads/}$w$i$r"
 146                        else
 147                                printf " (%s)" "$c${b##refs/heads/}$w$i$r"
 148                        fi
 149                fi
 150        fi
 151}
 152
 153# __gitcomp_1 requires 2 arguments
 154__gitcomp_1 ()
 155{
 156        local c IFS=' '$'\t'$'\n'
 157        for c in $1; do
 158                case "$c$2" in
 159                --*=*) printf %s$'\n' "$c$2" ;;
 160                *.)    printf %s$'\n' "$c$2" ;;
 161                *)     printf %s$'\n' "$c$2 " ;;
 162                esac
 163        done
 164}
 165
 166# __gitcomp accepts 1, 2, 3, or 4 arguments
 167# generates completion reply with compgen
 168__gitcomp ()
 169{
 170        local cur="${COMP_WORDS[COMP_CWORD]}"
 171        if [ $# -gt 2 ]; then
 172                cur="$3"
 173        fi
 174        case "$cur" in
 175        --*=)
 176                COMPREPLY=()
 177                ;;
 178        *)
 179                local IFS=$'\n'
 180                COMPREPLY=($(compgen -P "${2-}" \
 181                        -W "$(__gitcomp_1 "${1-}" "${4-}")" \
 182                        -- "$cur"))
 183                ;;
 184        esac
 185}
 186
 187# __git_heads accepts 0 or 1 arguments (to pass to __gitdir)
 188__git_heads ()
 189{
 190        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 191        if [ -d "$dir" ]; then
 192                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 193                        refs/heads
 194                return
 195        fi
 196        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 197                case "$is_hash,$i" in
 198                y,*) is_hash=n ;;
 199                n,*^{}) is_hash=y ;;
 200                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 201                n,*) is_hash=y; echo "$i" ;;
 202                esac
 203        done
 204}
 205
 206# __git_tags accepts 0 or 1 arguments (to pass to __gitdir)
 207__git_tags ()
 208{
 209        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 210        if [ -d "$dir" ]; then
 211                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 212                        refs/tags
 213                return
 214        fi
 215        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 216                case "$is_hash,$i" in
 217                y,*) is_hash=n ;;
 218                n,*^{}) is_hash=y ;;
 219                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 220                n,*) is_hash=y; echo "$i" ;;
 221                esac
 222        done
 223}
 224
 225# __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
 226__git_refs ()
 227{
 228        local i is_hash=y dir="$(__gitdir "${1-}")"
 229        local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 230        if [ -d "$dir" ]; then
 231                case "$cur" in
 232                refs|refs/*)
 233                        format="refname"
 234                        refs="${cur%/*}"
 235                        ;;
 236                *)
 237                        if [ -e "$dir/HEAD" ]; then echo HEAD; fi
 238                        format="refname:short"
 239                        refs="refs/tags refs/heads refs/remotes"
 240                        ;;
 241                esac
 242                git --git-dir="$dir" for-each-ref --format="%($format)" \
 243                        $refs
 244                return
 245        fi
 246        for i in $(git ls-remote "$dir" 2>/dev/null); do
 247                case "$is_hash,$i" in
 248                y,*) is_hash=n ;;
 249                n,*^{}) is_hash=y ;;
 250                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 251                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 252                n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
 253                n,*) is_hash=y; echo "$i" ;;
 254                esac
 255        done
 256}
 257
 258# __git_refs2 requires 1 argument (to pass to __git_refs)
 259__git_refs2 ()
 260{
 261        local i
 262        for i in $(__git_refs "$1"); do
 263                echo "$i:$i"
 264        done
 265}
 266
 267# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 268__git_refs_remotes ()
 269{
 270        local cmd i is_hash=y
 271        for i in $(git ls-remote "$1" 2>/dev/null); do
 272                case "$is_hash,$i" in
 273                n,refs/heads/*)
 274                        is_hash=y
 275                        echo "$i:refs/remotes/$1/${i#refs/heads/}"
 276                        ;;
 277                y,*) is_hash=n ;;
 278                n,*^{}) is_hash=y ;;
 279                n,refs/tags/*) is_hash=y;;
 280                n,*) is_hash=y; ;;
 281                esac
 282        done
 283}
 284
 285__git_remotes ()
 286{
 287        local i ngoff IFS=$'\n' d="$(__gitdir)"
 288        shopt -q nullglob || ngoff=1
 289        shopt -s nullglob
 290        for i in "$d/remotes"/*; do
 291                echo ${i#$d/remotes/}
 292        done
 293        [ "$ngoff" ] && shopt -u nullglob
 294        for i in $(git --git-dir="$d" config --list); do
 295                case "$i" in
 296                remote.*.url=*)
 297                        i="${i#remote.}"
 298                        echo "${i/.url=*/}"
 299                        ;;
 300                esac
 301        done
 302}
 303
 304__git_merge_strategies ()
 305{
 306        if [ -n "${__git_merge_strategylist-}" ]; then
 307                echo "$__git_merge_strategylist"
 308                return
 309        fi
 310        git merge -s help 2>&1 |
 311        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 312                s/\.$//
 313                s/.*://
 314                s/^[    ]*//
 315                s/[     ]*$//
 316                p
 317        }'
 318}
 319__git_merge_strategylist=
 320__git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
 321
 322__git_complete_file ()
 323{
 324        local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
 325        case "$cur" in
 326        ?*:*)
 327                ref="${cur%%:*}"
 328                cur="${cur#*:}"
 329                case "$cur" in
 330                ?*/*)
 331                        pfx="${cur%/*}"
 332                        cur="${cur##*/}"
 333                        ls="$ref:$pfx"
 334                        pfx="$pfx/"
 335                        ;;
 336                *)
 337                        ls="$ref"
 338                        ;;
 339            esac
 340
 341                case "$COMP_WORDBREAKS" in
 342                *:*) : great ;;
 343                *)   pfx="$ref:$pfx" ;;
 344                esac
 345
 346                local IFS=$'\n'
 347                COMPREPLY=($(compgen -P "$pfx" \
 348                        -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
 349                                | sed '/^100... blob /{
 350                                           s,^.*        ,,
 351                                           s,$, ,
 352                                       }
 353                                       /^120000 blob /{
 354                                           s,^.*        ,,
 355                                           s,$, ,
 356                                       }
 357                                       /^040000 tree /{
 358                                           s,^.*        ,,
 359                                           s,$,/,
 360                                       }
 361                                       s/^.*    //')" \
 362                        -- "$cur"))
 363                ;;
 364        *)
 365                __gitcomp "$(__git_refs)"
 366                ;;
 367        esac
 368}
 369
 370__git_complete_revlist ()
 371{
 372        local pfx cur="${COMP_WORDS[COMP_CWORD]}"
 373        case "$cur" in
 374        *...*)
 375                pfx="${cur%...*}..."
 376                cur="${cur#*...}"
 377                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 378                ;;
 379        *..*)
 380                pfx="${cur%..*}.."
 381                cur="${cur#*..}"
 382                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 383                ;;
 384        *)
 385                __gitcomp "$(__git_refs)"
 386                ;;
 387        esac
 388}
 389
 390__git_complete_remote_or_refspec ()
 391{
 392        local cmd="${COMP_WORDS[1]}"
 393        local cur="${COMP_WORDS[COMP_CWORD]}"
 394        local i c=2 remote="" pfx="" lhs=1
 395        while [ $c -lt $COMP_CWORD ]; do
 396                i="${COMP_WORDS[c]}"
 397                case "$i" in
 398                -*) ;;
 399                *) remote="$i"; break ;;
 400                esac
 401                c=$((++c))
 402        done
 403        if [ -z "$remote" ]; then
 404                __gitcomp "$(__git_remotes)"
 405                return
 406        fi
 407        [ "$remote" = "." ] && remote=
 408        case "$cur" in
 409        *:*)
 410                case "$COMP_WORDBREAKS" in
 411                *:*) : great ;;
 412                *)   pfx="${cur%%:*}:" ;;
 413                esac
 414                cur="${cur#*:}"
 415                lhs=0
 416                ;;
 417        +*)
 418                pfx="+"
 419                cur="${cur#+}"
 420                ;;
 421        esac
 422        case "$cmd" in
 423        fetch)
 424                if [ $lhs = 1 ]; then
 425                        __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
 426                else
 427                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 428                fi
 429                ;;
 430        pull)
 431                if [ $lhs = 1 ]; then
 432                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 433                else
 434                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 435                fi
 436                ;;
 437        push)
 438                if [ $lhs = 1 ]; then
 439                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 440                else
 441                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 442                fi
 443                ;;
 444        esac
 445}
 446
 447__git_complete_strategy ()
 448{
 449        case "${COMP_WORDS[COMP_CWORD-1]}" in
 450        -s|--strategy)
 451                __gitcomp "$(__git_merge_strategies)"
 452                return 0
 453        esac
 454        local cur="${COMP_WORDS[COMP_CWORD]}"
 455        case "$cur" in
 456        --strategy=*)
 457                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
 458                return 0
 459                ;;
 460        esac
 461        return 1
 462}
 463
 464__git_all_commands ()
 465{
 466        if [ -n "${__git_all_commandlist-}" ]; then
 467                echo "$__git_all_commandlist"
 468                return
 469        fi
 470        local i IFS=" "$'\n'
 471        for i in $(git help -a|egrep '^ ')
 472        do
 473                case $i in
 474                *--*)             : helper pattern;;
 475                *) echo $i;;
 476                esac
 477        done
 478}
 479__git_all_commandlist=
 480__git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 481
 482__git_porcelain_commands ()
 483{
 484        if [ -n "${__git_porcelain_commandlist-}" ]; then
 485                echo "$__git_porcelain_commandlist"
 486                return
 487        fi
 488        local i IFS=" "$'\n'
 489        for i in "help" $(__git_all_commands)
 490        do
 491                case $i in
 492                *--*)             : helper pattern;;
 493                applymbox)        : ask gittus;;
 494                applypatch)       : ask gittus;;
 495                archimport)       : import;;
 496                cat-file)         : plumbing;;
 497                check-attr)       : plumbing;;
 498                check-ref-format) : plumbing;;
 499                checkout-index)   : plumbing;;
 500                commit-tree)      : plumbing;;
 501                count-objects)    : infrequent;;
 502                cvsexportcommit)  : export;;
 503                cvsimport)        : import;;
 504                cvsserver)        : daemon;;
 505                daemon)           : daemon;;
 506                diff-files)       : plumbing;;
 507                diff-index)       : plumbing;;
 508                diff-tree)        : plumbing;;
 509                fast-import)      : import;;
 510                fast-export)      : export;;
 511                fsck-objects)     : plumbing;;
 512                fetch-pack)       : plumbing;;
 513                fmt-merge-msg)    : plumbing;;
 514                for-each-ref)     : plumbing;;
 515                hash-object)      : plumbing;;
 516                http-*)           : transport;;
 517                index-pack)       : plumbing;;
 518                init-db)          : deprecated;;
 519                local-fetch)      : plumbing;;
 520                lost-found)       : infrequent;;
 521                ls-files)         : plumbing;;
 522                ls-remote)        : plumbing;;
 523                ls-tree)          : plumbing;;
 524                mailinfo)         : plumbing;;
 525                mailsplit)        : plumbing;;
 526                merge-*)          : plumbing;;
 527                mktree)           : plumbing;;
 528                mktag)            : plumbing;;
 529                pack-objects)     : plumbing;;
 530                pack-redundant)   : plumbing;;
 531                pack-refs)        : plumbing;;
 532                parse-remote)     : plumbing;;
 533                patch-id)         : plumbing;;
 534                peek-remote)      : plumbing;;
 535                prune)            : plumbing;;
 536                prune-packed)     : plumbing;;
 537                quiltimport)      : import;;
 538                read-tree)        : plumbing;;
 539                receive-pack)     : plumbing;;
 540                reflog)           : plumbing;;
 541                repo-config)      : deprecated;;
 542                rerere)           : plumbing;;
 543                rev-list)         : plumbing;;
 544                rev-parse)        : plumbing;;
 545                runstatus)        : plumbing;;
 546                sh-setup)         : internal;;
 547                shell)            : daemon;;
 548                show-ref)         : plumbing;;
 549                send-pack)        : plumbing;;
 550                show-index)       : plumbing;;
 551                ssh-*)            : transport;;
 552                stripspace)       : plumbing;;
 553                symbolic-ref)     : plumbing;;
 554                tar-tree)         : deprecated;;
 555                unpack-file)      : plumbing;;
 556                unpack-objects)   : plumbing;;
 557                update-index)     : plumbing;;
 558                update-ref)       : plumbing;;
 559                update-server-info) : daemon;;
 560                upload-archive)   : plumbing;;
 561                upload-pack)      : plumbing;;
 562                write-tree)       : plumbing;;
 563                var)              : infrequent;;
 564                verify-pack)      : infrequent;;
 565                verify-tag)       : plumbing;;
 566                *) echo $i;;
 567                esac
 568        done
 569}
 570__git_porcelain_commandlist=
 571__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
 572
 573__git_aliases ()
 574{
 575        local i IFS=$'\n'
 576        for i in $(git --git-dir="$(__gitdir)" config --list); do
 577                case "$i" in
 578                alias.*)
 579                        i="${i#alias.}"
 580                        echo "${i/=*/}"
 581                        ;;
 582                esac
 583        done
 584}
 585
 586# __git_aliased_command requires 1 argument
 587__git_aliased_command ()
 588{
 589        local word cmdline=$(git --git-dir="$(__gitdir)" \
 590                config --get "alias.$1")
 591        for word in $cmdline; do
 592                if [ "${word##-*}" ]; then
 593                        echo $word
 594                        return
 595                fi
 596        done
 597}
 598
 599# __git_find_subcommand requires 1 argument
 600__git_find_subcommand ()
 601{
 602        local word subcommand c=1
 603
 604        while [ $c -lt $COMP_CWORD ]; do
 605                word="${COMP_WORDS[c]}"
 606                for subcommand in $1; do
 607                        if [ "$subcommand" = "$word" ]; then
 608                                echo "$subcommand"
 609                                return
 610                        fi
 611                done
 612                c=$((++c))
 613        done
 614}
 615
 616__git_has_doubledash ()
 617{
 618        local c=1
 619        while [ $c -lt $COMP_CWORD ]; do
 620                if [ "--" = "${COMP_WORDS[c]}" ]; then
 621                        return 0
 622                fi
 623                c=$((++c))
 624        done
 625        return 1
 626}
 627
 628__git_whitespacelist="nowarn warn error error-all fix"
 629
 630_git_am ()
 631{
 632        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
 633        if [ -d "$dir"/rebase-apply ]; then
 634                __gitcomp "--skip --resolved --abort"
 635                return
 636        fi
 637        case "$cur" in
 638        --whitespace=*)
 639                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 640                return
 641                ;;
 642        --*)
 643                __gitcomp "
 644                        --signoff --utf8 --binary --3way --interactive
 645                        --whitespace=
 646                        "
 647                return
 648        esac
 649        COMPREPLY=()
 650}
 651
 652_git_apply ()
 653{
 654        local cur="${COMP_WORDS[COMP_CWORD]}"
 655        case "$cur" in
 656        --whitespace=*)
 657                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 658                return
 659                ;;
 660        --*)
 661                __gitcomp "
 662                        --stat --numstat --summary --check --index
 663                        --cached --index-info --reverse --reject --unidiff-zero
 664                        --apply --no-add --exclude=
 665                        --whitespace= --inaccurate-eof --verbose
 666                        "
 667                return
 668        esac
 669        COMPREPLY=()
 670}
 671
 672_git_add ()
 673{
 674        __git_has_doubledash && return
 675
 676        local cur="${COMP_WORDS[COMP_CWORD]}"
 677        case "$cur" in
 678        --*)
 679                __gitcomp "
 680                        --interactive --refresh --patch --update --dry-run
 681                        --ignore-errors --intent-to-add
 682                        "
 683                return
 684        esac
 685        COMPREPLY=()
 686}
 687
 688_git_archive ()
 689{
 690        local cur="${COMP_WORDS[COMP_CWORD]}"
 691        case "$cur" in
 692        --format=*)
 693                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
 694                return
 695                ;;
 696        --remote=*)
 697                __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
 698                return
 699                ;;
 700        --*)
 701                __gitcomp "
 702                        --format= --list --verbose
 703                        --prefix= --remote= --exec=
 704                        "
 705                return
 706                ;;
 707        esac
 708        __git_complete_file
 709}
 710
 711_git_bisect ()
 712{
 713        __git_has_doubledash && return
 714
 715        local subcommands="start bad good skip reset visualize replay log run"
 716        local subcommand="$(__git_find_subcommand "$subcommands")"
 717        if [ -z "$subcommand" ]; then
 718                __gitcomp "$subcommands"
 719                return
 720        fi
 721
 722        case "$subcommand" in
 723        bad|good|reset|skip)
 724                __gitcomp "$(__git_refs)"
 725                ;;
 726        *)
 727                COMPREPLY=()
 728                ;;
 729        esac
 730}
 731
 732_git_branch ()
 733{
 734        local i c=1 only_local_ref="n" has_r="n"
 735
 736        while [ $c -lt $COMP_CWORD ]; do
 737                i="${COMP_WORDS[c]}"
 738                case "$i" in
 739                -d|-m)  only_local_ref="y" ;;
 740                -r)     has_r="y" ;;
 741                esac
 742                c=$((++c))
 743        done
 744
 745        case "${COMP_WORDS[COMP_CWORD]}" in
 746        --*)
 747                __gitcomp "
 748                        --color --no-color --verbose --abbrev= --no-abbrev
 749                        --track --no-track --contains --merged --no-merged
 750                        "
 751                ;;
 752        *)
 753                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 754                        __gitcomp "$(__git_heads)"
 755                else
 756                        __gitcomp "$(__git_refs)"
 757                fi
 758                ;;
 759        esac
 760}
 761
 762_git_bundle ()
 763{
 764        local cmd="${COMP_WORDS[2]}"
 765        case "$COMP_CWORD" in
 766        2)
 767                __gitcomp "create list-heads verify unbundle"
 768                ;;
 769        3)
 770                # looking for a file
 771                ;;
 772        *)
 773                case "$cmd" in
 774                        create)
 775                                __git_complete_revlist
 776                        ;;
 777                esac
 778                ;;
 779        esac
 780}
 781
 782_git_checkout ()
 783{
 784        __git_has_doubledash && return
 785
 786        __gitcomp "$(__git_refs)"
 787}
 788
 789_git_cherry ()
 790{
 791        __gitcomp "$(__git_refs)"
 792}
 793
 794_git_cherry_pick ()
 795{
 796        local cur="${COMP_WORDS[COMP_CWORD]}"
 797        case "$cur" in
 798        --*)
 799                __gitcomp "--edit --no-commit"
 800                ;;
 801        *)
 802                __gitcomp "$(__git_refs)"
 803                ;;
 804        esac
 805}
 806
 807_git_clean ()
 808{
 809        __git_has_doubledash && return
 810
 811        local cur="${COMP_WORDS[COMP_CWORD]}"
 812        case "$cur" in
 813        --*)
 814                __gitcomp "--dry-run --quiet"
 815                return
 816                ;;
 817        esac
 818        COMPREPLY=()
 819}
 820
 821_git_clone ()
 822{
 823        local cur="${COMP_WORDS[COMP_CWORD]}"
 824        case "$cur" in
 825        --*)
 826                __gitcomp "
 827                        --local
 828                        --no-hardlinks
 829                        --shared
 830                        --reference
 831                        --quiet
 832                        --no-checkout
 833                        --bare
 834                        --mirror
 835                        --origin
 836                        --upload-pack
 837                        --template=
 838                        --depth
 839                        "
 840                return
 841                ;;
 842        esac
 843        COMPREPLY=()
 844}
 845
 846_git_commit ()
 847{
 848        __git_has_doubledash && return
 849
 850        local cur="${COMP_WORDS[COMP_CWORD]}"
 851        case "$cur" in
 852        --*)
 853                __gitcomp "
 854                        --all --author= --signoff --verify --no-verify
 855                        --edit --amend --include --only --interactive
 856                        "
 857                return
 858        esac
 859        COMPREPLY=()
 860}
 861
 862_git_describe ()
 863{
 864        local cur="${COMP_WORDS[COMP_CWORD]}"
 865        case "$cur" in
 866        --*)
 867                __gitcomp "
 868                        --all --tags --contains --abbrev= --candidates=
 869                        --exact-match --debug --long --match --always
 870                        "
 871                return
 872        esac
 873        __gitcomp "$(__git_refs)"
 874}
 875
 876__git_diff_common_options="--stat --numstat --shortstat --summary
 877                        --patch-with-stat --name-only --name-status --color
 878                        --no-color --color-words --no-renames --check
 879                        --full-index --binary --abbrev --diff-filter=
 880                        --find-copies-harder
 881                        --text --ignore-space-at-eol --ignore-space-change
 882                        --ignore-all-space --exit-code --quiet --ext-diff
 883                        --no-ext-diff
 884                        --no-prefix --src-prefix= --dst-prefix=
 885                        --inter-hunk-context=
 886                        --patience
 887                        --raw
 888"
 889
 890_git_diff ()
 891{
 892        __git_has_doubledash && return
 893
 894        local cur="${COMP_WORDS[COMP_CWORD]}"
 895        case "$cur" in
 896        --*)
 897                __gitcomp "--cached --pickaxe-all --pickaxe-regex
 898                        --base --ours --theirs
 899                        $__git_diff_common_options
 900                        "
 901                return
 902                ;;
 903        esac
 904        __git_complete_file
 905}
 906
 907_git_fetch ()
 908{
 909        __git_complete_remote_or_refspec
 910}
 911
 912_git_format_patch ()
 913{
 914        local cur="${COMP_WORDS[COMP_CWORD]}"
 915        case "$cur" in
 916        --*)
 917                __gitcomp "
 918                        --stdout --attach --thread
 919                        --output-directory
 920                        --numbered --start-number
 921                        --numbered-files
 922                        --keep-subject
 923                        --signoff
 924                        --in-reply-to=
 925                        --full-index --binary
 926                        --not --all
 927                        --cover-letter
 928                        --no-prefix --src-prefix= --dst-prefix=
 929                        --inline --suffix= --ignore-if-in-upstream
 930                        --subject-prefix=
 931                        "
 932                return
 933                ;;
 934        esac
 935        __git_complete_revlist
 936}
 937
 938_git_gc ()
 939{
 940        local cur="${COMP_WORDS[COMP_CWORD]}"
 941        case "$cur" in
 942        --*)
 943                __gitcomp "--prune --aggressive"
 944                return
 945                ;;
 946        esac
 947        COMPREPLY=()
 948}
 949
 950_git_grep ()
 951{
 952        __git_has_doubledash && return
 953
 954        local cur="${COMP_WORDS[COMP_CWORD]}"
 955        case "$cur" in
 956        --*)
 957                __gitcomp "
 958                        --cached
 959                        --text --ignore-case --word-regexp --invert-match
 960                        --full-name
 961                        --extended-regexp --basic-regexp --fixed-strings
 962                        --files-with-matches --name-only
 963                        --files-without-match
 964                        --count
 965                        --and --or --not --all-match
 966                        "
 967                return
 968                ;;
 969        esac
 970        COMPREPLY=()
 971}
 972
 973_git_help ()
 974{
 975        local cur="${COMP_WORDS[COMP_CWORD]}"
 976        case "$cur" in
 977        --*)
 978                __gitcomp "--all --info --man --web"
 979                return
 980                ;;
 981        esac
 982        __gitcomp "$(__git_all_commands)
 983                attributes cli core-tutorial cvs-migration
 984                diffcore gitk glossary hooks ignore modules
 985                repository-layout tutorial tutorial-2
 986                workflows
 987                "
 988}
 989
 990_git_init ()
 991{
 992        local cur="${COMP_WORDS[COMP_CWORD]}"
 993        case "$cur" in
 994        --shared=*)
 995                __gitcomp "
 996                        false true umask group all world everybody
 997                        " "" "${cur##--shared=}"
 998                return
 999                ;;
1000        --*)
1001                __gitcomp "--quiet --bare --template= --shared --shared="
1002                return
1003                ;;
1004        esac
1005        COMPREPLY=()
1006}
1007
1008_git_ls_files ()
1009{
1010        __git_has_doubledash && return
1011
1012        local cur="${COMP_WORDS[COMP_CWORD]}"
1013        case "$cur" in
1014        --*)
1015                __gitcomp "--cached --deleted --modified --others --ignored
1016                        --stage --directory --no-empty-directory --unmerged
1017                        --killed --exclude= --exclude-from=
1018                        --exclude-per-directory= --exclude-standard
1019                        --error-unmatch --with-tree= --full-name
1020                        --abbrev --ignored --exclude-per-directory
1021                        "
1022                return
1023                ;;
1024        esac
1025        COMPREPLY=()
1026}
1027
1028_git_ls_remote ()
1029{
1030        __gitcomp "$(__git_remotes)"
1031}
1032
1033_git_ls_tree ()
1034{
1035        __git_complete_file
1036}
1037
1038# Options that go well for log, shortlog and gitk
1039__git_log_common_options="
1040        --not --all
1041        --branches --tags --remotes
1042        --first-parent --no-merges
1043        --max-count=
1044        --max-age= --since= --after=
1045        --min-age= --until= --before=
1046"
1047# Options that go well for log and gitk (not shortlog)
1048__git_log_gitk_options="
1049        --dense --sparse --full-history
1050        --simplify-merges --simplify-by-decoration
1051        --left-right
1052"
1053# Options that go well for log and shortlog (not gitk)
1054__git_log_shortlog_options="
1055        --author= --committer= --grep=
1056        --all-match
1057"
1058
1059__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1060
1061_git_log ()
1062{
1063        __git_has_doubledash && return
1064
1065        local cur="${COMP_WORDS[COMP_CWORD]}"
1066        local g="$(git rev-parse --git-dir 2>/dev/null)"
1067        local merge=""
1068        if [ -f $g/MERGE_HEAD ]; then
1069                merge="--merge"
1070        fi
1071        case "$cur" in
1072        --pretty=*)
1073                __gitcomp "$__git_log_pretty_formats
1074                        " "" "${cur##--pretty=}"
1075                return
1076                ;;
1077        --format=*)
1078                __gitcomp "$__git_log_pretty_formats
1079                        " "" "${cur##--format=}"
1080                return
1081                ;;
1082        --date=*)
1083                __gitcomp "
1084                        relative iso8601 rfc2822 short local default
1085                " "" "${cur##--date=}"
1086                return
1087                ;;
1088        --*)
1089                __gitcomp "
1090                        $__git_log_common_options
1091                        $__git_log_shortlog_options
1092                        $__git_log_gitk_options
1093                        --root --topo-order --date-order --reverse
1094                        --follow
1095                        --abbrev-commit --abbrev=
1096                        --relative-date --date=
1097                        --pretty= --format= --oneline
1098                        --cherry-pick
1099                        --graph
1100                        --decorate
1101                        --walk-reflogs
1102                        --parents --children
1103                        $merge
1104                        $__git_diff_common_options
1105                        --pickaxe-all --pickaxe-regex
1106                        "
1107                return
1108                ;;
1109        esac
1110        __git_complete_revlist
1111}
1112
1113_git_merge ()
1114{
1115        __git_complete_strategy && return
1116
1117        local cur="${COMP_WORDS[COMP_CWORD]}"
1118        case "$cur" in
1119        --*)
1120                __gitcomp "
1121                        --no-commit --no-stat --log --no-log --squash --strategy
1122                        --commit --stat --no-squash --ff --no-ff
1123                        "
1124                return
1125        esac
1126        __gitcomp "$(__git_refs)"
1127}
1128
1129_git_mergetool ()
1130{
1131        local cur="${COMP_WORDS[COMP_CWORD]}"
1132        case "$cur" in
1133        --tool=*)
1134                __gitcomp "
1135                        kdiff3 tkdiff meld xxdiff emerge
1136                        vimdiff gvimdiff ecmerge opendiff
1137                        " "" "${cur##--tool=}"
1138                return
1139                ;;
1140        --*)
1141                __gitcomp "--tool="
1142                return
1143                ;;
1144        esac
1145        COMPREPLY=()
1146}
1147
1148_git_merge_base ()
1149{
1150        __gitcomp "$(__git_refs)"
1151}
1152
1153_git_mv ()
1154{
1155        local cur="${COMP_WORDS[COMP_CWORD]}"
1156        case "$cur" in
1157        --*)
1158                __gitcomp "--dry-run"
1159                return
1160                ;;
1161        esac
1162        COMPREPLY=()
1163}
1164
1165_git_name_rev ()
1166{
1167        __gitcomp "--tags --all --stdin"
1168}
1169
1170_git_pull ()
1171{
1172        __git_complete_remote_or_refspec
1173}
1174
1175_git_push ()
1176{
1177        __git_complete_remote_or_refspec
1178}
1179
1180_git_rebase ()
1181{
1182        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1183        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1184                __gitcomp "--continue --skip --abort"
1185                return
1186        fi
1187        __git_complete_strategy && return
1188        case "$cur" in
1189        --*)
1190                __gitcomp "--onto --merge --strategy --interactive"
1191                return
1192        esac
1193        __gitcomp "$(__git_refs)"
1194}
1195
1196_git_send_email ()
1197{
1198        local cur="${COMP_WORDS[COMP_CWORD]}"
1199        case "$cur" in
1200        --*)
1201                __gitcomp "--bcc --cc --cc-cmd --chain-reply-to --compose
1202                        --dry-run --envelope-sender --from --identity
1203                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1204                        --no-suppress-from --no-thread --quiet
1205                        --signed-off-by-cc --smtp-pass --smtp-server
1206                        --smtp-server-port --smtp-ssl --smtp-user --subject
1207                        --suppress-cc --suppress-from --thread --to
1208                        --validate --no-validate"
1209                return
1210                ;;
1211        esac
1212        COMPREPLY=()
1213}
1214
1215_git_config ()
1216{
1217        local cur="${COMP_WORDS[COMP_CWORD]}"
1218        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1219        case "$prv" in
1220        branch.*.remote)
1221                __gitcomp "$(__git_remotes)"
1222                return
1223                ;;
1224        branch.*.merge)
1225                __gitcomp "$(__git_refs)"
1226                return
1227                ;;
1228        remote.*.fetch)
1229                local remote="${prv#remote.}"
1230                remote="${remote%.fetch}"
1231                __gitcomp "$(__git_refs_remotes "$remote")"
1232                return
1233                ;;
1234        remote.*.push)
1235                local remote="${prv#remote.}"
1236                remote="${remote%.push}"
1237                __gitcomp "$(git --git-dir="$(__gitdir)" \
1238                        for-each-ref --format='%(refname):%(refname)' \
1239                        refs/heads)"
1240                return
1241                ;;
1242        pull.twohead|pull.octopus)
1243                __gitcomp "$(__git_merge_strategies)"
1244                return
1245                ;;
1246        color.branch|color.diff|color.interactive|color.status|color.ui)
1247                __gitcomp "always never auto"
1248                return
1249                ;;
1250        color.pager)
1251                __gitcomp "false true"
1252                return
1253                ;;
1254        color.*.*)
1255                __gitcomp "
1256                        normal black red green yellow blue magenta cyan white
1257                        bold dim ul blink reverse
1258                        "
1259                return
1260                ;;
1261        *.*)
1262                COMPREPLY=()
1263                return
1264                ;;
1265        esac
1266        case "$cur" in
1267        --*)
1268                __gitcomp "
1269                        --global --system --file=
1270                        --list --replace-all
1271                        --get --get-all --get-regexp
1272                        --add --unset --unset-all
1273                        --remove-section --rename-section
1274                        "
1275                return
1276                ;;
1277        branch.*.*)
1278                local pfx="${cur%.*}."
1279                cur="${cur##*.}"
1280                __gitcomp "remote merge mergeoptions" "$pfx" "$cur"
1281                return
1282                ;;
1283        branch.*)
1284                local pfx="${cur%.*}."
1285                cur="${cur#*.}"
1286                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1287                return
1288                ;;
1289        remote.*.*)
1290                local pfx="${cur%.*}."
1291                cur="${cur##*.}"
1292                __gitcomp "
1293                        url proxy fetch push mirror skipDefaultUpdate
1294                        receivepack uploadpack tagopt
1295                        " "$pfx" "$cur"
1296                return
1297                ;;
1298        remote.*)
1299                local pfx="${cur%.*}."
1300                cur="${cur#*.}"
1301                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1302                return
1303                ;;
1304        esac
1305        __gitcomp "
1306                apply.whitespace
1307                branch.autosetupmerge
1308                branch.autosetuprebase
1309                clean.requireForce
1310                color.branch
1311                color.branch.current
1312                color.branch.local
1313                color.branch.plain
1314                color.branch.remote
1315                color.diff
1316                color.diff.commit
1317                color.diff.frag
1318                color.diff.meta
1319                color.diff.new
1320                color.diff.old
1321                color.diff.plain
1322                color.diff.whitespace
1323                color.interactive
1324                color.interactive.header
1325                color.interactive.help
1326                color.interactive.prompt
1327                color.pager
1328                color.status
1329                color.status.added
1330                color.status.changed
1331                color.status.header
1332                color.status.nobranch
1333                color.status.untracked
1334                color.status.updated
1335                color.ui
1336                commit.template
1337                core.autocrlf
1338                core.bare
1339                core.compression
1340                core.deltaBaseCacheLimit
1341                core.editor
1342                core.excludesfile
1343                core.fileMode
1344                core.fsyncobjectfiles
1345                core.gitProxy
1346                core.ignoreCygwinFSTricks
1347                core.ignoreStat
1348                core.logAllRefUpdates
1349                core.loosecompression
1350                core.packedGitLimit
1351                core.packedGitWindowSize
1352                core.pager
1353                core.preferSymlinkRefs
1354                core.preloadindex
1355                core.quotepath
1356                core.repositoryFormatVersion
1357                core.safecrlf
1358                core.sharedRepository
1359                core.symlinks
1360                core.trustctime
1361                core.warnAmbiguousRefs
1362                core.whitespace
1363                core.worktree
1364                diff.autorefreshindex
1365                diff.external
1366                diff.mnemonicprefix
1367                diff.renameLimit
1368                diff.renameLimit.
1369                diff.renames
1370                fetch.unpackLimit
1371                format.headers
1372                format.numbered
1373                format.pretty
1374                format.suffix
1375                gc.aggressiveWindow
1376                gc.auto
1377                gc.autopacklimit
1378                gc.packrefs
1379                gc.pruneexpire
1380                gc.reflogexpire
1381                gc.reflogexpireunreachable
1382                gc.rerereresolved
1383                gc.rerereunresolved
1384                gitcvs.allbinary
1385                gitcvs.dbTableNamePrefix
1386                gitcvs.dbdriver
1387                gitcvs.dbname
1388                gitcvs.dbpass
1389                gitcvs.dbuser
1390                gitcvs.enabled
1391                gitcvs.logfile
1392                gitcvs.usecrlfattr
1393                gui.blamehistoryctx
1394                gui.commitmsgwidth
1395                gui.copyblamethreshold
1396                gui.diffcontext
1397                gui.encoding
1398                gui.fastcopyblame
1399                gui.matchtrackingbranch
1400                gui.newbranchtemplate
1401                gui.pruneduringfetch
1402                gui.spellingdictionary
1403                gui.trustmtime
1404                help.autocorrect
1405                help.browser
1406                help.format
1407                http.lowSpeedLimit
1408                http.lowSpeedTime
1409                http.maxRequests
1410                http.noEPSV
1411                http.proxy
1412                http.sslCAInfo
1413                http.sslCAPath
1414                http.sslCert
1415                http.sslKey
1416                http.sslVerify
1417                i18n.commitEncoding
1418                i18n.logOutputEncoding
1419                instaweb.browser
1420                instaweb.httpd
1421                instaweb.local
1422                instaweb.modulepath
1423                instaweb.port
1424                log.date
1425                log.showroot
1426                man.viewer
1427                merge.conflictstyle
1428                merge.log
1429                merge.renameLimit
1430                merge.stat
1431                merge.tool
1432                merge.verbosity
1433                mergetool.keepBackup
1434                pack.compression
1435                pack.deltaCacheLimit
1436                pack.deltaCacheSize
1437                pack.depth
1438                pack.indexVersion
1439                pack.packSizeLimit
1440                pack.threads
1441                pack.window
1442                pack.windowMemory
1443                pull.octopus
1444                pull.twohead
1445                receive.denyCurrentBranch
1446                receive.denyDeletes
1447                receive.denyNonFastForwards
1448                receive.fsckObjects
1449                receive.unpackLimit
1450                repack.usedeltabaseoffset
1451                rerere.autoupdate
1452                rerere.enabled
1453                showbranch.default
1454                status.relativePaths
1455                status.showUntrackedFiles
1456                tar.umask
1457                transfer.unpackLimit
1458                user.email
1459                user.name
1460                user.signingkey
1461                web.browser
1462                branch. remote.
1463        "
1464}
1465
1466_git_remote ()
1467{
1468        local subcommands="add rename rm show prune update"
1469        local subcommand="$(__git_find_subcommand "$subcommands")"
1470        if [ -z "$subcommand" ]; then
1471                __gitcomp "$subcommands"
1472                return
1473        fi
1474
1475        case "$subcommand" in
1476        rename|rm|show|prune)
1477                __gitcomp "$(__git_remotes)"
1478                ;;
1479        update)
1480                local i c='' IFS=$'\n'
1481                for i in $(git --git-dir="$(__gitdir)" config --list); do
1482                        case "$i" in
1483                        remotes.*)
1484                                i="${i#remotes.}"
1485                                c="$c ${i/=*/}"
1486                                ;;
1487                        esac
1488                done
1489                __gitcomp "$c"
1490                ;;
1491        *)
1492                COMPREPLY=()
1493                ;;
1494        esac
1495}
1496
1497_git_reset ()
1498{
1499        __git_has_doubledash && return
1500
1501        local cur="${COMP_WORDS[COMP_CWORD]}"
1502        case "$cur" in
1503        --*)
1504                __gitcomp "--merge --mixed --hard --soft"
1505                return
1506                ;;
1507        esac
1508        __gitcomp "$(__git_refs)"
1509}
1510
1511_git_revert ()
1512{
1513        local cur="${COMP_WORDS[COMP_CWORD]}"
1514        case "$cur" in
1515        --*)
1516                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1517                return
1518                ;;
1519        esac
1520        __gitcomp "$(__git_refs)"
1521}
1522
1523_git_rm ()
1524{
1525        __git_has_doubledash && return
1526
1527        local cur="${COMP_WORDS[COMP_CWORD]}"
1528        case "$cur" in
1529        --*)
1530                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1531                return
1532                ;;
1533        esac
1534        COMPREPLY=()
1535}
1536
1537_git_shortlog ()
1538{
1539        __git_has_doubledash && return
1540
1541        local cur="${COMP_WORDS[COMP_CWORD]}"
1542        case "$cur" in
1543        --*)
1544                __gitcomp "
1545                        $__git_log_common_options
1546                        $__git_log_shortlog_options
1547                        --numbered --summary
1548                        "
1549                return
1550                ;;
1551        esac
1552        __git_complete_revlist
1553}
1554
1555_git_show ()
1556{
1557        __git_has_doubledash && return
1558
1559        local cur="${COMP_WORDS[COMP_CWORD]}"
1560        case "$cur" in
1561        --pretty=*)
1562                __gitcomp "$__git_log_pretty_formats
1563                        " "" "${cur##--pretty=}"
1564                return
1565                ;;
1566        --format=*)
1567                __gitcomp "$__git_log_pretty_formats
1568                        " "" "${cur##--format=}"
1569                return
1570                ;;
1571        --*)
1572                __gitcomp "--pretty= --format=
1573                        $__git_diff_common_options
1574                        "
1575                return
1576                ;;
1577        esac
1578        __git_complete_file
1579}
1580
1581_git_show_branch ()
1582{
1583        local cur="${COMP_WORDS[COMP_CWORD]}"
1584        case "$cur" in
1585        --*)
1586                __gitcomp "
1587                        --all --remotes --topo-order --current --more=
1588                        --list --independent --merge-base --no-name
1589                        --sha1-name --topics --reflog
1590                        "
1591                return
1592                ;;
1593        esac
1594        __git_complete_revlist
1595}
1596
1597_git_stash ()
1598{
1599        local subcommands='save list show apply clear drop pop create branch'
1600        local subcommand="$(__git_find_subcommand "$subcommands")"
1601        if [ -z "$subcommand" ]; then
1602                __gitcomp "$subcommands"
1603        else
1604                local cur="${COMP_WORDS[COMP_CWORD]}"
1605                case "$subcommand,$cur" in
1606                save,--*)
1607                        __gitcomp "--keep-index"
1608                        ;;
1609                apply,--*)
1610                        __gitcomp "--index"
1611                        ;;
1612                show,--*|drop,--*|pop,--*|branch,--*)
1613                        COMPREPLY=()
1614                        ;;
1615                show,*|apply,*|drop,*|pop,*|branch,*)
1616                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
1617                                        | sed -n -e 's/:.*//p')"
1618                        ;;
1619                *)
1620                        COMPREPLY=()
1621                        ;;
1622                esac
1623        fi
1624}
1625
1626_git_submodule ()
1627{
1628        __git_has_doubledash && return
1629
1630        local subcommands="add status init update summary foreach sync"
1631        if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1632                local cur="${COMP_WORDS[COMP_CWORD]}"
1633                case "$cur" in
1634                --*)
1635                        __gitcomp "--quiet --cached"
1636                        ;;
1637                *)
1638                        __gitcomp "$subcommands"
1639                        ;;
1640                esac
1641                return
1642        fi
1643}
1644
1645_git_svn ()
1646{
1647        local subcommands="
1648                init fetch clone rebase dcommit log find-rev
1649                set-tree commit-diff info create-ignore propget
1650                proplist show-ignore show-externals branch tag blame
1651                migrate
1652                "
1653        local subcommand="$(__git_find_subcommand "$subcommands")"
1654        if [ -z "$subcommand" ]; then
1655                __gitcomp "$subcommands"
1656        else
1657                local remote_opts="--username= --config-dir= --no-auth-cache"
1658                local fc_opts="
1659                        --follow-parent --authors-file= --repack=
1660                        --no-metadata --use-svm-props --use-svnsync-props
1661                        --log-window-size= --no-checkout --quiet
1662                        --repack-flags --use-log-author --localtime
1663                        --ignore-paths= $remote_opts
1664                        "
1665                local init_opts="
1666                        --template= --shared= --trunk= --tags=
1667                        --branches= --stdlayout --minimize-url
1668                        --no-metadata --use-svm-props --use-svnsync-props
1669                        --rewrite-root= --prefix= --use-log-author
1670                        --add-author-from $remote_opts
1671                        "
1672                local cmt_opts="
1673                        --edit --rmdir --find-copies-harder --copy-similarity=
1674                        "
1675
1676                local cur="${COMP_WORDS[COMP_CWORD]}"
1677                case "$subcommand,$cur" in
1678                fetch,--*)
1679                        __gitcomp "--revision= --fetch-all $fc_opts"
1680                        ;;
1681                clone,--*)
1682                        __gitcomp "--revision= $fc_opts $init_opts"
1683                        ;;
1684                init,--*)
1685                        __gitcomp "$init_opts"
1686                        ;;
1687                dcommit,--*)
1688                        __gitcomp "
1689                                --merge --strategy= --verbose --dry-run
1690                                --fetch-all --no-rebase --commit-url
1691                                --revision $cmt_opts $fc_opts
1692                                "
1693                        ;;
1694                set-tree,--*)
1695                        __gitcomp "--stdin $cmt_opts $fc_opts"
1696                        ;;
1697                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
1698                show-externals,--*)
1699                        __gitcomp "--revision="
1700                        ;;
1701                log,--*)
1702                        __gitcomp "
1703                                --limit= --revision= --verbose --incremental
1704                                --oneline --show-commit --non-recursive
1705                                --authors-file= --color
1706                                "
1707                        ;;
1708                rebase,--*)
1709                        __gitcomp "
1710                                --merge --verbose --strategy= --local
1711                                --fetch-all --dry-run $fc_opts
1712                                "
1713                        ;;
1714                commit-diff,--*)
1715                        __gitcomp "--message= --file= --revision= $cmt_opts"
1716                        ;;
1717                info,--*)
1718                        __gitcomp "--url"
1719                        ;;
1720                branch,--*)
1721                        __gitcomp "--dry-run --message --tag"
1722                        ;;
1723                tag,--*)
1724                        __gitcomp "--dry-run --message"
1725                        ;;
1726                blame,--*)
1727                        __gitcomp "--git-format"
1728                        ;;
1729                migrate,--*)
1730                        __gitcomp "
1731                                --config-dir= --ignore-paths= --minimize
1732                                --no-auth-cache --username=
1733                                "
1734                        ;;
1735                *)
1736                        COMPREPLY=()
1737                        ;;
1738                esac
1739        fi
1740}
1741
1742_git_tag ()
1743{
1744        local i c=1 f=0
1745        while [ $c -lt $COMP_CWORD ]; do
1746                i="${COMP_WORDS[c]}"
1747                case "$i" in
1748                -d|-v)
1749                        __gitcomp "$(__git_tags)"
1750                        return
1751                        ;;
1752                -f)
1753                        f=1
1754                        ;;
1755                esac
1756                c=$((++c))
1757        done
1758
1759        case "${COMP_WORDS[COMP_CWORD-1]}" in
1760        -m|-F)
1761                COMPREPLY=()
1762                ;;
1763        -*|tag)
1764                if [ $f = 1 ]; then
1765                        __gitcomp "$(__git_tags)"
1766                else
1767                        COMPREPLY=()
1768                fi
1769                ;;
1770        *)
1771                __gitcomp "$(__git_refs)"
1772                ;;
1773        esac
1774}
1775
1776_git ()
1777{
1778        local i c=1 command __git_dir
1779
1780        while [ $c -lt $COMP_CWORD ]; do
1781                i="${COMP_WORDS[c]}"
1782                case "$i" in
1783                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1784                --bare)      __git_dir="." ;;
1785                --version|-p|--paginate) ;;
1786                --help) command="help"; break ;;
1787                *) command="$i"; break ;;
1788                esac
1789                c=$((++c))
1790        done
1791
1792        if [ -z "$command" ]; then
1793                case "${COMP_WORDS[COMP_CWORD]}" in
1794                --*)   __gitcomp "
1795                        --paginate
1796                        --no-pager
1797                        --git-dir=
1798                        --bare
1799                        --version
1800                        --exec-path
1801                        --work-tree=
1802                        --help
1803                        "
1804                        ;;
1805                *)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1806                esac
1807                return
1808        fi
1809
1810        local expansion=$(__git_aliased_command "$command")
1811        [ "$expansion" ] && command="$expansion"
1812
1813        case "$command" in
1814        am)          _git_am ;;
1815        add)         _git_add ;;
1816        apply)       _git_apply ;;
1817        archive)     _git_archive ;;
1818        bisect)      _git_bisect ;;
1819        bundle)      _git_bundle ;;
1820        branch)      _git_branch ;;
1821        checkout)    _git_checkout ;;
1822        cherry)      _git_cherry ;;
1823        cherry-pick) _git_cherry_pick ;;
1824        clean)       _git_clean ;;
1825        clone)       _git_clone ;;
1826        commit)      _git_commit ;;
1827        config)      _git_config ;;
1828        describe)    _git_describe ;;
1829        diff)        _git_diff ;;
1830        fetch)       _git_fetch ;;
1831        format-patch) _git_format_patch ;;
1832        gc)          _git_gc ;;
1833        grep)        _git_grep ;;
1834        help)        _git_help ;;
1835        init)        _git_init ;;
1836        log)         _git_log ;;
1837        ls-files)    _git_ls_files ;;
1838        ls-remote)   _git_ls_remote ;;
1839        ls-tree)     _git_ls_tree ;;
1840        merge)       _git_merge;;
1841        mergetool)   _git_mergetool;;
1842        merge-base)  _git_merge_base ;;
1843        mv)          _git_mv ;;
1844        name-rev)    _git_name_rev ;;
1845        pull)        _git_pull ;;
1846        push)        _git_push ;;
1847        rebase)      _git_rebase ;;
1848        remote)      _git_remote ;;
1849        reset)       _git_reset ;;
1850        revert)      _git_revert ;;
1851        rm)          _git_rm ;;
1852        send-email)  _git_send_email ;;
1853        shortlog)    _git_shortlog ;;
1854        show)        _git_show ;;
1855        show-branch) _git_show_branch ;;
1856        stash)       _git_stash ;;
1857        stage)       _git_add ;;
1858        submodule)   _git_submodule ;;
1859        svn)         _git_svn ;;
1860        tag)         _git_tag ;;
1861        whatchanged) _git_log ;;
1862        *)           COMPREPLY=() ;;
1863        esac
1864}
1865
1866_gitk ()
1867{
1868        __git_has_doubledash && return
1869
1870        local cur="${COMP_WORDS[COMP_CWORD]}"
1871        local g="$(__gitdir)"
1872        local merge=""
1873        if [ -f $g/MERGE_HEAD ]; then
1874                merge="--merge"
1875        fi
1876        case "$cur" in
1877        --*)
1878                __gitcomp "
1879                        $__git_log_common_options
1880                        $__git_log_gitk_options
1881                        $merge
1882                        "
1883                return
1884                ;;
1885        esac
1886        __git_complete_revlist
1887}
1888
1889complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
1890        || complete -o default -o nospace -F _git git
1891complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
1892        || complete -o default -o nospace -F _gitk gitk
1893
1894# The following are necessary only for Cygwin, and only are needed
1895# when the user has tab-completed the executable name and consequently
1896# included the '.exe' suffix.
1897#
1898if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1899complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
1900        || complete -o default -o nospace -F _git git.exe
1901fi