git-completion.bash 32.8 KB
Newer Older
1 2 3
#
# bash completion support for core Git.
#
4
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
5
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6
# Distributed under the GNU General Public License, version 2.0.
7 8 9 10 11 12 13 14
#
# 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
15
#    *) common --long-options
16 17 18 19 20 21 22
#
# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
#    2) Added the following line to your .bashrc:
#        source ~/.git-completion.sh
#
23 24 25 26 27 28 29
#    3) You may want to make sure the git executable is available
#       in your PATH before this script is sourced, as some caching
#       is performed while the script loads.  If git isn't found
#       at source time then all lookups will be done on demand,
#       which may be slightly slower.
#
#    4) Consider changing your PS1 to also show the current branch:
30 31 32 33 34 35
#        PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
#
#       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.
#
36 37 38 39 40 41 42 43 44 45 46
# 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
#
47

48 49 50 51 52
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac

53 54
__gitdir ()
{
55 56 57 58 59 60 61 62 63 64 65 66 67
	if [ -z "$1" ]; then
		if [ -n "$__git_dir" ]; then
			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
68 69
}

70 71
__git_ps1 ()
{
72 73 74 75
	local g="$(git rev-parse --git-dir 2>/dev/null)"
	if [ -n "$g" ]; then
		local r
		local b
76
		if [ -d "$g/rebase-apply" ]
77
		then
78
			if test -f "$g/rebase-apply/rebasing"
J
Junio C Hamano 已提交
79 80
			then
				r="|REBASE"
81
			elif test -f "$g/rebase-apply/applying"
J
Junio C Hamano 已提交
82 83 84 85 86
			then
				r="|AM"
			else
				r="|AM/REBASE"
			fi
87
			b="$(git symbolic-ref HEAD 2>/dev/null)"
88
		elif [ -f "$g/rebase-merge/interactive" ]
89 90
		then
			r="|REBASE-i"
91 92
			b="$(cat "$g/rebase-merge/head-name")"
		elif [ -d "$g/rebase-merge" ]
93 94
		then
			r="|REBASE-m"
95
			b="$(cat "$g/rebase-merge/head-name")"
96 97 98 99 100
		elif [ -f "$g/MERGE_HEAD" ]
		then
			r="|MERGING"
			b="$(git symbolic-ref HEAD 2>/dev/null)"
		else
101
			if [ -f "$g/BISECT_LOG" ]
102 103 104 105 106
			then
				r="|BISECTING"
			fi
			if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
			then
107 108
				if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
				then
109
					b="$(cut -c1-7 "$g/HEAD")..."
110
				fi
111 112 113
			fi
		fi

114
		if [ -n "$1" ]; then
115
			printf "$1" "${b##refs/heads/}$r"
116
		else
117
			printf " (%s)" "${b##refs/heads/}$r"
118 119 120 121
		fi
	fi
}

122 123 124 125 126 127 128 129 130 131 132 133
__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
}

134 135
__gitcomp ()
{
136
	local cur="${COMP_WORDS[COMP_CWORD]}"
137
	if [ $# -gt 2 ]; then
138 139
		cur="$3"
	fi
140 141 142 143 144
	case "$cur" in
	--*=)
		COMPREPLY=()
		;;
	*)
145 146 147 148
		local IFS=$'\n'
		COMPREPLY=($(compgen -P "$2" \
			-W "$(__gitcomp_1 "$1" "$4")" \
			-- "$cur"))
149 150
		;;
	esac
151 152
}

153 154
__git_heads ()
{
155
	local cmd i is_hash=y dir="$(__gitdir "$1")"
156 157 158 159 160 161 162 163
	if [ -d "$dir" ]; then
		for i in $(git --git-dir="$dir" \
			for-each-ref --format='%(refname)' \
			refs/heads ); do
			echo "${i#refs/heads/}"
		done
		return
	fi
164
	for i in $(git ls-remote "$1" 2>/dev/null); do
165 166 167 168 169 170 171 172 173
		case "$is_hash,$i" in
		y,*) is_hash=n ;;
		n,*^{}) is_hash=y ;;
		n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

174 175 176 177 178 179 180 181 182 183 184
__git_tags ()
{
	local cmd i is_hash=y dir="$(__gitdir "$1")"
	if [ -d "$dir" ]; then
		for i in $(git --git-dir="$dir" \
			for-each-ref --format='%(refname)' \
			refs/tags ); do
			echo "${i#refs/tags/}"
		done
		return
	fi
185
	for i in $(git ls-remote "$1" 2>/dev/null); do
186 187 188 189 190 191 192 193 194
		case "$is_hash,$i" in
		y,*) is_hash=n ;;
		n,*^{}) is_hash=y ;;
		n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

