contrib / completion / git-completion.bashon commit Replace Free Software Foundation address in license notices (4842579)
   1# bash/zsh completion support for core Git.
   2#
   3# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
   4# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
   5# Distributed under the GNU General Public License, version 2.0.
   6#
   7# The contained completion routines provide support for completing:
   8#
   9#    *) local and remote branch names
  10#    *) local and remote tag names
  11#    *) .git/remotes file names
  12#    *) git 'subcommands'
  13#    *) git email aliases for git-send-email
  14#    *) tree paths within 'ref:path/to/file' expressions
  15#    *) file paths within current working directory and index
  16#    *) common --long-options
  17#
  18# To use these routines:
  19#
  20#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
  21#    2) Add the following line to your .bashrc/.zshrc:
  22#        source ~/.git-completion.bash
  23#    3) Consider changing your PS1 to also show the current branch,
  24#       see git-prompt.sh for details.
  25#
  26# If you use complex aliases of form '!f() { ... }; f', you can use the null
  27# command ':' as the first command in the function body to declare the desired
  28# completion style.  For example '!f() { : git commit ; ... }; f' will
  29# tell the completion to use commit completion.  This also works with aliases
  30# of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
  31#
  32# You can set the following environment variables to influence the behavior of
  33# the completion routines:
  34#
  35#   GIT_COMPLETION_CHECKOUT_NO_GUESS
  36#
  37#     When set to "1", do not include "DWIM" suggestions in git-checkout
  38#     completion (e.g., completing "foo" when "origin/foo" exists).
  39
  40case "$COMP_WORDBREAKS" in
  41*:*) : great ;;
  42*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
  43esac
  44
  45# Discovers the path to the git repository taking any '--git-dir=<path>' and
  46# '-C <path>' options into account and stores it in the $__git_repo_path
  47# variable.
  48__git_find_repo_path ()
  49{
  50        if [ -n "$__git_repo_path" ]; then
  51                # we already know where it is
  52                return
  53        fi
  54
  55        if [ -n "${__git_C_args-}" ]; then
  56                __git_repo_path="$(git "${__git_C_args[@]}" \
  57                        ${__git_dir:+--git-dir="$__git_dir"} \
  58                        rev-parse --absolute-git-dir 2>/dev/null)"
  59        elif [ -n "${__git_dir-}" ]; then
  60                test -d "$__git_dir" &&
  61                __git_repo_path="$__git_dir"
  62        elif [ -n "${GIT_DIR-}" ]; then
  63                test -d "${GIT_DIR-}" &&
  64                __git_repo_path="$GIT_DIR"
  65        elif [ -d .git ]; then
  66                __git_repo_path=.git
  67        else
  68                __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
  69        fi
  70}
  71
  72# Deprecated: use __git_find_repo_path() and $__git_repo_path instead
  73# __gitdir accepts 0 or 1 arguments (i.e., location)
  74# returns location of .git repo
  75__gitdir ()
  76{
  77        if [ -z "${1-}" ]; then
  78                __git_find_repo_path || return 1
  79                echo "$__git_repo_path"
  80        elif [ -d "$1/.git" ]; then
  81                echo "$1/.git"
  82        else
  83                echo "$1"
  84        fi
  85}
  86
  87# Runs git with all the options given as argument, respecting any
  88# '--git-dir=<path>' and '-C <path>' options present on the command line
  89__git ()
  90{
  91        git ${__git_C_args:+"${__git_C_args[@]}"} \
  92                ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
  93}
  94
  95# The following function is based on code from:
  96#
  97#   bash_completion - programmable completion functions for bash 3.2+
  98#
  99#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
 100#             © 2009-2010, Bash Completion Maintainers
 101#                     <bash-completion-devel@lists.alioth.debian.org>
 102#
 103#   This program is free software; you can redistribute it and/or modify
 104#   it under the terms of the GNU General Public License as published by
 105#   the Free Software Foundation; either version 2, or (at your option)
 106#   any later version.
 107#
 108#   This program is distributed in the hope that it will be useful,
 109#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 110#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 111#   GNU General Public License for more details.
 112#
 113#   You should have received a copy of the GNU General Public License
 114#   along with this program; if not, see <http://www.gnu.org/licenses/>.
 115#
 116#   The latest version of this software can be obtained here:
 117#
 118#   http://bash-completion.alioth.debian.org/
 119#
 120#   RELEASE: 2.x
 121
 122# This function can be used to access a tokenized list of words
 123# on the command line:
 124#
 125#       __git_reassemble_comp_words_by_ref '=:'
 126#       if test "${words_[cword_-1]}" = -w
 127#       then
 128#               ...
 129#       fi
 130#
 131# The argument should be a collection of characters from the list of
 132# word completion separators (COMP_WORDBREAKS) to treat as ordinary
 133# characters.
 134#
 135# This is roughly equivalent to going back in time and setting
 136# COMP_WORDBREAKS to exclude those characters.  The intent is to
 137# make option types like --date=<type> and <rev>:<path> easy to
 138# recognize by treating each shell word as a single token.
 139#
 140# It is best not to set COMP_WORDBREAKS directly because the value is
 141# shared with other completion scripts.  By the time the completion
 142# function gets called, COMP_WORDS has already been populated so local
 143# changes to COMP_WORDBREAKS have no effect.
 144#
 145# Output: words_, cword_, cur_.
 146
 147__git_reassemble_comp_words_by_ref()
 148{
 149        local exclude i j first
 150        # Which word separators to exclude?
 151        exclude="${1//[^$COMP_WORDBREAKS]}"
 152        cword_=$COMP_CWORD
 153        if [ -z "$exclude" ]; then
 154                words_=("${COMP_WORDS[@]}")
 155                return
 156        fi
 157        # List of word completion separators has shrunk;
 158        # re-assemble words to complete.
 159        for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
 160                # Append each nonempty word consisting of just
 161                # word separator characters to the current word.
 162                first=t
 163                while
 164                        [ $i -gt 0 ] &&
 165                        [ -n "${COMP_WORDS[$i]}" ] &&
 166                        # word consists of excluded word separators
 167                        [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
 168                do
 169                        # Attach to the previous token,
 170                        # unless the previous token is the command name.
 171                        if [ $j -ge 2 ] && [ -n "$first" ]; then
 172                                ((j--))
 173                        fi
 174                        first=
 175                        words_[$j]=${words_[j]}${COMP_WORDS[i]}
 176                        if [ $i = $COMP_CWORD ]; then
 177                                cword_=$j
 178                        fi
 179                        if (($i < ${#COMP_WORDS[@]} - 1)); then
 180                                ((i++))
 181                        else
 182                                # Done.
 183                                return
 184                        fi
 185                done
 186                words_[$j]=${words_[j]}${COMP_WORDS[i]}
 187                if [ $i = $COMP_CWORD ]; then
 188                        cword_=$j
 189                fi
 190        done
 191}
 192
 193if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
 194_get_comp_words_by_ref ()
 195{
 196        local exclude cur_ words_ cword_
 197        if [ "$1" = "-n" ]; then
 198                exclude=$2
 199                shift 2
 200        fi
 201        __git_reassemble_comp_words_by_ref "$exclude"
 202        cur_=${words_[cword_]}
 203        while [ $# -gt 0 ]; do
 204                case "$1" in
 205                cur)
 206                        cur=$cur_
 207                        ;;
 208                prev)
 209                        prev=${words_[$cword_-1]}
 210                        ;;
 211                words)
 212                        words=("${words_[@]}")
 213                        ;;
 214                cword)
 215                        cword=$cword_
 216                        ;;
 217                esac
 218                shift
 219        done
 220}
 221fi
 222
 223# Fills the COMPREPLY array with prefiltered words without any additional
 224# processing.
 225# Callers must take care of providing only words that match the current word
 226# to be completed and adding any prefix and/or suffix (trailing space!), if
 227# necessary.
 228# 1: List of newline-separated matching completion words, complete with
 229#    prefix and suffix.
 230__gitcomp_direct ()
 231{
 232        local IFS=$'\n'
 233
 234        COMPREPLY=($1)
 235}
 236
 237__gitcompappend ()
 238{
 239        local x i=${#COMPREPLY[@]}
 240        for x in $1; do
 241                if [[ "$x" == "$3"* ]]; then
 242                        COMPREPLY[i++]="$2$x$4"
 243                fi
 244        done
 245}
 246
 247__gitcompadd ()
 248{
 249        COMPREPLY=()
 250        __gitcompappend "$@"
 251}
 252
 253# Generates completion reply, appending a space to possible completion words,
 254# if necessary.
 255# It accepts 1 to 4 arguments:
 256# 1: List of possible completion words.
 257# 2: A prefix to be added to each possible completion word (optional).
 258# 3: Generate possible completion matches for this word (optional).
 259# 4: A suffix to be appended to each possible completion word (optional).
 260__gitcomp ()
 261{
 262        local cur_="${3-$cur}"
 263
 264        case "$cur_" in
 265        --*=)
 266                ;;
 267        *)
 268                local c i=0 IFS=$' \t\n'
 269                for c in $1; do
 270                        c="$c${4-}"
 271                        if [[ $c == "$cur_"* ]]; then
 272                                case $c in
 273                                --*=*|*.) ;;
 274                                *) c="$c " ;;
 275                                esac
 276                                COMPREPLY[i++]="${2-}$c"
 277                        fi
 278                done
 279                ;;
 280        esac
 281}
 282
 283# Variation of __gitcomp_nl () that appends to the existing list of
 284# completion candidates, COMPREPLY.
 285__gitcomp_nl_append ()
 286{
 287        local IFS=$'\n'
 288        __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
 289}
 290
 291# Generates completion reply from newline-separated possible completion words
 292# by appending a space to all of them.
 293# It accepts 1 to 4 arguments:
 294# 1: List of possible completion words, separated by a single newline.
 295# 2: A prefix to be added to each possible completion word (optional).
 296# 3: Generate possible completion matches for this word (optional).
 297# 4: A suffix to be appended to each possible completion word instead of
 298#    the default space (optional).  If specified but empty, nothing is
 299#    appended.
 300__gitcomp_nl ()
 301{
 302        COMPREPLY=()
 303        __gitcomp_nl_append "$@"
 304}
 305
 306# Generates completion reply with compgen from newline-separated possible
 307# completion filenames.
 308# It accepts 1 to 3 arguments:
 309# 1: List of possible completion filenames, separated by a single newline.
 310# 2: A directory prefix to be added to each possible completion filename
 311#    (optional).
 312# 3: Generate possible completion matches for this word (optional).
 313__gitcomp_file ()
 314{
 315        local IFS=$'\n'
 316
 317        # XXX does not work when the directory prefix contains a tilde,
 318        # since tilde expansion is not applied.
 319        # This means that COMPREPLY will be empty and Bash default
 320        # completion will be used.
 321        __gitcompadd "$1" "${2-}" "${3-$cur}" ""
 322
 323        # use a hack to enable file mode in bash < 4
 324        compopt -o filenames +o nospace 2>/dev/null ||
 325        compgen -f /non-existing-dir/ > /dev/null
 326}
 327
 328# Execute 'git ls-files', unless the --committable option is specified, in
 329# which case it runs 'git diff-index' to find out the files that can be
 330# committed.  It return paths relative to the directory specified in the first
 331# argument, and using the options specified in the second argument.
 332__git_ls_files_helper ()
 333{
 334        if [ "$2" == "--committable" ]; then
 335                __git -C "$1" diff-index --name-only --relative HEAD
 336        else
 337                # NOTE: $2 is not quoted in order to support multiple options
 338                __git -C "$1" ls-files --exclude-standard $2
 339        fi
 340}
 341
 342
 343# __git_index_files accepts 1 or 2 arguments:
 344# 1: Options to pass to ls-files (required).
 345# 2: A directory path (optional).
 346#    If provided, only files within the specified directory are listed.
 347#    Sub directories are never recursed.  Path must have a trailing
 348#    slash.
 349__git_index_files ()
 350{
 351        local root="${2-.}" file
 352
 353        __git_ls_files_helper "$root" "$1" |
 354        while read -r file; do
 355                case "$file" in
 356                ?*/*) echo "${file%%/*}" ;;
 357                *) echo "$file" ;;
 358                esac
 359        done | sort | uniq
 360}
 361
 362# Lists branches from the local repository.
 363# 1: A prefix to be added to each listed branch (optional).
 364# 2: List only branches matching this word (optional; list all branches if
 365#    unset or empty).
 366# 3: A suffix to be appended to each listed branch (optional).
 367__git_heads ()
 368{
 369        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 370
 371        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 372                        "refs/heads/$cur_*" "refs/heads/$cur_*/**"
 373}
 374
 375# Lists tags from the local repository.
 376# Accepts the same positional parameters as __git_heads() above.
 377__git_tags ()
 378{
 379        local pfx="${1-}" cur_="${2-}" sfx="${3-}"
 380
 381        __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
 382                        "refs/tags/$cur_*" "refs/tags/$cur_*/**"
 383}
 384
 385# Lists refs from the local (by default) or from a remote repository.
 386# It accepts 0, 1 or 2 arguments:
 387# 1: The remote to list refs from (optional; ignored, if set but empty).
 388#    Can be the name of a configured remote, a path, or a URL.
 389# 2: In addition to local refs, list unique branches from refs/remotes/ for
 390#    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
 391# 3: A prefix to be added to each listed ref (optional).
 392# 4: List only refs matching this word (optional; list all refs if unset or
 393#    empty).
 394# 5: A suffix to be appended to each listed ref (optional; ignored, if set
 395#    but empty).
 396#
 397# Use __git_complete_refs() instead.
 398__git_refs ()
 399{
 400        local i hash dir track="${2-}"
 401        local list_refs_from=path remote="${1-}"
 402        local format refs
 403        local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
 404        local match="${4-}"
 405        local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
 406
 407        __git_find_repo_path
 408        dir="$__git_repo_path"
 409
 410        if [ -z "$remote" ]; then
 411                if [ -z "$dir" ]; then
 412                        return
 413                fi
 414        else
 415                if __git_is_configured_remote "$remote"; then
 416                        # configured remote takes precedence over a
 417                        # local directory with the same name
 418                        list_refs_from=remote
 419                elif [ -d "$remote/.git" ]; then
 420                        dir="$remote/.git"
 421                elif [ -d "$remote" ]; then
 422                        dir="$remote"
 423                else
 424                        list_refs_from=url
 425                fi
 426        fi
 427
 428        if [ "$list_refs_from" = path ]; then
 429                if [[ "$cur_" == ^* ]]; then
 430                        pfx="$pfx^"
 431                        fer_pfx="$fer_pfx^"
 432                        cur_=${cur_#^}
 433                        match=${match#^}
 434                fi
 435                case "$cur_" in
 436                refs|refs/*)
 437                        format="refname"
 438                        refs=("$match*" "$match*/**")
 439                        track=""
 440                        ;;
 441                *)
 442                        for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
 443                                case "$i" in
 444                                $match*)
 445                                        if [ -e "$dir/$i" ]; then
 446                                                echo "$pfx$i$sfx"
 447                                        fi
 448                                        ;;
 449                                esac
 450                        done
 451                        format="refname:strip=2"
 452                        refs=("refs/tags/$match*" "refs/tags/$match*/**"
 453                                "refs/heads/$match*" "refs/heads/$match*/**"
 454                                "refs/remotes/$match*" "refs/remotes/$match*/**")
 455                        ;;
 456                esac
 457                __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
 458                        "${refs[@]}"
 459                if [ -n "$track" ]; then
 460                        # employ the heuristic used by git checkout
 461                        # Try to find a remote branch that matches the completion word
 462                        # but only output if the branch name is unique
 463                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 464                                --sort="refname:strip=3" \
 465                                "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
 466                        uniq -u
 467                fi
 468                return
 469        fi
 470        case "$cur_" in
 471        refs|refs/*)
 472                __git ls-remote "$remote" "$match*" | \
 473                while read -r hash i; do
 474                        case "$i" in
 475                        *^{}) ;;
 476                        *) echo "$pfx$i$sfx" ;;
 477                        esac
 478                done
 479                ;;
 480        *)
 481                if [ "$list_refs_from" = remote ]; then
 482                        case "HEAD" in
 483                        $match*)        echo "${pfx}HEAD$sfx" ;;
 484                        esac
 485                        __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
 486                                "refs/remotes/$remote/$match*" \
 487                                "refs/remotes/$remote/$match*/**"
 488                else
 489                        local query_symref
 490                        case "HEAD" in
 491                        $match*)        query_symref="HEAD" ;;
 492                        esac
 493                        __git ls-remote "$remote" $query_symref \
 494                                "refs/tags/$match*" "refs/heads/$match*" \
 495                                "refs/remotes/$match*" |
 496                        while read -r hash i; do
 497                                case "$i" in
 498                                *^{})   ;;
 499                                refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
 500                                *)      echo "$pfx$i$sfx" ;;  # symbolic refs
 501                                esac
 502                        done
 503                fi
 504                ;;
 505        esac
 506}
 507
 508# Completes refs, short and long, local and remote, symbolic and pseudo.
 509#
 510# Usage: __git_complete_refs [<option>]...
 511# --remote=<remote>: The remote to list refs from, can be the name of a
 512#                    configured remote, a path, or a URL.
 513# --track: List unique remote branches for 'git checkout's tracking DWIMery.
 514# --pfx=<prefix>: A prefix to be added to each ref.
 515# --cur=<word>: The current ref to be completed.  Defaults to the current
 516#               word to be completed.
 517# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
 518#                 space.
 519__git_complete_refs ()
 520{
 521        local remote track pfx cur_="$cur" sfx=" "
 522
 523        while test $# != 0; do
 524                case "$1" in
 525                --remote=*)     remote="${1##--remote=}" ;;
 526                --track)        track="yes" ;;
 527                --pfx=*)        pfx="${1##--pfx=}" ;;
 528                --cur=*)        cur_="${1##--cur=}" ;;
 529                --sfx=*)        sfx="${1##--sfx=}" ;;
 530                *)              return 1 ;;
 531                esac
 532                shift
 533        done
 534
 535        __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
 536}
 537
 538# __git_refs2 requires 1 argument (to pass to __git_refs)
 539# Deprecated: use __git_complete_fetch_refspecs() instead.
 540__git_refs2 ()
 541{
 542        local i
 543        for i in $(__git_refs "$1"); do
 544                echo "$i:$i"
 545        done
 546}
 547
 548# Completes refspecs for fetching from a remote repository.
 549# 1: The remote repository.
 550# 2: A prefix to be added to each listed refspec (optional).
 551# 3: The ref to be completed as a refspec instead of the current word to be
 552#    completed (optional)
 553# 4: A suffix to be appended to each listed refspec instead of the default
 554#    space (optional).
 555__git_complete_fetch_refspecs ()
 556{
 557        local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
 558
 559        __gitcomp_direct "$(
 560                for i in $(__git_refs "$remote" "" "" "$cur_") ; do
 561                        echo "$pfx$i:$i$sfx"
 562                done
 563                )"
 564}
 565
 566# __git_refs_remotes requires 1 argument (to pass to ls-remote)
 567__git_refs_remotes ()
 568{
 569        local i hash
 570        __git ls-remote "$1" 'refs/heads/*' | \
 571        while read -r hash i; do
 572                echo "$i:refs/remotes/$1/${i#refs/heads/}"
 573        done
 574}
 575
 576__git_remotes ()
 577{
 578        __git_find_repo_path
 579        test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
 580        __git remote
 581}
 582
 583# Returns true if $1 matches the name of a configured remote, false otherwise.
 584__git_is_configured_remote ()
 585{
 586        local remote
 587        for remote in $(__git_remotes); do
 588                if [ "$remote" = "$1" ]; then
 589                        return 0
 590                fi
 591        done
 592        return 1
 593}
 594
 595__git_list_merge_strategies ()
 596{
 597        git merge -s help 2>&1 |
 598        sed -n -e '/[Aa]vailable strategies are: /,/^$/{
 599                s/\.$//
 600                s/.*://
 601                s/^[    ]*//
 602                s/[     ]*$//
 603                p
 604        }'
 605}
 606
 607__git_merge_strategies=
 608# 'git merge -s help' (and thus detection of the merge strategy
 609# list) fails, unfortunately, if run outside of any git working
 610# tree.  __git_merge_strategies is set to the empty string in
 611# that case, and the detection will be repeated the next time it
 612# is needed.
 613__git_compute_merge_strategies ()
 614{
 615        test -n "$__git_merge_strategies" ||
 616        __git_merge_strategies=$(__git_list_merge_strategies)
 617}
 618
 619__git_complete_revlist_file ()
 620{
 621        local pfx ls ref cur_="$cur"
 622        case "$cur_" in
 623        *..?*:*)
 624                return
 625                ;;
 626        ?*:*)
 627                ref="${cur_%%:*}"
 628                cur_="${cur_#*:}"
 629                case "$cur_" in
 630                ?*/*)
 631                        pfx="${cur_%/*}"
 632                        cur_="${cur_##*/}"
 633                        ls="$ref:$pfx"
 634                        pfx="$pfx/"
 635                        ;;
 636                *)
 637                        ls="$ref"
 638                        ;;
 639                esac
 640
 641                case "$COMP_WORDBREAKS" in
 642                *:*) : great ;;
 643                *)   pfx="$ref:$pfx" ;;
 644                esac
 645
 646                __gitcomp_nl "$(__git ls-tree "$ls" \
 647                                | sed '/^100... blob /{
 648                                           s,^.*        ,,
 649                                           s,$, ,
 650                                       }
 651                                       /^120000 blob /{
 652                                           s,^.*        ,,
 653                                           s,$, ,
 654                                       }
 655                                       /^040000 tree /{
 656                                           s,^.*        ,,
 657                                           s,$,/,
 658                                       }
 659                                       s/^.*    //')" \
 660                        "$pfx" "$cur_" ""
 661                ;;
 662        *...*)
 663                pfx="${cur_%...*}..."
 664                cur_="${cur_#*...}"
 665                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 666                ;;
 667        *..*)
 668                pfx="${cur_%..*}.."
 669                cur_="${cur_#*..}"
 670                __git_complete_refs --pfx="$pfx" --cur="$cur_"
 671                ;;
 672        *)
 673                __git_complete_refs
 674                ;;
 675        esac
 676}
 677
 678
 679# __git_complete_index_file requires 1 argument:
 680# 1: the options to pass to ls-file
 681#
 682# The exception is --committable, which finds the files appropriate commit.
 683__git_complete_index_file ()
 684{
 685        local pfx="" cur_="$cur"
 686
 687        case "$cur_" in
 688        ?*/*)
 689                pfx="${cur_%/*}"
 690                cur_="${cur_##*/}"
 691                pfx="${pfx}/"
 692                ;;
 693        esac
 694
 695        __gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
 696}
 697
 698__git_complete_file ()
 699{
 700        __git_complete_revlist_file
 701}
 702
 703__git_complete_revlist ()
 704{
 705        __git_complete_revlist_file
 706}
 707
 708__git_complete_remote_or_refspec ()
 709{
 710        local cur_="$cur" cmd="${words[1]}"
 711        local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
 712        if [ "$cmd" = "remote" ]; then
 713                ((c++))
 714        fi
 715        while [ $c -lt $cword ]; do
 716                i="${words[c]}"
 717                case "$i" in
 718                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
 719                -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
 720                --all)
 721                        case "$cmd" in
 722                        push) no_complete_refspec=1 ;;
 723                        fetch)
 724                                return
 725                                ;;
 726                        *) ;;
 727                        esac
 728                        ;;
 729                -*) ;;
 730                *) remote="$i"; break ;;
 731                esac
 732                ((c++))
 733        done
 734        if [ -z "$remote" ]; then
 735                __gitcomp_nl "$(__git_remotes)"
 736                return
 737        fi
 738        if [ $no_complete_refspec = 1 ]; then
 739                return
 740        fi
 741        [ "$remote" = "." ] && remote=
 742        case "$cur_" in
 743        *:*)
 744                case "$COMP_WORDBREAKS" in
 745                *:*) : great ;;
 746                *)   pfx="${cur_%%:*}:" ;;
 747                esac
 748                cur_="${cur_#*:}"
 749                lhs=0
 750                ;;
 751        +*)
 752                pfx="+"
 753                cur_="${cur_#+}"
 754                ;;
 755        esac
 756        case "$cmd" in
 757        fetch)
 758                if [ $lhs = 1 ]; then
 759                        __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
 760                else
 761                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 762                fi
 763                ;;
 764        pull|remote)
 765                if [ $lhs = 1 ]; then
 766                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 767                else
 768                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 769                fi
 770                ;;
 771        push)
 772                if [ $lhs = 1 ]; then
 773                        __git_complete_refs --pfx="$pfx" --cur="$cur_"
 774                else
 775                        __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
 776                fi
 777                ;;
 778        esac
 779}
 780
 781__git_complete_strategy ()
 782{
 783        __git_compute_merge_strategies
 784        case "$prev" in
 785        -s|--strategy)
 786                __gitcomp "$__git_merge_strategies"
 787                return 0
 788        esac
 789        case "$cur" in
 790        --strategy=*)
 791                __gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
 792                return 0
 793                ;;
 794        esac
 795        return 1
 796}
 797
 798__git_commands () {
 799        if test -n "${GIT_TESTING_COMMAND_COMPLETION:-}"
 800        then
 801                printf "%s" "${GIT_TESTING_COMMAND_COMPLETION}"
 802        else
 803                git help -a|egrep '^  [a-zA-Z0-9]'
 804        fi
 805}
 806
 807__git_list_all_commands ()
 808{
 809        local i IFS=" "$'\n'
 810        for i in $(__git_commands)
 811        do
 812                case $i in
 813                *--*)             : helper pattern;;
 814                *) echo $i;;
 815                esac
 816        done
 817}
 818
 819__git_all_commands=
 820__git_compute_all_commands ()
 821{
 822        test -n "$__git_all_commands" ||
 823        __git_all_commands=$(__git_list_all_commands)
 824}
 825
 826__git_list_porcelain_commands ()
 827{
 828        local i IFS=" "$'\n'
 829        __git_compute_all_commands
 830        for i in $__git_all_commands
 831        do
 832                case $i in
 833                *--*)             : helper pattern;;
 834                applymbox)        : ask gittus;;
 835                applypatch)       : ask gittus;;
 836                archimport)       : import;;
 837                cat-file)         : plumbing;;
 838                check-attr)       : plumbing;;
 839                check-ignore)     : plumbing;;
 840                check-mailmap)    : plumbing;;
 841                check-ref-format) : plumbing;;
 842                checkout-index)   : plumbing;;
 843                column)           : internal helper;;
 844                commit-tree)      : plumbing;;
 845                count-objects)    : infrequent;;
 846                credential)       : credentials;;
 847                credential-*)     : credentials helper;;
 848                cvsexportcommit)  : export;;
 849                cvsimport)        : import;;
 850                cvsserver)        : daemon;;
 851                daemon)           : daemon;;
 852                diff-files)       : plumbing;;
 853                diff-index)       : plumbing;;
 854                diff-tree)        : plumbing;;
 855                fast-import)      : import;;
 856                fast-export)      : export;;
 857                fsck-objects)     : plumbing;;
 858                fetch-pack)       : plumbing;;
 859                fmt-merge-msg)    : plumbing;;
 860                for-each-ref)     : plumbing;;
 861                hash-object)      : plumbing;;
 862                http-*)           : transport;;
 863                index-pack)       : plumbing;;
 864                init-db)          : deprecated;;
 865                local-fetch)      : plumbing;;
 866                ls-files)         : plumbing;;
 867                ls-remote)        : plumbing;;
 868                ls-tree)          : plumbing;;
 869                mailinfo)         : plumbing;;
 870                mailsplit)        : plumbing;;
 871                merge-*)          : plumbing;;
 872                mktree)           : plumbing;;
 873                mktag)            : plumbing;;
 874                pack-objects)     : plumbing;;
 875                pack-redundant)   : plumbing;;
 876                pack-refs)        : plumbing;;
 877                parse-remote)     : plumbing;;
 878                patch-id)         : plumbing;;
 879                prune)            : plumbing;;
 880                prune-packed)     : plumbing;;
 881                quiltimport)      : import;;
 882                read-tree)        : plumbing;;
 883                receive-pack)     : plumbing;;
 884                remote-*)         : transport;;
 885                rerere)           : plumbing;;
 886                rev-list)         : plumbing;;
 887                rev-parse)        : plumbing;;
 888                runstatus)        : plumbing;;
 889                sh-setup)         : internal;;
 890                shell)            : daemon;;
 891                show-ref)         : plumbing;;
 892                send-pack)        : plumbing;;
 893                show-index)       : plumbing;;
 894                ssh-*)            : transport;;
 895                stripspace)       : plumbing;;
 896                symbolic-ref)     : plumbing;;
 897                unpack-file)      : plumbing;;
 898                unpack-objects)   : plumbing;;
 899                update-index)     : plumbing;;
 900                update-ref)       : plumbing;;
 901                update-server-info) : daemon;;
 902                upload-archive)   : plumbing;;
 903                upload-pack)      : plumbing;;
 904                write-tree)       : plumbing;;
 905                var)              : infrequent;;
 906                verify-pack)      : infrequent;;
 907                verify-tag)       : plumbing;;
 908                *) echo $i;;
 909                esac
 910        done
 911}
 912
 913__git_porcelain_commands=
 914__git_compute_porcelain_commands ()
 915{
 916        test -n "$__git_porcelain_commands" ||
 917        __git_porcelain_commands=$(__git_list_porcelain_commands)
 918}
 919
 920# Lists all set config variables starting with the given section prefix,
 921# with the prefix removed.
 922__git_get_config_variables ()
 923{
 924        local section="$1" i IFS=$'\n'
 925        for i in $(__git config --name-only --get-regexp "^$section\..*"); do
 926                echo "${i#$section.}"
 927        done
 928}
 929
 930__git_pretty_aliases ()
 931{
 932        __git_get_config_variables "pretty"
 933}
 934
 935__git_aliases ()
 936{
 937        __git_get_config_variables "alias"
 938}
 939
 940# __git_aliased_command requires 1 argument
 941__git_aliased_command ()
 942{
 943        local word cmdline=$(__git config --get "alias.$1")
 944        for word in $cmdline; do
 945                case "$word" in
 946                \!gitk|gitk)
 947                        echo "gitk"
 948                        return
 949                        ;;
 950                \!*)    : shell command alias ;;
 951                -*)     : option ;;
 952                *=*)    : setting env ;;
 953                git)    : git itself ;;
 954                \(\))   : skip parens of shell function definition ;;
 955                {)      : skip start of shell helper function ;;
 956                :)      : skip null command ;;
 957                \'*)    : skip opening quote after sh -c ;;
 958                *)
 959                        echo "$word"
 960                        return
 961                esac
 962        done
 963}
 964
 965# __git_find_on_cmdline requires 1 argument
 966__git_find_on_cmdline ()
 967{
 968        local word subcommand c=1
 969        while [ $c -lt $cword ]; do
 970                word="${words[c]}"
 971                for subcommand in $1; do
 972                        if [ "$subcommand" = "$word" ]; then
 973                                echo "$subcommand"
 974                                return
 975                        fi
 976                done
 977                ((c++))
 978        done
 979}
 980
 981# Echo the value of an option set on the command line or config
 982#
 983# $1: short option name
 984# $2: long option name including =
 985# $3: list of possible values
 986# $4: config string (optional)
 987#
 988# example:
 989# result="$(__git_get_option_value "-d" "--do-something=" \
 990#     "yes no" "core.doSomething")"
 991#
 992# result is then either empty (no option set) or "yes" or "no"
 993#
 994# __git_get_option_value requires 3 arguments
 995__git_get_option_value ()
 996{
 997        local c short_opt long_opt val
 998        local result= values config_key word
 999
1000        short_opt="$1"
1001        long_opt="$2"
1002        values="$3"
1003        config_key="$4"
1004
1005        ((c = $cword - 1))
1006        while [ $c -ge 0 ]; do
1007                word="${words[c]}"
1008                for val in $values; do
1009                        if [ "$short_opt$val" = "$word" ] ||
1010                           [ "$long_opt$val"  = "$word" ]; then
1011                                result="$val"
1012                                break 2
1013                        fi
1014                done
1015                ((c--))
1016        done
1017
1018        if [ -n "$config_key" ] && [ -z "$result" ]; then
1019                result="$(__git config "$config_key")"
1020        fi
1021
1022        echo "$result"
1023}
1024
1025__git_has_doubledash ()
1026{
1027        local c=1
1028        while [ $c -lt $cword ]; do
1029                if [ "--" = "${words[c]}" ]; then
1030                        return 0
1031                fi
1032                ((c++))
1033        done
1034        return 1
1035}
1036
1037# Try to count non option arguments passed on the command line for the
1038# specified git command.
1039# When options are used, it is necessary to use the special -- option to
1040# tell the implementation were non option arguments begin.
1041# XXX this can not be improved, since options can appear everywhere, as
1042# an example:
1043#       git mv x -n y
1044#
1045# __git_count_arguments requires 1 argument: the git command executed.
1046__git_count_arguments ()
1047{
1048        local word i c=0
1049
1050        # Skip "git" (first argument)
1051        for ((i=1; i < ${#words[@]}; i++)); do
1052                word="${words[i]}"
1053
1054                case "$word" in
1055                        --)
1056                                # Good; we can assume that the following are only non
1057                                # option arguments.
1058                                ((c = 0))
1059                                ;;
1060                        "$1")
1061                                # Skip the specified git command and discard git
1062                                # main options
1063                                ((c = 0))
1064                                ;;
1065                        ?*)
1066                                ((c++))
1067                                ;;
1068                esac
1069        done
1070
1071        printf "%d" $c
1072}
1073
1074__git_whitespacelist="nowarn warn error error-all fix"
1075
1076_git_am ()
1077{
1078        __git_find_repo_path
1079        if [ -d "$__git_repo_path"/rebase-apply ]; then
1080                __gitcomp "--skip --continue --resolved --abort"
1081                return
1082        fi
1083        case "$cur" in
1084        --whitespace=*)
1085                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1086                return
1087                ;;
1088        --*)
1089                __gitcomp "
1090                        --3way --committer-date-is-author-date --ignore-date
1091                        --ignore-whitespace --ignore-space-change
1092                        --interactive --keep --no-utf8 --signoff --utf8
1093                        --whitespace= --scissors
1094                        "
1095                return
1096        esac
1097}
1098
1099_git_apply ()
1100{
1101        case "$cur" in
1102        --whitespace=*)
1103                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1104                return
1105                ;;
1106        --*)
1107                __gitcomp "
1108                        --stat --numstat --summary --check --index
1109                        --cached --index-info --reverse --reject --unidiff-zero
1110                        --apply --no-add --exclude=
1111                        --ignore-whitespace --ignore-space-change
1112                        --whitespace= --inaccurate-eof --verbose
1113                        --recount --directory=
1114                        "
1115                return
1116        esac
1117}
1118
1119_git_add ()
1120{
1121        case "$cur" in
1122        --*)
1123                __gitcomp "
1124                        --interactive --refresh --patch --update --dry-run
1125                        --ignore-errors --intent-to-add --force --edit --chmod=
1126                        "
1127                return
1128        esac
1129
1130        local complete_opt="--others --modified --directory --no-empty-directory"
1131        if test -n "$(__git_find_on_cmdline "-u --update")"
1132        then
1133                complete_opt="--modified"
1134        fi
1135        __git_complete_index_file "$complete_opt"
1136}
1137
1138_git_archive ()
1139{
1140        case "$cur" in
1141        --format=*)
1142                __gitcomp "$(git archive --list)" "" "${cur##--format=}"
1143                return
1144                ;;
1145        --remote=*)
1146                __gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1147                return
1148                ;;
1149        --*)
1150                __gitcomp "
1151                        --format= --list --verbose
1152                        --prefix= --remote= --exec= --output
1153                        "
1154                return
1155                ;;
1156        esac
1157        __git_complete_file
1158}
1159
1160_git_bisect ()
1161{
1162        __git_has_doubledash && return
1163
1164        local subcommands="start bad good skip reset visualize replay log run"
1165        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1166        if [ -z "$subcommand" ]; then
1167                __git_find_repo_path
1168                if [ -f "$__git_repo_path"/BISECT_START ]; then
1169                        __gitcomp "$subcommands"
1170                else
1171                        __gitcomp "replay start"
1172                fi
1173                return
1174        fi
1175
1176        case "$subcommand" in
1177        bad|good|reset|skip|start)
1178                __git_complete_refs
1179                ;;
1180        *)
1181                ;;
1182        esac
1183}
1184
1185_git_branch ()
1186{
1187        local i c=1 only_local_ref="n" has_r="n"
1188
1189        while [ $c -lt $cword ]; do
1190                i="${words[c]}"
1191                case "$i" in
1192                -d|--delete|-m|--move)  only_local_ref="y" ;;
1193                -r|--remotes)           has_r="y" ;;
1194                esac
1195                ((c++))
1196        done
1197
1198        case "$cur" in
1199        --set-upstream-to=*)
1200                __git_complete_refs --cur="${cur##--set-upstream-to=}"
1201                ;;
1202        --*)
1203                __gitcomp "
1204                        --color --no-color --verbose --abbrev= --no-abbrev
1205                        --track --no-track --contains --no-contains --merged --no-merged
1206                        --set-upstream-to= --edit-description --list
1207                        --unset-upstream --delete --move --remotes
1208                        --column --no-column --sort= --points-at
1209                        "
1210                ;;
1211        *)
1212                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1213                        __gitcomp_direct "$(__git_heads "" "$cur" " ")"
1214                else
1215                        __git_complete_refs
1216                fi
1217                ;;
1218        esac
1219}
1220
1221_git_bundle ()
1222{
1223        local cmd="${words[2]}"
1224        case "$cword" in
1225        2)
1226                __gitcomp "create list-heads verify unbundle"
1227                ;;
1228        3)
1229                # looking for a file
1230                ;;
1231        *)
1232                case "$cmd" in
1233                        create)
1234                                __git_complete_revlist
1235                        ;;
1236                esac
1237                ;;
1238        esac
1239}
1240
1241_git_checkout ()
1242{
1243        __git_has_doubledash && return
1244
1245        case "$cur" in
1246        --conflict=*)
1247                __gitcomp "diff3 merge" "" "${cur##--conflict=}"
1248                ;;
1249        --*)
1250                __gitcomp "
1251                        --quiet --ours --theirs --track --no-track --merge
1252                        --conflict= --orphan --patch
1253                        "
1254                ;;
1255        *)
1256                # check if --track, --no-track, or --no-guess was specified
1257                # if so, disable DWIM mode
1258                local flags="--track --no-track --no-guess" track_opt="--track"
1259                if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
1260                   [ -n "$(__git_find_on_cmdline "$flags")" ]; then
1261                        track_opt=''
1262                fi
1263                __git_complete_refs $track_opt
1264                ;;
1265        esac
1266}
1267
1268_git_cherry ()
1269{
1270        __git_complete_refs
1271}
1272
1273_git_cherry_pick ()
1274{
1275        __git_find_repo_path
1276        if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
1277                __gitcomp "--continue --quit --abort"
1278                return
1279        fi
1280        case "$cur" in
1281        --*)
1282                __gitcomp "--edit --no-commit --signoff --strategy= --mainline"
1283                ;;
1284        *)
1285                __git_complete_refs
1286                ;;
1287        esac
1288}
1289
1290_git_clean ()
1291{
1292        case "$cur" in
1293        --*)
1294                __gitcomp "--dry-run --quiet"
1295                return
1296                ;;
1297        esac
1298
1299        # XXX should we check for -x option ?
1300        __git_complete_index_file "--others --directory"
1301}
1302
1303_git_clone ()
1304{
1305        case "$cur" in
1306        --*)
1307                __gitcomp "
1308                        --local
1309                        --no-hardlinks
1310                        --shared
1311                        --reference
1312                        --quiet
1313                        --no-checkout
1314                        --bare
1315                        --mirror
1316                        --origin
1317                        --upload-pack
1318                        --template=
1319                        --depth
1320                        --single-branch
1321                        --no-tags
1322                        --branch
1323                        --recurse-submodules
1324                        --no-single-branch
1325                        --shallow-submodules
1326                        "
1327                return
1328                ;;
1329        esac
1330}
1331
1332__git_untracked_file_modes="all no normal"
1333
1334_git_commit ()
1335{
1336        case "$prev" in
1337        -c|-C)
1338                __git_complete_refs
1339                return
1340                ;;
1341        esac
1342
1343        case "$cur" in
1344        --cleanup=*)
1345                __gitcomp "default scissors strip verbatim whitespace
1346                        " "" "${cur##--cleanup=}"
1347                return
1348                ;;
1349        --reuse-message=*|--reedit-message=*|\
1350        --fixup=*|--squash=*)
1351                __git_complete_refs --cur="${cur#*=}"
1352                return
1353                ;;
1354        --untracked-files=*)
1355                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
1356                return
1357                ;;
1358        --*)
1359                __gitcomp "
1360                        --all --author= --signoff --verify --no-verify
1361                        --edit --no-edit
1362                        --amend --include --only --interactive
1363                        --dry-run --reuse-message= --reedit-message=
1364                        --reset-author --file= --message= --template=
1365                        --cleanup= --untracked-files --untracked-files=
1366                        --verbose --quiet --fixup= --squash=
1367                        --patch --short --date --allow-empty
1368                        "
1369                return
1370        esac
1371
1372        if __git rev-parse --verify --quiet HEAD >/dev/null; then
1373                __git_complete_index_file "--committable"
1374        else
1375                # This is the first commit
1376                __git_complete_index_file "--cached"
1377        fi
1378}
1379
1380_git_describe ()
1381{
1382        case "$cur" in
1383        --*)
1384                __gitcomp "
1385                        --all --tags --contains --abbrev= --candidates=
1386                        --exact-match --debug --long --match --always --first-parent
1387                        --exclude --dirty --broken
1388                        "
1389                return
1390        esac
1391        __git_complete_refs
1392}
1393
1394__git_diff_algorithms="myers minimal patience histogram"
1395
1396__git_diff_submodule_formats="diff log short"
1397
1398__git_diff_common_options="--stat --numstat --shortstat --summary
1399                        --patch-with-stat --name-only --name-status --color
1400                        --no-color --color-words --no-renames --check
1401                        --full-index --binary --abbrev --diff-filter=
1402                        --find-copies-harder
1403                        --text --ignore-space-at-eol --ignore-space-change
1404                        --ignore-all-space --ignore-blank-lines --exit-code
1405                        --quiet --ext-diff --no-ext-diff
1406                        --no-prefix --src-prefix= --dst-prefix=
1407                        --inter-hunk-context=
1408                        --patience --histogram --minimal
1409                        --raw --word-diff --word-diff-regex=
1410                        --dirstat --dirstat= --dirstat-by-file
1411                        --dirstat-by-file= --cumulative
1412                        --diff-algorithm=
1413                        --submodule --submodule=
1414"
1415
1416_git_diff ()
1417{
1418        __git_has_doubledash && return
1419
1420        case "$cur" in
1421        --diff-algorithm=*)
1422                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1423                return
1424                ;;
1425        --submodule=*)
1426                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1427                return
1428                ;;
1429        --*)
1430                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1431                        --base --ours --theirs --no-index
1432                        $__git_diff_common_options
1433                        "
1434                return
1435                ;;
1436        esac
1437        __git_complete_revlist_file
1438}
1439
1440__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
1441                        tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
1442"
1443
1444_git_difftool ()
1445{
1446        __git_has_doubledash && return
1447
1448        case "$cur" in
1449        --tool=*)
1450                __gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
1451                return
1452                ;;
1453        --*)
1454                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1455                        --base --ours --theirs
1456                        --no-renames --diff-filter= --find-copies-harder
1457                        --relative --ignore-submodules
1458                        --tool="
1459                return
1460                ;;
1461        esac
1462        __git_complete_revlist_file
1463}
1464
1465__git_fetch_recurse_submodules="yes on-demand no"
1466
1467__git_fetch_options="
1468        --quiet --verbose --append --upload-pack --force --keep --depth=
1469        --tags --no-tags --all --prune --dry-run --recurse-submodules=
1470        --unshallow --update-shallow
1471"
1472
1473_git_fetch ()
1474{
1475        case "$cur" in
1476        --recurse-submodules=*)
1477                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1478                return
1479                ;;
1480        --*)
1481                __gitcomp "$__git_fetch_options"
1482                return
1483                ;;
1484        esac
1485        __git_complete_remote_or_refspec
1486}
1487
1488__git_format_patch_options="
1489        --stdout --attach --no-attach --thread --thread= --no-thread
1490        --numbered --start-number --numbered-files --keep-subject --signoff
1491        --signature --no-signature --in-reply-to= --cc= --full-index --binary
1492        --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
1493        --inline --suffix= --ignore-if-in-upstream --subject-prefix=
1494        --output-directory --reroll-count --to= --quiet --notes
1495"
1496
1497_git_format_patch ()
1498{
1499        case "$cur" in
1500        --thread=*)
1501                __gitcomp "
1502                        deep shallow
1503                        " "" "${cur##--thread=}"
1504                return
1505                ;;
1506        --*)
1507                __gitcomp "$__git_format_patch_options"
1508                return
1509                ;;
1510        esac
1511        __git_complete_revlist
1512}
1513
1514_git_fsck ()
1515{
1516        case "$cur" in
1517        --*)
1518                __gitcomp "
1519                        --tags --root --unreachable --cache --no-reflogs --full
1520                        --strict --verbose --lost-found --name-objects
1521                        "
1522                return
1523                ;;
1524        esac
1525}
1526
1527_git_gc ()
1528{
1529        case "$cur" in
1530        --*)
1531                __gitcomp "--prune --aggressive"
1532                return
1533                ;;
1534        esac
1535}
1536
1537_git_gitk ()
1538{
1539        _gitk
1540}
1541
1542# Lists matching symbol names from a tag (as in ctags) file.
1543# 1: List symbol names matching this word.
1544# 2: The tag file to list symbol names from.
1545# 3: A prefix to be added to each listed symbol name (optional).
1546# 4: A suffix to be appended to each listed symbol name (optional).
1547__git_match_ctag () {
1548        awk -v pfx="${3-}" -v sfx="${4-}" "
1549                /^${1//\//\\/}/ { print pfx \$1 sfx }
1550                " "$2"
1551}
1552
1553# Complete symbol names from a tag file.
1554# Usage: __git_complete_symbol [<option>]...
1555# --tags=<file>: The tag file to list symbol names from instead of the
1556#                default "tags".
1557# --pfx=<prefix>: A prefix to be added to each symbol name.
1558# --cur=<word>: The current symbol name to be completed.  Defaults to
1559#               the current word to be completed.
1560# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1561#                 of the default space.
1562__git_complete_symbol () {
1563        local tags=tags pfx="" cur_="${cur-}" sfx=" "
1564
1565        while test $# != 0; do
1566                case "$1" in
1567                --tags=*)       tags="${1##--tags=}" ;;
1568                --pfx=*)        pfx="${1##--pfx=}" ;;
1569                --cur=*)        cur_="${1##--cur=}" ;;
1570                --sfx=*)        sfx="${1##--sfx=}" ;;
1571                *)              return 1 ;;
1572                esac
1573                shift
1574        done
1575
1576        if test -r "$tags"; then
1577                __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1578        fi
1579}
1580
1581_git_grep ()
1582{
1583        __git_has_doubledash && return
1584
1585        case "$cur" in
1586        --*)
1587                __gitcomp "
1588                        --cached
1589                        --text --ignore-case --word-regexp --invert-match
1590                        --full-name --line-number
1591                        --extended-regexp --basic-regexp --fixed-strings
1592                        --perl-regexp
1593                        --threads
1594                        --files-with-matches --name-only
1595                        --files-without-match
1596                        --max-depth
1597                        --count
1598                        --and --or --not --all-match
1599                        --break --heading --show-function --function-context
1600                        --untracked --no-index
1601                        "
1602                return
1603                ;;
1604        esac
1605
1606        case "$cword,$prev" in
1607        2,*|*,-*)
1608                __git_complete_symbol && return
1609                ;;
1610        esac
1611
1612        __git_complete_refs
1613}
1614
1615_git_help ()
1616{
1617        case "$cur" in
1618        --*)
1619                __gitcomp "--all --guides --info --man --web"
1620                return
1621                ;;
1622        esac
1623        __git_compute_all_commands
1624        __gitcomp "$__git_all_commands $(__git_aliases)
1625                attributes cli core-tutorial cvs-migration
1626                diffcore everyday gitk glossary hooks ignore modules
1627                namespaces repository-layout revisions tutorial tutorial-2
1628                workflows
1629                "
1630}
1631
1632_git_init ()
1633{
1634        case "$cur" in
1635        --shared=*)
1636                __gitcomp "
1637                        false true umask group all world everybody
1638                        " "" "${cur##--shared=}"
1639                return
1640                ;;
1641        --*)
1642                __gitcomp "--quiet --bare --template= --shared --shared="
1643                return
1644                ;;
1645        esac
1646}
1647
1648_git_ls_files ()
1649{
1650        case "$cur" in
1651        --*)
1652                __gitcomp "--cached --deleted --modified --others --ignored
1653                        --stage --directory --no-empty-directory --unmerged
1654                        --killed --exclude= --exclude-from=
1655                        --exclude-per-directory= --exclude-standard
1656                        --error-unmatch --with-tree= --full-name
1657                        --abbrev --ignored --exclude-per-directory
1658                        "
1659                return
1660                ;;
1661        esac
1662
1663        # XXX ignore options like --modified and always suggest all cached
1664        # files.
1665        __git_complete_index_file "--cached"
1666}
1667
1668_git_ls_remote ()
1669{
1670        case "$cur" in
1671        --*)
1672                __gitcomp "--heads --tags --refs --get-url --symref"
1673                return
1674                ;;
1675        esac
1676        __gitcomp_nl "$(__git_remotes)"
1677}
1678
1679_git_ls_tree ()
1680{
1681        __git_complete_file
1682}
1683
1684# Options that go well for log, shortlog and gitk
1685__git_log_common_options="
1686        --not --all
1687        --branches --tags --remotes
1688        --first-parent --merges --no-merges
1689        --max-count=
1690        --max-age= --since= --after=
1691        --min-age= --until= --before=
1692        --min-parents= --max-parents=
1693        --no-min-parents --no-max-parents
1694"
1695# Options that go well for log and gitk (not shortlog)
1696__git_log_gitk_options="
1697        --dense --sparse --full-history
1698        --simplify-merges --simplify-by-decoration
1699        --left-right --notes --no-notes
1700"
1701# Options that go well for log and shortlog (not gitk)
1702__git_log_shortlog_options="
1703        --author= --committer= --grep=
1704        --all-match --invert-grep
1705"
1706
1707__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1708__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1709
1710_git_log ()
1711{
1712        __git_has_doubledash && return
1713        __git_find_repo_path
1714
1715        local merge=""
1716        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
1717                merge="--merge"
1718        fi
1719        case "$prev,$cur" in
1720        -L,:*:*)
1721                return  # fall back to Bash filename completion
1722                ;;
1723        -L,:*)
1724                __git_complete_symbol --cur="${cur#:}" --sfx=":"
1725                return
1726                ;;
1727        -G,*|-S,*)
1728                __git_complete_symbol
1729                return
1730                ;;
1731        esac
1732        case "$cur" in
1733        --pretty=*|--format=*)
1734                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1735                        " "" "${cur#*=}"
1736                return
1737                ;;
1738        --date=*)
1739                __gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1740                return
1741                ;;
1742        --decorate=*)
1743                __gitcomp "full short no" "" "${cur##--decorate=}"
1744                return
1745                ;;
1746        --diff-algorithm=*)
1747                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
1748                return
1749                ;;
1750        --submodule=*)
1751                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
1752                return
1753                ;;
1754        --*)
1755                __gitcomp "
1756                        $__git_log_common_options
1757                        $__git_log_shortlog_options
1758                        $__git_log_gitk_options
1759                        --root --topo-order --date-order --reverse
1760                        --follow --full-diff
1761                        --abbrev-commit --abbrev=
1762                        --relative-date --date=
1763                        --pretty= --format= --oneline
1764                        --show-signature
1765                        --cherry-mark
1766                        --cherry-pick
1767                        --graph
1768                        --decorate --decorate=
1769                        --walk-reflogs
1770                        --parents --children
1771                        $merge
1772                        $__git_diff_common_options
1773                        --pickaxe-all --pickaxe-regex
1774                        "
1775                return
1776                ;;
1777        -L:*:*)
1778                return  # fall back to Bash filename completion
1779                ;;
1780        -L:*)
1781                __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
1782                return
1783                ;;
1784        -G*)
1785                __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
1786                return
1787                ;;
1788        -S*)
1789                __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
1790                return
1791                ;;
1792        esac
1793        __git_complete_revlist
1794}
1795
1796# Common merge options shared by git-merge(1) and git-pull(1).
1797__git_merge_options="
1798        --no-commit --no-stat --log --no-log --squash --strategy
1799        --commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
1800        --verify-signatures --no-verify-signatures --gpg-sign
1801        --quiet --verbose --progress --no-progress
1802"
1803
1804_git_merge ()
1805{
1806        __git_complete_strategy && return
1807
1808        case "$cur" in
1809        --*)
1810                __gitcomp "$__git_merge_options
1811                        --rerere-autoupdate --no-rerere-autoupdate --abort --continue"
1812                return
1813        esac
1814        __git_complete_refs
1815}
1816
1817_git_mergetool ()
1818{
1819        case "$cur" in
1820        --tool=*)
1821                __gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1822                return
1823                ;;
1824        --*)
1825                __gitcomp "--tool= --prompt --no-prompt"
1826                return
1827                ;;
1828        esac
1829}
1830
1831_git_merge_base ()
1832{
1833        case "$cur" in
1834        --*)
1835                __gitcomp "--octopus --independent --is-ancestor --fork-point"
1836                return
1837                ;;
1838        esac
1839        __git_complete_refs
1840}
1841
1842_git_mv ()
1843{
1844        case "$cur" in
1845        --*)
1846                __gitcomp "--dry-run"
1847                return
1848                ;;
1849        esac
1850
1851        if [ $(__git_count_arguments "mv") -gt 0 ]; then
1852                # We need to show both cached and untracked files (including
1853                # empty directories) since this may not be the last argument.
1854                __git_complete_index_file "--cached --others --directory"
1855        else
1856                __git_complete_index_file "--cached"
1857        fi
1858}
1859
1860_git_name_rev ()
1861{
1862        __gitcomp "--tags --all --stdin"
1863}
1864
1865_git_notes ()
1866{
1867        local subcommands='add append copy edit list prune remove show'
1868        local subcommand="$(__git_find_on_cmdline "$subcommands")"
1869
1870        case "$subcommand,$cur" in
1871        ,--*)
1872                __gitcomp '--ref'
1873                ;;
1874        ,*)
1875                case "$prev" in
1876                --ref)
1877                        __git_complete_refs
1878                        ;;
1879                *)
1880                        __gitcomp "$subcommands --ref"
1881                        ;;
1882                esac
1883                ;;
1884        add,--reuse-message=*|append,--reuse-message=*|\
1885        add,--reedit-message=*|append,--reedit-message=*)
1886                __git_complete_refs --cur="${cur#*=}"
1887                ;;
1888        add,--*|append,--*)
1889                __gitcomp '--file= --message= --reedit-message=
1890                                --reuse-message='
1891                ;;
1892        copy,--*)
1893                __gitcomp '--stdin'
1894                ;;
1895        prune,--*)
1896                __gitcomp '--dry-run --verbose'
1897                ;;
1898        prune,*)
1899                ;;
1900        *)
1901                case "$prev" in
1902                -m|-F)
1903                        ;;
1904                *)
1905                        __git_complete_refs
1906                        ;;
1907                esac
1908                ;;
1909        esac
1910}
1911
1912_git_pull ()
1913{
1914        __git_complete_strategy && return
1915
1916        case "$cur" in
1917        --recurse-submodules=*)
1918                __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
1919                return
1920                ;;
1921        --*)
1922                __gitcomp "
1923                        --rebase --no-rebase
1924                        $__git_merge_options
1925                        $__git_fetch_options
1926                "
1927                return
1928                ;;
1929        esac
1930        __git_complete_remote_or_refspec
1931}
1932
1933__git_push_recurse_submodules="check on-demand only"
1934
1935__git_complete_force_with_lease ()
1936{
1937        local cur_=$1
1938
1939        case "$cur_" in
1940        --*=)
1941                ;;
1942        *:*)
1943                __git_complete_refs --cur="${cur_#*:}"
1944                ;;
1945        *)
1946                __git_complete_refs --cur="$cur_"
1947                ;;
1948        esac
1949}
1950
1951_git_push ()
1952{
1953        case "$prev" in
1954        --repo)
1955                __gitcomp_nl "$(__git_remotes)"
1956                return
1957                ;;
1958        --recurse-submodules)
1959                __gitcomp "$__git_push_recurse_submodules"
1960                return
1961                ;;
1962        esac
1963        case "$cur" in
1964        --repo=*)
1965                __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1966                return
1967                ;;
1968        --recurse-submodules=*)
1969                __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
1970                return
1971                ;;
1972        --force-with-lease=*)
1973                __git_complete_force_with_lease "${cur##--force-with-lease=}"
1974                return
1975                ;;
1976        --*)
1977                __gitcomp "
1978                        --all --mirror --tags --dry-run --force --verbose
1979                        --quiet --prune --delete --follow-tags
1980                        --receive-pack= --repo= --set-upstream
1981                        --force-with-lease --force-with-lease= --recurse-submodules=
1982                "
1983                return
1984                ;;
1985        esac
1986        __git_complete_remote_or_refspec
1987}
1988
1989_git_rebase ()
1990{
1991        __git_find_repo_path
1992        if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
1993                __gitcomp "--continue --skip --abort --quit --edit-todo"
1994                return
1995        elif [ -d "$__git_repo_path"/rebase-apply ] || \
1996             [ -d "$__git_repo_path"/rebase-merge ]; then
1997                __gitcomp "--continue --skip --abort --quit"
1998                return
1999        fi
2000        __git_complete_strategy && return
2001        case "$cur" in
2002        --whitespace=*)
2003                __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
2004                return
2005                ;;
2006        --*)
2007                __gitcomp "
2008                        --onto --merge --strategy --interactive
2009                        --preserve-merges --stat --no-stat
2010                        --committer-date-is-author-date --ignore-date
2011                        --ignore-whitespace --whitespace=
2012                        --autosquash --no-autosquash
2013                        --fork-point --no-fork-point
2014                        --autostash --no-autostash
2015                        --verify --no-verify
2016                        --keep-empty --root --force-rebase --no-ff
2017                        --exec
2018                        "
2019
2020                return
2021        esac
2022        __git_complete_refs
2023}
2024
2025_git_reflog ()
2026{
2027        local subcommands="show delete expire"
2028        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2029
2030        if [ -z "$subcommand" ]; then
2031                __gitcomp "$subcommands"
2032        else
2033                __git_complete_refs
2034        fi
2035}
2036
2037__git_send_email_confirm_options="always never auto cc compose"
2038__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
2039
2040_git_send_email ()
2041{
2042        case "$prev" in
2043        --to|--cc|--bcc|--from)
2044                __gitcomp "$(__git send-email --dump-aliases)"
2045                return
2046                ;;
2047        esac
2048
2049        case "$cur" in
2050        --confirm=*)
2051                __gitcomp "
2052                        $__git_send_email_confirm_options
2053                        " "" "${cur##--confirm=}"
2054                return
2055                ;;
2056        --suppress-cc=*)
2057                __gitcomp "
2058                        $__git_send_email_suppresscc_options
2059                        " "" "${cur##--suppress-cc=}"
2060
2061                return
2062                ;;
2063        --smtp-encryption=*)
2064                __gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
2065                return
2066                ;;
2067        --thread=*)
2068                __gitcomp "
2069                        deep shallow
2070                        " "" "${cur##--thread=}"
2071                return
2072                ;;
2073        --to=*|--cc=*|--bcc=*|--from=*)
2074                __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
2075                return
2076                ;;
2077        --*)
2078                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
2079                        --compose --confirm= --dry-run --envelope-sender
2080                        --from --identity
2081                        --in-reply-to --no-chain-reply-to --no-signed-off-by-cc
2082                        --no-suppress-from --no-thread --quiet
2083                        --signed-off-by-cc --smtp-pass --smtp-server
2084                        --smtp-server-port --smtp-encryption= --smtp-user
2085                        --subject --suppress-cc= --suppress-from --thread --to
2086                        --validate --no-validate
2087                        $__git_format_patch_options"
2088                return
2089                ;;
2090        esac
2091        __git_complete_revlist
2092}
2093
2094_git_stage ()
2095{
2096        _git_add
2097}
2098
2099_git_status ()
2100{
2101        local complete_opt
2102        local untracked_state
2103
2104        case "$cur" in
2105        --ignore-submodules=*)
2106                __gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
2107                return
2108                ;;
2109        --untracked-files=*)
2110                __gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
2111                return
2112                ;;
2113        --column=*)
2114                __gitcomp "
2115                        always never auto column row plain dense nodense
2116                        " "" "${cur##--column=}"
2117                return
2118                ;;
2119        --*)
2120                __gitcomp "
2121                        --short --branch --porcelain --long --verbose
2122                        --untracked-files= --ignore-submodules= --ignored
2123                        --column= --no-column
2124                        "
2125                return
2126                ;;
2127        esac
2128
2129        untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
2130                "$__git_untracked_file_modes" "status.showUntrackedFiles")"
2131
2132        case "$untracked_state" in
2133        no)
2134                # --ignored option does not matter
2135                complete_opt=
2136                ;;
2137        all|normal|*)
2138                complete_opt="--cached --directory --no-empty-directory --others"
2139
2140                if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
2141                        complete_opt="$complete_opt --ignored --exclude=*"
2142                fi
2143                ;;
2144        esac
2145
2146        __git_complete_index_file "$complete_opt"
2147}
2148
2149__git_config_get_set_variables ()
2150{
2151        local prevword word config_file= c=$cword
2152        while [ $c -gt 1 ]; do
2153                word="${words[c]}"
2154                case "$word" in
2155                --system|--global|--local|--file=*)
2156                        config_file="$word"
2157                        break
2158                        ;;
2159                -f|--file)
2160                        config_file="$word $prevword"
2161                        break
2162                        ;;
2163                esac
2164                prevword=$word
2165                c=$((--c))
2166        done
2167
2168        __git config $config_file --name-only --list
2169}
2170
2171_git_config ()
2172{
2173        case "$prev" in
2174        branch.*.remote|branch.*.pushremote)
2175                __gitcomp_nl "$(__git_remotes)"
2176                return
2177                ;;
2178        branch.*.merge)
2179                __git_complete_refs
2180                return
2181                ;;
2182        branch.*.rebase)
2183                __gitcomp "false true preserve interactive"
2184                return
2185                ;;
2186        remote.pushdefault)
2187                __gitcomp_nl "$(__git_remotes)"
2188                return
2189                ;;
2190        remote.*.fetch)
2191                local remote="${prev#remote.}"
2192                remote="${remote%.fetch}"
2193                if [ -z "$cur" ]; then
2194                        __gitcomp_nl "refs/heads/" "" "" ""
2195                        return
2196                fi
2197                __gitcomp_nl "$(__git_refs_remotes "$remote")"
2198                return
2199                ;;
2200        remote.*.push)
2201                local remote="${prev#remote.}"
2202                remote="${remote%.push}"
2203                __gitcomp_nl "$(__git for-each-ref \
2204                        --format='%(refname):%(refname)' refs/heads)"
2205                return
2206                ;;
2207        pull.twohead|pull.octopus)
2208                __git_compute_merge_strategies
2209                __gitcomp "$__git_merge_strategies"
2210                return
2211                ;;
2212        color.branch|color.diff|color.interactive|\
2213        color.showbranch|color.status|color.ui)
2214                __gitcomp "always never auto"
2215                return
2216                ;;
2217        color.pager)
2218                __gitcomp "false true"
2219                return
2220                ;;
2221        color.*.*)
2222                __gitcomp "
2223                        normal black red green yellow blue magenta cyan white
2224                        bold dim ul blink reverse
2225                        "
2226                return
2227                ;;
2228        diff.submodule)
2229                __gitcomp "log short"
2230                return
2231                ;;
2232        help.format)
2233                __gitcomp "man info web html"
2234                return
2235                ;;
2236        log.date)
2237                __gitcomp "$__git_log_date_formats"
2238                return
2239                ;;
2240        sendemail.aliasesfiletype)
2241                __gitcomp "mutt mailrc pine elm gnus"
2242                return
2243                ;;
2244        sendemail.confirm)
2245                __gitcomp "$__git_send_email_confirm_options"
2246                return
2247                ;;
2248        sendemail.suppresscc)
2249                __gitcomp "$__git_send_email_suppresscc_options"
2250                return
2251                ;;
2252        sendemail.transferencoding)
2253                __gitcomp "7bit 8bit quoted-printable base64"
2254                return
2255                ;;
2256        --get|--get-all|--unset|--unset-all)
2257                __gitcomp_nl "$(__git_config_get_set_variables)"
2258                return
2259                ;;
2260        *.*)
2261                return
2262                ;;
2263        esac
2264        case "$cur" in
2265        --*)
2266                __gitcomp "
2267                        --system --global --local --file=
2268                        --list --replace-all
2269                        --get --get-all --get-regexp
2270                        --add --unset --unset-all
2271                        --remove-section --rename-section
2272                        --name-only
2273                        "
2274                return
2275                ;;
2276        branch.*.*)
2277                local pfx="${cur%.*}." cur_="${cur##*.}"
2278                __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
2279                return
2280                ;;
2281        branch.*)
2282                local pfx="${cur%.*}." cur_="${cur#*.}"
2283                __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
2284                __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
2285                return
2286                ;;
2287        guitool.*.*)
2288                local pfx="${cur%.*}." cur_="${cur##*.}"
2289                __gitcomp "
2290                        argprompt cmd confirm needsfile noconsole norescan
2291                        prompt revprompt revunmerged title
2292                        " "$pfx" "$cur_"
2293                return
2294                ;;
2295        difftool.*.*)
2296                local pfx="${cur%.*}." cur_="${cur##*.}"
2297                __gitcomp "cmd path" "$pfx" "$cur_"
2298                return
2299                ;;
2300        man.*.*)
2301                local pfx="${cur%.*}." cur_="${cur##*.}"
2302                __gitcomp "cmd path" "$pfx" "$cur_"
2303                return
2304                ;;
2305        mergetool.*.*)
2306                local pfx="${cur%.*}." cur_="${cur##*.}"
2307                __gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
2308                return
2309                ;;
2310        pager.*)
2311                local pfx="${cur%.*}." cur_="${cur#*.}"
2312                __git_compute_all_commands
2313                __gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2314                return
2315                ;;
2316        remote.*.*)
2317                local pfx="${cur%.*}." cur_="${cur##*.}"
2318                __gitcomp "
2319                        url proxy fetch push mirror skipDefaultUpdate
2320                        receivepack uploadpack tagopt pushurl
2321                        " "$pfx" "$cur_"
2322                return
2323                ;;
2324        remote.*)
2325                local pfx="${cur%.*}." cur_="${cur#*.}"
2326                __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2327                __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
2328                return
2329                ;;
2330        url.*.*)
2331                local pfx="${cur%.*}." cur_="${cur##*.}"
2332                __gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2333                return
2334                ;;
2335        esac
2336        __gitcomp "
2337                add.ignoreErrors
2338                advice.amWorkDir
2339                advice.commitBeforeMerge
2340                advice.detachedHead
2341                advice.implicitIdentity
2342                advice.pushAlreadyExists
2343                advice.pushFetchFirst
2344                advice.pushNeedsForce
2345                advice.pushNonFFCurrent
2346                advice.pushNonFFMatching
2347                advice.pushUpdateRejected
2348                advice.resolveConflict
2349                advice.rmHints
2350                advice.statusHints
2351                advice.statusUoption
2352                alias.
2353                am.keepcr
2354                am.threeWay
2355                apply.ignorewhitespace
2356                apply.whitespace
2357                branch.autosetupmerge
2358                branch.autosetuprebase
2359                browser.
2360                clean.requireForce
2361                color.branch
2362                color.branch.current
2363                color.branch.local
2364                color.branch.plain
2365                color.branch.remote
2366                color.decorate.HEAD
2367                color.decorate.branch
2368                color.decorate.remoteBranch
2369                color.decorate.stash
2370                color.decorate.tag
2371                color.diff
2372                color.diff.commit
2373                color.diff.frag
2374                color.diff.func
2375                color.diff.meta
2376                color.diff.new
2377                color.diff.old
2378                color.diff.plain
2379                color.diff.whitespace
2380                color.grep
2381                color.grep.context
2382                color.grep.filename
2383                color.grep.function
2384                color.grep.linenumber
2385                color.grep.match
2386                color.grep.selected
2387                color.grep.separator
2388                color.interactive
2389                color.interactive.error
2390                color.interactive.header
2391                color.interactive.help
2392                color.interactive.prompt
2393                color.pager
2394                color.showbranch
2395                color.status
2396                color.status.added
2397                color.status.changed
2398                color.status.header
2399                color.status.localBranch
2400                color.status.nobranch
2401                color.status.remoteBranch
2402                color.status.unmerged
2403                color.status.untracked
2404                color.status.updated
2405                color.ui
2406                commit.cleanup
2407                commit.gpgSign
2408                commit.status
2409                commit.template
2410                commit.verbose
2411                core.abbrev
2412                core.askpass
2413                core.attributesfile
2414                core.autocrlf
2415                core.bare
2416                core.bigFileThreshold
2417                core.checkStat
2418                core.commentChar
2419                core.compression
2420                core.createObject
2421                core.deltaBaseCacheLimit
2422                core.editor
2423                core.eol
2424                core.excludesfile
2425                core.fileMode
2426                core.fsyncobjectfiles
2427                core.gitProxy
2428                core.hideDotFiles
2429                core.hooksPath
2430                core.ignoreStat
2431                core.ignorecase
2432                core.logAllRefUpdates
2433                core.loosecompression
2434                core.notesRef
2435                core.packedGitLimit
2436                core.packedGitWindowSize
2437                core.packedRefsTimeout
2438                core.pager
2439                core.precomposeUnicode
2440                core.preferSymlinkRefs
2441                core.preloadindex
2442                core.protectHFS
2443                core.protectNTFS
2444                core.quotepath
2445                core.repositoryFormatVersion
2446                core.safecrlf
2447                core.sharedRepository
2448                core.sparseCheckout
2449                core.splitIndex
2450                core.sshCommand
2451                core.symlinks
2452                core.trustctime
2453                core.untrackedCache
2454                core.warnAmbiguousRefs
2455                core.whitespace
2456                core.worktree
2457                credential.helper
2458                credential.useHttpPath
2459                credential.username
2460                credentialCache.ignoreSIGHUP
2461                diff.autorefreshindex
2462                diff.external
2463                diff.ignoreSubmodules
2464                diff.mnemonicprefix
2465                diff.noprefix
2466                diff.renameLimit
2467                diff.renames
2468                diff.statGraphWidth
2469                diff.submodule
2470                diff.suppressBlankEmpty
2471                diff.tool
2472                diff.wordRegex
2473                diff.algorithm
2474                difftool.
2475                difftool.prompt
2476                fetch.recurseSubmodules
2477                fetch.unpackLimit
2478                format.attach
2479                format.cc
2480                format.coverLetter
2481                format.from
2482                format.headers
2483                format.numbered
2484                format.pretty
2485                format.signature
2486                format.signoff
2487                format.subjectprefix
2488                format.suffix
2489                format.thread
2490                format.to
2491                gc.
2492                gc.aggressiveDepth
2493                gc.aggressiveWindow
2494                gc.auto
2495                gc.autoDetach
2496                gc.autopacklimit
2497                gc.logExpiry
2498                gc.packrefs
2499                gc.pruneexpire
2500                gc.reflogexpire
2501                gc.reflogexpireunreachable
2502                gc.rerereresolved
2503                gc.rerereunresolved
2504                gc.worktreePruneExpire
2505                gitcvs.allbinary
2506                gitcvs.commitmsgannotation
2507                gitcvs.dbTableNamePrefix
2508                gitcvs.dbdriver
2509                gitcvs.dbname
2510                gitcvs.dbpass
2511                gitcvs.dbuser
2512                gitcvs.enabled
2513                gitcvs.logfile
2514                gitcvs.usecrlfattr
2515                guitool.
2516                gui.blamehistoryctx
2517                gui.commitmsgwidth
2518                gui.copyblamethreshold
2519                gui.diffcontext
2520                gui.encoding
2521                gui.fastcopyblame
2522                gui.matchtrackingbranch
2523                gui.newbranchtemplate
2524                gui.pruneduringfetch
2525                gui.spellingdictionary
2526                gui.trustmtime
2527                help.autocorrect
2528                help.browser
2529                help.format
2530                http.lowSpeedLimit
2531                http.lowSpeedTime
2532                http.maxRequests
2533                http.minSessions
2534                http.noEPSV
2535                http.postBuffer
2536                http.proxy
2537                http.sslCipherList
2538                http.sslVersion
2539                http.sslCAInfo
2540                http.sslCAPath
2541                http.sslCert
2542                http.sslCertPasswordProtected
2543                http.sslKey
2544                http.sslVerify
2545                http.useragent
2546                i18n.commitEncoding
2547                i18n.logOutputEncoding
2548                imap.authMethod
2549                imap.folder
2550                imap.host
2551                imap.pass
2552                imap.port
2553                imap.preformattedHTML
2554                imap.sslverify
2555                imap.tunnel
2556                imap.user
2557                init.templatedir
2558                instaweb.browser
2559                instaweb.httpd
2560                instaweb.local
2561                instaweb.modulepath
2562                instaweb.port
2563                interactive.singlekey
2564                log.date
2565                log.decorate
2566                log.showroot
2567                mailmap.file
2568                man.
2569                man.viewer
2570                merge.
2571                merge.conflictstyle
2572                merge.log
2573                merge.renameLimit
2574                merge.renormalize
2575                merge.stat
2576                merge.tool
2577                merge.verbosity
2578                mergetool.
2579                mergetool.keepBackup
2580                mergetool.keepTemporaries
2581                mergetool.prompt
2582                notes.displayRef
2583                notes.rewrite.
2584                notes.rewrite.amend
2585                notes.rewrite.rebase
2586                notes.rewriteMode
2587                notes.rewriteRef
2588                pack.compression
2589                pack.deltaCacheLimit
2590                pack.deltaCacheSize
2591                pack.depth
2592                pack.indexVersion
2593                pack.packSizeLimit
2594                pack.threads
2595                pack.window
2596                pack.windowMemory
2597                pager.
2598                pretty.
2599                pull.octopus
2600                pull.twohead
2601                push.default
2602                push.followTags
2603                rebase.autosquash
2604                rebase.stat
2605                receive.autogc
2606                receive.denyCurrentBranch
2607                receive.denyDeleteCurrent
2608                receive.denyDeletes
2609                receive.denyNonFastForwards
2610                receive.fsckObjects
2611                receive.unpackLimit
2612                receive.updateserverinfo
2613                remote.pushdefault
2614                remotes.
2615                repack.usedeltabaseoffset
2616                rerere.autoupdate
2617                rerere.enabled
2618                sendemail.
2619                sendemail.aliasesfile
2620                sendemail.aliasfiletype
2621                sendemail.bcc
2622                sendemail.cc
2623                sendemail.cccmd
2624                sendemail.chainreplyto
2625                sendemail.confirm
2626                sendemail.envelopesender
2627                sendemail.from
2628                sendemail.identity
2629                sendemail.multiedit
2630                sendemail.signedoffbycc
2631                sendemail.smtpdomain
2632                sendemail.smtpencryption
2633                sendemail.smtppass
2634                sendemail.smtpserver
2635                sendemail.smtpserveroption
2636                sendemail.smtpserverport
2637                sendemail.smtpuser
2638                sendemail.suppresscc
2639                sendemail.suppressfrom
2640                sendemail.thread
2641                sendemail.to
2642                sendemail.validate
2643                sendemail.smtpbatchsize
2644                sendemail.smtprelogindelay
2645                showbranch.default
2646                status.relativePaths
2647                status.showUntrackedFiles
2648                status.submodulesummary
2649                submodule.
2650                tar.umask
2651                transfer.unpackLimit
2652                url.
2653                user.email
2654                user.name
2655                user.signingkey
2656                web.browser
2657                branch. remote.
2658        "
2659}
2660
2661_git_remote ()
2662{
2663        local subcommands="
2664                add rename remove set-head set-branches
2665                get-url set-url show prune update
2666                "
2667        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2668        if [ -z "$subcommand" ]; then
2669                case "$cur" in
2670                --*)
2671                        __gitcomp "--verbose"
2672                        ;;
2673                *)
2674                        __gitcomp "$subcommands"
2675                        ;;
2676                esac
2677                return
2678        fi
2679
2680        case "$subcommand,$cur" in
2681        add,--*)
2682                __gitcomp "--track --master --fetch --tags --no-tags --mirror="
2683                ;;
2684        add,*)
2685                ;;
2686        set-head,--*)
2687                __gitcomp "--auto --delete"
2688                ;;
2689        set-branches,--*)
2690                __gitcomp "--add"
2691                ;;
2692        set-head,*|set-branches,*)
2693                __git_complete_remote_or_refspec
2694                ;;
2695        update,--*)
2696                __gitcomp "--prune"
2697                ;;
2698        update,*)
2699                __gitcomp "$(__git_get_config_variables "remotes")"
2700                ;;
2701        set-url,--*)
2702                __gitcomp "--push --add --delete"
2703                ;;
2704        get-url,--*)
2705                __gitcomp "--push --all"
2706                ;;
2707        prune,--*)
2708                __gitcomp "--dry-run"
2709                ;;
2710        *)
2711                __gitcomp_nl "$(__git_remotes)"
2712                ;;
2713        esac
2714}
2715
2716_git_replace ()
2717{
2718        case "$cur" in
2719        --*)
2720                __gitcomp "--edit --graft --format= --list --delete"
2721                return
2722                ;;
2723        esac
2724        __git_complete_refs
2725}
2726
2727_git_rerere ()
2728{
2729        local subcommands="clear forget diff remaining status gc"
2730        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2731        if test -z "$subcommand"
2732        then
2733                __gitcomp "$subcommands"
2734                return
2735        fi
2736}
2737
2738_git_reset ()
2739{
2740        __git_has_doubledash && return
2741
2742        case "$cur" in
2743        --*)
2744                __gitcomp "--merge --mixed --hard --soft --patch --keep"
2745                return
2746                ;;
2747        esac
2748        __git_complete_refs
2749}
2750
2751_git_revert ()
2752{
2753        __git_find_repo_path
2754        if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
2755                __gitcomp "--continue --quit --abort"
2756                return
2757        fi
2758        case "$cur" in
2759        --*)
2760                __gitcomp "
2761                        --edit --mainline --no-edit --no-commit --signoff
2762                        --strategy= --strategy-option=
2763                        "
2764                return
2765                ;;
2766        esac
2767        __git_complete_refs
2768}
2769
2770_git_rm ()
2771{
2772        case "$cur" in
2773        --*)
2774                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
2775                return
2776                ;;
2777        esac
2778
2779        __git_complete_index_file "--cached"
2780}
2781
2782_git_shortlog ()
2783{
2784        __git_has_doubledash && return
2785
2786        case "$cur" in
2787        --*)
2788                __gitcomp "
2789                        $__git_log_common_options
2790                        $__git_log_shortlog_options
2791                        --numbered --summary --email
2792                        "
2793                return
2794                ;;
2795        esac
2796        __git_complete_revlist
2797}
2798
2799_git_show ()
2800{
2801        __git_has_doubledash && return
2802
2803        case "$cur" in
2804        --pretty=*|--format=*)
2805                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2806                        " "" "${cur#*=}"
2807                return
2808                ;;
2809        --diff-algorithm=*)
2810                __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
2811                return
2812                ;;
2813        --submodule=*)
2814                __gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
2815                return
2816                ;;
2817        --*)
2818                __gitcomp "--pretty= --format= --abbrev-commit --oneline
2819                        --show-signature
2820                        $__git_diff_common_options
2821                        "
2822                return
2823                ;;
2824        esac
2825        __git_complete_revlist_file
2826}
2827
2828_git_show_branch ()
2829{
2830        case "$cur" in
2831        --*)
2832                __gitcomp "
2833                        --all --remotes --topo-order --date-order --current --more=
2834                        --list --independent --merge-base --no-name
2835                        --color --no-color
2836                        --sha1-name --sparse --topics --reflog
2837                        "
2838                return
2839                ;;
2840        esac
2841        __git_complete_revlist
2842}
2843
2844_git_stash ()
2845{
2846        local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
2847        local subcommands='push save list show apply clear drop pop create branch'
2848        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2849        if [ -z "$subcommand" ]; then
2850                case "$cur" in
2851                --*)
2852                        __gitcomp "$save_opts"
2853                        ;;
2854                *)
2855                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
2856                                __gitcomp "$subcommands"
2857                        fi
2858                        ;;
2859                esac
2860        else
2861                case "$subcommand,$cur" in
2862                push,--*)
2863                        __gitcomp "$save_opts --message"
2864                        ;;
2865                save,--*)
2866                        __gitcomp "$save_opts"
2867                        ;;
2868                apply,--*|pop,--*)
2869                        __gitcomp "--index --quiet"
2870                        ;;
2871                drop,--*)
2872                        __gitcomp "--quiet"
2873                        ;;
2874                show,--*|branch,--*)
2875                        ;;
2876                branch,*)
2877                        if [ $cword -eq 3 ]; then
2878                                __git_complete_refs
2879                        else
2880                                __gitcomp_nl "$(__git stash list \
2881                                                | sed -n -e 's/:.*//p')"
2882                        fi
2883                        ;;
2884                show,*|apply,*|drop,*|pop,*)
2885                        __gitcomp_nl "$(__git stash list \
2886                                        | sed -n -e 's/:.*//p')"
2887                        ;;
2888                *)
2889                        ;;
2890                esac
2891        fi
2892}
2893
2894_git_submodule ()
2895{
2896        __git_has_doubledash && return
2897
2898        local subcommands="add status init deinit update summary foreach sync"
2899        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2900        if [ -z "$subcommand" ]; then
2901                case "$cur" in
2902                --*)
2903                        __gitcomp "--quiet"
2904                        ;;
2905                *)
2906                        __gitcomp "$subcommands"
2907                        ;;
2908                esac
2909                return
2910        fi
2911
2912        case "$subcommand,$cur" in
2913        add,--*)
2914                __gitcomp "--branch --force --name --reference --depth"
2915                ;;
2916        status,--*)
2917                __gitcomp "--cached --recursive"
2918                ;;
2919        deinit,--*)
2920                __gitcomp "--force --all"
2921                ;;
2922        update,--*)
2923                __gitcomp "
2924                        --init --remote --no-fetch
2925                        --recommend-shallow --no-recommend-shallow
2926                        --force --rebase --merge --reference --depth --recursive --jobs
2927                "
2928                ;;
2929        summary,--*)
2930                __gitcomp "--cached --files --summary-limit"
2931                ;;
2932        foreach,--*|sync,--*)
2933                __gitcomp "--recursive"
2934                ;;
2935        *)
2936                ;;
2937        esac
2938}
2939
2940_git_svn ()
2941{
2942        local subcommands="
2943                init fetch clone rebase dcommit log find-rev
2944                set-tree commit-diff info create-ignore propget
2945                proplist show-ignore show-externals branch tag blame
2946                migrate mkdirs reset gc
2947                "
2948        local subcommand="$(__git_find_on_cmdline "$subcommands")"
2949        if [ -z "$subcommand" ]; then
2950                __gitcomp "$subcommands"
2951        else
2952                local remote_opts="--username= --config-dir= --no-auth-cache"
2953                local fc_opts="
2954                        --follow-parent --authors-file= --repack=
2955                        --no-metadata --use-svm-props --use-svnsync-props
2956                        --log-window-size= --no-checkout --quiet
2957                        --repack-flags --use-log-author --localtime
2958                        --add-author-from
2959                        --ignore-paths= --include-paths= $remote_opts
2960                        "
2961                local init_opts="
2962                        --template= --shared= --trunk= --tags=
2963                        --branches= --stdlayout --minimize-url
2964                        --no-metadata --use-svm-props --use-svnsync-props
2965                        --rewrite-root= --prefix= $remote_opts
2966                        "
2967                local cmt_opts="
2968                        --edit --rmdir --find-copies-harder --copy-similarity=
2969                        "
2970
2971                case "$subcommand,$cur" in
2972                fetch,--*)
2973                        __gitcomp "--revision= --fetch-all $fc_opts"
2974                        ;;
2975                clone,--*)
2976                        __gitcomp "--revision= $fc_opts $init_opts"
2977                        ;;
2978                init,--*)
2979                        __gitcomp "$init_opts"
2980                        ;;
2981                dcommit,--*)
2982                        __gitcomp "
2983                                --merge --strategy= --verbose --dry-run
2984                                --fetch-all --no-rebase --commit-url
2985                                --revision --interactive $cmt_opts $fc_opts
2986                                "
2987                        ;;
2988                set-tree,--*)
2989                        __gitcomp "--stdin $cmt_opts $fc_opts"
2990                        ;;
2991                create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2992                show-externals,--*|mkdirs,--*)
2993                        __gitcomp "--revision="
2994                        ;;
2995                log,--*)
2996                        __gitcomp "
2997                                --limit= --revision= --verbose --incremental
2998                                --oneline --show-commit --non-recursive
2999                                --authors-file= --color
3000                                "
3001                        ;;
3002                rebase,--*)
3003                        __gitcomp "
3004                                --merge --verbose --strategy= --local
3005                                --fetch-all --dry-run $fc_opts
3006                                "
3007                        ;;
3008                commit-diff,--*)
3009                        __gitcomp "--message= --file= --revision= $cmt_opts"
3010                        ;;
3011                info,--*)
3012                        __gitcomp "--url"
3013                        ;;
3014                branch,--*)
3015                        __gitcomp "--dry-run --message --tag"
3016                        ;;
3017                tag,--*)
3018                        __gitcomp "--dry-run --message"
3019                        ;;
3020                blame,--*)
3021                        __gitcomp "--git-format"
3022                        ;;
3023                migrate,--*)
3024                        __gitcomp "
3025                                --config-dir= --ignore-paths= --minimize
3026                                --no-auth-cache --username=
3027                                "
3028                        ;;
3029                reset,--*)
3030                        __gitcomp "--revision= --parent"
3031                        ;;
3032                *)
3033                        ;;
3034                esac
3035        fi
3036}
3037
3038_git_tag ()
3039{
3040        local i c=1 f=0
3041        while [ $c -lt $cword ]; do
3042                i="${words[c]}"
3043                case "$i" in
3044                -d|-v)
3045                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3046                        return
3047                        ;;
3048                -f)
3049                        f=1
3050                        ;;
3051                esac
3052                ((c++))
3053        done
3054
3055        case "$prev" in
3056        -m|-F)
3057                ;;
3058        -*|tag)
3059                if [ $f = 1 ]; then
3060                        __gitcomp_direct "$(__git_tags "" "$cur" " ")"
3061                fi
3062                ;;
3063        *)
3064                __git_complete_refs
3065                ;;
3066        esac
3067
3068        case "$cur" in
3069        --*)
3070                __gitcomp "
3071                        --list --delete --verify --annotate --message --file
3072                        --sign --cleanup --local-user --force --column --sort=
3073                        --contains --no-contains --points-at --merged --no-merged --create-reflog
3074                        "
3075                ;;
3076        esac
3077}
3078
3079_git_whatchanged ()
3080{
3081        _git_log
3082}
3083
3084_git_worktree ()
3085{
3086        local subcommands="add list lock prune unlock"
3087        local subcommand="$(__git_find_on_cmdline "$subcommands")"
3088        if [ -z "$subcommand" ]; then
3089                __gitcomp "$subcommands"
3090        else
3091                case "$subcommand,$cur" in
3092                add,--*)
3093                        __gitcomp "--detach"
3094                        ;;
3095                list,--*)
3096                        __gitcomp "--porcelain"
3097                        ;;
3098                lock,--*)
3099                        __gitcomp "--reason"
3100                        ;;
3101                prune,--*)
3102                        __gitcomp "--dry-run --expire --verbose"
3103                        ;;
3104                *)
3105                        ;;
3106                esac
3107        fi
3108}
3109
3110__git_main ()
3111{
3112        local i c=1 command __git_dir __git_repo_path
3113        local __git_C_args C_args_count=0
3114
3115        while [ $c -lt $cword ]; do
3116                i="${words[c]}"
3117                case "$i" in
3118                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
3119                --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
3120                --bare)      __git_dir="." ;;
3121                --help) command="help"; break ;;
3122                -c|--work-tree|--namespace) ((c++)) ;;
3123                -C)     __git_C_args[C_args_count++]=-C
3124                        ((c++))
3125                        __git_C_args[C_args_count++]="${words[c]}"
3126                        ;;
3127                -*) ;;
3128                *) command="$i"; break ;;
3129                esac
3130                ((c++))
3131        done
3132
3133        if [ -z "$command" ]; then
3134                case "$prev" in
3135                --git-dir|-C|--work-tree)
3136                        # these need a path argument, let's fall back to
3137                        # Bash filename completion
3138                        return
3139                        ;;
3140                -c|--namespace)
3141                        # we don't support completing these options' arguments
3142                        return
3143                        ;;
3144                esac
3145                case "$cur" in
3146                --*)   __gitcomp "
3147                        --paginate
3148                        --no-pager
3149                        --git-dir=
3150                        --bare
3151                        --version
3152                        --exec-path
3153                        --exec-path=
3154                        --html-path
3155                        --man-path
3156                        --info-path
3157                        --work-tree=
3158                        --namespace=
3159                        --no-replace-objects
3160                        --help
3161                        "
3162                        ;;
3163                *)     __git_compute_porcelain_commands
3164                       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
3165                esac
3166                return
3167        fi
3168
3169        local completion_func="_git_${command//-/_}"
3170        declare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return
3171
3172        local expansion=$(__git_aliased_command "$command")
3173        if [ -n "$expansion" ]; then
3174                words[1]=$expansion
3175                completion_func="_git_${expansion//-/_}"
3176                declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
3177        fi
3178}
3179
3180__gitk_main ()
3181{
3182        __git_has_doubledash && return
3183
3184        local __git_repo_path
3185        __git_find_repo_path
3186
3187        local merge=""
3188        if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
3189                merge="--merge"
3190        fi
3191        case "$cur" in
3192        --*)
3193                __gitcomp "
3194                        $__git_log_common_options
3195                        $__git_log_gitk_options
3196                        $merge
3197                        "
3198                return
3199                ;;
3200        esac
3201        __git_complete_revlist
3202}
3203
3204if [[ -n ${ZSH_VERSION-} ]]; then
3205        echo "WARNING: this script is deprecated, please see git-completion.zsh" 1>&2
3206
3207        autoload -U +X compinit && compinit
3208
3209        __gitcomp ()
3210        {
3211                emulate -L zsh
3212
3213                local cur_="${3-$cur}"
3214
3215                case "$cur_" in
3216                --*=)
3217                        ;;
3218                *)
3219                        local c IFS=$' \t\n'
3220                        local -a array
3221                        for c in ${=1}; do
3222                                c="$c${4-}"
3223                                case $c in
3224                                --*=*|*.) ;;
3225                                *) c="$c " ;;
3226                                esac
3227                                array[${#array[@]}+1]="$c"
3228                        done
3229                        compset -P '*[=:]'
3230                        compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
3231                        ;;
3232                esac
3233        }
3234
3235        __gitcomp_direct ()
3236        {
3237                emulate -L zsh
3238
3239                local IFS=$'\n'
3240                compset -P '*[=:]'
3241                compadd -Q -- ${=1} && _ret=0
3242        }
3243
3244        __gitcomp_nl ()
3245        {
3246                emulate -L zsh
3247
3248                local IFS=$'\n'
3249                compset -P '*[=:]'
3250                compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
3251        }
3252
3253        __gitcomp_file ()
3254        {
3255                emulate -L zsh
3256
3257                local IFS=$'\n'
3258                compset -P '*[=:]'
3259                compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
3260        }
3261
3262        _git ()
3263        {
3264                local _ret=1 cur cword prev
3265                cur=${words[CURRENT]}
3266                prev=${words[CURRENT-1]}
3267                let cword=CURRENT-1
3268                emulate ksh -c __${service}_main
3269                let _ret && _default && _ret=0
3270                return _ret
3271        }
3272
3273        compdef _git git gitk
3274        return
3275fi
3276
3277__git_func_wrap ()
3278{
3279        local cur words cword prev
3280        _get_comp_words_by_ref -n =: cur words cword prev
3281        $1
3282}
3283
3284# Setup completion for certain functions defined above by setting common
3285# variables and workarounds.
3286# This is NOT a public function; use at your own risk.
3287__git_complete ()
3288{
3289        local wrapper="__git_wrap${2}"
3290        eval "$wrapper () { __git_func_wrap $2 ; }"
3291        complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
3292                || complete -o default -o nospace -F $wrapper $1
3293}
3294
3295# wrapper for backwards compatibility
3296_git ()
3297{
3298        __git_wrap__git_main
3299}
3300
3301# wrapper for backwards compatibility
3302_gitk ()
3303{
3304        __git_wrap__gitk_main
3305}
3306
3307__git_complete git __git_main
3308__git_complete gitk __gitk_main
3309
3310# The following are necessary only for Cygwin, and only are needed
3311# when the user has tab-completed the executable name and consequently
3312# included the '.exe' suffix.
3313#
3314if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
3315__git_complete git.exe __git_main
3316fi