git-completion.bash 29.0 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
__gitdir ()
{
50 51 52 53 54 55 56 57 58 59 60 61 62
	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
63 64
}

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

109
		if [ -n "$1" ]; then
110
			printf "$1" "${b##refs/heads/}$r"
111
		else
112
			printf " (%s)" "${b##refs/heads/}$r"
113 114 115 116
		fi
	fi
}

117 118 119
__gitcomp ()
{
	local all c s=$'\n' IFS=' '$'\t'$'\n'
120
	local cur="${COMP_WORDS[COMP_CWORD]}"
121
	if [ $# -gt 2 ]; then
122 123
		cur="$3"
	fi
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
	case "$cur" in
	--*=)
		COMPREPLY=()
		return
		;;
	*)
		for c in $1; do
			case "$c$4" in
			--*=*) all="$all$c$4$s" ;;
			*.)    all="$all$c$4$s" ;;
			*)     all="$all$c$4 $s" ;;
			esac
		done
		;;
	esac
139
	IFS=$s
140
	COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
141 142 143
	return
}

144 145
__git_heads ()
{
146
	local cmd i is_hash=y dir="$(__gitdir "$1")"
147 148 149 150 151 152 153 154
	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
155
	for i in $(git ls-remote "$1" 2>/dev/null); do
156 157 158 159 160 161 162 163 164
		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
}

165 166 167 168 169 170 171 172 173 174 175
__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
176
	for i in $(git ls-remote "$1" 2>/dev/null); do
177 178 179 180 181 182 183 184 185
		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
}

186 187
__git_refs ()
{
188
	local cmd i is_hash=y dir="$(__gitdir "$1")"
189
	if [ -d "$dir" ]; then
190 191 192 193 194 195 196 197 198 199 200 201
		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
202
	fi
203
	for i in $(git ls-remote "$dir" 2>/dev/null); do
204 205 206 207 208
		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/}" ;;
209
		n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
210 211 212 213 214 215 216
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

__git_refs2 ()
{
217 218 219
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
220 221 222
	done
}

223 224 225
__git_refs_remotes ()
{
	local cmd i is_hash=y
226
	for i in $(git ls-remote "$1" 2>/dev/null); do
227 228 229 230 231 232 233 234 235 236 237 238 239
		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
}

240 241
__git_remotes ()
{
242
	local i ngoff IFS=$'\n' d="$(__gitdir)"
243
	shopt -q nullglob || ngoff=1
244
	shopt -s nullglob
245 246
	for i in "$d/remotes"/*; do
		echo ${i#$d/remotes/}
247
	done
248
	[ "$ngoff" ] && shopt -u nullglob
249
	for i in $(git --git-dir="$d" config --list); do
250 251 252 253 254 255 256
		case "$i" in
		remote.*.url=*)
			i="${i#remote.}"
			echo "${i/.url=*/}"
			;;
		esac
	done
257 258
}

259 260
__git_merge_strategies ()
{
261 262 263 264
	if [ -n "$__git_merge_strategylist" ]; then
		echo "$__git_merge_strategylist"
		return
	fi
265 266 267 268 269 270 271
	sed -n "/^all_strategies='/{
		s/^all_strategies='//
		s/'//
		p
		q
		}" "$(git --exec-path)/git-merge"
}
272 273
__git_merge_strategylist=
__git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
274

275 276
__git_complete_file ()
{
277
	local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
278 279
	case "$cur" in
	?*:*)
280 281
		ref="${cur%%:*}"
		cur="${cur#*:}"
282 283
		case "$cur" in
		?*/*)
284 285
			pfx="${cur%/*}"
			cur="${cur##*/}"
