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