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