git-completion.bash 23.2 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 67 68 69 70 71 72 73 74 75 76
__git_ps1 ()
{
	local b="$(git symbolic-ref HEAD 2>/dev/null)"
	if [ -n "$b" ]; then
		if [ -n "$1" ]; then
			printf "$1" "${b##refs/heads/}"
		else
			printf " (%s)" "${b##refs/heads/}"
		fi
	fi
}

77 78 79
__gitcomp ()
{
	local all c s=$'\n' IFS=' '$'\t'$'\n'
80
	local cur="${COMP_WORDS[COMP_CWORD]}"
81
	if [ $# -gt 2 ]; then
82 83
		cur="$3"
	fi
84
	for c in $1; do
85 86 87 88
		case "$c$4" in
		--*=*) all="$all$c$4$s" ;;
		*.)    all="$all$c$4$s" ;;
		*)     all="$all$c$4 $s" ;;
89 90 91
		esac
	done
	IFS=$s
92
	COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
93 94 95
	return
}

96 97
__git_heads ()
{
98
	local cmd i is_hash=y dir="$(__gitdir "$1")"
99 100 101 102 103 104 105 106
	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
107
	for i in $(git-ls-remote "$1" 2>/dev/null); do
108 109 110 111 112 113 114 115 116
		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
}

117 118
__git_refs ()
{
119
	local cmd i is_hash=y dir="$(__gitdir "$1")"
120
	if [ -d "$dir" ]; then
121 122 123 124 125 126 127 128 129 130 131 132
		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
133
	fi
134
	for i in $(git-ls-remote "$dir" 2>/dev/null); do
135 136 137 138 139
		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/}" ;;
140
		n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
141 142 143 144 145 146 147
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

__git_refs2 ()
{
148 149 150
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
151 152 153
	done
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
__git_refs_remotes ()
{
	local cmd i is_hash=y
	for i in $(git-ls-remote "$1" 2>/dev/null); do
		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
}

171 172
__git_remotes ()
{
173
	local i ngoff IFS=$'\n' d="$(__gitdir)"
174
	shopt -q nullglob || ngoff=1
175
	shopt -s nullglob
176 177
	for i in "$d/remotes"/*; do
		echo ${i#$d/remotes/}
178
	done
179
	[ "$ngoff" ] && shopt -u nullglob
180
	for i in $(git --git-dir="$d" config --list); do
181 182 183 184 185 186 187
		case "$i" in
		remote.*.url=*)
			i="${i#remote.}"
			echo "${i/.url=*/}"
			;;
		esac
	done
188 189
}

190 191
__git_merge_strategies ()
{
192 193 194 195
	if [ -n "$__git_merge_strategylist" ]; then
		echo "$__git_merge_strategylist"
		return
	fi
196 197 198 199 200 201 202
	sed -n "/^all_strategies='/{
		s/^all_strategies='//
		s/'//
		p
		q
		}" "$(git --exec-path)/git-merge"
}
203 204
__git_merge_strategylist=
__git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
205

206 207
__git_complete_file ()
{
208
	local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
209 210
	case "$cur" in
	?*:*)
211 212
		ref="${cur%%:*}"
		cur="${cur#*:}"
213 214
		case "$cur" in
		?*/*)
215 216
			pfx="${cur%/*}"
			cur="${cur##*/}"
217 218 219 220 221 222 223 224
			ls="$ref:$pfx"
			pfx="$pfx/"
			;;
		*)
			ls="$ref"
			;;
	    esac
		COMPREPLY=($(compgen -P "$pfx" \
225
			-W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
226 227 228 229 230 231 232 233 234
				| sed '/^100... blob /s,^.*	,,
				       /^040000 tree /{
				           s,^.*	,,
				           s,$,/,
				       }
				       s/^.*	//')" \
			-- "$cur"))
		;;
	*)
235
		__gitcomp "$(__git_refs)"
236 237 238 239
		;;
	esac
}

