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