286 287 288 289 290 291 292 293
			ls="$ref:$pfx"
			pfx="$pfx/"
			;;
		*)
			ls="$ref"
			;;
	    esac
		COMPREPLY=($(compgen -P "$pfx" \
294
			-W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
295 296 297 298 299 300 301 302 303
				| sed '/^100... blob /s,^.*	,,
				       /^040000 tree /{
				           s,^.*	,,
				           s,$,/,
				       }
				       s/^.*	//')" \
			-- "$cur"))
		;;
	*)
304
		__gitcomp "$(__git_refs)"
305 306 307 308
		;;
	esac
}

309 310 311 312 313 314 315
__git_complete_revlist ()
{
	local pfx cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	*...*)
		pfx="${cur%...*}..."
		cur="${cur#*...}"
316
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
317 318 319 320
		;;
	*..*)
		pfx="${cur%..*}.."
		cur="${cur#*..}"
321 322 323 324
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
		;;
	*.)
		__gitcomp "$cur."
325 326
		;;
	*)
327
		__gitcomp "$(__git_refs)"
328 329 330 331
		;;
	esac
}

332 333
__git_commands ()
{
334 335 336 337
	if [ -n "$__git_commandlist" ]; then
		echo "$__git_commandlist"
		return
	fi
338 339 340 341
	local i IFS=" "$'\n'
	for i in $(git help -a|egrep '^ ')
	do
		case $i in
342
		*--*)             : helper pattern;;
343 344 345
		applymbox)        : ask gittus;;
		applypatch)       : ask gittus;;
		archimport)       : import;;
346
		cat-file)         : plumbing;;
347
		check-attr)       : plumbing;;
348 349
		check-ref-format) : plumbing;;
		commit-tree)      : plumbing;;
350 351
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
352 353
		cvsserver)        : daemon;;
		daemon)           : daemon;;
354 355 356
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
357
		fast-import)      : import;;
358
		fsck-objects)     : plumbing;;
359
		fetch-pack)       : plumbing;;
360
		fmt-merge-msg)    : plumbing;;
361
		for-each-ref)     : plumbing;;
362 363 364
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
365
		init-db)          : deprecated;;
366 367 368 369 370 371 372 373 374 375 376 377
		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;;
378 379 380
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
381 382
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
383
		reflog)           : plumbing;;
384
		repo-config)      : deprecated;;
385 386 387 388 389 390 391 392 393 394 395
		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;;
396
		tar-tree)         : deprecated;;
397 398
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
399
		update-index)     : plumbing;;
400 401 402 403 404
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
405
		verify-tag)       : plumbing;;
406 407 408 409
		*) echo $i;;
		esac
	done
}
410 411
__git_commandlist=
__git_commandlist="$(__git_commands 2>/dev/null)"
412

413 414
__git_aliases ()
{
415
	local i IFS=$'\n'
416
	for i in $(git --git-dir="$(__gitdir)" config --list); do
417 418 419 420 421 422 423
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/=*/}"
			;;
		esac
	done
424 425 426 427
}

__git_aliased_command ()
{
428
	local word cmdline=$(git --git-dir="$(__gitdir)" \
429
		config --get "alias.$1")
430 431 432 433 434 435 436 437
	for word in $cmdline; do
		if [ "${word##-*}" ]; then
			echo $word
			return
		fi
	done
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
__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
}

454 455 456 457 458 459
__git_whitespacelist="nowarn warn error error-all strip"

_git_am ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	if [ -d .dotest ]; then
460
		__gitcomp "--skip --resolved"
461 462 463 464
		return
	fi
	case "$cur" in
	--whitespace=*)
465
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
466 467 468
		return
		;;
	--*)
469
		__gitcomp "
470 471
			--signoff --utf8 --binary --3way --interactive
			--whitespace=
472
			"
473 474 475 476 477 478 479 480 481 482
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--whitespace=*)
483
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
484 485 486
		return
		;;
	--*)
487
		__gitcomp "
488 489 490 491
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
			--whitespace= --inaccurate-eof --verbose
492
			"
493 494 495 496 497
		return
	esac
	COMPREPLY=()
}

