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