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