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