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