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