e8c4be2e818ca30a87e7c11f10442c4c7862346f
   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_all_commands ()
 448{
 449        if [ -n "${__git_all_commandlist-}" ]; then
 450                echo "$__git_all_commandlist"
 451                return
 452        fi
 453        local i IFS=" "$'\n'
 454        for i in $(git help -a|egrep '^ ')
 455        do
 456                case $i in
 457                *--*)             : helper pattern;;
 458                *) echo $i;;
 459                esac
 460        done
 461}
 462__git_all_commandlist=
 463__git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 464
 465__git_porcelain_commands ()
 466{
 467        if [ -n "${__git_porcelain_commandlist-}" ]; then
 468                echo "$__git_porcelain_commandlist"
 469                return
 470        fi
 471        local i IFS=" "$'\n'
 472        for i in "help" $(__git_all_commands)
 473        do
 474                case $i in
 475                *--*)             : helper pattern;;
 476                applymbox)        : ask gittus;;
 477                applypatch)       : ask gittus;;
 478                archimport)       : import;;
 479                cat-file)         : plumbing;;
 480                check-attr)       : plumbing;;
 481                check-ref-format) : plumbing;;
 482                checkout-index)   : plumbing;;
 483                commit-tree)      : plumbing;;
 484                count-objects)    : infrequent;;
 485                cvsexportcommit)  : export;;
 486                cvsimport)        : import;;
 487                cvsserver)        : daemon;;
 488                daemon)           : daemon;;
 489                diff-files)       : plumbing;;
 490                diff-index)       : plumbing;;
 491                diff-tree)        : plumbing;;
 492                fast-import)      : import;;
 493                fast-export)      : export;;
 494                fsck-objects)     : plumbing;;
 495                fetch-pack)       : plumbing;;
 496                fmt-merge-msg)    : plumbing;;
 497                for-each-ref)     : plumbing;;
 498                hash-object)      : plumbing;;
 499                http-*)           : transport;;
 500                index-pack)       : plumbing;;
 501                init-db)          : deprecated;;
 502                local-fetch)      : plumbing;;
 503                lost-found)       : infrequent;;
 504                ls-files)         : plumbing;;
 505                ls-remote)        : plumbing;;
 506                ls-tree)          : plumbing;;
 507                mailinfo)         : plumbing;;
 508                mailsplit)        : plumbing;;
 509                merge-*)          : plumbing;;
 510                mktree)           : plumbing;;
 511                mktag)            : plumbing;;
 512                pack-objects)     : plumbing;;
 513                pack-redundant)   : plumbing;;
 514                pack-refs)        : plumbing;;
 515                parse-remote)     : plumbing;;
 516                patch-id)         : plumbing;;
 517                peek-remote)      : plumbing;;
 518                prune)            : plumbing;;
 519                prune-packed)     : plumbing;;
 520                quiltimport)      : import;;
 521                read-tree)        : plumbing;;
 522                receive-pack)     : plumbing;;
 523                reflog)           : plumbing;;
 524                repo-config)      : deprecated;;
 525                rerere)           : plumbing;;
 526                rev-list)         : plumbing;;
 527                rev-parse)        : plumbing;;
 528                runstatus)        : plumbing;;
 529                sh-setup)         : internal;;
 530                shell)            : daemon;;
 531                show-ref)         : plumbing;;
 532                send-pack)        : plumbing;;
 533                show-index)       : plumbing;;
 534                ssh-*)            : transport;;
 535                stripspace)       : plumbing;;
 536                symbolic-ref)     : plumbing;;
 537                tar-tree)         : deprecated;;
 538                unpack-file)      : plumbing;;
 539                unpack-objects)   : plumbing;;
 540                update-index)     : plumbing;;
 541                update-ref)       : plumbing;;
 542                update-server-info) : daemon;;
 543                upload-archive)   : plumbing;;
 544                upload-pack)      : plumbing;;
 545                write-tree)       : plumbing;;
 546                var)              : infrequent;;
 547                verify-pack)      : infrequent;;
 548                verify-tag)       : plumbing;;
 549                *) echo $i;;
 550                esac
 551        done
 552}
 553__git_porcelain_commandlist=
 554__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
 555
 556__git_aliases ()
 557{
 558        local i IFS=$'\n'
 559        for i in $(git --git-dir="$(__gitdir)" config --list); do
 560                case "$i" in
 561                alias.*)
 562                        i="${i#alias.}"
 563                        echo "${i/=*/}"
 564                        ;;
 565                esac
 566        done
 567}
 568
 569# __git_aliased_command requires 1 argument
 570__git_aliased_command ()
 571{
 572        local word cmdline=$(git --git-dir="$(__gitdir)" \
 573                config --get "alias.$1")
 574        for word in $cmdline; do
 575                if [ "${word##-*}" ]; then
 576                        echo $word
 577                        return
 578                fi
 579        done
 580}
 581
 582# __git_find_subcommand requires 1 argument
 583__git_find_subcommand ()
 584{
 585        local word subcommand c=1
 586
 587        while [ $c -lt $COMP_CWORD ]; do
 588                word="${COMP_WORDS[c]}"
 589                for subcommand in $1; do
 590                        if [ "$subcommand" = "$word" ]; then
 591                                echo "$subcommand"
 592                                return
 593                        fi
 594                done
 595                c=$((++c))
 596        done
 597}
 598
 599__git_has_doubledash ()
 600{
 601        local c=1
 602        while [ $c -lt $COMP_CWORD ]; do
 603                if [ "--" = "${COMP_WORDS[c]}" ]; then
 604                        return 0
 605                fi
 606                c=$((++c))
 607        done
 608        return 1
 609}
 610
 611__git_whitespacelist="nowarn warn error error-all fix"
 612
 613_git_am ()
 614{
 615        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
 616        if [ -d "$dir"/rebase-apply ]; then
 617                __gitcomp "--skip --resolved --abort"
 618                return
 619        fi
 620        case "$cur" in
 621        --whitespace=*)
 622                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 623                return
 624                ;;
 625        --*)
 626                __gitcomp "
 627                        --signoff --utf8 --binary --3way --interactive
 628                        --whitespace=
 629                        "
 630                return
 631        esac
 632        COMPREPLY=()
 633}
 634
 635_git_apply ()
 636{
 637        local cur="${COMP_WORDS[COMP_CWORD]}"
 638        case "$cur" in
 639        --whitespace=*)
 640                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 641                return
 642                ;;
 643        --*)
 644                __gitcomp "
 645                        --stat --numstat --summary --check --index
 646                        --cached --index-info --reverse --reject --unidiff-zero
 647                        --apply --no-add --exclude=
 648                        --whitespace= --inaccurate-eof --verbose
 649                        "
 650                return
 651        esac
 652        COMPREPLY=()
 653}
 654
 655_git_add ()
 656{
 657        __git_has_doubledash && return
 658
 659        local cur="${COMP_WORDS[COMP_CWORD]}"
 660        case "$cur" in
 661        --*)
 662                __gitcomp "
 663                        --interactive --refresh --patch --update --dry-run
 664                        --ignore-errors --intent-to-add
 665                        "
 666                return
 667        esac
 668        COMPREPLY=()
 669}
 670
 671_git_archive ()
 672{
 673        local cur="${COMP_WORDS[COMP_CWORD]}"
 674        case "$cur" in
 675        --format=*)
 676                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
 677                return
 678                ;;
 679        --remote=*)
 680                __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
 681                return
 682                ;;
 683        --*)
 684                __gitcomp "
 685                        --format= --list --verbose
 686                        --prefix= --remote= --exec=
 687                        "
 688                return
 689                ;;
 690        esac
 691        __git_complete_file
 692}
 693
 694_git_bisect ()
 695{
 696        __git_has_doubledash && return
 697
 698        local subcommands="start bad good skip reset visualize replay log run"
 699        local subcommand="$(__git_find_subcommand "$subcommands")"
 700        if [ -z "$subcommand" ]; then
 701                __gitcomp "$subcommands"
 702                return
 703        fi
 704
 705        case "$subcommand" in
 706        bad|good|reset|skip)
 707                __gitcomp "$(__git_refs)"
 708                ;;
 709        *)
 710                COMPREPLY=()
 711                ;;
 712        esac
 713}
 714
 715_git_branch ()
 716{
 717        local i c=1 only_local_ref="n" has_r="n"
 718
 719        while [ $c -lt $COMP_CWORD ]; do
 720                i="${COMP_WORDS[c]}"
 721                case "$i" in
 722                -d|-m)  only_local_ref="y" ;;
 723                -r)     has_r="y" ;;
 724                esac
 725                c=$((++c))
 726        done
 727
 728        case "${COMP_WORDS[COMP_CWORD]}" in
 729        --*)
 730                __gitcomp "
 731                        --color --no-color --verbose --abbrev= --no-abbrev
 732                        --track --no-track --contains --merged --no-merged
 733                        "
 734                ;;
 735        *)
 736                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 737                        __gitcomp "$(__git_heads)"
 738                else
 739                        __gitcomp "$(__git_refs)"
 740                fi
 741                ;;
 742        esac
 743}
 744
 745_git_bundle ()
 746{
 747        local cmd="${COMP_WORDS[2]}"
 748        case "$COMP_CWORD" in
 749        2)
 750                __gitcomp "create list-heads verify unbundle"
 751                ;;
 752        3)
 753                # looking for a file
 754                ;;
 755        *)
 756                case "$cmd" in
 757                        create)
 758                                __git_complete_revlist
 759                        ;;
 760                esac
 761                ;;
 762        esac
 763}
 764
 765_git_checkout ()
 766{
 767        __git_has_doubledash && return
 768
 769        __gitcomp "$(__git_refs)"
 770}
 771
 772_git_cherry ()
 773{
 774        __gitcomp "$(__git_refs)"
 775}
 776
 777_git_cherry_pick ()
 778{
 779        local cur="${COMP_WORDS[COMP_CWORD]}"
 780        case "$cur" in
 781        --*)
 782                __gitcomp "--edit --no-commit"
 783                ;;
 784        *)
 785                __gitcomp "$(__git_refs)"
 786                ;;
 787        esac
 788}
 789
 790_git_clean ()
 791{
 792        __git_has_doubledash && return
 793
 794        local cur="${COMP_WORDS[COMP_CWORD]}"
 795        case "$cur" in
 796        --*)
 797                __gitcomp "--dry-run --quiet"
 798                return
 799                ;;
 800        esac
 801        COMPREPLY=()
 802}
 803
 804_git_clone ()
 805{
 806        local cur="${COMP_WORDS[COMP_CWORD]}"
 807        case "$cur" in
 808        --*)
 809                __gitcomp "
 810                        --local
 811                        --no-hardlinks
 812                        --shared
 813                        --reference
 814                        --quiet
 815                        --no-checkout
 816                        --bare
 817                        --mirror
 818                        --origin
 819                        --upload-pack
 820                        --template=
 821                        --depth
 822                        "
 823                return
 824                ;;
 825        esac
 826        COMPREPLY=()
 827}
 828
 829_git_commit ()
 830{
 831        __git_has_doubledash && return
 832
 833        local cur="${COMP_WORDS[COMP_CWORD]}"
 834        case "$cur" in
 835        --*)
 836                __gitcomp "
 837                        --all --author= --signoff --verify --no-verify
 838                        --edit --amend --include --only --interactive
 839                        "
 840                return
 841        esac
 842        COMPREPLY=()
 843}
 844
 845_git_describe ()
 846{
 847        local cur="${COMP_WORDS[COMP_CWORD]}"
 848        case "$cur" in
 849        --*)
 850                __gitcomp "
 851                        --all --tags --contains --abbrev= --candidates=
 852                        --exact-match --debug --long --match --always
 853                        "
 854                return
 855        esac
 856        __gitcomp "$(__git_refs)"
 857}
 858
 859__git_diff_common_options="--stat --numstat --shortstat --summary
 860                        --patch-with-stat --name-only --name-status --color
 861                        --no-color --color-words --no-renames --check
 862                        --full-index --binary --abbrev --diff-filter=
 863                        --find-copies-harder
 864                        --text --ignore-space-at-eol --ignore-space-change
 865                        --ignore-all-space --exit-code --quiet --ext-diff
 866                        --no-ext-diff
 867                        --no-prefix --src-prefix= --dst-prefix=
 868                        --inter-hunk-context=
 869                        --patience
 870                        --raw
 871"
 872
 873_git_diff ()
 874{
 875        __git_has_doubledash && return
 876
 877        local cur="${COMP_WORDS[COMP_CWORD]}"
 878        case "$cur" in
 879        --*)
 880                __gitcomp "--cached --pickaxe-all --pickaxe-regex
 881                        --base --ours --theirs
 882                        $__git_diff_common_options
 883                        "
 884                return
 885                ;;
 886        esac
 887        __git_complete_file
 888}
 889
 890_git_fetch ()
 891{
 892        __git_complete_remote_or_refspec
 893}
 894
 895_git_format_patch ()
 896{
 897        local cur="${COMP_WORDS[COMP_CWORD]}"
 898        case "$cur" in
 899        --*)
 900                __gitcomp "
 901                        --stdout --attach --thread
 902                        --output-directory
 903                        --numbered --start-number
 904                        --numbered-files
 905                        --keep-subject
 906                        --signoff
 907                        --in-reply-to=
 908                        --full-index --binary
 909                        --not --all
 910                        --cover-letter
 911                        --no-prefix --src-prefix= --dst-prefix=
 912                        --inline --suffix= --ignore-if-in-upstream
 913                        --subject-prefix=
 914                        "
 915                return
 916                ;;
 917        esac
 918        __git_complete_revlist
 919}
 920
 921_git_gc ()
 922{
 923        local cur="${COMP_WORDS[COMP_CWORD]}"
 924        case "$cur" in
 925        --*)
 926                __gitcomp "--prune --aggressive"
 927                return
 928                ;;
 929        esac
 930        COMPREPLY=()
 931}
 932
 933_git_grep ()
 934{
 935        __git_has_doubledash && return
 936
 937        local cur="${COMP_WORDS[COMP_CWORD]}"
 938        case "$cur" in
 939        --*)
 940                __gitcomp "
 941                        --cached
 942                        --text --ignore-case --word-regexp --invert-match
 943                        --full-name
 944                        --extended-regexp --basic-regexp --fixed-strings
 945                        --files-with-matches --name-only
 946                        --files-without-match
 947                        --count
 948                        --and --or --not --all-match
 949                        "
 950                return
 951                ;;
 952        esac
 953        COMPREPLY=()
 954}
 955
 956_git_help ()
 957{
 958        local cur="${COMP_WORDS[COMP_CWORD]}"
 959        case "$cur" in
 960        --*)
 961                __gitcomp "--all --info --man --web"
 962                return
 963                ;;
 964        esac
 965        __gitcomp "$(__git_all_commands)
 966                attributes cli core-tutorial cvs-migration
 967                diffcore gitk glossary hooks ignore modules
 968                repository-layout tutorial tutorial-2
 969                workflows
 970                "
 971}
 972
 973_git_init ()
 974{
 975        local cur="${COMP_WORDS[COMP_CWORD]}"
 976        case "$cur" in
 977        --shared=*)
 978                __gitcomp "
 979                        false true umask group all world everybody
 980                        " "" "${cur##--shared=}"
 981                return
 982                ;;
 983        --*)
 984                __gitcomp "--quiet --bare --template= --shared --shared="
 985                return
 986                ;;
 987        esac
 988        COMPREPLY=()
 989}
 990
 991_git_ls_files ()
 992{
 993        __git_has_doubledash && return
 994
 995        local cur="${COMP_WORDS[COMP_CWORD]}"
 996        case "$cur" in
 997        --*)
 998                __gitcomp "--cached --deleted --modified --others --ignored
 999                        --stage --directory --no-empty-directory --unmerged