195 196
__git_refs ()
{
197
	local cmd i is_hash=y dir="$(__gitdir "$1")"
198
	if [ -d "$dir" ]; then
199 200 201 202 203 204 205 206 207 208 209 210
		if [ -e "$dir/HEAD" ]; then echo HEAD; fi
		for i in $(git --git-dir="$dir" \
			for-each-ref --format='%(refname)' \
			refs/tags refs/heads refs/remotes); do
			case "$i" in
				refs/tags/*)    echo "${i#refs/tags/}" ;;
				refs/heads/*)   echo "${i#refs/heads/}" ;;
				refs/remotes/*) echo "${i#refs/remotes/}" ;;
				*)              echo "$i" ;;
			esac
		done
		return
211
	fi
212
	for i in $(git ls-remote "$dir" 2>/dev/null); do
213 214 215 216 217
		case "$is_hash,$i" in
		y,*) is_hash=n ;;
		n,*^{}) is_hash=y ;;
		n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
		n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
218
		n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
219 220 221 222 223 224 225
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

__git_refs2 ()
{
226 227 228
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
229 230 231
	done
}

232 233 234
__git_refs_remotes ()
{
	local cmd i is_hash=y
235
	for i in $(git ls-remote "$1" 2>/dev/null); do
236 237 238 239 240 241 242 243 244 245 246 247 248
		case "$is_hash,$i" in
		n,refs/heads/*)
			is_hash=y
			echo "$i:refs/remotes/$1/${i#refs/heads/}"
			;;
		y,*) is_hash=n ;;
		n,*^{}) is_hash=y ;;
		n,refs/tags/*) is_hash=y;;
		n,*) is_hash=y; ;;
		esac
	done
}

249 250
__git_remotes ()
{
251
	local i ngoff IFS=$'\n' d="$(__gitdir)"
252
	shopt -q nullglob || ngoff=1
253
	shopt -s nullglob
254 255
	for i in "$d/remotes"/*; do
		echo ${i#$d/remotes/}
256
	done
257
	[ "$ngoff" ] && shopt -u nullglob
258
	for i in $(git --git-dir="$d" config --list); do
259 260 261 262 263 264 265
		case "$i" in
		remote.*.url=*)
			i="${i#remote.}"
			echo "${i/.url=*/}"
			;;
		esac
	done
266 267
}

268 269
__git_merge_strategies ()
{
270 271 272 273
	if [ -n "$__git_merge_strategylist" ]; then
		echo "$__git_merge_strategylist"
		return
	fi
274 275 276 277 278 279
	git merge -s help 2>&1 |
	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
		s/\.$//
		s/.*://
		s/^[ 	]*//
		s/[ 	]*$//
280
		p
281
	}'
282
}
283
__git_merge_strategylist=
284
__git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
285

286 287
__git_complete_file ()
{
288
	local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
289 290
	case "$cur" in
	?*:*)
291 292
		ref="${cur%%:*}"
		cur="${cur#*:}"
293 294
		case "$cur" in
		?*/*)
295 296
			pfx="${cur%/*}"
			cur="${cur##*/}"
297 298 299 300 301 302 303
			ls="$ref:$pfx"
			pfx="$pfx/"
			;;
		*)
			ls="$ref"
			;;
	    esac
304 305 306 307 308 309

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

310
		local IFS=$'\n'
311
		COMPREPLY=($(compgen -P "$pfx" \
312
			-W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
313 314 315 316 317 318 319 320
				| sed '/^100... blob /{
				           s,^.*	,,
				           s,$, ,
				       }
				       /^120000 blob /{
				           s,^.*	,,
				           s,$, ,
				       }
321 322 323 324 325 326 327 328
				       /^040000 tree /{
				           s,^.*	,,
				           s,$,/,
				       }
				       s/^.*	//')" \
			-- "$cur"))
		;;
	*)
329
		__gitcomp "$(__git_refs)"
330 331 332 333
		;;
	esac
}

334 335 336 337 338 339 340
__git_complete_revlist ()
{
	local pfx cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	*...*)
		pfx="${cur%...*}..."
		cur="${cur#*...}"
341
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
342 343 344 345
		;;
	*..*)
		pfx="${cur%..*}.."
		cur="${cur#*..}"
346 347
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
		;;
348
	*)
349
		__gitcomp "$(__git_refs)"
350 351 352 353
		;;
	esac
}

354
__git_all_commands ()
355
{
356 357
	if [ -n "$__git_all_commandlist" ]; then
		echo "$__git_all_commandlist"
358 359
		return
	fi
360 361
	local i IFS=" "$'\n'
	for i in $(git help -a|egrep '^ ')
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	do
		case $i in
		*--*)             : helper pattern;;
		*) echo $i;;
		esac
	done
}
__git_all_commandlist=
__git_all_commandlist="$(__git_all_commands 2>/dev/null)"

__git_porcelain_commands ()
{
	if [ -n "$__git_porcelain_commandlist" ]; then
		echo "$__git_porcelain_commandlist"
		return
	fi
	local i IFS=" "$'\n'
	for i in "help" $(__git_all_commands)
380 381
	do
		case $i in
382
		*--*)             : helper pattern;;
383 384 385
		applymbox)        : ask gittus;;
		applypatch)       : ask gittus;;
		archimport)       : import;;
386
		cat-file)         : plumbing;;
387
		check-attr)       : plumbing;;
388
		check-ref-format) : plumbing;;
389
		checkout-index)   : plumbing;;
390
		commit-tree)      : plumbing;;
