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