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