git-completion.bash 55.6 KB
Newer Older
1
#!bash
2
#
3
# bash/zsh completion support for core Git.
4
#
5
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
6
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
7
# Distributed under the GNU General Public License, version 2.0.
8 9 10 11 12 13 14 15
#
# The contained completion routines provide support for completing:
#
#    *) local and remote branch names
#    *) local and remote tag names
#    *) .git/remotes file names
#    *) git 'subcommands'
#    *) tree paths within 'ref:path/to/file' expressions
16
#    *) common --long-options
17 18 19 20
#
# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
21
#    2) Add the following line to your .bashrc/.zshrc:
22 23
#        source ~/.git-completion.sh
#
J
Jonathan Nieder 已提交
24
#    3) Consider changing your PS1 to also show the current branch:
25 26
#         Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#         ZSH:  PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ '
27 28 29 30 31
#
#       The argument to __git_ps1 will be displayed only if you
#       are currently in a git repository.  The %s token will be
#       the name of the current branch.
#
32 33 34 35 36
#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
#       value, unstaged (*) and staged (+) changes will be shown next
#       to the branch name.  You can configure this per-repository
#       with the bash.showDirtyState variable, which defaults to true
#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
37
#
38 39 40 41
#       You can also see if currently something is stashed, by setting
#       GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
#       then a '$' will be shown next to the branch name.
#
42 43 44 45
#       If you would like to see if there're untracked files, then you can
#       set GIT_PS1_SHOWUNTRACKEDFILES to a nonempty value. If there're
#       untracked files, then a '%' will be shown next to the branch name.
#
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#       If you would like to see the difference between HEAD and its
#       upstream, set GIT_PS1_SHOWUPSTREAM="auto".  A "<" indicates
#       you are behind, ">" indicates you are ahead, and "<>"
#       indicates you have diverged.  You can further control
#       behaviour by setting GIT_PS1_SHOWUPSTREAM to a space-separated
#       list of values:
#           verbose       show number of commits ahead/behind (+/-) upstream
#           legacy        don't use the '--count' option available in recent
#                         versions of git-rev-list
#           git           always compare HEAD to @{upstream}
#           svn           always compare HEAD to your SVN upstream
#       By default, __git_ps1 will compare HEAD to your SVN upstream
#       if it can find one, or @{upstream} otherwise.  Once you have
#       set GIT_PS1_SHOWUPSTREAM, you can override it on a
#       per-repository basis by setting the bash.showUpstream config
#       variable.
#
#
64 65 66 67 68 69 70 71 72 73 74
# To submit patches:
#
#    *) Read Documentation/SubmittingPatches
#    *) Send all patches to the current maintainer:
#
#       "Shawn O. Pearce" <spearce@spearce.org>
#
#    *) Always CC the Git mailing list:
#
#       git@vger.kernel.org
#
75

76 77 78 79
if [[ -n ${ZSH_VERSION-} ]]; then
	autoload -U +X bashcompinit && bashcompinit
fi

80 81 82 83 84
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac

85 86
# __gitdir accepts 0 or 1 arguments (i.e., location)
# returns location of .git repo
87 88
__gitdir ()
{
89
	if [ -z "${1-}" ]; then
90
		if [ -n "${__git_dir-}" ]; then
91 92 93 94 95 96 97 98 99 100 101
			echo "$__git_dir"
		elif [ -d .git ]; then
			echo .git
		else
			git rev-parse --git-dir 2>/dev/null
		fi
	elif [ -d "$1/.git" ]; then
		echo "$1/.git"
	else
		echo "$1"
	fi
102 103
}

104 105 106 107 108 109 110 111 112
# stores the divergence from upstream in $p
# used by GIT_PS1_SHOWUPSTREAM
__git_ps1_show_upstream ()
{
	local key value
	local svn_remote=() svn_url_pattern count n
	local upstream=git legacy="" verbose=""

	# get some config options from git-config
113
	local output="$(git config -z --get-regexp '^(svn-remote\..*\.url|bash\.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')"
114
	while read -r key value; do
115 116 117 118 119 120 121 122 123 124 125 126 127 128
		case "$key" in
		bash.showupstream)
			GIT_PS1_SHOWUPSTREAM="$value"
			if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then
				p=""
				return
			fi
			;;
		svn-remote.*.url)
			svn_remote[ $((${#svn_remote[@]} + 1)) ]="$value"
			svn_url_pattern+="\\|$value"
			upstream=svn+git # default upstream is SVN if available, else git
			;;
		esac
129
	done <<< "$output"
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

	# parse configuration values
	for option in ${GIT_PS1_SHOWUPSTREAM}; do
		case "$option" in
		git|svn) upstream="$option" ;;
		verbose) verbose=1 ;;
		legacy)  legacy=1  ;;
		esac
	done

	# Find our upstream
	case "$upstream" in
	git)    upstream="@{upstream}" ;;
	svn*)
		# get the upstream from the "git-svn-id: ..." in a commit message
		# (git-svn uses essentially the same procedure internally)
		local svn_upstream=($(git log --first-parent -1 \
147
					--grep="^git-svn-id: \(${svn_url_pattern#??}\)" 2>/dev/null))
148 149 150
		if [[ 0 -ne ${#svn_upstream[@]} ]]; then
			svn_upstream=${svn_upstream[ ${#svn_upstream[@]} - 2 ]}
			svn_upstream=${svn_upstream%@*}
151 152
			local n_stop="${#svn_remote[@]}"
			for ((n=1; n <= n_stop; ++n)); do
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
				svn_upstream=${svn_upstream#${svn_remote[$n]}}
			done

			if [[ -z "$svn_upstream" ]]; then
				# default branch name for checkouts with no layout:
				upstream=${GIT_SVN_ID:-git-svn}
			else
				upstream=${svn_upstream#/}
			fi
		elif [[ "svn+git" = "$upstream" ]]; then
			upstream="@{upstream}"
		fi
		;;
	esac

	# Find how many commits we are ahead/behind our upstream
	if [[ -z "$legacy" ]]; then
		count="$(git rev-list --count --left-right \
				"$upstream"...HEAD 2>/dev/null)"
	else
		# produce equivalent output to --count for older versions of git
		local commits
		if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
		then
			local commit behind=0 ahead=0
			for commit in $commits
			do
				case "$commit" in
				"<"*) let ++behind
					;;
				*)    let ++ahead
					;;
				esac
			done
			count="$behind	$ahead"
		else
			count=""
		fi
	fi

	# calculate the result
	if [[ -z "$verbose" ]]; then
		case "$count" in
		"") # no upstream
			p="" ;;
		"0	0") # equal to upstream
			p="=" ;;
		"0	"*) # ahead of upstream
			p=">" ;;
		*"	0") # behind upstream
			p="<" ;;
		*)	    # diverged from upstream
			p="<>" ;;
		esac
	else
		case "$count" in
		"") # no upstream
			p="" ;;
		"0	0") # equal to upstream
			p=" u=" ;;
		"0	"*) # ahead of upstream
			p=" u+${count#0	}" ;;
		*"	0") # behind upstream
			p=" u-${count%	0}" ;;
		*)	    # diverged from upstream
			p=" u+${count#*	}-${count%	*}" ;;
		esac
	fi

}


225 226
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
# returns text to add to bash PS1 prompt (includes branch name)
227 228
__git_ps1 ()
{
229
	local g="$(__gitdir)"
230
	if [ -n "$g" ]; then
231 232
		local r=""
		local b=""
233
		if [ -f "$g/rebase-merge/interactive" ]; then
234
			r="|REBASE-i"
235
			b="$(cat "$g/rebase-merge/head-name")"
236
		elif [ -d "$g/rebase-merge" ]; then
237
			r="|REBASE-m"
238
			b="$(cat "$g/rebase-merge/head-name")"
239
		else
240 241 242 243 244 245 246 247 248
			if [ -d "$g/rebase-apply" ]; then
				if [ -f "$g/rebase-apply/rebasing" ]; then
					r="|REBASE"
				elif [ -f "$g/rebase-apply/applying" ]; then
					r="|AM"
				else
					r="|AM/REBASE"
				fi
			elif [ -f "$g/MERGE_HEAD" ]; then
249
				r="|MERGING"
250 251
			elif [ -f "$g/CHERRY_PICK_HEAD" ]; then
				r="|CHERRY-PICKING"
252
			elif [ -f "$g/BISECT_LOG" ]; then
253 254
				r="|BISECTING"
			fi
255 256

			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
257 258 259 260 261 262 263 264 265 266

				b="$(
				case "${GIT_PS1_DESCRIBE_STYLE-}" in
				(contains)
					git describe --contains HEAD ;;
				(branch)
					git describe --contains --all HEAD ;;
				(describe)
					git describe HEAD ;;
				(* | default)
K
knittl 已提交
267
					git describe --tags --exact-match HEAD ;;
268 269
				esac 2>/dev/null)" ||

270 271 272 273
				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
				b="unknown"
				b="($b)"
			}
274 275
		fi

276 277 278 279 280
		local w=""
		local i=""
		local s=""
		local u=""
		local c=""
281
		local p=""
282

283
		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
284
			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
285 286 287 288
				c="BARE:"
			else
				b="GIT_DIR!"
			fi
289 290 291
		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
				if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
292
					git diff --no-ext-diff --quiet --exit-code || w="*"
293
					if git rev-parse --quiet --verify HEAD >/dev/null; then
294
						git diff-index --cached --quiet HEAD -- || i="+"
295 296 297
					else
						i="#"
					fi
298 299
				fi
			fi
300 301 302
			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
			        git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
			fi
303 304 305 306 307 308

			if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ]; then
			   if [ -n "$(git ls-files --others --exclude-standard)" ]; then
			      u="%"
			   fi
			fi
309 310 311 312

			if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
				__git_ps1_show_upstream
			fi
313 314
		fi

315
		local f="$w$i$s$u"