391
		count-objects)    : infrequent;;
392 393
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
394 395
		cvsserver)        : daemon;;
		daemon)           : daemon;;
396 397 398
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
399
		fast-import)      : import;;
400
		fast-export)      : export;;
401
		fsck-objects)     : plumbing;;
402
		fetch-pack)       : plumbing;;
403
		fmt-merge-msg)    : plumbing;;
404
		for-each-ref)     : plumbing;;
405 406 407
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
408
		init-db)          : deprecated;;
409
		local-fetch)      : plumbing;;
410 411 412 413
		lost-found)       : infrequent;;
		ls-files)         : plumbing;;
		ls-remote)        : plumbing;;
		ls-tree)          : plumbing;;
414 415 416 417 418 419 420 421 422 423 424
		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;;
425 426 427
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
428 429
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
430
		reflog)           : plumbing;;
431
		repo-config)      : deprecated;;
432 433 434 435 436 437
		rerere)           : plumbing;;
		rev-list)         : plumbing;;
		rev-parse)        : plumbing;;
		runstatus)        : plumbing;;
		sh-setup)         : internal;;
		shell)            : daemon;;
438
		show-ref)         : plumbing;;
439 440 441 442 443
		send-pack)        : plumbing;;
		show-index)       : plumbing;;
		ssh-*)            : transport;;
		stripspace)       : plumbing;;
		symbolic-ref)     : plumbing;;
444
		tar-tree)         : deprecated;;
445 446
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
447
		update-index)     : plumbing;;
448 449 450 451 452
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
453 454
		var)              : infrequent;;
		verify-pack)      : infrequent;;
455
		verify-tag)       : plumbing;;
456 457 458 459
		*) echo $i;;
		esac
	done
}
460 461
__git_porcelain_commandlist=
__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
462

463 464
__git_aliases ()
{
465
	local i IFS=$'\n'
466
	for i in $(git --git-dir="$(__gitdir)" config --list); do
467 468 469 470 471 472 473
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/=*/}"
			;;
		esac
	done
474 475 476 477
}

__git_aliased_command ()
{
478
	local word cmdline=$(git --git-dir="$(__gitdir)" \
479
		config --get "alias.$1")
480 481 482 483 484 485 486 487
	for word in $cmdline; do
		if [ "${word##-*}" ]; then
			echo $word
			return
		fi
	done
}

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
__git_find_subcommand ()
{
	local word subcommand c=1

	while [ $c -lt $COMP_CWORD ]; do
		word="${COMP_WORDS[c]}"
		for subcommand in $1; do
			if [ "$subcommand" = "$word" ]; then
				echo "$subcommand"
				return
			fi
		done
		c=$((++c))
	done
}

504 505 506 507 508 509 510 511 512 513 514 515
__git_has_doubledash ()
{
	local c=1
	while [ $c -lt $COMP_CWORD ]; do
		if [ "--" = "${COMP_WORDS[c]}" ]; then
			return 0
		fi
		c=$((++c))
	done
	return 1
}

516
__git_whitespacelist="nowarn warn error error-all fix"
517 518 519

_git_am ()
{
520
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
521
	if [ -d "$dir"/rebase-apply ]; then
522
		__gitcomp "--skip --resolved --abort"
523 524 525 526
		return
	fi
	case "$cur" in
	--whitespace=*)
527
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
528 529 530
		return
		;;
	--*)
531
		__gitcomp "
532 533
			--signoff --utf8 --binary --3way --interactive
			--whitespace=
534
			"
535 536 537 538 539 540 541 542 543 544
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--whitespace=*)
545
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
546 547 548
		return
		;;
	--*)
549
		__gitcomp "
550 551 552 553
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
			--whitespace= --inaccurate-eof --verbose
554
			"
555 556 557 558 559
		return
	esac
	COMPREPLY=()
}

560 561
_git_add ()
{
562 563
	__git_has_doubledash && return

564 565 566
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
567 568 569 570
		__gitcomp "
			--interactive --refresh --patch --update --dry-run
			--ignore-errors
			"
571 572 573 574 575
		return
	esac
	COMPREPLY=()
}

576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
_git_archive ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--format=*)
		__gitcomp "$(git archive --list)" "" "${cur##--format=}"
		return
		;;
	--remote=*)
		__gitcomp "$(__git_remotes)" "" "${cur##--remote=}"
		return
		;;
	--*)
		__gitcomp "
			--format= --list --verbose
			--prefix= --remote= --exec=
			"
		return
		;;
	esac
	__git_complete_file
}

599 600
_git_bisect ()
{
601 602
	__git_has_doubledash && return

603
	local subcommands="start bad good skip reset visualize replay log run"
604 605 606
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
607 608 609
		return
	fi

610
	case "$subcommand" in
611
	bad|good|reset|skip)
612 613 614 615 616 617 618 619
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

620 621
_git_branch ()
{
622 623 624 625 626 627 628 629 630 631 632
	local i c=1 only_local_ref="n" has_r="n"

	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
		-d|-m)	only_local_ref="y" ;;
		-r)	has_r="y" ;;
		esac
		c=$((++c))
	done

