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