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