240 241 242 243 244 245 246
__git_complete_revlist ()
{
	local pfx cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	*...*)
		pfx="${cur%...*}..."
		cur="${cur#*...}"
247
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
248 249 250 251
		;;
	*..*)
		pfx="${cur%..*}.."
		cur="${cur#*..}"
252 253 254 255
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
		;;
	*.)
		__gitcomp "$cur."
256 257
		;;
	*)
258
		__gitcomp "$(__git_refs)"
259 260 261 262
		;;
	esac
}

263 264
__git_commands ()
{
265 266 267 268
	if [ -n "$__git_commandlist" ]; then
		echo "$__git_commandlist"
		return
	fi
269 270 271 272
	local i IFS=" "$'\n'
	for i in $(git help -a|egrep '^ ')
	do
		case $i in
273
		add--interactive) : plumbing;;
274 275 276
		applymbox)        : ask gittus;;
		applypatch)       : ask gittus;;
		archimport)       : import;;
277
		cat-file)         : plumbing;;
278
		check-attr)       : plumbing;;
279 280 281
		check-ref-format) : plumbing;;
		commit-tree)      : plumbing;;
		convert-objects)  : plumbing;;
282 283
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
284 285
		cvsserver)        : daemon;;
		daemon)           : daemon;;
286 287 288
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
289
		fast-import)      : import;;
290
		fsck-objects)     : plumbing;;
291
		fetch--tool)      : plumbing;;
292
		fetch-pack)       : plumbing;;
293
		fmt-merge-msg)    : plumbing;;
294
		for-each-ref)     : plumbing;;
295 296 297
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
298
		init-db)          : deprecated;;
299 300 301 302 303 304 305 306 307 308 309 310
		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;;
311 312 313
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
314 315
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
316
		reflog)           : plumbing;;
317
		repo-config)      : plumbing;;
318 319 320 321 322 323 324 325 326 327
		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;;
328 329
		svn)              : import export;;
		svnimport)        : import;;
330
		symbolic-ref)     : plumbing;;
331
		tar-tree)         : deprecated;;
332 333
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
334
		update-index)     : plumbing;;
335 336 337 338 339
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
340
		verify-tag)       : plumbing;;
341 342 343 344
		*) echo $i;;
		esac
	done
}
345 346
__git_commandlist=
__git_commandlist="$(__git_commands 2>/dev/null)"
347

348 349
__git_aliases ()
{
350
	local i IFS=$'\n'
351
	for i in $(git --git-dir="$(__gitdir)" config --list); do
352 353 354 355 356 357 358
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/=*/}"
			;;
		esac
	done
359 360 361 362
}

__git_aliased_command ()
{
363
	local word cmdline=$(git --git-dir="$(__gitdir)" \
364
		config --get "alias.$1")
365 366 367 368 369 370 371 372
	for word in $cmdline; do
		if [ "${word##-*}" ]; then
			echo $word
			return
		fi
	done
}

373 374 375 376 377 378
__git_whitespacelist="nowarn warn error error-all strip"

_git_am ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	if [ -d .dotest ]; then
379
		__gitcomp "--skip --resolved"
380 381 382 383
		return
	fi
	case "$cur" in
	--whitespace=*)
384
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
385 386 387
		return
		;;
	--*)
388
		__gitcomp "
389 390
			--signoff --utf8 --binary --3way --interactive
			--whitespace=
391
			"
392 393 394 395 396 397 398 399 400 401
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--whitespace=*)
402
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
403 404 405
		return
		;;
	--*)
406
		__gitcomp "
407 408 409 410
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
			--whitespace= --inaccurate-eof --verbose
411
			"
412 413 414 415 416
		return
	esac
	COMPREPLY=()
}

417 418 419 420 421
_git_add ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
422
		__gitcomp "--interactive"
423 424 425 426 427
		return
	esac
	COMPREPLY=()
}

428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
_git_bisect ()
{
	local i c=1 command
	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
		start|bad|good|reset|visualize|replay|log)
			command="$i"
			break
			;;
		esac
		c=$((++c))
	done

	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
		__gitcomp "start bad good reset visualize replay log"
		return
	fi

	case "$command" in
	bad|good|reset)
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