S
SZEDER Gábor 已提交
633 634 635 636 637
	case "${COMP_WORDS[COMP_CWORD]}" in
	--*=*)	COMPREPLY=() ;;
	--*)
		__gitcomp "
			--color --no-color --verbose --abbrev= --no-abbrev
638
			--track --no-track --contains --merged --no-merged
S
SZEDER Gábor 已提交
639 640
			"
		;;
641 642 643 644 645 646 647
	*)
		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
			__gitcomp "$(__git_heads)"
		else
			__gitcomp "$(__git_refs)"
		fi
		;;
S
SZEDER Gábor 已提交
648
	esac
649 650
}

651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
_git_bundle ()
{
	local mycword="$COMP_CWORD"
	case "${COMP_WORDS[0]}" in
	git)
		local cmd="${COMP_WORDS[2]}"
		mycword="$((mycword-1))"
		;;
	git-bundle*)
		local cmd="${COMP_WORDS[1]}"
		;;
	esac
	case "$mycword" in
	1)
		__gitcomp "create list-heads verify unbundle"
		;;
	2)
		# looking for a file
		;;
	*)
		case "$cmd" in
			create)
				__git_complete_revlist
			;;
		esac
		;;
	esac
}

680 681
_git_checkout ()
{
682 683
	__git_has_doubledash && return

684
	__gitcomp "$(__git_refs)"
685 686
}

687 688 689 690 691
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

692 693 694 695 696
_git_cherry_pick ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
697
		__gitcomp "--edit --no-commit"
698 699
		;;
	*)
700
		__gitcomp "$(__git_refs)"
701 702 703 704
		;;
	esac
}

705 706 707 708 709 710 711 712 713 714 715 716 717 718
_git_clean ()
{
	__git_has_doubledash && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--dry-run --quiet"
		return
		;;
	esac
	COMPREPLY=()
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
_git_clone ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--local
			--no-hardlinks
			--shared
			--reference
			--quiet
			--no-checkout
			--bare
			--mirror
			--origin
			--upload-pack
			--template=
			--depth
			"
		return
		;;
	esac
	COMPREPLY=()
}

744 745
_git_commit ()
{
746 747
	__git_has_doubledash && return

748 749 750
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
751
		__gitcomp "
752 753
			--all --author= --signoff --verify --no-verify
			--edit --amend --include --only
754
			"
755 756 757 758 759
		return
	esac
	COMPREPLY=()
}

760 761
_git_describe ()
{
762 763 764 765 766 767 768 769 770
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--all --tags --contains --abbrev= --candidates=
			--exact-match --debug --long --match --always
			"
		return
	esac
771 772 773
	__gitcomp "$(__git_refs)"
}

774 775
_git_diff ()
{
776 777
	__git_has_doubledash && return

778 779 780 781 782 783
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--cached --stat --numstat --shortstat --summary
			--patch-with-stat --name-only --name-status --color
			--no-color --color-words --no-renames --check
784
			--full-index --binary --abbrev --diff-filter=
785 786 787
			--find-copies-harder --pickaxe-all --pickaxe-regex
			--text --ignore-space-at-eol --ignore-space-change
			--ignore-all-space --exit-code --quiet --ext-diff
788 789
			--no-ext-diff
			--no-prefix --src-prefix= --dst-prefix=
790
			--base --ours --theirs
791
			"
792 793 794
		return
		;;
	esac
795 796 797 798 799 800 801 802 803
	__git_complete_file
}

_git_fetch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-fetch*,1)
804
		__gitcomp "$(__git_remotes)"
805 806
		;;
	git,2)
807
		__gitcomp "$(__git_remotes)"
808 809 810 811
		;;
	*)
		case "$cur" in
		*:*)
812 813 814 815 816 817
			local pfx=""
			case "$COMP_WORDBREAKS" in
			*:*) : great ;;
			*)   pfx="${cur%%:*}:" ;;
			esac
			__gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
818 819 820 821 822 823 824
			;;
		*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-fetch) remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
825
			__gitcomp "$(__git_refs2 "$remote")"
826 827 828 829 830 831
			;;
		esac
		;;
	esac
}

832 833 834 835 836
_git_format_patch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
837
		__gitcomp "
838 839 840
			--stdout --attach --thread
			--output-directory
			--numbered --start-number
841
			--numbered-files
842 843 844 845
			--keep-subject
			--signoff
			--in-reply-to=
			--full-index --binary
846
			--not --all
847
			--cover-letter
848
			--no-prefix --src-prefix= --dst-prefix=
849
			"
850 851 852 853 854 855
		return
		;;
	esac
	__git_complete_revlist
}

856 857 858 859 860
_git_gc ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
861
		__gitcomp "--prune --aggressive"
862 863 864 865 866 867
		return
		;;
	esac
	COMPREPLY=()
}

868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
_git_grep ()
{
	__git_has_doubledash && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--cached
			--text --ignore-case --word-regexp --invert-match
			--full-name
			--extended-regexp --basic-regexp --fixed-strings
			--files-with-matches --name-only
			--files-without-match
			--count
			--and --or --not --all-match
			"
		return
		;;
	esac
	COMPREPLY=()
}

