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