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