457 458
_git_branch ()
{
459
	__gitcomp "$(__git_refs)"
460 461
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
_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
}

491 492
_git_checkout ()
{
493
	__gitcomp "$(__git_refs)"
494 495
}

496 497 498 499 500
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

501 502 503 504 505
_git_cherry_pick ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
506
		__gitcomp "--edit --no-commit"
507 508
		;;
	*)
509
		__gitcomp "$(__git_refs)"
510 511 512 513
		;;
	esac
}

514 515 516 517 518
_git_commit ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
519
		__gitcomp "
520 521
			--all --author= --signoff --verify --no-verify
			--edit --amend --include --only
522
			"
523 524 525 526 527
		return
	esac
	COMPREPLY=()
}

528 529 530 531 532 533 534
_git_diff ()
{
	__git_complete_file
}

_git_diff_tree ()
{
535
	__gitcomp "$(__git_refs)"
536 537 538 539 540 541 542 543
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-fetch*,1)
544
		__gitcomp "$(__git_remotes)"
545 546
		;;
	git,2)
547
		__gitcomp "$(__git_remotes)"
548 549 550 551
		;;
	*)
		case "$cur" in
		*:*)
552
			__gitcomp "$(__git_refs)" "" "${cur#*:}"
553 554 555 556 557 558 559
			;;
		*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-fetch) remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
560
			__gitcomp "$(__git_refs2 "$remote")"
561 562 563 564 565 566
			;;
		esac
		;;
	esac
}

567 568 569 570 571
_git_format_patch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
572
		__gitcomp "
573 574 575 576 577 578 579
			--stdout --attach --thread
			--output-directory
			--numbered --start-number
			--keep-subject
			--signoff
			--in-reply-to=
			--full-index --binary
580
			--not --all
581
			"
582 583 584 585 586 587
		return
		;;
	esac
	__git_complete_revlist
}

588 589 590 591 592 593 594 595 596 597 598 599
_git_gc ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--prune"
		return
		;;
	esac
	COMPREPLY=()
}

600 601
_git_ls_remote ()
{
602
	__gitcomp "$(__git_remotes)"
603 604 605 606 607 608 609 610 611
}

_git_ls_tree ()
{
	__git_complete_file
}

_git_log ()
{
612 613 614
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
615
		__gitcomp "
616
			oneline short medium full fuller email raw
617
			" "" "${cur##--pretty=}"
618 619 620
		return
		;;
	--*)
621
		__gitcomp "
622 623
			--max-count= --max-age= --since= --after=
			--min-age= --before= --until=
624
			--root --topo-order --date-order --reverse
625 626 627 628 629
			--no-merges
			--abbrev-commit --abbrev=
			--relative-date
			--author= --committer= --grep=
			--all-match
630
			--pretty= --name-status --name-only --raw
631
			--not --all
632
			"
633 634 635
		return
		;;
	esac
636
	__git_complete_revlist
637 638
}

639 640 641
_git_merge ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
642 643
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
644
		__gitcomp "$(__git_merge_strategies)"
645 646
		return
	esac
647
	case "$cur" in
648
	--strategy=*)
649
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
650 651
		return
		;;
652
	--*)
653
		__gitcomp "
654
			--no-commit --no-summary --squash --strategy
655
			"
656 657
		return
	esac
658
	__gitcomp "$(__git_refs)"
659 660
}

661 662
_git_merge_base ()
{
663
	__gitcomp "$(__git_refs)"
664 665
}

666 667
_git_name_rev ()
{
668
	__gitcomp "--tags --all --stdin"
669 670
}

671 672 673 674 675 676
_git_pull ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-pull*,1)
677
		__gitcomp "$(__git_remotes)"
678 679
		;;
	git,2)
680
		__gitcomp "$(__git_remotes)"
