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