1000                        --killed --exclude= --exclude-from=
1001                        --exclude-per-directory= --exclude-standard
1002                        --error-unmatch --with-tree= --full-name
1003                        --abbrev --ignored --exclude-per-directory
1004                        "
1005                return
1006                ;;
1007        esac
1008        COMPREPLY=()
1009}
1010
1011_git_ls_remote ()
1012{
1013        __gitcomp "$(__git_remotes)"
1014}
1015
1016_git_ls_tree ()
1017{
1018        __git_complete_file
1019}
1020
1021# Options that go well for log, shortlog and gitk
1022__git_log_common_options="
1023        --not --all
1024        --branches --tags --remotes
1025        --first-parent --no-merges
1026        --max-count=
1027        --max-age= --since= --after=
1028        --min-age= --until= --before=
1029"
1030# Options that go well for log and gitk (not shortlog)
1031__git_log_gitk_options="
1032        --dense --sparse --full-history
1033        --simplify-merges --simplify-by-decoration
1034        --left-right
1035"
1036# Options that go well for log and shortlog (not gitk)
1037__git_log_shortlog_options="
1038        --author= --committer= --grep=
1039        --all-match
1040"
1041
1042__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1043
1044_git_log ()
1045{
1046        __git_has_doubledash && return
1047
1048        local cur="${COMP_WORDS[COMP_CWORD]}"
1049        local g="$(git rev-parse --git-dir 2>/dev/null)"
1050        local merge=""
1051        if [ -f $g/MERGE_HEAD ]; then
1052                merge="--merge"
1053        fi
1054        case "$cur" in
1055        --pretty=*)
1056                __gitcomp "$__git_log_pretty_formats
1057                        " "" "${cur##--pretty=}"
1058                return
1059                ;;
1060        --format=*)
1061                __gitcomp "$__git_log_pretty_formats
1062                        " "" "${cur##--format=}"
1063                return
1064                ;;
1065        --date=*)
1066                __gitcomp "
1067                        relative iso8601 rfc2822 short local default
1068                " "" "${cur##--date=}"
1069                return
1070                ;;
1071        --*)
1072                __gitcomp "
1073                        $__git_log_common_options
1074                        $__git_log_shortlog_options
1075                        $__git_log_gitk_options
1076                        --root --topo-order --date-order --reverse
1077                        --follow
1078                        --abbrev-commit --abbrev=
1079                        --relative-date --date=
1080                        --pretty= --format= --oneline
1081                        --cherry-pick
1082                        --graph
1083                        --decorate
1084                        --walk-reflogs
1085                        --parents --children
1086                        $merge
1087                        $__git_diff_common_options
1088                        --pickaxe-all --pickaxe-regex
1089                        "
1090                return
1091                ;;
1092        esac
1093        __git_complete_revlist
1094}
1095
1096_git_merge ()
1097{
1098        local cur="${COMP_WORDS[COMP_CWORD]}"
1099        case "${COMP_WORDS[COMP_CWORD-1]}" in
1100        -s|--strategy)
1101                __gitcomp "$(__git_merge_strategies)"
1102                return
1103        esac
1104        case "$cur" in
1105        --strategy=*)
1106                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1107                return
1108                ;;
1109        --*)
1110                __gitcomp "
1111                        --no-commit --no-stat --log --no-log --squash --strategy
1112                        --commit --stat --no-squash --ff --no-ff
1113                        "
1114                return
1115        esac
1116        __gitcomp "$(__git_refs)"
1117}
1118
1119_git_mergetool ()
1120{
1121        local cur="${COMP_WORDS[COMP_CWORD]}"
1122        case "$cur" in
1123        --tool=*)
1124                __gitcomp "
1125                        kdiff3 tkdiff meld xxdiff emerge
1126                        vimdiff gvimdiff ecmerge opendiff
1127                        " "" "${cur##--tool=}"
1128                return
1129                ;;
1130        --*)
1131                __gitcomp "--tool="
1132                return
1133                ;;
1134        esac
1135        COMPREPLY=()
1136}
1137
1138_git_merge_base ()
1139{
1140        __gitcomp "$(__git_refs)"
1141}
1142
1143_git_mv ()
1144{
1145        local cur="${COMP_WORDS[COMP_CWORD]}"
1146        case "$cur" in
1147        --*)
1148                __gitcomp "--dry-run"
1149                return
1150                ;;
1151        esac
1152        COMPREPLY=()
1153}
1154
1155_git_name_rev ()
1156{
1157        __gitcomp "--tags --all --stdin"
1158}
1159
1160_git_pull ()
1161{
1162        __git_complete_remote_or_refspec
1163}
1164
1165_git_push ()
1166{
1167        __git_complete_remote_or_refspec
1168}
1169
1170_git_rebase ()
1171{
1172        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1173        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1174                __gitcomp "--continue --skip --abort"
1175                return
1176        fi
1177        case "${COMP_WORDS[COMP_CWORD-1]}" in
1178        -s|--strategy)
1179                __gitcomp "$(__git_merge_strategies)"
1180                return
1181        esac
1182        case "$cur" in
1183        --strategy=*)
1184                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1185                return
1186                ;;
1187        --*)
1188                __gitcomp "--onto --merge --strategy --interactive"
1189                return
1190        esac
1191        __gitcomp "$(__git_refs)"
1192}
1193
1194_git_send_email ()
1195{
1196        local cur="${COMP_WORDS[COMP_CWORD]}"
1197        case "$cur" in
1198        --*)
1199                __gitcomp "--bcc --cc --cc-cmd --chain-reply-to --compose
1200                        --dry-run --envelope-sender --from --identity
1201                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1202                        --no-suppress-from --no-thread --quiet
1203                        --signed-off-by-cc --smtp-pass --smtp-server
1204                        --smtp-server-port --smtp-ssl --smtp-user --subject
1205                        --suppress-cc --suppress-from --thread --to
1206                        --validate --no-validate"
1207                return
1208                ;;
1209        esac
1210        COMPREPLY=()
1211}
1212
1213_git_config ()
1214{
1215        local cur="${COMP_WORDS[COMP_CWORD]}"
1216        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1217        case "$prv" in
1218        branch.*.remote)
1219                __gitcomp "$(__git_remotes)"
1220                return
1221                ;;
1222        branch.*.merge)
1223                __gitcomp "$(__git_refs)"
1224                return
1225                ;;
1226        remote.*.fetch)
1227                local remote="${prv#remote.}"
1228                remote="${remote%.fetch}"
1229                __gitcomp "$(__git_refs_remotes "$remote")"
1230                return
1231                ;;
1232        remote.*.push)
1233                local remote="${prv#remote.}"
1234                remote="${remote%.push}"
1235                __gitcomp "$(git --git-dir="$(__gitdir)" \
1236                        for-each-ref --format='%(refname):%(refname)' \
1237                        refs/heads)"
1238                return
1239                ;;
1240        pull.twohead|pull.octopus)
1241                __gitcomp "$(__git_merge_strategies)"
1242                return
1243                ;;
1244        color.branch|color.diff|color.interactive|color.status|color.ui)
1245                __gitcomp "always never auto"
1246                return
1247                ;;
1248        color.pager)
1249                __gitcomp "false true"
1250                return
1251                ;;
1252        color.*.*)
1253                __gitcomp "
1254                        normal black red green yellow blue magenta cyan white
1255                        bold dim ul blink reverse
1256                        "
1257                return
1258                ;;
1259        *.*)
1260                COMPREPLY=()
1261                return
1262                ;;
1263        esac
1264        case "$cur" in
1265        --*)
1266                __gitcomp "
1267                        --global --system --file=
1268                        --list --replace-all
1269                        --get --get-all --get-regexp
1270                        --add --unset --unset-all
1271                        --remove-section --rename-section
1272                        "
1273                return
1274                ;;
1275        branch.*.*)
1276                local pfx="${cur%.*}."
1277                cur="${cur##*.}"
1278                __gitcomp "remote merge mergeoptions" "$pfx" "$cur"
1279                return
1280                ;;
1281        branch.*)
1282                local pfx="${cur%.*}."
1283                cur="${cur#*.}"
1284                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1285                return
1286                ;;
1287        remote.*.*)
1288                local pfx="${cur%.*}."
1289                cur="${cur##*.}"
1290                __gitcomp "
1291                        url proxy fetch push mirror skipDefaultUpdate
1292                        receivepack uploadpack tagopt
1293                        " "$pfx" "$cur"
1294                return
1295                ;;
1296        remote.*)
1297                local pfx="${cur%.*}."
1298                cur="${cur#*.}"
1299                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1300                return
1301                ;;
1302        esac
1303        __gitcomp "
1304                apply.whitespace
1305                branch.autosetupmerge
1306                branch.autosetuprebase
1307                clean.requireForce
1308                color.branch
1309                color.branch.current
1310                color.branch.local
1311                color.branch.plain
1312                color.branch.remote
1313                color.diff
1314                color.diff.commit
1315                color.diff.frag
1316                color.diff.meta
1317                color.diff.new
1318                color.diff.old
1319                color.diff.plain
1320                color.diff.whitespace
1321                color.interactive
1322                color.interactive.header
1323                color.interactive.help
1324                color.interactive.prompt
1325                color.pager
1326                color.status
1327                color.status.added
1328                color.status.changed
1329                color.status.header
1330                color.status.nobranch
1331                color.status.untracked
1332                color.status.updated
1333                color.ui
1334                commit.template
1335                core.autocrlf
1336                core.bare
1337                core.compression
1338                core.deltaBaseCacheLimit
1339                core.editor
1340                core.excludesfile
1341                core.fileMode
1342                core.fsyncobjectfiles
1343                core.gitProxy
1344                core.ignoreCygwinFSTricks
1345                core.ignoreStat
1346                core.logAllRefUpdates
1347                core.loosecompression
1348                core.packedGitLimit
1349                core.packedGitWindowSize
1350                core.pager
1351                core.preferSymlinkRefs
1352                core.preloadindex
1353                core.quotepath
1354                core.repositoryFormatVersion
1355                core.safecrlf
1356                core.sharedRepository
1357                core.symlinks
1358                core.trustctime
1359                core.warnAmbiguousRefs
1360                core.whitespace
1361                core.worktree
1362                diff.autorefreshindex
1363                diff.external
1364                diff.mnemonicprefix
1365                diff.renameLimit
1366                diff.renameLimit.
1367                diff.renames
1368                fetch.unpackLimit
1369                format.headers
1370                format.numbered
1371                format.pretty
1372                format.suffix
1373                gc.aggressiveWindow
1374                gc.auto
1375                gc.autopacklimit
1376                gc.packrefs
1377                gc.pruneexpire
1378                gc.reflogexpire
1379                gc.reflogexpireunreachable
1380                gc.rerereresolved
1381                gc.rerereunresolved
1382                gitcvs.allbinary
1383                gitcvs.dbTableNamePrefix
1384                gitcvs.dbdriver
1385                gitcvs.dbname
1386                gitcvs.dbpass
1387                gitcvs.dbuser
1388                gitcvs.enabled
1389                gitcvs.logfile
1390                gitcvs.usecrlfattr
1391                gui.blamehistoryctx
1392                gui.commitmsgwidth
1393                gui.copyblamethreshold
1394                gui.diffcontext
1395                gui.encoding
1396                gui.fastcopyblame
1397                gui.matchtrackingbranch
1398                gui.newbranchtemplate
1399                gui.pruneduringfetch
1400                gui.spellingdictionary
1401                gui.trustmtime
1402                help.autocorrect
1403                help.browser
1404                help.format
1405                http.lowSpeedLimit
1406                http.lowSpeedTime
1407                http.maxRequests
1408                http.noEPSV
1409                http.proxy
1410                http.sslCAInfo
1411                http.sslCAPath
1412                http.sslCert
1413                http.sslKey
1414                http.sslVerify
1415                i18n.commitEncoding
1416                i18n.logOutputEncoding
1417                instaweb.browser
1418                instaweb.httpd
1419                instaweb.local
1420                instaweb.modulepath
1421                instaweb.port
1422                log.date
1423                log.showroot
1424                man.viewer
1425                merge.conflictstyle
1426                merge.log
1427                merge.renameLimit
1428                merge.stat
1429                merge.tool
1430                merge.verbosity
1431                mergetool.keepBackup
1432                pack.compression
1433                pack.deltaCacheLimit
1434                pack.deltaCacheSize
1435                pack.depth
1436                pack.indexVersion
1437                pack.packSizeLimit
1438                pack.threads
1439                pack.window
1440                pack.windowMemory
1441                pull.octopus
1442                pull.twohead
1443                receive.denyCurrentBranch
1444                receive.denyDeletes
1445                receive.denyNonFastForwards
1446                receive.fsckObjects
1447                receive.unpackLimit
1448                repack.usedeltabaseoffset
1449                rerere.autoupdate
1450                rerere.enabled
1451                showbranch.default
1452                status.relativePaths
1453                status.showUntrackedFiles
1454                tar.umask
1455                transfer.unpackLimit
1456                user.email
1457                user.name
1458                user.signingkey
1459                web.browser
1460                branch. remote.
1461        "
1462}
1463
1464_git_remote ()
1465{
1466        local subcommands="add rename rm show prune update"
1467        local subcommand="$(__git_find_subcommand "$subcommands")"
1468        if [ -z "$subcommand" ]; then
1469                __gitcomp "$subcommands"
1470                return
1471        fi
1472
1473        case "$subcommand" in
1474        rename|rm|show|prune)
1475                __gitcomp "$(__git_remotes)"
1476                ;;
1477        update)
1478                local i c='' IFS=$'\n'
1479                for i in $(git --git-dir="$(__gitdir)" config --list); do
1480                        case "$i" in
1481                        remotes.*)
1482                                i="${i#remotes.}"
1483                                c="$c ${i/=*/}"
1484                                ;;
1485                        esac
1486                done
1487                __gitcomp "$c"
1488                ;;
1489        *)
1490                COMPREPLY=()
1491                ;;
1492        esac
1493}
1494
1495_git_reset ()
1496{
1497        __git_has_doubledash && return
1498
1499        local cur="${COMP_WORDS[COMP_CWORD]}"
1500        case "$cur" in
1501        --*)
1502                __gitcomp "--merge --mixed --hard --soft"
1503                return
1504                ;;
1505        esac
1506        __gitcomp "$(__git_refs)"
1507}
1508
1509_git_revert ()
1510{
1511        local cur="${COMP_WORDS[COMP_CWORD]}"
1512        case "$cur" in
1513        --*)
1514                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1515                return
1516                ;;
1517        esac
1518        __gitcomp "$(__git_refs)"
1519}
1520
1521_git_rm ()
1522{
1523        __git_has_doubledash && return
1524
1525        local cur="${COMP_WORDS[COMP_CWORD]}"
1526        case "$cur" in
1527        --*)
1528                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1529                return
1530                ;;
1531        esac
1532        COMPREPLY=()
1533}
1534
1535_git_shortlog ()
1536{
1537        __git_has_doubledash && return
1538
1539        local cur="${COMP_WORDS[COMP_CWORD]}"
1540        case "$cur" in
1541        --*)
1542                __gitcomp "
1543                        $__git_log_common_options
1544                        $__git_log_shortlog_options
1545                        --numbered --summary
1546                        "
1547                return
1548                ;;
1549        esac
1550        __git_complete_revlist
1551}
1552
1553_git_show ()
1554{
1555        __git_has_doubledash && return
1556
1557        local cur="${COMP_WORDS[COMP_CWORD]}"
1558        case "$cur" in
1559        --pretty=*)
1560                __gitcomp "$__git_log_pretty_formats
1561                        " "" "${cur##--pretty=}"
1562                return
1563                ;;
1564        --format=*)
1565                __gitcomp "$__git_log_pretty_formats
1566                        " "" "${cur##--format=}"
1567                return
1568                ;;
1569        --*)
1570                __gitcomp "--pretty= --format=
1571                        $__git_diff_common_options
1572                        "
1573                return
1574                ;;
1575        esac
1576        __git_complete_file
1577}
1578
1579_git_show_branch ()
1580{
1581        local cur="${COMP_WORDS[COMP_CWORD]}"
1582        case "$cur" in
1583        --*)
1584                __gitcomp "
1585                        --all --remotes --topo-order --current --more=
1586                        --list --independent --merge-base --no-name
1587                        --sha1-name --topics --reflog
1588                        "
1589                return
1590                ;;
1591        esac
1592        __git_complete_revlist
1593}
1594
1595_git_stash ()
1596{
1597        local subcommands='save list show apply clear drop pop create branch'
1598        local subcommand="$(__git_find_subcommand "$subcommands")"
1599        if [ -z "$subcommand" ]; then
1600                __gitcomp "$subcommands"
1601        else
1602                local cur="${COMP_WORDS[COMP_CWORD]}"
1603                case "$subcommand,$cur" in
1604                save,--*)
1605                        __gitcomp "--keep-index"
1606                        ;;
1607                apply,--*)
1608                        __gitcomp "--index"
1609                        ;;
1610                show,--*|drop,--*|pop,--*|branch,--*)
1611                        COMPREPLY=()
1612                        ;;
1613                show,*|apply,*|drop,*|pop,*|branch,*)
1614                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
1615                                        | sed -n -e 's/:.*//p')"
1616                        ;;
1617                *)
1618                        COMPREPLY=()
1619                        ;;
1620                esac
1621        fi
1622}
1623
1624_git_submodule ()
1625{
1626        __git_has_doubledash && return
1627
1628        local subcommands="add status init update summary foreach sync"
1629        if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1630                local cur="${COMP_WORDS[COMP_CWORD]}"
1631                case "$cur" in
1632                --*)
1633                        __gitcomp "--quiet --cached"
1634                        ;;
1635                *)
1636                        __gitcomp "$subcommands"
1637                        ;;
1638                esac
1639                return
1640        fi
1641}
1642
1643_git_svn ()
1644{
1645        local subcommands="
1646                init fetch clone rebase dcommit log find-rev
1647                set-tree commit-diff info create-ignore propget
1648                proplist show-ignore show-externals branch tag blame
1649                migrate
1650                "
1651        local subcommand="$(__git_find_subcommand "$subcommands")"
1652        if [ -z "$subcommand" ]; then
1653                __gitcomp "$subcommands"
1654        else
1655                local remote_opts="--username= --config-dir= --no-auth-cache"
1656                local fc_opts="
1657                        --follow-parent --authors-file= --repack=
1658                        --no-metadata --use-svm-props --use-svnsync-props
1659                        --log-window-size= --no-checkout --quiet
1660                        --repack-flags --use-log-author --localtime
1661                        --ignore-paths= $remote_opts
1662                        "
1663                local init_opts="
1664                        --template= --shared= --trunk= --tags=
1665                        --branches= --stdlayout --minimize-url
1666                        --no-metadata --use-svm-props --use-svnsync-props
1667                        --rewrite-root= --prefix= --use-log-author
1668                        --add-author-from $remote_opts
1669                        "
1670                local cmt_opts="
1671                        --edit --rmdir --find-copies-harder --copy-similarity=
1672                        "
1673
1674                local cur="${COMP_WORDS[COMP_CWORD]}"
1675                case "$subcommand,$cur" in
1676                fetch,--*)
1677                        __gitcomp "--revision= --fetch-all $fc_opts"
1678                        ;;
1679                clone,--*)
1680                        __gitcomp "--revision= $fc_opts $init_opts"
1681                        ;;
1682                init,--*)
1683                        __gitcomp "$init_opts"
1684                        ;;
1685                dcommit,--*)
1686                        __gitcomp "
1687                                --merge --strategy= --verbose --dry-run
1688                                --fetch-all --no-rebase --commit-url
1689                                --revision $cmt_opts $fc_opts
1690                                "
1691                        ;;
1692                set-tree,--*)
1693                        __gitcomp "--stdin $cmt_opts $fc_opts"
1694                        ;;
1695                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
1696                show-externals,--*)
1697                        __gitcomp "--revision="
1698                        ;;
1699                log,--*)
1700                        __gitcomp "
1701                                --limit= --revision= --verbose --incremental
1702                                --oneline --show-commit --non-recursive
1703                                --authors-file= --color
1704                                "
1705                        ;;
1706                rebase,--*)
1707                        __gitcomp "
1708                                --merge --verbose --strategy= --local
1709                                --fetch-all --dry-run $fc_opts
1710                                "
1711                        ;;
1712                commit-diff,--*)
1713                        __gitcomp "--message= --file= --revision= $cmt_opts"
1714                        ;;
1715                info,--*)
1716                        __gitcomp "--url"
1717                        ;;
1718                branch,--*)
1719                        __gitcomp "--dry-run --message --tag"
1720                        ;;
1721                tag,--*)
1722                        __gitcomp "--dry-run --message"
1723                        ;;
1724                blame,--*)
1725                        __gitcomp "--git-format"
1726                        ;;
1727                migrate,--*)
1728                        __gitcomp "
1729                                --config-dir= --ignore-paths= --minimize
1730                                --no-auth-cache --username=
1731                                "
1732                        ;;
1733                *)
1734                        COMPREPLY=()
1735                        ;;
1736                esac
1737        fi
1738}
1739
1740_git_tag ()
1741{
1742        local i c=1 f=0
1743        while [ $c -lt $COMP_CWORD ]; do
1744                i="${COMP_WORDS[c]}"
1745                case "$i" in
1746                -d|-v)
1747                        __gitcomp "$(__git_tags)"
1748                        return
1749                        ;;
1750                -f)
1751                        f=1
1752                        ;;
1753                esac
1754                c=$((++c))
1755        done
1756
1757        case "${COMP_WORDS[COMP_CWORD-1]}" in
1758        -m|-F)
1759                COMPREPLY=()
1760                ;;
1761        -*|tag)
1762                if [ $f = 1 ]; then
1763                        __gitcomp "$(__git_tags)"
1764                else
1765                        COMPREPLY=()
1766                fi
1767                ;;
1768        *)
1769                __gitcomp "$(__git_refs)"
1770                ;;
1771        esac
1772}
1773
1774_git ()
1775{
1776        local i c=1 command __git_dir
1777
1778        while [ $c -lt $COMP_CWORD ]; do
1779                i="${COMP_WORDS[c]}"
1780                case "$i" in
1781                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1782                --bare)      __git_dir="." ;;
1783                --version|-p|--paginate) ;;
1784                --help) command="help"; break ;;
1785                *) command="$i"; break ;;
1786                esac
1787                c=$((++c))
1788        done
1789
1790        if [ -z "$command" ]; then
1791                case "${COMP_WORDS[COMP_CWORD]}" in
1792                --*)   __gitcomp "
1793                        --paginate
1794                        --no-pager
1795                        --git-dir=
1796                        --bare
1797                        --version
1798                        --exec-path
1799                        --work-tree=
1800                        --help
1801                        "
1802                        ;;
1803                *)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1804                esac
1805                return
1806        fi
1807
1808        local expansion=$(__git_aliased_command "$command")
1809        [ "$expansion" ] && command="$expansion"
1810
1811        case "$command" in
1812        am)          _git_am ;;
1813        add)         _git_add ;;
1814        apply)       _git_apply ;;
1815        archive)     _git_archive ;;
1816        bisect)      _git_bisect ;;
1817        bundle)      _git_bundle ;;
1818        branch)      _git_branch ;;
1819        checkout)    _git_checkout ;;
1820        cherry)      _git_cherry ;;
1821        cherry-pick) _git_cherry_pick ;;
1822        clean)       _git_clean ;;
1823        clone)       _git_clone ;;
1824        commit)      _git_commit ;;
1825        config)      _git_config ;;
1826        describe)    _git_describe ;;
1827        diff)        _git_diff ;;
1828        fetch)       _git_fetch ;;
1829        format-patch) _git_format_patch ;;
1830        gc)          _git_gc ;;
1831        grep)        _git_grep ;;
1832        help)        _git_help ;;
1833        init)        _git_init ;;
1834        log)         _git_log ;;
1835        ls-files)    _git_ls_files ;;
1836        ls-remote)   _git_ls_remote ;;
1837        ls-tree)     _git_ls_tree ;;
1838        merge)       _git_merge;;
1839        mergetool)   _git_mergetool;;
1840        merge-base)  _git_merge_base ;;
1841        mv)          _git_mv ;;
1842        name-rev)    _git_name_rev ;;
1843        pull)        _git_pull ;;
1844        push)        _git_push ;;
1845        rebase)      _git_rebase ;;
1846        remote)      _git_remote ;;
1847        reset)       _git_reset ;;
1848        revert)      _git_revert ;;
1849        rm)          _git_rm ;;
1850        send-email)  _git_send_email ;;
1851        shortlog)    _git_shortlog ;;
1852        show)        _git_show ;;
1853        show-branch) _git_show_branch ;;
1854        stash)       _git_stash ;;
1855        stage)       _git_add ;;
1856        submodule)   _git_submodule ;;
1857        svn)         _git_svn ;;
1858        tag)         _git_tag ;;
1859        whatchanged) _git_log ;;
1860        *)           COMPREPLY=() ;;
1861        esac
1862}
1863
1864_gitk ()
1865{
1866        __git_has_doubledash && return
1867
1868        local cur="${COMP_WORDS[COMP_CWORD]}"
1869        local g="$(__gitdir)"
1870        local merge=""
1871        if [ -f $g/MERGE_HEAD ]; then
1872                merge="--merge"
1873        fi
1874        case "$cur" in
1875        --*)
1876                __gitcomp "
1877                        $__git_log_common_options
1878                        $__git_log_gitk_options
1879                        $merge
1880                        "
1881                return
1882                ;;
1883        esac
1884        __git_complete_revlist
1885}
1886
1887complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
1888        || complete -o default -o nospace -F _git git
1889complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
1890        || complete -o default -o nospace -F _gitk gitk
1891
1892# The following are necessary only for Cygwin, and only are needed
1893# when the user has tab-completed the executable name and consequently
1894# included the '.exe' suffix.
1895#
1896if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1897complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
1898        || complete -o default -o nospace -F _git git.exe
1899fi