316
		printf "${1:- (%s)}" "$c${b##refs/heads/}${f:+ $f}$r$p"
317 318 319
	fi
}

320
# __gitcomp_1 requires 2 arguments
321 322 323 324 325 326 327 328 329 330 331 332
__gitcomp_1 ()
{
	local c IFS=' '$'\t'$'\n'
	for c in $1; do
		case "$c$2" in
		--*=*) printf %s$'\n' "$c$2" ;;
		*.)    printf %s$'\n' "$c$2" ;;
		*)     printf %s$'\n' "$c$2 " ;;
		esac
	done
}

333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
# The following function is based on code from:
#
#   bash_completion - programmable completion functions for bash 3.2+
#
#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
#             © 2009-2010, Bash Completion Maintainers
#                     <bash-completion-devel@lists.alioth.debian.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2, or (at your option)
#   any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#   The latest version of this software can be obtained here:
#
#   http://bash-completion.alioth.debian.org/
#
#   RELEASE: 2.x

# This function can be used to access a tokenized list of words
# on the command line:
#
#	__git_reassemble_comp_words_by_ref '=:'
#	if test "${words_[cword_-1]}" = -w
#	then
#		...
#	fi
#
# The argument should be a collection of characters from the list of
# word completion separators (COMP_WORDBREAKS) to treat as ordinary
# characters.
#
# This is roughly equivalent to going back in time and setting
# COMP_WORDBREAKS to exclude those characters.  The intent is to
# make option types like --date=<type> and <rev>:<path> easy to
# recognize by treating each shell word as a single token.
#
# It is best not to set COMP_WORDBREAKS directly because the value is
# shared with other completion scripts.  By the time the completion
# function gets called, COMP_WORDS has already been populated so local
# changes to COMP_WORDBREAKS have no effect.
#
# Output: words_, cword_, cur_.

__git_reassemble_comp_words_by_ref()
{
	local exclude i j first
	# Which word separators to exclude?
	exclude="${1//[^$COMP_WORDBREAKS]}"
	cword_=$COMP_CWORD
	if [ -z "$exclude" ]; then
		words_=("${COMP_WORDS[@]}")
		return
	fi
	# List of word completion separators has shrunk;
	# re-assemble words to complete.
	for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
		# Append each nonempty word consisting of just
		# word separator characters to the current word.
		first=t
		while
			[ $i -gt 0 ] &&
			[ -n "${COMP_WORDS[$i]}" ] &&
			# word consists of excluded word separators
			[ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
		do
			# Attach to the previous token,
			# unless the previous token is the command name.
			if [ $j -ge 2 ] && [ -n "$first" ]; then
				((j--))
			fi
			first=
			words_[$j]=${words_[j]}${COMP_WORDS[i]}
			if [ $i = $COMP_CWORD ]; then
				cword_=$j
			fi
			if (($i < ${#COMP_WORDS[@]} - 1)); then
				((i++))
			else
				# Done.
				return
			fi
		done
		words_[$j]=${words_[j]}${COMP_WORDS[i]}
		if [ $i = $COMP_CWORD ]; then
			cword_=$j
		fi
	done
}

432
if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
433
if [[ -z ${ZSH_VERSION:+set} ]]; then
434 435
_get_comp_words_by_ref ()
{
436 437 438 439 440 441 442
	local exclude cur_ words_ cword_
	if [ "$1" = "-n" ]; then
		exclude=$2
		shift 2
	fi
	__git_reassemble_comp_words_by_ref "$exclude"
	cur_=${words_[cword_]}
443 444 445
	while [ $# -gt 0 ]; do
		case "$1" in
		cur)
446
			cur=$cur_
447 448
			;;
		prev)
449
			prev=${words_[$cword_-1]}
450 451
			;;
		words)
452
			words=("${words_[@]}")
453 454
			;;
		cword)
455
			cword=$cword_
456 457 458 459 460
			;;
		esac
		shift
	done
}
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
else
_get_comp_words_by_ref ()
{
	while [ $# -gt 0 ]; do
		case "$1" in
		cur)
			cur=${COMP_WORDS[COMP_CWORD]}
			;;
		prev)
			prev=${COMP_WORDS[COMP_CWORD-1]}
			;;
		words)
			words=("${COMP_WORDS[@]}")
			;;
		cword)
			cword=$COMP_CWORD
			;;
		-n)
			# assume COMP_WORDBREAKS is already set sanely
			shift
			;;
		esac
		shift
	done
}
fi
487 488
fi

S
SZEDER Gábor 已提交
489 490 491 492 493 494 495
# Generates completion reply with compgen, appending a space to possible
# completion words, if necessary.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word (optional).
496 497
__gitcomp ()
{
498 499
	local cur_="$cur"

500
	if [ $# -gt 2 ]; then
501
		cur_="$3"
502
	fi
503
	case "$cur_" in
504 505 506 507
	--*=)
		COMPREPLY=()
		;;
	*)
508
		local IFS=$'\n'
509 510
		COMPREPLY=($(compgen -P "${2-}" \
			-W "$(__gitcomp_1 "${1-}" "${4-}")" \
511
			-- "$cur_"))
512 513
		;;
	esac
514 515
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
# Generates completion reply with compgen from newline-separated possible
# completion words by appending a space to all of them.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words, separated by a single newline.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word instead of
#    the default space (optional).  If specified but empty, nothing is
#    appended.
__gitcomp_nl ()
{
	local s=$'\n' IFS=' '$'\t'$'\n'
	local cur_="$cur" suffix=" "

	if [ $# -gt 2 ]; then
		cur_="$3"
		if [ $# -gt 3 ]; then
			suffix="$4"
		fi
	fi

	IFS=$s
	COMPREPLY=($(compgen -P "${2-}" -S "$suffix" -W "$1" -- "$cur_"))
}

541 542
__git_heads ()
{
543
	local dir="$(__gitdir)"
544
	if [ -d "$dir" ]; then
545 546
		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
			refs/heads
547 548 549 550
		return
	fi
}

551 552
__git_tags ()
{
553
	local dir="$(__gitdir)"
554
	if [ -d "$dir" ]; then
555 556
		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
			refs/tags
557 558 559 560
		return
	fi
}

561 562 563
# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
# presence of 2nd argument means use the guess heuristic employed
# by checkout for tracking branches
564 565
__git_refs ()
{
566
	local i hash dir="$(__gitdir "${1-}")" track="${2-}"
567
	local format refs
568
	if [ -d "$dir" ]; then
S
SZEDER Gábor 已提交
569 570 571 572
		case "$cur" in
		refs|refs/*)
			format="refname"
			refs="${cur%/*}"
573
			track=""
S
SZEDER Gábor 已提交
574 575
			;;
		*)
576 577 578
			for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
				if [ -e "$dir/$i" ]; then echo $i; fi
			done
S
SZEDER Gábor 已提交
579 580 581 582 583 584
			format="refname:short"
			refs="refs/tags refs/heads refs/remotes"
			;;
		esac
		git --git-dir="$dir" for-each-ref --format="%($format)" \
			$refs
585 586 587 588 589 590 591
		if [ -n "$track" ]; then
			# employ the heuristic used by git checkout
			# Try to find a remote branch that matches the completion word
			# but only output if the branch name is unique
			local ref entry
			git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
				"refs/remotes/" | \
592
			while read -r entry; do
593 594 595 596 597 598 599
				eval "$entry"
				ref="${ref#*/}"
				if [[ "$ref" == "$cur"* ]]; then
					echo "$ref"
				fi
			done | uniq -u
		fi
600
		return
601
	fi
602 603 604
	case "$cur" in
	refs|refs/*)
		git ls-remote "$dir" "$cur*" 2>/dev/null | \
605
		while read -r hash i; do
606 607 608 609 610 611 612 613
			case "$i" in
			*^{}) ;;
			*) echo "$i" ;;
			esac
		done
		;;
	*)
		git ls-remote "$dir" HEAD ORIG_HEAD 'refs/tags/*' 'refs/heads/*' 'refs/remotes/*' 2>/dev/null | \
614
		while read -r hash i; do
615 616 617 618 619 620 621 622
			case "$i" in
			*^{}) ;;
			refs/*) echo "${i#refs/*/}" ;;
			*) echo "$i" ;;
			esac
		done
		;;
	esac
623 624
}

625
# __git_refs2 requires 1 argument (to pass to __git_refs)
626 627
__git_refs2 ()
{
628 629 630
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
631 632 633
	done
}

634
# __git_refs_remotes requires 1 argument (to pass to ls-remote)
635 636
__git_refs_remotes ()
{
637 638
	local i hash
	git ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
639
	while read -r hash i; do
640
		echo "$i:refs/remotes/$1/${i#refs/heads/}"
641 642 643
	done
}

644 645
__git_remotes ()
{
646
	local i ngoff IFS=$'\n' d="$(__gitdir)"
647 648
	__git_shopt -q nullglob || ngoff=1
	__git_shopt -s nullglob
649 650
	for i in "$d/remotes"/*; do
		echo ${i#$d/remotes/}
651
	done
652
	[ "$ngoff" ] && __git_shopt -u nullglob
653 654 655
	for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
		i="${i#remote.}"
		echo "${i/.url*/}"
656
	done
657 658
}

J
Jonathan Nieder 已提交
659
__git_list_merge_strategies ()
660
{
661 662 663 664 665 666
	git merge -s help 2>&1 |
	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
		s/\.$//
		s/.*://
		s/^[ 	]*//
		s/[ 	]*$//
667
		p
668
	}'
669
}
J
Jonathan Nieder 已提交
670 671 672 673 674 675 676 677 678 679 680

__git_merge_strategies=
# 'git merge -s help' (and thus detection of the merge strategy
# list) fails, unfortunately, if run outside of any git working
# tree.  __git_merge_strategies is set to the empty string in
# that case, and the detection will be repeated the next time it
# is needed.
__git_compute_merge_strategies ()
{
	: ${__git_merge_strategies:=$(__git_list_merge_strategies)}
}
681

