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