891 892 893 894 895 896 897 898 899
_git_help ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--all --info --man --web"
		return
		;;
	esac
900 901 902 903 904
	__gitcomp "$(__git_all_commands)
		attributes cli core-tutorial cvs-migration
		diffcore gitk glossary hooks ignore modules
		repository-layout tutorial tutorial-2
		"
905 906
}

907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
_git_init ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--shared=*)
		__gitcomp "
			false true umask group all world everybody
			" "" "${cur##--shared=}"
		return
		;;
	--*)
		__gitcomp "--quiet --bare --template= --shared --shared="
		return
		;;
	esac
	COMPREPLY=()
}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
_git_ls_files ()
{
	__git_has_doubledash && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	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=()
}

945 946
_git_ls_remote ()
{
947
	__gitcomp "$(__git_remotes)"
948 949 950 951 952 953 954 955 956
}

_git_ls_tree ()
{
	__git_complete_file
}

_git_log ()
{
957 958
	__git_has_doubledash && return

959 960 961
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
962
		__gitcomp "
963
			oneline short medium full fuller email raw
964
			" "" "${cur##--pretty=}"
965 966
		return
		;;
967 968 969 970 971 972
	--date=*)
		__gitcomp "
			relative iso8601 rfc2822 short local default
		" "" "${cur##--date=}"
		return
		;;
973
	--*)
974
		__gitcomp "
975 976
			--max-count= --max-age= --since= --after=
			--min-age= --before= --until=
977
			--root --topo-order --date-order --reverse
978
			--no-merges --follow
979
			--abbrev-commit --abbrev=
980
			--relative-date --date=
981 982
			--author= --committer= --grep=
			--all-match
983
			--pretty= --name-status --name-only --raw
984
			--not --all
985
			--left-right --cherry-pick
986
			--graph
987 988 989
			--stat --numstat --shortstat
			--decorate --diff-filter=
			--color-words --walk-reflogs
990
			--parents --children --full-history
991
			--merge
992
			"
993 994 995
		return
		;;
	esac
996
	__git_complete_revlist
997 998
}

999 1000 1001
_git_merge ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
1002 1003
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
1004
		__gitcomp "$(__git_merge_strategies)"
1005 1006
		return
	esac
1007
	case "$cur" in
1008
	--strategy=*)
1009
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1010 1011
		return
		;;
1012
	--*)
1013
		__gitcomp "
1014
			--no-commit --no-stat --log --no-log --squash --strategy
1015
			"
1016 1017
		return
	esac
1018
	__gitcomp "$(__git_refs)"
1019 1020
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
_git_mergetool ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--tool=*)
		__gitcomp "
			kdiff3 tkdiff meld xxdiff emerge
			vimdiff gvimdiff ecmerge opendiff
			" "" "${cur##--tool=}"
		return
		;;
	--*)
		__gitcomp "--tool="
		return
		;;
	esac
	COMPREPLY=()
}

1040 1041
_git_merge_base ()
{
1042
	__gitcomp "$(__git_refs)"
1043 1044
}

1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
_git_mv ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--dry-run"
		return
		;;
	esac
	COMPREPLY=()
}

1057 1058
_git_name_rev ()
{
1059
	__gitcomp "--tags --all --stdin"
1060 1061
}

1062 1063 1064 1065 1066 1067
_git_pull ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-pull*,1)
1068
		__gitcomp "$(__git_remotes)"
1069 1070
		;;
	git,2)
1071
		__gitcomp "$(__git_remotes)"
1072 1073 1074 1075 1076 1077 1078
		;;
	*)
		local remote
		case "${COMP_WORDS[0]}" in
		git-pull)  remote="${COMP_WORDS[1]}" ;;
		git)       remote="${COMP_WORDS[2]}" ;;
		esac
1079
		__gitcomp "$(__git_refs "$remote")"
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
		;;
	esac
}

_git_push ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-push*,1)
1090
		__gitcomp "$(__git_remotes)"
1091 1092
		;;
	git,2)
1093
		__gitcomp "$(__git_remotes)"
1094 1095 1096 1097 1098 1099 1100 1101 1102
		;;
	*)
		case "$cur" in
		*:*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-push)  remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
1103 1104 1105 1106 1107 1108 1109 1110

			local pfx=""
			case "$COMP_WORDBREAKS" in
			*:*) : great ;;
			*)   pfx="${cur%%:*}:" ;;
			esac

			__gitcomp "$(__git_refs "$remote")" "$pfx" "${cur#*:}"
1111
			;;
1112 1113 1114
		+*)
			__gitcomp "$(__git_refs)" + "${cur#+}"
			;;
1115
		*)
1116
			__gitcomp "$(__git_refs)"
1117 1118 1119 1120 1121 1122
			;;
		esac
		;;
	esac
}

1123 1124
_git_rebase ()
{
1125
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1126
	if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1127
		__gitcomp "--continue --skip --abort"
1128 1129
		return
	fi
1130 1131
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
1132
		__gitcomp "$(__git_merge_strategies)"
1133 1134
		return
	esac
1135
	case "$cur" in
1136
	--strategy=*)