682
__git_complete_revlist_file ()
683
{
684
	local pfx ls ref cur_="$cur"
685
	case "$cur_" in
686 687 688
	*..?*:*)
		return
		;;
689
	?*:*)
690 691 692
		ref="${cur_%%:*}"
		cur_="${cur_#*:}"
		case "$cur_" in
693
		?*/*)
694 695
			pfx="${cur_%/*}"
			cur_="${cur_##*/}"
696 697 698 699 700 701
			ls="$ref:$pfx"
			pfx="$pfx/"
			;;
		*)
			ls="$ref"
			;;
702
		esac
703 704 705 706 707 708

		case "$COMP_WORDBREAKS" in
		*:*) : great ;;
		*)   pfx="$ref:$pfx" ;;
		esac

709
		local IFS=$'\n'
710
		COMPREPLY=($(compgen -P "$pfx" \
711
			-W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
712 713 714 715 716 717 718 719
				| sed '/^100... blob /{
				           s,^.*	,,
				           s,$, ,
				       }
				       /^120000 blob /{
				           s,^.*	,,
				           s,$, ,
				       }
720 721 722 723 724
				       /^040000 tree /{
				           s,^.*	,,
				           s,$,/,
				       }
				       s/^.*	//')" \
725
			-- "$cur_"))
726
		;;
727
	*...*)
728 729
		pfx="${cur_%...*}..."
		cur_="${cur_#*...}"
730
		__gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
731 732
		;;
	*..*)
733 734
		pfx="${cur_%..*}.."
		cur_="${cur_#*..}"
735
		__gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
736
		;;
737
	*)
738
		__gitcomp_nl "$(__git_refs)"
739 740 741 742
		;;
	esac
}

743 744 745 746 747 748 749 750 751 752 753

__git_complete_file ()
{
	__git_complete_revlist_file
}

__git_complete_revlist ()
{
	__git_complete_revlist_file
}

754 755
__git_complete_remote_or_refspec ()
{
756
	local cur_="$cur" cmd="${words[1]}"
757
	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
758 759
	while [ $c -lt $cword ]; do
		i="${words[c]}"
760
		case "$i" in
761 762 763 764 765 766 767 768 769 770 771
		--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
		--all)
			case "$cmd" in
			push) no_complete_refspec=1 ;;
			fetch)
				COMPREPLY=()
				return
				;;
			*) ;;
			esac
			;;
772 773 774 775 776 777
		-*) ;;
		*) remote="$i"; break ;;
		esac
		c=$((++c))
	done
	if [ -z "$remote" ]; then
778
		__gitcomp_nl "$(__git_remotes)"
779 780
		return
	fi
781 782 783 784
	if [ $no_complete_refspec = 1 ]; then
		COMPREPLY=()
		return
	fi
785
	[ "$remote" = "." ] && remote=
786
	case "$cur_" in
787 788 789
	*:*)
		case "$COMP_WORDBREAKS" in
		*:*) : great ;;
790
		*)   pfx="${cur_%%:*}:" ;;
791
		esac
792
		cur_="${cur_#*:}"
793 794 795 796
		lhs=0
		;;
	+*)
		pfx="+"
797
		cur_="${cur_#+}"
798 799 800 801 802
		;;
	esac
	case "$cmd" in
	fetch)
		if [ $lhs = 1 ]; then
803
			__gitcomp_nl "$(__git_refs2 "$remote")" "$pfx" "$cur_"
804
		else
805
			__gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
806 807 808 809
		fi
		;;
	pull)
		if [ $lhs = 1 ]; then
810
			__gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
811
		else
812
			__gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
813 814 815 816
		fi
		;;
	push)
		if [ $lhs = 1 ]; then
817
			__gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
818
		else
819
			__gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
820 821 822 823 824
		fi
		;;
	esac
}

825 826
__git_complete_strategy ()
{
J
Jonathan Nieder 已提交
827
	__git_compute_merge_strategies
828
	case "$prev" in
829
	-s|--strategy)
J
Jonathan Nieder 已提交
830
		__gitcomp "$__git_merge_strategies"
831 832 833 834
		return 0
	esac
	case "$cur" in
	--strategy=*)
J
Jonathan Nieder 已提交
835
		__gitcomp "$__git_merge_strategies" "" "${cur##--strategy=}"
836 837 838 839 840 841
		return 0
		;;
	esac
	return 1
}

J
Jonathan Nieder 已提交
842
__git_list_all_commands ()
843 844
{
	local i IFS=" "$'\n'
845
	for i in $(git help -a|egrep '^  [a-zA-Z0-9]')
846 847 848 849 850 851 852 853
	do
		case $i in
		*--*)             : helper pattern;;
		*) echo $i;;
		esac
	done
}

J
Jonathan Nieder 已提交
854 855 856 857 858 859 860
__git_all_commands=
__git_compute_all_commands ()
{
	: ${__git_all_commands:=$(__git_list_all_commands)}
}

__git_list_porcelain_commands ()
861 862
{
	local i IFS=" "$'\n'
J
Jonathan Nieder 已提交
863 864
	__git_compute_all_commands
	for i in "help" $__git_all_commands
865 866
	do
		case $i in
867
		*--*)             : helper pattern;;
868 869 870
		applymbox)        : ask gittus;;
		applypatch)       : ask gittus;;
		archimport)       : import;;
871
		cat-file)         : plumbing;;
872
		check-attr)       : plumbing;;
873
		check-ref-format) : plumbing;;
874
		checkout-index)   : plumbing;;
875
		commit-tree)      : plumbing;;
876
		count-objects)    : infrequent;;
877 878
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
879 880
		cvsserver)        : daemon;;
		daemon)           : daemon;;
881 882 883
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
884
		fast-import)      : import;;
885
		fast-export)      : export;;
886
		fsck-objects)     : plumbing;;
887
		fetch-pack)       : plumbing;;
888
		fmt-merge-msg)    : plumbing;;
889
		for-each-ref)     : plumbing;;
890 891 892
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
893
		init-db)          : deprecated;;
894
		local-fetch)      : plumbing;;
895 896 897 898
		lost-found)       : infrequent;;
		ls-files)         : plumbing;;
		ls-remote)        : plumbing;;
		ls-tree)          : plumbing;;
899 900 901 902 903 904 905 906 907 908 909
		mailinfo)         : plumbing;;
		mailsplit)        : plumbing;;
		merge-*)          : plumbing;;
		mktree)           : plumbing;;
		mktag)            : plumbing;;
		pack-objects)     : plumbing;;
		pack-redundant)   : plumbing;;
		pack-refs)        : plumbing;;
		parse-remote)     : plumbing;;
		patch-id)         : plumbing;;
		peek-remote)      : plumbing;;
910 911 912
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
913 914
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
915
		remote-*)         : transport;;
916
		repo-config)      : deprecated;;
917 918 919 920 921 922
		rerere)           : plumbing;;
		rev-list)         : plumbing;;
		rev-parse)        : plumbing;;
		runstatus)        : plumbing;;
		sh-setup)         : internal;;
		shell)            : daemon;;
923
		show-ref)         : plumbing;;
924 925 926 927 928
		send-pack)        : plumbing;;
		show-index)       : plumbing;;
		ssh-*)            : transport;;
		stripspace)       : plumbing;;
		symbolic-ref)     : plumbing;;
929
		tar-tree)         : deprecated;;
930 931
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
932
		update-index)     : plumbing;;
933 934 935 936 937
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
938 939
		var)              : infrequent;;
		verify-pack)      : infrequent;;
940
		verify-tag)       : plumbing;;
941 942 943 944
		*) echo $i;;
		esac
	done
}
J
Jonathan Nieder 已提交
945 946 947 948 949 950 951

__git_porcelain_commands=
__git_compute_porcelain_commands ()
{
	__git_compute_all_commands
	: ${__git_porcelain_commands:=$(__git_list_porcelain_commands)}
}
952

953 954 955 956 957 958 959 960 961 962 963 964 965
__git_pretty_aliases ()
{
	local i IFS=$'\n'
	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
		case "$i" in
		pretty.*)
			i="${i#pretty.}"
			echo "${i/ */}"
			;;
		esac
	done
}

966 967
__git_aliases ()
{
968
	local i IFS=$'\n'
969
	for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
970 971 972 973 974 975
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/ */}"
			;;
		esac
976
	done
977 978
}

979
# __git_aliased_command requires 1 argument
980 981
__git_aliased_command ()
{
982
	local word cmdline=$(git --git-dir="$(__gitdir)" \
983
		config --get "alias.$1")
984
	for word in $cmdline; do
985
		case "$word" in
986 987
		\!gitk|gitk)
			echo "gitk"
988
			return
989
			;;
990 991 992 993 994 995
		\!*)	: shell command alias ;;
		-*)	: option ;;
		*=*)	: setting env ;;
		git)	: git itself ;;
		*)
			echo "$word"
996
			return
997
		esac
998 999 1000
	done
}

1001 1002
# __git_find_on_cmdline requires 1 argument
__git_find_on_cmdline ()
1003
{
1004
	local word subcommand c=1
1005 1006
	while [ $c -lt $cword ]; do
		word="${words[c]}"
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		for subcommand in $1; do
			if [ "$subcommand" = "$word" ]; then
				echo "$subcommand"
				return
			fi
		done
		c=$((++c))
	done
}

1017 1018
__git_has_doubledash ()
{
1019
	local c=1
1020 1021
	while [ $c -lt $cword ]; do
		if [ "--" = "${words[c]}" ]; then
1022 1023 1024 1025 1026 1027 1028
			return 0
		fi
		c=$((++c))
	done
	return 1
}

1029
__git_whitespacelist="nowarn warn error error-all fix"
1030 1031 1032