681 682 683 684 685 686 687
		;;
	*)
		local remote
		case "${COMP_WORDS[0]}" in
		git-pull)  remote="${COMP_WORDS[1]}" ;;
		git)       remote="${COMP_WORDS[2]}" ;;
		esac
688
		__gitcomp "$(__git_refs "$remote")"
689 690 691 692 693 694 695 696 697 698
		;;
	esac
}

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

	case "${COMP_WORDS[0]},$COMP_CWORD" in
	git-push*,1)
699
		__gitcomp "$(__git_remotes)"
700 701
		;;
	git,2)
702
		__gitcomp "$(__git_remotes)"
703 704 705 706 707 708 709 710 711
		;;
	*)
		case "$cur" in
		*:*)
			local remote
			case "${COMP_WORDS[0]}" in
			git-push)  remote="${COMP_WORDS[1]}" ;;
			git)       remote="${COMP_WORDS[2]}" ;;
			esac
712
			__gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
713
			;;
714 715 716
		+*)
			__gitcomp "$(__git_refs)" + "${cur#+}"
			;;
717
		*)
718
			__gitcomp "$(__git_refs)"
719 720 721 722 723 724
			;;
		esac
		;;
	esac
}

725 726 727
_git_rebase ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
728
	if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
729
		__gitcomp "--continue --skip --abort"
730 731
		return
	fi
732 733
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
734
		__gitcomp "$(__git_merge_strategies)"
735 736
		return
	esac
737
	case "$cur" in
738
	--strategy=*)
739
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
740 741
		return
		;;
742
	--*)
743
		__gitcomp "--onto --merge --strategy"
744 745
		return
	esac
746
	__gitcomp "$(__git_refs)"
747 748
}

749
_git_config ()
750 751 752 753 754
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prv="${COMP_WORDS[COMP_CWORD-1]}"
	case "$prv" in
	branch.*.remote)
755
		__gitcomp "$(__git_remotes)"
756 757 758
		return
		;;
	branch.*.merge)
759
		__gitcomp "$(__git_refs)"
760 761 762 763 764
		return
		;;
	remote.*.fetch)
		local remote="${prv#remote.}"
		remote="${remote%.fetch}"
765
		__gitcomp "$(__git_refs_remotes "$remote")"
766 767 768 769 770
		return
		;;
	remote.*.push)
		local remote="${prv#remote.}"
		remote="${remote%.push}"
771
		__gitcomp "$(git --git-dir="$(__gitdir)" \
772
			for-each-ref --format='%(refname):%(refname)' \
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
			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
			"
789 790 791 792 793 794 795 796 797
		return
		;;
	*.*)
		COMPREPLY=()
		return
		;;
	esac
	case "$cur" in
	--*)
798
		__gitcomp "
799 800
			--global --system
			--list --replace-all
801
			--get --get-all --get-regexp
802
			--add --unset --unset-all
803
			--remove-section --rename-section
804
			"
805 806 807 808 809
		return
		;;
	branch.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
810
		__gitcomp "remote merge" "$pfx" "$cur"
811 812 813 814 815
		return
		;;
	branch.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
816
		__gitcomp "$(__git_heads)" "$pfx" "$cur" "."
817 818 819 820 821
		return
		;;
	remote.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
822 823 824 825
		__gitcomp "
			url fetch push skipDefaultUpdate
			receivepack uploadpack tagopt
			" "$pfx" "$cur"
826 827 828 829 830
		return
		;;
	remote.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
831
		__gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
832 833 834
		return
		;;
	esac
835
	__gitcomp "
836 837 838 839 840 841 842 843 844 845 846
		apply.whitespace
		core.fileMode
		core.gitProxy
		core.ignoreStat
		core.preferSymlinkRefs
		core.logAllRefUpdates
		core.repositoryFormatVersion
		core.sharedRepository
		core.warnAmbiguousRefs
		core.compression
		core.legacyHeaders
847 848
		core.packedGitWindowSize
		core.packedGitLimit
849
		clean.requireForce
