git-completion.bash 32.5 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 389
		check-ref-format) : plumbing;;
		commit-tree)      : plumbing;;
390 391
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
392 393
		cvsserver)        : daemon;;
		daemon)           : daemon;;
394 395 396
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
397
		fast-import)      : import;;
398
		fsck-objects)     : plumbing;;
399
		fetch-pack)       : plumbing;;
400
		fmt-merge-msg)    : plumbing;;
401
		for-each-ref)     : plumbing;;
402 403 404
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
405
		init-db)          : deprecated;;
406 407 408 409 410 411 412 413 414 415 416 417
		local-fetch)      : plumbing;;
		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;;
418 419 420
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
421 422
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
423
		reflog)           : plumbing;;
424
		repo-config)      : deprecated;;
425 426 427 428 429 430 431 432 433 434 435
		rerere)           : plumbing;;
		rev-list)         : plumbing;;
		rev-parse)        : plumbing;;
		runstatus)        : plumbing;;
		sh-setup)         : internal;;
		shell)            : daemon;;
		send-pack)        : plumbing;;
		show-index)       : plumbing;;
		ssh-*)            : transport;;
		stripspace)       : plumbing;;
		symbolic-ref)     : plumbing;;
436
		tar-tree)         : deprecated;;
437 438
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
439
		update-index)     : plumbing;;
440 441 442 443 444
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
445
		verify-tag)       : plumbing;;
446 447 448 449
		*) echo $i;;
		esac
	done
}
450 451
__git_porcelain_commandlist=
__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
452

453 454
__git_aliases ()
{
455
	local i IFS=$'\n'
456
	for i in $(git --git-dir="$(__gitdir)" config --list); do
457 458 459 460 461 462 463
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/=*/}"
			;;
		esac
	done
464 465 466 467
}

__git_aliased_command ()
{
468
	local word cmdline=$(git --git-dir="$(__gitdir)" \
469
		config --get "alias.$1")
470 471 472 473 474 475 476 477
	for word in $cmdline; do
		if [ "${word##-*}" ]; then
			echo $word
			return
		fi
	done
}

478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
__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
}

494 495 496 497 498 499 500 501 502 503 504 505
__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
}

506
__git_whitespacelist="nowarn warn error error-all fix"
507 508 509

_git_am ()
{
510
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
511
	if [ -d "$dir"/rebase-apply ]; then
512
		__gitcomp "--skip --resolved --abort"
513 514 515 516
		return
	fi
	case "$cur" in
	--whitespace=*)
517
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
518 519 520
		return
		;;
	--*)
521
		__gitcomp "
522 523
			--signoff --utf8 --binary --3way --interactive
			--whitespace=
524
			"
525 526 527 528 529 530 531 532 533 534
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--whitespace=*)
535
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
536 537 538
		return
		;;
	--*)
539
		__gitcomp "
540 541 542 543
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
			--whitespace= --inaccurate-eof --verbose
544
			"
545 546 547 548 549
		return
	esac
	COMPREPLY=()
}

550 551
_git_add ()
{
552 553
	__git_has_doubledash && return

554 555 556
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
557 558 559 560
		__gitcomp "
			--interactive --refresh --patch --update --dry-run
			--ignore-errors
			"
561 562 563 564 565
		return
	esac
	COMPREPLY=()
}

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
_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
}

589 590
_git_bisect ()
{
591 592
	__git_has_doubledash && return

593
	local subcommands="start bad good skip reset visualize replay log run"
594 595 596
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
597 598 599
		return
	fi

600
	case "$subcommand" in
601
	bad|good|reset|skip)
602 603 604 605 606 607 608 609
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

610 611
_git_branch ()
{
612 613 614 615 616 617 618 619 620 621 622
	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 已提交
623 624 625 626 627
	case "${COMP_WORDS[COMP_CWORD]}" in
	--*=*)	COMPREPLY=() ;;
	--*)
		__gitcomp "
			--color --no-color --verbose --abbrev= --no-abbrev
628
			--track --no-track --contains --merged --no-merged
S
SZEDER Gábor 已提交
629 630
			"
		;;
631 632 633 634 635 636 637
	*)
		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
			__gitcomp "$(__git_heads)"
		else
			__gitcomp "$(__git_refs)"
		fi
		;;
S
SZEDER Gábor 已提交
638
	esac
639 640
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
_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
}

670 671
_git_checkout ()
{
672 673
	__git_has_doubledash && return

674
	__gitcomp "$(__git_refs)"
675 676
}

677 678 679 680 681
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

682 683 684 685 686
_git_cherry_pick ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
687
		__gitcomp "--edit --no-commit"
688 689
		;;
	*)
690
		__gitcomp "$(__git_refs)"
691 692 693 694
		;;
	esac
}

695 696 697 698 699 700 701 702 703 704 705 706 707 708
_git_clean ()
{
	__git_has_doubledash && return

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

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
_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=()
}

734 735
_git_commit ()
{
736 737
	__git_has_doubledash && return

738 739 740
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
741
		__gitcomp "
742 743
			--all --author= --signoff --verify --no-verify
			--edit --amend --include --only
744
			"
745 746 747 748 749
		return
	esac
	COMPREPLY=()
}