498 499 500 501 502
_git_add ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
503
		__gitcomp "--interactive --refresh"
504 505 506 507 508
		return
	esac
	COMPREPLY=()
}

509 510
_git_bisect ()
{
511 512 513 514
	local subcommands="start bad good reset visualize replay log"
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
515 516 517
		return
	fi

518
	case "$subcommand" in
519 520 521 522 523 524 525 526 527
	bad|good|reset)
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

528 529
_git_branch ()
{
530 531 532 533 534 535 536 537 538 539 540
	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 已提交
541 542 543 544 545 546 547 548
	case "${COMP_WORDS[COMP_CWORD]}" in
	--*=*)	COMPREPLY=() ;;
	--*)
		__gitcomp "
			--color --no-color --verbose --abbrev= --no-abbrev
			--track --no-track
			"
		;;
549 550 551 552 553 554 555
	*)
		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
			__gitcomp "$(__git_heads)"
		else
			__gitcomp "$(__git_refs)"
		fi
		;;
S
SZEDER Gábor 已提交
556
	esac
557 558
}

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

588 589
_git_checkout ()
{
590
	__gitcomp "$(__git_refs)"
591 592
}

593 594 595 596 597
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

598 599 600 601 602
_git_cherry_pick ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
603
		__gitcomp "--edit --no-commit"
604 605
		;;
	*)
606
		__gitcomp "$(__git_refs)"
607 608 609 610
		;;
	esac
}

611 612 613 614 615
_git_commit ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
616
		__gitcomp "
617 618
			--all --author= --signoff --verify --no-verify
			--edit --amend --include --only
619
			"
620 621 622 623 624
		return
	esac
	COMPREPLY=()
}

625 626 627 628 629
_git_describe ()
{
	__gitcomp "$(__git_refs)"
}

630 631
_git_diff ()
{
632 633 634 635 636 637 638 639 640 641
	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
642 643
			--no-ext-diff
			--no-prefix --src-prefix= --dst-prefix=
644
			--base --ours --theirs
645
			"
646 647 648
		return
		;;
	esac
649 650 651 652 653
	__git_complete_file
}

_git_diff_tree ()
{
654
	__gitcomp "$(__git_refs)"
655 656 657 658 659 660 661 662
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-fetch*,1)
663
		__gitcomp "$(__git_remotes)"
664 665
		;;
	git,2)
666
		__gitcomp "$(__git_remotes)"
667 668 669 670
		;;
	*)
		case "$cur" in
		*:*)
671
			__gitcomp "$(__git_refs)" "" "${cur#*:}"
672 673 674 675 676 677 678
			;;
		*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-fetch) remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
679
			__gitcomp "$(__git_refs2 "$remote")"
680 681 682 683 684 685
			;;
		esac
		;;
	esac
}

686 687 688 689 690
_git_format_patch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
691
		__gitcomp "
692 693 694
			--stdout --attach --thread
			--output-directory
			--numbered --start-number
695
			--numbered-files
696 697 698 699
			--keep-subject
			--signoff
			--in-reply-to=
			--full-index --binary
700
			--not --all
701
			--cover-letter
702
			--no-prefix --src-prefix= --dst-prefix=
703
			"
704 705 706 707 708 709
		return
		;;
	esac
	__git_complete_revlist
}

710 711 712 713 714
_git_gc ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
715
		__gitcomp "--prune --aggressive"
716 717 718 719 720 721
		return
		;;
	esac
	COMPREPLY=()
}

722 723
_git_ls_remote ()
{
724
	__gitcomp "$(__git_remotes)"
725 726 727 728 729 730 731 732 733
}

_git_ls_tree ()
{
	__git_complete_file
}

