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