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