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