_git_am ()
{
1033
	local dir="$(__gitdir)"
1034
	if [ -d "$dir"/rebase-apply ]; then
1035
		__gitcomp "--skip --continue --resolved --abort"
1036 1037 1038 1039
		return
	fi
	case "$cur" in
	--whitespace=*)
1040
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1041 1042 1043
		return
		;;
	--*)
1044
		__gitcomp "
1045
			--3way --committer-date-is-author-date --ignore-date
1046
			--ignore-whitespace --ignore-space-change
1047
			--interactive --keep --no-utf8 --signoff --utf8
1048
			--whitespace= --scissors
1049
			"
1050 1051 1052 1053 1054 1055 1056 1057 1058
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	case "$cur" in
	--whitespace=*)
1059
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
1060 1061 1062
		return
		;;
	--*)
1063
		__gitcomp "
1064 1065 1066
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
1067
			--ignore-whitespace --ignore-space-change
1068
			--whitespace= --inaccurate-eof --verbose
1069
			"
1070 1071 1072 1073 1074
		return
	esac
	COMPREPLY=()
}

1075 1076
_git_add ()
{
1077 1078
	__git_has_doubledash && return

1079 1080
	case "$cur" in
	--*)
1081 1082
		__gitcomp "
			--interactive --refresh --patch --update --dry-run
1083
			--ignore-errors --intent-to-add
1084
			"
1085 1086 1087 1088 1089
		return
	esac
	COMPREPLY=()
}

1090 1091 1092 1093 1094 1095 1096 1097
_git_archive ()
{
	case "$cur" in
	--format=*)
		__gitcomp "$(git archive --list)" "" "${cur##--format=}"
		return
		;;
	--remote=*)
1098
		__gitcomp_nl "$(__git_remotes)" "" "${cur##--remote=}"
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
		return
		;;
	--*)
		__gitcomp "
			--format= --list --verbose
			--prefix= --remote= --exec=
			"
		return
		;;
	esac
	__git_complete_file
}

1112 1113
_git_bisect ()
{
1114 1115
	__git_has_doubledash && return

1116
	local subcommands="start bad good skip reset visualize replay log run"
1117
	local subcommand="$(__git_find_on_cmdline "$subcommands")"
1118
	if [ -z "$subcommand" ]; then
1119 1120 1121 1122 1123
		if [ -f "$(__gitdir)"/BISECT_START ]; then
			__gitcomp "$subcommands"
		else
			__gitcomp "replay start"
		fi
1124 1125 1126
		return
	fi

1127
	case "$subcommand" in
1128
	bad|good|reset|skip|start)
1129
		__gitcomp_nl "$(__git_refs)"
1130 1131 1132 1133 1134 1135 1136
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

1137 1138
_git_branch ()
{
1139
	local i c=1 only_local_ref="n" has_r="n"
1140

1141 1142
	while [ $c -lt $cword ]; do
		i="${words[c]}"
1143 1144 1145 1146 1147 1148 1149
		case "$i" in
		-d|-m)	only_local_ref="y" ;;
		-r)	has_r="y" ;;
		esac
		c=$((++c))
	done

1150
	case "$cur" in
S
SZEDER Gábor 已提交
1151 1152 1153
	--*)
		__gitcomp "
			--color --no-color --verbose --abbrev= --no-abbrev
1154
			--track --no-track --contains --merged --no-merged
1155
			--set-upstream
S
SZEDER Gábor 已提交
1156 1157
			"
		;;
1158 1159
	*)
		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
1160
			__gitcomp_nl "$(__git_heads)"
1161
		else
1162
			__gitcomp_nl "$(__git_refs)"
1163 1164
		fi
		;;
S
SZEDER Gábor 已提交
1165
	esac
1166 1167
}

1168 1169
_git_bundle ()
{
1170 1171
	local cmd="${words[2]}"
	case "$cword" in
1172
	2)
1173 1174
		__gitcomp "create list-heads verify unbundle"
		;;
1175
	3)
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		# looking for a file
		;;
	*)
		case "$cmd" in
			create)
				__git_complete_revlist
			;;
		esac
		;;
	esac
}

1188 1189
_git_checkout ()
{
1190 1191
	__git_has_doubledash && return

1192 1193 1194 1195 1196 1197 1198
	case "$cur" in
	--conflict=*)
		__gitcomp "diff3 merge" "" "${cur##--conflict=}"
		;;
	--*)
		__gitcomp "
			--quiet --ours --theirs --track --no-track --merge
1199
			--conflict= --orphan --patch
1200 1201 1202
			"
		;;
	*)
1203 1204 1205 1206 1207 1208
		# check if --track, --no-track, or --no-guess was specified
		# if so, disable DWIM mode
		local flags="--track --no-track --no-guess" track=1
		if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
			track=''
		fi
1209
		__gitcomp_nl "$(__git_refs '' $track)"
1210 1211
		;;
	esac
1212 1213
}

1214 1215 1216 1217 1218
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

1219 1220 1221 1222
_git_cherry_pick ()
{
	case "$cur" in
	--*)
1223
		__gitcomp "--edit --no-commit"
1224 1225
		;;
	*)
1226
		__gitcomp_nl "$(__git_refs)"
1227 1228 1229 1230
		;;
	esac
}

1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
_git_clean ()
{
	__git_has_doubledash && return

	case "$cur" in
	--*)
		__gitcomp "--dry-run --quiet"
		return
		;;
	esac
	COMPREPLY=()
}

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267
_git_clone ()
{
	case "$cur" in
	--*)
		__gitcomp "
			--local
			--no-hardlinks
			--shared
			--reference
			--quiet
			--no-checkout
			--bare
			--mirror
			--origin
			--upload-pack
			--template=
			--depth
			"
		return
		;;
	esac
	COMPREPLY=()
}

1268 1269
_git_commit ()
{
1270 1271
	__git_has_doubledash && return

1272
	case "$cur" in
1273 1274 1275 1276 1277
	--cleanup=*)
		__gitcomp "default strip verbatim whitespace
			" "" "${cur##--cleanup=}"
		return
		;;
1278 1279
	--reuse-message=*|--reedit-message=*|\
	--fixup=*|--squash=*)
1280
		__gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1281 1282 1283 1284 1285 1286
		return
		;;
	--untracked-files=*)
		__gitcomp "all no normal" "" "${cur##--untracked-files=}"
		return
		;;
1287
	--*)
1288
		__gitcomp "
1289
			--all --author= --signoff --verify --no-verify
1290
			--edit --amend --include --only --interactive
1291 1292 1293
			--dry-run --reuse-message= --reedit-message=
			--reset-author --file= --message= --template=
			--cleanup= --untracked-files --untracked-files=
1294
			--verbose --quiet --fixup= --squash=
1295
			"
1296 1297 1298 1299 1300
		return
	esac
	COMPREPLY=()
}

1301 1302
_git_describe ()
{
1303 1304 1305 1306 1307 1308 1309 1310
	case "$cur" in
	--*)
		__gitcomp "
			--all --tags --contains --abbrev= --candidates=
			--exact-match --debug --long --match --always
			"
		return
	esac
1311
	__gitcomp_nl "$(__git_refs)"
1312 1313
}

1314
__git_diff_common_options="--stat --numstat --shortstat --summary
1315 1316
			--patch-with-stat --name-only --name-status --color
			--no-color --color-words --no-renames --check
1317
			--full-index --binary --abbrev --diff-filter=
1318
			--find-copies-harder
1319 1320
			--text --ignore-space-at-eol --ignore-space-change
			--ignore-all-space --exit-code --quiet --ext-diff
1321 1322
			--no-ext-diff
			--no-prefix --src-prefix= --dst-prefix=
1323
			--inter-hunk-context=
1324
			--patience
1325
			--raw
1326 1327
			--dirstat --dirstat= --dirstat-by-file
			--dirstat-by-file= --cumulative
1328 1329 1330 1331 1332 1333 1334 1335
"

_git_diff ()
{
	__git_has_doubledash && return

	case "$cur" in
	--*)
1336
		__gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
1337
			--base --ours --theirs --no-index
1338
			$__git_diff_common_options
1339
			"
1340 1341 1342
		return
		;;
	esac
1343
	__git_complete_revlist_file
1344 1345
}

1346
__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
1347
			tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc3
1348 1349 1350 1351
"

_git_difftool ()
{
1352 1353
	__git_has_doubledash && return

1354 1355 1356 1357 1358 1359
	case "$cur" in
	--tool=*)
		__gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
		return
		;;
	--*)
1360 1361 1362 1363 1364
		__gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
			--base --ours --theirs
			--no-renames --diff-filter= --find-copies-harder
			--relative --ignore-submodules
			--tool="
1365 1366 1367
		return
		;;
	esac
1368
	__git_complete_file
1369 1370
}

1371 1372
__git_fetch_options="
	--quiet --verbose --append --upload-pack --force --keep --depth=
1373
	--tags --no-tags --all --prune --dry-run
1374 1375
"

1376 1377
_git_fetch ()
{
1378 1379 1380 1381 1382 1383
	case "$cur" in
	--*)
		__gitcomp "$__git_fetch_options"
		return
		;;
	esac
1384
	__git_complete_remote_or_refspec
1385 1386
}

1387 1388 1389
_git_format_patch ()
{
	case "$cur" in
1390 1391 1392 1393 1394 1395
	--thread=*)
		__gitcomp "
			deep shallow
			" "" "${cur##--thread=}"
		return
		;;
1396
	--*)
1397
		__gitcomp "
1398
			--stdout --attach --no-attach --thread --thread=
1399 1400
			--output-directory
			--numbered --start-number
1401
			--numbered-files
1402
			--keep-subject
1403
			--signoff --signature --no-signature
1404
			--in-reply-to= --cc=
1405
			--full-index --binary
1406
			--not --all
1407
			--cover-letter
