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