contrib / completion / git-completion.bashon commit completion: More fixes to prevent unbound variable errors (5c9cc64)
   1#!bash
   2#
   3# bash completion support for core Git.
   4#
   5# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
   6# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
   7# Distributed under the GNU General Public License, version 2.0.
   8#
   9# The contained completion routines provide support for completing:
  10#
  11#    *) local and remote branch names
  12#    *) local and remote tag names
  13#    *) .git/remotes file names
  14#    *) git 'subcommands'
  15#    *) tree paths within 'ref:path/to/file' expressions
  16#    *) common --long-options
  17#
  18# To use these routines:
  19#
  20#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
  21#    2) Added the following line to your .bashrc:
  22#        source ~/.git-completion.sh
  23#
  24#    3) You may want to make sure the git executable is available
  25#       in your PATH before this script is sourced, as some caching
  26#       is performed while the script loads.  If git isn't found
  27#       at source time then all lookups will be done on demand,
  28#       which may be slightly slower.
  29#
  30#    4) Consider changing your PS1 to also show the current branch:
  31#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  32#
  33#       The argument to __git_ps1 will be displayed only if you
  34#       are currently in a git repository.  The %s token will be
  35#       the name of the current branch.
  36#
  37#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
  38#       value, unstaged (*) and staged (+) changes will be shown next
  39#       to the branch name.  You can configure this per-repository
  40#       with the bash.showDirtyState variable, which defaults to true
  41#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
  42#
  43# To submit patches:
  44#
  45#    *) Read Documentation/SubmittingPatches
  46#    *) Send all patches to the current maintainer:
  47#
  48#       "Shawn O. Pearce" <spearce@spearce.org>
  49#
  50#    *) Always CC the Git mailing list:
  51#
  52#       git@vger.kernel.org
  53#
  54
  55case "$COMP_WORDBREAKS" in
  56*:*) : great ;;
  57*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  58esac
  59
  60# __gitdir accepts 0 or 1 arguments (i.e., location)
  61# returns location of .git repo
  62__gitdir ()
  63{
  64        if [ -z "${1-}" ]; then
  65                if [ -n "${__git_dir-}" ]; then
  66                        echo "$__git_dir"
  67                elif [ -d .git ]; then
  68                        echo .git
  69                else
  70                        git rev-parse --git-dir 2>/dev/null
  71                fi
  72        elif [ -d "$1/.git" ]; then
  73                echo "$1/.git"
  74        else
  75                echo "$1"
  76        fi
  77}
  78
  79# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
  80# returns text to add to bash PS1 prompt (includes branch name)
  81__git_ps1 ()
  82{
  83        local g="$(__gitdir)"
  84        if [ -n "$g" ]; then
  85                local r
  86                local b
  87                if [ -d "$g/rebase-apply" ]; then
  88                        if [ -f "$g/rebase-apply/rebasing" ]; then
  89                                r="|REBASE"
  90                elif [ -f "$g/rebase-apply/applying" ]; then
  91                                r="|AM"
  92                        else
  93                                r="|AM/REBASE"
  94                        fi
  95                        b="$(git symbolic-ref HEAD 2>/dev/null)"
  96                elif [ -f "$g/rebase-merge/interactive" ]; then
  97                        r="|REBASE-i"
  98                        b="$(cat "$g/rebase-merge/head-name")"
  99                elif [ -d "$g/rebase-merge" ]; then
 100                        r="|REBASE-m"
 101                        b="$(cat "$g/rebase-merge/head-name")"
 102                elif [ -f "$g/MERGE_HEAD" ]; then
 103                        r="|MERGING"
 104                        b="$(git symbolic-ref HEAD 2>/dev/null)"
 105                else
 106                        if [ -f "$g/BISECT_LOG" ]; then
 107                                r="|BISECTING"
 108                        fi
 109                        if ! b="$(git symbolic-ref HEAD 2>/dev/null)"; then
 110                                if ! b="$(git describe --exact-match HEAD 2>/dev/null)"; then
 111                                        if [ -r "$g/HEAD" ]; then
 112                                                b="$(cut -c1-7 "$g/HEAD")..."
 113                                        fi
 114                                fi
 115                        fi
 116                fi
 117
 118                local w
 119                local i
 120
 121                if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 122                        b="GIT_DIR!"
 123                elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
 124                        if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
 125                                if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
 126                                        git diff --no-ext-diff --ignore-submodules \
 127                                                --quiet --exit-code || w="*"
 128                                        if git rev-parse --quiet --verify HEAD >/dev/null; then
 129                                                git diff-index --cached --quiet \
 130                                                        --ignore-submodules HEAD -- || i="+"
 131                                        else
 132                                                i="#"
 133                                        fi
 134                                fi
 135                        fi
 136                fi
 137
 138                if [ -n "$b" ]; then
 139                        if [ -n "${1-}" ]; then
 140                                printf "$1" "${b##refs/heads/}$w$i$r"
 141                        else
 142                                printf " (%s)" "${b##refs/heads/}$w$i$r"
 143                        fi
 144                fi
 145        fi
 146}
 147
 148# __gitcomp_1 requires 2 arguments
 149__gitcomp_1 ()
 150{
 151        local c IFS=' '$'\t'$'\n'
 152        for c in $1; do
 153                case "$c$2" in
 154                --*=*) printf %s$'\n' "$c$2" ;;
 155                *.)    printf %s$'\n' "$c$2" ;;
 156                *)     printf %s$'\n' "$c$2 " ;;
 157                esac
 158        done
 159}
 160
 161# __gitcomp accepts 1, 2, 3, or 4 arguments
 162# generates completion reply with compgen
 163__gitcomp ()
 164{
 165        local cur="${COMP_WORDS[COMP_CWORD]}"
 166        if [ $# -gt 2 ]; then
 167                cur="$3"
 168        fi
 169        case "$cur" in
 170        --*=)
 171                COMPREPLY=()
 172                ;;
 173        *)
 174                local IFS=$'\n'
 175                COMPREPLY=($(compgen -P "${2-}" \
 176                        -W "$(__gitcomp_1 "${1-}" "${4-}")" \
 177                        -- "$cur"))
 178                ;;
 179        esac
 180}
 181
 182# __git_heads accepts 0 or 1 arguments (to pass to __gitdir)
 183__git_heads ()
 184{
 185        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 186        if [ -d "$dir" ]; then
 187                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 188                        refs/heads
 189                return
 190        fi
 191        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 192                case "$is_hash,$i" in
 193                y,*) is_hash=n ;;
 194                n,*^{}) is_hash=y ;;
 195                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 196                n,*) is_hash=y; echo "$i" ;;
 197                esac
 198        done
 199}
 200
 201# __git_tags accepts 0 or 1 arguments (to pass to __gitdir)
 202__git_tags ()
 203{
 204        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 205        if [ -d "$dir" ]; then
 206                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 207                        refs/tags
 208                return
 209        fi
 210        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 211                case "$is_hash,$i" in
 212                y,*) is_hash=n ;;
 213                n,*^{}) is_hash=y ;;
 214                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 215                n,*) is_hash=y; echo "$i" ;;
 216                esac
 217        done
 218}
 219
 220# __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
 221__git_refs ()
 222{
 223        local i is_hash=y dir="$(__gitdir "${1-}")"
 224        local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 225        if [ -d "$dir" ]; then
 226                case "$cur" in
 227                refs|refs/*)
 228                        format="refname"
 229                        refs="${cur%/*}"
 230                        ;;
 231                *)
 232                        if [ -e "$dir/HEAD" ]; then echo HEAD; fi
 233                        format="refname:short"
 234                        refs="refs/tags refs/heads refs/remotes"
 235                        ;;
 236                esac
 237                git --git-dir="$dir" for-each-ref --format="%($format)" \
 238                        $refs
 239                return
 240        fi
 241        for i in $(git ls-remote "$dir" 2>/dev/null); do
 242                case "$is_hash,$i" in
 243                y,*) is_hash=n ;;
 244                n,*^{}) is_hash=y ;;
 245                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 246                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 247                n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
 248                n,*) is_hash=y; echo "$i" ;;
 249                esac
 250        done
 251}
 252
 253# __git_refs2 requires 1 argument (to pass to __git_refs)
 254__git_refs2 ()
 255{
 256        local i
 257        for i in $(__git_refs "$1"); do
 258                echo "$i:$i"
 259        done
 260}
 261
 262# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 263__git_refs_remotes ()
 264{
 265        local cmd i is_hash=y
 266        for i in $(git ls-remote "$1" 2>/dev/null); do
 267                case "$is_hash,$i" in
 268                n,refs/heads/*)
 269                        is_hash=y
 270                        echo "$i:refs/remotes/$1/${i#refs/heads/}"
 271                        ;;
 272                y,*) is_hash=n ;;
 273                n,*^{}) is_hash=y ;;
 274                n,refs/tags/*) is_hash=y;;
 275                n,*) is_hash=y; ;;
 276                esac
 277        done
 278}
 279
 280__git_remotes ()
 281{
 282        local i ngoff IFS=$'\n' d="$(__gitdir)"
 283        shopt -q nullglob || ngoff=1
 284        shopt -s nullglob
 285        for i in "$d/remotes"/*; do
 286                echo ${i#$d/remotes/}
 287        done
 288        [ "$ngoff" ] && shopt -u nullglob
 289        for i in $(git --git-dir="$d" config --list); do
 290                case "$i" in
 291                remote.*.url=*)
 292                        i="${i#remote.}"
 293                        echo "${i/.url=*/}"
 294                        ;;
 295                esac
 296        done
 297}
 298
 299__git_merge_strategies ()
 300{
 301        if [ -n "${__git_merge_strategylist-}" ]; then
 302                echo "$__git_merge_strategylist"
 303                return
 304        fi
 305        git merge -s help 2>&1 |
 306        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 307                s/\.$//
 308                s/.*://
 309                s/^[    ]*//
 310                s/[     ]*$//
 311                p
 312        }'
 313}
 314__git_merge_strategylist=
 315__git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
 316
 317__git_complete_file ()
 318{
 319        local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
 320        case "$cur" in
 321        ?*:*)
 322                ref="${cur%%:*}"
 323                cur="${cur#*:}"
 324                case "$cur" in
 325                ?*/*)
 326                        pfx="${cur%/*}"
 327                        cur="${cur##*/}"
 328                        ls="$ref:$pfx"
 329                        pfx="$pfx/"
 330                        ;;
 331                *)
 332                        ls="$ref"
 333                        ;;
 334            esac
 335
 336                case "$COMP_WORDBREAKS" in
 337                *:*) : great ;;
 338                *)   pfx="$ref:$pfx" ;;
 339                esac
 340
 341                local IFS=$'\n'
 342                COMPREPLY=($(compgen -P "$pfx" \
 343                        -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
 344                                | sed '/^100... blob /{
 345                                           s,^.*        ,,
 346                                           s,$, ,
 347                                       }
 348                                       /^120000 blob /{
 349                                           s,^.*        ,,
 350                                           s,$, ,
 351                                       }
 352                                       /^040000 tree /{
 353                                           s,^.*        ,,
 354                                           s,$,/,
 355                                       }
 356                                       s/^.*    //')" \
 357                        -- "$cur"))
 358                ;;
 359        *)
 360                __gitcomp "$(__git_refs)"
 361                ;;
 362        esac
 363}
 364
 365__git_complete_revlist ()
 366{
 367        local pfx cur="${COMP_WORDS[COMP_CWORD]}"
 368        case "$cur" in
 369        *...*)
 370                pfx="${cur%...*}..."
 371                cur="${cur#*...}"
 372                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 373                ;;
 374        *..*)
 375                pfx="${cur%..*}.."
 376                cur="${cur#*..}"
 377                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 378                ;;
 379        *)
 380                __gitcomp "$(__git_refs)"
 381                ;;
 382        esac
 383}
 384
 385__git_all_commands ()
 386{
 387        if [ -n "${__git_all_commandlist-}" ]; then
 388                echo "$__git_all_commandlist"
 389                return
 390        fi
 391        local i IFS=" "$'\n'
 392        for i in $(git help -a|egrep '^ ')
 393        do
 394                case $i in
 395                *--*)             : helper pattern;;
 396                *) echo $i;;
 397                esac
 398        done
 399}
 400__git_all_commandlist=
 401__git_all_commandlist="$(__git_all_commands 2>/dev/null)"
 402
 403__git_porcelain_commands ()
 404{
 405        if [ -n "${__git_porcelain_commandlist-}" ]; then
 406                echo "$__git_porcelain_commandlist"
 407                return
 408        fi
 409        local i IFS=" "$'\n'
 410        for i in "help" $(__git_all_commands)
 411        do
 412                case $i in
 413                *--*)             : helper pattern;;
 414                applymbox)        : ask gittus;;
 415                applypatch)       : ask gittus;;
 416                archimport)       : import;;
 417                cat-file)         : plumbing;;
 418                check-attr)       : plumbing;;
 419                check-ref-format) : plumbing;;
 420                checkout-index)   : plumbing;;
 421                commit-tree)      : plumbing;;
 422                count-objects)    : infrequent;;
 423                cvsexportcommit)  : export;;
 424                cvsimport)        : import;;
 425                cvsserver)        : daemon;;
 426                daemon)           : daemon;;
 427                diff-files)       : plumbing;;
 428                diff-index)       : plumbing;;
 429                diff-tree)        : plumbing;;
 430                fast-import)      : import;;
 431                fast-export)      : export;;
 432                fsck-objects)     : plumbing;;
 433                fetch-pack)       : plumbing;;
 434                fmt-merge-msg)    : plumbing;;
 435                for-each-ref)     : plumbing;;
 436                hash-object)      : plumbing;;
 437                http-*)           : transport;;
 438                index-pack)       : plumbing;;
 439                init-db)          : deprecated;;
 440                local-fetch)      : plumbing;;
 441                lost-found)       : infrequent;;
 442                ls-files)         : plumbing;;
 443                ls-remote)        : plumbing;;
 444                ls-tree)          : plumbing;;
 445                mailinfo)         : plumbing;;
 446                mailsplit)        : plumbing;;
 447                merge-*)          : plumbing;;
 448                mktree)           : plumbing;;
 449                mktag)            : plumbing;;
 450                pack-objects)     : plumbing;;
 451                pack-redundant)   : plumbing;;
 452                pack-refs)        : plumbing;;
 453                parse-remote)     : plumbing;;
 454                patch-id)         : plumbing;;
 455                peek-remote)      : plumbing;;
 456                prune)            : plumbing;;
 457                prune-packed)     : plumbing;;
 458                quiltimport)      : import;;
 459                read-tree)        : plumbing;;
 460                receive-pack)     : plumbing;;
 461                reflog)           : plumbing;;
 462                repo-config)      : deprecated;;
 463                rerere)           : plumbing;;
 464                rev-list)         : plumbing;;
 465                rev-parse)        : plumbing;;
 466                runstatus)        : plumbing;;
 467                sh-setup)         : internal;;
 468                shell)            : daemon;;
 469                show-ref)         : plumbing;;
 470                send-pack)        : plumbing;;
 471                show-index)       : plumbing;;
 472                ssh-*)            : transport;;
 473                stripspace)       : plumbing;;
 474                symbolic-ref)     : plumbing;;
 475                tar-tree)         : deprecated;;
 476                unpack-file)      : plumbing;;
 477                unpack-objects)   : plumbing;;
 478                update-index)     : plumbing;;
 479                update-ref)       : plumbing;;
 480                update-server-info) : daemon;;
 481                upload-archive)   : plumbing;;
 482                upload-pack)      : plumbing;;
 483                write-tree)       : plumbing;;
 484                var)              : infrequent;;
 485                verify-pack)      : infrequent;;
 486                verify-tag)       : plumbing;;
 487                *) echo $i;;
 488                esac
 489        done
 490}
 491__git_porcelain_commandlist=
 492__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
 493
 494__git_aliases ()
 495{
 496        local i IFS=$'\n'
 497        for i in $(git --git-dir="$(__gitdir)" config --list); do
 498                case "$i" in
 499                alias.*)
 500                        i="${i#alias.}"
 501                        echo "${i/=*/}"
 502                        ;;
 503                esac
 504        done
 505}
 506
 507# __git_aliased_command requires 1 argument
 508__git_aliased_command ()
 509{
 510        local word cmdline=$(git --git-dir="$(__gitdir)" \
 511                config --get "alias.$1")
 512        for word in $cmdline; do
 513                if [ "${word##-*}" ]; then
 514                        echo $word
 515                        return
 516                fi
 517        done
 518}
 519
 520# __git_find_subcommand requires 1 argument
 521__git_find_subcommand ()
 522{
 523        local word subcommand c=1
 524
 525        while [ $c -lt $COMP_CWORD ]; do
 526                word="${COMP_WORDS[c]}"
 527                for subcommand in $1; do
 528                        if [ "$subcommand" = "$word" ]; then
 529                                echo "$subcommand"
 530                                return
 531                        fi
 532                done
 533                c=$((++c))
 534        done
 535}
 536
 537__git_has_doubledash ()
 538{
 539        local c=1
 540        while [ $c -lt $COMP_CWORD ]; do
 541                if [ "--" = "${COMP_WORDS[c]}" ]; then
 542                        return 0
 543                fi
 544                c=$((++c))
 545        done
 546        return 1
 547}
 548
 549__git_whitespacelist="nowarn warn error error-all fix"
 550
 551_git_am ()
 552{
 553        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
 554        if [ -d "$dir"/rebase-apply ]; then
 555                __gitcomp "--skip --resolved --abort"
 556                return
 557        fi
 558        case "$cur" in
 559        --whitespace=*)
 560                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 561                return
 562                ;;
 563        --*)
 564                __gitcomp "
 565                        --signoff --utf8 --binary --3way --interactive
 566                        --whitespace=
 567                        "
 568                return
 569        esac
 570        COMPREPLY=()
 571}
 572
 573_git_apply ()
 574{
 575        local cur="${COMP_WORDS[COMP_CWORD]}"
 576        case "$cur" in
 577        --whitespace=*)
 578                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 579                return
 580                ;;
 581        --*)
 582                __gitcomp "
 583                        --stat --numstat --summary --check --index
 584                        --cached --index-info --reverse --reject --unidiff-zero
 585                        --apply --no-add --exclude=
 586                        --whitespace= --inaccurate-eof --verbose
 587                        "
 588                return
 589        esac
 590        COMPREPLY=()
 591}
 592
 593_git_add ()
 594{
 595        __git_has_doubledash && return
 596
 597        local cur="${COMP_WORDS[COMP_CWORD]}"
 598        case "$cur" in
 599        --*)
 600                __gitcomp "
 601                        --interactive --refresh --patch --update --dry-run
 602                        --ignore-errors --intent-to-add
 603                        "
 604                return
 605        esac
 606        COMPREPLY=()
 607}
 608
 609_git_archive ()
 610{
 611        local cur="${COMP_WORDS[COMP_CWORD]}"
 612        case "$cur" in
 613        --format=*)
 614                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
 615                return
 616                ;;
 617        --remote=*)
 618                __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
 619                return
 620                ;;
 621        --*)
 622                __gitcomp "
 623                        --format= --list --verbose
 624                        --prefix= --remote= --exec=
 625                        "
 626                return
 627                ;;
 628        esac
 629        __git_complete_file
 630}
 631
 632_git_bisect ()
 633{
 634        __git_has_doubledash && return
 635
 636        local subcommands="start bad good skip reset visualize replay log run"
 637        local subcommand="$(__git_find_subcommand "$subcommands")"
 638        if [ -z "$subcommand" ]; then
 639                __gitcomp "$subcommands"
 640                return
 641        fi
 642
 643        case "$subcommand" in
 644        bad|good|reset|skip)
 645                __gitcomp "$(__git_refs)"
 646                ;;
 647        *)
 648                COMPREPLY=()
 649                ;;
 650        esac
 651}
 652
 653_git_branch ()
 654{
 655        local i c=1 only_local_ref="n" has_r="n"
 656
 657        while [ $c -lt $COMP_CWORD ]; do
 658                i="${COMP_WORDS[c]}"
 659                case "$i" in
 660                -d|-m)  only_local_ref="y" ;;
 661                -r)     has_r="y" ;;
 662                esac
 663                c=$((++c))
 664        done
 665
 666        case "${COMP_WORDS[COMP_CWORD]}" in
 667        --*)
 668                __gitcomp "
 669                        --color --no-color --verbose --abbrev= --no-abbrev
 670                        --track --no-track --contains --merged --no-merged
 671                        "
 672                ;;
 673        *)
 674                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 675                        __gitcomp "$(__git_heads)"
 676                else
 677                        __gitcomp "$(__git_refs)"
 678                fi
 679                ;;
 680        esac
 681}
 682
 683_git_bundle ()
 684{
 685        local cmd="${COMP_WORDS[2]}"
 686        case "$COMP_CWORD" in
 687        2)
 688                __gitcomp "create list-heads verify unbundle"
 689                ;;
 690        3)
 691                # looking for a file
 692                ;;
 693        *)
 694                case "$cmd" in
 695                        create)
 696                                __git_complete_revlist
 697                        ;;
 698                esac
 699                ;;
 700        esac
 701}
 702
 703_git_checkout ()
 704{
 705        __git_has_doubledash && return
 706
 707        __gitcomp "$(__git_refs)"
 708}
 709
 710_git_cherry ()
 711{
 712        __gitcomp "$(__git_refs)"
 713}
 714
 715_git_cherry_pick ()
 716{
 717        local cur="${COMP_WORDS[COMP_CWORD]}"
 718        case "$cur" in
 719        --*)
 720                __gitcomp "--edit --no-commit"
 721                ;;
 722        *)
 723                __gitcomp "$(__git_refs)"
 724                ;;
 725        esac
 726}
 727
 728_git_clean ()
 729{
 730        __git_has_doubledash && return
 731
 732        local cur="${COMP_WORDS[COMP_CWORD]}"
 733        case "$cur" in
 734        --*)
 735                __gitcomp "--dry-run --quiet"
 736                return
 737                ;;
 738        esac
 739        COMPREPLY=()
 740}
 741
 742_git_clone ()
 743{
 744        local cur="${COMP_WORDS[COMP_CWORD]}"
 745        case "$cur" in
 746        --*)
 747                __gitcomp "
 748                        --local
 749                        --no-hardlinks
 750                        --shared
 751                        --reference
 752                        --quiet
 753                        --no-checkout
 754                        --bare
 755                        --mirror
 756                        --origin
 757                        --upload-pack
 758                        --template=
 759                        --depth
 760                        "
 761                return
 762                ;;
 763        esac
 764        COMPREPLY=()
 765}
 766
 767_git_commit ()
 768{
 769        __git_has_doubledash && return
 770
 771        local cur="${COMP_WORDS[COMP_CWORD]}"
 772        case "$cur" in
 773        --*)
 774                __gitcomp "
 775                        --all --author= --signoff --verify --no-verify
 776                        --edit --amend --include --only --interactive
 777                        "
 778                return
 779        esac
 780        COMPREPLY=()
 781}
 782
 783_git_describe ()
 784{
 785        local cur="${COMP_WORDS[COMP_CWORD]}"
 786        case "$cur" in
 787        --*)
 788                __gitcomp "
 789                        --all --tags --contains --abbrev= --candidates=
 790                        --exact-match --debug --long --match --always
 791                        "
 792                return
 793        esac
 794        __gitcomp "$(__git_refs)"
 795}
 796
 797__git_diff_common_options="--stat --numstat --shortstat --summary
 798                        --patch-with-stat --name-only --name-status --color
 799                        --no-color --color-words --no-renames --check
 800                        --full-index --binary --abbrev --diff-filter=
 801                        --find-copies-harder
 802                        --text --ignore-space-at-eol --ignore-space-change
 803                        --ignore-all-space --exit-code --quiet --ext-diff
 804                        --no-ext-diff
 805                        --no-prefix --src-prefix= --dst-prefix=
 806                        --inter-hunk-context=
 807                        --patience
 808                        --raw
 809"
 810
 811_git_diff ()
 812{
 813        __git_has_doubledash && return
 814
 815        local cur="${COMP_WORDS[COMP_CWORD]}"
 816        case "$cur" in
 817        --*)
 818                __gitcomp "--cached --pickaxe-all --pickaxe-regex
 819                        --base --ours --theirs
 820                        $__git_diff_common_options
 821                        "
 822                return
 823                ;;
 824        esac
 825        __git_complete_file
 826}
 827
 828_git_fetch ()
 829{
 830        local cur="${COMP_WORDS[COMP_CWORD]}"
 831
 832        if [ "$COMP_CWORD" = 2 ]; then
 833                __gitcomp "$(__git_remotes)"
 834        else
 835                case "$cur" in
 836                *:*)
 837                        local pfx=""
 838                        case "$COMP_WORDBREAKS" in
 839                        *:*) : great ;;
 840                        *)   pfx="${cur%%:*}:" ;;
 841                        esac
 842                        __gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
 843                        ;;
 844                *)
 845                        __gitcomp "$(__git_refs2 "${COMP_WORDS[2]}")"
 846                        ;;
 847                esac
 848        fi
 849}
 850
 851_git_format_patch ()
 852{
 853        local cur="${COMP_WORDS[COMP_CWORD]}"
 854        case "$cur" in
 855        --*)
 856                __gitcomp "
 857                        --stdout --attach --thread
 858                        --output-directory
 859                        --numbered --start-number
 860                        --numbered-files
 861                        --keep-subject
 862                        --signoff
 863                        --in-reply-to=
 864                        --full-index --binary
 865                        --not --all
 866                        --cover-letter
 867                        --no-prefix --src-prefix= --dst-prefix=
 868                        --inline --suffix= --ignore-if-in-upstream
 869                        --subject-prefix=
 870                        "
 871                return
 872                ;;
 873        esac
 874        __git_complete_revlist
 875}
 876
 877_git_gc ()
 878{
 879        local cur="${COMP_WORDS[COMP_CWORD]}"
 880        case "$cur" in
 881        --*)
 882                __gitcomp "--prune --aggressive"
 883                return
 884                ;;
 885        esac
 886        COMPREPLY=()
 887}
 888
 889_git_grep ()
 890{
 891        __git_has_doubledash && return
 892
 893        local cur="${COMP_WORDS[COMP_CWORD]}"
 894        case "$cur" in
 895        --*)
 896                __gitcomp "
 897                        --cached
 898                        --text --ignore-case --word-regexp --invert-match
 899                        --full-name
 900                        --extended-regexp --basic-regexp --fixed-strings
 901                        --files-with-matches --name-only
 902                        --files-without-match
 903                        --count
 904                        --and --or --not --all-match
 905                        "
 906                return
 907                ;;
 908        esac
 909        COMPREPLY=()
 910}
 911
 912_git_help ()
 913{
 914        local cur="${COMP_WORDS[COMP_CWORD]}"
 915        case "$cur" in
 916        --*)
 917                __gitcomp "--all --info --man --web"
 918                return
 919                ;;
 920        esac
 921        __gitcomp "$(__git_all_commands)
 922                attributes cli core-tutorial cvs-migration
 923                diffcore gitk glossary hooks ignore modules
 924                repository-layout tutorial tutorial-2
 925                workflows
 926                "
 927}
 928
 929_git_init ()
 930{
 931        local cur="${COMP_WORDS[COMP_CWORD]}"
 932        case "$cur" in
 933        --shared=*)
 934                __gitcomp "
 935                        false true umask group all world everybody
 936                        " "" "${cur##--shared=}"
 937                return
 938                ;;
 939        --*)
 940                __gitcomp "--quiet --bare --template= --shared --shared="
 941                return
 942                ;;
 943        esac
 944        COMPREPLY=()
 945}
 946
 947_git_ls_files ()
 948{
 949        __git_has_doubledash && return
 950
 951        local cur="${COMP_WORDS[COMP_CWORD]}"
 952        case "$cur" in
 953        --*)
 954                __gitcomp "--cached --deleted --modified --others --ignored
 955                        --stage --directory --no-empty-directory --unmerged
 956                        --killed --exclude= --exclude-from=
 957                        --exclude-per-directory= --exclude-standard
 958                        --error-unmatch --with-tree= --full-name
 959                        --abbrev --ignored --exclude-per-directory
 960                        "
 961                return
 962                ;;
 963        esac
 964        COMPREPLY=()
 965}
 966
 967_git_ls_remote ()
 968{
 969        __gitcomp "$(__git_remotes)"
 970}
 971
 972_git_ls_tree ()
 973{
 974        __git_complete_file
 975}
 976
 977__git_log_pretty_formats="oneline short medium full fuller email raw format:"
 978
 979_git_log ()
 980{
 981        __git_has_doubledash && return
 982
 983        local cur="${COMP_WORDS[COMP_CWORD]}"
 984        case "$cur" in
 985        --pretty=*)
 986                __gitcomp "$__git_log_pretty_formats
 987                        " "" "${cur##--pretty=}"
 988                return
 989                ;;
 990        --date=*)
 991                __gitcomp "
 992                        relative iso8601 rfc2822 short local default
 993                " "" "${cur##--date=}"
 994                return
 995                ;;
 996        --*)
 997                __gitcomp "
 998                        --max-count= --max-age= --since= --after=
 999                        --min-age= --before= --until=
