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