_git_log ()
{
734 735 736
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
737
		__gitcomp "
738
			oneline short medium full fuller email raw
739
			" "" "${cur##--pretty=}"
740 741
		return
		;;
742 743 744 745 746 747
	--date=*)
		__gitcomp "
			relative iso8601 rfc2822 short local default
		" "" "${cur##--date=}"
		return
		;;
748
	--*)
749
		__gitcomp "
750 751
			--max-count= --max-age= --since= --after=
			--min-age= --before= --until=
752
			--root --topo-order --date-order --reverse
753
			--no-merges --follow
754
			--abbrev-commit --abbrev=
755
			--relative-date --date=
756 757
			--author= --committer= --grep=
			--all-match
758
			--pretty= --name-status --name-only --raw
759
			--not --all
760
			--left-right --cherry-pick
761
			"
762 763 764
		return
		;;
	esac
765
	__git_complete_revlist
766 767
}

768 769 770
_git_merge ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
771 772
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
773
		__gitcomp "$(__git_merge_strategies)"
774 775
		return
	esac
776
	case "$cur" in
777
	--strategy=*)
778
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
779 780
		return
		;;
781
	--*)
782
		__gitcomp "
783
			--no-commit --no-summary --squash --strategy
784
			"
785 786
		return
	esac
787
	__gitcomp "$(__git_refs)"
788 789
}

790 791
_git_merge_base ()
{
792
	__gitcomp "$(__git_refs)"
793 794
}

795 796
_git_name_rev ()
{
797
	__gitcomp "--tags --all --stdin"
798 799
}

800 801 802 803 804 805
_git_pull ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-pull*,1)
806
		__gitcomp "$(__git_remotes)"
807 808
		;;
	git,2)
809
		__gitcomp "$(__git_remotes)"
810 811 812 813 814 815 816
		;;
	*)
		local remote
		case "${COMP_WORDS[0]}" in
		git-pull)  remote="${COMP_WORDS[1]}" ;;
		git)       remote="${COMP_WORDS[2]}" ;;
		esac
817
		__gitcomp "$(__git_refs "$remote")"
818 819 820 821 822 823 824 825 826 827
		;;
	esac
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-push*,1)
828
		__gitcomp "$(__git_remotes)"
829 830
		;;
	git,2)
831
		__gitcomp "$(__git_remotes)"
832 833 834 835 836 837 838 839 840
		;;
	*)
		case "$cur" in
		*:*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-push)  remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
841
			__gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
842
			;;
843 844 845
		+*)
			__gitcomp "$(__git_refs)" + "${cur#+}"
			;;
846
		*)
847
			__gitcomp "$(__git_refs)"
848 849 850 851 852 853
			;;
		esac
		;;
	esac
}

854 855
_git_rebase ()
{
856 857
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
	if [ -d .dotest ] || [ -d "$dir"/.dotest-merge ]; then
858
		__gitcomp "--continue --skip --abort"
859 860
		return
	fi
861 862
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
863
		__gitcomp "$(__git_merge_strategies)"
864 865
		return
	esac
866
	case "$cur" in
867
	--strategy=*)
868
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
869 870
		return
		;;
871
	--*)
872
		__gitcomp "--onto --merge --strategy --interactive"
873 874
		return
	esac
875
	__gitcomp "$(__git_refs)"
876 877
}

878
_git_config ()
879 880 881 882 883
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prv="${COMP_WORDS[COMP_CWORD-1]}"
	case "$prv" in
	branch.*.remote)
884
		__gitcomp "$(__git_remotes)"
885 886 887
		return
		;;
	branch.*.merge)
888
		__gitcomp "$(__git_refs)"
889 890 891 892 893
		return
		;;
	remote.*.fetch)
		local remote="${prv#remote.}"
		remote="${remote%.fetch}"
894
		__gitcomp "$(__git_refs_remotes "$remote")"
895 896 897 898 899
		return
		;;
	remote.*.push)
		local remote="${prv#remote.}"
		remote="${remote%.push}"
900
		__gitcomp "$(git --git-dir="$(__gitdir)" \
901
			for-each-ref --format='%(refname):%(refname)' \
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
			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
			"
