contrib / completion / git-completion.bashon commit Merge branch 'ae/pack-autothread' (3c972e1)
   1#
   2# bash completion support for core Git.
   3#
   4# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
   5# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
   6# Distributed under the GNU General Public License, version 2.0.
   7#
   8# The contained completion routines provide support for completing:
   9#
  10#    *) local and remote branch names
  11#    *) local and remote tag names
  12#    *) .git/remotes file names
  13#    *) git 'subcommands'
  14#    *) tree paths within 'ref:path/to/file' expressions
  15#    *) common --long-options
  16#
  17# To use these routines:
  18#
  19#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
  20#    2) Added the following line to your .bashrc:
  21#        source ~/.git-completion.sh
  22#
  23#    3) You may want to make sure the git executable is available
  24#       in your PATH before this script is sourced, as some caching
  25#       is performed while the script loads.  If git isn't found
  26#       at source time then all lookups will be done on demand,
  27#       which may be slightly slower.
  28#
  29#    4) Consider changing your PS1 to also show the current branch:
  30#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  31#
  32#       The argument to __git_ps1 will be displayed only if you
  33#       are currently in a git repository.  The %s token will be
  34#       the name of the current branch.
  35#
  36# To submit patches:
  37#
  38#    *) Read Documentation/SubmittingPatches
  39#    *) Send all patches to the current maintainer:
  40#
  41#       "Shawn O. Pearce" <spearce@spearce.org>
  42#
  43#    *) Always CC the Git mailing list:
  44#
  45#       git@vger.kernel.org
  46#
  47
  48__gitdir ()
  49{
  50        if [ -z "$1" ]; then
  51                if [ -n "$__git_dir" ]; then
  52                        echo "$__git_dir"
  53                elif [ -d .git ]; then
  54                        echo .git
  55                else
  56                        git rev-parse --git-dir 2>/dev/null
  57                fi
  58        elif [ -d "$1/.git" ]; then
  59                echo "$1/.git"
  60        else
  61                echo "$1"
  62        fi
  63}
  64
  65__git_ps1 ()
  66{
  67        local g="$(git rev-parse --git-dir 2>/dev/null)"
  68        if [ -n "$g" ]; then
  69                local r
  70                local b
  71                if [ -d "$g/../.dotest" ]
  72                then
  73                        r="|AM/REBASE"
  74                        b="$(git symbolic-ref HEAD 2>/dev/null)"
  75                elif [ -f "$g/.dotest-merge/interactive" ]
  76                then
  77                        r="|REBASE-i"
  78                        b="$(cat $g/.dotest-merge/head-name)"
  79                elif [ -d "$g/.dotest-merge" ]
  80                then
  81                        r="|REBASE-m"
  82                        b="$(cat $g/.dotest-merge/head-name)"
  83                elif [ -f "$g/MERGE_HEAD" ]
  84                then
  85                        r="|MERGING"
  86                        b="$(git symbolic-ref HEAD 2>/dev/null)"
  87                else
  88                        if [ -f $g/BISECT_LOG ]
  89                        then
  90                                r="|BISECTING"
  91                        fi
  92                        if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
  93                        then
  94                                if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
  95                                then
  96                                        b="$(cut -c1-7 $g/HEAD)..."
  97                                fi
  98                        fi
  99                fi
 100
 101                if [ -n "$1" ]; then
 102                        printf "$1" "${b##refs/heads/}$r"
 103                else
 104                        printf " (%s)" "${b##refs/heads/}$r"
 105                fi
 106        fi
 107}
 108
 109__gitcomp ()
 110{
 111        local all c s=$'\n' IFS=' '$'\t'$'\n'
 112        local cur="${COMP_WORDS[COMP_CWORD]}"
 113        if [ $# -gt 2 ]; then
 114                cur="$3"
 115        fi
 116        for c in $1; do
 117                case "$c$4" in
 118                --*=*) all="$all$c$4$s" ;;
 119                *.)    all="$all$c$4$s" ;;
 120                *)     all="$all$c$4 $s" ;;
 121                esac
 122        done
 123        IFS=$s
 124        COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
 125        return
 126}
 127
 128__git_heads ()
 129{
 130        local cmd i is_hash=y dir="$(__gitdir "$1")"
 131        if [ -d "$dir" ]; then
 132                for i in $(git --git-dir="$dir" \
 133                        for-each-ref --format='%(refname)' \
 134                        refs/heads ); do
 135                        echo "${i#refs/heads/}"
 136                done
 137                return
 138        fi
 139        for i in $(git-ls-remote "$1" 2>/dev/null); do
 140                case "$is_hash,$i" in
 141                y,*) is_hash=n ;;
 142                n,*^{}) is_hash=y ;;
 143                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 144                n,*) is_hash=y; echo "$i" ;;
 145                esac
 146        done
 147}
 148
 149__git_tags ()
 150{
 151        local cmd i is_hash=y dir="$(__gitdir "$1")"
 152        if [ -d "$dir" ]; then
 153                for i in $(git --git-dir="$dir" \
 154                        for-each-ref --format='%(refname)' \
 155                        refs/tags ); do
 156                        echo "${i#refs/tags/}"
 157                done
 158                return
 159        fi
 160        for i in $(git-ls-remote "$1" 2>/dev/null); do
 161                case "$is_hash,$i" in
 162                y,*) is_hash=n ;;
 163                n,*^{}) is_hash=y ;;
 164                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 165                n,*) is_hash=y; echo "$i" ;;
 166                esac
 167        done
 168}
 169
 170__git_refs ()
 171{
 172        local cmd i is_hash=y dir="$(__gitdir "$1")"
 173        if [ -d "$dir" ]; then
 174                if [ -e "$dir/HEAD" ]; then echo HEAD; fi
 175                for i in $(git --git-dir="$dir" \
 176                        for-each-ref --format='%(refname)' \
 177                        refs/tags refs/heads refs/remotes); do
 178                        case "$i" in
 179                                refs/tags/*)    echo "${i#refs/tags/}" ;;
 180                                refs/heads/*)   echo "${i#refs/heads/}" ;;
 181                                refs/remotes/*) echo "${i#refs/remotes/}" ;;
 182                                *)              echo "$i" ;;
 183                        esac
 184                done
 185                return
 186        fi
 187        for i in $(git-ls-remote "$dir" 2>/dev/null); do
 188                case "$is_hash,$i" in
 189                y,*) is_hash=n ;;
 190                n,*^{}) is_hash=y ;;
 191                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 192                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 193                n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
 194                n,*) is_hash=y; echo "$i" ;;
 195                esac
 196        done
 197}
 198
 199__git_refs2 ()
 200{
 201        local i
 202        for i in $(__git_refs "$1"); do
 203                echo "$i:$i"
 204        done
 205}
 206
 207__git_refs_remotes ()
 208{
 209        local cmd i is_hash=y
 210        for i in $(git-ls-remote "$1" 2>/dev/null); do
 211                case "$is_hash,$i" in
 212                n,refs/heads/*)
 213                        is_hash=y
 214                        echo "$i:refs/remotes/$1/${i#refs/heads/}"
 215                        ;;
 216                y,*) is_hash=n ;;
 217                n,*^{}) is_hash=y ;;
 218                n,refs/tags/*) is_hash=y;;
 219                n,*) is_hash=y; ;;
 220                esac
 221        done
 222}
 223
 224__git_remotes ()
 225{
 226        local i ngoff IFS=$'\n' d="$(__gitdir)"
 227        shopt -q nullglob || ngoff=1
 228        shopt -s nullglob
 229        for i in "$d/remotes"/*; do
 230                echo ${i#$d/remotes/}
 231        done
 232        [ "$ngoff" ] && shopt -u nullglob
 233        for i in $(git --git-dir="$d" config --list); do
 234                case "$i" in
 235                remote.*.url=*)
 236                        i="${i#remote.}"
 237                        echo "${i/.url=*/}"
 238                        ;;
 239                esac
 240        done
 241}
 242
 243__git_merge_strategies ()
 244{
 245        if [ -n "$__git_merge_strategylist" ]; then
 246                echo "$__git_merge_strategylist"
 247                return
 248        fi
 249        sed -n "/^all_strategies='/{
 250                s/^all_strategies='//
 251                s/'//
 252                p
 253                q
 254                }" "$(git --exec-path)/git-merge"
 255}
 256__git_merge_strategylist=
 257__git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
 258
 259__git_complete_file ()
 260{
 261        local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
 262        case "$cur" in
 263        ?*:*)
 264                ref="${cur%%:*}"
 265                cur="${cur#*:}"
 266                case "$cur" in
 267                ?*/*)
 268                        pfx="${cur%/*}"
 269                        cur="${cur##*/}"
 270                        ls="$ref:$pfx"
 271                        pfx="$pfx/"
 272                        ;;
 273                *)
 274                        ls="$ref"
 275                        ;;
 276            esac
 277                COMPREPLY=($(compgen -P "$pfx" \
 278                        -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
 279                                | sed '/^100... blob /s,^.*     ,,
 280                                       /^040000 tree /{
 281                                           s,^.*        ,,
 282                                           s,$,/,
 283                                       }
 284                                       s/^.*    //')" \
 285                        -- "$cur"))
 286                ;;
 287        *)
 288                __gitcomp "$(__git_refs)"
 289                ;;
 290        esac
 291}
 292
 293__git_complete_revlist ()
 294{
 295        local pfx cur="${COMP_WORDS[COMP_CWORD]}"
 296        case "$cur" in
 297        *...*)
 298                pfx="${cur%...*}..."
 299                cur="${cur#*...}"
 300                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 301                ;;
 302        *..*)
 303                pfx="${cur%..*}.."
 304                cur="${cur#*..}"
 305                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 306                ;;
 307        *.)
 308                __gitcomp "$cur."
 309                ;;
 310        *)
 311                __gitcomp "$(__git_refs)"
 312                ;;
 313        esac
 314}
 315
 316__git_commands ()
 317{
 318        if [ -n "$__git_commandlist" ]; then
 319                echo "$__git_commandlist"
 320                return
 321        fi
 322        local i IFS=" "$'\n'
 323        for i in $(git help -a|egrep '^ ')
 324        do
 325                case $i in
 326                *--*)             : helper pattern;;
 327                applymbox)        : ask gittus;;
 328                applypatch)       : ask gittus;;
 329                archimport)       : import;;
 330                cat-file)         : plumbing;;
 331                check-attr)       : plumbing;;
 332                check-ref-format) : plumbing;;
 333                commit-tree)      : plumbing;;
 334                cvsexportcommit)  : export;;
 335                cvsimport)        : import;;
 336                cvsserver)        : daemon;;
 337                daemon)           : daemon;;
 338                diff-files)       : plumbing;;
 339                diff-index)       : plumbing;;
 340                diff-tree)        : plumbing;;
 341                fast-import)      : import;;
 342                fsck-objects)     : plumbing;;
 343                fetch-pack)       : plumbing;;
 344                fmt-merge-msg)    : plumbing;;
 345                for-each-ref)     : plumbing;;
 346                hash-object)      : plumbing;;
 347                http-*)           : transport;;
 348                index-pack)       : plumbing;;
 349                init-db)          : deprecated;;
 350                local-fetch)      : plumbing;;
 351                mailinfo)         : plumbing;;
 352                mailsplit)        : plumbing;;
 353                merge-*)          : plumbing;;
 354                mktree)           : plumbing;;
 355                mktag)            : plumbing;;
 356                pack-objects)     : plumbing;;
 357                pack-redundant)   : plumbing;;
 358                pack-refs)        : plumbing;;
 359                parse-remote)     : plumbing;;
 360                patch-id)         : plumbing;;
 361                peek-remote)      : plumbing;;
 362                prune)            : plumbing;;
 363                prune-packed)     : plumbing;;
 364                quiltimport)      : import;;
 365                read-tree)        : plumbing;;
 366                receive-pack)     : plumbing;;
 367                reflog)           : plumbing;;
 368                repo-config)      : deprecated;;
 369                rerere)           : plumbing;;
 370                rev-list)         : plumbing;;
 371                rev-parse)        : plumbing;;
 372                runstatus)        : plumbing;;
 373                sh-setup)         : internal;;
 374                shell)            : daemon;;
 375                send-pack)        : plumbing;;
 376                show-index)       : plumbing;;
 377                ssh-*)            : transport;;
 378                stripspace)       : plumbing;;
 379                svn)              : import export;;
 380                symbolic-ref)     : plumbing;;
 381                tar-tree)         : deprecated;;
 382                unpack-file)      : plumbing;;
 383                unpack-objects)   : plumbing;;
 384                update-index)     : plumbing;;
 385                update-ref)       : plumbing;;
 386                update-server-info) : daemon;;
 387                upload-archive)   : plumbing;;
 388                upload-pack)      : plumbing;;
 389                write-tree)       : plumbing;;
 390                verify-tag)       : plumbing;;
 391                *) echo $i;;
 392                esac
 393        done
 394}
 395__git_commandlist=
 396__git_commandlist="$(__git_commands 2>/dev/null)"
 397
 398__git_aliases ()
 399{
 400        local i IFS=$'\n'
 401        for i in $(git --git-dir="$(__gitdir)" config --list); do
 402                case "$i" in
 403                alias.*)
 404                        i="${i#alias.}"
 405                        echo "${i/=*/}"
 406                        ;;
 407                esac
 408        done
 409}
 410
 411__git_aliased_command ()
 412{
 413        local word cmdline=$(git --git-dir="$(__gitdir)" \
 414                config --get "alias.$1")
 415        for word in $cmdline; do
 416                if [ "${word##-*}" ]; then
 417                        echo $word
 418                        return
 419                fi
 420        done
 421}
 422
 423__git_whitespacelist="nowarn warn error error-all strip"
 424
 425_git_am ()
 426{
 427        local cur="${COMP_WORDS[COMP_CWORD]}"
 428        if [ -d .dotest ]; then
 429                __gitcomp "--skip --resolved"
 430                return
 431        fi
 432        case "$cur" in
 433        --whitespace=*)
 434                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 435                return
 436                ;;
 437        --*)
 438                __gitcomp "
 439                        --signoff --utf8 --binary --3way --interactive
 440                        --whitespace=
 441                        "
 442                return
 443        esac
 444        COMPREPLY=()
 445}
 446
 447_git_apply ()
 448{
 449        local cur="${COMP_WORDS[COMP_CWORD]}"
 450        case "$cur" in
 451        --whitespace=*)
 452                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 453                return
 454                ;;
 455        --*)
 456                __gitcomp "
 457                        --stat --numstat --summary --check --index
 458                        --cached --index-info --reverse --reject --unidiff-zero
 459                        --apply --no-add --exclude=
 460                        --whitespace= --inaccurate-eof --verbose
 461                        "
 462                return
 463        esac
 464        COMPREPLY=()
 465}
 466
 467_git_add ()
 468{
 469        local cur="${COMP_WORDS[COMP_CWORD]}"
 470        case "$cur" in
 471        --*)
 472                __gitcomp "--interactive --refresh"
 473                return
 474        esac
 475        COMPREPLY=()
 476}
 477
 478_git_bisect ()
 479{
 480        local i c=1 command
 481        while [ $c -lt $COMP_CWORD ]; do
 482                i="${COMP_WORDS[c]}"
 483                case "$i" in
 484                start|bad|good|reset|visualize|replay|log)
 485                        command="$i"
 486                        break
 487                        ;;
 488                esac
 489                c=$((++c))
 490        done
 491
 492        if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
 493                __gitcomp "start bad good reset visualize replay log"
 494                return
 495        fi
 496
 497        case "$command" in
 498        bad|good|reset)
 499                __gitcomp "$(__git_refs)"
 500                ;;
 501        *)
 502                COMPREPLY=()
 503                ;;
 504        esac
 505}
 506
 507_git_branch ()
 508{
 509        __gitcomp "$(__git_refs)"
 510}
 511
 512_git_bundle ()
 513{
 514        local mycword="$COMP_CWORD"
 515        case "${COMP_WORDS[0]}" in
 516        git)
 517                local cmd="${COMP_WORDS[2]}"
 518                mycword="$((mycword-1))"
 519                ;;
 520        git-bundle*)
 521                local cmd="${COMP_WORDS[1]}"
 522                ;;
 523        esac
 524        case "$mycword" in
 525        1)
 526                __gitcomp "create list-heads verify unbundle"
 527                ;;
 528        2)
 529                # looking for a file
 530                ;;
 531        *)
 532                case "$cmd" in
 533                        create)
 534                                __git_complete_revlist
 535                        ;;
 536                esac
 537                ;;
 538        esac
 539}
 540
 541_git_checkout ()
 542{
 543        __gitcomp "$(__git_refs)"
 544}
 545
 546_git_cherry ()
 547{
 548        __gitcomp "$(__git_refs)"
 549}
 550
 551_git_cherry_pick ()
 552{
 553        local cur="${COMP_WORDS[COMP_CWORD]}"
 554        case "$cur" in
 555        --*)
 556                __gitcomp "--edit --no-commit"
 557                ;;
 558        *)
 559                __gitcomp "$(__git_refs)"
 560                ;;
 561        esac
 562}
 563
 564_git_commit ()
 565{
 566        local cur="${COMP_WORDS[COMP_CWORD]}"
 567        case "$cur" in
 568        --*)
 569                __gitcomp "
 570                        --all --author= --signoff --verify --no-verify
 571                        --edit --amend --include --only
 572                        "
 573                return
 574        esac
 575        COMPREPLY=()
 576}
 577
 578_git_describe ()
 579{
 580        __gitcomp "$(__git_refs)"
 581}
 582
 583_git_diff ()
 584{
 585        local cur="${COMP_WORDS[COMP_CWORD]}"
 586        case "$cur" in
 587        --*)
 588                __gitcomp "--cached --stat --numstat --shortstat --summary
 589                        --patch-with-stat --name-only --name-status --color
 590                        --no-color --color-words --no-renames --check
 591                        --full-index --binary --abbrev --diff-filter
 592                        --find-copies-harder --pickaxe-all --pickaxe-regex
 593                        --text --ignore-space-at-eol --ignore-space-change
 594                        --ignore-all-space --exit-code --quiet --ext-diff
 595                        --no-ext-diff"
 596                return
 597                ;;
 598        esac
 599        __git_complete_file
 600}
 601
 602_git_diff_tree ()
 603{
 604        __gitcomp "$(__git_refs)"
 605}
 606
 607_git_fetch ()
 608{
 609        local cur="${COMP_WORDS[COMP_CWORD]}"
 610
 611        case "${COMP_WORDS[0]},$COMP_CWORD" in
 612        git-fetch*,1)
 613                __gitcomp "$(__git_remotes)"
 614                ;;
 615        git,2)
 616                __gitcomp "$(__git_remotes)"
 617                ;;
 618        *)
 619                case "$cur" in
 620                *:*)
 621                        __gitcomp "$(__git_refs)" "" "${cur#*:}"
 622                        ;;
 623                *)
 624                        local remote
 625                        case "${COMP_WORDS[0]}" in
 626                        git-fetch) remote="${COMP_WORDS[1]}" ;;
 627                        git)       remote="${COMP_WORDS[2]}" ;;
 628                        esac
 629                        __gitcomp "$(__git_refs2 "$remote")"
 630                        ;;
 631                esac
 632                ;;
 633        esac
 634}
 635
 636_git_format_patch ()
 637{
 638        local cur="${COMP_WORDS[COMP_CWORD]}"
 639        case "$cur" in
 640        --*)
 641                __gitcomp "
 642                        --stdout --attach --thread
 643                        --output-directory
 644                        --numbered --start-number
 645                        --numbered-files
 646                        --keep-subject
 647                        --signoff
 648                        --in-reply-to=
 649                        --full-index --binary
 650                        --not --all
 651                        "
 652                return
 653                ;;
 654        esac
 655        __git_complete_revlist
 656}
 657
 658_git_gc ()
 659{
 660        local cur="${COMP_WORDS[COMP_CWORD]}"
 661        case "$cur" in
 662        --*)
 663                __gitcomp "--prune --aggressive"
 664                return
 665                ;;
 666        esac
 667        COMPREPLY=()
 668}
 669
 670_git_ls_remote ()
 671{
 672        __gitcomp "$(__git_remotes)"
 673}
 674
 675_git_ls_tree ()
 676{
 677        __git_complete_file
 678}
 679
 680_git_log ()
 681{
 682        local cur="${COMP_WORDS[COMP_CWORD]}"
 683        case "$cur" in
 684        --pretty=*)
 685                __gitcomp "
 686                        oneline short medium full fuller email raw
 687                        " "" "${cur##--pretty=}"
 688                return
 689                ;;
 690        --date=*)
 691                __gitcomp "
 692                        relative iso8601 rfc2822 short local default
 693                " "" "${cur##--date=}"
 694                return
 695                ;;
 696        --*)
 697                __gitcomp "
 698                        --max-count= --max-age= --since= --after=
 699                        --min-age= --before= --until=
 700                        --root --topo-order --date-order --reverse
 701                        --no-merges --follow
 702                        --abbrev-commit --abbrev=
 703                        --relative-date --date=
 704                        --author= --committer= --grep=
 705                        --all-match
 706                        --pretty= --name-status --name-only --raw
 707                        --not --all
 708                        --left-right --cherry-pick
 709                        "
 710                return
 711                ;;
 712        esac
 713        __git_complete_revlist
 714}
 715
 716_git_merge ()
 717{
 718        local cur="${COMP_WORDS[COMP_CWORD]}"
 719        case "${COMP_WORDS[COMP_CWORD-1]}" in
 720        -s|--strategy)
 721                __gitcomp "$(__git_merge_strategies)"
 722                return
 723        esac
 724        case "$cur" in
 725        --strategy=*)
 726                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
 727                return
 728                ;;
 729        --*)
 730                __gitcomp "
 731                        --no-commit --no-summary --squash --strategy
 732                        "
 733                return
 734        esac
 735        __gitcomp "$(__git_refs)"
 736}
 737
 738_git_merge_base ()
 739{
 740        __gitcomp "$(__git_refs)"
 741}
 742
 743_git_name_rev ()
 744{
 745        __gitcomp "--tags --all --stdin"
 746}
 747
 748_git_pull ()
 749{
 750        local cur="${COMP_WORDS[COMP_CWORD]}"
 751
 752        case "${COMP_WORDS[0]},$COMP_CWORD" in
 753        git-pull*,1)
 754                __gitcomp "$(__git_remotes)"
 755                ;;
 756        git,2)
 757                __gitcomp "$(__git_remotes)"
 758                ;;
 759        *)
 760                local remote
 761                case "${COMP_WORDS[0]}" in
 762                git-pull)  remote="${COMP_WORDS[1]}" ;;
 763                git)       remote="${COMP_WORDS[2]}" ;;
 764                esac
 765                __gitcomp "$(__git_refs "$remote")"
 766                ;;
 767        esac
 768}
 769
 770_git_push ()
 771{
 772        local cur="${COMP_WORDS[COMP_CWORD]}"
 773
 774        case "${COMP_WORDS[0]},$COMP_CWORD" in
 775        git-push*,1)
 776                __gitcomp "$(__git_remotes)"
 777                ;;
 778        git,2)
 779                __gitcomp "$(__git_remotes)"
 780                ;;
 781        *)
 782                case "$cur" in
 783                *:*)
 784                        local remote
 785                        case "${COMP_WORDS[0]}" in
 786                        git-push)  remote="${COMP_WORDS[1]}" ;;
 787                        git)       remote="${COMP_WORDS[2]}" ;;
 788                        esac
 789                        __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
 790                        ;;
 791                +*)
 792                        __gitcomp "$(__git_refs)" + "${cur#+}"
 793                        ;;
 794                *)
 795                        __gitcomp "$(__git_refs)"
 796                        ;;
 797                esac
 798                ;;
 799        esac
 800}
 801
 802_git_rebase ()
 803{
 804        local cur="${COMP_WORDS[COMP_CWORD]}"
 805        if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
 806                __gitcomp "--continue --skip --abort"
 807                return
 808        fi
 809        case "${COMP_WORDS[COMP_CWORD-1]}" in
 810        -s|--strategy)
 811                __gitcomp "$(__git_merge_strategies)"
 812                return
 813        esac
 814        case "$cur" in
 815        --strategy=*)
 816                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
 817                return
 818                ;;
 819        --*)
 820                __gitcomp "--onto --merge --strategy"
 821                return
 822        esac
 823        __gitcomp "$(__git_refs)"
 824}
 825
 826_git_config ()
 827{
 828        local cur="${COMP_WORDS[COMP_CWORD]}"
 829        local prv="${COMP_WORDS[COMP_CWORD-1]}"
 830        case "$prv" in
 831        branch.*.remote)
 832                __gitcomp "$(__git_remotes)"
 833                return
 834                ;;
 835        branch.*.merge)
 836                __gitcomp "$(__git_refs)"
 837                return
 838                ;;
 839        remote.*.fetch)
 840                local remote="${prv#remote.}"
 841                remote="${remote%.fetch}"
 842                __gitcomp "$(__git_refs_remotes "$remote")"
 843                return
 844                ;;
 845        remote.*.push)
 846                local remote="${prv#remote.}"
 847                remote="${remote%.push}"
 848                __gitcomp "$(git --git-dir="$(__gitdir)" \
 849                        for-each-ref --format='%(refname):%(refname)' \
 850                        refs/heads)"
 851                return
 852                ;;
 853        pull.twohead|pull.octopus)
 854                __gitcomp "$(__git_merge_strategies)"
 855                return
 856                ;;
 857        color.branch|color.diff|color.status)
 858                __gitcomp "always never auto"
 859                return
 860                ;;
 861        color.*.*)
 862                __gitcomp "
 863                        black red green yellow blue magenta cyan white
 864                        bold dim ul blink reverse
 865                        "
 866                return
 867                ;;
 868        *.*)
 869                COMPREPLY=()
 870                return
 871                ;;
 872        esac
 873        case "$cur" in
 874        --*)
 875                __gitcomp "
 876                        --global --system --file=
 877                        --list --replace-all
 878                        --get --get-all --get-regexp
 879                        --add --unset --unset-all
 880                        --remove-section --rename-section
 881                        "
 882                return
 883                ;;
 884        branch.*.*)
 885                local pfx="${cur%.*}."
 886                cur="${cur##*.}"
 887                __gitcomp "remote merge" "$pfx" "$cur"
 888                return
 889                ;;
 890        branch.*)
 891                local pfx="${cur%.*}."
 892                cur="${cur#*.}"
 893                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
 894                return
 895                ;;
 896        remote.*.*)
 897                local pfx="${cur%.*}."
 898                cur="${cur##*.}"
 899                __gitcomp "
 900                        url fetch push skipDefaultUpdate
 901                        receivepack uploadpack tagopt
 902                        " "$pfx" "$cur"
 903                return
 904                ;;
 905        remote.*)
 906                local pfx="${cur%.*}."
 907                cur="${cur#*.}"
 908                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
 909                return
 910                ;;
 911        esac
 912        __gitcomp "
 913                apply.whitespace
 914                core.fileMode
 915                core.gitProxy
 916                core.ignoreStat
 917                core.preferSymlinkRefs
 918                core.logAllRefUpdates
 919                core.loosecompression
 920                core.repositoryFormatVersion
 921                core.sharedRepository
 922                core.warnAmbiguousRefs
 923                core.compression
 924                core.legacyHeaders
 925                core.packedGitWindowSize
 926                core.packedGitLimit
 927                clean.requireForce
 928                color.branch
 929                color.branch.current
 930                color.branch.local
 931                color.branch.remote
 932                color.branch.plain
 933                color.diff
 934                color.diff.plain
 935                color.diff.meta
 936                color.diff.frag
 937                color.diff.old
 938                color.diff.new
 939                color.diff.commit
 940                color.diff.whitespace
 941                color.pager
 942                color.status
 943                color.status.header
 944                color.status.added
 945                color.status.changed
 946                color.status.untracked
 947                diff.renameLimit
 948                diff.renames
 949                fetch.unpackLimit
 950                format.headers
 951                format.subjectprefix
 952                gitcvs.enabled
 953                gitcvs.logfile
 954                gitcvs.allbinary
 955                gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dvpass
 956                gc.packrefs
 957                gc.reflogexpire
 958                gc.reflogexpireunreachable
 959                gc.rerereresolved
 960                gc.rerereunresolved
 961                http.sslVerify
 962                http.sslCert
 963                http.sslKey
 964                http.sslCAInfo
 965                http.sslCAPath
 966                http.maxRequests
 967                http.lowSpeedLimit
 968                http.lowSpeedTime
 969                http.noEPSV
 970                i18n.commitEncoding
 971                i18n.logOutputEncoding
 972                log.showroot
 973                merge.tool
 974                merge.summary
 975                merge.verbosity
 976                pack.window
 977                pack.depth
 978                pack.windowMemory
 979                pack.compression
 980                pack.deltaCacheSize
 981                pack.deltaCacheLimit
 982                pull.octopus
 983                pull.twohead
 984                repack.useDeltaBaseOffset
 985                show.difftree
 986                showbranch.default
 987                tar.umask
 988                transfer.unpackLimit
 989                receive.unpackLimit
 990                receive.denyNonFastForwards
 991                user.name
 992                user.email
 993                user.signingkey
 994                whatchanged.difftree
 995                branch. remote.
 996        "
 997}
 998
 999_git_remote ()