1408
			--no-prefix --src-prefix= --dst-prefix=
1409 1410
			--inline --suffix= --ignore-if-in-upstream
			--subject-prefix=
1411
			"
1412 1413 1414 1415 1416 1417
		return
		;;
	esac
	__git_complete_revlist
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
_git_fsck ()
{
	case "$cur" in
	--*)
		__gitcomp "
			--tags --root --unreachable --cache --no-reflogs --full
			--strict --verbose --lost-found
			"
		return
		;;
	esac
	COMPREPLY=()
}

1432 1433 1434 1435
_git_gc ()
{
	case "$cur" in
	--*)
1436
		__gitcomp "--prune --aggressive"
1437 1438 1439 1440 1441 1442
		return
		;;
	esac
	COMPREPLY=()
}

1443 1444 1445 1446 1447
_git_gitk ()
{
	_gitk
}

1448 1449 1450 1451
__git_match_ctag() {
	awk "/^${1////\\/}/ { print \$1 }" "$2"
}

1452 1453 1454 1455 1456 1457 1458 1459 1460
_git_grep ()
{
	__git_has_doubledash && return

	case "$cur" in
	--*)
		__gitcomp "
			--cached
			--text --ignore-case --word-regexp --invert-match
1461
			--full-name --line-number
1462
			--extended-regexp --basic-regexp --fixed-strings
M
Michał Kiedrowicz 已提交
1463
			--perl-regexp
1464 1465
			--files-with-matches --name-only
			--files-without-match
1466
			--max-depth
1467 1468 1469 1470 1471 1472
			--count
			--and --or --not --all-match
			"
		return
		;;
	esac
1473

1474 1475 1476
	case "$cword,$prev" in
	2,*|*,-*)
		if test -r tags; then
J
Junio C Hamano 已提交
1477
			__gitcomp_nl "$(__git_match_ctag "$cur" tags)"
1478 1479 1480 1481 1482
			return
		fi
		;;
	esac

1483
	__gitcomp_nl "$(__git_refs)"
1484 1485
}

1486 1487 1488 1489 1490 1491 1492 1493
_git_help ()
{
	case "$cur" in
	--*)
		__gitcomp "--all --info --man --web"
		return
		;;
	esac
J
Jonathan Nieder 已提交
1494
	__git_compute_all_commands
1495
	__gitcomp "$__git_all_commands $(__git_aliases)
1496 1497
		attributes cli core-tutorial cvs-migration
		diffcore gitk glossary hooks ignore modules
J
Josh Triplett 已提交
1498
		namespaces repository-layout tutorial tutorial-2
1499
		workflows
1500
		"
1501 1502
}

1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519
_git_init ()
{
	case "$cur" in
	--shared=*)
		__gitcomp "
			false true umask group all world everybody
			" "" "${cur##--shared=}"
		return
		;;
	--*)
		__gitcomp "--quiet --bare --template= --shared --shared="
		return
		;;
	esac
	COMPREPLY=()
}

1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
_git_ls_files ()
{
	__git_has_doubledash && return

	case "$cur" in
	--*)
		__gitcomp "--cached --deleted --modified --others --ignored
			--stage --directory --no-empty-directory --unmerged
			--killed --exclude= --exclude-from=
			--exclude-per-directory= --exclude-standard
			--error-unmatch --with-tree= --full-name
			--abbrev --ignored --exclude-per-directory
			"
		return
		;;
	esac
	COMPREPLY=()
}

1539 1540
_git_ls_remote ()
{
1541
	__gitcomp_nl "$(__git_remotes)"
1542 1543 1544 1545 1546 1547 1548
}

_git_ls_tree ()
{
	__git_complete_file
}

1549 1550 1551 1552
# Options that go well for log, shortlog and gitk
__git_log_common_options="
	--not --all
	--branches --tags --remotes
1553
	--first-parent --merges --no-merges
1554 1555 1556
	--max-count=
	--max-age= --since= --after=
	--min-age= --until= --before=
1557 1558
	--min-parents= --max-parents=
	--no-min-parents --no-max-parents
1559 1560 1561 1562 1563
"
# Options that go well for log and gitk (not shortlog)
__git_log_gitk_options="
	--dense --sparse --full-history
	--simplify-merges --simplify-by-decoration
1564
	--left-right --notes --no-notes
1565 1566 1567 1568 1569 1570 1571
"
# Options that go well for log and shortlog (not gitk)
__git_log_shortlog_options="
	--author= --committer= --grep=
	--all-match
"

1572
__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1573
__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1574

1575 1576
_git_log ()
{
1577 1578
	__git_has_doubledash && return

1579 1580
	local g="$(git rev-parse --git-dir 2>/dev/null)"
	local merge=""
1581
	if [ -f "$g/MERGE_HEAD" ]; then
1582 1583
		merge="--merge"
	fi
1584
	case "$cur" in
1585
	--pretty=*|--format=*)
1586
		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
1587
			" "" "${cur#*=}"
1588 1589
		return
		;;
1590
	--date=*)
1591
		__gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1592 1593
		return
		;;
1594 1595 1596 1597
	--decorate=*)
		__gitcomp "long short" "" "${cur##--decorate=}"
		return
		;;
1598
	--*)
1599
		__gitcomp "
1600 1601 1602
			$__git_log_common_options
			$__git_log_shortlog_options
			$__git_log_gitk_options
1603
			--root --topo-order --date-order --reverse
1604
			--follow --full-diff
1605
			--abbrev-commit --abbrev=
1606
			--relative-date --date=
1607
			--pretty= --format= --oneline
1608
			--cherry-pick
1609
			--graph
1610
			--decorate --decorate=
1611
			--walk-reflogs
1612
			--parents --children
1613
			$merge
1614
			$__git_diff_common_options
1615
			--pickaxe-all --pickaxe-regex
1616
			"
1617 1618 1619
		return
		;;
	esac
1620
	__git_complete_revlist
1621 1622
}

1623 1624
__git_merge_options="
	--no-commit --no-stat --log --no-log --squash --strategy
1625
	--commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
1626 1627
"

1628 1629
_git_merge ()
{
1630 1631
	__git_complete_strategy && return

1632 1633
	case "$cur" in
	--*)
1634
		__gitcomp "$__git_merge_options"
1635 1636
		return
	esac
1637
	__gitcomp_nl "$(__git_refs)"
1638 1639
}

1640 1641 1642 1643
_git_mergetool ()
{
	case "$cur" in
	--tool=*)
1644
		__gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654
		return
		;;
	--*)
		__gitcomp "--tool="
		return
		;;
	esac
	COMPREPLY=()
}

1655 1656
_git_merge_base ()
{
1657
	__gitcomp_nl "$(__git_refs)"
1658 1659
}

1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670
_git_mv ()
{
	case "$cur" in
	--*)
		__gitcomp "--dry-run"
		return
		;;
	esac
	COMPREPLY=()
}

1671 1672
_git_name_rev ()
{
1673
	__gitcomp "--tags --all --stdin"
1674 1675
}

1676 1677
_git_notes ()
{
1678 1679
	local subcommands='add append copy edit list prune remove show'
	local subcommand="$(__git_find_on_cmdline "$subcommands")"
1680

1681 1682 1683 1684 1685
	case "$subcommand,$cur" in
	,--*)
		__gitcomp '--ref'
		;;
	,*)
1686
		case "${words[cword-1]}" in
1687
		--ref)
1688
			__gitcomp_nl "$(__git_refs)"
1689 1690 1691 1692 1693 1694
			;;
		*)
			__gitcomp "$subcommands --ref"
			;;
		esac
		;;
1695
	add,--reuse-message=*|append,--reuse-message=*|\
1696
	add,--reedit-message=*|append,--reedit-message=*)
1697
		__gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709
		;;
	add,--*|append,--*)
		__gitcomp '--file= --message= --reedit-message=
				--reuse-message='
		;;
	copy,--*)
		__gitcomp '--stdin'
		;;
	prune,--*)
		__gitcomp '--dry-run --verbose'
		;;
	prune,*)
1710 1711
		;;
	*)
1712
		case "${words[cword-1]}" in
1713 1714 1715
		-m|-F)
			;;
		*)
1716
			__gitcomp_nl "$(__git_refs)"
1717 1718
			;;
		esac
1719 1720 1721 1722
		;;
	esac
}

1723 1724
_git_pull ()
{
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736
	__git_complete_strategy && return

	case "$cur" in
	--*)
		__gitcomp "
			--rebase --no-rebase
			$__git_merge_options
			$__git_fetch_options
		"
		return
		;;
	esac
1737
	__git_complete_remote_or_refspec
1738 1739 1740 1741
}

_git_push ()
{
1742
	case "$prev" in
1743
	--repo)
1744
		__gitcomp_nl "$(__git_remotes)"
1745 1746 1747 1748
		return
	esac
	case "$cur" in
	--repo=*)
1749
		__gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
1750 1751 1752 1753 1754
		return
		;;
	--*)
		__gitcomp "
			--all --mirror --tags --dry-run --force --verbose
T
Teemu Matilainen 已提交
1755
			--receive-pack= --repo= --set-upstream
1756 1757 1758 1759
		"
		return
		;;
	esac
1760
	__git_complete_remote_or_refspec
1761 1762
}

1763 1764
_git_rebase ()
{
1765
	local dir="$(__gitdir)"
1766
	if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1767
		__gitcomp "--continue --skip --abort"
1768 1769
		return
	fi
1770
	__git_complete_strategy && return
1771
	case "$cur" in
1772 1773 1774 1775
	--whitespace=*)
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
		return
		;;
1776
	--*)
1777 1778 1779 1780 1781
		__gitcomp "
			--onto --merge --strategy --interactive
			--preserve-merges --stat --no-stat
			--committer-date-is-author-date --ignore-date
			--ignore-whitespace --whitespace=
1782
			--autosquash
1783 1784
			"

