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