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