contrib / completion / git-completion.bashon commit Documentation/technical: document ll_merge (24d113e)
   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                        --set-upstream
 801                        "
 802                ;;
 803        *)
 804                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
 805                        __gitcomp "$(__git_heads)"
 806                else
 807                        __gitcomp "$(__git_refs)"
 808                fi
 809                ;;
 810        esac
 811}
 812
 813_git_bundle ()
 814{
 815        local cmd="${COMP_WORDS[2]}"
 816        case "$COMP_CWORD" in
 817        2)
 818                __gitcomp "create list-heads verify unbundle"
 819                ;;
 820        3)
 821                # looking for a file
 822                ;;
 823        *)
 824                case "$cmd" in
 825                        create)
 826                                __git_complete_revlist
 827                        ;;
 828                esac
 829                ;;
 830        esac
 831}
 832
 833_git_checkout ()
 834{
 835        __git_has_doubledash && return
 836
 837        local cur="${COMP_WORDS[COMP_CWORD]}"
 838        case "$cur" in
 839        --conflict=*)
 840                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
 841                ;;
 842        --*)
 843                __gitcomp "
 844                        --quiet --ours --theirs --track --no-track --merge
 845                        --conflict= --orphan --patch
 846                        "
 847                ;;
 848        *)
 849                __gitcomp "$(__git_refs)"
 850                ;;
 851        esac
 852}
 853
 854_git_cherry ()
 855{
 856        __gitcomp "$(__git_refs)"
 857}
 858
 859_git_cherry_pick ()
 860{
 861        local cur="${COMP_WORDS[COMP_CWORD]}"
 862        case "$cur" in
 863        --*)
 864                __gitcomp "--edit --no-commit"
 865                ;;
 866        *)
 867                __gitcomp "$(__git_refs)"
 868                ;;
 869        esac
 870}
 871
 872_git_clean ()
 873{
 874        __git_has_doubledash && return
 875
 876        local cur="${COMP_WORDS[COMP_CWORD]}"
 877        case "$cur" in
 878        --*)
 879                __gitcomp "--dry-run --quiet"
 880                return
 881                ;;
 882        esac
 883        COMPREPLY=()
 884}
 885
 886_git_clone ()
 887{
 888        local cur="${COMP_WORDS[COMP_CWORD]}"
 889        case "$cur" in
 890        --*)
 891                __gitcomp "
 892                        --local
 893                        --no-hardlinks
 894                        --shared
 895                        --reference
 896                        --quiet
 897                        --no-checkout
 898                        --bare
 899                        --mirror
 900                        --origin
 901                        --upload-pack
 902                        --template=
 903                        --depth
 904                        "
 905                return
 906                ;;
 907        esac
 908        COMPREPLY=()
 909}
 910
 911_git_commit ()
 912{
 913        __git_has_doubledash && return
 914
 915        local cur="${COMP_WORDS[COMP_CWORD]}"
 916        case "$cur" in
 917        --cleanup=*)
 918                __gitcomp "default strip verbatim whitespace
 919                        " "" "${cur##--cleanup=}"
 920                return
 921                ;;
 922        --reuse-message=*)
 923                __gitcomp "$(__git_refs)" "" "${cur##--reuse-message=}"
 924                return
 925                ;;
 926        --reedit-message=*)
 927                __gitcomp "$(__git_refs)" "" "${cur##--reedit-message=}"
 928                return
 929                ;;
 930        --untracked-files=*)
 931                __gitcomp "all no normal" "" "${cur##--untracked-files=}"
 932                return
 933                ;;
 934        --*)
 935                __gitcomp "
 936                        --all --author= --signoff --verify --no-verify
 937                        --edit --amend --include --only --interactive
 938                        --dry-run --reuse-message= --reedit-message=
 939                        --reset-author --file= --message= --template=
 940                        --cleanup= --untracked-files --untracked-files=
 941                        --verbose --quiet
 942                        "
 943                return
 944        esac
 945        COMPREPLY=()
 946}
 947
 948_git_describe ()
 949{
 950        local cur="${COMP_WORDS[COMP_CWORD]}"
 951        case "$cur" in
 952        --*)
 953                __gitcomp "
 954                        --all --tags --contains --abbrev= --candidates=
 955                        --exact-match --debug --long --match --always
 956                        "
 957                return
 958        esac
 959        __gitcomp "$(__git_refs)"
 960}
 961
 962__git_diff_common_options="--stat --numstat --shortstat --summary
 963                        --patch-with-stat --name-only --name-status --color
 964                        --no-color --color-words --no-renames --check
 965                        --full-index --binary --abbrev --diff-filter=
 966                        --find-copies-harder
 967                        --text --ignore-space-at-eol --ignore-space-change
 968                        --ignore-all-space --exit-code --quiet --ext-diff
 969                        --no-ext-diff
 970                        --no-prefix --src-prefix= --dst-prefix=
 971                        --inter-hunk-context=
 972                        --patience
 973                        --raw
 974                        --dirstat --dirstat= --dirstat-by-file
 975                        --dirstat-by-file= --cumulative
 976"
 977
 978_git_diff ()
 979{
 980        __git_has_doubledash && return
 981
 982        local cur="${COMP_WORDS[COMP_CWORD]}"
 983        case "$cur" in
 984        --*)
 985                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
 986                        --base --ours --theirs
 987                        $__git_diff_common_options
 988                        "
 989                return
 990                ;;
 991        esac
 992        __git_complete_file
 993}
 994
 995__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
 996                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge
 997"
 998
 999_git_difftool ()