750 751
_git_describe ()
{
752 753 754 755 756 757 758 759 760
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--all --tags --contains --abbrev= --candidates=
			--exact-match --debug --long --match --always
			"
		return
	esac
761 762 763
	__gitcomp "$(__git_refs)"
}

764 765
_git_diff ()
{
766 767
	__git_has_doubledash && return

768 769 770 771 772 773 774 775 776 777
	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
			--full-index --binary --abbrev --diff-filter
			--find-copies-harder --pickaxe-all --pickaxe-regex
			--text --ignore-space-at-eol --ignore-space-change
			--ignore-all-space --exit-code --quiet --ext-diff
778 779
			--no-ext-diff
			--no-prefix --src-prefix= --dst-prefix=
780
			--base --ours --theirs
781
			"
782 783 784
		return
		;;
	esac
785 786 787 788 789 790 791 792 793
	__git_complete_file
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-fetch*,1)
794
		__gitcomp "$(__git_remotes)"
795 796
		;;
	git,2)
797
		__gitcomp "$(__git_remotes)"
798 799 800 801
		;;
	*)
		case "$cur" in
		*:*)
802 803 804 805 806 807
			local pfx=""
			case "$COMP_WORDBREAKS" in
			*:*) : great ;;
			*)   pfx="${cur%%:*}:" ;;
			esac
			__gitcomp "$(__git_refs)" "$pfx" "${cur#*:}"
808 809 810 811 812 813 814
			;;
		*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-fetch) remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
815
			__gitcomp "$(__git_refs2 "$remote")"
816 817 818 819 820 821
			;;
		esac
		;;
	esac
}

822 823 824 825 826
_git_format_patch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
827
		__gitcomp "
828 829 830
			--stdout --attach --thread
			--output-directory
			--numbered --start-number
831
			--numbered-files
832 833 834 835
			--keep-subject
			--signoff
			--in-reply-to=
			--full-index --binary
836
			--not --all
837
			--cover-letter
838
			--no-prefix --src-prefix= --dst-prefix=
839
			"
840 841 842 843 844 845
		return
		;;
	esac
	__git_complete_revlist
}

846 847 848 849 850
_git_gc ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
851
		__gitcomp "--prune --aggressive"
852 853 854 855 856 857
		return
		;;
	esac
	COMPREPLY=()
}

858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
_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=()
}

881 882 883 884 885 886 887 888 889
_git_help ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--all --info --man --web"
		return
		;;
	esac
890 891 892 893 894
	__gitcomp "$(__git_all_commands)
		attributes cli core-tutorial cvs-migration
		diffcore gitk glossary hooks ignore modules
		repository-layout tutorial tutorial-2
		"
895 896
}

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
_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=()
}

915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
_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=()
}

935 936
_git_ls_remote ()
{
937
	__gitcomp "$(__git_remotes)"
938 939 940 941 942 943 944 945 946
}

_git_ls_tree ()
{
	__git_complete_file
}

_git_log ()
{
947 948
	__git_has_doubledash && return

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

989 990 991
_git_merge ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
992 993
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
994
		__gitcomp "$(__git_merge_strategies)"
995 996
		return
	esac
997
	case "$cur" in
998
	--strategy=*)
999
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1000 1001
		return
		;;
1002
	--*)
1003
		__gitcomp "
1004
			--no-commit --no-stat --log --no-log --squash --strategy
1005
			"
1006 1007
		return
	esac
1008
	__gitcomp "$(__git_refs)"
1009 1010
}

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
_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=()
}

1030 1031
_git_merge_base ()
{
1032
	__gitcomp "$(__git_refs)"
1033 1034
}

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
_git_mv ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--dry-run"
		return
		;;
	esac
	COMPREPLY=()
}

1047 1048
_git_name_rev ()
{
1049
	__gitcomp "--tags --all --stdin"
1050 1051
}

1052 1053 1054 1055 1056 1057
_git_pull ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-pull*,1)
1058
		__gitcomp "$(__git_remotes)"
1059 1060
		;;
	git,2)
1061
		__gitcomp "$(__git_remotes)"
1062 1063 1064 1065 1066 1067 1068
		;;
	*)
		local remote
		case "${COMP_WORDS[0]}" in
		git-pull)  remote="${COMP_WORDS[1]}" ;;
		git)       remote="${COMP_WORDS[2]}" ;;
		esac
1069
		__gitcomp "$(__git_refs "$remote")"
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		;;
	esac
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-push*,1)
1080
		__gitcomp "$(__git_remotes)"
1081 1082
		;;
	git,2)
1083
		__gitcomp "$(__git_remotes)"
1084 1085 1086 1087 1088 1089 1090 1091 1092
		;;
	*)
		case "$cur" in
		*:*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-push)  remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
1093 1094 1095 1096 1097 1098 1099 1100

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

			__gitcomp "$(__git_refs "$remote")" "$pfx" "${cur#*:}"
1101
			;;
1102 1103 1104
		+*)
			__gitcomp "$(__git_refs)" + "${cur#+}"
			;;