1785 1786
		return
	esac
1787
	__gitcomp_nl "$(__git_refs)"
1788 1789
}

1790 1791 1792 1793 1794 1795 1796 1797
_git_reflog ()
{
	local subcommands="show delete expire"
	local subcommand="$(__git_find_on_cmdline "$subcommands")"

	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
	else
1798
		__gitcomp_nl "$(__git_refs)"
1799 1800 1801
	fi
}

1802
__git_send_email_confirm_options="always never auto cc compose"
1803
__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1804

1805 1806 1807
_git_send_email ()
{
	case "$cur" in
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
	--confirm=*)
		__gitcomp "
			$__git_send_email_confirm_options
			" "" "${cur##--confirm=}"
		return
		;;
	--suppress-cc=*)
		__gitcomp "
			$__git_send_email_suppresscc_options
			" "" "${cur##--suppress-cc=}"

		return
		;;
	--smtp-encryption=*)
		__gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
		return
		;;
1825
	--*)
1826
		__gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1827 1828
			--compose --confirm= --dry-run --envelope-sender
			--from --identity
1829 1830 1831
			--in-reply-to --no-chain-reply-to --no-signed-off-by-cc
			--no-suppress-from --no-thread --quiet
			--signed-off-by-cc --smtp-pass --smtp-server
1832 1833
			--smtp-server-port --smtp-encryption= --smtp-user
			--subject --suppress-cc= --suppress-from --thread --to
1834
			--validate --no-validate"
1835 1836 1837 1838 1839 1840
		return
		;;
	esac
	COMPREPLY=()
}

1841 1842 1843 1844 1845
_git_stage ()
{
	_git_add
}

1846 1847
__git_config_get_set_variables ()
{
1848
	local prevword word config_file= c=$cword
1849
	while [ $c -gt 1 ]; do
1850
		word="${words[c]}"
1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
		case "$word" in
		--global|--system|--file=*)
			config_file="$word"
			break
			;;
		-f|--file)
			config_file="$word $prevword"
			break
			;;
		esac
		prevword=$word
		c=$((--c))
	done

1865
	git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
1866
	while read -r line
1867 1868 1869 1870
	do
		case "$line" in
		*.*=*)
			echo "${line/=*/}"
1871 1872 1873 1874 1875
			;;
		esac
	done
}

1876
_git_config ()
1877
{
1878
	case "$prev" in
1879
	branch.*.remote)
1880
		__gitcomp_nl "$(__git_remotes)"
1881 1882 1883
		return
		;;
	branch.*.merge)
1884
		__gitcomp_nl "$(__git_refs)"
1885 1886 1887
		return
		;;
	remote.*.fetch)
1888
		local remote="${prev#remote.}"
1889
		remote="${remote%.fetch}"
1890 1891 1892 1893
		if [ -z "$cur" ]; then
			COMPREPLY=("refs/heads/")
			return
		fi
1894
		__gitcomp_nl "$(__git_refs_remotes "$remote")"
1895 1896 1897
		return
		;;
	remote.*.push)
1898
		local remote="${prev#remote.}"
1899
		remote="${remote%.push}"
1900
		__gitcomp_nl "$(git --git-dir="$(__gitdir)" \
1901
			for-each-ref --format='%(refname):%(refname)' \
1902 1903 1904 1905
			refs/heads)"
		return
		;;
	pull.twohead|pull.octopus)
J
Jonathan Nieder 已提交
1906 1907
		__git_compute_merge_strategies
		__gitcomp "$__git_merge_strategies"
1908 1909
		return
		;;
1910 1911
	color.branch|color.diff|color.interactive|\
	color.showbranch|color.status|color.ui)
1912 1913 1914
		__gitcomp "always never auto"
		return
		;;
1915 1916 1917 1918
	color.pager)
		__gitcomp "false true"
		return
		;;
1919 1920
	color.*.*)
		__gitcomp "
1921
			normal black red green yellow blue magenta cyan white
1922 1923
			bold dim ul blink reverse
			"
1924 1925
		return
		;;
1926 1927 1928 1929
	help.format)
		__gitcomp "man info web html"
		return
		;;
1930 1931 1932 1933
	log.date)
		__gitcomp "$__git_log_date_formats"
		return
		;;
1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	sendemail.aliasesfiletype)
		__gitcomp "mutt mailrc pine elm gnus"
		return
		;;
	sendemail.confirm)
		__gitcomp "$__git_send_email_confirm_options"
		return
		;;
	sendemail.suppresscc)
		__gitcomp "$__git_send_email_suppresscc_options"
		return
		;;
1946
	--get|--get-all|--unset|--unset-all)
1947
		__gitcomp_nl "$(__git_config_get_set_variables)"
1948 1949
		return
		;;
1950 1951 1952 1953 1954 1955 1956
	*.*)
		COMPREPLY=()
		return
		;;
	esac
	case "$cur" in
	--*)
1957
		__gitcomp "
1958
			--global --system --file=
1959
			--list --replace-all
1960
			--get --get-all --get-regexp
1961
			--add --unset --unset-all
1962
			--remove-section --rename-section
1963
			"
1964 1965 1966
		return
		;;
	branch.*.*)
1967 1968
		local pfx="${cur%.*}." cur_="${cur##*.}"
		__gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur_"
1969 1970 1971
		return
		;;
	branch.*)
1972
		local pfx="${cur%.*}." cur_="${cur#*.}"
1973
		__gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
1974 1975
		return
		;;
1976
	guitool.*.*)
1977
		local pfx="${cur%.*}." cur_="${cur##*.}"
1978 1979 1980
		__gitcomp "
			argprompt cmd confirm needsfile noconsole norescan
			prompt revprompt revunmerged title
1981
			" "$pfx" "$cur_"
1982 1983 1984
		return
		;;
	difftool.*.*)
1985 1986
		local pfx="${cur%.*}." cur_="${cur##*.}"
		__gitcomp "cmd path" "$pfx" "$cur_"
1987 1988 1989
		return
		;;
	man.*.*)
1990 1991
		local pfx="${cur%.*}." cur_="${cur##*.}"
		__gitcomp "cmd path" "$pfx" "$cur_"
1992 1993 1994
		return
		;;
	mergetool.*.*)
1995 1996
		local pfx="${cur%.*}." cur_="${cur##*.}"
		__gitcomp "cmd path trustExitCode" "$pfx" "$cur_"
1997 1998 1999
		return
		;;
	pager.*)
2000
		local pfx="${cur%.*}." cur_="${cur#*.}"
J
Jonathan Nieder 已提交
2001
		__git_compute_all_commands
2002
		__gitcomp_nl "$__git_all_commands" "$pfx" "$cur_"
2003 2004
		return
		;;
2005
	remote.*.*)
2006
		local pfx="${cur%.*}." cur_="${cur##*.}"
2007
		__gitcomp "
2008
			url proxy fetch push mirror skipDefaultUpdate
2009
			receivepack uploadpack tagopt pushurl
2010
			" "$pfx" "$cur_"
2011 2012 2013
		return
		;;
	remote.*)
2014
		local pfx="${cur%.*}." cur_="${cur#*.}"
2015
		__gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
2016 2017
		return
		;;
2018
	url.*.*)
2019 2020
		local pfx="${cur%.*}." cur_="${cur##*.}"
		__gitcomp "insteadOf pushInsteadOf" "$pfx" "$cur_"
2021 2022
		return
		;;
2023
	esac
2024
	__gitcomp "
2025 2026 2027 2028 2029 2030 2031
		add.ignoreErrors
		advice.commitBeforeMerge
		advice.detachedHead
		advice.implicitIdentity
		advice.pushNonFastForward
		advice.resolveConflict
		advice.statusHints
2032
		alias.
2033
		am.keepcr
2034
		apply.ignorewhitespace
2035
		apply.whitespace
2036 2037
		branch.autosetupmerge
		branch.autosetuprebase
2038
		browser.
2039
		clean.requireForce
2040 2041 2042 2043
		color.branch
		color.branch.current
		color.branch.local
		color.branch.plain
2044
		color.branch.remote
2045 2046 2047 2048 2049
		color.decorate.HEAD
		color.decorate.branch
		color.decorate.remoteBranch
		color.decorate.stash
		color.decorate.tag
2050
		color.diff
2051
		color.diff.commit
2052
		color.diff.frag
2053
		color.diff.func
2054
		color.diff.meta
2055
		color.diff.new
2056 2057
		color.diff.old
		color.diff.plain
2058
		color.diff.whitespace
2059
		color.grep
2060 2061 2062 2063
		color.grep.context
		color.grep.filename
		color.grep.function
		color.grep.linenumber
2064
		color.grep.match
2065 2066
		color.grep.selected
		color.grep.separator
2067
		color.interactive
2068
		color.interactive.error
2069 2070 2071
		color.interactive.header
		color.interactive.help
		color.interactive.prompt
2072
		color.pager
2073
		color.showbranch
2074
		color.status
2075 2076
		color.status.added
		color.status.changed
2077
		color.status.header
2078
		color.status.nobranch
2079
		color.status.untracked
2080 2081
		color.status.updated
		color.ui
2082
		commit.status
2083
		commit.template
2084
		core.abbrev
2085 2086
		core.askpass
		core.attributesfile
2087 2088
		core.autocrlf
		core.bare
2089
		core.bigFileThreshold
2090
		core.compression
2091
		core.createObject
2092 2093
		core.deltaBaseCacheLimit
		core.editor
2094
		core.eol
2095
		core.excludesfile
2096
		core.fileMode
2097
		core.fsyncobjectfiles
2098
		core.gitProxy
2099
		core.ignoreCygwinFSTricks
2100
		core.ignoreStat
2101
		core.ignorecase
2102 2103
		core.logAllRefUpdates
		core.loosecompression
2104
		core.notesRef
2105 2106
		core.packedGitLimit
		core.packedGitWindowSize