850 851 852 853 854
		color.branch
		color.branch.current
		color.branch.local
		color.branch.remote
		color.branch.plain
855
		color.diff
856 857 858 859 860 861 862
		color.diff.plain
		color.diff.meta
		color.diff.frag
		color.diff.old
		color.diff.new
		color.diff.commit
		color.diff.whitespace
863 864
		color.pager
		color.status
865 866 867 868 869 870 871 872 873 874
		color.status.header
		color.status.added
		color.status.changed
		color.status.untracked
		diff.renameLimit
		diff.renames
		fetch.unpackLimit
		format.headers
		gitcvs.enabled
		gitcvs.logfile
875 876 877
		gitcvs.allbinary
		gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dvpass
		gc.packrefs
878 879 880 881
		gc.reflogexpire
		gc.reflogexpireunreachable
		gc.rerereresolved
		gc.rerereunresolved
882 883 884 885 886 887
		http.sslVerify
		http.sslCert
		http.sslKey
		http.sslCAInfo
		http.sslCAPath
		http.maxRequests
888 889
		http.lowSpeedLimit
		http.lowSpeedTime
890
		http.noEPSV
891 892 893
		i18n.commitEncoding
		i18n.logOutputEncoding
		log.showroot
894
		merge.tool
895 896
		merge.summary
		merge.verbosity
897
		pack.window
898
		pack.depth
899 900
		pull.octopus
		pull.twohead
901
		repack.useDeltaBaseOffset
902 903 904 905
		show.difftree
		showbranch.default
		tar.umask
		transfer.unpackLimit
906 907
		receive.unpackLimit
		receive.denyNonFastForwards
908 909 910 911
		user.name
		user.email
		user.signingkey
		whatchanged.difftree
912
		branch. remote.
913
	"
914 915
}

916 917 918 919 920 921
_git_remote ()
{
	local i c=1 command
	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
922
		add|show|prune|update) command="$i"; break ;;
923 924 925 926 927
		esac
		c=$((++c))
	done

	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
928
		__gitcomp "add show prune update"
929 930 931 932 933 934 935
		return
	fi

	case "$command" in
	show|prune)
		__gitcomp "$(__git_remotes)"
		;;
936 937 938 939 940 941 942 943 944 945 946 947
	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"
		;;
948 949 950 951 952 953
	*)
		COMPREPLY=()
		;;
	esac
}

954 955 956
_git_reset ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
957 958 959 960 961 962 963
	case "$cur" in
	--*)
		__gitcomp "--mixed --hard --soft"
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
964 965
}

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
_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
}

986 987 988 989 990
_git_show ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
991
		__gitcomp "
992
			oneline short medium full fuller email raw
993
			" "" "${cur##--pretty=}"
994 995 996
		return
		;;
	--*)
997
		__gitcomp "--pretty="
998 999 1000 1001 1002 1003
		return
		;;
	esac
	__git_complete_file
}

J
Junio C Hamano 已提交
1004 1005 1006 1007 1008
_git_stash ()
{
	__gitcomp 'list show apply clear'
}

1009 1010
_git ()
{
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	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

	if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1025 1026 1027 1028 1029 1030
		case "${COMP_WORDS[COMP_CWORD]}" in
		--*=*) COMPREPLY=() ;;
		--*)   __gitcomp "--git-dir= --bare --version --exec-path" ;;
		*)     __gitcomp "$(__git_commands) $(__git_aliases)" ;;
		esac
		return
1031
	fi
1032

1033 1034
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
1035

1036
	case "$command" in
1037
	am)          _git_am ;;
1038
	add)         _git_add ;;
1039
	apply)       _git_apply ;;
1040
	bisect)      _git_bisect ;;
1041
	bundle)      _git_bundle ;;
1042 1043
	branch)      _git_branch ;;
	checkout)    _git_checkout ;;
1044
	cherry)      _git_cherry ;;
1045
	cherry-pick) _git_cherry_pick ;;
1046
	commit)      _git_commit ;;
1047
	config)      _git_config ;;
