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