contrib / completion / git-completion.bashon commit completion: allow to customize the completable command list (6532f37)
   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.
 287unset $(set |sed -ne 's/^\(__gitcomp_builtin_[a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/p') 2>/dev/null
 288
 289# This function is equivalent to
 290#
 291#    __gitcomp "$(git xxx --git-completion-helper) ..."
 292#
 293# except that the output is cached. Accept 1-3 arguments:
 294# 1: the git command to execute, this is also the cache key
 295# 2: extra options to be added on top (e.g. negative forms)
 296# 3: options to be excluded
 297__gitcomp_builtin ()
 298{
 299        # spaces must be replaced with underscore for multi-word
 300        # commands, e.g. "git remote add" becomes remote_add.
 301        local cmd="$1"
 302        local incl="$2"
 303        local excl="$3"
 304
 305        local var=__gitcomp_builtin_"${cmd/-/_}"
 306        local options
 307        eval "options=\$$var"
 308
 309        if [ -z "$options" ]; then
 310                # leading and trailing spaces are significant to make
 311                # option removal work correctly.
 312                options=" $(__git ${cmd/_/ } --git-completion-helper) $incl "
 313                for i in $excl; do
 314                        options="${options/ $i / }"
 315                done
 316                eval "$var=\"$options\""
 317        fi
 318
 319        __gitcomp "$options"
 320}
 321
 322# Variation of __gitcomp_nl () that appends to the existing list of
 323# completion candidates, COMPREPLY.
 324__gitcomp_nl_append ()
 325{
 326        local IFS=$'\n'
 327        __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
 328}
 329
 330# Generates completion reply from newline-separated possible completion words
 331# by appending a space to all of them.
 332# It accepts 1 to 4 arguments:
 333# 1: List of possible completion words, separated by a single newline.
 334# 2: A prefix to be added to each possible completion word (optional).
 335# 3: Generate possible completion matches for this word (optional).
 336# 4: A suffix to be appended to each possible completion word instead of
 337#    the default space (optional).  If specified but empty, nothing is
 338#    appended.
 339__gitcomp_nl ()
 340{
 341        COMPREPLY=()
 342        __gitcomp_nl_append "$@"
 343}
 344
 345# Generates completion reply with compgen from newline-separated possible
 346# completion filenames.
 347# It accepts 1 to 3 arguments:
 348# 1: List of possible completion filenames, separated by a single newline.
 349# 2: A directory prefix to be added to each possible completion filename
 350#    (optional).
 351# 3: Generate possible completion matches for this word (optional).
 352__gitcomp_file ()
 353{
 354        local IFS=$'\n'
 355
 356        # XXX does not work when the directory prefix contains a tilde,
 357        # since tilde expansion is not applied.
 358        # This means that COMPREPLY will be empty and Bash default
 359        # completion will be used.
 360        __gitcompadd "$1" "${2-}" "${3-$cur}" ""
 361
 362        # use a hack to enable file mode in bash < 4
 363        compopt -o filenames +o nospace 2>/dev/null ||
 364        compgen -f /non-existing-dir/ > /dev/null
 365}
 366
 367# Execute 'git ls-files', unless the --committable option is specified, in
 368# which case it runs 'git diff-index' to find out the files that can be
 369# committed.  It return paths relative to the directory specified in the first
 370# argument, and using the options specified in the second argument.
 371__git_ls_files_helper ()
 372{
 373        if [ "$2" == "--committable" ]; then
 374                __git -C "$1" diff-index --name-only --relative HEAD
 375        else
 376                # NOTE: $2 is not quoted in order to support multiple options
 377                __git -C "$1" ls-files --exclude-standard $2
 378        fi
 379}
 380
 381
 382# __git_index_files accepts 1 or 2 arguments:
 383# 1: Options to pass to ls-files (required).
 384# 2: A directory path (optional).
 385#    If provided, only files within the specified directory are listed.
 386#    Sub directories are never recursed.  Path must have a trailing
 387#    slash.
 388__git_index_files ()
 389{
 390        local root="${2-.}" file
 391
 392        __git_ls_files_helper "$root" "$1" |
 393        while read -r file; do
 394                case "$file" in
 395                ?*/*) echo "${file%%/*}" ;;
 396                *) echo "$file" ;;
 397                esac
 398        done | sort | uniq
 399}
 400
 401# Lists branches from the local repository.
 402# 1: A prefix to be added to each listed branch (optional).
 403# 2: List only branches matching this word (optional; list all branches if
 404#    unset or empty).
 405# 3: A suffix to be appended to each listed branch (optional).
 406__git_heads ()
 407{
 408        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 409
 410        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 411                        "refs/heads/$cur_*" "refs/heads/$cur_*/**"
 412}
 413
 414# Lists tags from the local repository.
 415# Accepts the same positional parameters as __git_heads() above.
 416__git_tags ()
 417{
 418        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 419
 420        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 421                        "refs/tags/$cur_*" "refs/tags/$cur_*/**"
 422}
 423
 424# Lists refs from the local (by default) or from a remote repository.
 425# It accepts 0, 1 or 2 arguments:
 426# 1: The remote to list refs from (optional; ignored, if set but empty).
 427#    Can be the name of a configured remote, a path, or a URL.
 428# 2: In addition to local refs, list unique branches from refs/remotes/ for
 429#    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
 430# 3: A prefix to be added to each listed ref (optional).
 431# 4: List only refs matching this word (optional; list all refs if unset or
 432#    empty).
 433# 5: A suffix to be appended to each listed ref (optional; ignored, if set
 434#    but empty).
 435#
 436# Use __git_complete_refs() instead.
 437__git_refs ()
 438{
 439        local i hash dir track="${2-}"
 440        local list_refs_from=path remote="${1-}"
 441        local format refs
 442        local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
 443        local match="${4-}"
 444        local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
 445
 446        __git_find_repo_path
 447        dir="$__git_repo_path"
 448
 449        if [ -z "$remote" ]; then
 450                if [ -z "$dir" ]; then
 451                        return
 452                fi
 453        else
 454                if __git_is_configured_remote "$remote"; then
 455                        # configured remote takes precedence over a
 456                        # local directory with the same name
 457                        list_refs_from=remote
 458                elif [ -d "$remote/.git" ]; then
 459                        dir="$remote/.git"
 460                elif [ -d "$remote" ]; then
 461                        dir="$remote"
 462                else
 463                        list_refs_from=url
 464                fi
 465        fi
 466
 467        if [ "$list_refs_from" = path ]; then
 468                if [[ "$cur_" == ^* ]]; then
 469                        pfx="$pfx^"
 470                        fer_pfx="$fer_pfx^"
 471                        cur_=${cur_#^}
 472                        match=${match#^}
 473                fi
 474                case "$cur_" in
 475                refs|refs/*)
 476                        format="refname"
 477                        refs=("$match*" "$match*/**")
 478                        track=""
 479                        ;;
 480                *)
 481                        for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD; do
 482                                case "$i" in
 483                                $match*)
 484                                        if [ -e "$dir/$i" ]; then
 485                                                echo "$pfx$i$sfx"
 486                                        fi
 487                                        ;;
 488                                esac
 489                        done
 490                        format="refname:strip=2"
 491                        refs=("refs/tags/$match*" "refs/tags/$match*/**"
 492                                "refs/heads/$match*" "refs/heads/$match*/**"
 493                                "refs/remotes/$match*" "refs/remotes/$match*/**")
 494                        ;;
 495                esac
 496                __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
 497                        "${refs[@]}"
 498                if [ -n "$track" ]; then
 499                        # employ the heuristic used by git checkout
 500                        # Try to find a remote branch that matches the completion word
 501                        # but only output if the branch name is unique
 502                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 503                                --sort="refname:strip=3" \
 504                                "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
 505                        uniq -u
 506                fi
 507                return
 508        fi
 509        case "$cur_" in
 510        refs|refs/*)
 511                __git ls-remote "$remote" "$match*" | \
 512                while read -r hash i; do
 513                        case "$i" in
 514                        *^{}) ;;
 515                        *) echo "$pfx$i$sfx" ;;
 516                        esac
 517                done
 518                ;;
 519        *)
 520                if [ "$list_refs_from" = remote ]; then
 521                        case "HEAD" in
 522                        $match*)        echo "${pfx}HEAD$sfx" ;;
 523                        esac
 524                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 525                                "refs/remotes/$remote/$match*" \
 526                                "refs/remotes/$remote/$match*/**"
 527                else
 528                        local query_symref
 529                        case "HEAD" in
 530                        $match*)        query_symref="HEAD" ;;
 531                        esac
 532                        __git ls-remote "$remote" $query_symref \
 533                                "refs/tags/$match*" "refs/heads/$match*" \
 534                                "refs/remotes/$match*" |
 535                        while read -r hash i; do
 536                                case "$i" in
 537                                *^{})   ;;
 538                                refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
 539                                *)      echo "$pfx$i$sfx" ;;  # symbolic refs
 540                                esac
 541                        done
 542                fi
 543                ;;
 544        esac
 545}
 546
 547# Completes refs, short and long, local and remote, symbolic and pseudo.
 548#
 549# Usage: __git_complete_refs [<option>]...
 550# --remote=<remote>: The remote to list refs from, can be the name of a
 551#                    configured remote, a path, or a URL.
 552# --track: List unique remote branches for 'git checkout's tracking DWIMery.
 553# --pfx=<prefix>: A prefix to be added to each ref.
 554# --cur=<word>: The current ref to be completed.  Defaults to the current
 555#               word to be completed.
 556# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
 557#                 space.
 558__git_complete_refs ()
 559{
 560        local remote track pfx cur_="$cur" sfx=" "
 561
 562        while test $# != 0; do
 563                case "$1" in
 564                --remote=*)     remote="${1##--remote=}" ;;
 565                --track)        track="yes" ;;
 566                --pfx=*)        pfx="${1##--pfx=}" ;;
 567                --cur=*)        cur_="${1##--cur=}" ;;
 568                --sfx=*)        sfx="${1##--sfx=}" ;;
 569                *)              return 1 ;;
 570                esac
 571                shift
 572        done
 573
 574        __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
 575}
 576
 577# __git_refs2 requires 1 argument (to pass to __git_refs)
 578# Deprecated: use __git_complete_fetch_refspecs() instead.
 579__git_refs2 ()
 580{
 581        local i
 582        for i in $(__git_refs "$1"); do
 583                echo "$i:$i"
 584        done
 585}
 586
 587# Completes refspecs for fetching from a remote repository.
 588# 1: The remote repository.
 589# 2: A prefix to be added to each listed refspec (optional).
 590# 3: The ref to be completed as a refspec instead of the current word to be
 591#    completed (optional)
 592# 4: A suffix to be appended to each listed refspec instead of the default
 593#    space (optional).
 594__git_complete_fetch_refspecs ()
 595{
 596        local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
 597
 598        __gitcomp_direct "$(
 599                for i in $(__git_refs "$remote" "" "" "$cur_") ; do
 600                        echo "$pfx$i:$i$sfx"
 601                done
 602                )"
 603}
 604
 605# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 606__git_refs_remotes ()
 607{
 608        local i hash
 609        __git ls-remote "$1" 'refs/heads/*' | \
 610        while read -r hash i; do
 611                echo "$i:refs/remotes/$1/${i#refs/heads/}"
 612        done
 613}
 614
 615__git_remotes ()
 616{
 617        __git_find_repo_path
 618        test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
 619        __git remote
 620}
 621
 622# Returns true if $1 matches the name of a configured remote, false otherwise.
 623__git_is_configured_remote ()
 624{
 625        local remote
 626        for remote in $(__git_remotes); do
 627                if [ "$remote" = "$1" ]; then
 628                        return 0
 629                fi
 630        done
 631        return 1
 632}
 633
 634__git_list_merge_strategies ()
 635{
 636        LANG=C LC_ALL=C git merge -s help 2>&1 |
 637        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 638                s/\.$//
 639                s/.*://
 640                s/^[    ]*//
 641                s/[     ]*$//
 642                p
 643        }'
 644}
 645
 646__git_merge_strategies=
 647# 'git merge -s help' (and thus detection of the merge strategy
 648# list) fails, unfortunately, if run outside of any git working
 649# tree.  __git_merge_strategies is set to the empty string in
 650# that case, and the detection will be repeated the next time it
 651# is needed.
 652__git_compute_merge_strategies ()
 653{
 654        test -n "$__git_merge_strategies" ||
 655        __git_merge_strategies=$(__git_list_merge_strategies)
 656}
 657
 658__git_complete_revlist_file ()
 659{
 660        local pfx ls ref cur_="$cur"
 661        case "$cur_" in
 662        *..?*:*)
 663                return
 664                ;;
 665        ?*:*)
 666                ref="${cur_%%:*}"
 667                cur_="${cur_#*:}"
 668                case "$cur_" in
 669                ?*/*)
 670                        pfx="${cur_%/*}"
 671                        cur_="${cur_##*/}"
 672                        ls="$ref:$pfx"
 673                        pfx="$pfx/"
 674                        ;;
 675                *)
 676                        ls="$ref"
 677                        ;;
 678                esac
 679
 680                case "$COMP_WORDBREAKS" in
 681                *:*) : great ;;
 682                *)   pfx="$ref:$pfx" ;;
 683                esac
 684
 685                __gitcomp_nl "$(__git ls-tree "$ls" \
 686                                | sed '/^100... blob /{
 687                                           s,^.*        ,,
 688                                           s,$, ,
 689                                       }
 690                                       /^120000 blob /{
 691                                           s,^.*        ,,
 692                                           s,$, ,
 693                                       }
 694                                       /^040000 tree /{
 695                                           s,^.*        ,,
 696                                           s,$,/,
 697                                       }
 698                                       s/^.*    //')" \
 699                        "$pfx" "$cur_" ""
 700                ;;
 701        *...*)
 702                pfx="${cur_%...*}..."
 703                cur_="${cur_#*...}"
 704                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 705                ;;
 706        *..*)
 707                pfx="${cur_%..*}.."
 708                cur_="${cur_#*..}"
 709                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 710                ;;
 711        *)
 712                __git_complete_refs
 713                ;;
 714        esac
 715}
 716
 717
 718# __git_complete_index_file requires 1 argument:
 719# 1: the options to pass to ls-file
 720#
 721# The exception is --committable, which finds the files appropriate commit.
 722__git_complete_index_file ()
 723{
 724        local pfx="" cur_="$cur"
 725
 726        case "$cur_" in
 727        ?*/*)
 728                pfx="${cur_%/*}"
 729                cur_="${cur_##*/}"
 730                pfx="${pfx}/"
 731                ;;
 732        esac
 733
 734        __gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
 735}
 736
 737__git_complete_file ()
 738{
 739        __git_complete_revlist_file
 740}
 741
 742__git_complete_revlist ()
 743{
 744        __git_complete_revlist_file
 745}
 746
 747__git_complete_remote_or_refspec ()
 748{
 749        local cur_="$cur" cmd="${words[1]}"
 750        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
 751        if [ "$cmd" = "remote" ]; then
 752                ((c++))
 753        fi
 754        while [ $c -lt $cword ]; do
 755                i="${words[c]}"
 756                case "$i" in
 757                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
 758                -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
 759                --all)
 760                        case "$cmd" in
 761                        push) no_complete_refspec=1 ;;
 762                        fetch)
 763                                return
 764                                ;;
 765                        *) ;;
 766                        esac
 767                        ;;
 768                -*) ;;
 769                *) remote="$i"; break ;;
 770                esac
 771                ((c++))
 772        done
 773        if [ -z "$remote" ]; then
 774                __gitcomp_nl "$(__git_remotes)"
 775                return
 776        fi
 777        if [ $no_complete_refspec = 1 ]; then
 778                return
 779        fi
 780        [ "$remote" = "." ] && remote=
 781        case "$cur_" in
 782        *:*)
 783                case "$COMP_WORDBREAKS" in
 784                *:*) : great ;;
 785                *)   pfx="${cur_%%:*}:" ;;
 786                esac
 787                cur_="${cur_#*:}"
 788                lhs=0
 789                ;;
 790        +*)
 791                pfx="+"
 792                cur_="${cur_#+}"
 793                ;;
 794        esac
 795        case "$cmd" in
 796        fetch)
 797                if [ $lhs = 1 ]; then
 798                        __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
 799                else
 800                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 801                fi
 802                ;;
 803        pull|remote)
 804                if [ $lhs = 1 ]; then
 805                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 806                else
 807                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 808                fi
 809                ;;
 810        push)
 811                if [ $lhs = 1 ]; then
 812                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 813                else
 814                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 815                fi
 816                ;;
 817        esac
 818}
 819
 820__git_complete_strategy ()
 821{
 822        __git_compute_merge_strategies
 823        case "$prev" in
 824        -s|--strategy)
 825                __gitcomp "$__git_merge_strategies"
 826                return 0
 827        esac
 828        case "$cur" in
 829        --strategy=*)
 830                __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
 831                return 0
 832                ;;
 833        esac
 834        return 1
 835}
 836
 837__git_all_commands=
 838__git_compute_all_commands ()
 839{
 840        test -n "$__git_all_commands" ||
 841        __git_all_commands=$(git --list-cmds=main,others,alias,nohelpers)
 842}
 843
 844# Lists all set config variables starting with the given section prefix,
 845# with the prefix removed.
 846__git_get_config_variables ()
 847{
 848        local section="$1" i IFS=$'\n'
 849        for i in $(__git config --name-only --get-regexp "^$section\..*"); do
 850                echo "${i#$section.}"
 851        done
 852}
 853
 854__git_pretty_aliases ()
 855{
 856        __git_get_config_variables "pretty"
 857}
 858
 859# __git_aliased_command requires 1 argument
 860__git_aliased_command ()
 861{
 862        local word cmdline=$(__git config --get "alias.$1")
 863        for word in $cmdline; do
 864                case "$word" in
 865                \!gitk|gitk)
 866                        echo "gitk"
 867                        return
 868                        ;;
 869                \!*)    : shell command alias ;;
 870                -*)     : option ;;
 871                *=*)    : setting env ;;
 872                git)    : git itself ;;
 873                \(\))   : skip parens of shell function definition ;;
 874                {)      : skip start of shell helper function ;;
 875                :)      : skip null command ;;
 876                \'*)    : skip opening quote after sh -c ;;
 877                *)
 878                        echo "$word"
 879                        return
 880                esac
 881        done
 882}
 883
 884# __git_find_on_cmdline requires 1 argument
 885__git_find_on_cmdline ()
 886{
 887        local word subcommand c=1
 888        while [ $c -lt $cword ]; do
 889                word="${words[c]}"
 890                for subcommand in $1; do
 891                        if [ "$subcommand" = "$word" ]; then
 892                                echo "$subcommand"
 893                                return
 894                        fi
 895                done
 896                ((c++))
 897        done
 898}
 899
 900# Echo the value of an option set on the command line or config
 901#
 902# $1: short option name
 903# $2: long option name including =
 904# $3: list of possible values
 905# $4: config string (optional)
 906#
 907# example:
 908# result="$(__git_get_option_value "-d" "--do-something=" \
 909#     "yes no" "core.doSomething")"
 910#
 911# result is then either empty (no option set) or "yes" or "no"
 912#
 913# __git_get_option_value requires 3 arguments
 914__git_get_option_value ()
 915{
 916        local c short_opt long_opt val
 917        local result= values config_key word
 918
 919        short_opt="$1"
 920        long_opt="$2"
 921        values="$3"
 922        config_key="$4"
 923
 924        ((c = $cword - 1))
 925        while [ $c -ge 0 ]; do
 926                word="${words[c]}"
 927                for val in $values; do
 928                        if [ "$short_opt$val" = "$word" ] ||
 929                           [ "$long_opt$val"  = "$word" ]; then
 930                                result="$val"
 931                                break 2
 932                        fi
 933                done
 934                ((c--))
 935        done
 936
 937        if [ -n "$config_key" ] && [ -z "$result" ]; then
 938                result="$(__git config "$config_key")"
 939        fi
 940
 941        echo "$result"
 942}
 943
 944__git_has_doubledash ()
 945{
 946        local c=1
 947        while [ $c -lt $cword ]; do
 948                if [ "--" = "${words[c]}" ]; then
 949                        return 0
 950                fi
 951                ((c++))
 952        done
 953        return 1
 954}
 955
 956# Try to count non option arguments passed on the command line for the
 957# specified git command.
 958# When options are used, it is necessary to use the special -- option to
 959# tell the implementation were non option arguments begin.
 960# XXX this can not be improved, since options can appear everywhere, as
 961# an example:
 962#       git mv x -n y
 963#
 964# __git_count_arguments requires 1 argument: the git command executed.
 965__git_count_arguments ()
 966{
 967        local word i c=0
 968
 969        # Skip "git" (first argument)
 970        for ((i=1; i < ${#words[@]}; i++)); do
 971                word="${words[i]}"
 972
 973                case "$word" in
 974                        --)
 975                                # Good; we can assume that the following are only non
 976                                # option arguments.
 977                                ((c = 0))
 978                                ;;
 979                        "$1")
 980                                # Skip the specified git command and discard git
 981                                # main options
 982                                ((c = 0))
 983                                ;;
 984                        ?*)
 985                                ((c++))
 986                                ;;
 987                esac
 988        done
 989
 990        printf "%d" $c
 991}
 992
 993__git_whitespacelist="nowarn warn error error-all fix"
 994__git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
 995
 996_git_am ()
 997{
 998        __git_find_repo_path
 999        if [ -d "$__git_repo_path"/rebase-apply ]; then
1000                __gitcomp "$__git_am_inprogress_options"
1001                return
1002        fi
1003        case "$cur" in
1004        --whitespace=*)
1005                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1006                return
1007                ;;
1008        --*)
1009                __gitcomp_builtin am "--no-utf8" \
1010                        "$__git_am_inprogress_options"
1011                return
1012        esac
1013}
1014
1015_git_apply ()
1016{
1017        case "$cur" in
1018        --whitespace=*)
1019                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1020                return
1021                ;;
1022        --*)
1023                __gitcomp_builtin apply
1024                return
1025        esac
1026}
1027
1028_git_add ()
1029{
1030        case "$cur" in
1031        --*)
1032                __gitcomp_builtin add
1033                return
1034        esac
1035
1036        local complete_opt="--others --modified --directory --no-empty-directory"
1037        if test -n "$(__git_find_on_cmdline "-u --update")"
1038        then
1039                complete_opt="--modified"
1040        fi
1041        __git_complete_index_file "$complete_opt"
1042}
1043
1044_git_archive ()
1045{
1046        case "$cur" in
1047        --format=*)
1048                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1049                return
1050                ;;
1051        --remote=*)
1052                __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1053                return
1054                ;;
1055        --*)
1056                __gitcomp "
1057                        --format= --list --verbose
1058                        --prefix= --remote= --exec= --output
1059                        "
1060                return
1061                ;;
1062        esac
1063        __git_complete_file
1064}
1065
1066_git_bisect ()
1067{
1068        __git_has_doubledash && return
1069
1070        local subcommands="start bad good skip reset visualize replay log run"
1071        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1072        if [ -z "$subcommand" ]; then
1073                __git_find_repo_path
1074                if [ -f "$__git_repo_path"/BISECT_START ]; then
1075                        __gitcomp "$subcommands"
1076                else
1077                        __gitcomp "replay start"
1078                fi
1079                return
1080        fi
1081
1082        case "$subcommand" in
1083        bad|good|reset|skip|start)
1084                __git_complete_refs
1085                ;;
1086        *)
1087                ;;
1088        esac
1089}
1090
1091_git_branch ()
1092{
1093        local i c=1 only_local_ref="n" has_r="n"
1094
1095        while [ $c -lt $cword ]; do
1096                i="${words[c]}"
1097                case "$i" in
1098                -d|--delete|-m|--move)  only_local_ref="y" ;;
1099                -r|--remotes)           has_r="y" ;;
1100                esac
1101                ((c++))
1102        done
1103
1104        case "$cur" in
1105        --set-upstream-to=*)
1106                __git_complete_refs --cur="${cur##--set-upstream-to=}"
1107                ;;
1108        --*)
1109                __gitcomp_builtin branch "--no-color --no-abbrev
1110                        --no-track --no-column
1111                        "
1112                ;;
1113        *)
1114                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1115                        __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1116                else
1117                        __git_complete_refs
1118                fi
1119                ;;
1120        esac
1121}
1122
1123_git_bundle ()
1124{
1125        local cmd="${words[2]}"
1126        case "$cword" in
1127        2)
1128                __gitcomp "create list-heads verify unbundle"
1129                ;;
1130        3)
1131                # looking for a file
1132                ;;
1133        *)
1134                case "$cmd" in
1135                        create)
1136                                __git_complete_revlist
1137                        ;;
1138                esac
1139                ;;
1140        esac
1141}
1142
1143_git_checkout ()
1144{
1145        __git_has_doubledash && return
1146
1147        case "$cur" in
1148        --conflict=*)
1149                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1150                ;;
1151        --*)
1152                __gitcomp_builtin checkout "--no-track --no-recurse-submodules"
1153                ;;
1154        *)
1155                # check if --track, --no-track, or --no-guess was specified
1156                # if so, disable DWIM mode
1157                local flags="--track --no-track --no-guess" track_opt="--track"
1158                if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1159                   [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1160                        track_opt=''
1161                fi
1162                __git_complete_refs $track_opt
1163                ;;
1164        esac
1165}
1166
1167_git_cherry ()
1168{
1169        case "$cur" in
1170        --*)
1171                __gitcomp_builtin cherry
1172                return
1173        esac
1174
1175        __git_complete_refs
1176}
1177
1178__git_cherry_pick_inprogress_options="--continue --quit --abort"
1179
1180_git_cherry_pick ()
1181{
1182        __git_find_repo_path
1183        if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1184                __gitcomp "$__git_cherry_pick_inprogress_options"
1185                return
1186        fi
1187        case "$cur" in
1188        --*)
1189                __gitcomp_builtin cherry-pick "" \
1190                        "$__git_cherry_pick_inprogress_options"
1191                ;;
1192        *)
1193                __git_complete_refs
1194                ;;
1195        esac
1196}
1197
1198_git_clean ()
1199{
1200        case "$cur" in
1201        --*)
1202                __gitcomp_builtin clean
1203                return
1204                ;;
1205        esac
1206
1207        # XXX should we check for -x option ?
1208        __git_complete_index_file "--others --directory"
1209}
1210
1211_git_clone ()
1212{
1213        case "$cur" in
1214        --*)
1215                __gitcomp_builtin clone "--no-single-branch"
1216                return
1217                ;;
1218        esac
1219}
1220
1221__git_untracked_file_modes="all no normal"
1222
1223_git_commit ()
1224{
1225        case "$prev" in
1226        -c|-C)
1227                __git_complete_refs
1228                return
1229                ;;
1230        esac
1231
1232        case "$cur" in
1233        --cleanup=*)
1234                __gitcomp "default scissors strip verbatim whitespace
1235                        " "" "${cur##--cleanup=}"
1236                return
1237                ;;
1238        --reuse-message=*|--reedit-message=*|\
1239        --fixup=*|--squash=*)
1240                __git_complete_refs --cur="${cur#*=}"
1241                return
1242                ;;
1243        --untracked-files=*)
1244                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1245                return
1246                ;;
1247        --*)
1248                __gitcomp_builtin commit "--no-edit --verify"
1249                return
1250        esac
1251
1252        if __git rev-parse --verify --quiet HEAD >/dev/null; then
1253                __git_complete_index_file "--committable"
1254        else
1255                # This is the first commit
1256                __git_complete_index_file "--cached"
1257        fi
1258}
1259
1260_git_describe ()
1261{
1262        case "$cur" in
1263        --*)
1264                __gitcomp_builtin describe
1265                return
1266        esac
1267        __git_complete_refs
1268}
1269
1270__git_diff_algorithms="myers minimal patience histogram"
1271
1272__git_diff_submodule_formats="diff log short"
1273
1274__git_diff_common_options="--stat --numstat --shortstat --summary
1275                        --patch-with-stat --name-only --name-status --color
1276                        --no-color --color-words --no-renames --check
1277                        --full-index --binary --abbrev --diff-filter=
1278                        --find-copies-harder --ignore-cr-at-eol
1279                        --text --ignore-space-at-eol --ignore-space-change
1280                        --ignore-all-space --ignore-blank-lines --exit-code
1281                        --quiet --ext-diff --no-ext-diff
1282                        --no-prefix --src-prefix= --dst-prefix=
1283                        --inter-hunk-context=
1284                        --patience --histogram --minimal
1285                        --raw --word-diff --word-diff-regex=
1286                        --dirstat --dirstat= --dirstat-by-file
1287                        --dirstat-by-file= --cumulative
1288                        --diff-algorithm=
1289                        --submodule --submodule= --ignore-submodules
1290"
1291
1292_git_diff ()
1293{
1294        __git_has_doubledash && return
1295
1296        case "$cur" in
1297        --diff-algorithm=*)
1298                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1299                return
1300                ;;
1301        --submodule=*)
1302                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1303                return
1304                ;;
1305        --*)
1306                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1307                        --base --ours --theirs --no-index
1308                        $__git_diff_common_options
1309                        "
1310                return
1311                ;;
1312        esac
1313        __git_complete_revlist_file
1314}
1315
1316__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1317                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1318"
1319
1320_git_difftool ()
1321{
1322        __git_has_doubledash && return
1323
1324        case "$cur" in
1325        --tool=*)
1326                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1327                return
1328                ;;
1329        --*)
1330                __gitcomp_builtin difftool "$__git_diff_common_options
1331                                        --base --cached --ours --theirs
1332                                        --pickaxe-all --pickaxe-regex
1333                                        --relative --staged
1334                                        "
1335                return
1336                ;;
1337        esac
1338        __git_complete_revlist_file
1339}
1340
1341__git_fetch_recurse_submodules="yes on-demand no"
1342
1343_git_fetch ()
1344{
1345        case "$cur" in
1346        --recurse-submodules=*)
1347                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1348                return
1349                ;;
1350        --*)
1351                __gitcomp_builtin fetch "--no-tags"
1352                return
1353                ;;
1354        esac
1355        __git_complete_remote_or_refspec
1356}
1357
1358__git_format_patch_options="
1359        --stdout --attach --no-attach --thread --thread= --no-thread
1360        --numbered --start-number --numbered-files --keep-subject --signoff
1361        --signature --no-signature --in-reply-to= --cc= --full-index --binary
1362        --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1363        --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1364        --output-directory --reroll-count --to= --quiet --notes
1365"
1366
1367_git_format_patch ()
1368{
1369        case "$cur" in
1370        --thread=*)
1371                __gitcomp "
1372                        deep shallow
1373                        " "" "${cur##--thread=}"
1374                return
1375                ;;
1376        --*)
1377                __gitcomp "$__git_format_patch_options"
1378                return
1379                ;;
1380        esac
1381        __git_complete_revlist
1382}
1383
1384_git_fsck ()
1385{
1386        case "$cur" in
1387        --*)
1388                __gitcomp_builtin fsck "--no-reflogs"
1389                return
1390                ;;
1391        esac
1392}
1393
1394_git_gitk ()
1395{
1396        _gitk
1397}
1398
1399# Lists matching symbol names from a tag (as in ctags) file.
1400# 1: List symbol names matching this word.
1401# 2: The tag file to list symbol names from.
1402# 3: A prefix to be added to each listed symbol name (optional).
1403# 4: A suffix to be appended to each listed symbol name (optional).
1404__git_match_ctag () {
1405        awk -v pfx="${3-}" -v sfx="${4-}" "
1406                /^${1//\//\\/}/ { print pfx \$1 sfx }
1407                " "$2"
1408}
1409
1410# Complete symbol names from a tag file.
1411# Usage: __git_complete_symbol [<option>]...
1412# --tags=<file>: The tag file to list symbol names from instead of the
1413#                default "tags".
1414# --pfx=<prefix>: A prefix to be added to each symbol name.
1415# --cur=<word>: The current symbol name to be completed.  Defaults to
1416#               the current word to be completed.
1417# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1418#                 of the default space.
1419__git_complete_symbol () {
1420        local tags=tags pfx="" cur_="${cur-}" sfx=" "
1421
1422        while test $# != 0; do
1423                case "$1" in
1424                --tags=*)       tags="${1##--tags=}" ;;
1425                --pfx=*)        pfx="${1##--pfx=}" ;;
1426                --cur=*)        cur_="${1##--cur=}" ;;
1427                --sfx=*)        sfx="${1##--sfx=}" ;;
1428                *)              return 1 ;;
1429                esac
1430                shift
1431        done
1432
1433        if test -r "$tags"; then
1434                __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1435        fi
1436}
1437
1438_git_grep ()
1439{
1440        __git_has_doubledash && return
1441
1442        case "$cur" in
1443        --*)
1444                __gitcomp_builtin grep
1445                return
1446                ;;
1447        esac
1448
1449        case "$cword,$prev" in
1450        2,*|*,-*)
1451                __git_complete_symbol && return
1452                ;;
1453        esac
1454
1455        __git_complete_refs
1456}
1457
1458_git_help ()
1459{
1460        case "$cur" in
1461        --*)
1462                __gitcomp_builtin help
1463                return
1464                ;;
1465        esac
1466        if test -n "$GIT_TESTING_ALL_COMMAND_LIST"
1467        then
1468                __gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(git --list-cmds=alias,list-guide) gitk"
1469        else
1470                __gitcomp "$(git --list-cmds=main,nohelpers,alias,list-guide) gitk"
1471        fi
1472}
1473
1474_git_init ()
1475{
1476        case "$cur" in
1477        --shared=*)
1478                __gitcomp "
1479                        false true umask group all world everybody
1480                        " "" "${cur##--shared=}"
1481                return
1482                ;;
1483        --*)
1484                __gitcomp_builtin init
1485                return
1486                ;;
1487        esac
1488}
1489
1490_git_ls_files ()
1491{
1492        case "$cur" in
1493        --*)
1494                __gitcomp_builtin ls-files "--no-empty-directory"
1495                return
1496                ;;
1497        esac
1498
1499        # XXX ignore options like --modified and always suggest all cached
1500        # files.
1501        __git_complete_index_file "--cached"
1502}
1503
1504_git_ls_remote ()
1505{
1506        case "$cur" in
1507        --*)
1508                __gitcomp_builtin ls-remote
1509                return
1510                ;;
1511        esac
1512        __gitcomp_nl "$(__git_remotes)"
1513}
1514
1515_git_ls_tree ()
1516{
1517        case "$cur" in
1518        --*)
1519                __gitcomp_builtin ls-tree
1520                return
1521                ;;
1522        esac
1523
1524        __git_complete_file
1525}
1526
1527# Options that go well for log, shortlog and gitk
1528__git_log_common_options="
1529        --not --all
1530        --branches --tags --remotes
1531        --first-parent --merges --no-merges
1532        --max-count=
1533        --max-age= --since= --after=
1534        --min-age= --until= --before=
1535        --min-parents= --max-parents=
1536        --no-min-parents --no-max-parents
1537"
1538# Options that go well for log and gitk (not shortlog)
1539__git_log_gitk_options="
1540        --dense --sparse --full-history
1541        --simplify-merges --simplify-by-decoration
1542        --left-right --notes --no-notes
1543"
1544# Options that go well for log and shortlog (not gitk)
1545__git_log_shortlog_options="
1546        --author= --committer= --grep=
1547        --all-match --invert-grep
1548"
1549
1550__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1551__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1552
1553_git_log ()
1554{
1555        __git_has_doubledash && return
1556        __git_find_repo_path
1557
1558        local merge=""
1559        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1560                merge="--merge"
1561        fi
1562        case "$prev,$cur" in
1563        -L,:*:*)
1564                return  # fall back to Bash filename completion
1565                ;;
1566        -L,:*)
1567                __git_complete_symbol --cur="${cur#:}" --sfx=":"
1568                return
1569                ;;
1570        -G,*|-S,*)
1571                __git_complete_symbol
1572                return
1573                ;;
1574        esac
1575        case "$cur" in
1576        --pretty=*|--format=*)
1577                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1578                        " "" "${cur#*=}"
1579                return
1580                ;;
1581        --date=*)
1582                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1583                return
1584                ;;
1585        --decorate=*)
1586                __gitcomp "full short no" "" "${cur##--decorate=}"
1587                return
1588                ;;
1589        --diff-algorithm=*)
1590                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1591                return
1592                ;;
1593        --submodule=*)
1594                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1595                return
1596                ;;
1597        --*)
1598                __gitcomp "
1599                        $__git_log_common_options
1600                        $__git_log_shortlog_options
1601                        $__git_log_gitk_options
1602                        --root --topo-order --date-order --reverse
1603                        --follow --full-diff
1604                        --abbrev-commit --abbrev=
1605                        --relative-date --date=
1606                        --pretty= --format= --oneline
1607                        --show-signature
1608                        --cherry-mark
1609                        --cherry-pick
1610                        --graph
1611                        --decorate --decorate=
1612                        --walk-reflogs
1613                        --parents --children
1614                        $merge
1615                        $__git_diff_common_options
1616                        --pickaxe-all --pickaxe-regex
1617                        "
1618                return
1619                ;;
1620        -L:*:*)
1621                return  # fall back to Bash filename completion
1622                ;;
1623        -L:*)
1624                __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1625                return
1626                ;;
1627        -G*)
1628                __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1629                return
1630                ;;
1631        -S*)
1632                __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1633                return
1634                ;;
1635        esac
1636        __git_complete_revlist
1637}
1638
1639_git_merge ()
1640{
1641        __git_complete_strategy && return
1642
1643        case "$cur" in
1644        --*)
1645                __gitcomp_builtin merge "--no-rerere-autoupdate
1646                                --no-commit --no-edit --no-ff
1647                                --no-log --no-progress
1648                                --no-squash --no-stat
1649                                --no-verify-signatures
1650                                "
1651                return
1652        esac
1653        __git_complete_refs
1654}
1655
1656_git_mergetool ()
1657{
1658        case "$cur" in
1659        --tool=*)
1660                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1661                return
1662                ;;
1663        --*)
1664                __gitcomp "--tool= --prompt --no-prompt"
1665                return
1666                ;;
1667        esac
1668}
1669
1670_git_merge_base ()
1671{
1672        case "$cur" in
1673        --*)
1674                __gitcomp_builtin merge-base
1675                return
1676                ;;
1677        esac
1678        __git_complete_refs
1679}
1680
1681_git_mv ()
1682{
1683        case "$cur" in
1684        --*)
1685                __gitcomp_builtin mv
1686                return
1687                ;;
1688        esac
1689
1690        if [ $(__git_count_arguments "mv") -gt 0 ]; then
1691                # We need to show both cached and untracked files (including
1692                # empty directories) since this may not be the last argument.
1693                __git_complete_index_file "--cached --others --directory"
1694        else
1695                __git_complete_index_file "--cached"
1696        fi
1697}
1698
1699_git_notes ()
1700{
1701        local subcommands='add append copy edit get-ref list merge prune remove show'
1702        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1703
1704        case "$subcommand,$cur" in
1705        ,--*)
1706                __gitcomp_builtin notes
1707                ;;
1708        ,*)
1709                case "$prev" in
1710                --ref)
1711                        __git_complete_refs
1712                        ;;
1713                *)
1714                        __gitcomp "$subcommands --ref"
1715                        ;;
1716                esac
1717                ;;
1718        *,--reuse-message=*|*,--reedit-message=*)
1719                __git_complete_refs --cur="${cur#*=}"
1720                ;;
1721        *,--*)
1722                __gitcomp_builtin notes_$subcommand
1723                ;;
1724        prune,*|get-ref,*)
1725                # this command does not take a ref, do not complete it
1726                ;;
1727        *)
1728                case "$prev" in
1729                -m|-F)
1730                        ;;
1731                *)
1732                        __git_complete_refs
1733                        ;;
1734                esac
1735                ;;
1736        esac
1737}
1738
1739_git_pull ()
1740{
1741        __git_complete_strategy && return
1742
1743        case "$cur" in
1744        --recurse-submodules=*)
1745                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1746                return
1747                ;;
1748        --*)
1749                __gitcomp_builtin pull "--no-autostash --no-commit --no-edit
1750                                        --no-ff --no-log --no-progress --no-rebase
1751                                        --no-squash --no-stat --no-tags
1752                                        --no-verify-signatures"
1753
1754                return
1755                ;;
1756        esac
1757        __git_complete_remote_or_refspec
1758}
1759
1760__git_push_recurse_submodules="check on-demand only"
1761
1762__git_complete_force_with_lease ()
1763{
1764        local cur_=$1
1765
1766        case "$cur_" in
1767        --*=)
1768                ;;
1769        *:*)
1770                __git_complete_refs --cur="${cur_#*:}"
1771                ;;
1772        *)
1773                __git_complete_refs --cur="$cur_"
1774                ;;
1775        esac
1776}
1777
1778_git_push ()
1779{
1780        case "$prev" in
1781        --repo)
1782                __gitcomp_nl "$(__git_remotes)"
1783                return
1784                ;;
1785        --recurse-submodules)
1786                __gitcomp "$__git_push_recurse_submodules"
1787                return
1788                ;;
1789        esac
1790        case "$cur" in
1791        --repo=*)
1792                __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1793                return
1794                ;;
1795        --recurse-submodules=*)
1796                __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1797                return
1798                ;;
1799        --force-with-lease=*)
1800                __git_complete_force_with_lease "${cur##--force-with-lease=}"
1801                return
1802                ;;
1803        --*)
1804                __gitcomp_builtin push
1805                return
1806                ;;
1807        esac
1808        __git_complete_remote_or_refspec
1809}
1810
1811_git_rebase ()
1812{
1813        __git_find_repo_path
1814        if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1815                __gitcomp "--continue --skip --abort --quit --edit-todo --show-current-patch"
1816                return
1817        elif [ -d "$__git_repo_path"/rebase-apply ] || \
1818             [ -d "$__git_repo_path"/rebase-merge ]; then
1819                __gitcomp "--continue --skip --abort --quit --show-current-patch"
1820                return
1821        fi
1822        __git_complete_strategy && return
1823        case "$cur" in
1824        --whitespace=*)
1825                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1826                return
1827                ;;
1828        --*)
1829                __gitcomp "
1830                        --onto --merge --strategy --interactive
1831                        --preserve-merges --stat --no-stat
1832                        --committer-date-is-author-date --ignore-date
1833                        --ignore-whitespace --whitespace=
1834                        --autosquash --no-autosquash
1835                        --fork-point --no-fork-point
1836                        --autostash --no-autostash
1837                        --verify --no-verify
1838                        --keep-empty --root --force-rebase --no-ff
1839                        --rerere-autoupdate
1840                        --exec
1841                        "
1842
1843                return
1844        esac
1845        __git_complete_refs
1846}
1847
1848_git_reflog ()
1849{
1850        local subcommands="show delete expire"
1851        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1852
1853        if [ -z "$subcommand" ]; then
1854                __gitcomp "$subcommands"
1855        else
1856                __git_complete_refs
1857        fi
1858}
1859
1860__git_send_email_confirm_options="always never auto cc compose"
1861__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1862
1863_git_send_email ()
1864{
1865        case "$prev" in
1866        --to|--cc|--bcc|--from)
1867                __gitcomp "$(__git send-email --dump-aliases)"
1868                return
1869                ;;
1870        esac
1871
1872        case "$cur" in
1873        --confirm=*)
1874                __gitcomp "
1875                        $__git_send_email_confirm_options
1876                        " "" "${cur##--confirm=}"
1877                return
1878                ;;
1879        --suppress-cc=*)
1880                __gitcomp "
1881                        $__git_send_email_suppresscc_options
1882                        " "" "${cur##--suppress-cc=}"
1883
1884                return
1885                ;;
1886        --smtp-encryption=*)
1887                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
1888                return
1889                ;;
1890        --thread=*)
1891                __gitcomp "
1892                        deep shallow
1893                        " "" "${cur##--thread=}"
1894                return
1895                ;;
1896        --to=*|--cc=*|--bcc=*|--from=*)
1897                __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
1898                return
1899                ;;
1900        --*)
1901                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1902                        --compose --confirm= --dry-run --envelope-sender
1903                        --from --identity
1904                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
1905                        --no-suppress-from --no-thread --quiet --reply-to
1906                        --signed-off-by-cc --smtp-pass --smtp-server
1907                        --smtp-server-port --smtp-encryption= --smtp-user
1908                        --subject --suppress-cc= --suppress-from --thread --to
1909                        --validate --no-validate
1910                        $__git_format_patch_options"
1911                return
1912                ;;
1913        esac
1914        __git_complete_revlist
1915}
1916
1917_git_stage ()
1918{
1919        _git_add
1920}
1921
1922_git_status ()
1923{
1924        local complete_opt
1925        local untracked_state
1926
1927        case "$cur" in
1928        --ignore-submodules=*)
1929                __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
1930                return
1931                ;;
1932        --untracked-files=*)
1933                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1934                return
1935                ;;
1936        --column=*)
1937                __gitcomp "
1938                        always never auto column row plain dense nodense
1939                        " "" "${cur##--column=}"
1940                return
1941                ;;
1942        --*)
1943                __gitcomp_builtin status "--no-column"
1944                return
1945                ;;
1946        esac
1947
1948        untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
1949                "$__git_untracked_file_modes" "status.showUntrackedFiles")"
1950
1951        case "$untracked_state" in
1952        no)
1953                # --ignored option does not matter
1954                complete_opt=
1955                ;;
1956        all|normal|*)
1957                complete_opt="--cached --directory --no-empty-directory --others"
1958
1959                if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
1960                        complete_opt="$complete_opt --ignored --exclude=*"
1961                fi
1962                ;;
1963        esac
1964
1965        __git_complete_index_file "$complete_opt"
1966}
1967
1968__git_config_get_set_variables ()
1969{
1970        local prevword word config_file= c=$cword
1971        while [ $c -gt 1 ]; do
1972                word="${words[c]}"
1973                case "$word" in
1974                --system|--global|--local|--file=*)
1975                        config_file="$word"
1976                        break
1977                        ;;
1978                -f|--file)
1979                        config_file="$word $prevword"
1980                        break
1981                        ;;
1982                esac
1983                prevword=$word
1984                c=$((--c))
1985        done
1986
1987        __git config $config_file --name-only --list
1988}
1989
1990_git_config ()
1991{
1992        case "$prev" in
1993        branch.*.remote|branch.*.pushremote)
1994                __gitcomp_nl "$(__git_remotes)"
1995                return
1996                ;;
1997        branch.*.merge)
1998                __git_complete_refs
1999                return
2000                ;;
2001        branch.*.rebase)
2002                __gitcomp "false true preserve interactive"
2003                return
2004                ;;
2005        remote.pushdefault)
2006                __gitcomp_nl "$(__git_remotes)"
2007                return
2008                ;;
2009        remote.*.fetch)
2010                local remote="${prev#remote.}"
2011                remote="${remote%.fetch}"
2012                if [ -z "$cur" ]; then
2013                        __gitcomp_nl "refs/heads/" "" "" ""
2014                        return
2015                fi
2016                __gitcomp_nl "$(__git_refs_remotes "$remote")"
2017                return
2018                ;;
2019        remote.*.push)
2020                local remote="${prev#remote.}"
2021                remote="${remote%.push}"
2022                __gitcomp_nl "$(__git for-each-ref \
2023                        --format='%(refname):%(refname)' refs/heads)"
2024                return
2025                ;;
2026        pull.twohead|pull.octopus)
2027                __git_compute_merge_strategies
2028                __gitcomp "$__git_merge_strategies"
2029                return
2030                ;;
2031        color.branch|color.diff|color.interactive|\
2032        color.showbranch|color.status|color.ui)
2033                __gitcomp "always never auto"
2034                return
2035                ;;
2036        color.pager)
2037                __gitcomp "false true"
2038                return
2039                ;;
2040        color.*.*)
2041                __gitcomp "
2042                        normal black red green yellow blue magenta cyan white
2043                        bold dim ul blink reverse
2044                        "
2045                return
2046                ;;
2047        diff.submodule)
2048                __gitcomp "log short"
2049                return
2050                ;;
2051        help.format)
2052                __gitcomp "man info web html"
2053                return
2054                ;;
2055        log.date)
2056                __gitcomp "$__git_log_date_formats"
2057                return
2058                ;;
2059        sendemail.aliasesfiletype)
2060                __gitcomp "mutt mailrc pine elm gnus"
2061                return
2062                ;;
2063        sendemail.confirm)
2064                __gitcomp "$__git_send_email_confirm_options"
2065                return
2066                ;;
2067        sendemail.suppresscc)
2068                __gitcomp "$__git_send_email_suppresscc_options"
2069                return
2070                ;;
2071        sendemail.transferencoding)
2072                __gitcomp "7bit 8bit quoted-printable base64"
2073                return
2074                ;;
2075        --get|--get-all|--unset|--unset-all)
2076                __gitcomp_nl "$(__git_config_get_set_variables)"
2077                return
2078                ;;
2079        *.*)
2080                return
2081                ;;
2082        esac
2083        case "$cur" in
2084        --*)
2085                __gitcomp_builtin config
2086                return
2087                ;;
2088        branch.*.*)
2089                local pfx="${cur%.*}." cur_="${cur##*.}"
2090                __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
2091                return
2092                ;;
2093        branch.*)
2094                local pfx="${cur%.*}." cur_="${cur#*.}"
2095                __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2096                __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
2097                return
2098                ;;
2099        guitool.*.*)
2100                local pfx="${cur%.*}." cur_="${cur##*.}"
2101                __gitcomp "
2102                        argprompt cmd confirm needsfile noconsole norescan
2103                        prompt revprompt revunmerged title
2104                        " "$pfx" "$cur_"
2105                return
2106                ;;
2107        difftool.*.*)
2108                local pfx="${cur%.*}." cur_="${cur##*.}"
2109                __gitcomp "cmd path" "$pfx" "$cur_"
2110                return
2111                ;;
2112        man.*.*)
2113                local pfx="${cur%.*}." cur_="${cur##*.}"
2114                __gitcomp "cmd path" "$pfx" "$cur_"
2115                return
2116                ;;
2117        mergetool.*.*)
2118                local pfx="${cur%.*}." cur_="${cur##*.}"
2119                __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2120                return
2121                ;;
2122        pager.*)
2123                local pfx="${cur%.*}." cur_="${cur#*.}"
2124                __git_compute_all_commands
2125                __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2126                return
2127                ;;
2128        remote.*.*)
2129                local pfx="${cur%.*}." cur_="${cur##*.}"
2130                __gitcomp "
2131                        url proxy fetch push mirror skipDefaultUpdate
2132                        receivepack uploadpack tagopt pushurl
2133                        " "$pfx" "$cur_"
2134                return
2135                ;;
2136        remote.*)
2137                local pfx="${cur%.*}." cur_="${cur#*.}"
2138                __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2139                __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
2140                return
2141                ;;
2142        url.*.*)
2143                local pfx="${cur%.*}." cur_="${cur##*.}"
2144                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2145                return
2146                ;;
2147        esac
2148        __gitcomp "
2149                add.ignoreErrors
2150                advice.amWorkDir
2151                advice.commitBeforeMerge
2152                advice.detachedHead
2153                advice.implicitIdentity
2154                advice.pushAlreadyExists
2155                advice.pushFetchFirst
2156                advice.pushNeedsForce
2157                advice.pushNonFFCurrent
2158                advice.pushNonFFMatching
2159                advice.pushUpdateRejected
2160                advice.resolveConflict
2161                advice.rmHints
2162                advice.statusHints
2163                advice.statusUoption
2164                advice.ignoredHook
2165                alias.
2166                am.keepcr
2167                am.threeWay
2168                apply.ignorewhitespace
2169                apply.whitespace
2170                branch.autosetupmerge
2171                branch.autosetuprebase
2172                browser.
2173                clean.requireForce
2174                color.branch
2175                color.branch.current
2176                color.branch.local
2177                color.branch.plain
2178                color.branch.remote
2179                color.decorate.HEAD
2180                color.decorate.branch
2181                color.decorate.remoteBranch
2182                color.decorate.stash
2183                color.decorate.tag
2184                color.diff
2185                color.diff.commit
2186                color.diff.frag
2187                color.diff.func
2188                color.diff.meta
2189                color.diff.new
2190                color.diff.old
2191                color.diff.plain
2192                color.diff.whitespace
2193                color.grep
2194                color.grep.context
2195                color.grep.filename
2196                color.grep.function
2197                color.grep.linenumber
2198                color.grep.match
2199                color.grep.selected
2200                color.grep.separator
2201                color.interactive
2202                color.interactive.error
2203                color.interactive.header
2204                color.interactive.help
2205                color.interactive.prompt
2206                color.pager
2207                color.showbranch
2208                color.status
2209                color.status.added
2210                color.status.changed
2211                color.status.header
2212                color.status.localBranch
2213                color.status.nobranch
2214                color.status.remoteBranch
2215                color.status.unmerged
2216                color.status.untracked
2217                color.status.updated
2218                color.ui
2219                commit.cleanup
2220                commit.gpgSign
2221                commit.status
2222                commit.template
2223                commit.verbose
2224                core.abbrev
2225                core.askpass
2226                core.attributesfile
2227                core.autocrlf
2228                core.bare
2229                core.bigFileThreshold
2230                core.checkStat
2231                core.commentChar
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 save list show apply clear drop pop create branch'
2657        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2658        if [ -z "$subcommand" ]; then
2659                case "$cur" in
2660                --*)
2661                        __gitcomp "$save_opts"
2662                        ;;
2663                *)
2664                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2665                                __gitcomp "$subcommands"
2666                        fi
2667                        ;;
2668                esac
2669        else
2670                case "$subcommand,$cur" in
2671                push,--*)
2672                        __gitcomp "$save_opts --message"
2673                        ;;
2674                save,--*)
2675                        __gitcomp "$save_opts"
2676                        ;;
2677                apply,--*|pop,--*)
2678                        __gitcomp "--index --quiet"
2679                        ;;
2680                drop,--*)
2681                        __gitcomp "--quiet"
2682                        ;;
2683                show,--*|branch,--*)
2684                        ;;
2685                branch,*)
2686                        if [ $cword -eq 3 ]; then
2687                                __git_complete_refs
2688                        else
2689                                __gitcomp_nl "$(__git stash list \
2690                                                | sed -n -e 's/:.*//p')"
2691                        fi
2692                        ;;
2693                show,*|apply,*|drop,*|pop,*)
2694                        __gitcomp_nl "$(__git stash list \
2695                                        | sed -n -e 's/:.*//p')"
2696                        ;;
2697                *)
2698                        ;;
2699                esac
2700        fi
2701}
2702
2703_git_submodule ()
2704{
2705        __git_has_doubledash && return
2706
2707        local subcommands="add status init deinit update summary foreach sync"
2708        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2709        if [ -z "$subcommand" ]; then
2710                case "$cur" in
2711                --*)
2712                        __gitcomp "--quiet"
2713                        ;;
2714                *)
2715                        __gitcomp "$subcommands"
2716                        ;;
2717                esac
2718                return
2719        fi
2720
2721        case "$subcommand,$cur" in
2722        add,--*)
2723                __gitcomp "--branch --force --name --reference --depth"
2724                ;;
2725        status,--*)
2726                __gitcomp "--cached --recursive"
2727                ;;
2728        deinit,--*)
2729                __gitcomp "--force --all"
2730                ;;
2731        update,--*)
2732                __gitcomp "
2733                        --init --remote --no-fetch
2734                        --recommend-shallow --no-recommend-shallow
2735                        --force --rebase --merge --reference --depth --recursive --jobs
2736                "
2737                ;;
2738        summary,--*)
2739                __gitcomp "--cached --files --summary-limit"
2740                ;;
2741        foreach,--*|sync,--*)
2742                __gitcomp "--recursive"
2743                ;;
2744        *)
2745                ;;
2746        esac
2747}
2748
2749_git_svn ()
2750{
2751        local subcommands="
2752                init fetch clone rebase dcommit log find-rev
2753                set-tree commit-diff info create-ignore propget
2754                proplist show-ignore show-externals branch tag blame
2755                migrate mkdirs reset gc
2756                "
2757        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2758        if [ -z "$subcommand" ]; then
2759                __gitcomp "$subcommands"
2760        else
2761                local remote_opts="--username= --config-dir= --no-auth-cache"
2762                local fc_opts="
2763                        --follow-parent --authors-file= --repack=
2764                        --no-metadata --use-svm-props --use-svnsync-props
2765                        --log-window-size= --no-checkout --quiet
2766                        --repack-flags --use-log-author --localtime
2767                        --add-author-from
2768                        --ignore-paths= --include-paths= $remote_opts
2769                        "
2770                local init_opts="
2771                        --template= --shared= --trunk= --tags=
2772                        --branches= --stdlayout --minimize-url
2773                        --no-metadata --use-svm-props --use-svnsync-props
2774                        --rewrite-root= --prefix= $remote_opts
2775                        "
2776                local cmt_opts="
2777                        --edit --rmdir --find-copies-harder --copy-similarity=
2778                        "
2779
2780                case "$subcommand,$cur" in
2781                fetch,--*)
2782                        __gitcomp "--revision= --fetch-all $fc_opts"
2783                        ;;
2784                clone,--*)
2785                        __gitcomp "--revision= $fc_opts $init_opts"
2786                        ;;
2787                init,--*)
2788                        __gitcomp "$init_opts"
2789                        ;;
2790                dcommit,--*)
2791                        __gitcomp "
2792                                --merge --strategy= --verbose --dry-run
2793                                --fetch-all --no-rebase --commit-url
2794                                --revision --interactive $cmt_opts $fc_opts
2795                                "
2796                        ;;
2797                set-tree,--*)
2798                        __gitcomp "--stdin $cmt_opts $fc_opts"
2799                        ;;
2800                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2801                show-externals,--*|mkdirs,--*)
2802                        __gitcomp "--revision="
2803                        ;;
2804                log,--*)
2805                        __gitcomp "
2806                                --limit= --revision= --verbose --incremental
2807                                --oneline --show-commit --non-recursive
2808                                --authors-file= --color
2809                                "
2810                        ;;
2811                rebase,--*)
2812                        __gitcomp "
2813                                --merge --verbose --strategy= --local
2814                                --fetch-all --dry-run $fc_opts
2815                                "
2816                        ;;
2817                commit-diff,--*)
2818                        __gitcomp "--message= --file= --revision= $cmt_opts"
2819                        ;;
2820                info,--*)
2821                        __gitcomp "--url"
2822                        ;;
2823                branch,--*)
2824                        __gitcomp "--dry-run --message --tag"
2825                        ;;
2826                tag,--*)
2827                        __gitcomp "--dry-run --message"
2828                        ;;
2829                blame,--*)
2830                        __gitcomp "--git-format"
2831                        ;;
2832                migrate,--*)
2833                        __gitcomp "
2834                                --config-dir= --ignore-paths= --minimize
2835                                --no-auth-cache --username=
2836                                "
2837                        ;;
2838                reset,--*)
2839                        __gitcomp "--revision= --parent"
2840                        ;;
2841                *)
2842                        ;;
2843                esac
2844        fi
2845}
2846
2847_git_tag ()
2848{
2849        local i c=1 f=0
2850        while [ $c -lt $cword ]; do
2851                i="${words[c]}"
2852                case "$i" in
2853                -d|--delete|-v|--verify)
2854                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2855                        return
2856                        ;;
2857                -f)
2858                        f=1
2859                        ;;
2860                esac
2861                ((c++))
2862        done
2863
2864        case "$prev" in
2865        -m|-F)
2866                ;;
2867        -*|tag)
2868                if [ $f = 1 ]; then
2869                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
2870                fi
2871                ;;
2872        *)
2873                __git_complete_refs
2874                ;;
2875        esac
2876
2877        case "$cur" in
2878        --*)
2879                __gitcomp_builtin tag
2880                ;;
2881        esac
2882}
2883
2884_git_whatchanged ()
2885{
2886        _git_log
2887}
2888
2889_git_worktree ()
2890{
2891        local subcommands="add list lock move prune remove unlock"
2892        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2893        if [ -z "$subcommand" ]; then
2894                __gitcomp "$subcommands"
2895        else
2896                case "$subcommand,$cur" in
2897                add,--*)
2898                        __gitcomp_builtin worktree_add
2899                        ;;
2900                list,--*)
2901                        __gitcomp_builtin worktree_list
2902                        ;;
2903                lock,--*)
2904                        __gitcomp_builtin worktree_lock
2905                        ;;
2906                prune,--*)
2907                        __gitcomp_builtin worktree_prune
2908                        ;;
2909                remove,--*)
2910                        __gitcomp "--force"
2911                        ;;
2912                *)
2913                        ;;
2914                esac
2915        fi
2916}
2917
2918__git_complete_common () {
2919        local command="$1"
2920
2921        case "$cur" in
2922        --*)
2923                __gitcomp_builtin "$command"
2924                ;;
2925        esac
2926}
2927
2928__git_cmds_with_parseopt_helper=
2929__git_support_parseopt_helper () {
2930        test -n "$__git_cmds_with_parseopt_helper" ||
2931                __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
2932
2933        case " $__git_cmds_with_parseopt_helper " in
2934        *" $1 "*)
2935                return 0
2936                ;;
2937        *)
2938                return 1
2939                ;;
2940        esac
2941}
2942
2943__git_complete_command () {
2944        local command="$1"
2945        local completion_func="_git_${command//-/_}"
2946        if declare -f $completion_func >/dev/null 2>/dev/null; then
2947                $completion_func
2948                return 0
2949        elif __git_support_parseopt_helper "$command"; then
2950                __git_complete_common "$command"
2951                return 0
2952        else
2953                return 1
2954        fi
2955}
2956
2957__git_main ()
2958{
2959        local i c=1 command __git_dir __git_repo_path
2960        local __git_C_args C_args_count=0
2961
2962        while [ $c -lt $cword ]; do
2963                i="${words[c]}"
2964                case "$i" in
2965                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
2966                --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
2967                --bare)      __git_dir="." ;;
2968                --help) command="help"; break ;;
2969                -c|--work-tree|--namespace) ((c++)) ;;
2970                -C)     __git_C_args[C_args_count++]=-C
2971                        ((c++))
2972                        __git_C_args[C_args_count++]="${words[c]}"
2973                        ;;
2974                -*) ;;
2975                *) command="$i"; break ;;
2976                esac
2977                ((c++))
2978        done
2979
2980        if [ -z "$command" ]; then
2981                case "$prev" in
2982                --git-dir|-C|--work-tree)
2983                        # these need a path argument, let's fall back to
2984                        # Bash filename completion
2985                        return
2986                        ;;
2987                -c|--namespace)
2988                        # we don't support completing these options' arguments
2989                        return
2990                        ;;
2991                esac
2992                case "$cur" in
2993                --*)   __gitcomp "
2994                        --paginate
2995                        --no-pager
2996                        --git-dir=
2997                        --bare
2998                        --version
2999                        --exec-path
3000                        --exec-path=
3001                        --html-path
3002                        --man-path
3003                        --info-path
3004                        --work-tree=
3005                        --namespace=
3006                        --no-replace-objects
3007                        --help
3008                        "
3009                        ;;
3010                *)
3011                        if test -n "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3012                        then
3013                                __gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
3014                        else
3015                                __gitcomp "$(git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
3016                        fi
3017                        ;;
3018                esac
3019                return
3020        fi
3021
3022        __git_complete_command "$command" && return
3023
3024        local expansion=$(__git_aliased_command "$command")
3025        if [ -n "$expansion" ]; then
3026                words[1]=$expansion
3027                __git_complete_command "$expansion"
3028        fi
3029}
3030
3031__gitk_main ()
3032{
3033        __git_has_doubledash && return
3034
3035        local __git_repo_path
3036        __git_find_repo_path
3037
3038        local merge=""
3039        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3040                merge="--merge"
3041        fi
3042        case "$cur" in
3043        --*)
3044                __gitcomp "
3045                        $__git_log_common_options
3046                        $__git_log_gitk_options
3047                        $merge
3048                        "
3049                return
3050                ;;
3051        esac
3052        __git_complete_revlist
3053}
3054
3055if [[ -n ${ZSH_VERSION-} ]]; then
3056        echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3057
3058        autoload -U +X compinit && compinit
3059
3060        __gitcomp ()
3061        {
3062                emulate -L zsh
3063
3064                local cur_="${3-$cur}"
3065
3066                case "$cur_" in
3067                --*=)
3068                        ;;
3069                *)
3070                        local c IFS=$' \t\n'
3071                        local -a array
3072                        for c in ${=1}; do
3073                                c="$c${4-}"
3074                                case $c in
3075                                --*=*|*.) ;;
3076                                *) c="$c " ;;
3077                                esac
3078                                array[${#array[@]}+1]="$c"
3079                        done
3080                        compset -P '*[=:]'
3081                        compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3082                        ;;
3083                esac
3084        }
3085
3086        __gitcomp_direct ()
3087        {
3088                emulate -L zsh
3089
3090                local IFS=$'\n'
3091                compset -P '*[=:]'
3092                compadd -Q -- ${=1} && _ret=0
3093        }
3094
3095        __gitcomp_nl ()
3096        {
3097                emulate -L zsh
3098
3099                local IFS=$'\n'
3100                compset -P '*[=:]'
3101                compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3102        }
3103
3104        __gitcomp_file ()
3105        {
3106                emulate -L zsh
3107
3108                local IFS=$'\n'
3109                compset -P '*[=:]'
3110                compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3111        }
3112
3113        _git ()
3114        {
3115                local _ret=1 cur cword prev
3116                cur=${words[CURRENT]}
3117                prev=${words[CURRENT-1]}
3118                let cword=CURRENT-1
3119                emulate ksh -c __${service}_main
3120                let _ret && _default && _ret=0
3121                return _ret
3122        }
3123
3124        compdef _git git gitk
3125        return
3126fi
3127
3128__git_func_wrap ()
3129{
3130        local cur words cword prev
3131        _get_comp_words_by_ref -n =: cur words cword prev
3132        $1
3133}
3134
3135# Setup completion for certain functions defined above by setting common
3136# variables and workarounds.
3137# This is NOT a public function; use at your own risk.
3138__git_complete ()
3139{
3140        local wrapper="__git_wrap${2}"
3141        eval "$wrapper () { __git_func_wrap $2 ; }"
3142        complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3143                || complete -o default -o nospace -F $wrapper $1
3144}
3145
3146# wrapper for backwards compatibility
3147_git ()
3148{
3149        __git_wrap__git_main
3150}
3151
3152# wrapper for backwards compatibility
3153_gitk ()
3154{
3155        __git_wrap__gitk_main
3156}
3157
3158__git_complete git __git_main
3159__git_complete gitk __gitk_main
3160
3161# The following are necessary only for Cygwin, and only are needed
3162# when the user has tab-completed the executable name and consequently
3163# included the '.exe' suffix.
3164#
3165if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3166__git_complete git.exe __git_main
3167fi