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