1105
		*)
1106
			__gitcomp "$(__git_refs)"
1107 1108 1109 1110 1111 1112
			;;
		esac
		;;
	esac
}

1113 1114
_git_rebase ()
{
1115
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1116
	if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1117
		__gitcomp "--continue --skip --abort"
1118 1119
		return
	fi
1120 1121
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
1122
		__gitcomp "$(__git_merge_strategies)"
1123 1124
		return
	esac
1125
	case "$cur" in
1126
	--strategy=*)
1127
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
1128 1129
		return
		;;
1130
	--*)
1131
		__gitcomp "--onto --merge --strategy --interactive"
1132 1133
		return
	esac
1134
	__gitcomp "$(__git_refs)"
1135 1136
}

1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
_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=()
}

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

1326 1327
_git_remote ()
{
1328 1329 1330
	local subcommands="add rm show prune update"
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1331
		__gitcomp "$subcommands"
1332 1333 1334
		return
	fi

1335
	case "$subcommand" in
1336
	rm|show|prune)
1337 1338
		__gitcomp "$(__git_remotes)"
		;;
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	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"
		;;
1351 1352 1353 1354 1355 1356
	*)
		COMPREPLY=()
		;;
	esac
}

1357 1358
_git_reset ()
{
1359 1360
	__git_has_doubledash && return

1361
	local cur="${COMP_WORDS[COMP_CWORD]}"
1362 1363 1364 1365 1366 1367 1368
	case "$cur" in
	--*)
		__gitcomp "--mixed --hard --soft"
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
1369 1370
}

1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
_git_revert ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
		return
		;;
	esac
	COMPREPLY=()
}

1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
_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=()
}

1397 1398
_git_shortlog ()
{
1399 1400
	__git_has_doubledash && return

1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	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
}

1419 1420 1421 1422 1423
_git_show ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
1424
		__gitcomp "
1425
			oneline short medium full fuller email raw
1426
			" "" "${cur##--pretty=}"
1427 1428 1429
		return
		;;
	--*)
1430
		__gitcomp "--pretty="
1431 1432 1433 1434 1435 1436
		return
		;;
	esac
	__git_complete_file
}

1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
_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 已提交
1453 1454
_git_stash ()
{
1455
	local subcommands='save list show apply clear drop pop create branch'
1456 1457
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1458
		__gitcomp "$subcommands"
1459 1460 1461 1462 1463 1464
	else
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$subcommand,$cur" in
		save,--*)
			__gitcomp "--keep-index"
			;;
1465 1466 1467
		apply,--*)
			__gitcomp "--index"
			;;
1468
		show,--*|drop,--*|pop,--*|branch,--*)
1469 1470 1471 1472 1473 1474
			COMPREPLY=()
			;;
		show,*|apply,*|drop,*|pop,*|branch,*)
			__gitcomp "$(git --git-dir="$(__gitdir)" stash list \
					| sed -n -e 's/:.*//p')"
			;;
1475 1476 1477 1478
		*)
			COMPREPLY=()
			;;
		esac
1479
	fi
J
Junio C Hamano 已提交
1480 1481
}

1482 1483
_git_submodule ()
{
1484 1485
	__git_has_doubledash && return

1486 1487
	local subcommands="add status init update"
	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1488 1489 1490 1491 1492 1493
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$cur" in
		--*)
			__gitcomp "--quiet --cached"
			;;
		*)
1494
			__gitcomp "$subcommands"
1495 1496 1497 1498 1499 1500
			;;
		esac
		return
	fi
}

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 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
_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
}

1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
_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
}

1613 1614
_git ()
{
1615 1616 1617 1618 1619 1620 1621
	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="." ;;
1622 1623
		--version|-p|--paginate) ;;
		--help) command="help"; break ;;
1624 1625 1626 1627 1628
		*) command="$i"; break ;;
		esac
		c=$((++c))
	done

1629
	if [ -z "$command" ]; then
1630 1631
		case "${COMP_WORDS[COMP_CWORD]}" in
		--*=*) COMPREPLY=() ;;
1632
		--*)   __gitcomp "
1633
			--paginate
1634 1635 1636 1637 1638
			--no-pager
			--git-dir=
			--bare
			--version
			--exec-path
1639 1640
			--work-tree=
			--help
1641 1642
			"
			;;
1643
		*)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
1644 1645
		esac
		return
1646
	fi
1647

1648 1649
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
1650

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

_gitk ()
{
1705 1706
	__git_has_doubledash && return

1707
	local cur="${COMP_WORDS[COMP_CWORD]}"
1708 1709 1710 1711 1712
	local g="$(git rev-parse --git-dir 2>/dev/null)"
	local merge=""
	if [ -f $g/MERGE_HEAD ]; then
		merge="--merge"
	fi
1713 1714
	case "$cur" in
	--*)
1715
		__gitcomp "--not --all $merge"
1716 1717 1718
		return
		;;
	esac
1719
	__git_complete_revlist
1720 1721 1722
}

complete -o default -o nospace -F _git git
1723
complete -o default -o nospace -F _gitk gitk
1724 1725 1726 1727 1728

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