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