918 919 920 921 922 923 924 925 926
		return
		;;
	*.*)
		COMPREPLY=()
		return
		;;
	esac
	case "$cur" in
	--*)
927
		__gitcomp "
928
			--global --system --file=
929
			--list --replace-all
930
			--get --get-all --get-regexp
931
			--add --unset --unset-all
932
			--remove-section --rename-section
933
			"
934 935 936 937 938
		return
		;;
	branch.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
939
		__gitcomp "remote merge" "$pfx" "$cur"
940 941 942 943 944
		return
		;;
	branch.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
945
		__gitcomp "$(__git_heads)" "$pfx" "$cur" "."
946 947 948 949 950
		return
		;;
	remote.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
951 952 953 954
		__gitcomp "
			url fetch push skipDefaultUpdate
			receivepack uploadpack tagopt
			" "$pfx" "$cur"
955 956 957 958 959
		return
		;;
	remote.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
960
		__gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
961 962 963
		return
		;;
	esac
964
	__gitcomp "
965 966 967 968 969 970
		apply.whitespace
		core.fileMode
		core.gitProxy
		core.ignoreStat
		core.preferSymlinkRefs
		core.logAllRefUpdates
971
		core.loosecompression
972 973 974 975
		core.repositoryFormatVersion
		core.sharedRepository
		core.warnAmbiguousRefs
		core.compression
976 977
		core.packedGitWindowSize
		core.packedGitLimit
978
		clean.requireForce
979 980 981 982 983
		color.branch
		color.branch.current
		color.branch.local
		color.branch.remote
		color.branch.plain
984
		color.diff
985 986 987 988 989 990 991
		color.diff.plain
		color.diff.meta
		color.diff.frag
		color.diff.old
		color.diff.new
		color.diff.commit
		color.diff.whitespace
992 993
		color.pager
		color.status
994 995 996 997 998 999 1000 1001
		color.status.header
		color.status.added
		color.status.changed
		color.status.untracked
		diff.renameLimit
		diff.renames
		fetch.unpackLimit
		format.headers
1002
		format.subjectprefix
1003 1004
		gitcvs.enabled
		gitcvs.logfile
1005
		gitcvs.allbinary
1006 1007
		gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dbpass
		gitcvs.dbtablenameprefix
1008
		gc.packrefs
1009 1010 1011 1012
		gc.reflogexpire
		gc.reflogexpireunreachable
		gc.rerereresolved
		gc.rerereunresolved
1013 1014 1015 1016 1017 1018
		http.sslVerify
		http.sslCert
		http.sslKey
		http.sslCAInfo
		http.sslCAPath
		http.maxRequests
1019 1020
		http.lowSpeedLimit
		http.lowSpeedTime
1021
		http.noEPSV
1022 1023 1024
		i18n.commitEncoding
		i18n.logOutputEncoding
		log.showroot
1025
		merge.tool
1026 1027
		merge.summary
		merge.verbosity
1028
		pack.window
1029
		pack.depth
1030 1031 1032 1033
		pack.windowMemory
		pack.compression
		pack.deltaCacheSize
		pack.deltaCacheLimit
1034 1035
		pull.octopus
		pull.twohead
1036
		repack.useDeltaBaseOffset
1037 1038 1039 1040
		show.difftree
		showbranch.default
		tar.umask
		transfer.unpackLimit
1041 1042
		receive.unpackLimit
		receive.denyNonFastForwards
1043 1044 1045 1046
		user.name
		user.email
		user.signingkey
		whatchanged.difftree
1047
		branch. remote.
1048
	"
1049 1050
}

1051 1052
_git_remote ()
{
1053 1054 1055
	local subcommands="add rm show prune update"
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1056
		__gitcomp "$subcommands"
1057 1058 1059
		return
	fi

1060
	case "$subcommand" in
1061
	rm|show|prune)
1062 1063
		__gitcomp "$(__git_remotes)"
		;;
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	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"
		;;