1048 1049
	diff)        _git_diff ;;
	fetch)       _git_fetch ;;
1050
	format-patch) _git_format_patch ;;
1051
	gc)          _git_gc ;;
1052 1053 1054
	log)         _git_log ;;
	ls-remote)   _git_ls_remote ;;
	ls-tree)     _git_ls_tree ;;
1055
	merge)       _git_merge;;
1056
	merge-base)  _git_merge_base ;;
1057
	name-rev)    _git_name_rev ;;
1058 1059
	pull)        _git_pull ;;
	push)        _git_push ;;
1060
	rebase)      _git_rebase ;;
1061
	remote)      _git_remote ;;
1062
	reset)       _git_reset ;;
1063
	shortlog)    _git_shortlog ;;
1064
	show)        _git_show ;;
1065
	show-branch) _git_log ;;
J
Junio C Hamano 已提交
1066
	stash)       _git_stash ;;
1067 1068 1069
	whatchanged) _git_log ;;
	*)           COMPREPLY=() ;;
	esac
1070 1071 1072 1073 1074
}

_gitk ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
1075 1076 1077 1078 1079 1080
	case "$cur" in
	--*)
		__gitcomp "--not --all"
		return
		;;
	esac
1081
	__git_complete_revlist
1082 1083 1084
}

complete -o default -o nospace -F _git git
1085 1086 1087
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
1088
complete -o default -o nospace -F _git_bisect git-bisect
1089
complete -o default -o nospace -F _git_branch git-branch
1090
complete -o default -o nospace -F _git_bundle git-bundle
1091
complete -o default -o nospace -F _git_checkout git-checkout
1092
complete -o default -o nospace -F _git_cherry git-cherry
1093 1094
complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
complete -o default -o nospace -F _git_commit git-commit
1095 1096
complete -o default -o nospace -F _git_diff git-diff
complete -o default -o nospace -F _git_fetch git-fetch
1097
complete -o default -o nospace -F _git_format_patch git-format-patch
1098
complete -o default -o nospace -F _git_gc git-gc
1099
complete -o default -o nospace -F _git_log git-log
1100
complete -o default -o nospace -F _git_ls_remote git-ls-remote
1101
complete -o default -o nospace -F _git_ls_tree git-ls-tree
1102 1103 1104
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
1105 1106
complete -o default -o nospace -F _git_pull git-pull
complete -o default -o nospace -F _git_push git-push
1107 1108
complete -o default -o nospace -F _git_rebase git-rebase
complete -o default -o nospace -F _git_config git-config
1109
complete -o default -o nospace -F _git_remote git-remote
1110
complete -o default -o nospace -F _git_reset git-reset
1111
complete -o default -o nospace -F _git_shortlog git-shortlog
1112
complete -o default -o nospace -F _git_show git-show
J
Junio C Hamano 已提交
1113
complete -o default -o nospace -F _git_stash git-stash
1114
complete -o default -o nospace -F _git_log git-show-branch
1115 1116 1117 1118 1119 1120
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.
#
1121
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1122 1123
complete -o default -o nospace -F _git_add git-add.exe
complete -o default -o nospace -F _git_apply git-apply.exe
1124
complete -o default -o nospace -F _git git.exe
1125
complete -o default -o nospace -F _git_branch git-branch.exe
1126
complete -o default -o nospace -F _git_bundle git-bundle.exe
1127
complete -o default -o nospace -F _git_cherry git-cherry.exe
1128
complete -o default -o nospace -F _git_diff git-diff.exe
1129
complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1130 1131
complete -o default -o nospace -F _git_log git-log.exe
complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1132 1133
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
1134
complete -o default -o nospace -F _git_push git-push.exe
1135
complete -o default -o nospace -F _git_config git-config
1136
complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1137
complete -o default -o nospace -F _git_show git-show.exe
1138
complete -o default -o nospace -F _git_log git-show-branch.exe
1139
complete -o default -o nospace -F _git_log git-whatchanged.exe
1140
fi