contrib / completion / git-completion.bashon commit Merge branch 'jc/1.7.0-diff-whitespace-only-status' (3cc3fb7)
   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) Consider changing your PS1 to also show the current branch:
  25#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
  26#
  27#       The argument to __git_ps1 will be displayed only if you
  28#       are currently in a git repository.  The %s token will be
  29#       the name of the current branch.
  30#
  31#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
  32#       value, unstaged (*) and staged (+) changes will be shown next
  33#       to the branch name.  You can configure this per-repository
  34#       with the bash.showDirtyState variable, which defaults to true
  35#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
  36#
  37#       You can also see if currently something is stashed, by setting
  38#       GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
  39#       then a '$' will be shown next to the branch name.
  40#
  41#       If you would like to see if there're untracked files, then you can
  42#       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
  43#       untracked files, then a '%' will be shown next to the branch name.
  44#
  45# To submit patches:
  46#
  47#    *) Read Documentation/SubmittingPatches
  48#    *) Send all patches to the current maintainer:
  49#
  50#       "Shawn O. Pearce" <spearce@spearce.org>
  51#
  52#    *) Always CC the Git mailing list:
  53#
  54#       git@vger.kernel.org
  55#
  56
  57case "$COMP_WORDBREAKS" in
  58*:*) : great ;;
  59*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  60esac
  61
  62# __gitdir accepts 0 or 1 arguments (i.e., location)
  63# returns location of .git repo
  64__gitdir ()
  65{
  66        if [ -z "${1-}" ]; then
  67                if [ -n "${__git_dir-}" ]; then
  68                        echo "$__git_dir"
  69                elif [ -d .git ]; then
  70                        echo .git
  71                else
  72                        git rev-parse --git-dir 2>/dev/null
  73                fi
  74        elif [ -d "$1/.git" ]; then
  75                echo "$1/.git"
  76        else
  77                echo "$1"
  78        fi
  79}
  80
  81# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
  82# returns text to add to bash PS1 prompt (includes branch name)
  83__git_ps1 ()
  84{
  85        local g="$(__gitdir)"
  86        if [ -n "$g" ]; then
  87                local r
  88                local b
  89                if [ -f "$g/rebase-merge/interactive" ]; then
  90                        r="|REBASE-i"
  91                        b="$(cat "$g/rebase-merge/head-name")"
  92                elif [ -d "$g/rebase-merge" ]; then
  93                        r="|REBASE-m"
  94                        b="$(cat "$g/rebase-merge/head-name")"
  95                else
  96                        if [ -d "$g/rebase-apply" ]; then
  97                                if [ -f "$g/rebase-apply/rebasing" ]; then
  98                                        r="|REBASE"
  99                                elif [ -f "$g/rebase-apply/applying" ]; then
 100                                        r="|AM"
 101                                else
 102                                        r="|AM/REBASE"
 103                                fi
 104                        elif [ -f "$g/MERGE_HEAD" ]; then
 105                                r="|MERGING"
 106                        elif [ -f "$g/BISECT_LOG" ]; then
 107                                r="|BISECTING"
 108                        fi
 109
 110                        b="$(git symbolic-ref HEAD 2>/dev/null)" || {
 111
 112                                b="$(
 113                                case "${GIT_PS1_DESCRIBE_STYLE-}" in
 114                                (contains)
 115                                        git describe --contains HEAD ;;
 116                                (branch)
 117                                        git describe --contains --all HEAD ;;
 118                                (describe)
 119                                        git describe HEAD ;;
 120                                (* | default)
 121                                        git describe --exact-match HEAD ;;
 122                                esac 2>/dev/null)" ||
 123
 124                                b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
 125                                b="unknown"
 126                                b="($b)"
 127                        }
 128                fi
 129
 130                local w
 131                local i
 132                local s
 133                local u
 134                local c
 135
 136                if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
 137                        if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
 138                                c="BARE:"
 139                        else
 140                                b="GIT_DIR!"
 141                        fi
 142                elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
 143                        if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
 144                                if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
 145                                        git diff --no-ext-diff --ignore-submodules \
 146                                                --quiet --exit-code || w="*"
 147                                        if git rev-parse --quiet --verify HEAD >/dev/null; then
 148                                                git diff-index --cached --quiet \
 149                                                        --ignore-submodules HEAD -- || i="+"
 150                                        else
 151                                                i="#"
 152                                        fi
 153                                fi
 154                        fi
 155                        if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
 156                                git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
 157                        fi
 158
 159                        if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
 160                           if [ -n "$(git ls-files --others --exclude-standard)" ]; then
 161                              u="%"
 162                           fi
 163                        fi
 164                fi
 165
 166                if [ -n "${1-}" ]; then
 167                        printf "$1" "$c${b##refs/heads/}$w$i$s$u$r"
 168                else
 169                        printf " (%s)" "$c${b##refs/heads/}$w$i$s$u$r"
 170                fi
 171        fi
 172}
 173
 174# __gitcomp_1 requires 2 arguments
 175__gitcomp_1 ()
 176{
 177        local c IFS=' '$'\t'$'\n'
 178        for c in $1; do
 179                case "$c$2" in
 180                --*=*) printf %s$'\n' "$c$2" ;;
 181                *.)    printf %s$'\n' "$c$2" ;;
 182                *)     printf %s$'\n' "$c$2 " ;;
 183                esac
 184        done
 185}
 186
 187# __gitcomp accepts 1, 2, 3, or 4 arguments
 188# generates completion reply with compgen
 189__gitcomp ()
 190{
 191        local cur="${COMP_WORDS[COMP_CWORD]}"
 192        if [ $# -gt 2 ]; then
 193                cur="$3"
 194        fi
 195        case "$cur" in
 196        --*=)
 197                COMPREPLY=()
 198                ;;
 199        *)
 200                local IFS=$'\n'
 201                COMPREPLY=($(compgen -P "${2-}" \
 202                        -W "$(__gitcomp_1 "${1-}" "${4-}")" \
 203                        -- "$cur"))
 204                ;;
 205        esac
 206}
 207
 208# __git_heads accepts 0 or 1 arguments (to pass to __gitdir)
 209__git_heads ()
 210{
 211        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 212        if [ -d "$dir" ]; then
 213                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 214                        refs/heads
 215                return
 216        fi
 217        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 218                case "$is_hash,$i" in
 219                y,*) is_hash=n ;;
 220                n,*^{}) is_hash=y ;;
 221                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 222                n,*) is_hash=y; echo "$i" ;;
 223                esac
 224        done
 225}
 226
 227# __git_tags accepts 0 or 1 arguments (to pass to __gitdir)
 228__git_tags ()
 229{
 230        local cmd i is_hash=y dir="$(__gitdir "${1-}")"
 231        if [ -d "$dir" ]; then
 232                git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
 233                        refs/tags
 234                return
 235        fi
 236        for i in $(git ls-remote "${1-}" 2>/dev/null); do
 237                case "$is_hash,$i" in
 238                y,*) is_hash=n ;;
 239                n,*^{}) is_hash=y ;;
 240                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 241                n,*) is_hash=y; echo "$i" ;;
 242                esac
 243        done
 244}
 245
 246# __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
 247__git_refs ()
 248{
 249        local i is_hash=y dir="$(__gitdir "${1-}")"
 250        local cur="${COMP_WORDS[COMP_CWORD]}" format refs
 251        if [ -d "$dir" ]; then
 252                case "$cur" in
 253                refs|refs/*)
 254                        format="refname"
 255                        refs="${cur%/*}"
 256                        ;;
 257                *)
 258                        if [ -e "$dir/HEAD" ]; then echo HEAD; fi
 259                        format="refname:short"
 260                        refs="refs/tags refs/heads refs/remotes"
 261                        ;;
 262                esac
 263                git --git-dir="$dir" for-each-ref --format="%($format)" \
 264                        $refs
 265                return
 266        fi
 267        for i in $(git ls-remote "$dir" 2>/dev/null); do
 268                case "$is_hash,$i" in
 269                y,*) is_hash=n ;;
 270                n,*^{}) is_hash=y ;;
 271                n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
 272                n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
 273                n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
 274                n,*) is_hash=y; echo "$i" ;;
 275                esac
 276        done
 277}
 278
 279# __git_refs2 requires 1 argument (to pass to __git_refs)
 280__git_refs2 ()
 281{
 282        local i
 283        for i in $(__git_refs "$1"); do
 284                echo "$i:$i"
 285        done
 286}
 287
 288# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 289__git_refs_remotes ()
 290{
 291        local cmd i is_hash=y
 292        for i in $(git ls-remote "$1" 2>/dev/null); do
 293                case "$is_hash,$i" in
 294                n,refs/heads/*)
 295                        is_hash=y
 296                        echo "$i:refs/remotes/$1/${i#refs/heads/}"
 297                        ;;
 298                y,*) is_hash=n ;;
 299                n,*^{}) is_hash=y ;;
 300                n,refs/tags/*) is_hash=y;;
 301                n,*) is_hash=y; ;;
 302                esac
 303        done
 304}
 305
 306__git_remotes ()
 307{
 308        local i ngoff IFS=$'\n' d="$(__gitdir)"
 309        shopt -q nullglob || ngoff=1
 310        shopt -s nullglob
 311        for i in "$d/remotes"/*; do
 312                echo ${i#$d/remotes/}
 313        done
 314        [ "$ngoff" ] && shopt -u nullglob
 315        for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
 316                i="${i#remote.}"
 317                echo "${i/.url*/}"
 318        done
 319}
 320
 321__git_list_merge_strategies ()
 322{
 323        git merge -s help 2>&1 |
 324        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 325                s/\.$//
 326                s/.*://
 327                s/^[    ]*//
 328                s/[     ]*$//
 329                p
 330        }'
 331}
 332
 333__git_merge_strategies=
 334# 'git merge -s help' (and thus detection of the merge strategy
 335# list) fails, unfortunately, if run outside of any git working
 336# tree.  __git_merge_strategies is set to the empty string in
 337# that case, and the detection will be repeated the next time it
 338# is needed.
 339__git_compute_merge_strategies ()
 340{
 341        : ${__git_merge_strategies:=$(__git_list_merge_strategies)}
 342}
 343
 344__git_complete_file ()
 345{
 346        local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
 347        case "$cur" in
 348        ?*:*)
 349                ref="${cur%%:*}"
 350                cur="${cur#*:}"
 351                case "$cur" in
 352                ?*/*)
 353                        pfx="${cur%/*}"
 354                        cur="${cur##*/}"
 355                        ls="$ref:$pfx"
 356                        pfx="$pfx/"
 357                        ;;
 358                *)
 359                        ls="$ref"
 360                        ;;
 361            esac
 362
 363                case "$COMP_WORDBREAKS" in
 364                *:*) : great ;;
 365                *)   pfx="$ref:$pfx" ;;
 366                esac
 367
 368                local IFS=$'\n'
 369                COMPREPLY=($(compgen -P "$pfx" \
 370                        -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
 371                                | sed '/^100... blob /{
 372                                           s,^.*        ,,
 373                                           s,$, ,
 374                                       }
 375                                       /^120000 blob /{
 376                                           s,^.*        ,,
 377                                           s,$, ,
 378                                       }
 379                                       /^040000 tree /{
 380                                           s,^.*        ,,
 381                                           s,$,/,
 382                                       }
 383                                       s/^.*    //')" \
 384                        -- "$cur"))
 385                ;;
 386        *)
 387                __gitcomp "$(__git_refs)"
 388                ;;
 389        esac
 390}
 391
 392__git_complete_revlist ()
 393{
 394        local pfx cur="${COMP_WORDS[COMP_CWORD]}"
 395        case "$cur" in
 396        *...*)
 397                pfx="${cur%...*}..."
 398                cur="${cur#*...}"
 399                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 400                ;;
 401        *..*)
 402                pfx="${cur%..*}.."
 403                cur="${cur#*..}"
 404                __gitcomp "$(__git_refs)" "$pfx" "$cur"
 405                ;;
 406        *)
 407                __gitcomp "$(__git_refs)"
 408                ;;
 409        esac
 410}
 411
 412__git_complete_remote_or_refspec ()
 413{
 414        local cmd="${COMP_WORDS[1]}"
 415        local cur="${COMP_WORDS[COMP_CWORD]}"
 416        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
 417        while [ $c -lt $COMP_CWORD ]; do
 418                i="${COMP_WORDS[c]}"
 419                case "$i" in
 420                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
 421                --all)
 422                        case "$cmd" in
 423                        push) no_complete_refspec=1 ;;
 424                        fetch)
 425                                COMPREPLY=()
 426                                return
 427                                ;;
 428                        *) ;;
 429                        esac
 430                        ;;
 431                -*) ;;
 432                *) remote="$i"; break ;;
 433                esac
 434                c=$((++c))
 435        done
 436        if [ -z "$remote" ]; then
 437                __gitcomp "$(__git_remotes)"
 438                return
 439        fi
 440        if [ $no_complete_refspec = 1 ]; then
 441                COMPREPLY=()
 442                return
 443        fi
 444        [ "$remote" = "." ] && remote=
 445        case "$cur" in
 446        *:*)
 447                case "$COMP_WORDBREAKS" in
 448                *:*) : great ;;
 449                *)   pfx="${cur%%:*}:" ;;
 450                esac
 451                cur="${cur#*:}"
 452                lhs=0
 453                ;;
 454        +*)
 455                pfx="+"
 456                cur="${cur#+}"
 457                ;;
 458        esac
 459        case "$cmd" in
 460        fetch)
 461                if [ $lhs = 1 ]; then
 462                        __gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
 463                else
 464                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 465                fi
 466                ;;
 467        pull)
 468                if [ $lhs = 1 ]; then
 469                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 470                else
 471                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 472                fi
 473                ;;
 474        push)
 475                if [ $lhs = 1 ]; then
 476                        __gitcomp "$(__git_refs)" "$pfx" "$cur"
 477                else
 478                        __gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
 479                fi
 480                ;;
 481        esac
 482}
 483
 484__git_complete_strategy ()
 485{
 486        __git_compute_merge_strategies
 487        case "${COMP_WORDS[COMP_CWORD-1]}" in
 488        -s|--strategy)
 489                __gitcomp "$__git_merge_strategies"
 490                return 0
 491        esac
 492        local cur="${COMP_WORDS[COMP_CWORD]}"
 493        case "$cur" in
 494        --strategy=*)
 495                __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
 496                return 0
 497                ;;
 498        esac
 499        return 1
 500}
 501
 502__git_list_all_commands ()
 503{
 504        local i IFS=" "$'\n'
 505        for i in $(git help -a|egrep '^  [a-zA-Z0-9]')
 506        do
 507                case $i in
 508                *--*)             : helper pattern;;
 509                *) echo $i;;
 510                esac
 511        done
 512}
 513
 514__git_all_commands=
 515__git_compute_all_commands ()
 516{
 517        : ${__git_all_commands:=$(__git_list_all_commands)}
 518}
 519
 520__git_list_porcelain_commands ()
 521{
 522        local i IFS=" "$'\n'
 523        __git_compute_all_commands
 524        for i in "help" $__git_all_commands
 525        do
 526                case $i in
 527                *--*)             : helper pattern;;
 528                applymbox)        : ask gittus;;
 529                applypatch)       : ask gittus;;
 530                archimport)       : import;;
 531                cat-file)         : plumbing;;
 532                check-attr)       : plumbing;;
 533                check-ref-format) : plumbing;;
 534                checkout-index)   : plumbing;;
 535                commit-tree)      : plumbing;;
 536                count-objects)    : infrequent;;
 537                cvsexportcommit)  : export;;
 538                cvsimport)        : import;;
 539                cvsserver)        : daemon;;
 540                daemon)           : daemon;;
 541                diff-files)       : plumbing;;
 542                diff-index)       : plumbing;;
 543                diff-tree)        : plumbing;;
 544                fast-import)      : import;;
 545                fast-export)      : export;;
 546                fsck-objects)     : plumbing;;
 547                fetch-pack)       : plumbing;;
 548                fmt-merge-msg)    : plumbing;;
 549                for-each-ref)     : plumbing;;
 550                hash-object)      : plumbing;;
 551                http-*)           : transport;;
 552                index-pack)       : plumbing;;
 553                init-db)          : deprecated;;
 554                local-fetch)      : plumbing;;
 555                lost-found)       : infrequent;;
 556                ls-files)         : plumbing;;
 557                ls-remote)        : plumbing;;
 558                ls-tree)          : plumbing;;
 559                mailinfo)         : plumbing;;
 560                mailsplit)        : plumbing;;
 561                merge-*)          : plumbing;;
 562                mktree)           : plumbing;;
 563                mktag)            : plumbing;;
 564                pack-objects)     : plumbing;;
 565                pack-redundant)   : plumbing;;
 566                pack-refs)        : plumbing;;
 567                parse-remote)     : plumbing;;
 568                patch-id)         : plumbing;;
 569                peek-remote)      : plumbing;;
 570                prune)            : plumbing;;
 571                prune-packed)     : plumbing;;
 572                quiltimport)      : import;;
 573                read-tree)        : plumbing;;
 574                receive-pack)     : plumbing;;
 575                reflog)           : plumbing;;
 576                repo-config)      : deprecated;;
 577                rerere)           : plumbing;;
 578                rev-list)         : plumbing;;
 579                rev-parse)        : plumbing;;
 580                runstatus)        : plumbing;;
 581                sh-setup)         : internal;;
 582                shell)            : daemon;;
 583                show-ref)         : plumbing;;
 584                send-pack)        : plumbing;;
 585                show-index)       : plumbing;;
 586                ssh-*)            : transport;;
 587                stripspace)       : plumbing;;
 588                symbolic-ref)     : plumbing;;
 589                tar-tree)         : deprecated;;
 590                unpack-file)      : plumbing;;
 591                unpack-objects)   : plumbing;;
 592                update-index)     : plumbing;;
 593                update-ref)       : plumbing;;
 594                update-server-info) : daemon;;
 595                upload-archive)   : plumbing;;
 596                upload-pack)      : plumbing;;
 597                write-tree)       : plumbing;;
 598                var)              : infrequent;;
 599                verify-pack)      : infrequent;;
 600                verify-tag)       : plumbing;;
 601                *) echo $i;;
 602                esac
 603        done
 604}
 605
 606__git_porcelain_commands=
 607__git_compute_porcelain_commands ()
 608{
 609        __git_compute_all_commands
 610        : ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
 611}
 612
 613__git_aliases ()
 614{
 615        local i IFS=$'\n'
 616        for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
 617                case "$i" in
 618                alias.*)
 619                        i="${i#alias.}"
 620                        echo "${i/ */}"
 621                        ;;
 622                esac
 623        done
 624}
 625
 626# __git_aliased_command requires 1 argument
 627__git_aliased_command ()
 628{
 629        local word cmdline=$(git --git-dir="$(__gitdir)" \
 630                config --get "alias.$1")
 631        for word in $cmdline; do
 632                if [ "${word##-*}" ]; then
 633                        echo $word
 634                        return
 635                fi
 636        done
 637}
 638
 639# __git_find_on_cmdline requires 1 argument
 640__git_find_on_cmdline ()
 641{
 642        local word subcommand c=1
 643
 644        while [ $c -lt $COMP_CWORD ]; do
 645                word="${COMP_WORDS[c]}"
 646                for subcommand in $1; do
 647                        if [ "$subcommand" = "$word" ]; then
 648                                echo "$subcommand"
 649                                return
 650                        fi
 651                done
 652                c=$((++c))
 653        done
 654}
 655
 656__git_has_doubledash ()
 657{
 658        local c=1
 659        while [ $c -lt $COMP_CWORD ]; do
 660                if [ "--" = "${COMP_WORDS[c]}" ]; then
 661                        return 0
 662                fi
 663                c=$((++c))
 664        done
 665        return 1
 666}
 667
 668__git_whitespacelist="nowarn warn error error-all fix"
 669
 670_git_am ()
 671{
 672        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
 673        if [ -d "$dir"/rebase-apply ]; then
 674                __gitcomp "--skip --resolved --abort"
 675                return
 676        fi
 677        case "$cur" in
 678        --whitespace=*)
 679                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 680                return
 681                ;;
 682        --*)
 683                __gitcomp "
 684                        --3way --committer-date-is-author-date --ignore-date
 685                        --ignore-whitespace --ignore-space-change
 686                        --interactive --keep --no-utf8 --signoff --utf8
 687                        --whitespace= --scissors
 688                        "
 689                return
 690        esac
 691        COMPREPLY=()
 692}
 693
 694_git_apply ()
 695{
 696        local cur="${COMP_WORDS[COMP_CWORD]}"
 697        case "$cur" in
 698        --whitespace=*)
 699                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
 700                return
 701                ;;
 702        --*)
 703                __gitcomp "
 704                        --stat --numstat --summary --check --index
 705                        --cached --index-info --reverse --reject --unidiff-zero
 706                        --apply --no-add --exclude=
 707                        --ignore-whitespace --ignore-space-change
 708                        --whitespace= --inaccurate-eof --verbose
 709                        "
 710                return
 711        esac
 712        COMPREPLY=()
 713}
 714
 715_git_add ()
 716{
 717        __git_has_doubledash && return
 718
 719        local cur="${COMP_WORDS[COMP_CWORD]}"
 720        case "$cur" in
 721        --*)
 722                __gitcomp "
 723                        --interactive --refresh --patch --update --dry-run
 724                        --ignore-errors --intent-to-add
 725                        "
 726                return
 727        esac
 728        COMPREPLY=()
 729}
 730
 731_git_archive ()
 732{
 733        local cur="${COMP_WORDS[COMP_CWORD]}"
 734        case "$cur" in
 735        --format=*)
 736                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
 737                return
 738                ;;
 739        --remote=*)
 740                __gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
 741                return
 742                ;;
 743        --*)
 744                __gitcomp "
 745                        --format= --list --verbose
 746                        --prefix= --remote= --exec=
 747                        "
 748                return
 749                ;;
 750        esac
 751        __git_complete_file
 752}
 753
 754_git_bisect ()
 755{
 756        __git_has_doubledash && return
 757
 758        local subcommands="start bad good skip reset visualize replay log run"
 759        local subcommand="$(__git_find_on_cmdline "$subcommands")"
 760        if [ -z "$subcommand" ]; then
 761                __gitcomp "$subcommands"
 762                return
 763        fi
 764
 765        case "$subcommand" in
 766        bad|good|reset|skip)
 767                __gitcomp "$(__git_refs)"
 768                ;;
 769        *)
 770                COMPREPLY=()
 771                ;;
 772        esac
 773}
 774
 775_git_branch ()
 776{
 777        local i c=1 only_local_ref="n" has_r="n"
 778
 779        while [ $c -lt $COMP_CWORD ]; do
 780                i="${COMP_WORDS[c]}"
 781                case "$i" in
 782                -d|-m)  only_local_ref="y" ;;
 783                -r)     has_r="y" ;;
 784                esac
 785                c=$((++c))
 786        done
 787
 788        case "${COMP_WORDS[COMP_CWORD]}" in
 789        --*)
 790                __gitcomp "
 791                        --color --no-color --verbose --abbrev= --no-abbrev
 792                        --track --no-track --contains --merged --no-merged
 793                        "
 794                ;;
 795        *)
 796                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 797                        __gitcomp "$(__git_heads)"
 798                else
 799                        __gitcomp "$(__git_refs)"
 800                fi
 801                ;;
 802        esac
 803}
 804
 805_git_bundle ()
 806{
 807        local cmd="${COMP_WORDS[2]}"
 808        case "$COMP_CWORD" in
 809        2)
 810                __gitcomp "create list-heads verify unbundle"
 811                ;;
 812        3)
 813                # looking for a file
 814                ;;
 815        *)
 816                case "$cmd" in
 817                        create)
 818                                __git_complete_revlist
 819                        ;;
 820                esac
 821                ;;
 822        esac
 823}
 824
 825_git_checkout ()
 826{
 827        __git_has_doubledash && return
 828
 829        local cur="${COMP_WORDS[COMP_CWORD]}"
 830        case "$cur" in
 831        --conflict=*)
 832                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
 833                ;;
 834        --*)
 835                __gitcomp "
 836                        --quiet --ours --theirs --track --no-track --merge
 837                        --conflict= --patch
 838                        "
 839                ;;
 840        *)
 841                __gitcomp "$(__git_refs)"
 842                ;;
 843        esac
 844}
 845
 846_git_cherry ()
 847{
 848        __gitcomp "$(__git_refs)"
 849}
 850
 851_git_cherry_pick ()
 852{
 853        local cur="${COMP_WORDS[COMP_CWORD]}"
 854        case "$cur" in
 855        --*)
 856                __gitcomp "--edit --no-commit"
 857                ;;
 858        *)
 859                __gitcomp "$(__git_refs)"
 860                ;;
 861        esac
 862}
 863
 864_git_clean ()
 865{
 866        __git_has_doubledash && return
 867
 868        local cur="${COMP_WORDS[COMP_CWORD]}"
 869        case "$cur" in
 870        --*)
 871                __gitcomp "--dry-run --quiet"
 872                return
 873                ;;
 874        esac
 875        COMPREPLY=()
 876}
 877
 878_git_clone ()
 879{
 880        local cur="${COMP_WORDS[COMP_CWORD]}"
 881        case "$cur" in
 882        --*)
 883                __gitcomp "
 884                        --local
 885                        --no-hardlinks
 886                        --shared
 887                        --reference
 888                        --quiet
 889                        --no-checkout
 890                        --bare
 891                        --mirror
 892                        --origin
 893                        --upload-pack
 894                        --template=
 895                        --depth
 896                        "
 897                return
 898                ;;
 899        esac
 900        COMPREPLY=()
 901}
 902
 903_git_commit ()
 904{
 905        __git_has_doubledash && return
 906
 907        local cur="${COMP_WORDS[COMP_CWORD]}"
 908        case "$cur" in
 909        --cleanup=*)
 910                __gitcomp "default strip verbatim whitespace
 911                        " "" "${cur##--cleanup=}"
 912                return
 913                ;;
 914        --reuse-message=*)
 915                __gitcomp "$(__git_refs)" "" "${cur##--reuse-message=}"
 916                return
 917                ;;
 918        --reedit-message=*)
 919                __gitcomp "$(__git_refs)" "" "${cur##--reedit-message=}"
 920                return
 921                ;;
 922        --untracked-files=*)
 923                __gitcomp "all no normal" "" "${cur##--untracked-files=}"
 924                return
 925                ;;
 926        --*)
 927                __gitcomp "
 928                        --all --author= --signoff --verify --no-verify
 929                        --edit --amend --include --only --interactive
 930                        --dry-run --reuse-message= --reedit-message=
 931                        --reset-author --file= --message= --template=
 932                        --cleanup= --untracked-files --untracked-files=
 933                        --verbose --quiet
 934                        "
 935                return
 936        esac
 937        COMPREPLY=()
 938}
 939
 940_git_describe ()
 941{
 942        local cur="${COMP_WORDS[COMP_CWORD]}"
 943        case "$cur" in
 944        --*)
 945                __gitcomp "
 946                        --all --tags --contains --abbrev= --candidates=
 947                        --exact-match --debug --long --match --always
 948                        "
 949                return
 950        esac
 951        __gitcomp "$(__git_refs)"
 952}
 953
 954__git_diff_common_options="--stat --numstat --shortstat --summary
 955                        --patch-with-stat --name-only --name-status --color
 956                        --no-color --color-words --no-renames --check
 957                        --full-index --binary --abbrev --diff-filter=
 958                        --find-copies-harder
 959                        --text --ignore-space-at-eol --ignore-space-change
 960                        --ignore-all-space --exit-code --quiet --ext-diff
 961                        --no-ext-diff
 962                        --no-prefix --src-prefix= --dst-prefix=
 963                        --inter-hunk-context=
 964                        --patience
 965                        --raw
 966                        --dirstat --dirstat= --dirstat-by-file
 967                        --dirstat-by-file= --cumulative
 968"
 969
 970_git_diff ()
 971{
 972        __git_has_doubledash && return
 973
 974        local cur="${COMP_WORDS[COMP_CWORD]}"
 975        case "$cur" in
 976        --*)
 977                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
 978                        --base --ours --theirs
 979                        $__git_diff_common_options
 980                        "
 981                return
 982                ;;
 983        esac
 984        __git_complete_file
 985}
 986
 987__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
 988                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge
 989"
 990
 991_git_difftool ()
 992{
 993        __git_has_doubledash && return
 994
 995        local cur="${COMP_WORDS[COMP_CWORD]}"
 996        case "$cur" in
 997        --tool=*)
 998                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
 999                return
