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