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