2107
		core.pager
2108
		core.preferSymlinkRefs
2109 2110
		core.preloadindex
		core.quotepath
2111
		core.repositoryFormatVersion
2112
		core.safecrlf
2113
		core.sharedRepository
2114
		core.sparseCheckout
2115 2116
		core.symlinks
		core.trustctime
2117
		core.warnAmbiguousRefs
2118 2119 2120 2121
		core.whitespace
		core.worktree
		diff.autorefreshindex
		diff.external
2122
		diff.ignoreSubmodules
2123
		diff.mnemonicprefix
2124
		diff.noprefix
2125 2126
		diff.renameLimit
		diff.renames
2127 2128 2129
		diff.suppressBlankEmpty
		diff.tool
		diff.wordRegex
2130
		difftool.
2131
		difftool.prompt
2132
		fetch.recurseSubmodules
2133
		fetch.unpackLimit
2134 2135
		format.attach
		format.cc
2136
		format.headers
2137 2138
		format.numbered
		format.pretty
2139
		format.signature
2140 2141
		format.signoff
		format.subjectprefix
2142
		format.suffix
2143
		format.thread
2144 2145
		format.to
		gc.
2146 2147 2148
		gc.aggressiveWindow
		gc.auto
		gc.autopacklimit
2149
		gc.packrefs
2150
		gc.pruneexpire
2151 2152 2153 2154
		gc.reflogexpire
		gc.reflogexpireunreachable
		gc.rerereresolved
		gc.rerereunresolved
2155
		gitcvs.allbinary
2156
		gitcvs.commitmsgannotation
2157
		gitcvs.dbTableNamePrefix
2158 2159 2160 2161 2162 2163
		gitcvs.dbdriver
		gitcvs.dbname
		gitcvs.dbpass
		gitcvs.dbuser
		gitcvs.enabled
		gitcvs.logfile
2164
		gitcvs.usecrlfattr
2165
		guitool.
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
		gui.blamehistoryctx
		gui.commitmsgwidth
		gui.copyblamethreshold
		gui.diffcontext
		gui.encoding
		gui.fastcopyblame
		gui.matchtrackingbranch
		gui.newbranchtemplate
		gui.pruneduringfetch
		gui.spellingdictionary
		gui.trustmtime
		help.autocorrect
		help.browser
		help.format
2180 2181
		http.lowSpeedLimit
		http.lowSpeedTime
2182
		http.maxRequests
2183
		http.minSessions
2184
		http.noEPSV
2185
		http.postBuffer
2186
		http.proxy
2187 2188 2189
		http.sslCAInfo
		http.sslCAPath
		http.sslCert
2190
		http.sslCertPasswordProtected
2191 2192
		http.sslKey
		http.sslVerify
2193
		http.useragent
2194 2195
		i18n.commitEncoding
		i18n.logOutputEncoding
2196
		imap.authMethod
2197 2198 2199 2200 2201 2202 2203 2204
		imap.folder
		imap.host
		imap.pass
		imap.port
		imap.preformattedHTML
		imap.sslverify
		imap.tunnel
		imap.user
2205
		init.templatedir
2206 2207 2208 2209 2210
		instaweb.browser
		instaweb.httpd
		instaweb.local
		instaweb.modulepath
		instaweb.port
2211
		interactive.singlekey
2212
		log.date
2213
		log.decorate
2214
		log.showroot
2215
		mailmap.file
2216
		man.
2217
		man.viewer
2218
		merge.
2219 2220 2221
		merge.conflictstyle
		merge.log
		merge.renameLimit
2222
		merge.renormalize
2223
		merge.stat
2224
		merge.tool
2225
		merge.verbosity
2226
		mergetool.
2227
		mergetool.keepBackup
2228
		mergetool.keepTemporaries
2229
		mergetool.prompt
2230 2231 2232 2233 2234 2235
		notes.displayRef
		notes.rewrite.
		notes.rewrite.amend
		notes.rewrite.rebase
		notes.rewriteMode
		notes.rewriteRef
2236 2237
		pack.compression
		pack.deltaCacheLimit
2238 2239
		pack.deltaCacheSize
		pack.depth
2240 2241 2242
		pack.indexVersion
		pack.packSizeLimit
		pack.threads
2243 2244
		pack.window
		pack.windowMemory
2245
		pager.
2246
		pretty.
2247 2248
		pull.octopus
		pull.twohead
2249
		push.default
2250
		rebase.autosquash
2251
		rebase.stat
2252
		receive.autogc
2253
		receive.denyCurrentBranch
2254
		receive.denyDeleteCurrent
2255
		receive.denyDeletes
2256
		receive.denyNonFastForwards
2257
		receive.fsckObjects
2258
		receive.unpackLimit
2259 2260
		receive.updateserverinfo
		remotes.
2261 2262 2263
		repack.usedeltabaseoffset
		rerere.autoupdate
		rerere.enabled
2264
		sendemail.
2265
		sendemail.aliasesfile
2266
		sendemail.aliasfiletype
2267 2268 2269 2270 2271 2272
		sendemail.bcc
		sendemail.cc
		sendemail.cccmd
		sendemail.chainreplyto
		sendemail.confirm
		sendemail.envelopesender
2273 2274
		sendemail.from
		sendemail.identity
2275 2276
		sendemail.multiedit
		sendemail.signedoffbycc
2277
		sendemail.smtpdomain
2278 2279 2280
		sendemail.smtpencryption
		sendemail.smtppass
		sendemail.smtpserver
2281
		sendemail.smtpserveroption
2282 2283 2284 2285 2286 2287 2288
		sendemail.smtpserverport
		sendemail.smtpuser
		sendemail.suppresscc
		sendemail.suppressfrom
		sendemail.thread
		sendemail.to
		sendemail.validate
2289
		showbranch.default
2290 2291
		status.relativePaths
		status.showUntrackedFiles
2292 2293
		status.submodulesummary
		submodule.
2294 2295
		tar.umask
		transfer.unpackLimit
2296
		url.
2297
		user.email
2298
		user.name
2299
		user.signingkey
2300
		web.browser
2301
		branch. remote.
2302
	"
2303 2304
}

2305 2306
_git_remote ()
{
2307
	local subcommands="add rename rm show prune update set-head"
2308
	local subcommand="$(__git_find_on_cmdline "$subcommands")"
2309
	if [ -z "$subcommand" ]; then
2310
		__gitcomp "$subcommands"
2311 2312 2313
		return
	fi

2314
	case "$subcommand" in
2315
	rename|rm|show|prune)
2316
		__gitcomp_nl "$(__git_remotes)"
2317
		;;
2318 2319
	update)
		local i c='' IFS=$'\n'
2320 2321 2322
		for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
			i="${i#remotes.}"
			c="$c ${i/ */}"
2323 2324 2325
		done
		__gitcomp "$c"
		;;
2326 2327 2328 2329 2330 2331
	*)
		COMPREPLY=()
		;;
	esac
}

2332 2333
_git_replace ()
{
2334
	__gitcomp_nl "$(__git_refs)"
2335 2336
}

2337 2338
_git_reset ()
{
2339 2340
	__git_has_doubledash && return

2341 2342
	case "$cur" in
	--*)
S
SZEDER Gábor 已提交
2343
		__gitcomp "--merge --mixed --hard --soft --patch"
2344 2345 2346
		return
		;;
	esac
2347
	__gitcomp_nl "$(__git_refs)"
2348 2349
}

2350 2351 2352 2353 2354 2355 2356 2357
_git_revert ()
{
	case "$cur" in
	--*)
		__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
		return
		;;
	esac
2358
	__gitcomp_nl "$(__git_refs)"
2359 2360
}

2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373
_git_rm ()
{
	__git_has_doubledash && return

	case "$cur" in
	--*)
		__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
		return
		;;
	esac
	COMPREPLY=()
}

2374 2375
_git_shortlog ()
{
2376 2377
	__git_has_doubledash && return

2378 2379 2380
	case "$cur" in
	--*)
		__gitcomp "
2381 2382
			$__git_log_common_options
			$__git_log_shortlog_options
2383 2384 2385 2386 2387 2388 2389 2390
			--numbered --summary
			"
		return
		;;
	esac
	__git_complete_revlist
}

2391 2392
_git_show ()
{
2393 2394
	__git_has_doubledash && return

2395
	case "$cur" in
2396
	--pretty=*|--format=*)
2397
		__gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
2398
			" "" "${cur#*=}"
2399 2400
		return
		;;
2401
	--*)
2402
		__gitcomp "--pretty= --format= --abbrev-commit --oneline
2403 2404
			$__git_diff_common_options
			"
2405 2406 2407 2408 2409 2410
		return
		;;
	esac
	__git_complete_file
}

2411 2412 2413 2414 2415 2416 2417
_git_show_branch ()
{
	case "$cur" in
	--*)
		__gitcomp "
			--all --remotes --topo-order --current --more=
			--list --independent --merge-base --no-name
2418
			--color --no-color
2419
			--sha1-name --sparse --topics --reflog
2420 2421 2422 2423 2424 2425 2426
			"
		return
		;;
	esac
	__git_complete_revlist
}

J
Junio C Hamano 已提交
2427 2428
_git_stash ()
{
2429
	local save_opts='--keep-index --no-keep-index --quiet --patch'
2430
	local subcommands='save list show apply clear drop pop create branch'
2431
	local subcommand="$(__git_find_on_cmdline "$subcommands")"
2432
	if [ -z "$subcommand" ]; then
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444
		case "$cur" in
		--*)
			__gitcomp "$save_opts"
			;;
		*)
			if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
				__gitcomp "$subcommands"
			else
				COMPREPLY=()
			fi
			;;
		esac
2445 2446 2447
	else
		case "$subcommand,$cur" in
		save,--*)
2448
			__gitcomp "$save_opts"
2449
			;;
2450
		apply,--*|pop,--*)