1076 1077 1078 1079 1080 1081
	*)
		COMPREPLY=()
		;;
	esac
}

1082 1083 1084
_git_reset ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
1085 1086 1087 1088 1089 1090 1091
	case "$cur" in
	--*)
		__gitcomp "--mixed --hard --soft"
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
1092 1093
}

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
_git_shortlog ()
{
	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
}

1114 1115 1116 1117 1118
_git_show ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
1119
		__gitcomp "
1120
			oneline short medium full fuller email raw
1121
			" "" "${cur##--pretty=}"
1122 1123 1124
		return
		;;
	--*)
1125
		__gitcomp "--pretty="
1126 1127 1128 1129 1130 1131
		return
		;;
	esac
	__git_complete_file
}

J
Junio C Hamano 已提交
1132 1133
_git_stash ()
{
1134
	local subcommands='save list show apply clear drop pop create'
1135 1136 1137
	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
		__gitcomp "$subcommands"
	fi
J
Junio C Hamano 已提交
1138 1139
}

1140 1141
_git_submodule ()
{
1142 1143
	local subcommands="add status init update"
	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1144 1145 1146 1147 1148 1149
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$cur" in
		--*)
			__gitcomp "--quiet --cached"
			;;
		*)
1150
			__gitcomp "$subcommands"
1151 1152 1153 1154 1155 1156
			;;
		esac
		return
	fi
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
_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
}

1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268
_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
}

1269 1270
_git ()
{
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283
	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="." ;;
		--version|--help|-p|--paginate) ;;
		*) command="$i"; break ;;
		esac
		c=$((++c))
	done

1284
	if [ -z "$command" ]; then
1285 1286
		case "${COMP_WORDS[COMP_CWORD]}" in
		--*=*) COMPREPLY=() ;;
1287
		--*)   __gitcomp "
1288
			--paginate
1289 1290 1291 1292 1293
			--no-pager
			--git-dir=
			--bare
			--version
			--exec-path
1294 1295
			--work-tree=
			--help
1296 1297
			"
			;;
1298 1299 1300
		*)     __gitcomp "$(__git_commands) $(__git_aliases)" ;;
		esac
		return
1301
	fi
1302

1303 1304
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
1305

1306
	case "$command" in
1307
	am)          _git_am ;;
1308
	add)         _git_add ;;
1309
	apply)       _git_apply ;;
1310
	bisect)      _git_bisect ;;
1311
	bundle)      _git_bundle ;;
1312 1313
	branch)      _git_branch ;;
	checkout)    _git_checkout ;;
1314
	cherry)      _git_cherry ;;
1315
	cherry-pick) _git_cherry_pick ;;
1316
	commit)      _git_commit ;;
1317
	config)      _git_config ;;
1318
	describe)    _git_describe ;;
1319 1320
	diff)        _git_diff ;;
	fetch)       _git_fetch ;;
1321
	format-patch) _git_format_patch ;;
1322
	gc)          _git_gc ;;
1323 1324 1325
	log)         _git_log ;;
	ls-remote)   _git_ls_remote ;;
	ls-tree)     _git_ls_tree ;;
1326
	merge)       _git_merge;;
1327
	merge-base)  _git_merge_base ;;
1328
	name-rev)    _git_name_rev ;;
1329 1330
	pull)        _git_pull ;;
	push)        _git_push ;;
1331
	rebase)      _git_rebase ;;
1332
	remote)      _git_remote ;;
1333
	reset)       _git_reset ;;
1334
	shortlog)    _git_shortlog ;;
1335
	show)        _git_show ;;
1336
	show-branch) _git_log ;;
J
Junio C Hamano 已提交
1337
	stash)       _git_stash ;;
1338
	submodule)   _git_submodule ;;
1339
	svn)         _git_svn ;;
1340
	tag)         _git_tag ;;
