contrib / completion / git-completion.bashon commit completion: fix __gitcomp_builtin no longer consider extra options (276b49f)
   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#    *) git email aliases for git-send-email
  14#    *) tree paths within 'ref:path/to/file' expressions
  15#    *) file paths within current working directory and index
  16#    *) common --long-options
  17#
  18# To use these routines:
  19#
  20#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
  21#    2) Add the following line to your .bashrc/.zshrc:
  22#        source ~/.git-completion.bash
  23#    3) Consider changing your PS1 to also show the current branch,
  24#       see git-prompt.sh for details.
  25#
  26# If you use complex aliases of form '!f() { ... }; f', you can use the null
  27# command ':' as the first command in the function body to declare the desired
  28# completion style.  For example '!f() { : git commit ; ... }; f' will
  29# tell the completion to use commit completion.  This also works with aliases
  30# of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
  31#
  32# Compatible with bash 3.2.57.
  33#
  34# You can set the following environment variables to influence the behavior of
  35# the completion routines:
  36#
  37#   GIT_COMPLETION_CHECKOUT_NO_GUESS
  38#
  39#     When set to "1", do not include "DWIM" suggestions in git-checkout
  40#     completion (e.g., completing "foo" when "origin/foo" exists).
  41
  42case "$COMP_WORDBREAKS" in
  43*:*) : great ;;
  44*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  45esac
  46
  47# Discovers the path to the git repository taking any '--git-dir=<path>' and
  48# '-C <path>' options into account and stores it in the $__git_repo_path
  49# variable.
  50__git_find_repo_path ()
  51{
  52        if [ -n "$__git_repo_path" ]; then
  53                # we already know where it is
  54                return
  55        fi
  56
  57        if [ -n "${__git_C_args-}" ]; then
  58                __git_repo_path="$(git "${__git_C_args[@]}" \
  59                        ${__git_dir:+--git-dir="$__git_dir"} \
  60                        rev-parse --absolute-git-dir 2>/dev/null)"
  61        elif [ -n "${__git_dir-}" ]; then
  62                test -d "$__git_dir" &&
  63                __git_repo_path="$__git_dir"
  64        elif [ -n "${GIT_DIR-}" ]; then
  65                test -d "${GIT_DIR-}" &&
  66                __git_repo_path="$GIT_DIR"
  67        elif [ -d .git ]; then
  68                __git_repo_path=.git
  69        else
  70                __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
  71        fi
  72}
  73
  74# Deprecated: use __git_find_repo_path() and $__git_repo_path instead
  75# __gitdir accepts 0 or 1 arguments (i.e., location)
  76# returns location of .git repo
  77__gitdir ()
  78{
  79        if [ -z "${1-}" ]; then
  80                __git_find_repo_path || return 1
  81                echo "$__git_repo_path"
  82        elif [ -d "$1/.git" ]; then
  83                echo "$1/.git"
  84        else
  85                echo "$1"
  86        fi
  87}
  88
  89# Runs git with all the options given as argument, respecting any
  90# '--git-dir=<path>' and '-C <path>' options present on the command line
  91__git ()
  92{
  93        git ${__git_C_args:+"${__git_C_args[@]}"} \
  94                ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
  95}
  96
  97# The following function is based on code from:
  98#
  99#   bash_completion - programmable completion functions for bash 3.2+
 100#
 101#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
 102#             © 2009-2010, Bash Completion Maintainers
 103#                     <bash-completion-devel@lists.alioth.debian.org>
 104#
 105#   This program is free software; you can redistribute it and/or modify
 106#   it under the terms of the GNU General Public License as published by
 107#   the Free Software Foundation; either version 2, or (at your option)
 108#   any later version.
 109#
 110#   This program is distributed in the hope that it will be useful,
 111#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 112#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 113#   GNU General Public License for more details.
 114#
 115#   You should have received a copy of the GNU General Public License
 116#   along with this program; if not, see <http://www.gnu.org/licenses/>.
 117#
 118#   The latest version of this software can be obtained here:
 119#
 120#   http://bash-completion.alioth.debian.org/
 121#
 122#   RELEASE: 2.x
 123
 124# This function can be used to access a tokenized list of words
 125# on the command line:
 126#
 127#       __git_reassemble_comp_words_by_ref '=:'
 128#       if test "${words_[cword_-1]}" = -w
 129#       then
 130#               ...
 131#       fi
 132#
 133# The argument should be a collection of characters from the list of
 134# word completion separators (COMP_WORDBREAKS) to treat as ordinary
 135# characters.
 136#
 137# This is roughly equivalent to going back in time and setting
 138# COMP_WORDBREAKS to exclude those characters.  The intent is to
 139# make option types like --date=<type> and <rev>:<path> easy to
 140# recognize by treating each shell word as a single token.
 141#
 142# It is best not to set COMP_WORDBREAKS directly because the value is
 143# shared with other completion scripts.  By the time the completion
 144# function gets called, COMP_WORDS has already been populated so local
 145# changes to COMP_WORDBREAKS have no effect.
 146#
 147# Output: words_, cword_, cur_.
 148
 149__git_reassemble_comp_words_by_ref()
 150{
 151        local exclude i j first
 152        # Which word separators to exclude?
 153        exclude="${1//[^$COMP_WORDBREAKS]}"
 154        cword_=$COMP_CWORD
 155        if [ -z "$exclude" ]; then
 156                words_=("${COMP_WORDS[@]}")
 157                return
 158        fi
 159        # List of word completion separators has shrunk;
 160        # re-assemble words to complete.
 161        for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
 162                # Append each nonempty word consisting of just
 163                # word separator characters to the current word.
 164                first=t
 165                while
 166                        [ $i -gt 0 ] &&
 167                        [ -n "${COMP_WORDS[$i]}" ] &&
 168                        # word consists of excluded word separators
 169                        [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
 170                do
 171                        # Attach to the previous token,
 172                        # unless the previous token is the command name.
 173                        if [ $j -ge 2 ] && [ -n "$first" ]; then
 174                                ((j--))
 175                        fi
 176                        first=
 177                        words_[$j]=${words_[j]}${COMP_WORDS[i]}
 178                        if [ $i = $COMP_CWORD ]; then
 179                                cword_=$j
 180                        fi
 181                        if (($i < ${#COMP_WORDS[@]} - 1)); then
 182                                ((i++))
 183                        else
 184                                # Done.
 185                                return
 186                        fi
 187                done
 188                words_[$j]=${words_[j]}${COMP_WORDS[i]}
 189                if [ $i = $COMP_CWORD ]; then
 190                        cword_=$j
 191                fi
 192        done
 193}
 194
 195if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
 196_get_comp_words_by_ref ()
 197{
 198        local exclude cur_ words_ cword_
 199        if [ "$1" = "-n" ]; then
 200                exclude=$2
 201                shift 2
 202        fi
 203        __git_reassemble_comp_words_by_ref "$exclude"
 204        cur_=${words_[cword_]}
 205        while [ $# -gt 0 ]; do
 206                case "$1" in
 207                cur)
 208                        cur=$cur_
 209                        ;;
 210                prev)
 211                        prev=${words_[$cword_-1]}
 212                        ;;
 213                words)
 214                        words=("${words_[@]}")
 215                        ;;
 216                cword)
 217                        cword=$cword_
 218                        ;;
 219                esac
 220                shift
 221        done
 222}
 223fi
 224
 225# Fills the COMPREPLY array with prefiltered words without any additional
 226# processing.
 227# Callers must take care of providing only words that match the current word
 228# to be completed and adding any prefix and/or suffix (trailing space!), if
 229# necessary.
 230# 1: List of newline-separated matching completion words, complete with
 231#    prefix and suffix.
 232__gitcomp_direct ()
 233{
 234        local IFS=$'\n'
 235
 236        COMPREPLY=($1)
 237}
 238
 239__gitcompappend ()
 240{
 241        local x i=${#COMPREPLY[@]}
 242        for x in $1; do
 243                if [[ "$x" == "$3"* ]]; then
 244                        COMPREPLY[i++]="$2$x$4"
 245                fi
 246        done
 247}
 248
 249__gitcompadd ()
 250{
 251        COMPREPLY=()
 252        __gitcompappend "$@"
 253}
 254
 255# Generates completion reply, appending a space to possible completion words,
 256# if necessary.
 257# It accepts 1 to 4 arguments:
 258# 1: List of possible completion words.
 259# 2: A prefix to be added to each possible completion word (optional).
 260# 3: Generate possible completion matches for this word (optional).
 261# 4: A suffix to be appended to each possible completion word (optional).
 262__gitcomp ()
 263{
 264        local cur_="${3-$cur}"
 265
 266        case "$cur_" in
 267        --*=)
 268                ;;
 269        --no-*)
 270                local c i=0 IFS=$' \t\n'
 271                for c in $1; do
 272                        if [[ $c == "--" ]]; then
 273                                continue
 274                        fi
 275                        c="$c${4-}"
 276                        if [[ $c == "$cur_"* ]]; then
 277                                case $c in
 278                                --*=*|*.) ;;
 279                                *) c="$c " ;;
 280                                esac
 281                                COMPREPLY[i++]="${2-}$c"
 282                        fi
 283                done
 284                ;;
 285        *)
 286                local c i=0 IFS=$' \t\n'
 287                for c in $1; do
 288                        if [[ $c == "--" ]]; then
 289                                c="--no-...${4-}"
 290                                if [[ $c == "$cur_"* ]]; then
 291                                        COMPREPLY[i++]="${2-}$c "
 292                                fi
 293                                break
 294                        fi
 295                        c="$c${4-}"
 296                        if [[ $c == "$cur_"* ]]; then
 297                                case $c in
 298                                --*=*|*.) ;;
 299                                *) c="$c " ;;
 300                                esac
 301                                COMPREPLY[i++]="${2-}$c"
 302                        fi
 303                done
 304                ;;
 305        esac
 306}
 307
 308# Clear the variables caching builtins' options when (re-)sourcing
 309# the completion script.
 310if [[ -n ${ZSH_VERSION-} ]]; then
 311        unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
 312else
 313        unset $(compgen -v __gitcomp_builtin_)
 314fi
 315
 316# This function is equivalent to
 317#
 318#    __gitcomp "$(git xxx --git-completion-helper) ..."
 319#
 320# except that the output is cached. Accept 1-3 arguments:
 321# 1: the git command to execute, this is also the cache key
 322# 2: extra options to be added on top (e.g. negative forms)
 323# 3: options to be excluded
 324__gitcomp_builtin ()
 325{
 326        # spaces must be replaced with underscore for multi-word
 327        # commands, e.g. "git remote add" becomes remote_add.
 328        local cmd="$1"
 329        local incl="$2"
 330        local excl="$3"
 331
 332        local var=__gitcomp_builtin_"${cmd/-/_}"
 333        local options
 334        eval "options=\$$var"
 335
 336        if [ -z "$options" ]; then
 337                # leading and trailing spaces are significant to make
 338                # option removal work correctly.
 339                options=" $incl $(__git ${cmd/_/ } --git-completion-helper) "
 340                for i in $excl; do
 341                        options="${options/ $i / }"
 342                done
 343                eval "$var=\"$options\""
 344        fi
 345
 346        __gitcomp "$options"
 347}
 348
 349# Variation of __gitcomp_nl () that appends to the existing list of
 350# completion candidates, COMPREPLY.
 351__gitcomp_nl_append ()
 352{
 353        local IFS=$'\n'
 354        __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
 355}
 356
 357# Generates completion reply from newline-separated possible completion words
 358# by appending a space to all of them.
 359# It accepts 1 to 4 arguments:
 360# 1: List of possible completion words, separated by a single newline.
 361# 2: A prefix to be added to each possible completion word (optional).
 362# 3: Generate possible completion matches for this word (optional).
 363# 4: A suffix to be appended to each possible completion word instead of
 364#    the default space (optional).  If specified but empty, nothing is
 365#    appended.
 366__gitcomp_nl ()
 367{
 368        COMPREPLY=()
 369        __gitcomp_nl_append "$@"
 370}
 371
 372# Generates completion reply with compgen from newline-separated possible
 373# completion filenames.
 374# It accepts 1 to 3 arguments:
 375# 1: List of possible completion filenames, separated by a single newline.
 376# 2: A directory prefix to be added to each possible completion filename
 377#    (optional).
 378# 3: Generate possible completion matches for this word (optional).
 379__gitcomp_file ()
 380{
 381        local IFS=$'\n'
 382
 383        # XXX does not work when the directory prefix contains a tilde,
 384        # since tilde expansion is not applied.
 385        # This means that COMPREPLY will be empty and Bash default
 386        # completion will be used.
 387        __gitcompadd "$1" "${2-}" "${3-$cur}" ""
 388
 389        # use a hack to enable file mode in bash < 4
 390        compopt -o filenames +o nospace 2>/dev/null ||
 391        compgen -f /non-existing-dir/ > /dev/null
 392}
 393
 394# Execute 'git ls-files', unless the --committable option is specified, in
 395# which case it runs 'git diff-index' to find out the files that can be
 396# committed.  It return paths relative to the directory specified in the first
 397# argument, and using the options specified in the second argument.
 398__git_ls_files_helper ()
 399{
 400        if [ "$2" == "--committable" ]; then
 401                __git -C "$1" diff-index --name-only --relative HEAD
 402        else
 403                # NOTE: $2 is not quoted in order to support multiple options
 404                __git -C "$1" ls-files --exclude-standard $2
 405        fi
 406}
 407
 408
 409# __git_index_files accepts 1 or 2 arguments:
 410# 1: Options to pass to ls-files (required).
 411# 2: A directory path (optional).
 412#    If provided, only files within the specified directory are listed.
 413#    Sub directories are never recursed.  Path must have a trailing
 414#    slash.
 415__git_index_files ()
 416{
 417        local root="${2-.}" file
 418
 419        __git_ls_files_helper "$root" "$1" |
 420        cut -f1 -d/ | sort | uniq
 421}
 422
 423# Lists branches from the local repository.
 424# 1: A prefix to be added to each listed branch (optional).
 425# 2: List only branches matching this word (optional; list all branches if
 426#    unset or empty).
 427# 3: A suffix to be appended to each listed branch (optional).
 428__git_heads ()
 429{
 430        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 431
 432        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 433                        "refs/heads/$cur_*" "refs/heads/$cur_*/**"
 434}
 435
 436# Lists tags from the local repository.
 437# Accepts the same positional parameters as __git_heads() above.
 438__git_tags ()
 439{
 440        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 441
 442        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 443                        "refs/tags/$cur_*" "refs/tags/$cur_*/**"
 444}
 445
 446# Lists refs from the local (by default) or from a remote repository.
 447# It accepts 0, 1 or 2 arguments:
 448# 1: The remote to list refs from (optional; ignored, if set but empty).
 449#    Can be the name of a configured remote, a path, or a URL.
 450# 2: In addition to local refs, list unique branches from refs/remotes/ for
 451#    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
 452# 3: A prefix to be added to each listed ref (optional).
 453# 4: List only refs matching this word (optional; list all refs if unset or
 454#    empty).
 455# 5: A suffix to be appended to each listed ref (optional; ignored, if set
 456#    but empty).
 457#
 458# Use __git_complete_refs() instead.
 459__git_refs ()
 460{
 461        local i hash dir track="${2-}"
 462        local list_refs_from=path remote="${1-}"
 463        local format refs
 464        local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
 465        local match="${4-}"
 466        local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
 467
 468        __git_find_repo_path
 469        dir="$__git_repo_path"
 470
 471        if [ -z "$remote" ]; then
 472                if [ -z "$dir" ]; then
 473                        return
 474                fi
 475        else
 476                if __git_is_configured_remote "$remote"; then
 477                        # configured remote takes precedence over a
 478                        # local directory with the same name
 479                        list_refs_from=remote
 480                elif [ -d "$remote/.git" ]; then
 481                        dir="$remote/.git"
 482                elif [ -d "$remote" ]; then
 483                        dir="$remote"
 484                else
 485                        list_refs_from=url
 486                fi
 487        fi
 488
 489        if [ "$list_refs_from" = path ]; then
 490                if [[ "$cur_" == ^* ]]; then
 491                        pfx="$pfx^"
 492                        fer_pfx="$fer_pfx^"
 493                        cur_=${cur_#^}
 494                        match=${match#^}
 495                fi
 496                case "$cur_" in
 497                refs|refs/*)
 498                        format="refname"
 499                        refs=("$match*" "$match*/**")
 500                        track=""
 501                        ;;
 502                *)
 503                        for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
 504                                case "$i" in
 505                                $match*)
 506                                        if [ -e "$dir/$i" ]; then
 507                                                echo "$pfx$i$sfx"
 508                                        fi
 509                                        ;;
 510                                esac
 511                        done
 512                        format="refname:strip=2"
 513                        refs=("refs/tags/$match*" "refs/tags/$match*/**"
 514                                "refs/heads/$match*" "refs/heads/$match*/**"
 515                                "refs/remotes/$match*" "refs/remotes/$match*/**")
 516                        ;;
 517                esac
 518                __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
 519                        "${refs[@]}"
 520                if [ -n "$track" ]; then
 521                        # employ the heuristic used by git checkout
 522                        # Try to find a remote branch that matches the completion word
 523                        # but only output if the branch name is unique
 524                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 525                                --sort="refname:strip=3" \
 526                                "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
 527                        uniq -u
 528                fi
 529                return
 530        fi
 531        case "$cur_" in
 532        refs|refs/*)
 533                __git ls-remote "$remote" "$match*" | \
 534                while read -r hash i; do
 535                        case "$i" in
 536                        *^{}) ;;
 537                        *) echo "$pfx$i$sfx" ;;
 538                        esac
 539                done
 540                ;;
 541        *)
 542                if [ "$list_refs_from" = remote ]; then
 543                        case "HEAD" in
 544                        $match*)        echo "${pfx}HEAD$sfx" ;;
 545                        esac
 546                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 547                                "refs/remotes/$remote/$match*" \
 548                                "refs/remotes/$remote/$match*/**"
 549                else
 550                        local query_symref
 551                        case "HEAD" in
 552                        $match*)        query_symref="HEAD" ;;
 553                        esac
 554                        __git ls-remote "$remote" $query_symref \
 555                                "refs/tags/$match*" "refs/heads/$match*" \
 556                                "refs/remotes/$match*" |
 557                        while read -r hash i; do
 558                                case "$i" in
 559                                *^{})   ;;
 560                                refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
 561                                *)      echo "$pfx$i$sfx" ;;  # symbolic refs
 562                                esac
 563                        done
 564                fi
 565                ;;
 566        esac
 567}
 568
 569# Completes refs, short and long, local and remote, symbolic and pseudo.
 570#
 571# Usage: __git_complete_refs [<option>]...
 572# --remote=<remote>: The remote to list refs from, can be the name of a
 573#                    configured remote, a path, or a URL.
 574# --track: List unique remote branches for 'git checkout's tracking DWIMery.
 575# --pfx=<prefix>: A prefix to be added to each ref.
 576# --cur=<word>: The current ref to be completed.  Defaults to the current
 577#               word to be completed.
 578# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
 579#                 space.
 580__git_complete_refs ()
 581{
 582        local remote track pfx cur_="$cur" sfx=" "
 583
 584        while test $# != 0; do
 585                case "$1" in
 586                --remote=*)     remote="${1##--remote=}" ;;
 587                --track)        track="yes" ;;
 588                --pfx=*)        pfx="${1##--pfx=}" ;;
 589                --cur=*)        cur_="${1##--cur=}" ;;
 590                --sfx=*)        sfx="${1##--sfx=}" ;;
 591                *)              return 1 ;;
 592                esac
 593                shift
 594        done
 595
 596        __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
 597}
 598
 599# __git_refs2 requires 1 argument (to pass to __git_refs)
 600# Deprecated: use __git_complete_fetch_refspecs() instead.
 601__git_refs2 ()
 602{
 603        local i
 604        for i in $(__git_refs "$1"); do
 605                echo "$i:$i"
 606        done
 607}
 608
 609# Completes refspecs for fetching from a remote repository.
 610# 1: The remote repository.
 611# 2: A prefix to be added to each listed refspec (optional).
 612# 3: The ref to be completed as a refspec instead of the current word to be
 613#    completed (optional)
 614# 4: A suffix to be appended to each listed refspec instead of the default
 615#    space (optional).
 616__git_complete_fetch_refspecs ()
 617{
 618        local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
 619
 620        __gitcomp_direct "$(
 621                for i in $(__git_refs "$remote" "" "" "$cur_") ; do
 622                        echo "$pfx$i:$i$sfx"
 623                done
 624                )"
 625}
 626
 627# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 628__git_refs_remotes ()
 629{
 630        local i hash
 631        __git ls-remote "$1" 'refs/heads/*' | \
 632        while read -r hash i; do
 633                echo "$i:refs/remotes/$1/${i#refs/heads/}"
 634        done
 635}
 636
 637__git_remotes ()
 638{
 639        __git_find_repo_path
 640        test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
 641        __git remote
 642}
 643
 644# Returns true if $1 matches the name of a configured remote, false otherwise.
 645__git_is_configured_remote ()
 646{
 647        local remote
 648        for remote in $(__git_remotes); do
 649                if [ "$remote" = "$1" ]; then
 650                        return 0
 651                fi
 652        done
 653        return 1
 654}
 655
 656__git_list_merge_strategies ()
 657{
 658        LANG=C LC_ALL=C git merge -s help 2>&1 |
 659        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 660                s/\.$//
 661                s/.*://
 662                s/^[    ]*//
 663                s/[     ]*$//
 664                p
 665        }'
 666}
 667
 668__git_merge_strategies=
 669# 'git merge -s help' (and thus detection of the merge strategy
 670# list) fails, unfortunately, if run outside of any git working
 671# tree.  __git_merge_strategies is set to the empty string in
 672# that case, and the detection will be repeated the next time it
 673# is needed.
 674__git_compute_merge_strategies ()
 675{
 676        test -n "$__git_merge_strategies" ||
 677        __git_merge_strategies=$(__git_list_merge_strategies)
 678}
 679
 680__git_complete_revlist_file ()
 681{
 682        local pfx ls ref cur_="$cur"
 683        case "$cur_" in
 684        *..?*:*)
 685                return
 686                ;;
 687        ?*:*)
 688                ref="${cur_%%:*}"
 689                cur_="${cur_#*:}"
 690                case "$cur_" in
 691                ?*/*)
 692                        pfx="${cur_%/*}"
 693                        cur_="${cur_##*/}"
 694                        ls="$ref:$pfx"
 695                        pfx="$pfx/"
 696                        ;;
 697                *)
 698                        ls="$ref"
 699                        ;;
 700                esac
 701
 702                case "$COMP_WORDBREAKS" in
 703                *:*) : great ;;
 704                *)   pfx="$ref:$pfx" ;;
 705                esac
 706
 707                __gitcomp_nl "$(__git ls-tree "$ls" \
 708                                | sed '/^100... blob /{
 709                                           s,^.*        ,,
 710                                           s,$, ,
 711                                       }
 712                                       /^120000 blob /{
 713                                           s,^.*        ,,
 714                                           s,$, ,
 715                                       }
 716                                       /^040000 tree /{
 717                                           s,^.*        ,,
 718                                           s,$,/,
 719                                       }
 720                                       s/^.*    //')" \
 721                        "$pfx" "$cur_" ""
 722                ;;
 723        *...*)
 724                pfx="${cur_%...*}..."
 725                cur_="${cur_#*...}"
 726                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 727                ;;
 728        *..*)
 729                pfx="${cur_%..*}.."
 730                cur_="${cur_#*..}"
 731                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 732                ;;
 733        *)
 734                __git_complete_refs
 735                ;;
 736        esac
 737}
 738
 739
 740# __git_complete_index_file requires 1 argument:
 741# 1: the options to pass to ls-file
 742#
 743# The exception is --committable, which finds the files appropriate commit.
 744__git_complete_index_file ()
 745{
 746        local pfx="" cur_="$cur"
 747
 748        case "$cur_" in
 749        ?*/*)
 750                pfx="${cur_%/*}"
 751                cur_="${cur_##*/}"
 752                pfx="${pfx}/"
 753                ;;
 754        esac
 755
 756        __gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
 757}
 758
 759__git_complete_file ()
 760{
 761        __git_complete_revlist_file
 762}
 763
 764__git_complete_revlist ()
 765{
 766        __git_complete_revlist_file
 767}
 768
 769__git_complete_remote_or_refspec ()
 770{
 771        local cur_="$cur" cmd="${words[1]}"
 772        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
 773        if [ "$cmd" = "remote" ]; then
 774                ((c++))
 775        fi
 776        while [ $c -lt $cword ]; do
 777                i="${words[c]}"
 778                case "$i" in
 779                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
 780                -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
 781                --all)
 782                        case "$cmd" in
 783                        push) no_complete_refspec=1 ;;
 784                        fetch)
 785                                return
 786                                ;;
 787                        *) ;;
 788                        esac
 789                        ;;
 790                -*) ;;
 791                *) remote="$i"; break ;;
 792                esac
 793                ((c++))
 794        done
 795        if [ -z "$remote" ]; then
 796                __gitcomp_nl "$(__git_remotes)"
 797                return
 798        fi
 799        if [ $no_complete_refspec = 1 ]; then
 800                return
 801        fi
 802        [ "$remote" = "." ] && remote=
 803        case "$cur_" in
 804        *:*)
 805                case "$COMP_WORDBREAKS" in
 806                *:*) : great ;;
 807                *)   pfx="${cur_%%:*}:" ;;
 808                esac
 809                cur_="${cur_#*:}"
 810                lhs=0
 811                ;;
 812        +*)
 813                pfx="+"
 814                cur_="${cur_#+}"
 815                ;;
 816        esac
 817        case "$cmd" in
 818        fetch)
 819                if [ $lhs = 1 ]; then
 820                        __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
 821                else
 822                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 823                fi
 824                ;;
 825        pull|remote)
 826                if [ $lhs = 1 ]; then
 827                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 828                else
 829                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 830                fi
 831                ;;
 832        push)
 833                if [ $lhs = 1 ]; then
 834                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 835                else
 836                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 837                fi
 838                ;;
 839        esac
 840}
 841
 842__git_complete_strategy ()
 843{
 844        __git_compute_merge_strategies
 845        case "$prev" in
 846        -s|--strategy)
 847                __gitcomp "$__git_merge_strategies"
 848                return 0
 849        esac
 850        case "$cur" in
 851        --strategy=*)
 852                __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
 853                return 0
 854                ;;
 855        esac
 856        return 1
 857}
 858
 859__git_commands () {
 860        if test -n "${GIT_TESTING_COMMAND_COMPLETION:-}"
 861        then
 862                printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
 863        else
 864                git help -a|egrep '^  [a-zA-Z0-9]'
 865        fi
 866}
 867
 868__git_list_all_commands ()
 869{
 870        local i IFS=" "$'\n'
 871        for i in $(__git_commands)
 872        do
 873                case $i in
 874                *--*)             : helper pattern;;
 875                *) echo $i;;
 876                esac
 877        done
 878}
 879
 880__git_all_commands=
 881__git_compute_all_commands ()
 882{
 883        test -n "$__git_all_commands" ||
 884        __git_all_commands=$(__git_list_all_commands)
 885}
 886
 887__git_list_porcelain_commands ()
 888{
 889        local i IFS=" "$'\n'
 890        __git_compute_all_commands
 891        for i in $__git_all_commands
 892        do
 893                case $i in
 894                *--*)             : helper pattern;;
 895                applymbox)        : ask gittus;;
 896                applypatch)       : ask gittus;;
 897                archimport)       : import;;
 898                cat-file)         : plumbing;;
 899                check-attr)       : plumbing;;
 900                check-ignore)     : plumbing;;
 901                check-mailmap)    : plumbing;;
 902                check-ref-format) : plumbing;;
 903                checkout-index)   : plumbing;;
 904                column)           : internal helper;;
 905                commit-graph)     : plumbing;;
 906                commit-tree)      : plumbing;;
 907                count-objects)    : infrequent;;
 908                credential)       : credentials;;
 909                credential-*)     : credentials helper;;
 910                cvsexportcommit)  : export;;
 911                cvsimport)        : import;;
 912                cvsserver)        : daemon;;
 913                daemon)           : daemon;;
 914                diff-files)       : plumbing;;
 915                diff-index)       : plumbing;;
 916                diff-tree)        : plumbing;;
 917                fast-import)      : import;;
 918                fast-export)      : export;;
 919                fsck-objects)     : plumbing;;
 920                fetch-pack)       : plumbing;;
 921                fmt-merge-msg)    : plumbing;;
 922                for-each-ref)     : plumbing;;
 923                hash-object)      : plumbing;;
 924                http-*)           : transport;;
 925                index-pack)       : plumbing;;
 926                init-db)          : deprecated;;
 927                local-fetch)      : plumbing;;
 928                ls-files)         : plumbing;;
 929                ls-remote)        : plumbing;;
 930                ls-tree)          : plumbing;;
 931                mailinfo)         : plumbing;;
 932                mailsplit)        : plumbing;;
 933                merge-*)          : plumbing;;
 934                mktree)           : plumbing;;
 935                mktag)            : plumbing;;
 936                pack-objects)     : plumbing;;
 937                pack-redundant)   : plumbing;;
 938                pack-refs)        : plumbing;;
 939                parse-remote)     : plumbing;;
 940                patch-id)         : plumbing;;
 941                prune)            : plumbing;;
 942                prune-packed)     : plumbing;;
 943                quiltimport)      : import;;
 944                read-tree)        : plumbing;;
 945                receive-pack)     : plumbing;;
 946                remote-*)         : transport;;
 947                rerere)           : plumbing;;
 948                rev-list)         : plumbing;;
 949                rev-parse)        : plumbing;;
 950                runstatus)        : plumbing;;
 951                sh-setup)         : internal;;
 952                shell)            : daemon;;
 953                show-ref)         : plumbing;;
 954                send-pack)        : plumbing;;
 955                show-index)       : plumbing;;
 956                ssh-*)            : transport;;
 957                stripspace)       : plumbing;;
 958                symbolic-ref)     : plumbing;;
 959                unpack-file)      : plumbing;;
 960                unpack-objects)   : plumbing;;
 961                update-index)     : plumbing;;
 962                update-ref)       : plumbing;;
 963                update-server-info) : daemon;;
 964                upload-archive)   : plumbing;;
 965                upload-pack)      : plumbing;;
 966                write-tree)       : plumbing;;
 967                var)              : infrequent;;
 968                verify-pack)      : infrequent;;
 969                verify-tag)       : plumbing;;
 970                *) echo $i;;
 971                esac
 972        done
 973}
 974
 975__git_porcelain_commands=
 976__git_compute_porcelain_commands ()
 977{
 978        test -n "$__git_porcelain_commands" ||
 979        __git_porcelain_commands=$(__git_list_porcelain_commands)
 980}
 981
 982# Lists all set config variables starting with the given section prefix,
 983# with the prefix removed.
 984__git_get_config_variables ()
 985{
 986        local section="$1" i IFS=$'\n'
 987        for i in $(__git config --name-only --get-regexp "^$section\..*"); do
 988                echo "${i#$section.}"
 989        done
 990}
 991
 992__git_pretty_aliases ()
 993{
 994        __git_get_config_variables "pretty"
 995}
 996
 997__git_aliases ()
 998{
 999        __git_get_config_variables "alias"
1000}
1001
1002# __git_aliased_command requires 1 argument
1003__git_aliased_command ()
1004{
1005        local word cmdline=$(__git config --get "alias.$1")
1006        for word in $cmdline; do
1007                case "$word" in
1008                \!gitk|gitk)
1009                        echo "gitk"
1010                        return
1011                        ;;
1012                \!*)    : shell command alias ;;
1013                -*)     : option ;;
1014                *=*)    : setting env ;;
1015                git)    : git itself ;;
1016                \(\))   : skip parens of shell function definition ;;
1017                {)      : skip start of shell helper function ;;
1018                :)      : skip null command ;;
1019                \'*)    : skip opening quote after sh -c ;;
1020                *)
1021                        echo "$word"
1022                        return
1023                esac
1024        done
1025}
1026
1027# __git_find_on_cmdline requires 1 argument
1028__git_find_on_cmdline ()
1029{
1030        local word subcommand c=1
1031        while [ $c -lt $cword ]; do
1032                word="${words[c]}"
1033                for subcommand in $1; do
1034                        if [ "$subcommand" = "$word" ]; then
1035                                echo "$subcommand"
1036                                return
1037                        fi
1038                done
1039                ((c++))
1040        done
1041}
1042
1043# Echo the value of an option set on the command line or config
1044#
1045# $1: short option name
1046# $2: long option name including =
1047# $3: list of possible values
1048# $4: config string (optional)
1049#
1050# example:
1051# result="$(__git_get_option_value "-d" "--do-something=" \
1052#     "yes no" "core.doSomething")"
1053#
1054# result is then either empty (no option set) or "yes" or "no"
1055#
1056# __git_get_option_value requires 3 arguments
1057__git_get_option_value ()
1058{
1059        local c short_opt long_opt val
1060        local result= values config_key word
1061
1062        short_opt="$1"
1063        long_opt="$2"
1064        values="$3"
1065        config_key="$4"
1066
1067        ((c = $cword - 1))
1068        while [ $c -ge 0 ]; do
1069                word="${words[c]}"
1070                for val in $values; do
1071                        if [ "$short_opt$val" = "$word" ] ||
1072                           [ "$long_opt$val"  = "$word" ]; then
1073                                result="$val"
1074                                break 2
1075                        fi
1076                done
1077                ((c--))
1078        done
1079
1080        if [ -n "$config_key" ] && [ -z "$result" ]; then
1081                result="$(__git config "$config_key")"
1082        fi
1083
1084        echo "$result"
1085}
1086
1087__git_has_doubledash ()
1088{
1089        local c=1
1090        while [ $c -lt $cword ]; do
1091                if [ "--" = "${words[c]}" ]; then
1092                        return 0
1093                fi
1094                ((c++))
1095        done
1096        return 1
1097}
1098
1099# Try to count non option arguments passed on the command line for the
1100# specified git command.
1101# When options are used, it is necessary to use the special -- option to
1102# tell the implementation were non option arguments begin.
1103# XXX this can not be improved, since options can appear everywhere, as
1104# an example:
1105#       git mv x -n y
1106#
1107# __git_count_arguments requires 1 argument: the git command executed.
1108__git_count_arguments ()
1109{
1110        local word i c=0
1111
1112        # Skip "git" (first argument)
1113        for ((i=1; i < ${#words[@]}; i++)); do
1114                word="${words[i]}"
1115
1116                case "$word" in
1117                        --)
1118                                # Good; we can assume that the following are only non
1119                                # option arguments.
1120                                ((c = 0))
1121                                ;;
1122                        "$1")
1123                                # Skip the specified git command and discard git
1124                                # main options
1125                                ((c = 0))
1126                                ;;
1127                        ?*)
1128                                ((c++))
1129                                ;;
1130                esac
1131        done
1132
1133        printf "%d" $c
1134}
1135
1136__git_whitespacelist="nowarn warn error error-all fix"
1137__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
1138
1139_git_am ()
1140{
1141        __git_find_repo_path
1142        if [ -d "$__git_repo_path"/rebase-apply ]; then
1143                __gitcomp "$__git_am_inprogress_options"
1144                return
1145        fi
1146        case "$cur" in
1147        --whitespace=*)
1148                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1149                return
1150                ;;
1151        --*)
1152                __gitcomp_builtin am "" \
1153                        "$__git_am_inprogress_options"
1154                return
1155        esac
1156}
1157
1158_git_apply ()
1159{
1160        case "$cur" in
1161        --whitespace=*)
1162                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1163                return
1164                ;;
1165        --*)
1166                __gitcomp_builtin apply
1167                return
1168        esac
1169}
1170
1171_git_add ()
1172{
1173        case "$cur" in
1174        --*)
1175                __gitcomp_builtin add
1176                return
1177        esac
1178
1179        local complete_opt="--others --modified --directory --no-empty-directory"
1180        if test -n "$(__git_find_on_cmdline "-u --update")"
1181        then
1182                complete_opt="--modified"
1183        fi
1184        __git_complete_index_file "$complete_opt"
1185}
1186
1187_git_archive ()
1188{
1189        case "$cur" in
1190        --format=*)
1191                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1192                return
1193                ;;
1194        --remote=*)
1195                __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1196                return
1197                ;;
1198        --*)
1199                __gitcomp "
1200                        --format= --list --verbose
1201                        --prefix= --remote= --exec= --output
1202                        "
1203                return
1204                ;;
1205        esac
1206        __git_complete_file
1207}
1208
1209_git_bisect ()
1210{
1211        __git_has_doubledash && return
1212
1213        local subcommands="start bad good skip reset visualize replay log run"
1214        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1215        if [ -z "$subcommand" ]; then
1216                __git_find_repo_path
1217                if [ -f "$__git_repo_path"/BISECT_START ]; then
1218                        __gitcomp "$subcommands"
1219                else
1220                        __gitcomp "replay start"
1221                fi
1222                return
1223        fi
1224
1225        case "$subcommand" in
1226        bad|good|reset|skip|start)
1227                __git_complete_refs
1228                ;;
1229        *)
1230                ;;
1231        esac
1232}
1233
1234_git_branch ()
1235{
1236        local i c=1 only_local_ref="n" has_r="n"
1237
1238        while [ $c -lt $cword ]; do
1239                i="${words[c]}"
1240                case "$i" in
1241                -d|--delete|-m|--move)  only_local_ref="y" ;;
1242                -r|--remotes)           has_r="y" ;;
1243                esac
1244                ((c++))
1245        done
1246
1247        case "$cur" in
1248        --set-upstream-to=*)
1249                __git_complete_refs --cur="${cur##--set-upstream-to=}"
1250                ;;
1251        --*)
1252                __gitcomp_builtin branch
1253                ;;
1254        *)
1255                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1256                        __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1257                else
1258                        __git_complete_refs
1259                fi
1260                ;;
1261        esac
1262}
1263
1264_git_bundle ()
1265{
1266        local cmd="${words[2]}"
1267        case "$cword" in
1268        2)
1269                __gitcomp "create list-heads verify unbundle"
1270                ;;
1271        3)
1272                # looking for a file
1273                ;;
1274        *)
1275                case "$cmd" in
1276                        create)
1277                                __git_complete_revlist
1278                        ;;
1279                esac
1280                ;;
1281        esac
1282}
1283
1284_git_checkout ()
1285{
1286        __git_has_doubledash && return
1287
1288        case "$cur" in
1289        --conflict=*)
1290                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1291                ;;
1292        --*)
1293                __gitcomp_builtin checkout
1294                ;;
1295        *)
1296                # check if --track, --no-track, or --no-guess was specified
1297                # if so, disable DWIM mode
1298                local flags="--track --no-track --no-guess" track_opt="--track"
1299                if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1300                   [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1301                        track_opt=''
1302                fi
1303                __git_complete_refs $track_opt
1304                ;;
1305        esac
1306}
1307
1308_git_cherry ()
1309{
1310        case "$cur" in
1311        --*)
1312                __gitcomp_builtin cherry
1313                return
1314        esac
1315
1316        __git_complete_refs
1317}
1318
1319__git_cherry_pick_inprogress_options="--continue --quit --abort"
1320
1321_git_cherry_pick ()
1322{
1323        __git_find_repo_path
1324        if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1325                __gitcomp "$__git_cherry_pick_inprogress_options"
1326                return
1327        fi
1328        case "$cur" in
1329        --*)
1330                __gitcomp_builtin cherry-pick "" \
1331                        "$__git_cherry_pick_inprogress_options"
1332                ;;
1333        *)
1334                __git_complete_refs
1335                ;;
1336        esac
1337}
1338
1339_git_clean ()
1340{
1341        case "$cur" in
1342        --*)
1343                __gitcomp_builtin clean
1344                return
1345                ;;
1346        esac
1347
1348        # XXX should we check for -x option ?
1349        __git_complete_index_file "--others --directory"
1350}
1351
1352_git_clone ()
1353{
1354        case "$cur" in
1355        --*)
1356                __gitcomp_builtin clone
1357                return
1358                ;;
1359        esac
1360}
1361
1362__git_untracked_file_modes="all no normal"
1363
1364_git_commit ()
1365{
1366        case "$prev" in
1367        -c|-C)
1368                __git_complete_refs
1369                return
1370                ;;
1371        esac
1372
1373        case "$cur" in
1374        --cleanup=*)
1375                __gitcomp "default scissors strip verbatim whitespace
1376                        " "" "${cur##--cleanup=}"
1377                return
1378                ;;
1379        --reuse-message=*|--reedit-message=*|\
1380        --fixup=*|--squash=*)
1381                __git_complete_refs --cur="${cur#*=}"
1382                return
1383                ;;
1384        --untracked-files=*)
1385                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1386                return
1387                ;;
1388        --*)
1389                __gitcomp_builtin commit
1390                return
1391        esac
1392
1393        if __git rev-parse --verify --quiet HEAD >/dev/null; then
1394                __git_complete_index_file "--committable"
1395        else
1396                # This is the first commit
1397                __git_complete_index_file "--cached"
1398        fi
1399}
1400
1401_git_describe ()
1402{
1403        case "$cur" in
1404        --*)
1405                __gitcomp_builtin describe
1406                return
1407        esac
1408        __git_complete_refs
1409}
1410
1411__git_diff_algorithms="myers minimal patience histogram"
1412
1413__git_diff_submodule_formats="diff log short"
1414
1415__git_diff_common_options="--stat --numstat --shortstat --summary
1416                        --patch-with-stat --name-only --name-status --color
1417                        --no-color --color-words --no-renames --check
1418                        --full-index --binary --abbrev --diff-filter=
1419                        --find-copies-harder --ignore-cr-at-eol
1420                        --text --ignore-space-at-eol --ignore-space-change
1421                        --ignore-all-space --ignore-blank-lines --exit-code
1422                        --quiet --ext-diff --no-ext-diff
1423                        --no-prefix --src-prefix= --dst-prefix=
1424                        --inter-hunk-context=
1425                        --patience --histogram --minimal
1426                        --raw --word-diff --word-diff-regex=
1427                        --dirstat --dirstat= --dirstat-by-file
1428                        --dirstat-by-file= --cumulative
1429                        --diff-algorithm=
1430                        --submodule --submodule= --ignore-submodules
1431"
1432
1433_git_diff ()
1434{
1435        __git_has_doubledash && return
1436
1437        case "$cur" in
1438        --diff-algorithm=*)
1439                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1440                return
1441                ;;
1442        --submodule=*)
1443                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1444                return
1445                ;;
1446        --*)
1447                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1448                        --base --ours --theirs --no-index
1449                        $__git_diff_common_options
1450                        "
1451                return
1452                ;;
1453        esac
1454        __git_complete_revlist_file
1455}
1456
1457__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1458                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1459"
1460
1461_git_difftool ()
1462{
1463        __git_has_doubledash && return
1464
1465        case "$cur" in
1466        --tool=*)
1467                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1468                return
1469                ;;
1470        --*)
1471                __gitcomp_builtin difftool "$__git_diff_common_options
1472                                        --base --cached --ours --theirs
1473                                        --pickaxe-all --pickaxe-regex
1474                                        --relative --staged
1475                                        "
1476                return
1477                ;;
1478        esac
1479        __git_complete_revlist_file
1480}
1481
1482__git_fetch_recurse_submodules="yes on-demand no"
1483
1484_git_fetch ()
1485{
1486        case "$cur" in
1487        --recurse-submodules=*)
1488                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1489                return
1490                ;;
1491        --*)
1492                __gitcomp_builtin fetch
1493                return
1494                ;;
1495        esac
1496        __git_complete_remote_or_refspec
1497}
1498
1499__git_format_patch_options="
1500        --stdout --attach --no-attach --thread --thread= --no-thread
1501        --numbered --start-number --numbered-files --keep-subject --signoff
1502        --signature --no-signature --in-reply-to= --cc= --full-index --binary
1503        --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1504        --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1505        --output-directory --reroll-count --to= --quiet --notes
1506"
1507
1508_git_format_patch ()
1509{
1510        case "$cur" in
1511        --thread=*)
1512                __gitcomp "
1513                        deep shallow
1514                        " "" "${cur##--thread=}"
1515                return
1516                ;;
1517        --*)
1518                __gitcomp "$__git_format_patch_options"
1519                return
1520                ;;
1521        esac
1522        __git_complete_revlist
1523}
1524
1525_git_fsck ()
1526{
1527        case "$cur" in
1528        --*)
1529                __gitcomp_builtin fsck
1530                return
1531                ;;
1532        esac
1533}
1534
1535_git_gitk ()
1536{
1537        _gitk
1538}
1539
1540# Lists matching symbol names from a tag (as in ctags) file.
1541# 1: List symbol names matching this word.
1542# 2: The tag file to list symbol names from.
1543# 3: A prefix to be added to each listed symbol name (optional).
1544# 4: A suffix to be appended to each listed symbol name (optional).
1545__git_match_ctag () {
1546        awk -v pfx="${3-}" -v sfx="${4-}" "
1547                /^${1//\//\\/}/ { print pfx \$1 sfx }
1548                " "$2"
1549}
1550
1551# Complete symbol names from a tag file.
1552# Usage: __git_complete_symbol [<option>]...
1553# --tags=<file>: The tag file to list symbol names from instead of the
1554#                default "tags".
1555# --pfx=<prefix>: A prefix to be added to each symbol name.
1556# --cur=<word>: The current symbol name to be completed.  Defaults to
1557#               the current word to be completed.
1558# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1559#                 of the default space.
1560__git_complete_symbol () {
1561        local tags=tags pfx="" cur_="${cur-}" sfx=" "
1562
1563        while test $# != 0; do
1564                case "$1" in
1565                --tags=*)       tags="${1##--tags=}" ;;
1566                --pfx=*)        pfx="${1##--pfx=}" ;;
1567                --cur=*)        cur_="${1##--cur=}" ;;
1568                --sfx=*)        sfx="${1##--sfx=}" ;;
1569                *)              return 1 ;;
1570                esac
1571                shift
1572        done
1573
1574        if test -r "$tags"; then
1575                __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1576        fi
1577}
1578
1579_git_grep ()
1580{
1581        __git_has_doubledash && return
1582
1583        case "$cur" in
1584        --*)
1585                __gitcomp_builtin grep
1586                return
1587                ;;
1588        esac
1589
1590        case "$cword,$prev" in
1591        2,*|*,-*)
1592                __git_complete_symbol && return
1593                ;;
1594        esac
1595
1596        __git_complete_refs
1597}
1598
1599_git_help ()
1600{
1601        case "$cur" in
1602        --*)
1603                __gitcomp_builtin help
1604                return
1605                ;;
1606        esac
1607        __git_compute_all_commands
1608        __gitcomp "$__git_all_commands $(__git_aliases)
1609                attributes cli core-tutorial cvs-migration
1610                diffcore everyday gitk glossary hooks ignore modules
1611                namespaces repository-layout revisions tutorial tutorial-2
1612                workflows
1613                "
1614}
1615
1616_git_init ()
1617{
1618        case "$cur" in
1619        --shared=*)
1620                __gitcomp "
1621                        false true umask group all world everybody
1622                        " "" "${cur##--shared=}"
1623                return
1624                ;;
1625        --*)
1626                __gitcomp_builtin init
1627                return
1628                ;;
1629        esac
1630}
1631
1632_git_ls_files ()
1633{
1634        case "$cur" in
1635        --*)
1636                __gitcomp_builtin ls-files
1637                return
1638                ;;
1639        esac
1640
1641        # XXX ignore options like --modified and always suggest all cached
1642        # files.
1643        __git_complete_index_file "--cached"
1644}
1645
1646_git_ls_remote ()
1647{
1648        case "$cur" in
1649        --*)
1650                __gitcomp_builtin ls-remote
1651                return
1652                ;;
1653        esac
1654        __gitcomp_nl "$(__git_remotes)"
1655}
1656
1657_git_ls_tree ()
1658{
1659        case "$cur" in
1660        --*)
1661                __gitcomp_builtin ls-tree
1662                return
1663                ;;
1664        esac
1665
1666        __git_complete_file
1667}
1668
1669# Options that go well for log, shortlog and gitk
1670__git_log_common_options="
1671        --not --all
1672        --branches --tags --remotes
1673        --first-parent --merges --no-merges
1674        --max-count=
1675        --max-age= --since= --after=
1676        --min-age= --until= --before=
1677        --min-parents= --max-parents=
1678        --no-min-parents --no-max-parents
1679"
1680# Options that go well for log and gitk (not shortlog)
1681__git_log_gitk_options="
1682        --dense --sparse --full-history
1683        --simplify-merges --simplify-by-decoration
1684        --left-right --notes --no-notes
1685"
1686# Options that go well for log and shortlog (not gitk)
1687__git_log_shortlog_options="
1688        --author= --committer= --grep=
1689        --all-match --invert-grep
1690"
1691
1692__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1693__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1694
1695_git_log ()
1696{
1697        __git_has_doubledash && return
1698        __git_find_repo_path
1699
1700        local merge=""
1701        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1702                merge="--merge"
1703        fi
1704        case "$prev,$cur" in
1705        -L,:*:*)
1706                return  # fall back to Bash filename completion
1707                ;;
1708        -L,:*)
1709                __git_complete_symbol --cur="${cur#:}" --sfx=":"
1710                return
1711                ;;
1712        -G,*|-S,*)
1713                __git_complete_symbol
1714                return
1715                ;;
1716        esac
1717        case "$cur" in
1718        --pretty=*|--format=*)
1719                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1720                        " "" "${cur#*=}"
1721                return
1722                ;;
1723        --date=*)
1724                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1725                return
1726                ;;
1727        --decorate=*)
1728                __gitcomp "full short no" "" "${cur##--decorate=}"
1729                return
1730                ;;
1731        --diff-algorithm=*)
1732                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1733                return
1734                ;;
1735        --submodule=*)
1736                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1737                return
1738                ;;
1739        --*)
1740                __gitcomp "
1741                        $__git_log_common_options
1742                        $__git_log_shortlog_options
1743                        $__git_log_gitk_options
1744                        --root --topo-order --date-order --reverse
1745                        --follow --full-diff
1746                        --abbrev-commit --abbrev=
1747                        --relative-date --date=
1748                        --pretty= --format= --oneline
1749                        --show-signature
1750                        --cherry-mark
1751                        --cherry-pick
1752                        --graph
1753                        --decorate --decorate=
1754                        --walk-reflogs
1755                        --parents --children
1756                        $merge
1757                        $__git_diff_common_options
1758                        --pickaxe-all --pickaxe-regex
1759                        "
1760                return
1761                ;;
1762        -L:*:*)
1763                return  # fall back to Bash filename completion
1764                ;;
1765        -L:*)
1766                __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1767                return
1768                ;;
1769        -G*)
1770                __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1771                return
1772                ;;
1773        -S*)
1774                __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1775                return
1776                ;;
1777        esac
1778        __git_complete_revlist
1779}
1780
1781_git_merge ()
1782{
1783        __git_complete_strategy && return
1784
1785        case "$cur" in
1786        --*)
1787                __gitcomp_builtin merge
1788                return
1789        esac
1790        __git_complete_refs
1791}
1792
1793_git_mergetool ()
1794{
1795        case "$cur" in
1796        --tool=*)
1797                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1798                return
1799                ;;
1800        --*)
1801                __gitcomp "--tool= --prompt --no-prompt"
1802                return
1803                ;;
1804        esac
1805}
1806
1807_git_merge_base ()
1808{
1809        case "$cur" in
1810        --*)
1811                __gitcomp_builtin merge-base
1812                return
1813                ;;
1814        esac
1815        __git_complete_refs
1816}
1817
1818_git_mv ()
1819{
1820        case "$cur" in
1821        --*)
1822                __gitcomp_builtin mv
1823                return
1824                ;;
1825        esac
1826
1827        if [ $(__git_count_arguments "mv") -gt 0 ]; then
1828                # We need to show both cached and untracked files (including
1829                # empty directories) since this may not be the last argument.
1830                __git_complete_index_file "--cached --others --directory"
1831        else
1832                __git_complete_index_file "--cached"
1833        fi
1834}
1835
1836_git_notes ()
1837{
1838        local subcommands='add append copy edit get-ref list merge prune remove show'
1839        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1840
1841        case "$subcommand,$cur" in
1842        ,--*)
1843                __gitcomp_builtin notes
1844                ;;
1845        ,*)
1846                case "$prev" in
1847                --ref)
1848                        __git_complete_refs
1849                        ;;
1850                *)
1851                        __gitcomp "$subcommands --ref"
1852                        ;;
1853                esac
1854                ;;
1855        *,--reuse-message=*|*,--reedit-message=*)
1856                __git_complete_refs --cur="${cur#*=}"
1857                ;;
1858        *,--*)
1859                __gitcomp_builtin notes_$subcommand
1860                ;;
1861        prune,*|get-ref,*)
1862                # this command does not take a ref, do not complete it
1863                ;;
1864        *)
1865                case "$prev" in
1866                -m|-F)
1867                        ;;
1868                *)
1869                        __git_complete_refs
1870                        ;;
1871                esac
1872                ;;
1873        esac
1874}
1875
1876_git_pull ()
1877{
1878        __git_complete_strategy && return
1879
1880        case "$cur" in
1881        --recurse-submodules=*)
1882                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1883                return
1884                ;;
1885        --*)
1886                __gitcomp_builtin pull
1887
1888                return
1889                ;;
1890        esac
1891        __git_complete_remote_or_refspec
1892}
1893
1894__git_push_recurse_submodules="check on-demand only"
1895
1896__git_complete_force_with_lease ()
1897{
1898        local cur_=$1
1899
1900        case "$cur_" in
1901        --*=)
1902                ;;
1903        *:*)
1904                __git_complete_refs --cur="${cur_#*:}"
1905                ;;
1906        *)
1907                __git_complete_refs --cur="$cur_"
1908                ;;
1909        esac
1910}
1911
1912_git_push ()
1913{
1914        case "$prev" in
1915        --repo)
1916                __gitcomp_nl "$(__git_remotes)"
1917                return
1918                ;;
1919        --recurse-submodules)
1920                __gitcomp "$__git_push_recurse_submodules"
1921                return
1922                ;;
1923        esac
1924        case "$cur" in
1925        --repo=*)
1926                __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1927                return
1928                ;;
1929        --recurse-submodules=*)
1930                __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1931                return
1932                ;;
1933        --force-with-lease=*)
1934                __git_complete_force_with_lease "${cur##--force-with-lease=}"
1935                return
1936                ;;
1937        --*)
1938                __gitcomp_builtin push
1939                return
1940                ;;
1941        esac
1942        __git_complete_remote_or_refspec
1943}
1944
1945_git_rebase ()
1946{
1947        __git_find_repo_path
1948        if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1949                __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1950                return
1951        elif [ -d "$__git_repo_path"/rebase-apply ] || \
1952             [ -d "$__git_repo_path"/rebase-merge ]; then
1953                __gitcomp "--continue --skip --abort --quit --show-current-patch"
1954                return
1955        fi
1956        __git_complete_strategy && return
1957        case "$cur" in
1958        --whitespace=*)
1959                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1960                return
1961                ;;
1962        --*)
1963                __gitcomp "
1964                        --onto --merge --strategy --interactive
1965                        --rebase-merges --preserve-merges --stat --no-stat
1966                        --committer-date-is-author-date --ignore-date
1967                        --ignore-whitespace --whitespace=
1968                        --autosquash --no-autosquash
1969                        --fork-point --no-fork-point
1970                        --autostash --no-autostash
1971                        --verify --no-verify
1972                        --keep-empty --root --force-rebase --no-ff
1973                        --rerere-autoupdate
1974                        --exec
1975                        "
1976
1977                return
1978        esac
1979        __git_complete_refs
1980}
1981
1982_git_reflog ()
1983{
1984        local subcommands="show delete expire"
1985        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1986
1987        if [ -z "$subcommand" ]; then
1988                __gitcomp "$subcommands"
1989        else
1990                __git_complete_refs
1991        fi
1992}
1993
1994__git_send_email_confirm_options="always never auto cc compose"
1995__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1996
1997_git_send_email ()
1998{
1999        case "$prev" in
2000        --to|--cc|--bcc|--from)
2001                __gitcomp "$(__git send-email --dump-aliases)"
2002                return
2003                ;;
2004        esac
2005
2006        case "$cur" in
2007        --confirm=*)
2008                __gitcomp "
2009                        $__git_send_email_confirm_options
2010                        " "" "${cur##--confirm=}"
2011                return
2012                ;;
2013        --suppress-cc=*)
2014                __gitcomp "
2015                        $__git_send_email_suppresscc_options
2016                        " "" "${cur##--suppress-cc=}"
2017
2018                return
2019                ;;
2020        --smtp-encryption=*)
2021                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2022                return
2023                ;;
2024        --thread=*)
2025                __gitcomp "
2026                        deep shallow
2027                        " "" "${cur##--thread=}"
2028                return
2029                ;;
2030        --to=*|--cc=*|--bcc=*|--from=*)
2031                __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2032                return
2033                ;;
2034        --*)
2035                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2036                        --compose --confirm= --dry-run --envelope-sender
2037                        --from --identity
2038                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2039                        --no-suppress-from --no-thread --quiet --reply-to
2040                        --signed-off-by-cc --smtp-pass --smtp-server
2041                        --smtp-server-port --smtp-encryption= --smtp-user
2042                        --subject --suppress-cc= --suppress-from --thread --to
2043                        --validate --no-validate
2044                        $__git_format_patch_options"
2045                return
2046                ;;
2047        esac
2048        __git_complete_revlist
2049}
2050
2051_git_stage ()
2052{
2053        _git_add
2054}
2055
2056_git_status ()
2057{
2058        local complete_opt
2059        local untracked_state
2060
2061        case "$cur" in
2062        --ignore-submodules=*)
2063                __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2064                return
2065                ;;
2066        --untracked-files=*)
2067                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2068                return
2069                ;;
2070        --column=*)
2071                __gitcomp "
2072                        always never auto column row plain dense nodense
2073                        " "" "${cur##--column=}"
2074                return
2075                ;;
2076        --*)
2077                __gitcomp_builtin status
2078                return
2079                ;;
2080        esac
2081
2082        untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2083                "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2084
2085        case "$untracked_state" in
2086        no)
2087                # --ignored option does not matter
2088                complete_opt=
2089                ;;
2090        all|normal|*)
2091                complete_opt="--cached --directory --no-empty-directory --others"
2092
2093                if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2094                        complete_opt="$complete_opt --ignored --exclude=*"
2095                fi
2096                ;;
2097        esac
2098
2099        __git_complete_index_file "$complete_opt"
2100}
2101
2102__git_config_get_set_variables ()
2103{
2104        local prevword word config_file= c=$cword
2105        while [ $c -gt 1 ]; do
2106                word="${words[c]}"
2107                case "$word" in
2108                --system|--global|--local|--file=*)
2109                        config_file="$word"
2110                        break
2111                        ;;
2112                -f|--file)
2113                        config_file="$word $prevword"
2114                        break
2115                        ;;
2116                esac
2117                prevword=$word
2118                c=$((--c))
2119        done
2120
2121        __git config $config_file --name-only --list
2122}
2123
2124_git_config ()
2125{
2126        case "$prev" in
2127        branch.*.remote|branch.*.pushremote)
2128                __gitcomp_nl "$(__git_remotes)"
2129                return
2130                ;;
2131        branch.*.merge)
2132                __git_complete_refs
2133                return
2134                ;;
2135        branch.*.rebase)
2136                __gitcomp "false true merges preserve interactive"
2137                return
2138                ;;
2139        remote.pushdefault)
2140                __gitcomp_nl "$(__git_remotes)"
2141                return
2142                ;;
2143        remote.*.fetch)
2144                local remote="${prev#remote.}"
2145                remote="${remote%.fetch}"
2146                if [ -z "$cur" ]; then
2147                        __gitcomp_nl "refs/heads/" "" "" ""
2148                        return
2149                fi
2150                __gitcomp_nl "$(__git_refs_remotes "$remote")"
2151                return
2152                ;;
2153        remote.*.push)
2154                local remote="${prev#remote.}"
2155                remote="${remote%.push}"
2156                __gitcomp_nl "$(__git for-each-ref \
2157                        --format='%(refname):%(refname)' refs/heads)"
2158                return
2159                ;;
2160        pull.twohead|pull.octopus)
2161                __git_compute_merge_strategies
2162                __gitcomp "$__git_merge_strategies"
2163                return
2164                ;;
2165        color.branch|color.diff|color.interactive|\
2166        color.showbranch|color.status|color.ui)
2167                __gitcomp "always never auto"
2168                return
2169                ;;
2170        color.pager)
2171                __gitcomp "false true"
2172                return
2173                ;;
2174        color.*.*)
2175                __gitcomp "
2176                        normal black red green yellow blue magenta cyan white
2177                        bold dim ul blink reverse
2178                        "
2179                return
2180                ;;
2181        diff.submodule)
2182                __gitcomp "log short"
2183                return
2184                ;;
2185        help.format)
2186                __gitcomp "man info web html"
2187                return
2188                ;;
2189        log.date)
2190                __gitcomp "$__git_log_date_formats"
2191                return
2192                ;;
2193        sendemail.aliasfiletype)
2194                __gitcomp "mutt mailrc pine elm gnus"
2195                return
2196                ;;
2197        sendemail.confirm)
2198                __gitcomp "$__git_send_email_confirm_options"
2199                return
2200                ;;
2201        sendemail.suppresscc)
2202                __gitcomp "$__git_send_email_suppresscc_options"
2203                return
2204                ;;
2205        sendemail.transferencoding)
2206                __gitcomp "7bit 8bit quoted-printable base64"
2207                return
2208                ;;
2209        --get|--get-all|--unset|--unset-all)
2210                __gitcomp_nl "$(__git_config_get_set_variables)"
2211                return
2212                ;;
2213        *.*)
2214                return
2215                ;;
2216        esac
2217        case "$cur" in
2218        --*)
2219                __gitcomp_builtin config
2220                return
2221                ;;
2222        branch.*.*)
2223                local pfx="${cur%.*}." cur_="${cur##*.}"
2224                __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
2225                return
2226                ;;
2227        branch.*)
2228                local pfx="${cur%.*}." cur_="${cur#*.}"
2229                __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2230                __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
2231                return
2232                ;;
2233        guitool.*.*)
2234                local pfx="${cur%.*}." cur_="${cur##*.}"
2235                __gitcomp "
2236                        argprompt cmd confirm needsfile noconsole norescan
2237                        prompt revprompt revunmerged title
2238                        " "$pfx" "$cur_"
2239                return
2240                ;;
2241        difftool.*.*)
2242                local pfx="${cur%.*}." cur_="${cur##*.}"
2243                __gitcomp "cmd path" "$pfx" "$cur_"
2244                return
2245                ;;
2246        man.*.*)
2247                local pfx="${cur%.*}." cur_="${cur##*.}"
2248                __gitcomp "cmd path" "$pfx" "$cur_"
2249                return
2250                ;;
2251        mergetool.*.*)
2252                local pfx="${cur%.*}." cur_="${cur##*.}"
2253                __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2254                return
2255                ;;
2256        pager.*)
2257                local pfx="${cur%.*}." cur_="${cur#*.}"
2258                __git_compute_all_commands
2259                __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2260                return
2261                ;;
2262        remote.*.*)
2263                local pfx="${cur%.*}." cur_="${cur##*.}"
2264                __gitcomp "
2265                        url proxy fetch push mirror skipDefaultUpdate
2266                        receivepack uploadpack tagopt pushurl
2267                        " "$pfx" "$cur_"
2268                return
2269                ;;
2270        remote.*)
2271                local pfx="${cur%.*}." cur_="${cur#*.}"
2272                __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2273                __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
2274                return
2275                ;;
2276        url.*.*)
2277                local pfx="${cur%.*}." cur_="${cur##*.}"
2278                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2279                return
2280                ;;
2281        esac
2282        __gitcomp "
2283                add.ignoreErrors
2284                advice.amWorkDir
2285                advice.commitBeforeMerge
2286                advice.detachedHead
2287                advice.implicitIdentity
2288                advice.pushAlreadyExists
2289                advice.pushFetchFirst
2290                advice.pushNeedsForce
2291                advice.pushNonFFCurrent
2292                advice.pushNonFFMatching
2293                advice.pushUpdateRejected
2294                advice.resolveConflict
2295                advice.rmHints
2296                advice.statusHints
2297                advice.statusUoption
2298                advice.ignoredHook
2299                alias.
2300                am.keepcr
2301                am.threeWay
2302                apply.ignorewhitespace
2303                apply.whitespace
2304                branch.autosetupmerge
2305                branch.autosetuprebase
2306                browser.
2307                clean.requireForce
2308                color.branch
2309                color.branch.current
2310                color.branch.local
2311                color.branch.plain
2312                color.branch.remote
2313                color.decorate.HEAD
2314                color.decorate.branch
2315                color.decorate.remoteBranch
2316                color.decorate.stash
2317                color.decorate.tag
2318                color.diff
2319                color.diff.commit
2320                color.diff.frag
2321                color.diff.func
2322                color.diff.meta
2323                color.diff.new
2324                color.diff.old
2325                color.diff.plain
2326                color.diff.whitespace
2327                color.grep
2328                color.grep.context
2329                color.grep.filename
2330                color.grep.function
2331                color.grep.linenumber
2332                color.grep.match
2333                color.grep.selected
2334                color.grep.separator
2335                color.interactive
2336                color.interactive.error
2337                color.interactive.header
2338                color.interactive.help
2339                color.interactive.prompt
2340                color.pager
2341                color.showbranch
2342                color.status
2343                color.status.added
2344                color.status.changed
2345                color.status.header
2346                color.status.localBranch
2347                color.status.nobranch
2348                color.status.remoteBranch
2349                color.status.unmerged
2350                color.status.untracked
2351                color.status.updated
2352                color.ui
2353                commit.cleanup
2354                commit.gpgSign
2355                commit.status
2356                commit.template
2357                commit.verbose
2358                core.abbrev
2359                core.askpass
2360                core.attributesfile
2361                core.autocrlf
2362                core.bare
2363                core.bigFileThreshold
2364                core.checkStat
2365                core.commentChar
2366                core.commitGraph
2367                core.compression
2368                core.createObject
2369                core.deltaBaseCacheLimit
2370                core.editor
2371                core.eol
2372                core.excludesfile
2373                core.fileMode
2374                core.fsyncobjectfiles
2375                core.gitProxy
2376                core.hideDotFiles
2377                core.hooksPath
2378                core.ignoreStat
2379                core.ignorecase
2380                core.logAllRefUpdates
2381                core.loosecompression
2382                core.notesRef
2383                core.packedGitLimit
2384                core.packedGitWindowSize
2385                core.packedRefsTimeout
2386                core.pager
2387                core.precomposeUnicode
2388                core.preferSymlinkRefs
2389                core.preloadindex
2390                core.protectHFS
2391                core.protectNTFS
2392                core.quotepath
2393                core.repositoryFormatVersion
2394                core.safecrlf
2395                core.sharedRepository
2396                core.sparseCheckout
2397                core.splitIndex
2398                core.sshCommand
2399                core.symlinks
2400                core.trustctime
2401                core.untrackedCache
2402                core.warnAmbiguousRefs
2403                core.whitespace
2404                core.worktree
2405                credential.helper
2406                credential.useHttpPath
2407                credential.username
2408                credentialCache.ignoreSIGHUP
2409                diff.autorefreshindex
2410                diff.external
2411                diff.ignoreSubmodules
2412                diff.mnemonicprefix
2413                diff.noprefix
2414                diff.renameLimit
2415                diff.renames
2416                diff.statGraphWidth
2417                diff.submodule
2418                diff.suppressBlankEmpty
2419                diff.tool
2420                diff.wordRegex
2421                diff.algorithm
2422                difftool.
2423                difftool.prompt
2424                fetch.recurseSubmodules
2425                fetch.unpackLimit
2426                format.attach
2427                format.cc
2428                format.coverLetter
2429                format.from
2430                format.headers
2431                format.numbered
2432                format.pretty
2433                format.signature
2434                format.signoff
2435                format.subjectprefix
2436                format.suffix
2437                format.thread
2438                format.to
2439                gc.
2440                gc.aggressiveDepth
2441                gc.aggressiveWindow
2442                gc.auto
2443                gc.autoDetach
2444                gc.autopacklimit
2445                gc.logExpiry
2446                gc.packrefs
2447                gc.pruneexpire
2448                gc.reflogexpire
2449                gc.reflogexpireunreachable
2450                gc.rerereresolved
2451                gc.rerereunresolved
2452                gc.worktreePruneExpire
2453                gitcvs.allbinary
2454                gitcvs.commitmsgannotation
2455                gitcvs.dbTableNamePrefix
2456                gitcvs.dbdriver
2457                gitcvs.dbname
2458                gitcvs.dbpass
2459                gitcvs.dbuser
2460                gitcvs.enabled
2461                gitcvs.logfile
2462                gitcvs.usecrlfattr
2463                guitool.
2464                gui.blamehistoryctx
2465                gui.commitmsgwidth
2466                gui.copyblamethreshold
2467                gui.diffcontext
2468                gui.encoding
2469                gui.fastcopyblame
2470                gui.matchtrackingbranch
2471                gui.newbranchtemplate
2472                gui.pruneduringfetch
2473                gui.spellingdictionary
2474                gui.trustmtime
2475                help.autocorrect
2476                help.browser
2477                help.format
2478                http.lowSpeedLimit
2479                http.lowSpeedTime
2480                http.maxRequests
2481                http.minSessions
2482                http.noEPSV
2483                http.postBuffer
2484                http.proxy
2485                http.sslCipherList
2486                http.sslVersion
2487                http.sslCAInfo
2488                http.sslCAPath
2489                http.sslCert
2490                http.sslCertPasswordProtected
2491                http.sslKey
2492                http.sslVerify
2493                http.useragent
2494                i18n.commitEncoding
2495                i18n.logOutputEncoding
2496                imap.authMethod
2497                imap.folder
2498                imap.host
2499                imap.pass
2500                imap.port
2501                imap.preformattedHTML
2502                imap.sslverify
2503                imap.tunnel
2504                imap.user
2505                init.templatedir
2506                instaweb.browser
2507                instaweb.httpd
2508                instaweb.local
2509                instaweb.modulepath
2510                instaweb.port
2511                interactive.singlekey
2512                log.date
2513                log.decorate
2514                log.showroot
2515                mailmap.file
2516                man.
2517                man.viewer
2518                merge.
2519                merge.conflictstyle
2520                merge.log
2521                merge.renameLimit
2522                merge.renormalize
2523                merge.stat
2524                merge.tool
2525                merge.verbosity
2526                mergetool.
2527                mergetool.keepBackup
2528                mergetool.keepTemporaries
2529                mergetool.prompt
2530                notes.displayRef
2531                notes.rewrite.
2532                notes.rewrite.amend
2533                notes.rewrite.rebase
2534                notes.rewriteMode
2535                notes.rewriteRef
2536                pack.compression
2537                pack.deltaCacheLimit
2538                pack.deltaCacheSize
2539                pack.depth
2540                pack.indexVersion
2541                pack.packSizeLimit
2542                pack.threads
2543                pack.window
2544                pack.windowMemory
2545                pager.
2546                pretty.
2547                pull.octopus
2548                pull.twohead
2549                push.default
2550                push.followTags
2551                rebase.autosquash
2552                rebase.stat
2553                receive.autogc
2554                receive.denyCurrentBranch
2555                receive.denyDeleteCurrent
2556                receive.denyDeletes
2557                receive.denyNonFastForwards
2558                receive.fsckObjects
2559                receive.unpackLimit
2560                receive.updateserverinfo
2561                remote.pushdefault
2562                remotes.
2563                repack.usedeltabaseoffset
2564                rerere.autoupdate
2565                rerere.enabled
2566                sendemail.
2567                sendemail.aliasesfile
2568                sendemail.aliasfiletype
2569                sendemail.bcc
2570                sendemail.cc
2571                sendemail.cccmd
2572                sendemail.chainreplyto
2573                sendemail.confirm
2574                sendemail.envelopesender
2575                sendemail.from
2576                sendemail.identity
2577                sendemail.multiedit
2578                sendemail.signedoffbycc
2579                sendemail.smtpdomain
2580                sendemail.smtpencryption
2581                sendemail.smtppass
2582                sendemail.smtpserver
2583                sendemail.smtpserveroption
2584                sendemail.smtpserverport
2585                sendemail.smtpuser
2586                sendemail.suppresscc
2587                sendemail.suppressfrom
2588                sendemail.thread
2589                sendemail.to
2590                sendemail.tocmd
2591                sendemail.validate
2592                sendemail.smtpbatchsize
2593                sendemail.smtprelogindelay
2594                showbranch.default
2595                status.relativePaths
2596                status.showUntrackedFiles
2597                status.submodulesummary
2598                submodule.
2599                tar.umask
2600                transfer.unpackLimit
2601                url.
2602                user.email
2603                user.name
2604                user.signingkey
2605                web.browser
2606                branch. remote.
2607        "
2608}
2609
2610_git_remote ()
2611{
2612        local subcommands="
2613                add rename remove set-head set-branches
2614                get-url set-url show prune update
2615                "
2616        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2617        if [ -z "$subcommand" ]; then
2618                case "$cur" in
2619                --*)
2620                        __gitcomp_builtin remote
2621                        ;;
2622                *)
2623                        __gitcomp "$subcommands"
2624                        ;;
2625                esac
2626                return
2627        fi
2628
2629        case "$subcommand,$cur" in
2630        add,--*)
2631                __gitcomp_builtin remote_add
2632                ;;
2633        add,*)
2634                ;;
2635        set-head,--*)
2636                __gitcomp_builtin remote_set-head
2637                ;;
2638        set-branches,--*)
2639                __gitcomp_builtin remote_set-branches
2640                ;;
2641        set-head,*|set-branches,*)
2642                __git_complete_remote_or_refspec
2643                ;;
2644        update,--*)
2645                __gitcomp_builtin remote_update
2646                ;;
2647        update,*)
2648                __gitcomp "$(__git_get_config_variables "remotes")"
2649                ;;
2650        set-url,--*)
2651                __gitcomp_builtin remote_set-url
2652                ;;
2653        get-url,--*)
2654                __gitcomp_builtin remote_get-url
2655                ;;
2656        prune,--*)
2657                __gitcomp_builtin remote_prune
2658                ;;
2659        *)
2660                __gitcomp_nl "$(__git_remotes)"
2661                ;;
2662        esac
2663}
2664
2665_git_replace ()
2666{
2667        case "$cur" in
2668        --*)
2669                __gitcomp_builtin replace
2670                return
2671                ;;
2672        esac
2673        __git_complete_refs
2674}
2675
2676_git_rerere ()
2677{
2678        local subcommands="clear forget diff remaining status gc"
2679        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2680        if test -z "$subcommand"
2681        then
2682                __gitcomp "$subcommands"
2683                return
2684        fi
2685}
2686
2687_git_reset ()
2688{
2689        __git_has_doubledash && return
2690
2691        case "$cur" in
2692        --*)
2693                __gitcomp_builtin reset
2694                return
2695                ;;
2696        esac
2697        __git_complete_refs
2698}
2699
2700__git_revert_inprogress_options="--continue --quit --abort"
2701
2702_git_revert ()
2703{
2704        __git_find_repo_path
2705        if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2706                __gitcomp "$__git_revert_inprogress_options"
2707                return
2708        fi
2709        case "$cur" in
2710        --*)
2711                __gitcomp_builtin revert "" \
2712                        "$__git_revert_inprogress_options"
2713                return
2714                ;;
2715        esac
2716        __git_complete_refs
2717}
2718
2719_git_rm ()
2720{
2721        case "$cur" in
2722        --*)
2723                __gitcomp_builtin rm
2724                return
2725                ;;
2726        esac
2727
2728        __git_complete_index_file "--cached"
2729}
2730
2731_git_shortlog ()
2732{
2733        __git_has_doubledash && return
2734
2735        case "$cur" in
2736        --*)
2737                __gitcomp "
2738                        $__git_log_common_options
2739                        $__git_log_shortlog_options
2740                        --numbered --summary --email
2741                        "
2742                return
2743                ;;
2744        esac
2745        __git_complete_revlist
2746}
2747
2748_git_show ()
2749{
2750        __git_has_doubledash && return
2751
2752        case "$cur" in
2753        --pretty=*|--format=*)
2754                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2755                        " "" "${cur#*=}"
2756                return
2757                ;;
2758        --diff-algorithm=*)
2759                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2760                return
2761                ;;
2762        --submodule=*)
2763                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2764                return
2765                ;;
2766        --*)
2767                __gitcomp "--pretty= --format= --abbrev-commit --oneline
2768                        --show-signature
2769                        $__git_diff_common_options
2770                        "
2771                return
2772                ;;
2773        esac
2774        __git_complete_revlist_file
2775}
2776
2777_git_show_branch ()
2778{
2779        case "$cur" in
2780        --*)
2781                __gitcomp_builtin show-branch
2782                return
2783                ;;
2784        esac
2785        __git_complete_revlist
2786}
2787
2788_git_stash ()
2789{
2790        local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2791        local subcommands='push list show apply clear drop pop create branch'
2792        local subcommand="$(__git_find_on_cmdline "$subcommands save")"
2793        if [ -n "$(__git_find_on_cmdline "-p")" ]; then
2794                subcommand="push"
2795        fi
2796        if [ -z "$subcommand" ]; then
2797                case "$cur" in
2798                --*)
2799                        __gitcomp "$save_opts"
2800                        ;;
2801                sa*)
2802                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2803                                __gitcomp "save"
2804                        fi
2805                        ;;
2806                *)
2807                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2808                                __gitcomp "$subcommands"
2809                        fi
2810                        ;;
2811                esac
2812        else
2813                case "$subcommand,$cur" in
2814                push,--*)
2815                        __gitcomp "$save_opts --message"
2816                        ;;
2817                save,--*)
2818                        __gitcomp "$save_opts"
2819                        ;;
2820                apply,--*|pop,--*)
2821                        __gitcomp "--index --quiet"
2822                        ;;
2823                drop,--*)
2824                        __gitcomp "--quiet"
2825                        ;;
2826                show,--*|branch,--*)
2827                        ;;
2828                branch,*)
2829                        if [ $cword -eq 3 ]; then
2830                                __git_complete_refs
2831                        else
2832                                __gitcomp_nl "$(__git stash list \
2833                                                | sed -n -e 's/:.*//p')"
2834                        fi
2835                        ;;
2836                show,*|apply,*|drop,*|pop,*)
2837                        __gitcomp_nl "$(__git stash list \
2838                                        | sed -n -e 's/:.*//p')"
2839                        ;;
2840                *)
2841                        ;;
2842                esac
2843        fi
2844}
2845
2846_git_submodule ()
2847{
2848        __git_has_doubledash && return
2849
2850        local subcommands="add status init deinit update summary foreach sync"
2851        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2852        if [ -z "$subcommand" ]; then
2853                case "$cur" in
2854                --*)
2855                        __gitcomp "--quiet"
2856                        ;;
2857                *)
2858                        __gitcomp "$subcommands"
2859                        ;;
2860                esac
2861                return
2862        fi
2863
2864        case "$subcommand,$cur" in
2865        add,--*)
2866                __gitcomp "--branch --force --name --reference --depth"
2867                ;;
2868        status,--*)
2869                __gitcomp "--cached --recursive"
2870                ;;
2871        deinit,--*)
2872                __gitcomp "--force --all"
2873                ;;
2874        update,--*)
2875                __gitcomp "
2876                        --init --remote --no-fetch
2877                        --recommend-shallow --no-recommend-shallow
2878                        --force --rebase --merge --reference --depth --recursive --jobs
2879                "
2880                ;;
2881        summary,--*)
2882                __gitcomp "--cached --files --summary-limit"
2883                ;;
2884        foreach,--*|sync,--*)
2885                __gitcomp "--recursive"
2886                ;;
2887        *)
2888                ;;
2889        esac
2890}
2891
2892_git_svn ()
2893{
2894        local subcommands="
2895                init fetch clone rebase dcommit log find-rev
2896                set-tree commit-diff info create-ignore propget
2897                proplist show-ignore show-externals branch tag blame
2898                migrate mkdirs reset gc
2899                "
2900        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2901        if [ -z "$subcommand" ]; then
2902                __gitcomp "$subcommands"
2903        else
2904                local remote_opts="--username= --config-dir= --no-auth-cache"
2905                local fc_opts="
2906                        --follow-parent --authors-file= --repack=
2907                        --no-metadata --use-svm-props --use-svnsync-props
2908                        --log-window-size= --no-checkout --quiet
2909                        --repack-flags --use-log-author --localtime
2910                        --add-author-from
2911                        --ignore-paths= --include-paths= $remote_opts
2912                        "
2913                local init_opts="
2914                        --template= --shared= --trunk= --tags=
2915                        --branches= --stdlayout --minimize-url
2916                        --no-metadata --use-svm-props --use-svnsync-props
2917                        --rewrite-root= --prefix= $remote_opts
2918                        "
2919                local cmt_opts="
2920                        --edit --rmdir --find-copies-harder --copy-similarity=
2921                        "
2922
2923                case "$subcommand,$cur" in
2924                fetch,--*)
2925                        __gitcomp "--revision= --fetch-all $fc_opts"
2926                        ;;
2927                clone,--*)
2928                        __gitcomp "--revision= $fc_opts $init_opts"
2929                        ;;
2930                init,--*)
2931                        __gitcomp "$init_opts"
2932                        ;;
2933                dcommit,--*)
2934                        __gitcomp "
2935                                --merge --strategy= --verbose --dry-run
2936                                --fetch-all --no-rebase --commit-url
2937                                --revision --interactive $cmt_opts $fc_opts
2938                                "
2939                        ;;
2940                set-tree,--*)
2941                        __gitcomp "--stdin $cmt_opts $fc_opts"
2942                        ;;
2943                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2944                show-externals,--*|mkdirs,--*)
2945                        __gitcomp "--revision="
2946                        ;;
2947                log,--*)
2948                        __gitcomp "
2949                                --limit= --revision= --verbose --incremental
2950                                --oneline --show-commit --non-recursive
2951                                --authors-file= --color
2952                                "
2953                        ;;
2954                rebase,--*)
2955                        __gitcomp "
2956                                --merge --verbose --strategy= --local
2957                                --fetch-all --dry-run $fc_opts
2958                                "
2959                        ;;
2960                commit-diff,--*)
2961                        __gitcomp "--message= --file= --revision= $cmt_opts"
2962                        ;;
2963                info,--*)
2964                        __gitcomp "--url"
2965                        ;;
2966                branch,--*)
2967                        __gitcomp "--dry-run --message --tag"
2968                        ;;
2969                tag,--*)
2970                        __gitcomp "--dry-run --message"
2971                        ;;
2972                blame,--*)
2973                        __gitcomp "--git-format"
2974                        ;;
2975                migrate,--*)
2976                        __gitcomp "
2977                                --config-dir= --ignore-paths= --minimize
2978                                --no-auth-cache --username=
2979                                "
2980                        ;;
2981                reset,--*)
2982                        __gitcomp "--revision= --parent"
2983                        ;;
2984                *)
2985                        ;;
2986                esac
2987        fi
2988}
2989
2990_git_tag ()
2991{
2992        local i c=1 f=0
2993        while [ $c -lt $cword ]; do
2994                i="${words[c]}"
2995                case "$i" in
2996                -d|--delete|-v|--verify)
2997                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2998                        return
2999                        ;;
3000                -f)
3001                        f=1
3002                        ;;
3003                esac
3004                ((c++))
3005        done
3006
3007        case "$prev" in
3008        -m|-F)
3009                ;;
3010        -*|tag)
3011                if [ $f = 1 ]; then
3012                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3013                fi
3014                ;;
3015        *)
3016                __git_complete_refs
3017                ;;
3018        esac
3019
3020        case "$cur" in
3021        --*)
3022                __gitcomp_builtin tag
3023                ;;
3024        esac
3025}
3026
3027_git_whatchanged ()
3028{
3029        _git_log
3030}
3031
3032_git_worktree ()
3033{
3034        local subcommands="add list lock move prune remove unlock"
3035        local subcommand="$(__git_find_on_cmdline "$subcommands")"
3036        if [ -z "$subcommand" ]; then
3037                __gitcomp "$subcommands"
3038        else
3039                case "$subcommand,$cur" in
3040                add,--*)
3041                        __gitcomp_builtin worktree_add
3042                        ;;
3043                list,--*)
3044                        __gitcomp_builtin worktree_list
3045                        ;;
3046                lock,--*)
3047                        __gitcomp_builtin worktree_lock
3048                        ;;
3049                prune,--*)
3050                        __gitcomp_builtin worktree_prune
3051                        ;;
3052                remove,--*)
3053                        __gitcomp "--force"
3054                        ;;
3055                *)
3056                        ;;
3057                esac
3058        fi
3059}
3060
3061__git_complete_common () {
3062        local command="$1"
3063
3064        case "$cur" in
3065        --*)
3066                __gitcomp_builtin "$command"
3067                ;;
3068        esac
3069}
3070
3071__git_cmds_with_parseopt_helper=
3072__git_support_parseopt_helper () {
3073        test -n "$__git_cmds_with_parseopt_helper" ||
3074                __git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
3075
3076        case " $__git_cmds_with_parseopt_helper " in
3077        *" $1 "*)
3078                return 0
3079                ;;
3080        *)
3081                return 1
3082                ;;
3083        esac
3084}
3085
3086__git_complete_command () {
3087        local command="$1"
3088        local completion_func="_git_${command//-/_}"
3089        if ! declare -f $completion_func >/dev/null 2>/dev/null &&
3090                declare -f _completion_loader >/dev/null 2>/dev/null
3091        then
3092                _completion_loader "git-$command"
3093        fi
3094        if declare -f $completion_func >/dev/null 2>/dev/null
3095        then
3096                $completion_func
3097                return 0
3098        elif __git_support_parseopt_helper "$command"
3099        then
3100                __git_complete_common "$command"
3101                return 0
3102        else
3103                return 1
3104        fi
3105}
3106
3107__git_main ()
3108{
3109        local i c=1 command __git_dir __git_repo_path
3110        local __git_C_args C_args_count=0
3111
3112        while [ $c -lt $cword ]; do
3113                i="${words[c]}"
3114                case "$i" in
3115                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
3116                --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
3117                --bare)      __git_dir="." ;;
3118                --help) command="help"; break ;;
3119                -c|--work-tree|--namespace) ((c++)) ;;
3120                -C)     __git_C_args[C_args_count++]=-C
3121                        ((c++))
3122                        __git_C_args[C_args_count++]="${words[c]}"
3123                        ;;
3124                -*) ;;
3125                *) command="$i"; break ;;
3126                esac
3127                ((c++))
3128        done
3129
3130        if [ -z "$command" ]; then
3131                case "$prev" in
3132                --git-dir|-C|--work-tree)
3133                        # these need a path argument, let's fall back to
3134                        # Bash filename completion
3135                        return
3136                        ;;
3137                -c|--namespace)
3138                        # we don't support completing these options' arguments
3139                        return
3140                        ;;
3141                esac
3142                case "$cur" in
3143                --*)   __gitcomp "
3144                        --paginate
3145                        --no-pager
3146                        --git-dir=
3147                        --bare
3148                        --version
3149                        --exec-path
3150                        --exec-path=
3151                        --html-path
3152                        --man-path
3153                        --info-path
3154                        --work-tree=
3155                        --namespace=
3156                        --no-replace-objects
3157                        --help
3158                        "
3159                        ;;
3160                *)     __git_compute_porcelain_commands
3161                       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
3162                esac
3163                return
3164        fi
3165
3166        __git_complete_command "$command" && return
3167
3168        local expansion=$(__git_aliased_command "$command")
3169        if [ -n "$expansion" ]; then
3170                words[1]=$expansion
3171                __git_complete_command "$expansion"
3172        fi
3173}
3174
3175__gitk_main ()
3176{
3177        __git_has_doubledash && return
3178
3179        local __git_repo_path
3180        __git_find_repo_path
3181
3182        local merge=""
3183        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3184                merge="--merge"
3185        fi
3186        case "$cur" in
3187        --*)
3188                __gitcomp "
3189                        $__git_log_common_options
3190                        $__git_log_gitk_options
3191                        $merge
3192                        "
3193                return
3194                ;;
3195        esac
3196        __git_complete_revlist
3197}
3198
3199if [[ -n ${ZSH_VERSION-} ]]; then
3200        echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3201
3202        autoload -U +X compinit && compinit
3203
3204        __gitcomp ()
3205        {
3206                emulate -L zsh
3207
3208                local cur_="${3-$cur}"
3209
3210                case "$cur_" in
3211                --*=)
3212                        ;;
3213                *)
3214                        local c IFS=$' \t\n'
3215                        local -a array
3216                        for c in ${=1}; do
3217                                c="$c${4-}"
3218                                case $c in
3219                                --*=*|*.) ;;
3220                                *) c="$c " ;;
3221                                esac
3222                                array[${#array[@]}+1]="$c"
3223                        done
3224                        compset -P '*[=:]'
3225                        compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3226                        ;;
3227                esac
3228        }
3229
3230        __gitcomp_direct ()
3231        {
3232                emulate -L zsh
3233
3234                local IFS=$'\n'
3235                compset -P '*[=:]'
3236                compadd -Q -- ${=1} && _ret=0
3237        }
3238
3239        __gitcomp_nl ()
3240        {
3241                emulate -L zsh
3242
3243                local IFS=$'\n'
3244                compset -P '*[=:]'
3245                compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3246        }
3247
3248        __gitcomp_file ()
3249        {
3250                emulate -L zsh
3251
3252                local IFS=$'\n'
3253                compset -P '*[=:]'
3254                compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3255        }
3256
3257        _git ()
3258        {
3259                local _ret=1 cur cword prev
3260                cur=${words[CURRENT]}
3261                prev=${words[CURRENT-1]}
3262                let cword=CURRENT-1
3263                emulate ksh -c __${service}_main
3264                let _ret && _default && _ret=0
3265                return _ret
3266        }
3267
3268        compdef _git git gitk
3269        return
3270fi
3271
3272__git_func_wrap ()
3273{
3274        local cur words cword prev
3275        _get_comp_words_by_ref -n =: cur words cword prev
3276        $1
3277}
3278
3279# Setup completion for certain functions defined above by setting common
3280# variables and workarounds.
3281# This is NOT a public function; use at your own risk.
3282__git_complete ()
3283{
3284        local wrapper="__git_wrap${2}"
3285        eval "$wrapper () { __git_func_wrap $2 ; }"
3286        complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3287                || complete -o default -o nospace -F $wrapper $1
3288}
3289
3290# wrapper for backwards compatibility
3291_git ()
3292{
3293        __git_wrap__git_main
3294}
3295
3296# wrapper for backwards compatibility
3297_gitk ()
3298{
3299        __git_wrap__gitk_main
3300}
3301
3302__git_complete git __git_main
3303__git_complete gitk __gitk_main
3304
3305# The following are necessary only for Cygwin, and only are needed
3306# when the user has tab-completed the executable name and consequently
3307# included the '.exe' suffix.
3308#
3309if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3310__git_complete git.exe __git_main
3311fi