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