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