dotfiles

Clean dotfiles
git clone git://git.dimitrijedobrota.com/dotfiles.git
Log | Files | Refs

git-completion (82153B)


      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 # If you have a command that is not part of git, but you would still
     33 # like completion, you can use __git_complete:
     34 #
     35 #   __git_complete gl git_log
     36 #
     37 # Or if it's a main command (i.e. git or gitk):
     38 #
     39 #   __git_complete gk gitk
     40 #
     41 # Compatible with bash 3.2.57.
     42 #
     43 # You can set the following environment variables to influence the behavior of
     44 # the completion routines:
     45 #
     46 #   GIT_COMPLETION_CHECKOUT_NO_GUESS
     47 #
     48 #     When set to "1", do not include "DWIM" suggestions in git-checkout
     49 #     and git-switch completion (e.g., completing "foo" when "origin/foo"
     50 #     exists).
     51 #
     52 #   GIT_COMPLETION_SHOW_ALL_COMMANDS
     53 #
     54 #     When set to "1" suggest all commands, including plumbing commands
     55 #     which are hidden by default (e.g. "cat-file" on "git ca<TAB>").
     56 #
     57 #   GIT_COMPLETION_SHOW_ALL
     58 #
     59 #     When set to "1" suggest all options, including options which are
     60 #     typically hidden (e.g. '--allow-empty' for 'git commit').
     61 #
     62 #   GIT_COMPLETION_IGNORE_CASE
     63 #
     64 #     When set, uses for-each-ref '--ignore-case' to find refs that match
     65 #     case insensitively, even on systems with case sensitive file systems
     66 #     (e.g., completing tag name "FOO" on "git checkout f<TAB>").
     67 
     68 case "$COMP_WORDBREAKS" in
     69 *:*) : great ;;
     70 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
     71 esac
     72 
     73 # Discovers the path to the git repository taking any '--git-dir=<path>' and
     74 # '-C <path>' options into account and stores it in the $__git_repo_path
     75 # variable.
     76 __git_find_repo_path ()
     77 {
     78 	if [ -n "${__git_repo_path-}" ]; then
     79 		# we already know where it is
     80 		return
     81 	fi
     82 
     83 	if [ -n "${__git_C_args-}" ]; then
     84 		__git_repo_path="$(git "${__git_C_args[@]}" \
     85 			${__git_dir:+--git-dir="$__git_dir"} \
     86 			rev-parse --absolute-git-dir 2>/dev/null)"
     87 	elif [ -n "${__git_dir-}" ]; then
     88 		test -d "$__git_dir" &&
     89 		__git_repo_path="$__git_dir"
     90 	elif [ -n "${GIT_DIR-}" ]; then
     91 		test -d "$GIT_DIR" &&
     92 		__git_repo_path="$GIT_DIR"
     93 	elif [ -d .git ]; then
     94 		__git_repo_path=.git
     95 	else
     96 		__git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
     97 	fi
     98 }
     99 
    100 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead
    101 # __gitdir accepts 0 or 1 arguments (i.e., location)
    102 # returns location of .git repo
    103 __gitdir ()
    104 {
    105 	if [ -z "${1-}" ]; then
    106 		__git_find_repo_path || return 1
    107 		echo "$__git_repo_path"
    108 	elif [ -d "$1/.git" ]; then
    109 		echo "$1/.git"
    110 	else
    111 		echo "$1"
    112 	fi
    113 }
    114 
    115 # Runs git with all the options given as argument, respecting any
    116 # '--git-dir=<path>' and '-C <path>' options present on the command line
    117 __git ()
    118 {
    119 	git ${__git_C_args:+"${__git_C_args[@]}"} \
    120 		${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
    121 }
    122 
    123 # Removes backslash escaping, single quotes and double quotes from a word,
    124 # stores the result in the variable $dequoted_word.
    125 # 1: The word to dequote.
    126 __git_dequote ()
    127 {
    128 	local rest="$1" len ch
    129 
    130 	dequoted_word=""
    131 
    132 	while test -n "$rest"; do
    133 		len=${#dequoted_word}
    134 		dequoted_word="$dequoted_word${rest%%[\\\'\"]*}"
    135 		rest="${rest:$((${#dequoted_word}-$len))}"
    136 
    137 		case "${rest:0:1}" in
    138 		\\)
    139 			ch="${rest:1:1}"
    140 			case "$ch" in
    141 			$'\n')
    142 				;;
    143 			*)
    144 				dequoted_word="$dequoted_word$ch"
    145 				;;
    146 			esac
    147 			rest="${rest:2}"
    148 			;;
    149 		\')
    150 			rest="${rest:1}"
    151 			len=${#dequoted_word}
    152 			dequoted_word="$dequoted_word${rest%%\'*}"
    153 			rest="${rest:$((${#dequoted_word}-$len+1))}"
    154 			;;
    155 		\")
    156 			rest="${rest:1}"
    157 			while test -n "$rest" ; do
    158 				len=${#dequoted_word}
    159 				dequoted_word="$dequoted_word${rest%%[\\\"]*}"
    160 				rest="${rest:$((${#dequoted_word}-$len))}"
    161 				case "${rest:0:1}" in
    162 				\\)
    163 					ch="${rest:1:1}"
    164 					case "$ch" in
    165 					\"|\\|\$|\`)
    166 						dequoted_word="$dequoted_word$ch"
    167 						;;
    168 					$'\n')
    169 						;;
    170 					*)
    171 						dequoted_word="$dequoted_word\\$ch"
    172 						;;
    173 					esac
    174 					rest="${rest:2}"
    175 					;;
    176 				\")
    177 					rest="${rest:1}"
    178 					break
    179 					;;
    180 				esac
    181 			done
    182 			;;
    183 		esac
    184 	done
    185 }
    186 
    187 # The following function is based on code from:
    188 #
    189 #   bash_completion - programmable completion functions for bash 3.2+
    190 #
    191 #   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
    192 #             © 2009-2010, Bash Completion Maintainers
    193 #                     <bash-completion-devel@lists.alioth.debian.org>
    194 #
    195 #   This program is free software; you can redistribute it and/or modify
    196 #   it under the terms of the GNU General Public License as published by
    197 #   the Free Software Foundation; either version 2, or (at your option)
    198 #   any later version.
    199 #
    200 #   This program is distributed in the hope that it will be useful,
    201 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
    202 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    203 #   GNU General Public License for more details.
    204 #
    205 #   You should have received a copy of the GNU General Public License
    206 #   along with this program; if not, see <http://www.gnu.org/licenses/>.
    207 #
    208 #   The latest version of this software can be obtained here:
    209 #
    210 #   http://bash-completion.alioth.debian.org/
    211 #
    212 #   RELEASE: 2.x
    213 
    214 # This function can be used to access a tokenized list of words
    215 # on the command line:
    216 #
    217 #	__git_reassemble_comp_words_by_ref '=:'
    218 #	if test "${words_[cword_-1]}" = -w
    219 #	then
    220 #		...
    221 #	fi
    222 #
    223 # The argument should be a collection of characters from the list of
    224 # word completion separators (COMP_WORDBREAKS) to treat as ordinary
    225 # characters.
    226 #
    227 # This is roughly equivalent to going back in time and setting
    228 # COMP_WORDBREAKS to exclude those characters.  The intent is to
    229 # make option types like --date=<type> and <rev>:<path> easy to
    230 # recognize by treating each shell word as a single token.
    231 #
    232 # It is best not to set COMP_WORDBREAKS directly because the value is
    233 # shared with other completion scripts.  By the time the completion
    234 # function gets called, COMP_WORDS has already been populated so local
    235 # changes to COMP_WORDBREAKS have no effect.
    236 #
    237 # Output: words_, cword_, cur_.
    238 
    239 __git_reassemble_comp_words_by_ref()
    240 {
    241 	local exclude i j first
    242 	# Which word separators to exclude?
    243 	exclude="${1//[^$COMP_WORDBREAKS]}"
    244 	cword_=$COMP_CWORD
    245 	if [ -z "$exclude" ]; then
    246 		words_=("${COMP_WORDS[@]}")
    247 		return
    248 	fi
    249 	# List of word completion separators has shrunk;
    250 	# re-assemble words to complete.
    251 	for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
    252 		# Append each nonempty word consisting of just
    253 		# word separator characters to the current word.
    254 		first=t
    255 		while
    256 			[ $i -gt 0 ] &&
    257 			[ -n "${COMP_WORDS[$i]}" ] &&
    258 			# word consists of excluded word separators
    259 			[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
    260 		do
    261 			# Attach to the previous token,
    262 			# unless the previous token is the command name.
    263 			if [ $j -ge 2 ] && [ -n "$first" ]; then
    264 				((j--))
    265 			fi
    266 			first=
    267 			words_[$j]=${words_[j]}${COMP_WORDS[i]}
    268 			if [ $i = $COMP_CWORD ]; then
    269 				cword_=$j
    270 			fi
    271 			if (($i < ${#COMP_WORDS[@]} - 1)); then
    272 				((i++))
    273 			else
    274 				# Done.
    275 				return
    276 			fi
    277 		done
    278 		words_[$j]=${words_[j]}${COMP_WORDS[i]}
    279 		if [ $i = $COMP_CWORD ]; then
    280 			cword_=$j
    281 		fi
    282 	done
    283 }
    284 
    285 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
    286 _get_comp_words_by_ref ()
    287 {
    288 	local exclude cur_ words_ cword_
    289 	if [ "$1" = "-n" ]; then
    290 		exclude=$2
    291 		shift 2
    292 	fi
    293 	__git_reassemble_comp_words_by_ref "$exclude"
    294 	cur_=${words_[cword_]}
    295 	while [ $# -gt 0 ]; do
    296 		case "$1" in
    297 		cur)
    298 			cur=$cur_
    299 			;;
    300 		prev)
    301 			prev=${words_[$cword_-1]}
    302 			;;
    303 		words)
    304 			words=("${words_[@]}")
    305 			;;
    306 		cword)
    307 			cword=$cword_
    308 			;;
    309 		esac
    310 		shift
    311 	done
    312 }
    313 fi
    314 
    315 # Fills the COMPREPLY array with prefiltered words without any additional
    316 # processing.
    317 # Callers must take care of providing only words that match the current word
    318 # to be completed and adding any prefix and/or suffix (trailing space!), if
    319 # necessary.
    320 # 1: List of newline-separated matching completion words, complete with
    321 #    prefix and suffix.
    322 __gitcomp_direct ()
    323 {
    324 	local IFS=$'\n'
    325 
    326 	COMPREPLY=($1)
    327 }
    328 
    329 # Similar to __gitcomp_direct, but appends to COMPREPLY instead.
    330 # Callers must take care of providing only words that match the current word
    331 # to be completed and adding any prefix and/or suffix (trailing space!), if
    332 # necessary.
    333 # 1: List of newline-separated matching completion words, complete with
    334 #    prefix and suffix.
    335 __gitcomp_direct_append ()
    336 {
    337 	local IFS=$'\n'
    338 
    339 	COMPREPLY+=($1)
    340 }
    341 
    342 __gitcompappend ()
    343 {
    344 	local x i=${#COMPREPLY[@]}
    345 	for x in $1; do
    346 		if [[ "$x" == "$3"* ]]; then
    347 			COMPREPLY[i++]="$2$x$4"
    348 		fi
    349 	done
    350 }
    351 
    352 __gitcompadd ()
    353 {
    354 	COMPREPLY=()
    355 	__gitcompappend "$@"
    356 }
    357 
    358 # Generates completion reply, appending a space to possible completion words,
    359 # if necessary.
    360 # It accepts 1 to 4 arguments:
    361 # 1: List of possible completion words.
    362 # 2: A prefix to be added to each possible completion word (optional).
    363 # 3: Generate possible completion matches for this word (optional).
    364 # 4: A suffix to be appended to each possible completion word (optional).
    365 __gitcomp ()
    366 {
    367 	local cur_="${3-$cur}"
    368 
    369 	case "$cur_" in
    370 	*=)
    371 		;;
    372 	--no-*)
    373 		local c i=0 IFS=$' \t\n'
    374 		for c in $1; do
    375 			if [[ $c == "--" ]]; then
    376 				continue
    377 			fi
    378 			c="$c${4-}"
    379 			if [[ $c == "$cur_"* ]]; then
    380 				case $c in
    381 				--*=|*.) ;;
    382 				*) c="$c " ;;
    383 				esac
    384 				COMPREPLY[i++]="${2-}$c"
    385 			fi
    386 		done
    387 		;;
    388 	*)
    389 		local c i=0 IFS=$' \t\n'
    390 		for c in $1; do
    391 			if [[ $c == "--" ]]; then
    392 				c="--no-...${4-}"
    393 				if [[ $c == "$cur_"* ]]; then
    394 					COMPREPLY[i++]="${2-}$c "
    395 				fi
    396 				break
    397 			fi
    398 			c="$c${4-}"
    399 			if [[ $c == "$cur_"* ]]; then
    400 				case $c in
    401 				*=|*.) ;;
    402 				*) c="$c " ;;
    403 				esac
    404 				COMPREPLY[i++]="${2-}$c"
    405 			fi
    406 		done
    407 		;;
    408 	esac
    409 }
    410 
    411 # Clear the variables caching builtins' options when (re-)sourcing
    412 # the completion script.
    413 if [[ -n ${ZSH_VERSION-} ]]; then
    414 	unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null
    415 else
    416 	unset $(compgen -v __gitcomp_builtin_)
    417 fi
    418 
    419 # This function is equivalent to
    420 #
    421 #    __gitcomp "$(git xxx --git-completion-helper) ..."
    422 #
    423 # except that the output is cached. Accept 1-3 arguments:
    424 # 1: the git command to execute, this is also the cache key
    425 # 2: extra options to be added on top (e.g. negative forms)
    426 # 3: options to be excluded
    427 __gitcomp_builtin ()
    428 {
    429 	# spaces must be replaced with underscore for multi-word
    430 	# commands, e.g. "git remote add" becomes remote_add.
    431 	local cmd="$1"
    432 	local incl="${2-}"
    433 	local excl="${3-}"
    434 
    435 	local var=__gitcomp_builtin_"${cmd//-/_}"
    436 	local options
    437 	eval "options=\${$var-}"
    438 
    439 	if [ -z "$options" ]; then
    440 		local completion_helper
    441 		if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then
    442 			completion_helper="--git-completion-helper-all"
    443 		else
    444 			completion_helper="--git-completion-helper"
    445 		fi
    446 		# leading and trailing spaces are significant to make
    447 		# option removal work correctly.
    448 		options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return
    449 
    450 		for i in $excl; do
    451 			options="${options/ $i / }"
    452 		done
    453 		eval "$var=\"$options\""
    454 	fi
    455 
    456 	__gitcomp "$options"
    457 }
    458 
    459 # Variation of __gitcomp_nl () that appends to the existing list of
    460 # completion candidates, COMPREPLY.
    461 __gitcomp_nl_append ()
    462 {
    463 	local IFS=$'\n'
    464 	__gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
    465 }
    466 
    467 # Generates completion reply from newline-separated possible completion words
    468 # by appending a space to all of them.
    469 # It accepts 1 to 4 arguments:
    470 # 1: List of possible completion words, separated by a single newline.
    471 # 2: A prefix to be added to each possible completion word (optional).
    472 # 3: Generate possible completion matches for this word (optional).
    473 # 4: A suffix to be appended to each possible completion word instead of
    474 #    the default space (optional).  If specified but empty, nothing is
    475 #    appended.
    476 __gitcomp_nl ()
    477 {
    478 	COMPREPLY=()
    479 	__gitcomp_nl_append "$@"
    480 }
    481 
    482 # Fills the COMPREPLY array with prefiltered paths without any additional
    483 # processing.
    484 # Callers must take care of providing only paths that match the current path
    485 # to be completed and adding any prefix path components, if necessary.
    486 # 1: List of newline-separated matching paths, complete with all prefix
    487 #    path components.
    488 __gitcomp_file_direct ()
    489 {
    490 	local IFS=$'\n'
    491 
    492 	COMPREPLY=($1)
    493 
    494 	# use a hack to enable file mode in bash < 4
    495 	compopt -o filenames +o nospace 2>/dev/null ||
    496 	compgen -f /non-existing-dir/ >/dev/null ||
    497 	true
    498 }
    499 
    500 # Generates completion reply with compgen from newline-separated possible
    501 # completion filenames.
    502 # It accepts 1 to 3 arguments:
    503 # 1: List of possible completion filenames, separated by a single newline.
    504 # 2: A directory prefix to be added to each possible completion filename
    505 #    (optional).
    506 # 3: Generate possible completion matches for this word (optional).
    507 __gitcomp_file ()
    508 {
    509 	local IFS=$'\n'
    510 
    511 	# XXX does not work when the directory prefix contains a tilde,
    512 	# since tilde expansion is not applied.
    513 	# This means that COMPREPLY will be empty and Bash default
    514 	# completion will be used.
    515 	__gitcompadd "$1" "${2-}" "${3-$cur}" ""
    516 
    517 	# use a hack to enable file mode in bash < 4
    518 	compopt -o filenames +o nospace 2>/dev/null ||
    519 	compgen -f /non-existing-dir/ >/dev/null ||
    520 	true
    521 }
    522 
    523 # Execute 'git ls-files', unless the --committable option is specified, in
    524 # which case it runs 'git diff-index' to find out the files that can be
    525 # committed.  It return paths relative to the directory specified in the first
    526 # argument, and using the options specified in the second argument.
    527 __git_ls_files_helper ()
    528 {
    529 	if [ "$2" = "--committable" ]; then
    530 		__git -C "$1" -c core.quotePath=false diff-index \
    531 			--name-only --relative HEAD -- "${3//\\/\\\\}*"
    532 	else
    533 		# NOTE: $2 is not quoted in order to support multiple options
    534 		__git -C "$1" -c core.quotePath=false ls-files \
    535 			--exclude-standard $2 -- "${3//\\/\\\\}*"
    536 	fi
    537 }
    538 
    539 
    540 # __git_index_files accepts 1 or 2 arguments:
    541 # 1: Options to pass to ls-files (required).
    542 # 2: A directory path (optional).
    543 #    If provided, only files within the specified directory are listed.
    544 #    Sub directories are never recursed.  Path must have a trailing
    545 #    slash.
    546 # 3: List only paths matching this path component (optional).
    547 __git_index_files ()
    548 {
    549 	local root="$2" match="$3"
    550 
    551 	__git_ls_files_helper "$root" "$1" "${match:-?}" |
    552 	awk -F / -v pfx="${2//\\/\\\\}" '{
    553 		paths[$1] = 1
    554 	}
    555 	END {
    556 		for (p in paths) {
    557 			if (substr(p, 1, 1) != "\"") {
    558 				# No special characters, easy!
    559 				print pfx p
    560 				continue
    561 			}
    562 
    563 			# The path is quoted.
    564 			p = dequote(p)
    565 			if (p == "")
    566 				continue
    567 
    568 			# Even when a directory name itself does not contain
    569 			# any special characters, it will still be quoted if
    570 			# any of its (stripped) trailing path components do.
    571 			# Because of this we may have seen the same directory
    572 			# both quoted and unquoted.
    573 			if (p in paths)
    574 				# We have seen the same directory unquoted,
    575 				# skip it.
    576 				continue
    577 			else
    578 				print pfx p
    579 		}
    580 	}
    581 	function dequote(p,    bs_idx, out, esc, esc_idx, dec) {
    582 		# Skip opening double quote.
    583 		p = substr(p, 2)
    584 
    585 		# Interpret backslash escape sequences.
    586 		while ((bs_idx = index(p, "\\")) != 0) {
    587 			out = out substr(p, 1, bs_idx - 1)
    588 			esc = substr(p, bs_idx + 1, 1)
    589 			p = substr(p, bs_idx + 2)
    590 
    591 			if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
    592 				# C-style one-character escape sequence.
    593 				out = out substr("\a\b\t\v\f\r\"\\",
    594 						 esc_idx, 1)
    595 			} else if (esc == "n") {
    596 				# Uh-oh, a newline character.
    597 				# We cannot reliably put a pathname
    598 				# containing a newline into COMPREPLY,
    599 				# and the newline would create a mess.
    600 				# Skip this path.
    601 				return ""
    602 			} else {
    603 				# Must be a \nnn octal value, then.
    604 				dec = esc             * 64 + \
    605 				      substr(p, 1, 1) * 8  + \
    606 				      substr(p, 2, 1)
    607 				out = out sprintf("%c", dec)
    608 				p = substr(p, 3)
    609 			}
    610 		}
    611 		# Drop closing double quote, if there is one.
    612 		# (There is not any if this is a directory, as it was
    613 		# already stripped with the trailing path components.)
    614 		if (substr(p, length(p), 1) == "\"")
    615 			out = out substr(p, 1, length(p) - 1)
    616 		else
    617 			out = out p
    618 
    619 		return out
    620 	}'
    621 }
    622 
    623 # __git_complete_index_file requires 1 argument:
    624 # 1: the options to pass to ls-file
    625 #
    626 # The exception is --committable, which finds the files appropriate commit.
    627 __git_complete_index_file ()
    628 {
    629 	local dequoted_word pfx="" cur_
    630 
    631 	__git_dequote "$cur"
    632 
    633 	case "$dequoted_word" in
    634 	?*/*)
    635 		pfx="${dequoted_word%/*}/"
    636 		cur_="${dequoted_word##*/}"
    637 		;;
    638 	*)
    639 		cur_="$dequoted_word"
    640 	esac
    641 
    642 	__gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")"
    643 }
    644 
    645 # Lists branches from the local repository.
    646 # 1: A prefix to be added to each listed branch (optional).
    647 # 2: List only branches matching this word (optional; list all branches if
    648 #    unset or empty).
    649 # 3: A suffix to be appended to each listed branch (optional).
    650 __git_heads ()
    651 {
    652 	local pfx="${1-}" cur_="${2-}" sfx="${3-}"
    653 
    654 	__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
    655 			${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    656 			"refs/heads/$cur_*" "refs/heads/$cur_*/**"
    657 }
    658 
    659 # Lists branches from remote repositories.
    660 # 1: A prefix to be added to each listed branch (optional).
    661 # 2: List only branches matching this word (optional; list all branches if
    662 #    unset or empty).
    663 # 3: A suffix to be appended to each listed branch (optional).
    664 __git_remote_heads ()
    665 {
    666 	local pfx="${1-}" cur_="${2-}" sfx="${3-}"
    667 
    668 	__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
    669 			${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    670 			"refs/remotes/$cur_*" "refs/remotes/$cur_*/**"
    671 }
    672 
    673 # Lists tags from the local repository.
    674 # Accepts the same positional parameters as __git_heads() above.
    675 __git_tags ()
    676 {
    677 	local pfx="${1-}" cur_="${2-}" sfx="${3-}"
    678 
    679 	__git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
    680 			${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    681 			"refs/tags/$cur_*" "refs/tags/$cur_*/**"
    682 }
    683 
    684 # List unique branches from refs/remotes used for 'git checkout' and 'git
    685 # switch' tracking DWIMery.
    686 # 1: A prefix to be added to each listed branch (optional)
    687 # 2: List only branches matching this word (optional; list all branches if
    688 #    unset or empty).
    689 # 3: A suffix to be appended to each listed branch (optional).
    690 __git_dwim_remote_heads ()
    691 {
    692 	local pfx="${1-}" cur_="${2-}" sfx="${3-}"
    693 	local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
    694 
    695 	# employ the heuristic used by git checkout and git switch
    696 	# Try to find a remote branch that cur_es the completion word
    697 	# but only output if the branch name is unique
    698 	__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
    699 		--sort="refname:strip=3" \
    700 		${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    701 		"refs/remotes/*/$cur_*" "refs/remotes/*/$cur_*/**" | \
    702 	uniq -u
    703 }
    704 
    705 # Lists refs from the local (by default) or from a remote repository.
    706 # It accepts 0, 1 or 2 arguments:
    707 # 1: The remote to list refs from (optional; ignored, if set but empty).
    708 #    Can be the name of a configured remote, a path, or a URL.
    709 # 2: In addition to local refs, list unique branches from refs/remotes/ for
    710 #    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
    711 # 3: A prefix to be added to each listed ref (optional).
    712 # 4: List only refs matching this word (optional; list all refs if unset or
    713 #    empty).
    714 # 5: A suffix to be appended to each listed ref (optional; ignored, if set
    715 #    but empty).
    716 #
    717 # Use __git_complete_refs() instead.
    718 __git_refs ()
    719 {
    720 	local i hash dir track="${2-}"
    721 	local list_refs_from=path remote="${1-}"
    722 	local format refs
    723 	local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
    724 	local match="${4-}"
    725 	local umatch="${4-}"
    726 	local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
    727 
    728 	__git_find_repo_path
    729 	dir="$__git_repo_path"
    730 
    731 	if [ -z "$remote" ]; then
    732 		if [ -z "$dir" ]; then
    733 			return
    734 		fi
    735 	else
    736 		if __git_is_configured_remote "$remote"; then
    737 			# configured remote takes precedence over a
    738 			# local directory with the same name
    739 			list_refs_from=remote
    740 		elif [ -d "$remote/.git" ]; then
    741 			dir="$remote/.git"
    742 		elif [ -d "$remote" ]; then
    743 			dir="$remote"
    744 		else
    745 			list_refs_from=url
    746 		fi
    747 	fi
    748 
    749 	if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1"
    750 	then
    751 		# uppercase with tr instead of ${match,^^} for bash 3.2 compatibility
    752 		umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match")
    753 	fi
    754 
    755 	if [ "$list_refs_from" = path ]; then
    756 		if [[ "$cur_" == ^* ]]; then
    757 			pfx="$pfx^"
    758 			fer_pfx="$fer_pfx^"
    759 			cur_=${cur_#^}
    760 			match=${match#^}
    761 			umatch=${umatch#^}
    762 		fi
    763 		case "$cur_" in
    764 		refs|refs/*)
    765 			format="refname"
    766 			refs=("$match*" "$match*/**")
    767 			track=""
    768 			;;
    769 		*)
    770 			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do
    771 				case "$i" in
    772 				$match*|$umatch*)
    773 					if [ -e "$dir/$i" ]; then
    774 						echo "$pfx$i$sfx"
    775 					fi
    776 					;;
    777 				esac
    778 			done
    779 			format="refname:strip=2"
    780 			refs=("refs/tags/$match*" "refs/tags/$match*/**"
    781 				"refs/heads/$match*" "refs/heads/$match*/**"
    782 				"refs/remotes/$match*" "refs/remotes/$match*/**")
    783 			;;
    784 		esac
    785 		__git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
    786 			${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    787 			"${refs[@]}"
    788 		if [ -n "$track" ]; then
    789 			__git_dwim_remote_heads "$pfx" "$match" "$sfx"
    790 		fi
    791 		return
    792 	fi
    793 	case "$cur_" in
    794 	refs|refs/*)
    795 		__git ls-remote "$remote" "$match*" | \
    796 		while read -r hash i; do
    797 			case "$i" in
    798 			*^{}) ;;
    799 			*) echo "$pfx$i$sfx" ;;
    800 			esac
    801 		done
    802 		;;
    803 	*)
    804 		if [ "$list_refs_from" = remote ]; then
    805 			case "HEAD" in
    806 			$match*|$umatch*)	echo "${pfx}HEAD$sfx" ;;
    807 			esac
    808 			__git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
    809 				${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \
    810 				"refs/remotes/$remote/$match*" \
    811 				"refs/remotes/$remote/$match*/**"
    812 		else
    813 			local query_symref
    814 			case "HEAD" in
    815 			$match*|$umatch*)	query_symref="HEAD" ;;
    816 			esac
    817 			__git ls-remote "$remote" $query_symref \
    818 				"refs/tags/$match*" "refs/heads/$match*" \
    819 				"refs/remotes/$match*" |
    820 			while read -r hash i; do
    821 				case "$i" in
    822 				*^{})	;;
    823 				refs/*)	echo "$pfx${i#refs/*/}$sfx" ;;
    824 				*)	echo "$pfx$i$sfx" ;;  # symbolic refs
    825 				esac
    826 			done
    827 		fi
    828 		;;
    829 	esac
    830 }
    831 
    832 # Completes refs, short and long, local and remote, symbolic and pseudo.
    833 #
    834 # Usage: __git_complete_refs [<option>]...
    835 # --remote=<remote>: The remote to list refs from, can be the name of a
    836 #                    configured remote, a path, or a URL.
    837 # --dwim: List unique remote branches for 'git switch's tracking DWIMery.
    838 # --pfx=<prefix>: A prefix to be added to each ref.
    839 # --cur=<word>: The current ref to be completed.  Defaults to the current
    840 #               word to be completed.
    841 # --sfx=<suffix>: A suffix to be appended to each ref instead of the default
    842 #                 space.
    843 # --mode=<mode>: What set of refs to complete, one of 'refs' (the default) to
    844 #                complete all refs, 'heads' to complete only branches, or
    845 #                'remote-heads' to complete only remote branches. Note that
    846 #                --remote is only compatible with --mode=refs.
    847 __git_complete_refs ()
    848 {
    849 	local remote= dwim= pfx= cur_="$cur" sfx=" " mode="refs"
    850 
    851 	while test $# != 0; do
    852 		case "$1" in
    853 		--remote=*)	remote="${1##--remote=}" ;;
    854 		--dwim)		dwim="yes" ;;
    855 		# --track is an old spelling of --dwim
    856 		--track)	dwim="yes" ;;
    857 		--pfx=*)	pfx="${1##--pfx=}" ;;
    858 		--cur=*)	cur_="${1##--cur=}" ;;
    859 		--sfx=*)	sfx="${1##--sfx=}" ;;
    860 		--mode=*)	mode="${1##--mode=}" ;;
    861 		*)		return 1 ;;
    862 		esac
    863 		shift
    864 	done
    865 
    866 	# complete references based on the specified mode
    867 	case "$mode" in
    868 		refs)
    869 			__gitcomp_direct "$(__git_refs "$remote" "" "$pfx" "$cur_" "$sfx")" ;;
    870 		heads)
    871 			__gitcomp_direct "$(__git_heads "$pfx" "$cur_" "$sfx")" ;;
    872 		remote-heads)
    873 			__gitcomp_direct "$(__git_remote_heads "$pfx" "$cur_" "$sfx")" ;;
    874 		*)
    875 			return 1 ;;
    876 	esac
    877 
    878 	# Append DWIM remote branch names if requested
    879 	if [ "$dwim" = "yes" ]; then
    880 		__gitcomp_direct_append "$(__git_dwim_remote_heads "$pfx" "$cur_" "$sfx")"
    881 	fi
    882 }
    883 
    884 # __git_refs2 requires 1 argument (to pass to __git_refs)
    885 # Deprecated: use __git_complete_fetch_refspecs() instead.
    886 __git_refs2 ()
    887 {
    888 	local i
    889 	for i in $(__git_refs "$1"); do
    890 		echo "$i:$i"
    891 	done
    892 }
    893 
    894 # Completes refspecs for fetching from a remote repository.
    895 # 1: The remote repository.
    896 # 2: A prefix to be added to each listed refspec (optional).
    897 # 3: The ref to be completed as a refspec instead of the current word to be
    898 #    completed (optional)
    899 # 4: A suffix to be appended to each listed refspec instead of the default
    900 #    space (optional).
    901 __git_complete_fetch_refspecs ()
    902 {
    903 	local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
    904 
    905 	__gitcomp_direct "$(
    906 		for i in $(__git_refs "$remote" "" "" "$cur_") ; do
    907 			echo "$pfx$i:$i$sfx"
    908 		done
    909 		)"
    910 }
    911 
    912 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
    913 __git_refs_remotes ()
    914 {
    915 	local i hash
    916 	__git ls-remote "$1" 'refs/heads/*' | \
    917 	while read -r hash i; do
    918 		echo "$i:refs/remotes/$1/${i#refs/heads/}"
    919 	done
    920 }
    921 
    922 __git_remotes ()
    923 {
    924 	__git_find_repo_path
    925 	test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
    926 	__git remote
    927 }
    928 
    929 # Returns true if $1 matches the name of a configured remote, false otherwise.
    930 __git_is_configured_remote ()
    931 {
    932 	local remote
    933 	for remote in $(__git_remotes); do
    934 		if [ "$remote" = "$1" ]; then
    935 			return 0
    936 		fi
    937 	done
    938 	return 1
    939 }
    940 
    941 __git_list_merge_strategies ()
    942 {
    943 	LANG=C LC_ALL=C git merge -s help 2>&1 |
    944 	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
    945 		s/\.$//
    946 		s/.*://
    947 		s/^[ 	]*//
    948 		s/[ 	]*$//
    949 		p
    950 	}'
    951 }
    952 
    953 __git_merge_strategies=
    954 # 'git merge -s help' (and thus detection of the merge strategy
    955 # list) fails, unfortunately, if run outside of any git working
    956 # tree.  __git_merge_strategies is set to the empty string in
    957 # that case, and the detection will be repeated the next time it
    958 # is needed.
    959 __git_compute_merge_strategies ()
    960 {
    961 	test -n "$__git_merge_strategies" ||
    962 	__git_merge_strategies=$(__git_list_merge_strategies)
    963 }
    964 
    965 __git_merge_strategy_options="ours theirs subtree subtree= patience
    966 	histogram diff-algorithm= ignore-space-change ignore-all-space
    967 	ignore-space-at-eol renormalize no-renormalize no-renames
    968 	find-renames find-renames= rename-threshold="
    969 
    970 __git_complete_revlist_file ()
    971 {
    972 	local dequoted_word pfx ls ref cur_="$cur"
    973 	case "$cur_" in
    974 	*..?*:*)
    975 		return
    976 		;;
    977 	?*:*)
    978 		ref="${cur_%%:*}"
    979 		cur_="${cur_#*:}"
    980 
    981 		__git_dequote "$cur_"
    982 
    983 		case "$dequoted_word" in
    984 		?*/*)
    985 			pfx="${dequoted_word%/*}"
    986 			cur_="${dequoted_word##*/}"
    987 			ls="$ref:$pfx"
    988 			pfx="$pfx/"
    989 			;;
    990 		*)
    991 			cur_="$dequoted_word"
    992 			ls="$ref"
    993 			;;
    994 		esac
    995 
    996 		case "$COMP_WORDBREAKS" in
    997 		*:*) : great ;;
    998 		*)   pfx="$ref:$pfx" ;;
    999 		esac
   1000 
   1001 		__gitcomp_file "$(__git ls-tree "$ls" \
   1002 				| sed 's/^.*	//
   1003 				       s/$//')" \
   1004 			"$pfx" "$cur_"
   1005 		;;
   1006 	*...*)
   1007 		pfx="${cur_%...*}..."
   1008 		cur_="${cur_#*...}"
   1009 		__git_complete_refs --pfx="$pfx" --cur="$cur_"
   1010 		;;
   1011 	*..*)
   1012 		pfx="${cur_%..*}.."
   1013 		cur_="${cur_#*..}"
   1014 		__git_complete_refs --pfx="$pfx" --cur="$cur_"
   1015 		;;
   1016 	*)
   1017 		__git_complete_refs
   1018 		;;
   1019 	esac
   1020 }
   1021 
   1022 __git_complete_file ()
   1023 {
   1024 	__git_complete_revlist_file
   1025 }
   1026 
   1027 __git_complete_revlist ()
   1028 {
   1029 	__git_complete_revlist_file
   1030 }
   1031 
   1032 __git_complete_remote_or_refspec ()
   1033 {
   1034 	local cur_="$cur" cmd="${words[__git_cmd_idx]}"
   1035 	local i c=$((__git_cmd_idx+1)) remote="" pfx="" lhs=1 no_complete_refspec=0
   1036 	if [ "$cmd" = "remote" ]; then
   1037 		((c++))
   1038 	fi
   1039 	while [ $c -lt $cword ]; do
   1040 		i="${words[c]}"
   1041 		case "$i" in
   1042 		--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
   1043 		-d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
   1044 		--all)
   1045 			case "$cmd" in
   1046 			push) no_complete_refspec=1 ;;
   1047 			fetch)
   1048 				return
   1049 				;;
   1050 			*) ;;
   1051 			esac
   1052 			;;
   1053 		--multiple) no_complete_refspec=1; break ;;
   1054 		-*) ;;
   1055 		*) remote="$i"; break ;;
   1056 		esac
   1057 		((c++))
   1058 	done
   1059 	if [ -z "$remote" ]; then
   1060 		__gitcomp_nl "$(__git_remotes)"
   1061 		return
   1062 	fi
   1063 	if [ $no_complete_refspec = 1 ]; then
   1064 		return
   1065 	fi
   1066 	[ "$remote" = "." ] && remote=
   1067 	case "$cur_" in
   1068 	*:*)
   1069 		case "$COMP_WORDBREAKS" in
   1070 		*:*) : great ;;
   1071 		*)   pfx="${cur_%%:*}:" ;;
   1072 		esac
   1073 		cur_="${cur_#*:}"
   1074 		lhs=0
   1075 		;;
   1076 	+*)
   1077 		pfx="+"
   1078 		cur_="${cur_#+}"
   1079 		;;
   1080 	esac
   1081 	case "$cmd" in
   1082 	fetch)
   1083 		if [ $lhs = 1 ]; then
   1084 			__git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
   1085 		else
   1086 			__git_complete_refs --pfx="$pfx" --cur="$cur_"
   1087 		fi
   1088 		;;
   1089 	pull|remote)
   1090 		if [ $lhs = 1 ]; then
   1091 			__git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
   1092 		else
   1093 			__git_complete_refs --pfx="$pfx" --cur="$cur_"
   1094 		fi
   1095 		;;
   1096 	push)
   1097 		if [ $lhs = 1 ]; then
   1098 			__git_complete_refs --pfx="$pfx" --cur="$cur_"
   1099 		else
   1100 			__git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
   1101 		fi
   1102 		;;
   1103 	esac
   1104 }
   1105 
   1106 __git_complete_strategy ()
   1107 {
   1108 	__git_compute_merge_strategies
   1109 	case "$prev" in
   1110 	-s|--strategy)
   1111 		__gitcomp "$__git_merge_strategies"
   1112 		return 0
   1113 		;;
   1114 	-X)
   1115 		__gitcomp "$__git_merge_strategy_options"
   1116 		return 0
   1117 		;;
   1118 	esac
   1119 	case "$cur" in
   1120 	--strategy=*)
   1121 		__gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
   1122 		return 0
   1123 		;;
   1124 	--strategy-option=*)
   1125 		__gitcomp "$__git_merge_strategy_options" "" "${cur##--strategy-option=}"
   1126 		return 0
   1127 		;;
   1128 	esac
   1129 	return 1
   1130 }
   1131 
   1132 __git_all_commands=
   1133 __git_compute_all_commands ()
   1134 {
   1135 	test -n "$__git_all_commands" ||
   1136 	__git_all_commands=$(__git --list-cmds=main,others,alias,nohelpers)
   1137 }
   1138 
   1139 # Lists all set config variables starting with the given section prefix,
   1140 # with the prefix removed.
   1141 __git_get_config_variables ()
   1142 {
   1143 	local section="$1" i IFS=$'\n'
   1144 	for i in $(__git config --name-only --get-regexp "^$section\..*"); do
   1145 		echo "${i#$section.}"
   1146 	done
   1147 }
   1148 
   1149 __git_pretty_aliases ()
   1150 {
   1151 	__git_get_config_variables "pretty"
   1152 }
   1153 
   1154 # __git_aliased_command requires 1 argument
   1155 __git_aliased_command ()
   1156 {
   1157 	local cur=$1 last list= word cmdline
   1158 
   1159 	while [[ -n "$cur" ]]; do
   1160 		if [[ "$list" == *" $cur "* ]]; then
   1161 			# loop detected
   1162 			return
   1163 		fi
   1164 
   1165 		cmdline=$(__git config --get "alias.$cur")
   1166 		list=" $cur $list"
   1167 		last=$cur
   1168 		cur=
   1169 
   1170 		for word in $cmdline; do
   1171 			case "$word" in
   1172 			\!gitk|gitk)
   1173 				cur="gitk"
   1174 				break
   1175 				;;
   1176 			\!*)	: shell command alias ;;
   1177 			-*)	: option ;;
   1178 			*=*)	: setting env ;;
   1179 			git)	: git itself ;;
   1180 			\(\))   : skip parens of shell function definition ;;
   1181 			{)	: skip start of shell helper function ;;
   1182 			:)	: skip null command ;;
   1183 			\'*)	: skip opening quote after sh -c ;;
   1184 			*)
   1185 				cur="$word"
   1186 				break
   1187 			esac
   1188 		done
   1189 	done
   1190 
   1191 	cur=$last
   1192 	if [[ "$cur" != "$1" ]]; then
   1193 		echo "$cur"
   1194 	fi
   1195 }
   1196 
   1197 # Check whether one of the given words is present on the command line,
   1198 # and print the first word found.
   1199 #
   1200 # Usage: __git_find_on_cmdline [<option>]... "<wordlist>"
   1201 # --show-idx: Optionally show the index of the found word in the $words array.
   1202 __git_find_on_cmdline ()
   1203 {
   1204 	local word c="$__git_cmd_idx" show_idx
   1205 
   1206 	while test $# -gt 1; do
   1207 		case "$1" in
   1208 		--show-idx)	show_idx=y ;;
   1209 		*)		return 1 ;;
   1210 		esac
   1211 		shift
   1212 	done
   1213 	local wordlist="$1"
   1214 
   1215 	while [ $c -lt $cword ]; do
   1216 		for word in $wordlist; do
   1217 			if [ "$word" = "${words[c]}" ]; then
   1218 				if [ -n "${show_idx-}" ]; then
   1219 					echo "$c $word"
   1220 				else
   1221 					echo "$word"
   1222 				fi
   1223 				return
   1224 			fi
   1225 		done
   1226 		((c++))
   1227 	done
   1228 }
   1229 
   1230 # Similar to __git_find_on_cmdline, except that it loops backwards and thus
   1231 # prints the *last* word found. Useful for finding which of two options that
   1232 # supersede each other came last, such as "--guess" and "--no-guess".
   1233 #
   1234 # Usage: __git_find_last_on_cmdline [<option>]... "<wordlist>"
   1235 # --show-idx: Optionally show the index of the found word in the $words array.
   1236 __git_find_last_on_cmdline ()
   1237 {
   1238 	local word c=$cword show_idx
   1239 
   1240 	while test $# -gt 1; do
   1241 		case "$1" in
   1242 		--show-idx)	show_idx=y ;;
   1243 		*)		return 1 ;;
   1244 		esac
   1245 		shift
   1246 	done
   1247 	local wordlist="$1"
   1248 
   1249 	while [ $c -gt "$__git_cmd_idx" ]; do
   1250 		((c--))
   1251 		for word in $wordlist; do
   1252 			if [ "$word" = "${words[c]}" ]; then
   1253 				if [ -n "$show_idx" ]; then
   1254 					echo "$c $word"
   1255 				else
   1256 					echo "$word"
   1257 				fi
   1258 				return
   1259 			fi
   1260 		done
   1261 	done
   1262 }
   1263 
   1264 # Echo the value of an option set on the command line or config
   1265 #
   1266 # $1: short option name
   1267 # $2: long option name including =
   1268 # $3: list of possible values
   1269 # $4: config string (optional)
   1270 #
   1271 # example:
   1272 # result="$(__git_get_option_value "-d" "--do-something=" \
   1273 #     "yes no" "core.doSomething")"
   1274 #
   1275 # result is then either empty (no option set) or "yes" or "no"
   1276 #
   1277 # __git_get_option_value requires 3 arguments
   1278 __git_get_option_value ()
   1279 {
   1280 	local c short_opt long_opt val
   1281 	local result= values config_key word
   1282 
   1283 	short_opt="$1"
   1284 	long_opt="$2"
   1285 	values="$3"
   1286 	config_key="$4"
   1287 
   1288 	((c = $cword - 1))
   1289 	while [ $c -ge 0 ]; do
   1290 		word="${words[c]}"
   1291 		for val in $values; do
   1292 			if [ "$short_opt$val" = "$word" ] ||
   1293 			   [ "$long_opt$val"  = "$word" ]; then
   1294 				result="$val"
   1295 				break 2
   1296 			fi
   1297 		done
   1298 		((c--))
   1299 	done
   1300 
   1301 	if [ -n "$config_key" ] && [ -z "$result" ]; then
   1302 		result="$(__git config "$config_key")"
   1303 	fi
   1304 
   1305 	echo "$result"
   1306 }
   1307 
   1308 __git_has_doubledash ()
   1309 {
   1310 	local c=1
   1311 	while [ $c -lt $cword ]; do
   1312 		if [ "--" = "${words[c]}" ]; then
   1313 			return 0
   1314 		fi
   1315 		((c++))
   1316 	done
   1317 	return 1
   1318 }
   1319 
   1320 # Try to count non option arguments passed on the command line for the
   1321 # specified git command.
   1322 # When options are used, it is necessary to use the special -- option to
   1323 # tell the implementation were non option arguments begin.
   1324 # XXX this can not be improved, since options can appear everywhere, as
   1325 # an example:
   1326 #	git mv x -n y
   1327 #
   1328 # __git_count_arguments requires 1 argument: the git command executed.
   1329 __git_count_arguments ()
   1330 {
   1331 	local word i c=0
   1332 
   1333 	# Skip "git" (first argument)
   1334 	for ((i=$__git_cmd_idx; i < ${#words[@]}; i++)); do
   1335 		word="${words[i]}"
   1336 
   1337 		case "$word" in
   1338 			--)
   1339 				# Good; we can assume that the following are only non
   1340 				# option arguments.
   1341 				((c = 0))
   1342 				;;
   1343 			"$1")
   1344 				# Skip the specified git command and discard git
   1345 				# main options
   1346 				((c = 0))
   1347 				;;
   1348 			?*)
   1349 				((c++))
   1350 				;;
   1351 		esac
   1352 	done
   1353 
   1354 	printf "%d" $c
   1355 }
   1356 
   1357 __git_whitespacelist="nowarn warn error error-all fix"
   1358 __git_patchformat="mbox stgit stgit-series hg mboxrd"
   1359 __git_showcurrentpatch="diff raw"
   1360 __git_am_inprogress_options="--skip --continue --resolved --abort --quit --show-current-patch"
   1361 __git_quoted_cr="nowarn warn strip"
   1362 
   1363 _git_am ()
   1364 {
   1365 	__git_find_repo_path
   1366 	if [ -d "$__git_repo_path"/rebase-apply ]; then
   1367 		__gitcomp "$__git_am_inprogress_options"
   1368 		return
   1369 	fi
   1370 	case "$cur" in
   1371 	--whitespace=*)
   1372 		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
   1373 		return
   1374 		;;
   1375 	--patch-format=*)
   1376 		__gitcomp "$__git_patchformat" "" "${cur##--patch-format=}"
   1377 		return
   1378 		;;
   1379 	--show-current-patch=*)
   1380 		__gitcomp "$__git_showcurrentpatch" "" "${cur##--show-current-patch=}"
   1381 		return
   1382 		;;
   1383 	--quoted-cr=*)
   1384 		__gitcomp "$__git_quoted_cr" "" "${cur##--quoted-cr=}"
   1385 		return
   1386 		;;
   1387 	--*)
   1388 		__gitcomp_builtin am "" \
   1389 			"$__git_am_inprogress_options"
   1390 		return
   1391 	esac
   1392 }
   1393 
   1394 _git_apply ()
   1395 {
   1396 	case "$cur" in
   1397 	--whitespace=*)
   1398 		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
   1399 		return
   1400 		;;
   1401 	--*)
   1402 		__gitcomp_builtin apply
   1403 		return
   1404 	esac
   1405 }
   1406 
   1407 _git_add ()
   1408 {
   1409 	case "$cur" in
   1410 	--chmod=*)
   1411 		__gitcomp "+x -x" "" "${cur##--chmod=}"
   1412 		return
   1413 		;;
   1414 	--*)
   1415 		__gitcomp_builtin add
   1416 		return
   1417 	esac
   1418 
   1419 	local complete_opt="--others --modified --directory --no-empty-directory"
   1420 	if test -n "$(__git_find_on_cmdline "-u --update")"
   1421 	then
   1422 		complete_opt="--modified"
   1423 	fi
   1424 	__git_complete_index_file "$complete_opt"
   1425 }
   1426 
   1427 _git_archive ()
   1428 {
   1429 	case "$cur" in
   1430 	--format=*)
   1431 		__gitcomp "$(git archive --list)" "" "${cur##--format=}"
   1432 		return
   1433 		;;
   1434 	--remote=*)
   1435 		__gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
   1436 		return
   1437 		;;
   1438 	--*)
   1439 		__gitcomp_builtin archive "--format= --list --verbose --prefix= --worktree-attributes"
   1440 		return
   1441 		;;
   1442 	esac
   1443 	__git_complete_file
   1444 }
   1445 
   1446 _git_bisect ()
   1447 {
   1448 	__git_has_doubledash && return
   1449 
   1450 	local subcommands="start bad good skip reset visualize replay log run"
   1451 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   1452 	if [ -z "$subcommand" ]; then
   1453 		__git_find_repo_path
   1454 		if [ -f "$__git_repo_path"/BISECT_START ]; then
   1455 			__gitcomp "$subcommands"
   1456 		else
   1457 			__gitcomp "replay start"
   1458 		fi
   1459 		return
   1460 	fi
   1461 
   1462 	case "$subcommand" in
   1463 	bad|good|reset|skip|start)
   1464 		__git_complete_refs
   1465 		;;
   1466 	*)
   1467 		;;
   1468 	esac
   1469 }
   1470 
   1471 __git_ref_fieldlist="refname objecttype objectsize objectname upstream push HEAD symref"
   1472 
   1473 _git_branch ()
   1474 {
   1475 	local i c="$__git_cmd_idx" only_local_ref="n" has_r="n"
   1476 
   1477 	while [ $c -lt $cword ]; do
   1478 		i="${words[c]}"
   1479 		case "$i" in
   1480 		-d|-D|--delete|-m|-M|--move|-c|-C|--copy)
   1481 			only_local_ref="y" ;;
   1482 		-r|--remotes)
   1483 			has_r="y" ;;
   1484 		esac
   1485 		((c++))
   1486 	done
   1487 
   1488 	case "$cur" in
   1489 	--set-upstream-to=*)
   1490 		__git_complete_refs --cur="${cur##--set-upstream-to=}"
   1491 		;;
   1492 	--*)
   1493 		__gitcomp_builtin branch
   1494 		;;
   1495 	*)
   1496 		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
   1497 			__gitcomp_direct "$(__git_heads "" "$cur" " ")"
   1498 		else
   1499 			__git_complete_refs
   1500 		fi
   1501 		;;
   1502 	esac
   1503 }
   1504 
   1505 _git_bundle ()
   1506 {
   1507 	local cmd="${words[__git_cmd_idx+1]}"
   1508 	case "$cword" in
   1509 	$((__git_cmd_idx+1)))
   1510 		__gitcomp "create list-heads verify unbundle"
   1511 		;;
   1512 	$((__git_cmd_idx+2)))
   1513 		# looking for a file
   1514 		;;
   1515 	*)
   1516 		case "$cmd" in
   1517 			create)
   1518 				__git_complete_revlist
   1519 			;;
   1520 		esac
   1521 		;;
   1522 	esac
   1523 }
   1524 
   1525 # Helper function to decide whether or not we should enable DWIM logic for
   1526 # git-switch and git-checkout.
   1527 #
   1528 # To decide between the following rules in decreasing priority order:
   1529 # - the last provided of "--guess" or "--no-guess" explicitly enable or
   1530 #   disable completion of DWIM logic respectively.
   1531 # - If checkout.guess is false, disable completion of DWIM logic.
   1532 # - If the --no-track option is provided, take this as a hint to disable the
   1533 #   DWIM completion logic
   1534 # - If GIT_COMPLETION_CHECKOUT_NO_GUESS is set, disable the DWIM completion
   1535 #   logic, as requested by the user.
   1536 # - Enable DWIM logic otherwise.
   1537 #
   1538 __git_checkout_default_dwim_mode ()
   1539 {
   1540 	local last_option dwim_opt="--dwim"
   1541 
   1542 	if [ "${GIT_COMPLETION_CHECKOUT_NO_GUESS-}" = "1" ]; then
   1543 		dwim_opt=""
   1544 	fi
   1545 
   1546 	# --no-track disables DWIM, but with lower priority than
   1547 	# --guess/--no-guess/checkout.guess
   1548 	if [ -n "$(__git_find_on_cmdline "--no-track")" ]; then
   1549 		dwim_opt=""
   1550 	fi
   1551 
   1552 	# checkout.guess = false disables DWIM, but with lower priority than
   1553 	# --guess/--no-guess
   1554 	if [ "$(__git config --type=bool checkout.guess)" = "false" ]; then
   1555 		dwim_opt=""
   1556 	fi
   1557 
   1558 	# Find the last provided --guess or --no-guess
   1559 	last_option="$(__git_find_last_on_cmdline "--guess --no-guess")"
   1560 	case "$last_option" in
   1561 		--guess)
   1562 			dwim_opt="--dwim"
   1563 			;;
   1564 		--no-guess)
   1565 			dwim_opt=""
   1566 			;;
   1567 	esac
   1568 
   1569 	echo "$dwim_opt"
   1570 }
   1571 
   1572 _git_checkout ()
   1573 {
   1574 	__git_has_doubledash && return
   1575 
   1576 	local dwim_opt="$(__git_checkout_default_dwim_mode)"
   1577 
   1578 	case "$prev" in
   1579 	-b|-B|--orphan)
   1580 		# Complete local branches (and DWIM branch
   1581 		# remote branch names) for an option argument
   1582 		# specifying a new branch name. This is for
   1583 		# convenience, assuming new branches are
   1584 		# possibly based on pre-existing branch names.
   1585 		__git_complete_refs $dwim_opt --mode="heads"
   1586 		return
   1587 		;;
   1588 	*)
   1589 		;;
   1590 	esac
   1591 
   1592 	case "$cur" in
   1593 	--conflict=*)
   1594 		__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
   1595 		;;
   1596 	--*)
   1597 		__gitcomp_builtin checkout
   1598 		;;
   1599 	*)
   1600 		# At this point, we've already handled special completion for
   1601 		# the arguments to -b/-B, and --orphan. There are 3 main
   1602 		# things left we can possibly complete:
   1603 		# 1) a start-point for -b/-B, -d/--detach, or --orphan
   1604 		# 2) a remote head, for --track
   1605 		# 3) an arbitrary reference, possibly including DWIM names
   1606 		#
   1607 
   1608 		if [ -n "$(__git_find_on_cmdline "-b -B -d --detach --orphan")" ]; then
   1609 			__git_complete_refs --mode="refs"
   1610 		elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
   1611 			__git_complete_refs --mode="remote-heads"
   1612 		else
   1613 			__git_complete_refs $dwim_opt --mode="refs"
   1614 		fi
   1615 		;;
   1616 	esac
   1617 }
   1618 
   1619 __git_sequencer_inprogress_options="--continue --quit --abort --skip"
   1620 
   1621 __git_cherry_pick_inprogress_options=$__git_sequencer_inprogress_options
   1622 
   1623 _git_cherry_pick ()
   1624 {
   1625 	__git_find_repo_path
   1626 	if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
   1627 		__gitcomp "$__git_cherry_pick_inprogress_options"
   1628 		return
   1629 	fi
   1630 
   1631 	__git_complete_strategy && return
   1632 
   1633 	case "$cur" in
   1634 	--*)
   1635 		__gitcomp_builtin cherry-pick "" \
   1636 			"$__git_cherry_pick_inprogress_options"
   1637 		;;
   1638 	*)
   1639 		__git_complete_refs
   1640 		;;
   1641 	esac
   1642 }
   1643 
   1644 _git_clean ()
   1645 {
   1646 	case "$cur" in
   1647 	--*)
   1648 		__gitcomp_builtin clean
   1649 		return
   1650 		;;
   1651 	esac
   1652 
   1653 	# XXX should we check for -x option ?
   1654 	__git_complete_index_file "--others --directory"
   1655 }
   1656 
   1657 _git_clone ()
   1658 {
   1659 	case "$prev" in
   1660 	-c|--config)
   1661 		__git_complete_config_variable_name_and_value
   1662 		return
   1663 		;;
   1664 	esac
   1665 	case "$cur" in
   1666 	--config=*)
   1667 		__git_complete_config_variable_name_and_value \
   1668 			--cur="${cur##--config=}"
   1669 		return
   1670 		;;
   1671 	--*)
   1672 		__gitcomp_builtin clone
   1673 		return
   1674 		;;
   1675 	esac
   1676 }
   1677 
   1678 __git_untracked_file_modes="all no normal"
   1679 
   1680 _git_commit ()
   1681 {
   1682 	case "$prev" in
   1683 	-c|-C)
   1684 		__git_complete_refs
   1685 		return
   1686 		;;
   1687 	esac
   1688 
   1689 	case "$cur" in
   1690 	--cleanup=*)
   1691 		__gitcomp "default scissors strip verbatim whitespace
   1692 			" "" "${cur##--cleanup=}"
   1693 		return
   1694 		;;
   1695 	--reuse-message=*|--reedit-message=*|\
   1696 	--fixup=*|--squash=*)
   1697 		__git_complete_refs --cur="${cur#*=}"
   1698 		return
   1699 		;;
   1700 	--untracked-files=*)
   1701 		__gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
   1702 		return
   1703 		;;
   1704 	--*)
   1705 		__gitcomp_builtin commit
   1706 		return
   1707 	esac
   1708 
   1709 	if __git rev-parse --verify --quiet HEAD >/dev/null; then
   1710 		__git_complete_index_file "--committable"
   1711 	else
   1712 		# This is the first commit
   1713 		__git_complete_index_file "--cached"
   1714 	fi
   1715 }
   1716 
   1717 _git_describe ()
   1718 {
   1719 	case "$cur" in
   1720 	--*)
   1721 		__gitcomp_builtin describe
   1722 		return
   1723 	esac
   1724 	__git_complete_refs
   1725 }
   1726 
   1727 __git_diff_algorithms="myers minimal patience histogram"
   1728 
   1729 __git_diff_submodule_formats="diff log short"
   1730 
   1731 __git_color_moved_opts="no default plain blocks zebra dimmed-zebra"
   1732 
   1733 __git_color_moved_ws_opts="no ignore-space-at-eol ignore-space-change
   1734 			ignore-all-space allow-indentation-change"
   1735 
   1736 __git_ws_error_highlight_opts="context old new all default"
   1737 
   1738 # Options for the diff machinery (diff, log, show, stash, range-diff, ...)
   1739 __git_diff_common_options="--stat --numstat --shortstat --summary
   1740 			--patch-with-stat --name-only --name-status --color
   1741 			--no-color --color-words --no-renames --check
   1742 			--color-moved --color-moved= --no-color-moved
   1743 			--color-moved-ws= --no-color-moved-ws
   1744 			--full-index --binary --abbrev --diff-filter=
   1745 			--find-copies --find-object --find-renames
   1746 			--no-relative --relative
   1747 			--find-copies-harder --ignore-cr-at-eol
   1748 			--text --ignore-space-at-eol --ignore-space-change
   1749 			--ignore-all-space --ignore-blank-lines --exit-code
   1750 			--quiet --ext-diff --no-ext-diff --unified=
   1751 			--no-prefix --src-prefix= --dst-prefix=
   1752 			--inter-hunk-context= --function-context
   1753 			--patience --histogram --minimal
   1754 			--raw --word-diff --word-diff-regex=
   1755 			--dirstat --dirstat= --dirstat-by-file
   1756 			--dirstat-by-file= --cumulative
   1757 			--diff-algorithm= --default-prefix
   1758 			--submodule --submodule= --ignore-submodules
   1759 			--indent-heuristic --no-indent-heuristic
   1760 			--textconv --no-textconv --break-rewrites
   1761 			--patch --no-patch --cc --combined-all-paths
   1762 			--anchored= --compact-summary --ignore-matching-lines=
   1763 			--irreversible-delete --line-prefix --no-stat
   1764 			--output= --output-indicator-context=
   1765 			--output-indicator-new= --output-indicator-old=
   1766 			--ws-error-highlight=
   1767 			--pickaxe-all --pickaxe-regex
   1768 "
   1769 
   1770 # Options for diff/difftool
   1771 __git_diff_difftool_options="--cached --staged
   1772 			--base --ours --theirs --no-index --merge-base
   1773 			--ita-invisible-in-index --ita-visible-in-index
   1774 			$__git_diff_common_options"
   1775 
   1776 _git_diff ()
   1777 {
   1778 	__git_has_doubledash && return
   1779 
   1780 	case "$cur" in
   1781 	--diff-algorithm=*)
   1782 		__gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
   1783 		return
   1784 		;;
   1785 	--submodule=*)
   1786 		__gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
   1787 		return
   1788 		;;
   1789 	--color-moved=*)
   1790 		__gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
   1791 		return
   1792 		;;
   1793 	--color-moved-ws=*)
   1794 		__gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
   1795 		return
   1796 		;;
   1797 	--ws-error-highlight=*)
   1798 		__gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
   1799 		return
   1800 		;;
   1801 	--*)
   1802 		__gitcomp "$__git_diff_difftool_options"
   1803 		return
   1804 		;;
   1805 	esac
   1806 	__git_complete_revlist_file
   1807 }
   1808 
   1809 __git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
   1810 			tkdiff vimdiff nvimdiff gvimdiff xxdiff araxis p4merge
   1811 			bc codecompare smerge
   1812 "
   1813 
   1814 _git_difftool ()
   1815 {
   1816 	__git_has_doubledash && return
   1817 
   1818 	case "$cur" in
   1819 	--tool=*)
   1820 		__gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
   1821 		return
   1822 		;;
   1823 	--*)
   1824 		__gitcomp_builtin difftool "$__git_diff_difftool_options"
   1825 		return
   1826 		;;
   1827 	esac
   1828 	__git_complete_revlist_file
   1829 }
   1830 
   1831 __git_fetch_recurse_submodules="yes on-demand no"
   1832 
   1833 _git_fetch ()
   1834 {
   1835 	case "$cur" in
   1836 	--recurse-submodules=*)
   1837 		__gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
   1838 		return
   1839 		;;
   1840 	--filter=*)
   1841 		__gitcomp "blob:none blob:limit= sparse:oid=" "" "${cur##--filter=}"
   1842 		return
   1843 		;;
   1844 	--*)
   1845 		__gitcomp_builtin fetch
   1846 		return
   1847 		;;
   1848 	esac
   1849 	__git_complete_remote_or_refspec
   1850 }
   1851 
   1852 __git_format_patch_extra_options="
   1853 	--full-index --not --all --no-prefix --src-prefix=
   1854 	--dst-prefix= --notes
   1855 "
   1856 
   1857 _git_format_patch ()
   1858 {
   1859 	case "$cur" in
   1860 	--thread=*)
   1861 		__gitcomp "
   1862 			deep shallow
   1863 			" "" "${cur##--thread=}"
   1864 		return
   1865 		;;
   1866 	--base=*|--interdiff=*|--range-diff=*)
   1867 		__git_complete_refs --cur="${cur#--*=}"
   1868 		return
   1869 		;;
   1870 	--*)
   1871 		__gitcomp_builtin format-patch "$__git_format_patch_extra_options"
   1872 		return
   1873 		;;
   1874 	esac
   1875 	__git_complete_revlist
   1876 }
   1877 
   1878 _git_fsck ()
   1879 {
   1880 	case "$cur" in
   1881 	--*)
   1882 		__gitcomp_builtin fsck
   1883 		return
   1884 		;;
   1885 	esac
   1886 }
   1887 
   1888 _git_gitk ()
   1889 {
   1890 	__gitk_main
   1891 }
   1892 
   1893 # Lists matching symbol names from a tag (as in ctags) file.
   1894 # 1: List symbol names matching this word.
   1895 # 2: The tag file to list symbol names from.
   1896 # 3: A prefix to be added to each listed symbol name (optional).
   1897 # 4: A suffix to be appended to each listed symbol name (optional).
   1898 __git_match_ctag () {
   1899 	awk -v pfx="${3-}" -v sfx="${4-}" "
   1900 		/^${1//\//\\/}/ { print pfx \$1 sfx }
   1901 		" "$2"
   1902 }
   1903 
   1904 # Complete symbol names from a tag file.
   1905 # Usage: __git_complete_symbol [<option>]...
   1906 # --tags=<file>: The tag file to list symbol names from instead of the
   1907 #                default "tags".
   1908 # --pfx=<prefix>: A prefix to be added to each symbol name.
   1909 # --cur=<word>: The current symbol name to be completed.  Defaults to
   1910 #               the current word to be completed.
   1911 # --sfx=<suffix>: A suffix to be appended to each symbol name instead
   1912 #                 of the default space.
   1913 __git_complete_symbol () {
   1914 	local tags=tags pfx="" cur_="${cur-}" sfx=" "
   1915 
   1916 	while test $# != 0; do
   1917 		case "$1" in
   1918 		--tags=*)	tags="${1##--tags=}" ;;
   1919 		--pfx=*)	pfx="${1##--pfx=}" ;;
   1920 		--cur=*)	cur_="${1##--cur=}" ;;
   1921 		--sfx=*)	sfx="${1##--sfx=}" ;;
   1922 		*)		return 1 ;;
   1923 		esac
   1924 		shift
   1925 	done
   1926 
   1927 	if test -r "$tags"; then
   1928 		__gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
   1929 	fi
   1930 }
   1931 
   1932 _git_grep ()
   1933 {
   1934 	__git_has_doubledash && return
   1935 
   1936 	case "$cur" in
   1937 	--*)
   1938 		__gitcomp_builtin grep
   1939 		return
   1940 		;;
   1941 	esac
   1942 
   1943 	case "$cword,$prev" in
   1944 	$((__git_cmd_idx+1)),*|*,-*)
   1945 		__git_complete_symbol && return
   1946 		;;
   1947 	esac
   1948 
   1949 	__git_complete_refs
   1950 }
   1951 
   1952 _git_help ()
   1953 {
   1954 	case "$cur" in
   1955 	--*)
   1956 		__gitcomp_builtin help
   1957 		return
   1958 		;;
   1959 	esac
   1960 	if test -n "${GIT_TESTING_ALL_COMMAND_LIST-}"
   1961 	then
   1962 		__gitcomp "$GIT_TESTING_ALL_COMMAND_LIST $(__git --list-cmds=alias,list-guide) gitk"
   1963 	else
   1964 		__gitcomp "$(__git --list-cmds=main,nohelpers,alias,list-guide) gitk"
   1965 	fi
   1966 }
   1967 
   1968 _git_init ()
   1969 {
   1970 	case "$cur" in
   1971 	--shared=*)
   1972 		__gitcomp "
   1973 			false true umask group all world everybody
   1974 			" "" "${cur##--shared=}"
   1975 		return
   1976 		;;
   1977 	--*)
   1978 		__gitcomp_builtin init
   1979 		return
   1980 		;;
   1981 	esac
   1982 }
   1983 
   1984 _git_ls_files ()
   1985 {
   1986 	case "$cur" in
   1987 	--*)
   1988 		__gitcomp_builtin ls-files
   1989 		return
   1990 		;;
   1991 	esac
   1992 
   1993 	# XXX ignore options like --modified and always suggest all cached
   1994 	# files.
   1995 	__git_complete_index_file "--cached"
   1996 }
   1997 
   1998 _git_ls_remote ()
   1999 {
   2000 	case "$cur" in
   2001 	--*)
   2002 		__gitcomp_builtin ls-remote
   2003 		return
   2004 		;;
   2005 	esac
   2006 	__gitcomp_nl "$(__git_remotes)"
   2007 }
   2008 
   2009 _git_ls_tree ()
   2010 {
   2011 	case "$cur" in
   2012 	--*)
   2013 		__gitcomp_builtin ls-tree
   2014 		return
   2015 		;;
   2016 	esac
   2017 
   2018 	__git_complete_file
   2019 }
   2020 
   2021 # Options that go well for log, shortlog and gitk
   2022 __git_log_common_options="
   2023 	--not --all
   2024 	--branches --tags --remotes
   2025 	--first-parent --merges --no-merges
   2026 	--max-count=
   2027 	--max-age= --since= --after=
   2028 	--min-age= --until= --before=
   2029 	--min-parents= --max-parents=
   2030 	--no-min-parents --no-max-parents
   2031 "
   2032 # Options that go well for log and gitk (not shortlog)
   2033 __git_log_gitk_options="
   2034 	--dense --sparse --full-history
   2035 	--simplify-merges --simplify-by-decoration
   2036 	--left-right --notes --no-notes
   2037 "
   2038 # Options that go well for log and shortlog (not gitk)
   2039 __git_log_shortlog_options="
   2040 	--author= --committer= --grep=
   2041 	--all-match --invert-grep
   2042 "
   2043 # Options accepted by log and show
   2044 __git_log_show_options="
   2045 	--diff-merges --diff-merges= --no-diff-merges --remerge-diff
   2046 "
   2047 
   2048 __git_diff_merges_opts="off none on first-parent 1 separate m combined c dense-combined cc remerge r"
   2049 
   2050 __git_log_pretty_formats="oneline short medium full fuller reference email raw format: tformat: mboxrd"
   2051 __git_log_date_formats="relative iso8601 iso8601-strict rfc2822 short local default human raw unix auto: format:"
   2052 
   2053 _git_log ()
   2054 {
   2055 	__git_has_doubledash && return
   2056 	__git_find_repo_path
   2057 
   2058 	local merge=""
   2059 	if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
   2060 		merge="--merge"
   2061 	fi
   2062 	case "$prev,$cur" in
   2063 	-L,:*:*)
   2064 		return	# fall back to Bash filename completion
   2065 		;;
   2066 	-L,:*)
   2067 		__git_complete_symbol --cur="${cur#:}" --sfx=":"
   2068 		return
   2069 		;;
   2070 	-G,*|-S,*)
   2071 		__git_complete_symbol
   2072 		return
   2073 		;;
   2074 	esac
   2075 	case "$cur" in
   2076 	--pretty=*|--format=*)
   2077 		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
   2078 			" "" "${cur#*=}"
   2079 		return
   2080 		;;
   2081 	--date=*)
   2082 		__gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
   2083 		return
   2084 		;;
   2085 	--decorate=*)
   2086 		__gitcomp "full short no" "" "${cur##--decorate=}"
   2087 		return
   2088 		;;
   2089 	--diff-algorithm=*)
   2090 		__gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
   2091 		return
   2092 		;;
   2093 	--submodule=*)
   2094 		__gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
   2095 		return
   2096 		;;
   2097 	--ws-error-highlight=*)
   2098 		__gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
   2099 		return
   2100 		;;
   2101 	--no-walk=*)
   2102 		__gitcomp "sorted unsorted" "" "${cur##--no-walk=}"
   2103 		return
   2104 		;;
   2105 	--diff-merges=*)
   2106                 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
   2107                 return
   2108                 ;;
   2109 	--*)
   2110 		__gitcomp "
   2111 			$__git_log_common_options
   2112 			$__git_log_shortlog_options
   2113 			$__git_log_gitk_options
   2114 			$__git_log_show_options
   2115 			--root --topo-order --date-order --reverse
   2116 			--follow --full-diff
   2117 			--abbrev-commit --no-abbrev-commit --abbrev=
   2118 			--relative-date --date=
   2119 			--pretty= --format= --oneline
   2120 			--show-signature
   2121 			--cherry-mark
   2122 			--cherry-pick
   2123 			--graph
   2124 			--decorate --decorate= --no-decorate
   2125 			--walk-reflogs
   2126 			--no-walk --no-walk= --do-walk
   2127 			--parents --children
   2128 			--expand-tabs --expand-tabs= --no-expand-tabs
   2129 			$merge
   2130 			$__git_diff_common_options
   2131 			"
   2132 		return
   2133 		;;
   2134 	-L:*:*)
   2135 		return	# fall back to Bash filename completion
   2136 		;;
   2137 	-L:*)
   2138 		__git_complete_symbol --cur="${cur#-L:}" --sfx=":"
   2139 		return
   2140 		;;
   2141 	-G*)
   2142 		__git_complete_symbol --pfx="-G" --cur="${cur#-G}"
   2143 		return
   2144 		;;
   2145 	-S*)
   2146 		__git_complete_symbol --pfx="-S" --cur="${cur#-S}"
   2147 		return
   2148 		;;
   2149 	esac
   2150 	__git_complete_revlist
   2151 }
   2152 
   2153 _git_merge ()
   2154 {
   2155 	__git_complete_strategy && return
   2156 
   2157 	case "$cur" in
   2158 	--*)
   2159 		__gitcomp_builtin merge
   2160 		return
   2161 	esac
   2162 	__git_complete_refs
   2163 }
   2164 
   2165 _git_mergetool ()
   2166 {
   2167 	case "$cur" in
   2168 	--tool=*)
   2169 		__gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
   2170 		return
   2171 		;;
   2172 	--*)
   2173 		__gitcomp "--tool= --prompt --no-prompt --gui --no-gui"
   2174 		return
   2175 		;;
   2176 	esac
   2177 }
   2178 
   2179 _git_merge_base ()
   2180 {
   2181 	case "$cur" in
   2182 	--*)
   2183 		__gitcomp_builtin merge-base
   2184 		return
   2185 		;;
   2186 	esac
   2187 	__git_complete_refs
   2188 }
   2189 
   2190 _git_mv ()
   2191 {
   2192 	case "$cur" in
   2193 	--*)
   2194 		__gitcomp_builtin mv
   2195 		return
   2196 		;;
   2197 	esac
   2198 
   2199 	if [ $(__git_count_arguments "mv") -gt 0 ]; then
   2200 		# We need to show both cached and untracked files (including
   2201 		# empty directories) since this may not be the last argument.
   2202 		__git_complete_index_file "--cached --others --directory"
   2203 	else
   2204 		__git_complete_index_file "--cached"
   2205 	fi
   2206 }
   2207 
   2208 _git_notes ()
   2209 {
   2210 	local subcommands='add append copy edit get-ref list merge prune remove show'
   2211 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   2212 
   2213 	case "$subcommand,$cur" in
   2214 	,--*)
   2215 		__gitcomp_builtin notes
   2216 		;;
   2217 	,*)
   2218 		case "$prev" in
   2219 		--ref)
   2220 			__git_complete_refs
   2221 			;;
   2222 		*)
   2223 			__gitcomp "$subcommands --ref"
   2224 			;;
   2225 		esac
   2226 		;;
   2227 	*,--reuse-message=*|*,--reedit-message=*)
   2228 		__git_complete_refs --cur="${cur#*=}"
   2229 		;;
   2230 	*,--*)
   2231 		__gitcomp_builtin notes_$subcommand
   2232 		;;
   2233 	prune,*|get-ref,*)
   2234 		# this command does not take a ref, do not complete it
   2235 		;;
   2236 	*)
   2237 		case "$prev" in
   2238 		-m|-F)
   2239 			;;
   2240 		*)
   2241 			__git_complete_refs
   2242 			;;
   2243 		esac
   2244 		;;
   2245 	esac
   2246 }
   2247 
   2248 _git_pull ()
   2249 {
   2250 	__git_complete_strategy && return
   2251 
   2252 	case "$cur" in
   2253 	--recurse-submodules=*)
   2254 		__gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
   2255 		return
   2256 		;;
   2257 	--*)
   2258 		__gitcomp_builtin pull
   2259 
   2260 		return
   2261 		;;
   2262 	esac
   2263 	__git_complete_remote_or_refspec
   2264 }
   2265 
   2266 __git_push_recurse_submodules="check on-demand only"
   2267 
   2268 __git_complete_force_with_lease ()
   2269 {
   2270 	local cur_=$1
   2271 
   2272 	case "$cur_" in
   2273 	--*=)
   2274 		;;
   2275 	*:*)
   2276 		__git_complete_refs --cur="${cur_#*:}"
   2277 		;;
   2278 	*)
   2279 		__git_complete_refs --cur="$cur_"
   2280 		;;
   2281 	esac
   2282 }
   2283 
   2284 _git_push ()
   2285 {
   2286 	case "$prev" in
   2287 	--repo)
   2288 		__gitcomp_nl "$(__git_remotes)"
   2289 		return
   2290 		;;
   2291 	--recurse-submodules)
   2292 		__gitcomp "$__git_push_recurse_submodules"
   2293 		return
   2294 		;;
   2295 	esac
   2296 	case "$cur" in
   2297 	--repo=*)
   2298 		__gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
   2299 		return
   2300 		;;
   2301 	--recurse-submodules=*)
   2302 		__gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
   2303 		return
   2304 		;;
   2305 	--force-with-lease=*)
   2306 		__git_complete_force_with_lease "${cur##--force-with-lease=}"
   2307 		return
   2308 		;;
   2309 	--*)
   2310 		__gitcomp_builtin push
   2311 		return
   2312 		;;
   2313 	esac
   2314 	__git_complete_remote_or_refspec
   2315 }
   2316 
   2317 _git_range_diff ()
   2318 {
   2319 	case "$cur" in
   2320 	--*)
   2321 		__gitcomp "
   2322 			--creation-factor= --no-dual-color
   2323 			$__git_diff_common_options
   2324 		"
   2325 		return
   2326 		;;
   2327 	esac
   2328 	__git_complete_revlist
   2329 }
   2330 
   2331 __git_rebase_inprogress_options="--continue --skip --abort --quit --show-current-patch"
   2332 __git_rebase_interactive_inprogress_options="$__git_rebase_inprogress_options --edit-todo"
   2333 
   2334 _git_rebase ()
   2335 {
   2336 	__git_find_repo_path
   2337 	if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
   2338 		__gitcomp "$__git_rebase_interactive_inprogress_options"
   2339 		return
   2340 	elif [ -d "$__git_repo_path"/rebase-apply ] || \
   2341 	     [ -d "$__git_repo_path"/rebase-merge ]; then
   2342 		__gitcomp "$__git_rebase_inprogress_options"
   2343 		return
   2344 	fi
   2345 	__git_complete_strategy && return
   2346 	case "$cur" in
   2347 	--whitespace=*)
   2348 		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
   2349 		return
   2350 		;;
   2351 	--onto=*)
   2352 		__git_complete_refs --cur="${cur##--onto=}"
   2353 		return
   2354 		;;
   2355 	--*)
   2356 		__gitcomp_builtin rebase "" \
   2357 			"$__git_rebase_interactive_inprogress_options"
   2358 
   2359 		return
   2360 	esac
   2361 	__git_complete_refs
   2362 }
   2363 
   2364 _git_reflog ()
   2365 {
   2366 	local subcommands="show delete expire"
   2367 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   2368 
   2369 	if [ -z "$subcommand" ]; then
   2370 		__gitcomp "$subcommands"
   2371 	else
   2372 		__git_complete_refs
   2373 	fi
   2374 }
   2375 
   2376 __git_send_email_confirm_options="always never auto cc compose"
   2377 __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
   2378 
   2379 _git_send_email ()
   2380 {
   2381 	case "$prev" in
   2382 	--to|--cc|--bcc|--from)
   2383 		__gitcomp "$(__git send-email --dump-aliases)"
   2384 		return
   2385 		;;
   2386 	esac
   2387 
   2388 	case "$cur" in
   2389 	--confirm=*)
   2390 		__gitcomp "
   2391 			$__git_send_email_confirm_options
   2392 			" "" "${cur##--confirm=}"
   2393 		return
   2394 		;;
   2395 	--suppress-cc=*)
   2396 		__gitcomp "
   2397 			$__git_send_email_suppresscc_options
   2398 			" "" "${cur##--suppress-cc=}"
   2399 
   2400 		return
   2401 		;;
   2402 	--smtp-encryption=*)
   2403 		__gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
   2404 		return
   2405 		;;
   2406 	--thread=*)
   2407 		__gitcomp "
   2408 			deep shallow
   2409 			" "" "${cur##--thread=}"
   2410 		return
   2411 		;;
   2412 	--to=*|--cc=*|--bcc=*|--from=*)
   2413 		__gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
   2414 		return
   2415 		;;
   2416 	--*)
   2417 		__gitcomp_builtin send-email "$__git_format_patch_extra_options"
   2418 		return
   2419 		;;
   2420 	esac
   2421 	__git_complete_revlist
   2422 }
   2423 
   2424 _git_stage ()
   2425 {
   2426 	_git_add
   2427 }
   2428 
   2429 _git_status ()
   2430 {
   2431 	local complete_opt
   2432 	local untracked_state
   2433 
   2434 	case "$cur" in
   2435 	--ignore-submodules=*)
   2436 		__gitcomp "none untracked dirty all" "" "${cur##--ignore-submodules=}"
   2437 		return
   2438 		;;
   2439 	--untracked-files=*)
   2440 		__gitcomp "$__git_untracked_file_modes" "" "${cur##--untracked-files=}"
   2441 		return
   2442 		;;
   2443 	--column=*)
   2444 		__gitcomp "
   2445 			always never auto column row plain dense nodense
   2446 			" "" "${cur##--column=}"
   2447 		return
   2448 		;;
   2449 	--*)
   2450 		__gitcomp_builtin status
   2451 		return
   2452 		;;
   2453 	esac
   2454 
   2455 	untracked_state="$(__git_get_option_value "-u" "--untracked-files=" \
   2456 		"$__git_untracked_file_modes" "status.showUntrackedFiles")"
   2457 
   2458 	case "$untracked_state" in
   2459 	no)
   2460 		# --ignored option does not matter
   2461 		complete_opt=
   2462 		;;
   2463 	all|normal|*)
   2464 		complete_opt="--cached --directory --no-empty-directory --others"
   2465 
   2466 		if [ -n "$(__git_find_on_cmdline "--ignored")" ]; then
   2467 			complete_opt="$complete_opt --ignored --exclude=*"
   2468 		fi
   2469 		;;
   2470 	esac
   2471 
   2472 	__git_complete_index_file "$complete_opt"
   2473 }
   2474 
   2475 _git_switch ()
   2476 {
   2477 	local dwim_opt="$(__git_checkout_default_dwim_mode)"
   2478 
   2479 	case "$prev" in
   2480 	-c|-C|--orphan)
   2481 		# Complete local branches (and DWIM branch
   2482 		# remote branch names) for an option argument
   2483 		# specifying a new branch name. This is for
   2484 		# convenience, assuming new branches are
   2485 		# possibly based on pre-existing branch names.
   2486 		__git_complete_refs $dwim_opt --mode="heads"
   2487 		return
   2488 		;;
   2489 	*)
   2490 		;;
   2491 	esac
   2492 
   2493 	case "$cur" in
   2494 	--conflict=*)
   2495 		__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
   2496 		;;
   2497 	--*)
   2498 		__gitcomp_builtin switch
   2499 		;;
   2500 	*)
   2501 		# Unlike in git checkout, git switch --orphan does not take
   2502 		# a start point. Thus we really have nothing to complete after
   2503 		# the branch name.
   2504 		if [ -n "$(__git_find_on_cmdline "--orphan")" ]; then
   2505 			return
   2506 		fi
   2507 
   2508 		# At this point, we've already handled special completion for
   2509 		# -c/-C, and --orphan. There are 3 main things left to
   2510 		# complete:
   2511 		# 1) a start-point for -c/-C or -d/--detach
   2512 		# 2) a remote head, for --track
   2513 		# 3) a branch name, possibly including DWIM remote branches
   2514 
   2515 		if [ -n "$(__git_find_on_cmdline "-c -C -d --detach")" ]; then
   2516 			__git_complete_refs --mode="refs"
   2517 		elif [ -n "$(__git_find_on_cmdline "--track")" ]; then
   2518 			__git_complete_refs --mode="remote-heads"
   2519 		else
   2520 			__git_complete_refs $dwim_opt --mode="heads"
   2521 		fi
   2522 		;;
   2523 	esac
   2524 }
   2525 
   2526 __git_config_get_set_variables ()
   2527 {
   2528 	local prevword word config_file= c=$cword
   2529 	while [ $c -gt "$__git_cmd_idx" ]; do
   2530 		word="${words[c]}"
   2531 		case "$word" in
   2532 		--system|--global|--local|--file=*)
   2533 			config_file="$word"
   2534 			break
   2535 			;;
   2536 		-f|--file)
   2537 			config_file="$word $prevword"
   2538 			break
   2539 			;;
   2540 		esac
   2541 		prevword=$word
   2542 		c=$((--c))
   2543 	done
   2544 
   2545 	__git config $config_file --name-only --list
   2546 }
   2547 
   2548 __git_config_vars=
   2549 __git_compute_config_vars ()
   2550 {
   2551 	test -n "$__git_config_vars" ||
   2552 	__git_config_vars="$(git help --config-for-completion)"
   2553 }
   2554 
   2555 __git_config_sections=
   2556 __git_compute_config_sections ()
   2557 {
   2558 	test -n "$__git_config_sections" ||
   2559 	__git_config_sections="$(git help --config-sections-for-completion)"
   2560 }
   2561 
   2562 # Completes possible values of various configuration variables.
   2563 #
   2564 # Usage: __git_complete_config_variable_value [<option>]...
   2565 # --varname=<word>: The name of the configuration variable whose value is
   2566 #                   to be completed.  Defaults to the previous word on the
   2567 #                   command line.
   2568 # --cur=<word>: The current value to be completed.  Defaults to the current
   2569 #               word to be completed.
   2570 __git_complete_config_variable_value ()
   2571 {
   2572 	local varname="$prev" cur_="$cur"
   2573 
   2574 	while test $# != 0; do
   2575 		case "$1" in
   2576 		--varname=*)	varname="${1##--varname=}" ;;
   2577 		--cur=*)	cur_="${1##--cur=}" ;;
   2578 		*)		return 1 ;;
   2579 		esac
   2580 		shift
   2581 	done
   2582 
   2583 	if [ "${BASH_VERSINFO[0]:-0}" -ge 4 ]; then
   2584 		varname="${varname,,}"
   2585 	else
   2586 		varname="$(echo "$varname" |tr A-Z a-z)"
   2587 	fi
   2588 
   2589 	case "$varname" in
   2590 	branch.*.remote|branch.*.pushremote)
   2591 		__gitcomp_nl "$(__git_remotes)" "" "$cur_"
   2592 		return
   2593 		;;
   2594 	branch.*.merge)
   2595 		__git_complete_refs --cur="$cur_"
   2596 		return
   2597 		;;
   2598 	branch.*.rebase)
   2599 		__gitcomp "false true merges interactive" "" "$cur_"
   2600 		return
   2601 		;;
   2602 	remote.pushdefault)
   2603 		__gitcomp_nl "$(__git_remotes)" "" "$cur_"
   2604 		return
   2605 		;;
   2606 	remote.*.fetch)
   2607 		local remote="${varname#remote.}"
   2608 		remote="${remote%.fetch}"
   2609 		if [ -z "$cur_" ]; then
   2610 			__gitcomp_nl "refs/heads/" "" "" ""
   2611 			return
   2612 		fi
   2613 		__gitcomp_nl "$(__git_refs_remotes "$remote")" "" "$cur_"
   2614 		return
   2615 		;;
   2616 	remote.*.push)
   2617 		local remote="${varname#remote.}"
   2618 		remote="${remote%.push}"
   2619 		__gitcomp_nl "$(__git for-each-ref \
   2620 			--format='%(refname):%(refname)' refs/heads)" "" "$cur_"
   2621 		return
   2622 		;;
   2623 	pull.twohead|pull.octopus)
   2624 		__git_compute_merge_strategies
   2625 		__gitcomp "$__git_merge_strategies" "" "$cur_"
   2626 		return
   2627 		;;
   2628 	color.pager)
   2629 		__gitcomp "false true" "" "$cur_"
   2630 		return
   2631 		;;
   2632 	color.*.*)
   2633 		__gitcomp "
   2634 			normal black red green yellow blue magenta cyan white
   2635 			bold dim ul blink reverse
   2636 			" "" "$cur_"
   2637 		return
   2638 		;;
   2639 	color.*)
   2640 		__gitcomp "false true always never auto" "" "$cur_"
   2641 		return
   2642 		;;
   2643 	diff.submodule)
   2644 		__gitcomp "$__git_diff_submodule_formats" "" "$cur_"
   2645 		return
   2646 		;;
   2647 	help.format)
   2648 		__gitcomp "man info web html" "" "$cur_"
   2649 		return
   2650 		;;
   2651 	log.date)
   2652 		__gitcomp "$__git_log_date_formats" "" "$cur_"
   2653 		return
   2654 		;;
   2655 	sendemail.aliasfiletype)
   2656 		__gitcomp "mutt mailrc pine elm gnus" "" "$cur_"
   2657 		return
   2658 		;;
   2659 	sendemail.confirm)
   2660 		__gitcomp "$__git_send_email_confirm_options" "" "$cur_"
   2661 		return
   2662 		;;
   2663 	sendemail.suppresscc)
   2664 		__gitcomp "$__git_send_email_suppresscc_options" "" "$cur_"
   2665 		return
   2666 		;;
   2667 	sendemail.transferencoding)
   2668 		__gitcomp "7bit 8bit quoted-printable base64" "" "$cur_"
   2669 		return
   2670 		;;
   2671 	*.*)
   2672 		return
   2673 		;;
   2674 	esac
   2675 }
   2676 
   2677 # Completes configuration sections, subsections, variable names.
   2678 #
   2679 # Usage: __git_complete_config_variable_name [<option>]...
   2680 # --cur=<word>: The current configuration section/variable name to be
   2681 #               completed.  Defaults to the current word to be completed.
   2682 # --sfx=<suffix>: A suffix to be appended to each fully completed
   2683 #                 configuration variable name (but not to sections or
   2684 #                 subsections) instead of the default space.
   2685 __git_complete_config_variable_name ()
   2686 {
   2687 	local cur_="$cur" sfx
   2688 
   2689 	while test $# != 0; do
   2690 		case "$1" in
   2691 		--cur=*)	cur_="${1##--cur=}" ;;
   2692 		--sfx=*)	sfx="${1##--sfx=}" ;;
   2693 		*)		return 1 ;;
   2694 		esac
   2695 		shift
   2696 	done
   2697 
   2698 	case "$cur_" in
   2699 	branch.*.*)
   2700 		local pfx="${cur_%.*}."
   2701 		cur_="${cur_##*.}"
   2702 		__gitcomp "remote pushRemote merge mergeOptions rebase" "$pfx" "$cur_" "$sfx"
   2703 		return
   2704 		;;
   2705 	branch.*)
   2706 		local pfx="${cur_%.*}."
   2707 		cur_="${cur_#*.}"
   2708 		__gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
   2709 		__gitcomp_nl_append $'autoSetupMerge\nautoSetupRebase\n' "$pfx" "$cur_" "${sfx- }"
   2710 		return
   2711 		;;
   2712 	guitool.*.*)
   2713 		local pfx="${cur_%.*}."
   2714 		cur_="${cur_##*.}"
   2715 		__gitcomp "
   2716 			argPrompt cmd confirm needsFile noConsole noRescan
   2717 			prompt revPrompt revUnmerged title
   2718 			" "$pfx" "$cur_" "$sfx"
   2719 		return
   2720 		;;
   2721 	difftool.*.*)
   2722 		local pfx="${cur_%.*}."
   2723 		cur_="${cur_##*.}"
   2724 		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
   2725 		return
   2726 		;;
   2727 	man.*.*)
   2728 		local pfx="${cur_%.*}."
   2729 		cur_="${cur_##*.}"
   2730 		__gitcomp "cmd path" "$pfx" "$cur_" "$sfx"
   2731 		return
   2732 		;;
   2733 	mergetool.*.*)
   2734 		local pfx="${cur_%.*}."
   2735 		cur_="${cur_##*.}"
   2736 		__gitcomp "cmd path trustExitCode" "$pfx" "$cur_" "$sfx"
   2737 		return
   2738 		;;
   2739 	pager.*)
   2740 		local pfx="${cur_%.*}."
   2741 		cur_="${cur_#*.}"
   2742 		__git_compute_all_commands
   2743 		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_" "${sfx- }"
   2744 		return
   2745 		;;
   2746 	remote.*.*)
   2747 		local pfx="${cur_%.*}."
   2748 		cur_="${cur_##*.}"
   2749 		__gitcomp "
   2750 			url proxy fetch push mirror skipDefaultUpdate
   2751 			receivepack uploadpack tagOpt pushurl
   2752 			" "$pfx" "$cur_" "$sfx"
   2753 		return
   2754 		;;
   2755 	remote.*)
   2756 		local pfx="${cur_%.*}."
   2757 		cur_="${cur_#*.}"
   2758 		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
   2759 		__gitcomp_nl_append "pushDefault" "$pfx" "$cur_" "${sfx- }"
   2760 		return
   2761 		;;
   2762 	url.*.*)
   2763 		local pfx="${cur_%.*}."
   2764 		cur_="${cur_##*.}"
   2765 		__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_" "$sfx"
   2766 		return
   2767 		;;
   2768 	*.*)
   2769 		__git_compute_config_vars
   2770 		__gitcomp "$__git_config_vars" "" "$cur_" "$sfx"
   2771 		;;
   2772 	*)
   2773 		__git_compute_config_sections
   2774 		__gitcomp "$__git_config_sections" "" "$cur_" "."
   2775 		;;
   2776 	esac
   2777 }
   2778 
   2779 # Completes '='-separated configuration sections/variable names and values
   2780 # for 'git -c section.name=value'.
   2781 #
   2782 # Usage: __git_complete_config_variable_name_and_value [<option>]...
   2783 # --cur=<word>: The current configuration section/variable name/value to be
   2784 #               completed. Defaults to the current word to be completed.
   2785 __git_complete_config_variable_name_and_value ()
   2786 {
   2787 	local cur_="$cur"
   2788 
   2789 	while test $# != 0; do
   2790 		case "$1" in
   2791 		--cur=*)	cur_="${1##--cur=}" ;;
   2792 		*)		return 1 ;;
   2793 		esac
   2794 		shift
   2795 	done
   2796 
   2797 	case "$cur_" in
   2798 	*=*)
   2799 		__git_complete_config_variable_value \
   2800 			--varname="${cur_%%=*}" --cur="${cur_#*=}"
   2801 		;;
   2802 	*)
   2803 		__git_complete_config_variable_name --cur="$cur_" --sfx='='
   2804 		;;
   2805 	esac
   2806 }
   2807 
   2808 _git_config ()
   2809 {
   2810 	case "$prev" in
   2811 	--get|--get-all|--unset|--unset-all)
   2812 		__gitcomp_nl "$(__git_config_get_set_variables)"
   2813 		return
   2814 		;;
   2815 	*.*)
   2816 		__git_complete_config_variable_value
   2817 		return
   2818 		;;
   2819 	esac
   2820 	case "$cur" in
   2821 	--*)
   2822 		__gitcomp_builtin config
   2823 		;;
   2824 	*)
   2825 		__git_complete_config_variable_name
   2826 		;;
   2827 	esac
   2828 }
   2829 
   2830 _git_remote ()
   2831 {
   2832 	local subcommands="
   2833 		add rename remove set-head set-branches
   2834 		get-url set-url show prune update
   2835 		"
   2836 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   2837 	if [ -z "$subcommand" ]; then
   2838 		case "$cur" in
   2839 		--*)
   2840 			__gitcomp_builtin remote
   2841 			;;
   2842 		*)
   2843 			__gitcomp "$subcommands"
   2844 			;;
   2845 		esac
   2846 		return
   2847 	fi
   2848 
   2849 	case "$subcommand,$cur" in
   2850 	add,--*)
   2851 		__gitcomp_builtin remote_add
   2852 		;;
   2853 	add,*)
   2854 		;;
   2855 	set-head,--*)
   2856 		__gitcomp_builtin remote_set-head
   2857 		;;
   2858 	set-branches,--*)
   2859 		__gitcomp_builtin remote_set-branches
   2860 		;;
   2861 	set-head,*|set-branches,*)
   2862 		__git_complete_remote_or_refspec
   2863 		;;
   2864 	update,--*)
   2865 		__gitcomp_builtin remote_update
   2866 		;;
   2867 	update,*)
   2868 		__gitcomp "$(__git_remotes) $(__git_get_config_variables "remotes")"
   2869 		;;
   2870 	set-url,--*)
   2871 		__gitcomp_builtin remote_set-url
   2872 		;;
   2873 	get-url,--*)
   2874 		__gitcomp_builtin remote_get-url
   2875 		;;
   2876 	prune,--*)
   2877 		__gitcomp_builtin remote_prune
   2878 		;;
   2879 	*)
   2880 		__gitcomp_nl "$(__git_remotes)"
   2881 		;;
   2882 	esac
   2883 }
   2884 
   2885 _git_replace ()
   2886 {
   2887 	case "$cur" in
   2888 	--format=*)
   2889 		__gitcomp "short medium long" "" "${cur##--format=}"
   2890 		return
   2891 		;;
   2892 	--*)
   2893 		__gitcomp_builtin replace
   2894 		return
   2895 		;;
   2896 	esac
   2897 	__git_complete_refs
   2898 }
   2899 
   2900 _git_rerere ()
   2901 {
   2902 	local subcommands="clear forget diff remaining status gc"
   2903 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   2904 	if test -z "$subcommand"
   2905 	then
   2906 		__gitcomp "$subcommands"
   2907 		return
   2908 	fi
   2909 }
   2910 
   2911 _git_reset ()
   2912 {
   2913 	__git_has_doubledash && return
   2914 
   2915 	case "$cur" in
   2916 	--*)
   2917 		__gitcomp_builtin reset
   2918 		return
   2919 		;;
   2920 	esac
   2921 	__git_complete_refs
   2922 }
   2923 
   2924 _git_restore ()
   2925 {
   2926 	case "$prev" in
   2927 	-s)
   2928 		__git_complete_refs
   2929 		return
   2930 		;;
   2931 	esac
   2932 
   2933 	case "$cur" in
   2934 	--conflict=*)
   2935 		__gitcomp "diff3 merge zdiff3" "" "${cur##--conflict=}"
   2936 		;;
   2937 	--source=*)
   2938 		__git_complete_refs --cur="${cur##--source=}"
   2939 		;;
   2940 	--*)
   2941 		__gitcomp_builtin restore
   2942 		;;
   2943 	*)
   2944 		if __git rev-parse --verify --quiet HEAD >/dev/null; then
   2945 			__git_complete_index_file "--modified"
   2946 		fi
   2947 	esac
   2948 }
   2949 
   2950 __git_revert_inprogress_options=$__git_sequencer_inprogress_options
   2951 
   2952 _git_revert ()
   2953 {
   2954 	__git_find_repo_path
   2955 	if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
   2956 		__gitcomp "$__git_revert_inprogress_options"
   2957 		return
   2958 	fi
   2959 	__git_complete_strategy && return
   2960 	case "$cur" in
   2961 	--*)
   2962 		__gitcomp_builtin revert "" \
   2963 			"$__git_revert_inprogress_options"
   2964 		return
   2965 		;;
   2966 	esac
   2967 	__git_complete_refs
   2968 }
   2969 
   2970 _git_rm ()
   2971 {
   2972 	case "$cur" in
   2973 	--*)
   2974 		__gitcomp_builtin rm
   2975 		return
   2976 		;;
   2977 	esac
   2978 
   2979 	__git_complete_index_file "--cached"
   2980 }
   2981 
   2982 _git_shortlog ()
   2983 {
   2984 	__git_has_doubledash && return
   2985 
   2986 	case "$cur" in
   2987 	--*)
   2988 		__gitcomp "
   2989 			$__git_log_common_options
   2990 			$__git_log_shortlog_options
   2991 			--numbered --summary --email
   2992 			"
   2993 		return
   2994 		;;
   2995 	esac
   2996 	__git_complete_revlist
   2997 }
   2998 
   2999 _git_show ()
   3000 {
   3001 	__git_has_doubledash && return
   3002 
   3003 	case "$cur" in
   3004 	--pretty=*|--format=*)
   3005 		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
   3006 			" "" "${cur#*=}"
   3007 		return
   3008 		;;
   3009 	--diff-algorithm=*)
   3010 		__gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
   3011 		return
   3012 		;;
   3013 	--submodule=*)
   3014 		__gitcomp "$__git_diff_submodule_formats" "" "${cur##--submodule=}"
   3015 		return
   3016 		;;
   3017 	--color-moved=*)
   3018 		__gitcomp "$__git_color_moved_opts" "" "${cur##--color-moved=}"
   3019 		return
   3020 		;;
   3021 	--color-moved-ws=*)
   3022 		__gitcomp "$__git_color_moved_ws_opts" "" "${cur##--color-moved-ws=}"
   3023 		return
   3024 		;;
   3025 	--ws-error-highlight=*)
   3026 		__gitcomp "$__git_ws_error_highlight_opts" "" "${cur##--ws-error-highlight=}"
   3027 		return
   3028 		;;
   3029 	--diff-merges=*)
   3030                 __gitcomp "$__git_diff_merges_opts" "" "${cur##--diff-merges=}"
   3031                 return
   3032                 ;;
   3033 	--*)
   3034 		__gitcomp "--pretty= --format= --abbrev-commit --no-abbrev-commit
   3035 			--oneline --show-signature
   3036 			--expand-tabs --expand-tabs= --no-expand-tabs
   3037 			$__git_log_show_options
   3038 			$__git_diff_common_options
   3039 			"
   3040 		return
   3041 		;;
   3042 	esac
   3043 	__git_complete_revlist_file
   3044 }
   3045 
   3046 _git_show_branch ()
   3047 {
   3048 	case "$cur" in
   3049 	--*)
   3050 		__gitcomp_builtin show-branch
   3051 		return
   3052 		;;
   3053 	esac
   3054 	__git_complete_revlist
   3055 }
   3056 
   3057 __gitcomp_directories ()
   3058 {
   3059 	local _tmp_dir _tmp_completions _found=0
   3060 
   3061 	# Get the directory of the current token; this differs from dirname
   3062 	# in that it keeps up to the final trailing slash.  If no slash found
   3063 	# that's fine too.
   3064 	[[ "$cur" =~ .*/ ]]
   3065 	_tmp_dir=$BASH_REMATCH
   3066 
   3067 	# Find possible directory completions, adding trailing '/' characters,
   3068 	# de-quoting, and handling unusual characters.
   3069 	while IFS= read -r -d $'\0' c ; do
   3070 		# If there are directory completions, find ones that start
   3071 		# with "$cur", the current token, and put those in COMPREPLY
   3072 		if [[ $c == "$cur"* ]]; then
   3073 			COMPREPLY+=("$c/")
   3074 			_found=1
   3075 		fi
   3076 	done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir)
   3077 
   3078 	if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
   3079 		# No possible further completions any deeper, so assume we're at
   3080 		# a leaf directory and just consider it complete
   3081 		__gitcomp_direct_append "$cur "
   3082 	fi
   3083 }
   3084 
   3085 _git_sparse_checkout ()
   3086 {
   3087 	local subcommands="list init set disable add reapply"
   3088 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   3089 	if [ -z "$subcommand" ]; then
   3090 		__gitcomp "$subcommands"
   3091 		return
   3092 	fi
   3093 
   3094 	case "$subcommand,$cur" in
   3095 	*,--*)
   3096 		__gitcomp_builtin sparse-checkout_$subcommand "" "--"
   3097 		;;
   3098 	set,*|add,*)
   3099 		if [ "$(__git config core.sparseCheckoutCone)" == "true" ] ||
   3100 		[ -n "$(__git_find_on_cmdline --cone)" ]; then
   3101 			__gitcomp_directories
   3102 		fi
   3103 	esac
   3104 }
   3105 
   3106 _git_stash ()
   3107 {
   3108 	local subcommands='push list show apply clear drop pop create branch'
   3109 	local subcommand="$(__git_find_on_cmdline "$subcommands save")"
   3110 
   3111 	if [ -z "$subcommand" ]; then
   3112 		case "$((cword - __git_cmd_idx)),$cur" in
   3113 		*,--*)
   3114 			__gitcomp_builtin stash_push
   3115 			;;
   3116 		1,sa*)
   3117 			__gitcomp "save"
   3118 			;;
   3119 		1,*)
   3120 			__gitcomp "$subcommands"
   3121 			;;
   3122 		esac
   3123 		return
   3124 	fi
   3125 
   3126 	case "$subcommand,$cur" in
   3127 	list,--*)
   3128 		# NEEDSWORK: can we somehow unify this with the options in _git_log() and _git_show()
   3129 		__gitcomp_builtin stash_list "$__git_log_common_options $__git_diff_common_options"
   3130 		;;
   3131 	show,--*)
   3132 		__gitcomp_builtin stash_show "$__git_diff_common_options"
   3133 		;;
   3134 	*,--*)
   3135 		__gitcomp_builtin "stash_$subcommand"
   3136 		;;
   3137 	branch,*)
   3138 		if [ $cword -eq $((__git_cmd_idx+2)) ]; then
   3139 			__git_complete_refs
   3140 		else
   3141 			__gitcomp_nl "$(__git stash list \
   3142 					| sed -n -e 's/:.*//p')"
   3143 		fi
   3144 		;;
   3145 	show,*|apply,*|drop,*|pop,*)
   3146 		__gitcomp_nl "$(__git stash list \
   3147 				| sed -n -e 's/:.*//p')"
   3148 		;;
   3149 	esac
   3150 }
   3151 
   3152 _git_submodule ()
   3153 {
   3154 	__git_has_doubledash && return
   3155 
   3156 	local subcommands="add status init deinit update set-branch set-url summary foreach sync absorbgitdirs"
   3157 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   3158 	if [ -z "$subcommand" ]; then
   3159 		case "$cur" in
   3160 		--*)
   3161 			__gitcomp "--quiet"
   3162 			;;
   3163 		*)
   3164 			__gitcomp "$subcommands"
   3165 			;;
   3166 		esac
   3167 		return
   3168 	fi
   3169 
   3170 	case "$subcommand,$cur" in
   3171 	add,--*)
   3172 		__gitcomp "--branch --force --name --reference --depth"
   3173 		;;
   3174 	status,--*)
   3175 		__gitcomp "--cached --recursive"
   3176 		;;
   3177 	deinit,--*)
   3178 		__gitcomp "--force --all"
   3179 		;;
   3180 	update,--*)
   3181 		__gitcomp "
   3182 			--init --remote --no-fetch
   3183 			--recommend-shallow --no-recommend-shallow
   3184 			--force --rebase --merge --reference --depth --recursive --jobs
   3185 		"
   3186 		;;
   3187 	set-branch,--*)
   3188 		__gitcomp "--default --branch"
   3189 		;;
   3190 	summary,--*)
   3191 		__gitcomp "--cached --files --summary-limit"
   3192 		;;
   3193 	foreach,--*|sync,--*)
   3194 		__gitcomp "--recursive"
   3195 		;;
   3196 	*)
   3197 		;;
   3198 	esac
   3199 }
   3200 
   3201 _git_svn ()
   3202 {
   3203 	local subcommands="
   3204 		init fetch clone rebase dcommit log find-rev
   3205 		set-tree commit-diff info create-ignore propget
   3206 		proplist show-ignore show-externals branch tag blame
   3207 		migrate mkdirs reset gc
   3208 		"
   3209 	local subcommand="$(__git_find_on_cmdline "$subcommands")"
   3210 	if [ -z "$subcommand" ]; then
   3211 		__gitcomp "$subcommands"
   3212 	else
   3213 		local remote_opts="--username= --config-dir= --no-auth-cache"
   3214 		local fc_opts="
   3215 			--follow-parent --authors-file= --repack=
   3216 			--no-metadata --use-svm-props --use-svnsync-props
   3217 			--log-window-size= --no-checkout --quiet
   3218 			--repack-flags --use-log-author --localtime
   3219 			--add-author-from
   3220 			--recursive
   3221 			--ignore-paths= --include-paths= $remote_opts
   3222 			"
   3223 		local init_opts="
   3224 			--template= --shared= --trunk= --tags=
   3225 			--branches= --stdlayout --minimize-url
   3226 			--no-metadata --use-svm-props --use-svnsync-props
   3227 			--rewrite-root= --prefix= $remote_opts
   3228 			"
   3229 		local cmt_opts="
   3230 			--edit --rmdir --find-copies-harder --copy-similarity=
   3231 			"
   3232 
   3233 		case "$subcommand,$cur" in
   3234 		fetch,--*)
   3235 			__gitcomp "--revision= --fetch-all $fc_opts"
   3236 			;;
   3237 		clone,--*)
   3238 			__gitcomp "--revision= $fc_opts $init_opts"
   3239 			;;
   3240 		init,--*)
   3241 			__gitcomp "$init_opts"
   3242 			;;
   3243 		dcommit,--*)
   3244 			__gitcomp "
   3245 				--merge --strategy= --verbose --dry-run
   3246 				--fetch-all --no-rebase --commit-url
   3247 				--revision --interactive $cmt_opts $fc_opts
   3248 				"
   3249 			;;
   3250 		set-tree,--*)
   3251 			__gitcomp "--stdin $cmt_opts $fc_opts"
   3252 			;;
   3253 		create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
   3254 		show-externals,--*|mkdirs,--*)
   3255 			__gitcomp "--revision="
   3256 			;;
   3257 		log,--*)
   3258 			__gitcomp "
   3259 				--limit= --revision= --verbose --incremental
   3260 				--oneline --show-commit --non-recursive
   3261 				--authors-file= --color
   3262 				"
   3263 			;;
   3264 		rebase,--*)
   3265 			__gitcomp "
   3266 				--merge --verbose --strategy= --local
   3267 				--fetch-all --dry-run $fc_opts
   3268 				"
   3269 			;;
   3270 		commit-diff,--*)
   3271 			__gitcomp "--message= --file= --revision= $cmt_opts"
   3272 			;;
   3273 		info,--*)
   3274 			__gitcomp "--url"
   3275 			;;
   3276 		branch,--*)
   3277 			__gitcomp "--dry-run --message --tag"
   3278 			;;
   3279 		tag,--*)
   3280 			__gitcomp "--dry-run --message"
   3281 			;;
   3282 		blame,--*)
   3283 			__gitcomp "--git-format"
   3284 			;;
   3285 		migrate,--*)
   3286 			__gitcomp "
   3287 				--config-dir= --ignore-paths= --minimize
   3288 				--no-auth-cache --username=
   3289 				"
   3290 			;;
   3291 		reset,--*)
   3292 			__gitcomp "--revision= --parent"
   3293 			;;
   3294 		*)
   3295 			;;
   3296 		esac
   3297 	fi
   3298 }
   3299 
   3300 _git_tag ()
   3301 {
   3302 	local i c="$__git_cmd_idx" f=0
   3303 	while [ $c -lt $cword ]; do
   3304 		i="${words[c]}"
   3305 		case "$i" in
   3306 		-d|--delete|-v|--verify)
   3307 			__gitcomp_direct "$(__git_tags "" "$cur" " ")"
   3308 			return
   3309 			;;
   3310 		-f)
   3311 			f=1
   3312 			;;
   3313 		esac
   3314 		((c++))
   3315 	done
   3316 
   3317 	case "$prev" in
   3318 	-m|-F)
   3319 		;;
   3320 	-*|tag)
   3321 		if [ $f = 1 ]; then
   3322 			__gitcomp_direct "$(__git_tags "" "$cur" " ")"
   3323 		fi
   3324 		;;
   3325 	*)
   3326 		__git_complete_refs
   3327 		;;
   3328 	esac
   3329 
   3330 	case "$cur" in
   3331 	--*)
   3332 		__gitcomp_builtin tag
   3333 		;;
   3334 	esac
   3335 }
   3336 
   3337 _git_whatchanged ()
   3338 {
   3339 	_git_log
   3340 }
   3341 
   3342 __git_complete_worktree_paths ()
   3343 {
   3344 	local IFS=$'\n'
   3345 	# Generate completion reply from worktree list skipping the first
   3346 	# entry: it's the path of the main worktree, which can't be moved,
   3347 	# removed, locked, etc.
   3348 	__gitcomp_nl "$(git worktree list --porcelain |
   3349 		sed -n -e '2,$ s/^worktree //p')"
   3350 }
   3351 
   3352 _git_worktree ()
   3353 {
   3354 	local subcommands="add list lock move prune remove unlock"
   3355 	local subcommand subcommand_idx
   3356 
   3357 	subcommand="$(__git_find_on_cmdline --show-idx "$subcommands")"
   3358 	subcommand_idx="${subcommand% *}"
   3359 	subcommand="${subcommand#* }"
   3360 
   3361 	case "$subcommand,$cur" in
   3362 	,*)
   3363 		__gitcomp "$subcommands"
   3364 		;;
   3365 	*,--*)
   3366 		__gitcomp_builtin worktree_$subcommand
   3367 		;;
   3368 	add,*)	# usage: git worktree add [<options>] <path> [<commit-ish>]
   3369 		# Here we are not completing an --option, it's either the
   3370 		# path or a ref.
   3371 		case "$prev" in
   3372 		-b|-B)	# Complete refs for branch to be created/reseted.
   3373 			__git_complete_refs
   3374 			;;
   3375 		-*)	# The previous word is an -o|--option without an
   3376 			# unstuck argument: have to complete the path for
   3377 			# the new worktree, so don't list anything, but let
   3378 			# Bash fall back to filename completion.
   3379 			;;
   3380 		*)	# The previous word is not an --option, so it must
   3381 			# be either the 'add' subcommand, the unstuck
   3382 			# argument of an option (e.g. branch for -b|-B), or
   3383 			# the path for the new worktree.
   3384 			if [ $cword -eq $((subcommand_idx+1)) ]; then
   3385 				# Right after the 'add' subcommand: have to
   3386 				# complete the path, so fall back to Bash
   3387 				# filename completion.
   3388 				:
   3389 			else
   3390 				case "${words[cword-2]}" in
   3391 				-b|-B)	# After '-b <branch>': have to
   3392 					# complete the path, so fall back
   3393 					# to Bash filename completion.
   3394 					;;
   3395 				*)	# After the path: have to complete
   3396 					# the ref to be checked out.
   3397 					__git_complete_refs
   3398 					;;
   3399 				esac
   3400 			fi
   3401 			;;
   3402 		esac
   3403 		;;
   3404 	lock,*|remove,*|unlock,*)
   3405 		__git_complete_worktree_paths
   3406 		;;
   3407 	move,*)
   3408 		if [ $cword -eq $((subcommand_idx+1)) ]; then
   3409 			# The first parameter must be an existing working
   3410 			# tree to be moved.
   3411 			__git_complete_worktree_paths
   3412 		else
   3413 			# The second parameter is the destination: it could
   3414 			# be any path, so don't list anything, but let Bash
   3415 			# fall back to filename completion.
   3416 			:
   3417 		fi
   3418 		;;
   3419 	esac
   3420 }
   3421 
   3422 __git_complete_common () {
   3423 	local command="$1"
   3424 
   3425 	case "$cur" in
   3426 	--*)
   3427 		__gitcomp_builtin "$command"
   3428 		;;
   3429 	esac
   3430 }
   3431 
   3432 __git_cmds_with_parseopt_helper=
   3433 __git_support_parseopt_helper () {
   3434 	test -n "$__git_cmds_with_parseopt_helper" ||
   3435 		__git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
   3436 
   3437 	case " $__git_cmds_with_parseopt_helper " in
   3438 	*" $1 "*)
   3439 		return 0
   3440 		;;
   3441 	*)
   3442 		return 1
   3443 		;;
   3444 	esac
   3445 }
   3446 
   3447 __git_have_func () {
   3448 	declare -f -- "$1" >/dev/null 2>&1
   3449 }
   3450 
   3451 __git_complete_command () {
   3452 	local command="$1"
   3453 	local completion_func="_git_${command//-/_}"
   3454 	if ! __git_have_func $completion_func &&
   3455 		__git_have_func _completion_loader
   3456 	then
   3457 		_completion_loader "git-$command"
   3458 	fi
   3459 	if __git_have_func $completion_func
   3460 	then
   3461 		$completion_func
   3462 		return 0
   3463 	elif __git_support_parseopt_helper "$command"
   3464 	then
   3465 		__git_complete_common "$command"
   3466 		return 0
   3467 	else
   3468 		return 1
   3469 	fi
   3470 }
   3471 
   3472 __git_main ()
   3473 {
   3474 	local i c=1 command __git_dir __git_repo_path
   3475 	local __git_C_args C_args_count=0
   3476 	local __git_cmd_idx
   3477 
   3478 	while [ $c -lt $cword ]; do
   3479 		i="${words[c]}"
   3480 		case "$i" in
   3481 		--git-dir=*)
   3482 			__git_dir="${i#--git-dir=}"
   3483 			;;
   3484 		--git-dir)
   3485 			((c++))
   3486 			__git_dir="${words[c]}"
   3487 			;;
   3488 		--bare)
   3489 			__git_dir="."
   3490 			;;
   3491 		--help)
   3492 			command="help"
   3493 			break
   3494 			;;
   3495 		-c|--work-tree|--namespace)
   3496 			((c++))
   3497 			;;
   3498 		-C)
   3499 			__git_C_args[C_args_count++]=-C
   3500 			((c++))
   3501 			__git_C_args[C_args_count++]="${words[c]}"
   3502 			;;
   3503 		-*)
   3504 			;;
   3505 		*)
   3506 			command="$i"
   3507 			__git_cmd_idx="$c"
   3508 			break
   3509 			;;
   3510 		esac
   3511 		((c++))
   3512 	done
   3513 
   3514 	if [ -z "${command-}" ]; then
   3515 		case "$prev" in
   3516 		--git-dir|-C|--work-tree)
   3517 			# these need a path argument, let's fall back to
   3518 			# Bash filename completion
   3519 			return
   3520 			;;
   3521 		-c)
   3522 			__git_complete_config_variable_name_and_value
   3523 			return
   3524 			;;
   3525 		--namespace)
   3526 			# we don't support completing these options' arguments
   3527 			return
   3528 			;;
   3529 		esac
   3530 		case "$cur" in
   3531 		--*)
   3532 			__gitcomp "
   3533 			--paginate
   3534 			--no-pager
   3535 			--git-dir=
   3536 			--bare
   3537 			--version
   3538 			--exec-path
   3539 			--exec-path=
   3540 			--html-path
   3541 			--man-path
   3542 			--info-path
   3543 			--work-tree=
   3544 			--namespace=
   3545 			--no-replace-objects
   3546 			--help
   3547 			"
   3548 			;;
   3549 		*)
   3550 			if test -n "${GIT_TESTING_PORCELAIN_COMMAND_LIST-}"
   3551 			then
   3552 				__gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
   3553 			else
   3554 				local list_cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
   3555 
   3556 				if test "${GIT_COMPLETION_SHOW_ALL_COMMANDS-}" = "1"
   3557 				then
   3558 					list_cmds=builtins,$list_cmds
   3559 				fi
   3560 				__gitcomp "$(__git --list-cmds=$list_cmds)"
   3561 			fi
   3562 			;;
   3563 		esac
   3564 		return
   3565 	fi
   3566 
   3567 	__git_complete_command "$command" && return
   3568 
   3569 	local expansion=$(__git_aliased_command "$command")
   3570 	if [ -n "$expansion" ]; then
   3571 		words[1]=$expansion
   3572 		__git_complete_command "$expansion"
   3573 	fi
   3574 }
   3575 
   3576 __gitk_main ()
   3577 {
   3578 	__git_has_doubledash && return
   3579 
   3580 	local __git_repo_path
   3581 	__git_find_repo_path
   3582 
   3583 	local merge=""
   3584 	if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
   3585 		merge="--merge"
   3586 	fi
   3587 	case "$cur" in
   3588 	--*)
   3589 		__gitcomp "
   3590 			$__git_log_common_options
   3591 			$__git_log_gitk_options
   3592 			$merge
   3593 			"
   3594 		return
   3595 		;;
   3596 	esac
   3597 	__git_complete_revlist
   3598 }
   3599 
   3600 if [[ -n ${ZSH_VERSION-} && -z ${GIT_SOURCING_ZSH_COMPLETION-} ]]; then
   3601 	echo "ERROR: this script is obsolete, please see git-completion.zsh" 1>&2
   3602 	return
   3603 fi
   3604 
   3605 __git_func_wrap ()
   3606 {
   3607 	local cur words cword prev
   3608 	local __git_cmd_idx=0
   3609 	_get_comp_words_by_ref -n =: cur words cword prev
   3610 	$1
   3611 }
   3612 
   3613 ___git_complete ()
   3614 {
   3615 	local wrapper="__git_wrap${2}"
   3616 	eval "$wrapper () { __git_func_wrap $2 ; }"
   3617 	complete -o bashdefault -o default -o nospace -F $wrapper $1 2>/dev/null \
   3618 		|| complete -o default -o nospace -F $wrapper $1
   3619 }
   3620 
   3621 # Setup the completion for git commands
   3622 # 1: command or alias
   3623 # 2: function to call (e.g. `git`, `gitk`, `git_fetch`)
   3624 __git_complete ()
   3625 {
   3626 	local func
   3627 
   3628 	if __git_have_func $2; then
   3629 		func=$2
   3630 	elif __git_have_func __$2_main; then
   3631 		func=__$2_main
   3632 	elif __git_have_func _$2; then
   3633 		func=_$2
   3634 	else
   3635 		echo "ERROR: could not find function '$2'" 1>&2
   3636 		return 1
   3637 	fi
   3638 	___git_complete $1 $func
   3639 }
   3640 
   3641 ___git_complete git __git_main
   3642 ___git_complete gitk __gitk_main
   3643 
   3644 # The following are necessary only for Cygwin, and only are needed
   3645 # when the user has tab-completed the executable name and consequently
   3646 # included the '.exe' suffix.
   3647 #
   3648 if [ "$OSTYPE" = cygwin ]; then
   3649 	___git_complete git.exe __git_main
   3650 fi