1000{
1001        local i c=1 command
1002        while [ $c -lt $COMP_CWORD ]; do
1003                i="${COMP_WORDS[c]}"
1004                case "$i" in
1005                add|rm|show|prune|update) command="$i"; break ;;
1006                esac
1007                c=$((++c))
1008        done
1009
1010        if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1011                __gitcomp "add rm show prune update"
1012                return
1013        fi
1014
1015        case "$command" in
1016        rm|show|prune)
1017                __gitcomp "$(__git_remotes)"
1018                ;;
1019        update)
1020                local i c='' IFS=$'\n'
1021                for i in $(git --git-dir="$(__gitdir)" config --list); do
1022                        case "$i" in
1023                        remotes.*)
1024                                i="${i#remotes.}"
1025                                c="$c ${i/=*/}"
1026                                ;;
1027                        esac
1028                done
1029                __gitcomp "$c"
1030                ;;
1031        *)
1032                COMPREPLY=()
1033                ;;
1034        esac
1035}
1036
1037_git_reset ()
1038{
1039        local cur="${COMP_WORDS[COMP_CWORD]}"
1040        case "$cur" in
1041        --*)
1042                __gitcomp "--mixed --hard --soft"
1043                return
1044                ;;
1045        esac
1046        __gitcomp "$(__git_refs)"
1047}
1048
1049_git_shortlog ()
1050{
1051        local cur="${COMP_WORDS[COMP_CWORD]}"
1052        case "$cur" in
1053        --*)
1054                __gitcomp "
1055                        --max-count= --max-age= --since= --after=
1056                        --min-age= --before= --until=
1057                        --no-merges
1058                        --author= --committer= --grep=
1059                        --all-match
1060                        --not --all
1061                        --numbered --summary
1062                        "
1063                return
1064                ;;
1065        esac
1066        __git_complete_revlist
1067}
1068
1069_git_show ()
1070{
1071        local cur="${COMP_WORDS[COMP_CWORD]}"
1072        case "$cur" in
1073        --pretty=*)
1074                __gitcomp "
1075                        oneline short medium full fuller email raw
1076                        " "" "${cur##--pretty=}"
1077                return
1078                ;;
1079        --*)
1080                __gitcomp "--pretty="
1081                return
1082                ;;
1083        esac
1084        __git_complete_file
1085}
1086
1087_git_stash ()
1088{
1089        __gitcomp 'list show apply clear'
1090}
1091
1092_git_submodule ()
1093{
1094        local i c=1 command
1095        while [ $c -lt $COMP_CWORD ]; do
1096                i="${COMP_WORDS[c]}"
1097                case "$i" in
1098                add|status|init|update) command="$i"; break ;;
1099                esac
1100                c=$((++c))
1101        done
1102
1103        if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1104                local cur="${COMP_WORDS[COMP_CWORD]}"
1105                case "$cur" in
1106                --*)
1107                        __gitcomp "--quiet --cached"
1108                        ;;
1109                *)
1110                        __gitcomp "add status init update"
1111                        ;;
1112                esac
1113                return
1114        fi
1115}
1116
1117_git_tag ()
1118{
1119        local i c=1 f=0
1120        while [ $c -lt $COMP_CWORD ]; do
1121                i="${COMP_WORDS[c]}"
1122                case "$i" in
1123                -d|-v)
1124                        __gitcomp "$(__git_tags)"
1125                        return
1126                        ;;
1127                -f)
1128                        f=1
1129                        ;;
1130                esac
1131                c=$((++c))
1132        done
1133
1134        case "${COMP_WORDS[COMP_CWORD-1]}" in
1135        -m|-F)
1136                COMPREPLY=()
1137                ;;
1138        -*|tag|git-tag)
1139                if [ $f = 1 ]; then
1140                        __gitcomp "$(__git_tags)"
1141                else
1142                        COMPREPLY=()
1143                fi
1144                ;;
1145        *)
1146                __gitcomp "$(__git_refs)"
1147                ;;
1148        esac
1149}
1150
1151_git ()
1152{
1153        local i c=1 command __git_dir
1154
1155        while [ $c -lt $COMP_CWORD ]; do
1156                i="${COMP_WORDS[c]}"
1157                case "$i" in
1158                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1159                --bare)      __git_dir="." ;;
1160                --version|--help|-p|--paginate) ;;
1161                *) command="$i"; break ;;
1162                esac
1163                c=$((++c))
1164        done
1165
1166        if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1167                case "${COMP_WORDS[COMP_CWORD]}" in
1168                --*=*) COMPREPLY=() ;;
1169                --*)   __gitcomp "
1170                        --no-pager
1171                        --git-dir=
1172                        --bare
1173                        --version
1174                        --exec-path
1175                        "
1176                        ;;
1177                *)     __gitcomp "$(__git_commands) $(__git_aliases)" ;;
1178                esac
1179                return
1180        fi
1181
1182        local expansion=$(__git_aliased_command "$command")
1183        [ "$expansion" ] && command="$expansion"
1184
1185        case "$command" in
1186        am)          _git_am ;;
1187        add)         _git_add ;;
1188        apply)       _git_apply ;;
1189        bisect)      _git_bisect ;;
1190        bundle)      _git_bundle ;;
1191        branch)      _git_branch ;;
1192        checkout)    _git_checkout ;;
1193        cherry)      _git_cherry ;;
1194        cherry-pick) _git_cherry_pick ;;
1195        commit)      _git_commit ;;
1196        config)      _git_config ;;
1197        describe)    _git_describe ;;
1198        diff)        _git_diff ;;
1199        fetch)       _git_fetch ;;
1200        format-patch) _git_format_patch ;;
1201        gc)          _git_gc ;;
1202        log)         _git_log ;;
1203        ls-remote)   _git_ls_remote ;;
1204        ls-tree)     _git_ls_tree ;;
1205        merge)       _git_merge;;
1206        merge-base)  _git_merge_base ;;
1207        name-rev)    _git_name_rev ;;
1208        pull)        _git_pull ;;
1209        push)        _git_push ;;
1210        rebase)      _git_rebase ;;
1211        remote)      _git_remote ;;
1212        reset)       _git_reset ;;
1213        shortlog)    _git_shortlog ;;
1214        show)        _git_show ;;
1215        show-branch) _git_log ;;
1216        stash)       _git_stash ;;
1217        submodule)   _git_submodule ;;
1218        tag)         _git_tag ;;
1219        whatchanged) _git_log ;;
1220        *)           COMPREPLY=() ;;
1221        esac
1222}
1223
1224_gitk ()
1225{
1226        local cur="${COMP_WORDS[COMP_CWORD]}"
1227        case "$cur" in
1228        --*)
1229                __gitcomp "--not --all"
1230                return
1231                ;;
1232        esac
1233        __git_complete_revlist
1234}
1235
1236complete -o default -o nospace -F _git git
1237complete -o default -o nospace -F _gitk gitk
1238complete -o default -o nospace -F _git_am git-am
1239complete -o default -o nospace -F _git_apply git-apply
1240complete -o default -o nospace -F _git_bisect git-bisect
1241complete -o default -o nospace -F _git_branch git-branch
1242complete -o default -o nospace -F _git_bundle git-bundle
1243complete -o default -o nospace -F _git_checkout git-checkout
1244complete -o default -o nospace -F _git_cherry git-cherry
1245complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
1246complete -o default -o nospace -F _git_commit git-commit
1247complete -o default -o nospace -F _git_describe git-describe
1248complete -o default -o nospace -F _git_diff git-diff
1249complete -o default -o nospace -F _git_fetch git-fetch
1250complete -o default -o nospace -F _git_format_patch git-format-patch
1251complete -o default -o nospace -F _git_gc git-gc
1252complete -o default -o nospace -F _git_log git-log
1253complete -o default -o nospace -F _git_ls_remote git-ls-remote
1254complete -o default -o nospace -F _git_ls_tree git-ls-tree
1255complete -o default -o nospace -F _git_merge git-merge
1256complete -o default -o nospace -F _git_merge_base git-merge-base
1257complete -o default -o nospace -F _git_name_rev git-name-rev
1258complete -o default -o nospace -F _git_pull git-pull
1259complete -o default -o nospace -F _git_push git-push
1260complete -o default -o nospace -F _git_rebase git-rebase
1261complete -o default -o nospace -F _git_config git-config
1262complete -o default -o nospace -F _git_remote git-remote
1263complete -o default -o nospace -F _git_reset git-reset
1264complete -o default -o nospace -F _git_shortlog git-shortlog
1265complete -o default -o nospace -F _git_show git-show
1266complete -o default -o nospace -F _git_stash git-stash
1267complete -o default -o nospace -F _git_submodule git-submodule
1268complete -o default -o nospace -F _git_log git-show-branch
1269complete -o default -o nospace -F _git_tag git-tag
1270complete -o default -o nospace -F _git_log git-whatchanged
1271
1272# The following are necessary only for Cygwin, and only are needed
1273# when the user has tab-completed the executable name and consequently
1274# included the '.exe' suffix.
1275#
1276if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1277complete -o default -o nospace -F _git_add git-add.exe
1278complete -o default -o nospace -F _git_apply git-apply.exe
1279complete -o default -o nospace -F _git git.exe
1280complete -o default -o nospace -F _git_branch git-branch.exe
1281complete -o default -o nospace -F _git_bundle git-bundle.exe
1282complete -o default -o nospace -F _git_cherry git-cherry.exe
1283complete -o default -o nospace -F _git_describe git-describe.exe
1284complete -o default -o nospace -F _git_diff git-diff.exe
1285complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1286complete -o default -o nospace -F _git_log git-log.exe
1287complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1288complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1289complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1290complete -o default -o nospace -F _git_push git-push.exe
1291complete -o default -o nospace -F _git_config git-config
1292complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1293complete -o default -o nospace -F _git_show git-show.exe
1294complete -o default -o nospace -F _git_log git-show-branch.exe
1295complete -o default -o nospace -F _git_tag git-tag.exe
1296complete -o default -o nospace -F _git_log git-whatchanged.exe
1297fi