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