1137
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1138 1139
		return
		;;
1140
	--*)
1141
		__gitcomp "--onto --merge --strategy --interactive"
1142 1143
		return
	esac
1144
	__gitcomp "$(__git_refs)"
1145 1146
}

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
_git_send_email ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--bcc --cc --cc-cmd --chain-reply-to --compose
			--dry-run --envelope-sender --from --identity
			--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
			--smtp-server-port --smtp-ssl --smtp-user --subject
			--suppress-cc --suppress-from --thread --to"
		return
		;;
	esac
	COMPREPLY=()
}

1165
_git_config ()
1166 1167 1168 1169 1170
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prv="${COMP_WORDS[COMP_CWORD-1]}"
	case "$prv" in
	branch.*.remote)
1171
		__gitcomp "$(__git_remotes)"
1172 1173 1174
		return
		;;
	branch.*.merge)
1175
		__gitcomp "$(__git_refs)"
1176 1177 1178 1179 1180
		return
		;;
	remote.*.fetch)
		local remote="${prv#remote.}"
		remote="${remote%.fetch}"
1181
		__gitcomp "$(__git_refs_remotes "$remote")"
1182 1183 1184 1185 1186
		return
		;;
	remote.*.push)
		local remote="${prv#remote.}"
		remote="${remote%.push}"
1187
		__gitcomp "$(git --git-dir="$(__gitdir)" \
1188
			for-each-ref --format='%(refname):%(refname)' \
1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
			refs/heads)"
		return
		;;
	pull.twohead|pull.octopus)
		__gitcomp "$(__git_merge_strategies)"
		return
		;;
	color.branch|color.diff|color.status)
		__gitcomp "always never auto"
		return
		;;
	color.*.*)
		__gitcomp "
			black red green yellow blue magenta cyan white
			bold dim ul blink reverse
			"
1205 1206 1207 1208 1209 1210 1211 1212 1213
		return
		;;
	*.*)
		COMPREPLY=()
		return
		;;
	esac
	case "$cur" in
	--*)
1214
		__gitcomp "
1215
			--global --system --file=
1216
			--list --replace-all
1217
			--get --get-all --get-regexp
1218
			--add --unset --unset-all
1219
			--remove-section --rename-section
1220
			"
1221 1222 1223 1224 1225
		return
		;;
	branch.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
1226
		__gitcomp "remote merge" "$pfx" "$cur"
1227 1228 1229 1230 1231
		return
		;;
	branch.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
1232
		__gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1233 1234 1235 1236 1237
		return
		;;
	remote.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
1238 1239 1240 1241
		__gitcomp "
			url fetch push skipDefaultUpdate
			receivepack uploadpack tagopt
			" "$pfx" "$cur"
1242 1243 1244 1245 1246
		return
		;;
	remote.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
1247
		__gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1248 1249 1250
		return
		;;
	esac
1251
	__gitcomp "
1252 1253 1254 1255 1256 1257
		apply.whitespace
		core.fileMode
		core.gitProxy
		core.ignoreStat
		core.preferSymlinkRefs
		core.logAllRefUpdates
1258
		core.loosecompression
1259 1260 1261 1262
		core.repositoryFormatVersion
		core.sharedRepository
		core.warnAmbiguousRefs
		core.compression
1263 1264
		core.packedGitWindowSize
		core.packedGitLimit
1265
		clean.requireForce
1266 1267 1268 1269 1270
		color.branch
		color.branch.current
		color.branch.local
		color.branch.remote
		color.branch.plain
1271
		color.diff
1272 1273 1274 1275 1276 1277 1278
		color.diff.plain
		color.diff.meta
		color.diff.frag
		color.diff.old
		color.diff.new
		color.diff.commit
		color.diff.whitespace
1279 1280
		color.pager
		color.status
1281 1282 1283 1284 1285 1286 1287 1288
		color.status.header
		color.status.added
		color.status.changed
		color.status.untracked
		diff.renameLimit
		diff.renames
		fetch.unpackLimit
		format.headers
1289
		format.subjectprefix
1290 1291
		gitcvs.enabled
		gitcvs.logfile
1292
		gitcvs.allbinary
1293 1294
		gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dbpass
		gitcvs.dbtablenameprefix
1295
		gc.packrefs
1296 1297 1298 1299
		gc.reflogexpire
		gc.reflogexpireunreachable
		gc.rerereresolved
		gc.rerereunresolved
1300 1301 1302 1303 1304 1305
		http.sslVerify
		http.sslCert
		http.sslKey
		http.sslCAInfo
		http.sslCAPath
		http.maxRequests
1306 1307
		http.lowSpeedLimit
		http.lowSpeedTime
1308
		http.noEPSV
1309 1310 1311
		i18n.commitEncoding
		i18n.logOutputEncoding
		log.showroot
1312
		merge.tool
1313 1314
		merge.summary
		merge.verbosity
1315
		pack.window
1316
		pack.depth
1317 1318 1319 1320
		pack.windowMemory
		pack.compression
		pack.deltaCacheSize
		pack.deltaCacheLimit