2451
			__gitcomp "--index --quiet"
2452
			;;
2453
		show,--*|drop,--*|branch,--*)
2454 2455 2456
			COMPREPLY=()
			;;
		show,*|apply,*|drop,*|pop,*|branch,*)
2457
			__gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
2458 2459
					| sed -n -e 's/:.*//p')"
			;;
2460 2461 2462 2463
		*)
			COMPREPLY=()
			;;
		esac
2464
	fi
J
Junio C Hamano 已提交
2465 2466
}

2467 2468
_git_submodule ()
{
2469 2470
	__git_has_doubledash && return

2471
	local subcommands="add status init update summary foreach sync"
2472
	if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
2473 2474 2475 2476 2477
		case "$cur" in
		--*)
			__gitcomp "--quiet --cached"
			;;
		*)
2478
			__gitcomp "$subcommands"
2479 2480 2481 2482 2483 2484
			;;
		esac
		return
	fi
}

2485 2486 2487 2488 2489
_git_svn ()
{
	local subcommands="
		init fetch clone rebase dcommit log find-rev
		set-tree commit-diff info create-ignore propget
S
SZEDER Gábor 已提交
2490
		proplist show-ignore show-externals branch tag blame
2491
		migrate mkdirs reset gc
2492
		"
2493
	local subcommand="$(__git_find_on_cmdline "$subcommands")"
2494 2495 2496 2497 2498 2499 2500 2501
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
	else
		local remote_opts="--username= --config-dir= --no-auth-cache"
		local fc_opts="
			--follow-parent --authors-file= --repack=
			--no-metadata --use-svm-props --use-svnsync-props
			--log-window-size= --no-checkout --quiet
S
SZEDER Gábor 已提交
2502 2503
			--repack-flags --use-log-author --localtime
			--ignore-paths= $remote_opts
2504 2505 2506 2507 2508
			"
		local init_opts="
			--template= --shared= --trunk= --tags=
			--branches= --stdlayout --minimize-url
			--no-metadata --use-svm-props --use-svnsync-props
S
SZEDER Gábor 已提交
2509 2510
			--rewrite-root= --prefix= --use-log-author
			--add-author-from $remote_opts
2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
			"
		local cmt_opts="
			--edit --rmdir --find-copies-harder --copy-similarity=
			"

		case "$subcommand,$cur" in
		fetch,--*)
			__gitcomp "--revision= --fetch-all $fc_opts"
			;;
		clone,--*)
			__gitcomp "--revision= $fc_opts $init_opts"
			;;
		init,--*)
			__gitcomp "$init_opts"
			;;
		dcommit,--*)
			__gitcomp "
				--merge --strategy= --verbose --dry-run
S
SZEDER Gábor 已提交
2529 2530
				--fetch-all --no-rebase --commit-url
				--revision $cmt_opts $fc_opts
2531 2532 2533 2534 2535 2536
				"
			;;
		set-tree,--*)
			__gitcomp "--stdin $cmt_opts $fc_opts"
			;;
		create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
2537
		show-externals,--*|mkdirs,--*)
2538 2539 2540 2541 2542 2543
			__gitcomp "--revision="
			;;
		log,--*)
			__gitcomp "
				--limit= --revision= --verbose --incremental
				--oneline --show-commit --non-recursive
S
SZEDER Gábor 已提交
2544
				--authors-file= --color
2545 2546 2547 2548 2549
				"
			;;
		rebase,--*)
			__gitcomp "
				--merge --verbose --strategy= --local
S
SZEDER Gábor 已提交
2550
				--fetch-all --dry-run $fc_opts
2551 2552 2553 2554 2555 2556 2557 2558
				"
			;;
		commit-diff,--*)
			__gitcomp "--message= --file= --revision= $cmt_opts"
			;;
		info,--*)
			__gitcomp "--url"
			;;
S
SZEDER Gábor 已提交
2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573
		branch,--*)
			__gitcomp "--dry-run --message --tag"
			;;
		tag,--*)
			__gitcomp "--dry-run --message"
			;;
		blame,--*)
			__gitcomp "--git-format"
			;;
		migrate,--*)
			__gitcomp "
				--config-dir= --ignore-paths= --minimize
				--no-auth-cache --username=
				"
			;;
2574 2575 2576
		reset,--*)
			__gitcomp "--revision= --parent"
			;;
2577 2578 2579 2580 2581 2582 2583
		*)
			COMPREPLY=()
			;;
		esac
	fi
}

2584 2585 2586
_git_tag ()
{
	local i c=1 f=0
2587 2588
	while [ $c -lt $cword ]; do
		i="${words[c]}"
2589 2590
		case "$i" in
		-d|-v)
2591
			__gitcomp_nl "$(__git_tags)"
2592 2593 2594 2595 2596 2597 2598 2599 2600
			return
			;;
		-f)
			f=1
			;;
		esac
		c=$((++c))
	done

2601
	case "$prev" in
2602 2603 2604
	-m|-F)
		COMPREPLY=()
		;;
2605
	-*|tag)
2606
		if [ $f = 1 ]; then
2607
			__gitcomp_nl "$(__git_tags)"
2608 2609 2610 2611 2612
		else
			COMPREPLY=()
		fi
		;;
	*)
2613
		__gitcomp_nl "$(__git_refs)"
2614 2615 2616 2617
		;;
	esac
}

2618 2619 2620 2621 2622
_git_whatchanged ()
{
	_git_log
}

2623 2624
_git ()
{
2625 2626
	local i c=1 command __git_dir

2627
	if [[ -n ${ZSH_VERSION-} ]]; then
2628 2629
		emulate -L bash
		setopt KSH_TYPESET
2630 2631 2632 2633

		# workaround zsh's bug that leaves 'words' as a special
		# variable in versions < 4.3.12
		typeset -h words
2634 2635 2636 2637

		# workaround zsh's bug that quotes spaces in the COMPREPLY
		# array if IFS doesn't contain spaces.
		typeset -h IFS
2638 2639
	fi

2640 2641
	local cur words cword prev
	_get_comp_words_by_ref -n =: cur words cword prev
2642 2643
	while [ $c -lt $cword ]; do
		i="${words[c]}"
2644 2645 2646
		case "$i" in
		--git-dir=*) __git_dir="${i#--git-dir=}" ;;
		--bare)      __git_dir="." ;;
2647 2648
		--version|-p|--paginate) ;;
		--help) command="help"; break ;;
2649 2650 2651 2652 2653
		*) command="$i"; break ;;
		esac
		c=$((++c))
	done

2654
	if [ -z "$command" ]; then
2655
		case "$cur" in
2656
		--*)   __gitcomp "
2657
			--paginate
2658 2659 2660 2661 2662
			--no-pager
			--git-dir=
			--bare
			--version
			--exec-path
2663
			--html-path
2664
			--work-tree=
J
Josh Triplett 已提交
2665
			--namespace=
2666
			--help
2667 2668
			"
			;;
J
Jonathan Nieder 已提交
2669 2670
		*)     __git_compute_porcelain_commands
		       __gitcomp "$__git_porcelain_commands $(__git_aliases)" ;;
2671 2672
		esac
		return
2673
	fi
2674

2675
	local completion_func="_git_${command//-/_}"
2676
	declare -f $completion_func >/dev/null && $completion_func && return
2677

2678
	local expansion=$(__git_aliased_command "$command")
2679 2680
	if [ -n "$expansion" ]; then
		completion_func="_git_${expansion//-/_}"
2681
		declare -f $completion_func >/dev/null && $completion_func
2682
	fi
2683 2684 2685 2686
}

_gitk ()
{
2687
	if [[ -n ${ZSH_VERSION-} ]]; then
2688 2689
		emulate -L bash
		setopt KSH_TYPESET
2690 2691 2692 2693

		# workaround zsh's bug that leaves 'words' as a special
		# variable in versions < 4.3.12
		typeset -h words
2694 2695 2696 2697

		# workaround zsh's bug that quotes spaces in the COMPREPLY
		# array if IFS doesn't contain spaces.
		typeset -h IFS
2698 2699
	fi

2700 2701 2702
	local cur words cword prev
	_get_comp_words_by_ref -n =: cur words cword prev

2703 2704
	__git_has_doubledash && return

2705
	local g="$(__gitdir)"
2706
	local merge=""
2707
	if [ -f "$g/MERGE_HEAD" ]; then
2708 2709
		merge="--merge"
	fi
2710 2711
	case "$cur" in
	--*)
2712 2713 2714 2715 2716
		__gitcomp "
			$__git_log_common_options
			$__git_log_gitk_options
			$merge
			"
2717 2718 2719
		return
		;;
	esac
2720
	__git_complete_revlist
2721 2722
}

2723 2724 2725 2726
complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
	|| complete -o default -o nospace -F _git git
complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
	|| complete -o default -o nospace -F _gitk gitk
2727 2728 2729 2730 2731

# The following are necessary only for Cygwin, and only are needed
# when the user has tab-completed the executable name and consequently
# included the '.exe' suffix.
#
2732
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2733 2734
complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
	|| complete -o default -o nospace -F _git git.exe
2735
fi
2736

2737
if [[ -n ${ZSH_VERSION-} ]]; then
2738
	__git_shopt () {
2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760
		local option
		if [ $# -ne 2 ]; then
			echo "USAGE: $0 (-q|-s|-u) <option>" >&2
			return 1
		fi
		case "$2" in
		nullglob)
			option="$2"
			;;
		*)
			echo "$0: invalid option: $2" >&2
			return 1
		esac
		case "$1" in
		-q)	setopt | grep -q "$option" ;;
		-u)	unsetopt "$option" ;;
		-s)	setopt "$option" ;;
		*)
			echo "$0: invalid flag: $1" >&2
			return 1
		esac
	}
2761 2762 2763 2764
else
	__git_shopt () {
		shopt "$@"
	}
2765
fi