Parent directory

git-completion.bash

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