1321 1322
		pull.octopus
		pull.twohead
1323
		repack.useDeltaBaseOffset
1324 1325 1326
		showbranch.default
		tar.umask
		transfer.unpackLimit
1327 1328
		receive.unpackLimit
		receive.denyNonFastForwards
1329 1330 1331
		user.name
		user.email
		user.signingkey
1332
		branch. remote.
1333
	"
1334 1335
}

1336 1337
_git_remote ()
{
1338 1339 1340
	local subcommands="add rm show prune update"
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1341
		__gitcomp "$subcommands"
1342 1343 1344
		return
	fi

1345
	case "$subcommand" in
1346
	rm|show|prune)
1347 1348
		__gitcomp "$(__git_remotes)"
		;;
1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	update)
		local i c='' IFS=$'\n'
		for i in $(git --git-dir="$(__gitdir)" config --list); do
			case "$i" in
			remotes.*)
				i="${i#remotes.}"
				c="$c ${i/=*/}"
				;;
			esac
		done
		__gitcomp "$c"
		;;
1361 1362 1363 1364 1365 1366
	*)
		COMPREPLY=()
		;;
	esac
}

1367 1368
_git_reset ()
{
1369 1370
	__git_has_doubledash && return

1371
	local cur="${COMP_WORDS[COMP_CWORD]}"
1372 1373 1374 1375 1376 1377 1378
	case "$cur" in
	--*)
		__gitcomp "--mixed --hard --soft"
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
1379 1380
}

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
_git_revert ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
		return
		;;
	esac
	COMPREPLY=()
}

1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
_git_rm ()
{
	__git_has_doubledash && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--cached --dry-run --ignore-unmatch --quiet"
		return
		;;
	esac
	COMPREPLY=()
}

1407 1408
_git_shortlog ()
{
1409 1410
	__git_has_doubledash && return

1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--max-count= --max-age= --since= --after=
			--min-age= --before= --until=
			--no-merges
			--author= --committer= --grep=
			--all-match
			--not --all
			--numbered --summary
			"
		return
		;;
	esac
	__git_complete_revlist
}

1429 1430 1431 1432 1433
_git_show ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
1434
		__gitcomp "
1435
			oneline short medium full fuller email raw
1436
			" "" "${cur##--pretty=}"
1437 1438 1439
		return
		;;
	--*)
1440
		__gitcomp "--pretty="
1441 1442 1443 1444 1445 1446
		return
		;;
	esac
	__git_complete_file
}

1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462
_git_show_branch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--all --remotes --topo-order --current --more=
			--list --independent --merge-base --no-name
			--sha1-name --topics --reflog
			"
		return
		;;
	esac
	__git_complete_revlist
}

J
Junio C Hamano 已提交
1463 1464
_git_stash ()
{
1465
	local subcommands='save list show apply clear drop pop create branch'
1466 1467
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1468
		__gitcomp "$subcommands"
1469 1470 1471 1472 1473 1474
	else
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$subcommand,$cur" in
		save,--*)
			__gitcomp "--keep-index"
			;;
1475 1476 1477
		apply,--*)
			__gitcomp "--index"
			;;
1478
		show,--*|drop,--*|pop,--*|branch,--*)
1479 1480 1481 1482 1483 1484
			COMPREPLY=()
			;;
		show,*|apply,*|drop,*|pop,*|branch,*)
			__gitcomp "$(git --git-dir="$(__gitdir)" stash list \
					| sed -n -e 's/:.*//p')"
			;;
1485 1486 1487 1488
		*)
			COMPREPLY=()
			;;
		esac
1489
	fi
J
Junio C Hamano 已提交
1490 1491
}

1492 1493
_git_submodule ()
{
1494 1495
	__git_has_doubledash && return

1496 1497
	local subcommands="add status init update"
	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1498 1499 1500 1501 1502 1503
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$cur" in
		--*)
			__gitcomp "--quiet --cached"
			;;
		*)
1504
			__gitcomp "$subcommands"
1505 1506 1507 1508 1509 1510
			;;
		esac
		return
	fi
}

1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
_git_svn ()
{
	local subcommands="
		init fetch clone rebase dcommit log find-rev
		set-tree commit-diff info create-ignore propget
		proplist show-ignore show-externals
		"
	local subcommand="$(__git_find_subcommand "$subcommands")"
	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
			--repack-flags --user-log-author $remote_opts
			"
		local init_opts="
			--template= --shared= --trunk= --tags=
			--branches= --stdlayout --minimize-url
			--no-metadata --use-svm-props --use-svnsync-props
			--rewrite-root= $remote_opts
			"
		local cmt_opts="
			--edit --rmdir --find-copies-harder --copy-similarity=
			"

		local cur="${COMP_WORDS[COMP_CWORD]}"
		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
				--fetch-all --no-rebase $cmt_opts $fc_opts
				"
			;;
		set-tree,--*)
			__gitcomp "--stdin $cmt_opts $fc_opts"
			;;
		create-ignore,--*|propget,--*|proplist,--*|show-ignore,--*|\
		show-externals,--*)
			__gitcomp "--revision="
			;;
		log,--*)
			__gitcomp "
				--limit= --revision= --verbose --incremental
				--oneline --show-commit --non-recursive
				--authors-file=
				"
			;;
		rebase,--*)
			__gitcomp "
				--merge --verbose --strategy= --local
				--fetch-all $fc_opts
				"
			;;
		commit-diff,--*)
			__gitcomp "--message= --file= --revision= $cmt_opts"
			;;
		info,--*)
			__gitcomp "--url"
			;;
		*)
			COMPREPLY=()
			;;
		esac
	fi
}

