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