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