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