1000{
1001        __git_has_doubledash && return
1002
1003        local cur="${COMP_WORDS[COMP_CWORD]}"
1004        case "$cur" in
1005        --tool=*)
1006                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1007                return
1008                ;;
1009        --*)
1010                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1011                        --base --ours --theirs
1012                        --no-renames --diff-filter= --find-copies-harder
1013                        --relative --ignore-submodules
1014                        --tool="
1015                return
1016                ;;
1017        esac
1018        __git_complete_file
1019}
1020
1021__git_fetch_options="
1022        --quiet --verbose --append --upload-pack --force --keep --depth=
1023        --tags --no-tags --all --prune --dry-run
1024"
1025
1026_git_fetch ()
1027{
1028        local cur="${COMP_WORDS[COMP_CWORD]}"
1029        case "$cur" in
1030        --*)
1031                __gitcomp "$__git_fetch_options"
1032                return
1033                ;;
1034        esac
1035        __git_complete_remote_or_refspec
1036}
1037
1038_git_format_patch ()
1039{
1040        local cur="${COMP_WORDS[COMP_CWORD]}"
1041        case "$cur" in
1042        --thread=*)
1043                __gitcomp "
1044                        deep shallow
1045                        " "" "${cur##--thread=}"
1046                return
1047                ;;
1048        --*)
1049                __gitcomp "
1050                        --stdout --attach --no-attach --thread --thread=
1051                        --output-directory
1052                        --numbered --start-number
1053                        --numbered-files
1054                        --keep-subject
1055                        --signoff --signature --no-signature
1056                        --in-reply-to= --cc=
1057                        --full-index --binary
1058                        --not --all
1059                        --cover-letter
1060                        --no-prefix --src-prefix= --dst-prefix=
1061                        --inline --suffix= --ignore-if-in-upstream
1062                        --subject-prefix=
1063                        "
1064                return
1065                ;;
1066        esac
1067        __git_complete_revlist
1068}
1069
1070_git_fsck ()
1071{
1072        local cur="${COMP_WORDS[COMP_CWORD]}"
1073        case "$cur" in
1074        --*)
1075                __gitcomp "
1076                        --tags --root --unreachable --cache --no-reflogs --full
1077                        --strict --verbose --lost-found
1078                        "
1079                return
1080                ;;
1081        esac
1082        COMPREPLY=()
1083}
1084
1085_git_gc ()
1086{
1087        local cur="${COMP_WORDS[COMP_CWORD]}"
1088        case "$cur" in
1089        --*)
1090                __gitcomp "--prune --aggressive"
1091                return
1092                ;;
1093        esac
1094        COMPREPLY=()
1095}
1096
1097_git_gitk ()
1098{
1099        _gitk
1100}
1101
1102_git_grep ()
1103{
1104        __git_has_doubledash && return
1105
1106        local cur="${COMP_WORDS[COMP_CWORD]}"
1107        case "$cur" in
1108        --*)
1109                __gitcomp "
1110                        --cached
1111                        --text --ignore-case --word-regexp --invert-match
1112                        --full-name
1113                        --extended-regexp --basic-regexp --fixed-strings
1114                        --files-with-matches --name-only
1115                        --files-without-match
1116                        --max-depth
1117                        --count
1118                        --and --or --not --all-match
1119                        "
1120                return
1121                ;;
1122        esac
1123
1124        __gitcomp "$(__git_refs)"
1125}
1126
1127_git_help ()
1128{
1129        local cur="${COMP_WORDS[COMP_CWORD]}"
1130        case "$cur" in
1131        --*)
1132                __gitcomp "--all --info --man --web"
1133                return
1134                ;;
1135        esac
1136        __git_compute_all_commands
1137        __gitcomp "$__git_all_commands
1138                attributes cli core-tutorial cvs-migration
1139                diffcore gitk glossary hooks ignore modules
1140                repository-layout tutorial tutorial-2
1141                workflows
1142                "
1143}
1144
1145_git_init ()
1146{
1147        local cur="${COMP_WORDS[COMP_CWORD]}"
1148        case "$cur" in
1149        --shared=*)
1150                __gitcomp "
1151                        false true umask group all world everybody
1152                        " "" "${cur##--shared=}"
1153                return
1154                ;;
1155        --*)
1156                __gitcomp "--quiet --bare --template= --shared --shared="
1157                return
1158                ;;
1159        esac
1160        COMPREPLY=()
1161}
1162
1163_git_ls_files ()
1164{
1165        __git_has_doubledash && return
1166
1167        local cur="${COMP_WORDS[COMP_CWORD]}"
1168        case "$cur" in
1169        --*)
1170                __gitcomp "--cached --deleted --modified --others --ignored
1171                        --stage --directory --no-empty-directory --unmerged
1172                        --killed --exclude= --exclude-from=
1173                        --exclude-per-directory= --exclude-standard
1174                        --error-unmatch --with-tree= --full-name
1175                        --abbrev --ignored --exclude-per-directory
1176                        "
1177                return
1178                ;;
1179        esac
1180        COMPREPLY=()
1181}
1182
1183_git_ls_remote ()
1184{
1185        __gitcomp "$(__git_remotes)"
1186}
1187
1188_git_ls_tree ()
1189{
1190        __git_complete_file
1191}
1192
1193# Options that go well for log, shortlog and gitk
1194__git_log_common_options="
1195        --not --all
1196        --branches --tags --remotes
1197        --first-parent --merges --no-merges
1198        --max-count=
1199        --max-age= --since= --after=
1200        --min-age= --until= --before=
1201"
1202# Options that go well for log and gitk (not shortlog)
1203__git_log_gitk_options="
1204        --dense --sparse --full-history
1205        --simplify-merges --simplify-by-decoration
1206        --left-right
1207"
1208# Options that go well for log and shortlog (not gitk)
1209__git_log_shortlog_options="
1210        --author= --committer= --grep=
1211        --all-match
1212"
1213
1214__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1215__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1216
1217_git_log ()
1218{
1219        __git_has_doubledash && return
1220
1221        local cur="${COMP_WORDS[COMP_CWORD]}"
1222        local g="$(git rev-parse --git-dir 2>/dev/null)"
1223        local merge=""
1224        if [ -f "$g/MERGE_HEAD" ]; then
1225                merge="--merge"
1226        fi
1227        case "$cur" in
1228        --pretty=*)
1229                __gitcomp "$__git_log_pretty_formats
1230                        " "" "${cur##--pretty=}"
1231                return
1232                ;;
1233        --format=*)
1234                __gitcomp "$__git_log_pretty_formats
1235                        " "" "${cur##--format=}"
1236                return
1237                ;;
1238        --date=*)
1239                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1240                return
1241                ;;
1242        --decorate=*)
1243                __gitcomp "long short" "" "${cur##--decorate=}"
1244                return
1245                ;;
1246        --*)
1247                __gitcomp "
1248                        $__git_log_common_options
1249                        $__git_log_shortlog_options
1250                        $__git_log_gitk_options
1251                        --root --topo-order --date-order --reverse
1252                        --follow --full-diff
1253                        --abbrev-commit --abbrev=
1254                        --relative-date --date=
1255                        --pretty= --format= --oneline
1256                        --cherry-pick
1257                        --graph
1258                        --decorate --decorate=
1259                        --walk-reflogs
1260                        --parents --children
1261                        $merge
1262                        $__git_diff_common_options
1263                        --pickaxe-all --pickaxe-regex
1264                        "
1265                return
1266                ;;
1267        esac
1268        __git_complete_revlist
1269}
1270
1271__git_merge_options="
1272        --no-commit --no-stat --log --no-log --squash --strategy
1273        --commit --stat --no-squash --ff --no-ff --ff-only
1274"
1275
1276_git_merge ()
1277{
1278        __git_complete_strategy && return
1279
1280        local cur="${COMP_WORDS[COMP_CWORD]}"
1281        case "$cur" in
1282        --*)
1283                __gitcomp "$__git_merge_options"
1284                return
1285        esac
1286        __gitcomp "$(__git_refs)"
1287}
1288
1289_git_mergetool ()
1290{
1291        local cur="${COMP_WORDS[COMP_CWORD]}"
1292        case "$cur" in
1293        --tool=*)
1294                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1295                return
1296                ;;
1297        --*)
1298                __gitcomp "--tool="
1299                return
1300                ;;
1301        esac
1302        COMPREPLY=()
1303}
1304
1305_git_merge_base ()
1306{
1307        __gitcomp "$(__git_refs)"
1308}
1309
1310_git_mv ()
1311{
1312        local cur="${COMP_WORDS[COMP_CWORD]}"
1313        case "$cur" in
1314        --*)
1315                __gitcomp "--dry-run"
1316                return
1317                ;;
1318        esac
1319        COMPREPLY=()
1320}
1321
1322_git_name_rev ()
1323{
1324        __gitcomp "--tags --all --stdin"
1325}
1326
1327_git_notes ()
1328{
1329        local subcommands="edit show"
1330        if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
1331                __gitcomp "$subcommands"
1332                return
1333        fi
1334
1335        case "${COMP_WORDS[COMP_CWORD-1]}" in
1336        -m|-F)
1337                COMPREPLY=()
1338                ;;
1339        *)
1340                __gitcomp "$(__git_refs)"
1341                ;;
1342        esac
1343}
1344
1345_git_pull ()
1346{
1347        __git_complete_strategy && return
1348
1349        local cur="${COMP_WORDS[COMP_CWORD]}"
1350        case "$cur" in
1351        --*)
1352                __gitcomp "
1353                        --rebase --no-rebase
1354                        $__git_merge_options
1355                        $__git_fetch_options
1356                "
1357                return
1358                ;;
1359        esac
1360        __git_complete_remote_or_refspec
1361}
1362
1363_git_push ()
1364{
1365        local cur="${COMP_WORDS[COMP_CWORD]}"
1366        case "${COMP_WORDS[COMP_CWORD-1]}" in
1367        --repo)
1368                __gitcomp "$(__git_remotes)"
1369                return
1370        esac
1371        case "$cur" in
1372        --repo=*)
1373                __gitcomp "$(__git_remotes)" "" "${cur##--repo=}"
1374                return
1375                ;;
1376        --*)
1377                __gitcomp "
1378                        --all --mirror --tags --dry-run --force --verbose
1379                        --receive-pack= --repo=
1380                "
1381                return
1382                ;;
1383        esac
1384        __git_complete_remote_or_refspec
1385}
1386
1387_git_rebase ()
1388{
1389        local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1390        if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1391                __gitcomp "--continue --skip --abort"
1392                return
1393        fi
1394        __git_complete_strategy && return
1395        case "$cur" in
1396        --whitespace=*)
1397                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1398                return
1399                ;;
1400        --*)
1401                __gitcomp "
1402                        --onto --merge --strategy --interactive
1403                        --preserve-merges --stat --no-stat
1404                        --committer-date-is-author-date --ignore-date
1405                        --ignore-whitespace --whitespace=
1406                        --autosquash
1407                        "
1408
1409                return
1410        esac
1411        __gitcomp "$(__git_refs)"
1412}
1413
1414__git_send_email_confirm_options="always never auto cc compose"
1415__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1416
1417_git_send_email ()
1418{
1419        local cur="${COMP_WORDS[COMP_CWORD]}"
1420        case "$cur" in
1421        --confirm=*)
1422                __gitcomp "
1423                        $__git_send_email_confirm_options
1424                        " "" "${cur##--confirm=}"
1425                return
1426                ;;
1427        --suppress-cc=*)
1428                __gitcomp "
1429                        $__git_send_email_suppresscc_options
1430                        " "" "${cur##--suppress-cc=}"
1431
1432                return
1433                ;;
1434        --smtp-encryption=*)
1435                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1436                return
1437                ;;
1438        --*)
1439                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1440                        --compose --confirm= --dry-run --envelope-sender
1441                        --from --identity
1442                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1443                        --no-suppress-from --no-thread --quiet
1444                        --signed-off-by-cc --smtp-pass --smtp-server
1445                        --smtp-server-port --smtp-encryption= --smtp-user
1446                        --subject --suppress-cc= --suppress-from --thread --to
1447                        --validate --no-validate"
1448                return
1449                ;;
1450        esac
1451        COMPREPLY=()
1452}
1453
1454_git_stage ()
1455{
1456        _git_add
1457}
1458
1459__git_config_get_set_variables ()
1460{
1461        local prevword word config_file= c=$COMP_CWORD
1462        while [ $c -gt 1 ]; do
1463                word="${COMP_WORDS[c]}"
1464                case "$word" in
1465                --global|--system|--file=*)
1466                        config_file="$word"
1467                        break
1468                        ;;
1469                -f|--file)
1470                        config_file="$word $prevword"
1471                        break
1472                        ;;
1473                esac
1474                prevword=$word
1475                c=$((--c))
1476        done
1477
1478        git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1479        while read line
1480        do
1481                case "$line" in
1482                *.*=*)
1483                        echo "${line/=*/}"
1484                        ;;
1485                esac
1486        done
1487}
1488
1489_git_config ()
1490{
1491        local cur="${COMP_WORDS[COMP_CWORD]}"
1492        local prv="${COMP_WORDS[COMP_CWORD-1]}"
1493        case "$prv" in
1494        branch.*.remote)
1495                __gitcomp "$(__git_remotes)"
1496                return
1497                ;;
1498        branch.*.merge)
1499                __gitcomp "$(__git_refs)"
1500                return
1501                ;;
1502        remote.*.fetch)
1503                local remote="${prv#remote.}"
1504                remote="${remote%.fetch}"
1505                __gitcomp "$(__git_refs_remotes "$remote")"
1506                return
1507                ;;
1508        remote.*.push)
1509                local remote="${prv#remote.}"
1510                remote="${remote%.push}"
1511                __gitcomp "$(git --git-dir="$(__gitdir)" \
1512                        for-each-ref --format='%(refname):%(refname)' \
1513                        refs/heads)"
1514                return
1515                ;;
1516        pull.twohead|pull.octopus)
1517                __git_compute_merge_strategies
1518                __gitcomp "$__git_merge_strategies"
1519                return
1520                ;;
1521        color.branch|color.diff|color.interactive|\
1522        color.showbranch|color.status|color.ui)
1523                __gitcomp "always never auto"
1524                return
1525                ;;
1526        color.pager)
1527                __gitcomp "false true"
1528                return
1529                ;;
1530        color.*.*)
1531                __gitcomp "
1532                        normal black red green yellow blue magenta cyan white
1533                        bold dim ul blink reverse
1534                        "
1535                return
1536                ;;
1537        help.format)
1538                __gitcomp "man info web html"
1539                return
1540                ;;
1541        log.date)
1542                __gitcomp "$__git_log_date_formats"
1543                return
1544                ;;
1545        sendemail.aliasesfiletype)
1546                __gitcomp "mutt mailrc pine elm gnus"
1547                return
1548                ;;
1549        sendemail.confirm)
1550                __gitcomp "$__git_send_email_confirm_options"
1551                return
1552                ;;
1553        sendemail.suppresscc)
1554                __gitcomp "$__git_send_email_suppresscc_options"
1555                return
1556                ;;
1557        --get|--get-all|--unset|--unset-all)
1558                __gitcomp "$(__git_config_get_set_variables)"
1559                return
1560                ;;
1561        *.*)
1562                COMPREPLY=()
1563                return
1564                ;;
1565        esac
1566        case "$cur" in
1567        --*)
1568                __gitcomp "
1569                        --global --system --file=
1570                        --list --replace-all
1571                        --get --get-all --get-regexp
1572                        --add --unset --unset-all
1573                        --remove-section --rename-section
1574                        "
1575                return
1576                ;;
1577        branch.*.*)
1578                local pfx="${cur%.*}."
1579                cur="${cur##*.}"
1580                __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur"
1581                return
1582                ;;
1583        branch.*)
1584                local pfx="${cur%.*}."
1585                cur="${cur#*.}"
1586                __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1587                return
1588                ;;
1589        guitool.*.*)
1590                local pfx="${cur%.*}."
1591                cur="${cur##*.}"
1592                __gitcomp "
1593                        argprompt cmd confirm needsfile noconsole norescan
1594                        prompt revprompt revunmerged title
1595                        " "$pfx" "$cur"
1596                return
1597                ;;
1598        difftool.*.*)
1599                local pfx="${cur%.*}."
1600                cur="${cur##*.}"
1601                __gitcomp "cmd path" "$pfx" "$cur"
1602                return
1603                ;;
1604        man.*.*)
1605                local pfx="${cur%.*}."
1606                cur="${cur##*.}"
1607                __gitcomp "cmd path" "$pfx" "$cur"
1608                return
1609                ;;
1610        mergetool.*.*)
1611                local pfx="${cur%.*}."
1612                cur="${cur##*.}"
1613                __gitcomp "cmd path trustExitCode" "$pfx" "$cur"
1614                return
1615                ;;
1616        pager.*)
1617                local pfx="${cur%.*}."
1618                cur="${cur#*.}"
1619                __git_compute_all_commands
1620                __gitcomp "$__git_all_commands" "$pfx" "$cur"
1621                return
1622                ;;
1623        remote.*.*)
1624                local pfx="${cur%.*}."
1625                cur="${cur##*.}"
1626                __gitcomp "
1627                        url proxy fetch push mirror skipDefaultUpdate
1628                        receivepack uploadpack tagopt pushurl
1629                        " "$pfx" "$cur"
1630                return
1631                ;;
1632        remote.*)
1633                local pfx="${cur%.*}."
1634                cur="${cur#*.}"
1635                __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1636                return
1637                ;;
1638        url.*.*)
1639                local pfx="${cur%.*}."
1640                cur="${cur##*.}"
1641                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur"
1642                return
1643                ;;
1644        esac
1645        __gitcomp "
1646                add.ignore-errors
1647                alias.
1648                apply.ignorewhitespace
1649                apply.whitespace
1650                branch.autosetupmerge
1651                branch.autosetuprebase
1652                clean.requireForce
1653                color.branch
1654                color.branch.current
1655                color.branch.local
1656                color.branch.plain
1657                color.branch.remote
1658                color.diff
1659                color.diff.commit
1660                color.diff.frag
1661                color.diff.meta
1662                color.diff.new
1663                color.diff.old
1664                color.diff.plain
1665                color.diff.whitespace
1666                color.grep
1667                color.grep.external
1668                color.grep.match
1669                color.interactive
1670                color.interactive.header
1671                color.interactive.help
1672                color.interactive.prompt
1673                color.pager
1674                color.showbranch
1675                color.status
1676                color.status.added
1677                color.status.changed
1678                color.status.header
1679                color.status.nobranch
1680                color.status.untracked
1681                color.status.updated
1682                color.ui
1683                commit.template
1684                core.autocrlf
1685                core.bare
1686                core.compression
1687                core.createObject
1688                core.deltaBaseCacheLimit
1689                core.editor
1690                core.excludesfile
1691                core.fileMode
1692                core.fsyncobjectfiles
1693                core.gitProxy
1694                core.ignoreCygwinFSTricks
1695                core.ignoreStat
1696                core.logAllRefUpdates
1697                core.loosecompression
1698                core.packedGitLimit
1699                core.packedGitWindowSize
1700                core.pager
1701                core.preferSymlinkRefs
1702                core.preloadindex
1703                core.quotepath
1704                core.repositoryFormatVersion
1705                core.safecrlf
1706                core.sharedRepository
1707                core.symlinks
1708                core.trustctime
1709                core.warnAmbiguousRefs
1710                core.whitespace
1711                core.worktree
1712                diff.autorefreshindex
1713                diff.external
1714                diff.mnemonicprefix
1715                diff.renameLimit
1716                diff.renameLimit.
1717                diff.renames
1718                diff.suppressBlankEmpty
1719                diff.tool
1720                diff.wordRegex
1721                difftool.
1722                difftool.prompt
1723                fetch.unpackLimit
1724                format.attach
1725                format.cc
1726                format.headers
1727                format.numbered
1728                format.pretty
1729                format.signature
1730                format.signoff
1731                format.subjectprefix
1732                format.suffix
1733                format.thread
1734                gc.aggressiveWindow
1735                gc.auto
1736                gc.autopacklimit
1737                gc.packrefs
1738                gc.pruneexpire
1739                gc.reflogexpire
1740                gc.reflogexpireunreachable
1741                gc.rerereresolved
1742                gc.rerereunresolved
1743                gitcvs.allbinary
1744                gitcvs.commitmsgannotation
1745                gitcvs.dbTableNamePrefix
1746                gitcvs.dbdriver
1747                gitcvs.dbname
1748                gitcvs.dbpass
1749                gitcvs.dbuser
1750                gitcvs.enabled
1751                gitcvs.logfile
1752                gitcvs.usecrlfattr
1753                guitool.
1754                gui.blamehistoryctx
1755                gui.commitmsgwidth
1756                gui.copyblamethreshold
1757                gui.diffcontext
1758                gui.encoding
1759                gui.fastcopyblame
1760                gui.matchtrackingbranch
1761                gui.newbranchtemplate
1762                gui.pruneduringfetch
1763                gui.spellingdictionary
1764                gui.trustmtime
1765                help.autocorrect
1766                help.browser
1767                help.format
1768                http.lowSpeedLimit
1769                http.lowSpeedTime
1770                http.maxRequests
1771                http.noEPSV
1772                http.proxy
1773                http.sslCAInfo
1774                http.sslCAPath
1775                http.sslCert
1776                http.sslKey
1777                http.sslVerify
1778                i18n.commitEncoding
1779                i18n.logOutputEncoding
1780                imap.folder
1781                imap.host
1782                imap.pass
1783                imap.port
1784                imap.preformattedHTML
1785                imap.sslverify
1786                imap.tunnel
1787                imap.user
1788                instaweb.browser
1789                instaweb.httpd
1790                instaweb.local
1791                instaweb.modulepath
1792                instaweb.port
1793                interactive.singlekey
1794                log.date
1795                log.showroot
1796                mailmap.file
1797                man.
1798                man.viewer
1799                merge.conflictstyle
1800                merge.log
1801                merge.renameLimit
1802                merge.stat
1803                merge.tool
1804                merge.verbosity
1805                mergetool.
1806                mergetool.keepBackup
1807                mergetool.prompt
1808                pack.compression
1809                pack.deltaCacheLimit
1810                pack.deltaCacheSize
1811                pack.depth
1812                pack.indexVersion
1813                pack.packSizeLimit
1814                pack.threads
1815                pack.window
1816                pack.windowMemory
1817                pager.
1818                pull.octopus
1819                pull.twohead
1820                push.default
1821                rebase.stat
1822                receive.denyCurrentBranch
1823                receive.denyDeletes
1824                receive.denyNonFastForwards
1825                receive.fsckObjects
1826                receive.unpackLimit
1827                repack.usedeltabaseoffset
1828                rerere.autoupdate
1829                rerere.enabled
1830                sendemail.aliasesfile
1831                sendemail.aliasesfiletype
1832                sendemail.bcc
1833                sendemail.cc
1834                sendemail.cccmd
1835                sendemail.chainreplyto
1836                sendemail.confirm
1837                sendemail.envelopesender
1838                sendemail.multiedit
1839                sendemail.signedoffbycc
1840                sendemail.smtpencryption
1841                sendemail.smtppass
1842                sendemail.smtpserver
1843                sendemail.smtpserverport
1844                sendemail.smtpuser
1845                sendemail.suppresscc
1846                sendemail.suppressfrom
1847                sendemail.thread
1848                sendemail.to
1849                sendemail.validate
1850                showbranch.default
1851                status.relativePaths
1852                status.showUntrackedFiles
1853                tar.umask
1854                transfer.unpackLimit
1855                url.
1856                user.email
1857                user.name
1858                user.signingkey
1859                web.browser
1860                branch. remote.
1861        "
1862}
1863
1864_git_remote ()
1865{
1866        local subcommands="add rename rm show prune update set-head"
1867        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1868        if [ -z "$subcommand" ]; then
1869                __gitcomp "$subcommands"
1870                return
1871        fi
1872
1873        case "$subcommand" in
1874        rename|rm|show|prune)
1875                __gitcomp "$(__git_remotes)"
1876                ;;
1877        update)
1878                local i c='' IFS=$'\n'
1879                for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
1880                        i="${i#remotes.}"
1881                        c="$c ${i/ */}"
1882                done
1883                __gitcomp "$c"
1884                ;;
1885        *)
1886                COMPREPLY=()
1887                ;;
1888        esac
1889}
1890
1891_git_replace ()
1892{
1893        __gitcomp "$(__git_refs)"
1894}
1895
1896_git_reset ()
1897{
1898        __git_has_doubledash && return
1899
1900        local cur="${COMP_WORDS[COMP_CWORD]}"
1901        case "$cur" in
1902        --*)
1903                __gitcomp "--merge --mixed --hard --soft --patch"
1904                return
1905                ;;
1906        esac
1907        __gitcomp "$(__git_refs)"
1908}
1909
1910_git_revert ()
1911{
1912        local cur="${COMP_WORDS[COMP_CWORD]}"
1913        case "$cur" in
1914        --*)
1915                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
1916                return
1917                ;;
1918        esac
1919        __gitcomp "$(__git_refs)"
1920}
1921
1922_git_rm ()
1923{
1924        __git_has_doubledash && return
1925
1926        local cur="${COMP_WORDS[COMP_CWORD]}"
1927        case "$cur" in
1928        --*)
1929                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
1930                return
1931                ;;
1932        esac
1933        COMPREPLY=()
1934}
1935
1936_git_shortlog ()
1937{
1938        __git_has_doubledash && return
1939
1940        local cur="${COMP_WORDS[COMP_CWORD]}"
1941        case "$cur" in
1942        --*)
1943                __gitcomp "
1944                        $__git_log_common_options
1945                        $__git_log_shortlog_options
1946                        --numbered --summary
1947                        "
1948                return
1949                ;;
1950        esac
1951        __git_complete_revlist
1952}
1953
1954_git_show ()
1955{
1956        __git_has_doubledash && return
1957
1958        local cur="${COMP_WORDS[COMP_CWORD]}"
1959        case "$cur" in
1960        --pretty=*)
1961                __gitcomp "$__git_log_pretty_formats
1962                        " "" "${cur##--pretty=}"
1963                return
1964                ;;
1965        --format=*)
1966                __gitcomp "$__git_log_pretty_formats
1967                        " "" "${cur##--format=}"
1968                return
1969                ;;
1970        --*)
1971                __gitcomp "--pretty= --format= --abbrev-commit --oneline
1972                        $__git_diff_common_options
1973                        "
1974                return
1975                ;;
1976        esac
1977        __git_complete_file
1978}
1979
1980_git_show_branch ()
1981{
1982        local cur="${COMP_WORDS[COMP_CWORD]}"
1983        case "$cur" in
1984        --*)
1985                __gitcomp "
1986                        --all --remotes --topo-order --current --more=
1987                        --list --independent --merge-base --no-name
1988                        --color --no-color
1989                        --sha1-name --sparse --topics --reflog
1990                        "
1991                return
1992                ;;
1993        esac
1994        __git_complete_revlist
1995}
1996
1997_git_stash ()
1998{
1999        local cur="${COMP_WORDS[COMP_CWORD]}"
2000        local save_opts='--keep-index --no-keep-index --quiet --patch'
2001        local subcommands='save list show apply clear drop pop create branch'
2002        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2003        if [ -z "$subcommand" ]; then
2004                case "$cur" in
2005                --*)
2006                        __gitcomp "$save_opts"
2007                        ;;
2008                *)
2009                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2010                                __gitcomp "$subcommands"
2011                        else
2012                                COMPREPLY=()
2013                        fi
2014                        ;;
2015                esac
2016        else
2017                case "$subcommand,$cur" in
2018                save,--*)
2019                        __gitcomp "$save_opts"
2020                        ;;
2021                apply,--*|pop,--*)
2022                        __gitcomp "--index --quiet"
2023                        ;;
2024                show,--*|drop,--*|branch,--*)
2025                        COMPREPLY=()
2026                        ;;
2027                show,*|apply,*|drop,*|pop,*|branch,*)
2028                        __gitcomp "$(git --git-dir="$(__gitdir)" stash list \
2029                                        | sed -n -e 's/:.*//p')"
2030                        ;;
2031                *)
2032                        COMPREPLY=()
2033                        ;;
2034                esac
2035        fi
2036}
2037
2038_git_submodule ()
2039{
2040        __git_has_doubledash && return
2041
2042        local subcommands="add status init update summary foreach sync"
2043        if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2044                local cur="${COMP_WORDS[COMP_CWORD]}"
2045                case "$cur" in
2046                --*)
2047                        __gitcomp "--quiet --cached"
2048                        ;;
2049                *)
2050                        __gitcomp "$subcommands"
2051                        ;;
2052                esac
2053                return
2054        fi
2055}
2056
2057_git_svn ()
2058{
2059        local subcommands="
2060                init fetch clone rebase dcommit log find-rev
2061                set-tree commit-diff info create-ignore propget
2062                proplist show-ignore show-externals branch tag blame
2063                migrate mkdirs reset gc
2064                "
2065        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2066        if [ -z "$subcommand" ]; then
2067                __gitcomp "$subcommands"
2068        else
2069                local remote_opts="--username= --config-dir= --no-auth-cache"
2070                local fc_opts="
2071                        --follow-parent --authors-file= --repack=
2072                        --no-metadata --use-svm-props --use-svnsync-props
2073                        --log-window-size= --no-checkout --quiet
2074                        --repack-flags --use-log-author --localtime
2075                        --ignore-paths= $remote_opts
2076                        "
2077                local init_opts="
2078                        --template= --shared= --trunk= --tags=
2079                        --branches= --stdlayout --minimize-url
2080                        --no-metadata --use-svm-props --use-svnsync-props
2081                        --rewrite-root= --prefix= --use-log-author
2082                        --add-author-from $remote_opts
2083                        "
2084                local cmt_opts="
2085                        --edit --rmdir --find-copies-harder --copy-similarity=
2086                        "
2087
2088                local cur="${COMP_WORDS[COMP_CWORD]}"
2089                case "$subcommand,$cur" in
2090                fetch,--*)
2091                        __gitcomp "--revision= --fetch-all $fc_opts"
2092                        ;;
2093                clone,--*)
2094                        __gitcomp "--revision= $fc_opts $init_opts"
2095                        ;;
2096                init,--*)
2097                        __gitcomp "$init_opts"
2098                        ;;
2099                dcommit,--*)
2100                        __gitcomp "
2101                                --merge --strategy= --verbose --dry-run
2102                                --fetch-all --no-rebase --commit-url
2103                                --revision $cmt_opts $fc_opts
2104                                "
2105                        ;;
2106                set-tree,--*)
2107                        __gitcomp "--stdin $cmt_opts $fc_opts"
2108                        ;;
2109                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2110                show-externals,--*|mkdirs,--*)
2111                        __gitcomp "--revision="
2112                        ;;
2113                log,--*)
2114                        __gitcomp "
2115                                --limit= --revision= --verbose --incremental
2116                                --oneline --show-commit --non-recursive
2117                                --authors-file= --color
2118                                "
2119                        ;;
2120                rebase,--*)
2121                        __gitcomp "
2122                                --merge --verbose --strategy= --local
2123                                --fetch-all --dry-run $fc_opts
2124                                "
2125                        ;;
2126                commit-diff,--*)
2127                        __gitcomp "--message= --file= --revision= $cmt_opts"
2128                        ;;
2129                info,--*)
2130                        __gitcomp "--url"
2131                        ;;
2132                branch,--*)
2133                        __gitcomp "--dry-run --message --tag"
2134                        ;;
2135                tag,--*)
2136                        __gitcomp "--dry-run --message"
2137                        ;;
2138                blame,--*)
2139                        __gitcomp "--git-format"
2140                        ;;
2141                migrate,--*)
2142                        __gitcomp "
2143                                --config-dir= --ignore-paths= --minimize
2144                                --no-auth-cache --username=
2145                                "
2146                        ;;
2147                reset,--*)
2148                        __gitcomp "--revision= --parent"
2149                        ;;
2150                *)
2151                        COMPREPLY=()
2152                        ;;
2153                esac
2154        fi
2155}
2156
2157_git_tag ()
2158{
2159        local i c=1 f=0
2160        while [ $c -lt $COMP_CWORD ]; do
2161                i="${COMP_WORDS[c]}"
2162                case "$i" in
2163                -d|-v)
2164                        __gitcomp "$(__git_tags)"
2165                        return
2166                        ;;
2167                -f)
2168                        f=1
2169                        ;;
2170                esac
2171                c=$((++c))
2172        done
2173
2174        case "${COMP_WORDS[COMP_CWORD-1]}" in
2175        -m|-F)
2176                COMPREPLY=()
2177                ;;
2178        -*|tag)
2179                if [ $f = 1 ]; then
2180                        __gitcomp "$(__git_tags)"
2181                else
2182                        COMPREPLY=()
2183                fi
2184                ;;
2185        *)
2186                __gitcomp "$(__git_refs)"
2187                ;;
2188        esac
2189}
2190
2191_git_whatchanged ()
2192{
2193        _git_log
2194}
2195
2196_git ()
2197{
2198        local i c=1 command __git_dir
2199
2200        while [ $c -lt $COMP_CWORD ]; do
2201                i="${COMP_WORDS[c]}"
2202                case "$i" in
2203                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2204                --bare)      __git_dir="." ;;
2205                --version|-p|--paginate) ;;
2206                --help) command="help"; break ;;
2207                *) command="$i"; break ;;
2208                esac
2209                c=$((++c))
2210        done
2211
2212        if [ -z "$command" ]; then
2213                case "${COMP_WORDS[COMP_CWORD]}" in
2214                --*)   __gitcomp "
2215                        --paginate
2216                        --no-pager
2217                        --git-dir=
2218                        --bare
2219                        --version
2220                        --exec-path
2221                        --html-path
2222                        --work-tree=
2223                        --help
2224                        "
2225                        ;;
2226                *)     __git_compute_porcelain_commands
2227                       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2228                esac
2229                return
2230        fi
2231
2232        local completion_func="_git_${command//-/_}"
2233        declare -F $completion_func >/dev/null && $completion_func && return
2234
2235        local expansion=$(__git_aliased_command "$command")
2236        if [ -n "$expansion" ]; then
2237                completion_func="_git_${expansion//-/_}"
2238                declare -F $completion_func >/dev/null && $completion_func
2239        fi
2240}
2241
2242_gitk ()
2243{
2244        __git_has_doubledash && return
2245
2246        local cur="${COMP_WORDS[COMP_CWORD]}"
2247        local g="$(__gitdir)"
2248        local merge=""
2249        if [ -f "$g/MERGE_HEAD" ]; then
2250                merge="--merge"
2251        fi
2252        case "$cur" in
2253        --*)
2254                __gitcomp "
2255                        $__git_log_common_options
2256                        $__git_log_gitk_options
2257                        $merge
2258                        "
2259                return
2260                ;;
2261        esac
2262        __git_complete_revlist
2263}
2264
2265complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
2266        || complete -o default -o nospace -F _git git
2267complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
2268        || complete -o default -o nospace -F _gitk gitk
2269
2270# The following are necessary only for Cygwin, and only are needed
2271# when the user has tab-completed the executable name and consequently
2272# included the '.exe' suffix.
2273#
2274if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2275complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
2276        || complete -o default -o nospace -F _git git.exe
2277fi