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