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