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