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