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