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