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