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