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