1000                        --root --topo-order --date-order --reverse
1001                        --no-merges --follow
1002                        --abbrev-commit --abbrev=
1003                        --relative-date --date=
1004                        --author= --committer= --grep=
1005                        --all-match
1006                        --pretty=
1007                        --not --all
1008                        --left-right --cherry-pick
1009                        --graph
1010                        --decorate
1011                        --walk-reflogs
1012                        --parents --children --full-history
1013                        --merge
1014                        $__git_diff_common_options
1015                        --pickaxe-all --pickaxe-regex
1016                        "
1017                return
1018                ;;
1019        esac
1020        __git_complete_revlist
1021}
1022
1023_git_merge ()
1024{
1025        local cur="${COMP_WORDS[COMP_CWORD]}"
1026        case "${COMP_WORDS[COMP_CWORD-1]}" in
1027        -s|--strategy)
1028                __gitcomp "$(__git_merge_strategies)"
1029                return
1030        esac
1031        case "$cur" in
1032        --strategy=*)
1033                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1034                return
1035                ;;
1036        --*)
1037                __gitcomp "
1038                        --no-commit --no-stat --log --no-log --squash --strategy
1039                        "
1040                return
1041        esac
1042        __gitcomp "$(__git_refs)"
1043}
1044
1045_git_mergetool ()
1046{
1047        local cur="${COMP_WORDS[COMP_CWORD]}"
1048        case "$cur" in
1049        --tool=*)
1050                __gitcomp "
1051                        kdiff3 tkdiff meld xxdiff emerge
1052                        vimdiff gvimdiff ecmerge opendiff
1053                        " "" "${cur##--tool=}"
1054                return
1055                ;;
1056        --*)
1057                __gitcomp "--tool="
1058                return
1059                ;;
1060        esac
1061        COMPREPLY=()
1062}
1063
1064_git_merge_base ()
1065{
1066        __gitcomp "$(__git_refs)"
1067}
1068
1069_git_mv ()
1070{
1071        local cur="${COMP_WORDS[COMP_CWORD]}"
1072        case "$cur" in
1073        --*)
1074                __gitcomp "--dry-run"
1075                return
1076                ;;
1077        esac
1078        COMPREPLY=()
1079}
1080
1081_git_name_rev ()
1082{
1083        __gitcomp "--tags --all --stdin"
1084}
1085
1086_git_pull ()
1087{
1088        local cur="${COMP_WORDS[COMP_CWORD]}"
1089
1090        if [ "$COMP_CWORD" = 2 ]; then
1091                __gitcomp "$(__git_remotes)"
1092        else
1093                __gitcomp "$(__git_refs "${COMP_WORDS[2]}")"
1094        fi
1095}
1096
1097_git_push ()
1098{
1099        local cur="${COMP_WORDS[COMP_CWORD]}"
1100
1101        if [ "$COMP_CWORD" = 2 ]; then
1102                __gitcomp "$(__git_remotes)"
1103        else
1104                case "$cur" in
1105                *:*)
1106                        local pfx=""
1107                        case "$COMP_WORDBREAKS" in
1108                        *:*) : great ;;
1109                        *)   pfx="${cur%%:*}:" ;;
1110                        esac
1111
1112                        __gitcomp "$(__git_refs "${COMP_WORDS[2]}")" "$pfx" "${cur#*:}"
1113                        ;;
1114                +*)
1115                        __gitcomp "$(__git_refs)" + "${cur#+}"
1116                        ;;
1117                *)
1118                        __gitcomp "$(__git_refs)"
1119                        ;;
1120                esac
1121        fi
1122}
1123
1124_git_rebase ()
1125{
1126        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1127        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1128                __gitcomp "--continue --skip --abort"
1129                return
1130        fi
1131        case "${COMP_WORDS[COMP_CWORD-1]}" in
1132        -s|--strategy)
1133                __gitcomp "$(__git_merge_strategies)"
1134                return
1135        esac
1136        case "$cur" in
1137        --strategy=*)
1138                __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1139                return
1140                ;;
1141        --*)
1142                __gitcomp "--onto --merge --strategy --interactive"
1143                return
1144        esac
1145        __gitcomp "$(__git_refs)"
1146}
1147
1148_git_send_email ()
1149{
1150        local cur="${COMP_WORDS[COMP_CWORD]}"
1151        case "$cur" in
1152        --*)
1153                __gitcomp "--bcc --cc --cc-cmd --chain-reply-to --compose
1154                        --dry-run --envelope-sender --from --identity
1155                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1156                        --no-suppress-from --no-thread --quiet
1157                        --signed-off-by-cc --smtp-pass --smtp-server
1158                        --smtp-server-port --smtp-ssl --smtp-user --subject
1159                        --suppress-cc --suppress-from --thread --to
1160                        --validate --no-validate"
1161                return
1162                ;;
1163        esac
1164        COMPREPLY=()
1165}
1166
1167_git_config ()
1168{
1169        local cur="${COMP_WORDS[COMP_CWORD]}"
1170        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1171        case "$prv" in
1172        branch.*.remote)
1173                __gitcomp "$(__git_remotes)"
1174                return
1175                ;;
1176        branch.*.merge)
1177                __gitcomp "$(__git_refs)"
1178                return
1179                ;;
1180        remote.*.fetch)
1181                local remote="${prv#remote.}"
1182                remote="${remote%.fetch}"
1183                __gitcomp "$(__git_refs_remotes "$remote")"
1184                return
1185                ;;
1186        remote.*.push)
1187                local remote="${prv#remote.}"
1188                remote="${remote%.push}"
1189                __gitcomp "$(git --git-dir="$(__gitdir)" \
1190                        for-each-ref --format='%(refname):%(refname)' \
1191                        refs/heads)"
1192                return
1193                ;;
1194        pull.twohead|pull.octopus)
1195                __gitcomp "$(__git_merge_strategies)"
1196                return
1197                ;;
1198        color.branch|color.diff|color.status)
1199                __gitcomp "always never auto"
1200                return
1201                ;;
1202        color.*.*)
1203                __gitcomp "
1204                        normal black red green yellow blue magenta cyan white
1205                        bold dim ul blink reverse
1206                        "
1207                return
1208                ;;
1209        *.*)
1210                COMPREPLY=()
1211                return
1212                ;;
1213        esac
1214        case "$cur" in
1215        --*)
1216                __gitcomp "
1217                        --global --system --file=
1218                        --list --replace-all
1219                        --get --get-all --get-regexp
1220                        --add --unset --unset-all
1221                        --remove-section --rename-section
1222                        "
1223                return
1224                ;;
1225        branch.*.*)
1226                local pfx="${cur%.*}."
1227                cur="${cur##*.}"
1228                __gitcomp "remote merge mergeoptions" "$pfx" "$cur"
1229                return
1230                ;;
1231        branch.*)
1232                local pfx="${cur%.*}."
1233                cur="${cur#*.}"
1234                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1235                return
1236                ;;
1237        remote.*.*)
1238                local pfx="${cur%.*}."
1239                cur="${cur##*.}"
1240                __gitcomp "
1241                        url proxy fetch push mirror skipDefaultUpdate
1242                        receivepack uploadpack tagopt
1243                        " "$pfx" "$cur"
1244                return
1245                ;;
1246        remote.*)
1247                local pfx="${cur%.*}."
1248                cur="${cur#*.}"
1249                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1250                return
1251                ;;
1252        esac
1253        __gitcomp "
1254                apply.whitespace
1255                branch.autosetupmerge
1256                branch.autosetuprebase
1257                clean.requireForce
1258                color.branch
1259                color.branch.current
1260                color.branch.local
1261                color.branch.plain
1262                color.branch.remote
1263                color.diff
1264                color.diff.commit
1265                color.diff.frag
1266                color.diff.meta
1267                color.diff.new
1268                color.diff.old
1269                color.diff.plain
1270                color.diff.whitespace
1271                color.interactive
1272                color.interactive.header
1273                color.interactive.help
1274                color.interactive.prompt
1275                color.pager
1276                color.status
1277                color.status.added
1278                color.status.changed
1279                color.status.header
1280                color.status.nobranch
1281                color.status.untracked
1282                color.status.updated
1283                color.ui
1284                commit.template
1285                core.autocrlf
1286                core.bare
1287                core.compression
1288                core.deltaBaseCacheLimit
1289                core.editor
1290                core.excludesfile
1291                core.fileMode
1292                core.fsyncobjectfiles
1293                core.gitProxy
1294                core.ignoreCygwinFSTricks
1295                core.ignoreStat
1296                core.logAllRefUpdates
1297                core.loosecompression
1298                core.packedGitLimit
1299                core.packedGitWindowSize
1300                core.pager
1301                core.preferSymlinkRefs
1302                core.preloadindex
1303                core.quotepath
1304                core.repositoryFormatVersion
1305                core.safecrlf
1306                core.sharedRepository
1307                core.symlinks
1308                core.trustctime
1309                core.warnAmbiguousRefs
1310                core.whitespace
1311                core.worktree
1312                diff.autorefreshindex
1313                diff.external
1314                diff.mnemonicprefix
1315                diff.renameLimit
1316                diff.renameLimit.
1317                diff.renames
1318                fetch.unpackLimit
1319                format.headers
1320                format.numbered
1321                format.pretty
1322                format.suffix
1323                gc.aggressiveWindow
1324                gc.auto
1325                gc.autopacklimit
1326                gc.packrefs
1327                gc.pruneexpire
1328                gc.reflogexpire
1329                gc.reflogexpireunreachable
1330                gc.rerereresolved
1331                gc.rerereunresolved
1332                gitcvs.allbinary
1333                gitcvs.dbTableNamePrefix
1334                gitcvs.dbdriver
1335                gitcvs.dbname
1336                gitcvs.dbpass
1337                gitcvs.dbuser
1338                gitcvs.enabled
1339                gitcvs.logfile
1340                gitcvs.usecrlfattr
1341                gui.blamehistoryctx
1342                gui.commitmsgwidth
1343                gui.copyblamethreshold
1344                gui.diffcontext
1345                gui.encoding
1346                gui.fastcopyblame
1347                gui.matchtrackingbranch
1348                gui.newbranchtemplate
1349                gui.pruneduringfetch
1350                gui.spellingdictionary
1351                gui.trustmtime
1352                help.autocorrect
1353                help.browser
1354                help.format
1355                http.lowSpeedLimit
1356                http.lowSpeedTime
1357                http.maxRequests
1358                http.noEPSV
1359                http.proxy
1360                http.sslCAInfo
1361                http.sslCAPath
1362                http.sslCert
1363                http.sslKey
1364                http.sslVerify
1365                i18n.commitEncoding
1366                i18n.logOutputEncoding
1367                instaweb.browser
1368                instaweb.httpd
1369                instaweb.local
1370                instaweb.modulepath
1371                instaweb.port
1372                log.date
1373                log.showroot
1374                man.viewer
1375                merge.conflictstyle
1376                merge.log
1377                merge.renameLimit
1378                merge.stat
1379                merge.tool
1380                merge.verbosity
1381                mergetool.keepBackup
1382                pack.compression
1383                pack.deltaCacheLimit
1384                pack.deltaCacheSize
1385                pack.depth
1386                pack.indexVersion
1387                pack.packSizeLimit
1388                pack.threads
1389                pack.window
1390                pack.windowMemory
1391                pull.octopus
1392                pull.twohead
1393                receive.denyCurrentBranch
1394                receive.denyDeletes
1395                receive.denyNonFastForwards
1396                receive.fsckObjects
1397                receive.unpackLimit
1398                repack.usedeltabaseoffset
1399                rerere.autoupdate
1400                rerere.enabled
1401                showbranch.default
1402                status.relativePaths
1403                status.showUntrackedFiles
1404                tar.umask
1405                transfer.unpackLimit
1406                user.email
1407                user.name
1408                user.signingkey
1409                web.browser
1410                branch. remote.
1411        "
1412}
1413
1414_git_remote ()
1415{
1416        local subcommands="add rename rm show prune update"
1417        local subcommand="$(__git_find_subcommand "$subcommands")"
1418        if [ -z "$subcommand" ]; then
1419                __gitcomp "$subcommands"
1420                return
1421        fi
1422
1423        case "$subcommand" in
1424        rename|rm|show|prune)
1425                __gitcomp "$(__git_remotes)"
1426                ;;
1427        update)
1428                local i c='' IFS=$'\n'
1429                for i in $(git --git-dir="$(__gitdir)" config --list); do
1430                        case "$i" in
1431                        remotes.*)
1432                                i="${i#remotes.}"
1433                                c="$c ${i/=*/}"
1434                                ;;
1435                        esac
1436                done
1437                __gitcomp "$c"
1438                ;;
1439        *)
1440                COMPREPLY=()
1441                ;;
1442        esac
1443}
1444
1445_git_reset ()
1446{
1447        __git_has_doubledash && return
1448
1449        local cur="${COMP_WORDS[COMP_CWORD]}"
1450        case "$cur" in
1451        --*)
1452                __gitcomp "--merge --mixed --hard --soft"
1453                return
1454                ;;
1455        esac
1456        __gitcomp "$(__git_refs)"
1457}
1458
1459_git_revert ()
1460{
1461        local cur="${COMP_WORDS[COMP_CWORD]}"
1462        case "$cur" in
1463        --*)
1464                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1465                return
1466                ;;
1467        esac
1468        __gitcomp "$(__git_refs)"
1469}
1470
1471_git_rm ()
1472{
1473        __git_has_doubledash && return
1474
1475        local cur="${COMP_WORDS[COMP_CWORD]}"
1476        case "$cur" in
1477        --*)
1478                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1479                return
1480                ;;
1481        esac
1482        COMPREPLY=()
1483}
1484
1485_git_shortlog ()
1486{
1487        __git_has_doubledash && return
1488
1489        local cur="${COMP_WORDS[COMP_CWORD]}"
1490        case "$cur" in
1491        --*)
1492                __gitcomp "
1493                        --max-count= --max-age= --since= --after=
1494                        --min-age= --before= --until=
1495                        --no-merges
1496                        --author= --committer= --grep=
1497                        --all-match
1498                        --not --all
1499                        --numbered --summary
1500                        "
1501                return
1502                ;;
1503        esac
1504        __git_complete_revlist
1505}
1506
1507_git_show ()
1508{
1509        __git_has_doubledash && return
1510
1511        local cur="${COMP_WORDS[COMP_CWORD]}"
1512        case "$cur" in
1513        --pretty=*)
1514                __gitcomp "$__git_log_pretty_formats
1515                        " "" "${cur##--pretty=}"
1516                return
1517                ;;
1518        --*)
1519                __gitcomp "--pretty=
1520                        $__git_diff_common_options
1521                        "
1522                return
1523                ;;
1524        esac
1525        __git_complete_file
1526}
1527
1528_git_show_branch ()
1529{
1530        local cur="${COMP_WORDS[COMP_CWORD]}"
1531        case "$cur" in
1532        --*)
1533                __gitcomp "
1534                        --all --remotes --topo-order --current --more=
1535                        --list --independent --merge-base --no-name
1536                        --sha1-name --topics --reflog
1537                        "
1538                return
1539                ;;
1540        esac
1541        __git_complete_revlist
1542}
1543
1544_git_stash ()
1545{
1546        local subcommands='save list show apply clear drop pop create branch'
1547        local subcommand="$(__git_find_subcommand "$subcommands")"
1548        if [ -z "$subcommand" ]; then
1549                __gitcomp "$subcommands"
1550        else
1551                local cur="${COMP_WORDS[COMP_CWORD]}"
1552                case "$subcommand,$cur" in
1553                save,--*)
1554                        __gitcomp "--keep-index"
1555                        ;;
1556                apply,--*)
1557                        __gitcomp "--index"
1558                        ;;
1559                show,--*|drop,--*|pop,--*|branch,--*)
1560                        COMPREPLY=()
1561                        ;;
1562                show,*|apply,*|drop,*|pop,*|branch,*)
1563                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
1564                                        | sed -n -e 's/:.*//p')"
1565                        ;;
1566                *)
1567                        COMPREPLY=()
1568                        ;;
1569                esac
1570        fi
1571}
1572
1573_git_submodule ()
1574{
1575        __git_has_doubledash && return
1576
1577        local subcommands="add status init update summary foreach sync"
1578        if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1579                local cur="${COMP_WORDS[COMP_CWORD]}"
1580                case "$cur" in
1581                --*)
1582                        __gitcomp "--quiet --cached"
1583                        ;;
1584                *)
1585                        __gitcomp "$subcommands"
1586                        ;;
1587                esac
1588                return
1589        fi
1590}
1591
1592_git_svn ()
1593{
1594        local subcommands="
1595                init fetch clone rebase dcommit log find-rev
1596                set-tree commit-diff info create-ignore propget
1597                proplist show-ignore show-externals
1598                "
1599        local subcommand="$(__git_find_subcommand "$subcommands")"
1600        if [ -z "$subcommand" ]; then
1601                __gitcomp "$subcommands"
1602        else
1603                local remote_opts="--username= --config-dir= --no-auth-cache"
1604                local fc_opts="
1605                        --follow-parent --authors-file= --repack=
1606                        --no-metadata --use-svm-props --use-svnsync-props
1607                        --log-window-size= --no-checkout --quiet
1608                        --repack-flags --user-log-author --localtime $remote_opts
1609                        "
1610                local init_opts="
1611                        --template= --shared= --trunk= --tags=
1612                        --branches= --stdlayout --minimize-url
1613                        --no-metadata --use-svm-props --use-svnsync-props
1614                        --rewrite-root= $remote_opts
1615                        "
1616                local cmt_opts="
1617                        --edit --rmdir --find-copies-harder --copy-similarity=
1618                        "
1619
1620                local cur="${COMP_WORDS[COMP_CWORD]}"
1621                case "$subcommand,$cur" in
1622                fetch,--*)
1623                        __gitcomp "--revision= --fetch-all $fc_opts"
1624                        ;;
1625                clone,--*)
1626                        __gitcomp "--revision= $fc_opts $init_opts"
1627                        ;;
1628                init,--*)
1629                        __gitcomp "$init_opts"
1630                        ;;
1631                dcommit,--*)
1632                        __gitcomp "
1633                                --merge --strategy= --verbose --dry-run
1634                                --fetch-all --no-rebase $cmt_opts $fc_opts
1635                                "
1636                        ;;
1637                set-tree,--*)
1638                        __gitcomp "--stdin $cmt_opts $fc_opts"
1639                        ;;
1640                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
1641                show-externals,--*)
1642                        __gitcomp "--revision="
1643                        ;;
1644                log,--*)
1645                        __gitcomp "
1646                                --limit= --revision= --verbose --incremental
1647                                --oneline --show-commit --non-recursive
1648                                --authors-file=
1649                                "
1650                        ;;
1651                rebase,--*)
1652                        __gitcomp "
1653                                --merge --verbose --strategy= --local
1654                                --fetch-all $fc_opts
1655                                "
1656                        ;;
1657                commit-diff,--*)
1658                        __gitcomp "--message= --file= --revision= $cmt_opts"
1659                        ;;
1660                info,--*)
1661                        __gitcomp "--url"
1662                        ;;
1663                *)
1664                        COMPREPLY=()
1665                        ;;
1666                esac
1667        fi
1668}
1669
1670_git_tag ()
1671{
1672        local i c=1 f=0
1673        while [ $c -lt $COMP_CWORD ]; do
1674                i="${COMP_WORDS[c]}"
1675                case "$i" in
1676                -d|-v)
1677                        __gitcomp "$(__git_tags)"
1678                        return
1679                        ;;
1680                -f)
1681                        f=1
1682                        ;;
1683                esac
1684                c=$((++c))
1685        done
1686
1687        case "${COMP_WORDS[COMP_CWORD-1]}" in
1688        -m|-F)
1689                COMPREPLY=()
1690                ;;
1691        -*|tag)
1692                if [ $f = 1 ]; then
1693                        __gitcomp "$(__git_tags)"
1694                else
1695                        COMPREPLY=()
1696                fi
1697                ;;
1698        *)
1699                __gitcomp "$(__git_refs)"
1700                ;;
1701        esac
1702}
1703
1704_git ()
1705{
1706        local i c=1 command __git_dir
1707
1708        while [ $c -lt $COMP_CWORD ]; do
1709                i="${COMP_WORDS[c]}"
1710                case "$i" in
1711                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1712                --bare)      __git_dir="." ;;
1713                --version|-p|--paginate) ;;
1714                --help) command="help"; break ;;
1715                *) command="$i"; break ;;
1716                esac
1717                c=$((++c))
1718        done
1719
1720        if [ -z "$command" ]; then
1721                case "${COMP_WORDS[COMP_CWORD]}" in
1722                --*)   __gitcomp "
1723                        --paginate
1724                        --no-pager
1725                        --git-dir=
1726                        --bare
1727                        --version
1728                        --exec-path
1729                        --work-tree=
1730                        --help
1731                        "
1732                        ;;
1733                *)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1734                esac
1735                return
1736        fi
1737
1738        local expansion=$(__git_aliased_command "$command")
1739        [ "$expansion" ] && command="$expansion"
1740
1741        case "$command" in
1742        am)          _git_am ;;
1743        add)         _git_add ;;
1744        apply)       _git_apply ;;
1745        archive)     _git_archive ;;
1746        bisect)      _git_bisect ;;
1747        bundle)      _git_bundle ;;
1748        branch)      _git_branch ;;
1749        checkout)    _git_checkout ;;
1750        cherry)      _git_cherry ;;
1751        cherry-pick) _git_cherry_pick ;;
1752        clean)       _git_clean ;;
1753        clone)       _git_clone ;;
1754        commit)      _git_commit ;;
1755        config)      _git_config ;;
1756        describe)    _git_describe ;;
1757        diff)        _git_diff ;;
1758        fetch)       _git_fetch ;;
1759        format-patch) _git_format_patch ;;
1760        gc)          _git_gc ;;
1761        grep)        _git_grep ;;
1762        help)        _git_help ;;
1763        init)        _git_init ;;
1764        log)         _git_log ;;
1765        ls-files)    _git_ls_files ;;
1766        ls-remote)   _git_ls_remote ;;
1767        ls-tree)     _git_ls_tree ;;
1768        merge)       _git_merge;;
1769        mergetool)   _git_mergetool;;
1770        merge-base)  _git_merge_base ;;
1771        mv)          _git_mv ;;
1772        name-rev)    _git_name_rev ;;
1773        pull)        _git_pull ;;
1774        push)        _git_push ;;
1775        rebase)      _git_rebase ;;
1776        remote)      _git_remote ;;
1777        reset)       _git_reset ;;
1778        revert)      _git_revert ;;
1779        rm)          _git_rm ;;
1780        send-email)  _git_send_email ;;
1781        shortlog)    _git_shortlog ;;
1782        show)        _git_show ;;
1783        show-branch) _git_show_branch ;;
1784        stash)       _git_stash ;;
1785        stage)       _git_add ;;
1786        submodule)   _git_submodule ;;
1787        svn)         _git_svn ;;
1788        tag)         _git_tag ;;
1789        whatchanged) _git_log ;;
1790        *)           COMPREPLY=() ;;
1791        esac
1792}
1793
1794_gitk ()
1795{
1796        __git_has_doubledash && return
1797
1798        local cur="${COMP_WORDS[COMP_CWORD]}"
1799        local g="$(__gitdir)"
1800        local merge=""
1801        if [ -f $g/MERGE_HEAD ]; then
1802                merge="--merge"
1803        fi
1804        case "$cur" in
1805        --*)
1806                __gitcomp "--not --all $merge"
1807                return
1808                ;;
1809        esac
1810        __git_complete_revlist
1811}
1812
1813complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
1814        || complete -o default -o nospace -F _git git
1815complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
1816        || complete -o default -o nospace -F _gitk gitk
1817
1818# The following are necessary only for Cygwin, and only are needed
1819# when the user has tab-completed the executable name and consequently
1820# included the '.exe' suffix.
1821#
1822if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1823complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
1824        || complete -o default -o nospace -F _git git.exe
1825fi