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