1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
_git_tag ()
{
	local i c=1 f=0
	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
		-d|-v)
			__gitcomp "$(__git_tags)"
			return
			;;
		-f)
			f=1
			;;
		esac
		c=$((++c))
	done

	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-m|-F)
		COMPREPLY=()
		;;
	-*|tag|git-tag)
		if [ $f = 1 ]; then
			__gitcomp "$(__git_tags)"
		else
			COMPREPLY=()
		fi
		;;
	*)
		__gitcomp "$(__git_refs)"
		;;
	esac
}

1623 1624
_git ()
{
1625 1626 1627 1628 1629 1630 1631
	local i c=1 command __git_dir

	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
		--git-dir=*) __git_dir="${i#--git-dir=}" ;;
		--bare)      __git_dir="." ;;
1632 1633
		--version|-p|--paginate) ;;
		--help) command="help"; break ;;
1634 1635 1636 1637 1638
		*) command="$i"; break ;;
		esac
		c=$((++c))
	done

1639
	if [ -z "$command" ]; then
1640 1641
		case "${COMP_WORDS[COMP_CWORD]}" in
		--*=*) COMPREPLY=() ;;
1642
		--*)   __gitcomp "
1643
			--paginate
1644 1645 1646 1647 1648
			--no-pager
			--git-dir=
			--bare
			--version
			--exec-path
1649 1650
			--work-tree=
			--help
1651 1652
			"
			;;
1653
		*)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1654 1655
		esac
		return
1656
	fi
1657

1658 1659
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
1660

1661
	case "$command" in
1662
	am)          _git_am ;;
1663
	add)         _git_add ;;
1664
	apply)       _git_apply ;;
1665
	archive)     _git_archive ;;
1666
	bisect)      _git_bisect ;;
1667
	bundle)      _git_bundle ;;
1668 1669
	branch)      _git_branch ;;
	checkout)    _git_checkout ;;
1670
	cherry)      _git_cherry ;;
1671
	cherry-pick) _git_cherry_pick ;;
1672
	clean)       _git_clean ;;
1673
	clone)       _git_clone ;;
1674
	commit)      _git_commit ;;
1675
	config)      _git_config ;;
1676
	describe)    _git_describe ;;
1677 1678
	diff)        _git_diff ;;
	fetch)       _git_fetch ;;
1679
	format-patch) _git_format_patch ;;
1680
	gc)          _git_gc ;;
1681
	grep)        _git_grep ;;
1682
	help)        _git_help ;;
1683
	init)        _git_init ;;
1684
	log)         _git_log ;;
1685
	ls-files)    _git_ls_files ;;
1686 1687
	ls-remote)   _git_ls_remote ;;
	ls-tree)     _git_ls_tree ;;
1688
	merge)       _git_merge;;
1689
	mergetool)   _git_mergetool;;
1690
	merge-base)  _git_merge_base ;;
1691
	mv)          _git_mv ;;
1692
	name-rev)    _git_name_rev ;;
1693 1694
	pull)        _git_pull ;;
	push)        _git_push ;;
1695
	rebase)      _git_rebase ;;
1696
	remote)      _git_remote ;;
1697
	reset)       _git_reset ;;
1698
	revert)      _git_revert ;;
1699
	rm)          _git_rm ;;
1700
	send-email)  _git_send_email ;;
1701
	shortlog)    _git_shortlog ;;
1702
	show)        _git_show ;;
1703
	show-branch) _git_show_branch ;;
J
Junio C Hamano 已提交
1704
	stash)       _git_stash ;;
1705
	submodule)   _git_submodule ;;
1706
	svn)         _git_svn ;;
1707
	tag)         _git_tag ;;
1708 1709 1710
	whatchanged) _git_log ;;
	*)           COMPREPLY=() ;;
	esac
1711 1712 1713 1714
}

_gitk ()
{
1715 1716
	__git_has_doubledash && return

1717
	local cur="${COMP_WORDS[COMP_CWORD]}"
1718 1719 1720 1721 1722
	local g="$(git rev-parse --git-dir 2>/dev/null)"
	local merge=""
	if [ -f $g/MERGE_HEAD ]; then
		merge="--merge"
	fi
1723 1724
	case "$cur" in
	--*)
1725
		__gitcomp "--not --all $merge"
1726 1727 1728
		return
		;;
	esac
1729
	__git_complete_revlist
1730 1731 1732
}

complete -o default -o nospace -F _git git
1733
complete -o default -o nospace -F _gitk gitk
1734 1735 1736 1737 1738

# 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.
#
1739
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1740
complete -o default -o nospace -F _git git.exe
1741
fi