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