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