1341 1342 1343
	whatchanged) _git_log ;;
	*)           COMPREPLY=() ;;
	esac
1344 1345 1346 1347 1348
}

_gitk ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
1349 1350 1351 1352 1353 1354
	case "$cur" in
	--*)
		__gitcomp "--not --all"
		return
		;;
	esac
1355
	__git_complete_revlist
1356 1357 1358
}

complete -o default -o nospace -F _git git
1359 1360 1361
complete -o default -o nospace -F _gitk gitk
complete -o default -o nospace -F _git_am git-am
complete -o default -o nospace -F _git_apply git-apply
1362
complete -o default -o nospace -F _git_bisect git-bisect
1363
complete -o default -o nospace -F _git_branch git-branch
1364
complete -o default -o nospace -F _git_bundle git-bundle
1365
complete -o default -o nospace -F _git_checkout git-checkout
1366
complete -o default -o nospace -F _git_cherry git-cherry
1367 1368
complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
complete -o default -o nospace -F _git_commit git-commit
1369
complete -o default -o nospace -F _git_describe git-describe
1370 1371
complete -o default -o nospace -F _git_diff git-diff
complete -o default -o nospace -F _git_fetch git-fetch
1372
complete -o default -o nospace -F _git_format_patch git-format-patch
1373
complete -o default -o nospace -F _git_gc git-gc
1374
complete -o default -o nospace -F _git_log git-log
1375
complete -o default -o nospace -F _git_ls_remote git-ls-remote
1376
complete -o default -o nospace -F _git_ls_tree git-ls-tree
1377 1378 1379
complete -o default -o nospace -F _git_merge git-merge
complete -o default -o nospace -F _git_merge_base git-merge-base
complete -o default -o nospace -F _git_name_rev git-name-rev
1380 1381
complete -o default -o nospace -F _git_pull git-pull
complete -o default -o nospace -F _git_push git-push
1382 1383
complete -o default -o nospace -F _git_rebase git-rebase
complete -o default -o nospace -F _git_config git-config
1384
complete -o default -o nospace -F _git_remote git-remote
1385
complete -o default -o nospace -F _git_reset git-reset
1386
complete -o default -o nospace -F _git_shortlog git-shortlog
1387
complete -o default -o nospace -F _git_show git-show
J
Junio C Hamano 已提交
1388
complete -o default -o nospace -F _git_stash git-stash
1389
complete -o default -o nospace -F _git_submodule git-submodule
1390
complete -o default -o nospace -F _git_svn git-svn
1391
complete -o default -o nospace -F _git_log git-show-branch
1392
complete -o default -o nospace -F _git_tag git-tag
1393 1394 1395 1396 1397 1398
complete -o default -o nospace -F _git_log git-whatchanged

# 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.
#
1399
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1400 1401
complete -o default -o nospace -F _git_add git-add.exe
complete -o default -o nospace -F _git_apply git-apply.exe
1402
complete -o default -o nospace -F _git git.exe
1403
complete -o default -o nospace -F _git_branch git-branch.exe
1404
complete -o default -o nospace -F _git_bundle git-bundle.exe
1405
complete -o default -o nospace -F _git_cherry git-cherry.exe
1406
complete -o default -o nospace -F _git_describe git-describe.exe
1407
complete -o default -o nospace -F _git_diff git-diff.exe
1408
complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1409 1410
complete -o default -o nospace -F _git_log git-log.exe
complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1411 1412
complete -o default -o nospace -F _git_merge_base git-merge-base.exe
complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1413
complete -o default -o nospace -F _git_push git-push.exe
1414
complete -o default -o nospace -F _git_config git-config
1415
complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1416
complete -o default -o nospace -F _git_show git-show.exe
1417
complete -o default -o nospace -F _git_log git-show-branch.exe
1418
complete -o default -o nospace -F _git_tag git-tag.exe
1419
complete -o default -o nospace -F _git_log git-whatchanged.exe
1420
fi