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