1000                ;;
1001        --*)
1002                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1003                        --base --ours --theirs
1004                        --no-renames --diff-filter= --find-copies-harder
1005                        --relative --ignore-submodules
1006                        --tool="
1007                return
1008                ;;
1009        esac
1010        __git_complete_file
1011}
1012
1013__git_fetch_options="
1014        --quiet --verbose --append --upload-pack --force --keep --depth=
1015        --tags --no-tags --all --prune --dry-run
1016"
1017
1018_git_fetch ()
1019{
1020        local cur="${COMP_WORDS[COMP_CWORD]}"
1021        case "$cur" in
1022        --*)
1023                __gitcomp "$__git_fetch_options"
1024                return
1025                ;;
1026        esac
1027        __git_complete_remote_or_refspec
1028}
1029
1030_git_format_patch ()
1031{
1032        local cur="${COMP_WORDS[COMP_CWORD]}"
1033        case "$cur" in
1034        --thread=*)
1035                __gitcomp "
1036                        deep shallow
1037                        " "" "${cur##--thread=}"
1038                return
1039                ;;
1040        --*)
1041                __gitcomp "
1042                        --stdout --attach --no-attach --thread --thread=
1043                        --output-directory
1044                        --numbered --start-number
1045                        --numbered-files
1046                        --keep-subject
1047                        --signoff
1048                        --in-reply-to= --cc=
1049                        --full-index --binary
1050                        --not --all
1051                        --cover-letter
1052                        --no-prefix --src-prefix= --dst-prefix=
1053                        --inline --suffix= --ignore-if-in-upstream
1054                        --subject-prefix=
1055                        "
1056                return
1057                ;;
1058        esac
1059        __git_complete_revlist
1060}
1061
1062_git_fsck ()
1063{
1064        local cur="${COMP_WORDS[COMP_CWORD]}"
1065        case "$cur" in
1066        --*)
1067                __gitcomp "
1068                        --tags --root --unreachable --cache --no-reflogs --full
1069                        --strict --verbose --lost-found
1070                        "
1071                return
1072                ;;
1073        esac
1074        COMPREPLY=()
1075}
1076
1077_git_gc ()
1078{
1079        local cur="${COMP_WORDS[COMP_CWORD]}"
1080        case "$cur" in
1081        --*)
1082                __gitcomp "--prune --aggressive"
1083                return
1084                ;;
1085        esac
1086        COMPREPLY=()
1087}
1088
1089_git_grep ()
1090{
1091        __git_has_doubledash && return
1092
1093        local cur="${COMP_WORDS[COMP_CWORD]}"
1094        case "$cur" in
1095        --*)
1096                __gitcomp "
1097                        --cached
1098                        --text --ignore-case --word-regexp --invert-match
1099                        --full-name
1100                        --extended-regexp --basic-regexp --fixed-strings
1101                        --files-with-matches --name-only
1102                        --files-without-match
1103                        --max-depth
1104                        --count
1105                        --and --or --not --all-match
1106                        "
1107                return
1108                ;;
1109        esac
1110
1111        __gitcomp "$(__git_refs)"
1112}
1113
1114_git_help ()
1115{
1116        local cur="${COMP_WORDS[COMP_CWORD]}"
1117        case "$cur" in
1118        --*)
1119                __gitcomp "--all --info --man --web"
1120                return
1121                ;;
1122        esac
1123        __git_compute_all_commands
1124        __gitcomp "$__git_all_commands
1125                attributes cli core-tutorial cvs-migration
1126                diffcore gitk glossary hooks ignore modules
1127                repository-layout tutorial tutorial-2
1128                workflows
1129                "
1130}
1131
1132_git_init ()
1133{
1134        local cur="${COMP_WORDS[COMP_CWORD]}"
1135        case "$cur" in
1136        --shared=*)
1137                __gitcomp "
1138                        false true umask group all world everybody
1139                        " "" "${cur##--shared=}"
1140                return
1141                ;;
1142        --*)
1143                __gitcomp "--quiet --bare --template= --shared --shared="
1144                return
1145                ;;
1146        esac
1147        COMPREPLY=()
1148}
1149
1150_git_ls_files ()
1151{
1152        __git_has_doubledash && return
1153
1154        local cur="${COMP_WORDS[COMP_CWORD]}"
1155        case "$cur" in
1156        --*)
1157                __gitcomp "--cached --deleted --modified --others --ignored
1158                        --stage --directory --no-empty-directory --unmerged
1159                        --killed --exclude= --exclude-from=
1160                        --exclude-per-directory= --exclude-standard
1161                        --error-unmatch --with-tree= --full-name
1162                        --abbrev --ignored --exclude-per-directory
1163                        "
1164                return
1165                ;;
1166        esac
1167        COMPREPLY=()
1168}
1169
1170_git_ls_remote ()
1171{
1172        __gitcomp "$(__git_remotes)"
1173}
1174
1175_git_ls_tree ()
1176{
1177        __git_complete_file
1178}
1179
1180# Options that go well for log, shortlog and gitk
1181__git_log_common_options="
1182        --not --all
1183        --branches --tags --remotes
1184        --first-parent --merges --no-merges
1185        --max-count=
1186        --max-age= --since= --after=
1187        --min-age= --until= --before=
1188"
1189# Options that go well for log and gitk (not shortlog)
1190__git_log_gitk_options="
1191        --dense --sparse --full-history
1192        --simplify-merges --simplify-by-decoration
1193        --left-right
1194"
1195# Options that go well for log and shortlog (not gitk)
1196__git_log_shortlog_options="
1197        --author= --committer= --grep=
1198        --all-match
1199"
1200
1201__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1202__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1203
1204_git_log ()
1205{
1206        __git_has_doubledash && return
1207
1208        local cur="${COMP_WORDS[COMP_CWORD]}"
1209        local g="$(git rev-parse --git-dir 2>/dev/null)"
1210        local merge=""
1211        if [ -f "$g/MERGE_HEAD" ]; then
1212                merge="--merge"
1213        fi
1214        case "$cur" in
1215        --pretty=*)
1216                __gitcomp "$__git_log_pretty_formats
1217                        " "" "${cur##--pretty=}"
1218                return
1219                ;;
1220        --format=*)
1221                __gitcomp "$__git_log_pretty_formats
1222                        " "" "${cur##--format=}"
1223                return
1224                ;;
1225        --date=*)
1226                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1227                return
1228                ;;
1229        --decorate=*)
1230                __gitcomp "long short" "" "${cur##--decorate=}"
1231                return
1232                ;;
1233        --*)
1234                __gitcomp "
1235                        $__git_log_common_options
1236                        $__git_log_shortlog_options
1237                        $__git_log_gitk_options
1238                        --root --topo-order --date-order --reverse
1239                        --follow --full-diff
1240                        --abbrev-commit --abbrev=
1241                        --relative-date --date=
1242                        --pretty= --format= --oneline
1243                        --cherry-pick
1244                        --graph
1245                        --decorate --decorate=
1246                        --walk-reflogs
1247                        --parents --children
1248                        $merge
1249                        $__git_diff_common_options
1250                        --pickaxe-all --pickaxe-regex
1251                        "
1252                return
1253                ;;
1254        esac
1255        __git_complete_revlist
1256}
1257
1258__git_merge_options="
1259        --no-commit --no-stat --log --no-log --squash --strategy
1260        --commit --stat --no-squash --ff --no-ff --ff-only
1261"
1262
1263_git_merge ()
1264{
1265        __git_complete_strategy && return
1266
1267        local cur="${COMP_WORDS[COMP_CWORD]}"
1268        case "$cur" in
1269        --*)
1270                __gitcomp "$__git_merge_options"
1271                return
1272        esac
1273        __gitcomp "$(__git_refs)"
1274}
1275
1276_git_mergetool ()
1277{
1278        local cur="${COMP_WORDS[COMP_CWORD]}"
1279        case "$cur" in
1280        --tool=*)
1281                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1282                return
1283                ;;
1284        --*)
1285                __gitcomp "--tool="
1286                return
1287                ;;
1288        esac
1289        COMPREPLY=()
1290}
1291
1292_git_merge_base ()
1293{
1294        __gitcomp "$(__git_refs)"
1295}
1296
1297_git_mv ()
1298{
1299        local cur="${COMP_WORDS[COMP_CWORD]}"
1300        case "$cur" in
1301        --*)
1302                __gitcomp "--dry-run"
1303                return
1304                ;;
1305        esac
1306        COMPREPLY=()
1307}
1308
1309_git_name_rev ()
1310{
1311        __gitcomp "--tags --all --stdin"
1312}
1313
1314_git_pull ()
1315{
1316        __git_complete_strategy && return
1317
1318        local cur="${COMP_WORDS[COMP_CWORD]}"
1319        case "$cur" in
1320        --*)
1321                __gitcomp "
1322                        --rebase --no-rebase
1323                        $__git_merge_options
1324                        $__git_fetch_options
1325                "
1326                return
1327                ;;
1328        esac
1329        __git_complete_remote_or_refspec
1330}
1331
1332_git_push ()
1333{
1334        local cur="${COMP_WORDS[COMP_CWORD]}"
1335        case "${COMP_WORDS[COMP_CWORD-1]}" in
1336        --repo)
1337                __gitcomp "$(__git_remotes)"
1338                return
1339        esac
1340        case "$cur" in
1341        --repo=*)
1342                __gitcomp "$(__git_remotes)" "" "${cur##--repo=}"
1343                return
1344                ;;
1345        --*)
1346                __gitcomp "
1347                        --all --mirror --tags --dry-run --force --verbose
1348                        --receive-pack= --repo=
1349                "
1350                return
1351                ;;
1352        esac
1353        __git_complete_remote_or_refspec
1354}
1355
1356_git_rebase ()
1357{
1358        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1359        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1360                __gitcomp "--continue --skip --abort"
1361                return
1362        fi
1363        __git_complete_strategy && return
1364        case "$cur" in
1365        --whitespace=*)
1366                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1367                return
1368                ;;
1369        --*)
1370                __gitcomp "
1371                        --onto --merge --strategy --interactive
1372                        --preserve-merges --stat --no-stat
1373                        --committer-date-is-author-date --ignore-date
1374                        --ignore-whitespace --whitespace=
1375                        "
1376
1377                return
1378        esac
1379        __gitcomp "$(__git_refs)"
1380}
1381
1382__git_send_email_confirm_options="always never auto cc compose"
1383__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1384
1385_git_send_email ()
1386{
1387        local cur="${COMP_WORDS[COMP_CWORD]}"
1388        case "$cur" in
1389        --confirm=*)
1390                __gitcomp "
1391                        $__git_send_email_confirm_options
1392                        " "" "${cur##--confirm=}"
1393                return
1394                ;;
1395        --suppress-cc=*)
1396                __gitcomp "
1397                        $__git_send_email_suppresscc_options
1398                        " "" "${cur##--suppress-cc=}"
1399
1400                return
1401                ;;
1402        --smtp-encryption=*)
1403                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1404                return
1405                ;;
1406        --*)
1407                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1408                        --compose --confirm= --dry-run --envelope-sender
1409                        --from --identity
1410                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1411                        --no-suppress-from --no-thread --quiet
1412                        --signed-off-by-cc --smtp-pass --smtp-server
1413                        --smtp-server-port --smtp-encryption= --smtp-user
1414                        --subject --suppress-cc= --suppress-from --thread --to
1415                        --validate --no-validate"
1416                return
1417                ;;
1418        esac
1419        COMPREPLY=()
1420}
1421
1422__git_config_get_set_variables ()
1423{
1424        local prevword word config_file= c=$COMP_CWORD
1425        while [ $c -gt 1 ]; do
1426                word="${COMP_WORDS[c]}"
1427                case "$word" in
1428                --global|--system|--file=*)
1429                        config_file="$word"
1430                        break
1431                        ;;
1432                -f|--file)
1433                        config_file="$word $prevword"
1434                        break
1435                        ;;
1436                esac
1437                prevword=$word
1438                c=$((--c))
1439        done
1440
1441        git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1442        while read line
1443        do
1444                case "$line" in
1445                *.*=*)
1446                        echo "${line/=*/}"
1447                        ;;
1448                esac
1449        done
1450}
1451
1452_git_config ()
1453{
1454        local cur="${COMP_WORDS[COMP_CWORD]}"
1455        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1456        case "$prv" in
1457        branch.*.remote)
1458                __gitcomp "$(__git_remotes)"
1459                return
1460                ;;
1461        branch.*.merge)
1462                __gitcomp "$(__git_refs)"
1463                return
1464                ;;
1465        remote.*.fetch)
1466                local remote="${prv#remote.}"
1467                remote="${remote%.fetch}"
1468                __gitcomp "$(__git_refs_remotes "$remote")"
1469                return
1470                ;;
1471        remote.*.push)
1472                local remote="${prv#remote.}"
1473                remote="${remote%.push}"
1474                __gitcomp "$(git --git-dir="$(__gitdir)" \
1475                        for-each-ref --format='%(refname):%(refname)' \
1476                        refs/heads)"
1477                return
1478                ;;
1479        pull.twohead|pull.octopus)
1480                __git_compute_merge_strategies
1481                __gitcomp "$__git_merge_strategies"
1482                return
1483                ;;
1484        color.branch|color.diff|color.interactive|\
1485        color.showbranch|color.status|color.ui)
1486                __gitcomp "always never auto"
1487                return
1488                ;;
1489        color.pager)
1490                __gitcomp "false true"
1491                return
1492                ;;
1493        color.*.*)
1494                __gitcomp "
1495                        normal black red green yellow blue magenta cyan white
1496                        bold dim ul blink reverse
1497                        "
1498                return
1499                ;;
1500        help.format)
1501                __gitcomp "man info web html"
1502                return
1503                ;;
1504        log.date)
1505                __gitcomp "$__git_log_date_formats"
1506                return
1507                ;;
1508        sendemail.aliasesfiletype)
1509                __gitcomp "mutt mailrc pine elm gnus"
1510                return
1511                ;;
1512        sendemail.confirm)
1513                __gitcomp "$__git_send_email_confirm_options"
1514                return
1515                ;;
1516        sendemail.suppresscc)
1517                __gitcomp "$__git_send_email_suppresscc_options"
1518                return
1519                ;;
1520        --get|--get-all|--unset|--unset-all)
1521                __gitcomp "$(__git_config_get_set_variables)"
1522                return
1523                ;;
1524        *.*)
1525                COMPREPLY=()
1526                return
1527                ;;
1528        esac
1529        case "$cur" in
1530        --*)
1531                __gitcomp "
1532                        --global --system --file=
1533                        --list --replace-all
1534                        --get --get-all --get-regexp
1535                        --add --unset --unset-all
1536                        --remove-section --rename-section
1537                        "
1538                return
1539                ;;
1540        branch.*.*)
1541                local pfx="${cur%.*}."
1542                cur="${cur##*.}"
1543                __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur"
1544                return
1545                ;;
1546        branch.*)
1547                local pfx="${cur%.*}."
1548                cur="${cur#*.}"
1549                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1550                return
1551                ;;
1552        guitool.*.*)
1553                local pfx="${cur%.*}."
1554                cur="${cur##*.}"
1555                __gitcomp "
1556                        argprompt cmd confirm needsfile noconsole norescan
1557                        prompt revprompt revunmerged title
1558                        " "$pfx" "$cur"
1559                return
1560                ;;
1561        difftool.*.*)
1562                local pfx="${cur%.*}."
1563                cur="${cur##*.}"
1564                __gitcomp "cmd path" "$pfx" "$cur"
1565                return
1566                ;;
1567        man.*.*)
1568                local pfx="${cur%.*}."
1569                cur="${cur##*.}"
1570                __gitcomp "cmd path" "$pfx" "$cur"
1571                return
1572                ;;
1573        mergetool.*.*)
1574                local pfx="${cur%.*}."
1575                cur="${cur##*.}"
1576                __gitcomp "cmd path trustExitCode" "$pfx" "$cur"
1577                return
1578                ;;
1579        pager.*)
1580                local pfx="${cur%.*}."
1581                cur="${cur#*.}"
1582                __git_compute_all_commands
1583                __gitcomp "$__git_all_commands" "$pfx" "$cur"
1584                return
1585                ;;
1586        remote.*.*)
1587                local pfx="${cur%.*}."
1588                cur="${cur##*.}"
1589                __gitcomp "
1590                        url proxy fetch push mirror skipDefaultUpdate
1591                        receivepack uploadpack tagopt pushurl
1592                        " "$pfx" "$cur"
1593                return
1594                ;;
1595        remote.*)
1596                local pfx="${cur%.*}."
1597                cur="${cur#*.}"
1598                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1599                return
1600                ;;
1601        url.*.*)
1602                local pfx="${cur%.*}."
1603                cur="${cur##*.}"
1604                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur"
1605                return
1606                ;;
1607        esac
1608        __gitcomp "
1609                add.ignore-errors
1610                alias.
1611                apply.ignorewhitespace
1612                apply.whitespace
1613                branch.autosetupmerge
1614                branch.autosetuprebase
1615                clean.requireForce
1616                color.branch
1617                color.branch.current
1618                color.branch.local
1619                color.branch.plain
1620                color.branch.remote
1621                color.diff
1622                color.diff.commit
1623                color.diff.frag
1624                color.diff.meta
1625                color.diff.new
1626                color.diff.old
1627                color.diff.plain
1628                color.diff.whitespace
1629                color.grep
1630                color.grep.external
1631                color.grep.match
1632                color.interactive
1633                color.interactive.header
1634                color.interactive.help
1635                color.interactive.prompt
1636                color.pager
1637                color.showbranch
1638                color.status
1639                color.status.added
1640                color.status.changed
1641                color.status.header
1642                color.status.nobranch
1643                color.status.untracked
1644                color.status.updated
1645                color.ui
1646                commit.template
1647                core.autocrlf
1648                core.bare
1649                core.compression
1650                core.createObject
1651                core.deltaBaseCacheLimit
1652                core.editor
1653                core.excludesfile
1654                core.fileMode
1655                core.fsyncobjectfiles
1656                core.gitProxy
1657                core.ignoreCygwinFSTricks
1658                core.ignoreStat
1659                core.logAllRefUpdates
1660                core.loosecompression
1661                core.packedGitLimit
1662                core.packedGitWindowSize
1663                core.pager
1664                core.preferSymlinkRefs
1665                core.preloadindex
1666                core.quotepath
1667                core.repositoryFormatVersion
1668                core.safecrlf
1669                core.sharedRepository
1670                core.symlinks
1671                core.trustctime
1672                core.warnAmbiguousRefs
1673                core.whitespace
1674                core.worktree
1675                diff.autorefreshindex
1676                diff.external
1677                diff.mnemonicprefix
1678                diff.renameLimit
1679                diff.renameLimit.
1680                diff.renames
1681                diff.suppressBlankEmpty
1682                diff.tool
1683                diff.wordRegex
1684                difftool.
1685                difftool.prompt
1686                fetch.unpackLimit
1687                format.attach
1688                format.cc
1689                format.headers
1690                format.numbered
1691                format.pretty
1692                format.signoff
1693                format.subjectprefix
1694                format.suffix
1695                format.thread
1696                gc.aggressiveWindow
1697                gc.auto
1698                gc.autopacklimit
1699                gc.packrefs
1700                gc.pruneexpire
1701                gc.reflogexpire
1702                gc.reflogexpireunreachable
1703                gc.rerereresolved
1704                gc.rerereunresolved
1705                gitcvs.allbinary
1706                gitcvs.commitmsgannotation
1707                gitcvs.dbTableNamePrefix
1708                gitcvs.dbdriver
1709                gitcvs.dbname
1710                gitcvs.dbpass
1711                gitcvs.dbuser
1712                gitcvs.enabled
1713                gitcvs.logfile
1714                gitcvs.usecrlfattr
1715                guitool.
1716                gui.blamehistoryctx
1717                gui.commitmsgwidth
1718                gui.copyblamethreshold
1719                gui.diffcontext
1720                gui.encoding
1721                gui.fastcopyblame
1722                gui.matchtrackingbranch
1723                gui.newbranchtemplate
1724                gui.pruneduringfetch
1725                gui.spellingdictionary
1726                gui.trustmtime
1727                help.autocorrect
1728                help.browser
1729                help.format
1730                http.lowSpeedLimit
1731                http.lowSpeedTime
1732                http.maxRequests
1733                http.noEPSV
1734                http.proxy
1735                http.sslCAInfo
1736                http.sslCAPath
1737                http.sslCert
1738                http.sslKey
1739                http.sslVerify
1740                i18n.commitEncoding
1741                i18n.logOutputEncoding
1742                imap.folder
1743                imap.host
1744                imap.pass
1745                imap.port
1746                imap.preformattedHTML
1747                imap.sslverify
1748                imap.tunnel
1749                imap.user
1750                instaweb.browser
1751                instaweb.httpd
1752                instaweb.local
1753                instaweb.modulepath
1754                instaweb.port
1755                interactive.singlekey
1756                log.date
1757                log.showroot
1758                mailmap.file
1759                man.
1760                man.viewer
1761                merge.conflictstyle
1762                merge.log
1763                merge.renameLimit
1764                merge.stat
1765                merge.tool
1766                merge.verbosity
1767                mergetool.
1768                mergetool.keepBackup
1769                mergetool.prompt
1770                pack.compression
1771                pack.deltaCacheLimit
1772                pack.deltaCacheSize
1773                pack.depth
1774                pack.indexVersion
1775                pack.packSizeLimit
1776                pack.threads
1777                pack.window
1778                pack.windowMemory
1779                pager.
1780                pull.octopus
1781                pull.twohead
1782                push.default
1783                rebase.stat
1784                receive.denyCurrentBranch
1785                receive.denyDeletes
1786                receive.denyNonFastForwards
1787                receive.fsckObjects
1788                receive.unpackLimit
1789                repack.usedeltabaseoffset
1790                rerere.autoupdate
1791                rerere.enabled
1792                sendemail.aliasesfile
1793                sendemail.aliasesfiletype
1794                sendemail.bcc
1795                sendemail.cc
1796                sendemail.cccmd
1797                sendemail.chainreplyto
1798                sendemail.confirm
1799                sendemail.envelopesender
1800                sendemail.multiedit
1801                sendemail.signedoffbycc
1802                sendemail.smtpencryption
1803                sendemail.smtppass
1804                sendemail.smtpserver
1805                sendemail.smtpserverport
1806                sendemail.smtpuser
1807                sendemail.suppresscc
1808                sendemail.suppressfrom
1809                sendemail.thread
1810                sendemail.to
1811                sendemail.validate
1812                showbranch.default
1813                status.relativePaths
1814                status.showUntrackedFiles
1815                tar.umask
1816                transfer.unpackLimit
1817                url.
1818                user.email
1819                user.name
1820                user.signingkey
1821                web.browser
1822                branch. remote.
1823        "
1824}
1825
1826_git_remote ()
1827{
1828        local subcommands="add rename rm show prune update set-head"
1829        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1830        if [ -z "$subcommand" ]; then
1831                __gitcomp "$subcommands"
1832                return
1833        fi
1834
1835        case "$subcommand" in
1836        rename|rm|show|prune)
1837                __gitcomp "$(__git_remotes)"
1838                ;;
1839        update)
1840                local i c='' IFS=$'\n'
1841                for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
1842                        i="${i#remotes.}"
1843                        c="$c ${i/ */}"
1844                done
1845                __gitcomp "$c"
1846                ;;
1847        *)
1848                COMPREPLY=()
1849                ;;
1850        esac
1851}
1852
1853_git_replace ()
1854{
1855        __gitcomp "$(__git_refs)"
1856}
1857
1858_git_reset ()
1859{
1860        __git_has_doubledash && return
1861
1862        local cur="${COMP_WORDS[COMP_CWORD]}"
1863        case "$cur" in
1864        --*)
1865                __gitcomp "--merge --mixed --hard --soft --patch"
1866                return
1867                ;;
1868        esac
1869        __gitcomp "$(__git_refs)"
1870}
1871
1872_git_revert ()
1873{
1874        local cur="${COMP_WORDS[COMP_CWORD]}"
1875        case "$cur" in
1876        --*)
1877                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1878                return
1879                ;;
1880        esac
1881        __gitcomp "$(__git_refs)"
1882}
1883
1884_git_rm ()
1885{
1886        __git_has_doubledash && return
1887
1888        local cur="${COMP_WORDS[COMP_CWORD]}"
1889        case "$cur" in
1890        --*)
1891                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1892                return
1893                ;;
1894        esac
1895        COMPREPLY=()
1896}
1897
1898_git_shortlog ()
1899{
1900        __git_has_doubledash && return
1901
1902        local cur="${COMP_WORDS[COMP_CWORD]}"
1903        case "$cur" in
1904        --*)
1905                __gitcomp "
1906                        $__git_log_common_options
1907                        $__git_log_shortlog_options
1908                        --numbered --summary
1909                        "
1910                return
1911                ;;
1912        esac
1913        __git_complete_revlist
1914}
1915
1916_git_show ()
1917{
1918        __git_has_doubledash && return
1919
1920        local cur="${COMP_WORDS[COMP_CWORD]}"
1921        case "$cur" in
1922        --pretty=*)
1923                __gitcomp "$__git_log_pretty_formats
1924                        " "" "${cur##--pretty=}"
1925                return
1926                ;;
1927        --format=*)
1928                __gitcomp "$__git_log_pretty_formats
1929                        " "" "${cur##--format=}"
1930                return
1931                ;;
1932        --*)
1933                __gitcomp "--pretty= --format= --abbrev-commit --oneline
1934                        $__git_diff_common_options
1935                        "
1936                return
1937                ;;
1938        esac
1939        __git_complete_file
1940}
1941
1942_git_show_branch ()
1943{
1944        local cur="${COMP_WORDS[COMP_CWORD]}"
1945        case "$cur" in
1946        --*)
1947                __gitcomp "
1948                        --all --remotes --topo-order --current --more=
1949                        --list --independent --merge-base --no-name
1950                        --color --no-color
1951                        --sha1-name --sparse --topics --reflog
1952                        "
1953                return
1954                ;;
1955        esac
1956        __git_complete_revlist
1957}
1958
1959_git_stash ()
1960{
1961        local cur="${COMP_WORDS[COMP_CWORD]}"
1962        local save_opts='--keep-index --no-keep-index --quiet --patch'
1963        local subcommands='save list show apply clear drop pop create branch'
1964        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1965        if [ -z "$subcommand" ]; then
1966                case "$cur" in
1967                --*)
1968                        __gitcomp "$save_opts"
1969                        ;;
1970                *)
1971                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
1972                                __gitcomp "$subcommands"
1973                        else
1974                                COMPREPLY=()
1975                        fi
1976                        ;;
1977                esac
1978        else
1979                case "$subcommand,$cur" in
1980                save,--*)
1981                        __gitcomp "$save_opts"
1982                        ;;
1983                apply,--*|pop,--*)
1984                        __gitcomp "--index --quiet"
1985                        ;;
1986                show,--*|drop,--*|branch,--*)
1987                        COMPREPLY=()
1988                        ;;
1989                show,*|apply,*|drop,*|pop,*|branch,*)
1990                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
1991                                        | sed -n -e 's/:.*//p')"
1992                        ;;
1993                *)
1994                        COMPREPLY=()
1995                        ;;
1996                esac
1997        fi
1998}
1999
2000_git_submodule ()
2001{
2002        __git_has_doubledash && return
2003
2004        local subcommands="add status init update summary foreach sync"
2005        if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2006                local cur="${COMP_WORDS[COMP_CWORD]}"
2007                case "$cur" in
2008                --*)
2009                        __gitcomp "--quiet --cached"
2010                        ;;
2011                *)
2012                        __gitcomp "$subcommands"
2013                        ;;
2014                esac
2015                return
2016        fi
2017}
2018
2019_git_svn ()
2020{
2021        local subcommands="
2022                init fetch clone rebase dcommit log find-rev
2023                set-tree commit-diff info create-ignore propget
2024                proplist show-ignore show-externals branch tag blame
2025                migrate
2026                "
2027        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2028        if [ -z "$subcommand" ]; then
2029                __gitcomp "$subcommands"
2030        else
2031                local remote_opts="--username= --config-dir= --no-auth-cache"
2032                local fc_opts="
2033                        --follow-parent --authors-file= --repack=
2034                        --no-metadata --use-svm-props --use-svnsync-props
2035                        --log-window-size= --no-checkout --quiet
2036                        --repack-flags --use-log-author --localtime
2037                        --ignore-paths= $remote_opts
2038                        "
2039                local init_opts="
2040                        --template= --shared= --trunk= --tags=
2041                        --branches= --stdlayout --minimize-url
2042                        --no-metadata --use-svm-props --use-svnsync-props
2043                        --rewrite-root= --prefix= --use-log-author
2044                        --add-author-from $remote_opts
2045                        "
2046                local cmt_opts="
2047                        --edit --rmdir --find-copies-harder --copy-similarity=
2048                        "
2049
2050                local cur="${COMP_WORDS[COMP_CWORD]}"
2051                case "$subcommand,$cur" in
2052                fetch,--*)
2053                        __gitcomp "--revision= --fetch-all $fc_opts"
2054                        ;;
2055                clone,--*)
2056                        __gitcomp "--revision= $fc_opts $init_opts"
2057                        ;;
2058                init,--*)
2059                        __gitcomp "$init_opts"
2060                        ;;
2061                dcommit,--*)
2062                        __gitcomp "
2063                                --merge --strategy= --verbose --dry-run
2064                                --fetch-all --no-rebase --commit-url
2065                                --revision $cmt_opts $fc_opts
2066                                "
2067                        ;;
2068                set-tree,--*)
2069                        __gitcomp "--stdin $cmt_opts $fc_opts"
2070                        ;;
2071                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2072                show-externals,--*)
2073                        __gitcomp "--revision="
2074                        ;;
2075                log,--*)
2076                        __gitcomp "
2077                                --limit= --revision= --verbose --incremental
2078                                --oneline --show-commit --non-recursive
2079                                --authors-file= --color
2080                                "
2081                        ;;
2082                rebase,--*)
2083                        __gitcomp "
2084                                --merge --verbose --strategy= --local
2085                                --fetch-all --dry-run $fc_opts
2086                                "
2087                        ;;
2088                commit-diff,--*)
2089                        __gitcomp "--message= --file= --revision= $cmt_opts"
2090                        ;;
2091                info,--*)
2092                        __gitcomp "--url"
2093                        ;;
2094                branch,--*)
2095                        __gitcomp "--dry-run --message --tag"
2096                        ;;
2097                tag,--*)
2098                        __gitcomp "--dry-run --message"
2099                        ;;
2100                blame,--*)
2101                        __gitcomp "--git-format"
2102                        ;;
2103                migrate,--*)
2104                        __gitcomp "
2105                                --config-dir= --ignore-paths= --minimize
2106                                --no-auth-cache --username=
2107                                "
2108                        ;;
2109                *)
2110                        COMPREPLY=()
2111                        ;;
2112                esac
2113        fi
2114}
2115
2116_git_tag ()
2117{
2118        local i c=1 f=0
2119        while [ $c -lt $COMP_CWORD ]; do
2120                i="${COMP_WORDS[c]}"
2121                case "$i" in
2122                -d|-v)
2123                        __gitcomp "$(__git_tags)"
2124                        return
2125                        ;;
2126                -f)
2127                        f=1
2128                        ;;
2129                esac
2130                c=$((++c))
2131        done
2132
2133        case "${COMP_WORDS[COMP_CWORD-1]}" in
2134        -m|-F)
2135                COMPREPLY=()
2136                ;;
2137        -*|tag)
2138                if [ $f = 1 ]; then
2139                        __gitcomp "$(__git_tags)"
2140                else
2141                        COMPREPLY=()
2142                fi
2143                ;;
2144        *)
2145                __gitcomp "$(__git_refs)"
2146                ;;
2147        esac
2148}
2149
2150_git ()
2151{
2152        local i c=1 command __git_dir
2153
2154        while [ $c -lt $COMP_CWORD ]; do
2155                i="${COMP_WORDS[c]}"
2156                case "$i" in
2157                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2158                --bare)      __git_dir="." ;;
2159                --version|-p|--paginate) ;;
2160                --help) command="help"; break ;;
2161                *) command="$i"; break ;;
2162                esac
2163                c=$((++c))
2164        done
2165
2166        if [ -z "$command" ]; then
2167                case "${COMP_WORDS[COMP_CWORD]}" in
2168                --*)   __gitcomp "
2169                        --paginate
2170                        --no-pager
2171                        --git-dir=
2172                        --bare
2173                        --version
2174                        --exec-path
2175                        --html-path
2176                        --work-tree=
2177                        --help
2178                        "
2179                        ;;
2180                *)     __git_compute_porcelain_commands
2181                       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2182                esac
2183                return
2184        fi
2185
2186        local expansion=$(__git_aliased_command "$command")
2187        [ "$expansion" ] && command="$expansion"
2188
2189        case "$command" in
2190        am)          _git_am ;;
2191        add)         _git_add ;;
2192        apply)       _git_apply ;;
2193        archive)     _git_archive ;;
2194        bisect)      _git_bisect ;;
2195        bundle)      _git_bundle ;;
2196        branch)      _git_branch ;;
2197        checkout)    _git_checkout ;;
2198        cherry)      _git_cherry ;;
2199        cherry-pick) _git_cherry_pick ;;
2200        clean)       _git_clean ;;
2201        clone)       _git_clone ;;
2202        commit)      _git_commit ;;
2203        config)      _git_config ;;
2204        describe)    _git_describe ;;
2205        diff)        _git_diff ;;
2206        difftool)    _git_difftool ;;
2207        fetch)       _git_fetch ;;
2208        format-patch) _git_format_patch ;;
2209        fsck)        _git_fsck ;;
2210        gc)          _git_gc ;;
2211        grep)        _git_grep ;;
2212        help)        _git_help ;;
2213        init)        _git_init ;;
2214        log)         _git_log ;;
2215        ls-files)    _git_ls_files ;;
2216        ls-remote)   _git_ls_remote ;;
2217        ls-tree)     _git_ls_tree ;;
2218        merge)       _git_merge;;
2219        mergetool)   _git_mergetool;;
2220        merge-base)  _git_merge_base ;;
2221        mv)          _git_mv ;;
2222        name-rev)    _git_name_rev ;;
2223        pull)        _git_pull ;;
2224        push)        _git_push ;;
2225        rebase)      _git_rebase ;;
2226        remote)      _git_remote ;;
2227        replace)     _git_replace ;;
2228        reset)       _git_reset ;;
2229        revert)      _git_revert ;;
2230        rm)          _git_rm ;;
2231        send-email)  _git_send_email ;;
2232        shortlog)    _git_shortlog ;;
2233        show)        _git_show ;;
2234        show-branch) _git_show_branch ;;
2235        stash)       _git_stash ;;
2236        stage)       _git_add ;;
2237        submodule)   _git_submodule ;;
2238        svn)         _git_svn ;;
2239        tag)         _git_tag ;;
2240        whatchanged) _git_log ;;
2241        *)           COMPREPLY=() ;;
2242        esac
2243}
2244
2245_gitk ()
2246{
2247        __git_has_doubledash && return
2248
2249        local cur="${COMP_WORDS[COMP_CWORD]}"
2250        local g="$(__gitdir)"
2251        local merge=""
2252        if [ -f "$g/MERGE_HEAD" ]; then
2253                merge="--merge"
2254        fi
2255        case "$cur" in
2256        --*)
2257                __gitcomp "
2258                        $__git_log_common_options
2259                        $__git_log_gitk_options
2260                        $merge
2261                        "
2262                return
2263                ;;
2264        esac
2265        __git_complete_revlist
2266}
2267
2268complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
2269        || complete -o default -o nospace -F _git git
2270complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
2271        || complete -o default -o nospace -F _gitk gitk
2272
2273# The following are necessary only for Cygwin, and only are needed
2274# when the user has tab-completed the executable name and consequently
2275# included the '.exe' suffix.
2276#
2277if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2278complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
2279        || complete -o default -o nospace -F _git git.exe
2280fi