git-completion.bash 42.2 KB
Newer Older
1
#!bash
2 3 4
#
# bash completion support for core Git.
#
5
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
6
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
7
# Distributed under the GNU General Public License, version 2.0.
8 9 10 11 12 13 14 15
#
# The contained completion routines provide support for completing:
#
#    *) local and remote branch names
#    *) local and remote tag names
#    *) .git/remotes file names
#    *) git 'subcommands'
#    *) tree paths within 'ref:path/to/file' expressions
16
#    *) common --long-options
17 18 19 20 21 22 23
#
# 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
#
24 25 26 27 28 29 30
#    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:
31 32 33 34 35 36
#        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.
#
37 38 39 40 41
#       In addition, if you set GIT_PS1_SHOWDIRTYSTATE to a nonempty
#       value, unstaged (*) and staged (+) changes will be shown next
#       to the branch name.  You can configure this per-repository
#       with the bash.showDirtyState variable, which defaults to true
#       once GIT_PS1_SHOWDIRTYSTATE is enabled.
42
#
43 44 45 46 47 48 49 50 51 52 53
# 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
#
54

55 56 57 58 59
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac

60 61
# __gitdir accepts 0 or 1 arguments (i.e., location)
# returns location of .git repo
62 63
__gitdir ()
{
64
	if [ -z "${1-}" ]; then
65
		if [ -n "${__git_dir-}" ]; then
66 67 68 69 70 71 72 73 74 75 76
			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
77 78
}

79 80
# __git_ps1 accepts 0 or 1 arguments (i.e., format string)
# returns text to add to bash PS1 prompt (includes branch name)
81 82
__git_ps1 ()
{
83
	local g="$(__gitdir)"
84 85 86
	if [ -n "$g" ]; then
		local r
		local b
87 88
		if [ -d "$g/rebase-apply" ]; then
			if [ -f "$g/rebase-apply/rebasing" ]; then
J
Junio C Hamano 已提交
89
				r="|REBASE"
90
		elif [ -f "$g/rebase-apply/applying" ]; then
J
Junio C Hamano 已提交
91 92 93 94
				r="|AM"
			else
				r="|AM/REBASE"
			fi
95
			b="$(git symbolic-ref HEAD 2>/dev/null)"
96
		elif [ -f "$g/rebase-merge/interactive" ]; then
97
			r="|REBASE-i"
98
			b="$(cat "$g/rebase-merge/head-name")"
99
		elif [ -d "$g/rebase-merge" ]; then
100
			r="|REBASE-m"
101
			b="$(cat "$g/rebase-merge/head-name")"
102
		else
103 104 105
			if [ -f "$g/MERGE_HEAD" ]; then
				r="|MERGING"
			fi
106
			if [ -f "$g/BISECT_LOG" ]; then
107 108
				r="|BISECTING"
			fi
109 110

			b="$(git symbolic-ref HEAD 2>/dev/null)" || {
111 112 113 114 115 116 117 118 119 120 121 122 123

				b="$(
				case "${GIT_PS1_DESCRIBE_STYLE-}" in
				(contains)
					git describe --contains HEAD ;;
				(branch)
					git describe --contains --all HEAD ;;
				(describe)
					git describe HEAD ;;
				(* | default)
					git describe --exact-match HEAD ;;
				esac 2>/dev/null)" ||

124 125 126 127
				b="$(cut -c1-7 "$g/HEAD" 2>/dev/null)..." ||
				b="unknown"
				b="($b)"
			}
128 129
		fi

130 131
		local w
		local i
132
		local c
133

134
		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
135
			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
136 137 138 139
				c="BARE:"
			else
				b="GIT_DIR!"
			fi
140 141 142 143 144 145 146 147 148 149 150
		elif [ "true" = "$(git rev-parse --is-inside-work-tree 2>/dev/null)" ]; then
			if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ]; then
				if [ "$(git config --bool bash.showDirtyState)" != "false" ]; then
					git diff --no-ext-diff --ignore-submodules \
						--quiet --exit-code || w="*"
					if git rev-parse --quiet --verify HEAD >/dev/null; then
						git diff-index --cached --quiet \
							--ignore-submodules HEAD -- || i="+"
					else
						i="#"
					fi
151 152 153 154
				fi
			fi
		fi

155 156
		if [ -n "$b" ]; then
			if [ -n "${1-}" ]; then
157
				printf "$1" "$c${b##refs/heads/}$w$i$r"
158
			else
159
				printf " (%s)" "$c${b##refs/heads/}$w$i$r"
160
			fi
161 162 163 164
		fi
	fi
}

165
# __gitcomp_1 requires 2 arguments
166 167 168 169 170 171 172 173 174 175 176 177
__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
}

178 179
# __gitcomp accepts 1, 2, 3, or 4 arguments
# generates completion reply with compgen
180 181
__gitcomp ()
{
182
	local cur="${COMP_WORDS[COMP_CWORD]}"
183
	if [ $# -gt 2 ]; then
184 185
		cur="$3"
	fi
186 187 188 189 190
	case "$cur" in
	--*=)
		COMPREPLY=()
		;;
	*)
191
		local IFS=$'\n'
192 193
		COMPREPLY=($(compgen -P "${2-}" \
			-W "$(__gitcomp_1 "${1-}" "${4-}")" \
194
			-- "$cur"))
195 196
		;;
	esac
197 198
}

199
# __git_heads accepts 0 or 1 arguments (to pass to __gitdir)
200 201
__git_heads ()
{
202
	local cmd i is_hash=y dir="$(__gitdir "${1-}")"
203
	if [ -d "$dir" ]; then
204 205
		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
			refs/heads
206 207
		return
	fi
208
	for i in $(git ls-remote "${1-}" 2>/dev/null); do
209 210 211 212 213 214 215 216 217
		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
}

218
# __git_tags accepts 0 or 1 arguments (to pass to __gitdir)
219 220
__git_tags ()
{
221
	local cmd i is_hash=y dir="$(__gitdir "${1-}")"
222
	if [ -d "$dir" ]; then
223 224
		git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
			refs/tags
225 226
		return
	fi
227
	for i in $(git ls-remote "${1-}" 2>/dev/null); do
228 229 230 231 232 233 234 235 236
		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
}

237
# __git_refs accepts 0 or 1 arguments (to pass to __gitdir)
238 239
__git_refs ()
{
240
	local i is_hash=y dir="$(__gitdir "${1-}")"
S
SZEDER Gábor 已提交
241
	local cur="${COMP_WORDS[COMP_CWORD]}" format refs
242
	if [ -d "$dir" ]; then
S
SZEDER Gábor 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255
		case "$cur" in
		refs|refs/*)
			format="refname"
			refs="${cur%/*}"
			;;
		*)
			if [ -e "$dir/HEAD" ]; then echo HEAD; fi
			format="refname:short"
			refs="refs/tags refs/heads refs/remotes"
			;;
		esac
		git --git-dir="$dir" for-each-ref --format="%($format)" \
			$refs
256
		return
257
	fi
258
	for i in $(git ls-remote "$dir" 2>/dev/null); do
259 260 261 262 263
		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/}" ;;
264
		n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
265 266 267 268 269
		n,*) is_hash=y; echo "$i" ;;
		esac
	done
}

270
# __git_refs2 requires 1 argument (to pass to __git_refs)
271 272
__git_refs2 ()
{
273 274 275
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
276 277 278
	done
}

279
# __git_refs_remotes requires 1 argument (to pass to ls-remote)
280 281 282
__git_refs_remotes ()
{
	local cmd i is_hash=y
283
	for i in $(git ls-remote "$1" 2>/dev/null); do
284 285 286 287 288 289 290 291 292 293 294 295 296
		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
}

297 298
__git_remotes ()
{
299
	local i ngoff IFS=$'\n' d="$(__gitdir)"
300
	shopt -q nullglob || ngoff=1
301
	shopt -s nullglob
302 303
	for i in "$d/remotes"/*; do
		echo ${i#$d/remotes/}
304
	done
305
	[ "$ngoff" ] && shopt -u nullglob
306
	for i in $(git --git-dir="$d" config --list); do
307 308 309 310 311 312 313
		case "$i" in
		remote.*.url=*)
			i="${i#remote.}"
			echo "${i/.url=*/}"
			;;
		esac
	done
314 315
}

316 317
__git_merge_strategies ()
{
318
	if [ -n "${__git_merge_strategylist-}" ]; then
319 320 321
		echo "$__git_merge_strategylist"
		return
	fi
322 323 324 325 326 327
	git merge -s help 2>&1 |
	sed -n -e '/[Aa]vailable strategies are: /,/^$/{
		s/\.$//
		s/.*://
		s/^[ 	]*//
		s/[ 	]*$//
328
		p
329
	}'
330
}
331
__git_merge_strategylist=
332
__git_merge_strategylist=$(__git_merge_strategies 2>/dev/null)
333

334 335
__git_complete_file ()
{
336
	local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
337 338
	case "$cur" in
	?*:*)
339 340
		ref="${cur%%:*}"
		cur="${cur#*:}"
341 342
		case "$cur" in
		?*/*)
343 344
			pfx="${cur%/*}"
			cur="${cur##*/}"
345 346 347 348 349 350 351
			ls="$ref:$pfx"
			pfx="$pfx/"
			;;
		*)
			ls="$ref"
			;;
	    esac
352 353 354 355 356 357

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

358
		local IFS=$'\n'
359
		COMPREPLY=($(compgen -P "$pfx" \
360
			-W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
361 362 363 364 365 366 367 368
				| sed '/^100... blob /{
				           s,^.*	,,
				           s,$, ,
				       }
				       /^120000 blob /{
				           s,^.*	,,
				           s,$, ,
				       }
369 370 371 372 373 374 375 376
				       /^040000 tree /{
				           s,^.*	,,
				           s,$,/,
				       }
				       s/^.*	//')" \
			-- "$cur"))
		;;
	*)
377
		__gitcomp "$(__git_refs)"
378 379 380 381
		;;
	esac
}

382 383 384 385 386 387 388
__git_complete_revlist ()
{
	local pfx cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	*...*)
		pfx="${cur%...*}..."
		cur="${cur#*...}"
389
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
390 391 392 393
		;;
	*..*)
		pfx="${cur%..*}.."
		cur="${cur#*..}"
394 395
		__gitcomp "$(__git_refs)" "$pfx" "$cur"
		;;
396
	*)
397
		__gitcomp "$(__git_refs)"
398 399 400 401
		;;
	esac
}

402 403 404 405
__git_complete_remote_or_refspec ()
{
	local cmd="${COMP_WORDS[1]}"
	local cur="${COMP_WORDS[COMP_CWORD]}"
406
	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
407 408 409
	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
410
		--all|--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
411 412 413 414 415 416 417 418 419
		-*) ;;
		*) remote="$i"; break ;;
		esac
		c=$((++c))
	done
	if [ -z "$remote" ]; then
		__gitcomp "$(__git_remotes)"
		return
	fi
420 421 422 423
	if [ $no_complete_refspec = 1 ]; then
		COMPREPLY=()
		return
	fi
424 425 426 427 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 457 458 459 460 461 462 463
	[ "$remote" = "." ] && remote=
	case "$cur" in
	*:*)
		case "$COMP_WORDBREAKS" in
		*:*) : great ;;
		*)   pfx="${cur%%:*}:" ;;
		esac
		cur="${cur#*:}"
		lhs=0
		;;
	+*)
		pfx="+"
		cur="${cur#+}"
		;;
	esac
	case "$cmd" in
	fetch)
		if [ $lhs = 1 ]; then
			__gitcomp "$(__git_refs2 "$remote")" "$pfx" "$cur"
		else
			__gitcomp "$(__git_refs)" "$pfx" "$cur"
		fi
		;;
	pull)
		if [ $lhs = 1 ]; then
			__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
		else
			__gitcomp "$(__git_refs)" "$pfx" "$cur"
		fi
		;;
	push)
		if [ $lhs = 1 ]; then
			__gitcomp "$(__git_refs)" "$pfx" "$cur"
		else
			__gitcomp "$(__git_refs "$remote")" "$pfx" "$cur"
		fi
		;;
	esac
}

464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
__git_complete_strategy ()
{
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	-s|--strategy)
		__gitcomp "$(__git_merge_strategies)"
		return 0
	esac
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--strategy=*)
		__gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
		return 0
		;;
	esac
	return 1
}

481
__git_all_commands ()
482
{
483
	if [ -n "${__git_all_commandlist-}" ]; then
484
		echo "$__git_all_commandlist"
485 486
		return
	fi
487 488
	local i IFS=" "$'\n'
	for i in $(git help -a|egrep '^ ')
489 490 491 492 493 494 495 496 497 498 499 500
	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 ()
{
501
	if [ -n "${__git_porcelain_commandlist-}" ]; then
502 503 504 505 506
		echo "$__git_porcelain_commandlist"
		return
	fi
	local i IFS=" "$'\n'
	for i in "help" $(__git_all_commands)
507 508
	do
		case $i in
509
		*--*)             : helper pattern;;
510 511 512
		applymbox)        : ask gittus;;
		applypatch)       : ask gittus;;
		archimport)       : import;;
513
		cat-file)         : plumbing;;
514
		check-attr)       : plumbing;;
515
		check-ref-format) : plumbing;;
516
		checkout-index)   : plumbing;;
517
		commit-tree)      : plumbing;;
518
		count-objects)    : infrequent;;
519 520
		cvsexportcommit)  : export;;
		cvsimport)        : import;;
521 522
		cvsserver)        : daemon;;
		daemon)           : daemon;;
523 524 525
		diff-files)       : plumbing;;
		diff-index)       : plumbing;;
		diff-tree)        : plumbing;;
S
Shawn O. Pearce 已提交
526
		fast-import)      : import;;
527
		fast-export)      : export;;
528
		fsck-objects)     : plumbing;;
529
		fetch-pack)       : plumbing;;
530
		fmt-merge-msg)    : plumbing;;
531
		for-each-ref)     : plumbing;;
532 533 534
		hash-object)      : plumbing;;
		http-*)           : transport;;
		index-pack)       : plumbing;;
535
		init-db)          : deprecated;;
536
		local-fetch)      : plumbing;;
537 538 539 540
		lost-found)       : infrequent;;
		ls-files)         : plumbing;;
		ls-remote)        : plumbing;;
		ls-tree)          : plumbing;;
541 542 543 544 545 546 547 548 549 550 551
		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;;
552 553 554
		prune)            : plumbing;;
		prune-packed)     : plumbing;;
		quiltimport)      : import;;
555 556
		read-tree)        : plumbing;;
		receive-pack)     : plumbing;;
557
		reflog)           : plumbing;;
558
		repo-config)      : deprecated;;
559 560 561 562 563 564
		rerere)           : plumbing;;
		rev-list)         : plumbing;;
		rev-parse)        : plumbing;;
		runstatus)        : plumbing;;
		sh-setup)         : internal;;
		shell)            : daemon;;
565
		show-ref)         : plumbing;;
566 567 568 569 570
		send-pack)        : plumbing;;
		show-index)       : plumbing;;
		ssh-*)            : transport;;
		stripspace)       : plumbing;;
		symbolic-ref)     : plumbing;;
571
		tar-tree)         : deprecated;;
572 573
		unpack-file)      : plumbing;;
		unpack-objects)   : plumbing;;
574
		update-index)     : plumbing;;
575 576 577 578 579
		update-ref)       : plumbing;;
		update-server-info) : daemon;;
		upload-archive)   : plumbing;;
		upload-pack)      : plumbing;;
		write-tree)       : plumbing;;
580 581
		var)              : infrequent;;
		verify-pack)      : infrequent;;
582
		verify-tag)       : plumbing;;
583 584 585 586
		*) echo $i;;
		esac
	done
}
587 588
__git_porcelain_commandlist=
__git_porcelain_commandlist="$(__git_porcelain_commands 2>/dev/null)"
589

590 591
__git_aliases ()
{
592
	local i IFS=$'\n'
593
	for i in $(git --git-dir="$(__gitdir)" config --list); do
594 595 596 597 598 599 600
		case "$i" in
		alias.*)
			i="${i#alias.}"
			echo "${i/=*/}"
			;;
		esac
	done
601 602
}

603
# __git_aliased_command requires 1 argument
604 605
__git_aliased_command ()
{
606
	local word cmdline=$(git --git-dir="$(__gitdir)" \
607
		config --get "alias.$1")
608 609 610 611 612 613 614 615
	for word in $cmdline; do
		if [ "${word##-*}" ]; then
			echo $word
			return
		fi
	done
}

616
# __git_find_subcommand requires 1 argument
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
__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
}

633 634 635 636 637 638 639 640 641 642 643 644
__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
}

645
__git_whitespacelist="nowarn warn error error-all fix"
646 647 648

_git_am ()
{
649
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
650
	if [ -d "$dir"/rebase-apply ]; then
651
		__gitcomp "--skip --resolved --abort"
652 653 654 655
		return
	fi
	case "$cur" in
	--whitespace=*)
656
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
657 658 659
		return
		;;
	--*)
660
		__gitcomp "
661 662
			--3way --committer-date-is-author-date --ignore-date
			--interactive --keep --no-utf8 --signoff --utf8
663
			--whitespace=
664
			"
665 666 667 668 669 670 671 672 673 674
		return
	esac
	COMPREPLY=()
}

_git_apply ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--whitespace=*)
675
		__gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
676 677 678
		return
		;;
	--*)
679
		__gitcomp "
680 681 682 683
			--stat --numstat --summary --check --index
			--cached --index-info --reverse --reject --unidiff-zero
			--apply --no-add --exclude=
			--whitespace= --inaccurate-eof --verbose
684
			"
685 686 687 688 689
		return
	esac
	COMPREPLY=()
}

690 691
_git_add ()
{
692 693
	__git_has_doubledash && return

694 695 696
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
697 698
		__gitcomp "
			--interactive --refresh --patch --update --dry-run
699
			--ignore-errors --intent-to-add
700
			"
701 702 703 704 705
		return
	esac
	COMPREPLY=()
}

706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
_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
}

729 730
_git_bisect ()
{
731 732
	__git_has_doubledash && return

733
	local subcommands="start bad good skip reset visualize replay log run"
734 735 736
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
737 738 739
		return
	fi

740
	case "$subcommand" in
741
	bad|good|reset|skip)
742 743 744 745 746 747 748 749
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

750 751
_git_branch ()
{
752 753 754 755 756 757 758 759 760 761 762
	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 已提交
763 764 765 766
	case "${COMP_WORDS[COMP_CWORD]}" in
	--*)
		__gitcomp "
			--color --no-color --verbose --abbrev= --no-abbrev
767
			--track --no-track --contains --merged --no-merged
S
SZEDER Gábor 已提交
768 769
			"
		;;
770 771 772 773 774 775 776
	*)
		if [ $only_local_ref = "y" -a $has_r = "n" ]; then
			__gitcomp "$(__git_heads)"
		else
			__gitcomp "$(__git_refs)"
		fi
		;;
S
SZEDER Gábor 已提交
777
	esac
778 779
}

780 781
_git_bundle ()
{
782 783 784
	local cmd="${COMP_WORDS[2]}"
	case "$COMP_CWORD" in
	2)
785 786
		__gitcomp "create list-heads verify unbundle"
		;;
787
	3)
788 789 790 791 792 793 794 795 796 797 798 799
		# looking for a file
		;;
	*)
		case "$cmd" in
			create)
				__git_complete_revlist
			;;
		esac
		;;
	esac
}

800 801
_git_checkout ()
{
802 803
	__git_has_doubledash && return

804
	__gitcomp "$(__git_refs)"
805 806
}

807 808 809 810 811
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

812 813 814 815 816
_git_cherry_pick ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
817
		__gitcomp "--edit --no-commit"
818 819
		;;
	*)
820
		__gitcomp "$(__git_refs)"
821 822 823 824
		;;
	esac
}

825 826 827 828 829 830 831 832 833 834 835 836 837 838
_git_clean ()
{
	__git_has_doubledash && return

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

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
_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=()
}

864 865
_git_commit ()
{
866 867
	__git_has_doubledash && return

868 869 870
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
871
		__gitcomp "
872
			--all --author= --signoff --verify --no-verify
873
			--edit --amend --include --only --interactive
874
			"
875 876 877 878 879
		return
	esac
	COMPREPLY=()
}

880 881
_git_describe ()
{
882 883 884 885 886 887 888 889 890
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--all --tags --contains --abbrev= --candidates=
			--exact-match --debug --long --match --always
			"
		return
	esac
891 892 893
	__gitcomp "$(__git_refs)"
}

894
__git_diff_common_options="--stat --numstat --shortstat --summary
895 896
			--patch-with-stat --name-only --name-status --color
			--no-color --color-words --no-renames --check
897
			--full-index --binary --abbrev --diff-filter=
898
			--find-copies-harder
899 900
			--text --ignore-space-at-eol --ignore-space-change
			--ignore-all-space --exit-code --quiet --ext-diff
901 902
			--no-ext-diff
			--no-prefix --src-prefix= --dst-prefix=
903
			--inter-hunk-context=
904
			--patience
905 906 907 908 909 910 911 912 913 914
			--raw
"

_git_diff ()
{
	__git_has_doubledash && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
915
		__gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
916 917
			--base --ours --theirs
			$__git_diff_common_options
918
			"
919 920 921
		return
		;;
	esac
922 923 924
	__git_complete_file
}

925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
			tkdiff vimdiff gvimdiff xxdiff
"

_git_difftool ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--tool=*)
		__gitcomp "$__git_mergetools_common kompare" "" "${cur##--tool=}"
		return
		;;
	--*)
		__gitcomp "--tool="
		return
		;;
	esac
	COMPREPLY=()
}

945 946 947 948 949
__git_fetch_options="
	--quiet --verbose --append --upload-pack --force --keep --depth=
	--tags --no-tags
"

950 951
_git_fetch ()
{
952 953 954 955 956 957 958
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "$__git_fetch_options"
		return
		;;
	esac
959
	__git_complete_remote_or_refspec
960 961
}

962 963 964 965
_git_format_patch ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
966 967 968 969 970 971
	--thread=*)
		__gitcomp "
			deep shallow
			" "" "${cur##--thread=}"
		return
		;;
972
	--*)
973
		__gitcomp "
974
			--stdout --attach --no-attach --thread --thread=
975 976
			--output-directory
			--numbered --start-number
977
			--numbered-files
978 979
			--keep-subject
			--signoff
980
			--in-reply-to= --cc=
981
			--full-index --binary
982
			--not --all
983
			--cover-letter
984
			--no-prefix --src-prefix= --dst-prefix=
985 986
			--inline --suffix= --ignore-if-in-upstream
			--subject-prefix=
987
			"
988 989 990 991 992 993
		return
		;;
	esac
	__git_complete_revlist
}

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
_git_fsck ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--tags --root --unreachable --cache --no-reflogs --full
			--strict --verbose --lost-found
			"
		return
		;;
	esac
	COMPREPLY=()
}

1009 1010 1011 1012 1013
_git_gc ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
1014
		__gitcomp "--prune --aggressive"
1015 1016 1017 1018 1019 1020
		return
		;;
	esac
	COMPREPLY=()
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
_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=()
}

1044 1045 1046 1047 1048 1049 1050 1051 1052
_git_help ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--all --info --man --web"
		return
		;;
	esac
1053 1054 1055 1056
	__gitcomp "$(__git_all_commands)
		attributes cli core-tutorial cvs-migration
		diffcore gitk glossary hooks ignore modules
		repository-layout tutorial tutorial-2
1057
		workflows
1058
		"
1059 1060
}

1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
_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=()
}

1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
_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=()
}

1099 1100
_git_ls_remote ()
{
1101
	__gitcomp "$(__git_remotes)"
1102 1103 1104 1105 1106 1107 1108
}

_git_ls_tree ()
{
	__git_complete_file
}

1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
# Options that go well for log, shortlog and gitk
__git_log_common_options="
	--not --all
	--branches --tags --remotes
	--first-parent --no-merges
	--max-count=
	--max-age= --since= --after=
	--min-age= --until= --before=
"
# Options that go well for log and gitk (not shortlog)
__git_log_gitk_options="
	--dense --sparse --full-history
	--simplify-merges --simplify-by-decoration
	--left-right
"
# Options that go well for log and shortlog (not gitk)
__git_log_shortlog_options="
	--author= --committer= --grep=
	--all-match
"

1130
__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1131
__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1132

1133 1134
_git_log ()
{
1135 1136
	__git_has_doubledash && return

1137
	local cur="${COMP_WORDS[COMP_CWORD]}"
1138 1139
	local g="$(git rev-parse --git-dir 2>/dev/null)"
	local merge=""
1140
	if [ -f "$g/MERGE_HEAD" ]; then
1141 1142
		merge="--merge"
	fi
1143 1144
	case "$cur" in
	--pretty=*)
1145
		__gitcomp "$__git_log_pretty_formats
1146
			" "" "${cur##--pretty=}"
1147 1148
		return
		;;
1149 1150 1151 1152 1153
	--format=*)
		__gitcomp "$__git_log_pretty_formats
			" "" "${cur##--format=}"
		return
		;;
1154
	--date=*)
1155
		__gitcomp "$__git_log_date_formats" "" "${cur##--date=}"
1156 1157
		return
		;;
1158
	--*)
1159
		__gitcomp "
1160 1161 1162
			$__git_log_common_options
			$__git_log_shortlog_options
			$__git_log_gitk_options
1163
			--root --topo-order --date-order --reverse
1164
			--follow
1165
			--abbrev-commit --abbrev=
1166
			--relative-date --date=
1167
			--pretty= --format= --oneline
1168
			--cherry-pick
1169
			--graph
1170 1171
			--decorate
			--walk-reflogs
1172
			--parents --children
1173
			$merge
1174
			$__git_diff_common_options
1175
			--pickaxe-all --pickaxe-regex
1176
			"
1177 1178 1179
		return
		;;
	esac
1180
	__git_complete_revlist
1181 1182
}

1183 1184 1185 1186 1187
__git_merge_options="
	--no-commit --no-stat --log --no-log --squash --strategy
	--commit --stat --no-squash --ff --no-ff
"

1188 1189
_git_merge ()
{
1190 1191
	__git_complete_strategy && return

1192 1193 1194
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
1195
		__gitcomp "$__git_merge_options"
1196 1197
		return
	esac
1198
	__gitcomp "$(__git_refs)"
1199 1200
}

1201 1202 1203 1204 1205
_git_mergetool ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--tool=*)
1206
		__gitcomp "$__git_mergetools_common tortoisemerge" "" "${cur##--tool=}"
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
		return
		;;
	--*)
		__gitcomp "--tool="
		return
		;;
	esac
	COMPREPLY=()
}

1217 1218
_git_merge_base ()
{
1219
	__gitcomp "$(__git_refs)"
1220 1221
}

1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
_git_mv ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--dry-run"
		return
		;;
	esac
	COMPREPLY=()
}

1234 1235
_git_name_rev ()
{
1236
	__gitcomp "--tags --all --stdin"
1237 1238
}

1239 1240
_git_pull ()
{
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	__git_complete_strategy && return

	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
			--rebase --no-rebase
			$__git_merge_options
			$__git_fetch_options
		"
		return
		;;
	esac
1254
	__git_complete_remote_or_refspec
1255 1256 1257 1258
}

_git_push ()
{
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "${COMP_WORDS[COMP_CWORD-1]}" in
	--repo)
		__gitcomp "$(__git_remotes)"
		return
	esac
	case "$cur" in
	--repo=*)
		__gitcomp "$(__git_remotes)" "" "${cur##--repo=}"
		return
		;;
	--*)
		__gitcomp "
			--all --mirror --tags --dry-run --force --verbose
			--receive-pack= --repo=
		"
		return
		;;
	esac
1278
	__git_complete_remote_or_refspec
1279 1280
}

1281 1282
_git_rebase ()
{
1283
	local cur="${COMP_WORDS[COMP_CWORD]}" dir="$(__gitdir)"
1284
	if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
1285
		__gitcomp "--continue --skip --abort"
1286 1287
		return
	fi
1288
	__git_complete_strategy && return
1289 1290
	case "$cur" in
	--*)
1291
		__gitcomp "--onto --merge --strategy --interactive"
1292 1293
		return
	esac
1294
	__gitcomp "$(__git_refs)"
1295 1296
}

1297 1298 1299
__git_send_email_confirm_options="always never auto cc compose"
__git_send_email_suppresscc_options="author self cc ccbody sob cccmd body all"

1300 1301 1302 1303
_git_send_email ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	--confirm=*)
		__gitcomp "
			$__git_send_email_confirm_options
			" "" "${cur##--confirm=}"
		return
		;;
	--suppress-cc=*)
		__gitcomp "
			$__git_send_email_suppresscc_options
			" "" "${cur##--suppress-cc=}"

		return
		;;
	--smtp-encryption=*)
		__gitcomp "ssl tls" "" "${cur##--smtp-encryption=}"
		return
		;;
1321
	--*)
1322
		__gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1323 1324
			--compose --confirm= --dry-run --envelope-sender
			--from --identity
1325 1326 1327
			--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
1328 1329
			--smtp-server-port --smtp-encryption= --smtp-user
			--subject --suppress-cc= --suppress-from --thread --to
1330
			--validate --no-validate"
1331 1332 1333 1334 1335 1336
		return
		;;
	esac
	COMPREPLY=()
}

1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
__git_config_get_set_variables ()
{
	local prevword word config_file= c=$COMP_CWORD
	while [ $c -gt 1 ]; do
		word="${COMP_WORDS[c]}"
		case "$word" in
		--global|--system|--file=*)
			config_file="$word"
			break
			;;
		-f|--file)
			config_file="$word $prevword"
			break
			;;
		esac
		prevword=$word
		c=$((--c))
	done

	for i in $(git --git-dir="$(__gitdir)" config $config_file --list \
			2>/dev/null); do
		case "$i" in
		*.*)
			echo "${i/=*/}"
			;;
		esac
	done
}

1366
_git_config ()
1367 1368 1369 1370 1371
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	local prv="${COMP_WORDS[COMP_CWORD-1]}"
	case "$prv" in
	branch.*.remote)
1372
		__gitcomp "$(__git_remotes)"
1373 1374 1375
		return
		;;
	branch.*.merge)
1376
		__gitcomp "$(__git_refs)"
1377 1378 1379 1380 1381
		return
		;;
	remote.*.fetch)
		local remote="${prv#remote.}"
		remote="${remote%.fetch}"
1382
		__gitcomp "$(__git_refs_remotes "$remote")"
1383 1384 1385 1386 1387
		return
		;;
	remote.*.push)
		local remote="${prv#remote.}"
		remote="${remote%.push}"
1388
		__gitcomp "$(git --git-dir="$(__gitdir)" \
1389
			for-each-ref --format='%(refname):%(refname)' \
1390 1391 1392 1393 1394 1395 1396
			refs/heads)"
		return
		;;
	pull.twohead|pull.octopus)
		__gitcomp "$(__git_merge_strategies)"
		return
		;;
1397 1398
	color.branch|color.diff|color.interactive|\
	color.showbranch|color.status|color.ui)
1399 1400 1401
		__gitcomp "always never auto"
		return
		;;
1402 1403 1404 1405
	color.pager)
		__gitcomp "false true"
		return
		;;
1406 1407
	color.*.*)
		__gitcomp "
1408
			normal black red green yellow blue magenta cyan white
1409 1410
			bold dim ul blink reverse
			"
1411 1412
		return
		;;
1413 1414 1415 1416
	help.format)
		__gitcomp "man info web html"
		return
		;;
1417 1418 1419 1420
	log.date)
		__gitcomp "$__git_log_date_formats"
		return
		;;
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	sendemail.aliasesfiletype)
		__gitcomp "mutt mailrc pine elm gnus"
		return
		;;
	sendemail.confirm)
		__gitcomp "$__git_send_email_confirm_options"
		return
		;;
	sendemail.suppresscc)
		__gitcomp "$__git_send_email_suppresscc_options"
		return
		;;
1433 1434 1435 1436
	--get|--get-all|--unset|--unset-all)
		__gitcomp "$(__git_config_get_set_variables)"
		return
		;;
1437 1438 1439 1440 1441 1442 1443
	*.*)
		COMPREPLY=()
		return
		;;
	esac
	case "$cur" in
	--*)
1444
		__gitcomp "
1445
			--global --system --file=
1446
			--list --replace-all
1447
			--get --get-all --get-regexp
1448
			--add --unset --unset-all
1449
			--remove-section --rename-section
1450
			"
1451 1452 1453 1454 1455
		return
		;;
	branch.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
1456
		__gitcomp "remote merge mergeoptions" "$pfx" "$cur"
1457 1458 1459 1460 1461
		return
		;;
	branch.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
1462
		__gitcomp "$(__git_heads)" "$pfx" "$cur" "."
1463 1464
		return
		;;
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
	guitool.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
		__gitcomp "
			argprompt cmd confirm needsfile noconsole norescan
			prompt revprompt revunmerged title
			" "$pfx" "$cur"
		return
		;;
	difftool.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
		__gitcomp "cmd path" "$pfx" "$cur"
		return
		;;
	man.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
		__gitcomp "cmd path" "$pfx" "$cur"
		return
		;;
	mergetool.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
		__gitcomp "cmd path trustExitCode" "$pfx" "$cur"
		return
		;;
	pager.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
		__gitcomp "$(__git_all_commands)" "$pfx" "$cur"
		return
		;;
1498 1499 1500
	remote.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
1501
		__gitcomp "
1502
			url proxy fetch push mirror skipDefaultUpdate
1503 1504
			receivepack uploadpack tagopt
			" "$pfx" "$cur"
1505 1506 1507 1508 1509
		return
		;;
	remote.*)
		local pfx="${cur%.*}."
		cur="${cur#*.}"
1510
		__gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
1511 1512
		return
		;;
1513 1514 1515 1516 1517 1518
	url.*.*)
		local pfx="${cur%.*}."
		cur="${cur##*.}"
		__gitcomp "insteadof" "$pfx" "$cur"
		return
		;;
1519
	esac
1520
	__gitcomp "
1521
		alias.
1522
		apply.whitespace
1523 1524
		branch.autosetupmerge
		branch.autosetuprebase
1525
		clean.requireForce
1526 1527 1528 1529
		color.branch
		color.branch.current
		color.branch.local
		color.branch.plain
1530
		color.branch.remote
1531
		color.diff
1532
		color.diff.commit
1533
		color.diff.frag
1534
		color.diff.meta
1535
		color.diff.new
1536 1537
		color.diff.old
		color.diff.plain
1538
		color.diff.whitespace
1539 1540 1541
		color.grep
		color.grep.external
		color.grep.match
1542 1543 1544 1545
		color.interactive
		color.interactive.header
		color.interactive.help
		color.interactive.prompt
1546
		color.pager
1547
		color.showbranch
1548
		color.status
1549 1550
		color.status.added
		color.status.changed
1551
		color.status.header
1552
		color.status.nobranch
1553
		color.status.untracked
1554 1555 1556 1557 1558
		color.status.updated
		color.ui
		commit.template
		core.autocrlf
		core.bare
1559
		core.compression
1560
		core.createObject
1561 1562 1563
		core.deltaBaseCacheLimit
		core.editor
		core.excludesfile
1564
		core.fileMode
1565
		core.fsyncobjectfiles
1566
		core.gitProxy
1567
		core.ignoreCygwinFSTricks
1568 1569 1570 1571 1572
		core.ignoreStat
		core.logAllRefUpdates
		core.loosecompression
		core.packedGitLimit
		core.packedGitWindowSize
1573
		core.pager
1574
		core.preferSymlinkRefs
1575 1576
		core.preloadindex
		core.quotepath
1577
		core.repositoryFormatVersion
1578
		core.safecrlf
1579
		core.sharedRepository
1580 1581
		core.symlinks
		core.trustctime
1582
		core.warnAmbiguousRefs
1583 1584 1585 1586 1587
		core.whitespace
		core.worktree
		diff.autorefreshindex
		diff.external
		diff.mnemonicprefix
1588
		diff.renameLimit
1589
		diff.renameLimit.
1590
		diff.renames
1591 1592 1593
		diff.suppressBlankEmpty
		diff.tool
		diff.wordRegex
1594
		difftool.
1595
		difftool.prompt
1596
		fetch.unpackLimit
1597 1598
		format.attach
		format.cc
1599
		format.headers
1600 1601
		format.numbered
		format.pretty
1602 1603
		format.signoff
		format.subjectprefix
1604
		format.suffix
1605
		format.thread
1606 1607 1608
		gc.aggressiveWindow
		gc.auto
		gc.autopacklimit
1609
		gc.packrefs
1610
		gc.pruneexpire
1611 1612 1613 1614
		gc.reflogexpire
		gc.reflogexpireunreachable
		gc.rerereresolved
		gc.rerereunresolved
1615
		gitcvs.allbinary
1616
		gitcvs.commitmsgannotation
1617
		gitcvs.dbTableNamePrefix
1618 1619 1620 1621 1622 1623
		gitcvs.dbdriver
		gitcvs.dbname
		gitcvs.dbpass
		gitcvs.dbuser
		gitcvs.enabled
		gitcvs.logfile
1624
		gitcvs.usecrlfattr
1625
		guitool.
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639
		gui.blamehistoryctx
		gui.commitmsgwidth
		gui.copyblamethreshold
		gui.diffcontext
		gui.encoding
		gui.fastcopyblame
		gui.matchtrackingbranch
		gui.newbranchtemplate
		gui.pruneduringfetch
		gui.spellingdictionary
		gui.trustmtime
		help.autocorrect
		help.browser
		help.format
1640 1641
		http.lowSpeedLimit
		http.lowSpeedTime
1642
		http.maxRequests
1643
		http.noEPSV
1644
		http.proxy
1645 1646 1647 1648 1649
		http.sslCAInfo
		http.sslCAPath
		http.sslCert
		http.sslKey
		http.sslVerify
1650 1651
		i18n.commitEncoding
		i18n.logOutputEncoding
1652 1653 1654 1655 1656 1657 1658 1659
		imap.folder
		imap.host
		imap.pass
		imap.port
		imap.preformattedHTML
		imap.sslverify
		imap.tunnel
		imap.user
1660 1661 1662 1663 1664
		instaweb.browser
		instaweb.httpd
		instaweb.local
		instaweb.modulepath
		instaweb.port
1665
		interactive.singlekey
1666
		log.date
1667
		log.showroot
1668
		mailmap.file
1669
		man.
1670 1671 1672 1673 1674
		man.viewer
		merge.conflictstyle
		merge.log
		merge.renameLimit
		merge.stat
1675
		merge.tool
1676
		merge.verbosity
1677
		mergetool.
1678
		mergetool.keepBackup
1679
		mergetool.prompt
1680 1681
		pack.compression
		pack.deltaCacheLimit
1682 1683
		pack.deltaCacheSize
		pack.depth
1684 1685 1686
		pack.indexVersion
		pack.packSizeLimit
		pack.threads
1687 1688
		pack.window
		pack.windowMemory
1689
		pager.
1690 1691
		pull.octopus
		pull.twohead
1692 1693
		push.default
		rebase.stat
1694 1695
		receive.denyCurrentBranch
		receive.denyDeletes
1696
		receive.denyNonFastForwards
1697
		receive.fsckObjects
1698
		receive.unpackLimit
1699 1700 1701
		repack.usedeltabaseoffset
		rerere.autoupdate
		rerere.enabled
1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721
		sendemail.aliasesfile
		sendemail.aliasesfiletype
		sendemail.bcc
		sendemail.cc
		sendemail.cccmd
		sendemail.chainreplyto
		sendemail.confirm
		sendemail.envelopesender
		sendemail.multiedit
		sendemail.signedoffbycc
		sendemail.smtpencryption
		sendemail.smtppass
		sendemail.smtpserver
		sendemail.smtpserverport
		sendemail.smtpuser
		sendemail.suppresscc
		sendemail.suppressfrom
		sendemail.thread
		sendemail.to
		sendemail.validate
1722
		showbranch.default
1723 1724
		status.relativePaths
		status.showUntrackedFiles
1725 1726
		tar.umask
		transfer.unpackLimit
1727
		url.
1728
		user.email
1729
		user.name
1730
		user.signingkey
1731
		web.browser
1732
		branch. remote.
1733
	"
1734 1735
}

1736 1737
_git_remote ()
{
1738
	local subcommands="add rename rm show prune update set-head"
1739 1740
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1741
		__gitcomp "$subcommands"
1742 1743 1744
		return
	fi

1745
	case "$subcommand" in
1746
	rename|rm|show|prune)
1747 1748
		__gitcomp "$(__git_remotes)"
		;;
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760
	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"
		;;
1761 1762 1763 1764 1765 1766
	*)
		COMPREPLY=()
		;;
	esac
}

1767 1768
_git_reset ()
{
1769 1770
	__git_has_doubledash && return

1771
	local cur="${COMP_WORDS[COMP_CWORD]}"
1772 1773
	case "$cur" in
	--*)
1774
		__gitcomp "--merge --mixed --hard --soft"
1775 1776 1777 1778
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
1779 1780
}

1781 1782 1783 1784 1785 1786 1787 1788 1789
_git_revert ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
		return
		;;
	esac
1790
	__gitcomp "$(__git_refs)"
1791 1792
}

1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
_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=()
}

1807 1808
_git_shortlog ()
{
1809 1810
	__git_has_doubledash && return

1811 1812 1813 1814
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
1815 1816
			$__git_log_common_options
			$__git_log_shortlog_options
1817 1818 1819 1820 1821 1822 1823 1824
			--numbered --summary
			"
		return
		;;
	esac
	__git_complete_revlist
}

1825 1826
_git_show ()
{
1827 1828
	__git_has_doubledash && return

1829 1830 1831
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--pretty=*)
1832
		__gitcomp "$__git_log_pretty_formats
1833
			" "" "${cur##--pretty=}"
1834 1835
		return
		;;
1836 1837 1838 1839 1840
	--format=*)
		__gitcomp "$__git_log_pretty_formats
			" "" "${cur##--format=}"
		return
		;;
1841
	--*)
1842
		__gitcomp "--pretty= --format= --abbrev-commit --oneline
1843 1844
			$__git_diff_common_options
			"
1845 1846 1847 1848 1849 1850
		return
		;;
	esac
	__git_complete_file
}

1851 1852 1853 1854 1855 1856 1857 1858
_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
1859
			--color --no-color
1860
			--sha1-name --sparse --topics --reflog
1861 1862 1863 1864 1865 1866 1867
			"
		return
		;;
	esac
	__git_complete_revlist
}

J
Junio C Hamano 已提交
1868 1869
_git_stash ()
{
1870
	local subcommands='save list show apply clear drop pop create branch'
1871 1872
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1873
		__gitcomp "$subcommands"
1874 1875 1876 1877 1878 1879
	else
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$subcommand,$cur" in
		save,--*)
			__gitcomp "--keep-index"
			;;
1880 1881 1882
		apply,--*)
			__gitcomp "--index"
			;;
1883
		show,--*|drop,--*|pop,--*|branch,--*)
1884 1885 1886 1887 1888 1889
			COMPREPLY=()
			;;
		show,*|apply,*|drop,*|pop,*|branch,*)
			__gitcomp "$(git --git-dir="$(__gitdir)" stash list \
					| sed -n -e 's/:.*//p')"
			;;
1890 1891 1892 1893
		*)
			COMPREPLY=()
			;;
		esac
1894
	fi
J
Junio C Hamano 已提交
1895 1896
}

1897 1898
_git_submodule ()
{
1899 1900
	__git_has_doubledash && return

1901
	local subcommands="add status init update summary foreach sync"
1902
	if [ -z "$(__git_find_subcommand "$subcommands")" ]; then
1903 1904 1905 1906 1907 1908
		local cur="${COMP_WORDS[COMP_CWORD]}"
		case "$cur" in
		--*)
			__gitcomp "--quiet --cached"
			;;
		*)
1909
			__gitcomp "$subcommands"
1910 1911 1912 1913 1914 1915
			;;
		esac
		return
	fi
}

1916 1917 1918 1919 1920
_git_svn ()
{
	local subcommands="
		init fetch clone rebase dcommit log find-rev
		set-tree commit-diff info create-ignore propget
S
SZEDER Gábor 已提交
1921 1922
		proplist show-ignore show-externals branch tag blame
		migrate
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
		"
	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
S
SZEDER Gábor 已提交
1933 1934
			--repack-flags --use-log-author --localtime
			--ignore-paths= $remote_opts
1935 1936 1937 1938 1939
			"
		local init_opts="
			--template= --shared= --trunk= --tags=
			--branches= --stdlayout --minimize-url
			--no-metadata --use-svm-props --use-svnsync-props
S
SZEDER Gábor 已提交
1940 1941
			--rewrite-root= --prefix= --use-log-author
			--add-author-from $remote_opts
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
			"
		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
S
SZEDER Gábor 已提交
1961 1962
				--fetch-all --no-rebase --commit-url
				--revision $cmt_opts $fc_opts
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
				"
			;;
		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
S
SZEDER Gábor 已提交
1976
				--authors-file= --color
1977 1978 1979 1980 1981
				"
			;;
		rebase,--*)
			__gitcomp "
				--merge --verbose --strategy= --local
S
SZEDER Gábor 已提交
1982
				--fetch-all --dry-run $fc_opts
1983 1984 1985 1986 1987 1988 1989 1990
				"
			;;
		commit-diff,--*)
			__gitcomp "--message= --file= --revision= $cmt_opts"
			;;
		info,--*)
			__gitcomp "--url"
			;;
S
SZEDER Gábor 已提交
1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
		branch,--*)
			__gitcomp "--dry-run --message --tag"
			;;
		tag,--*)
			__gitcomp "--dry-run --message"
			;;
		blame,--*)
			__gitcomp "--git-format"
			;;
		migrate,--*)
			__gitcomp "
				--config-dir= --ignore-paths= --minimize
				--no-auth-cache --username=
				"
			;;
2006 2007 2008 2009 2010 2011 2012
		*)
			COMPREPLY=()
			;;
		esac
	fi
}

2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
_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=()
		;;
2034
	-*|tag)
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046
		if [ $f = 1 ]; then
			__gitcomp "$(__git_tags)"
		else
			COMPREPLY=()
		fi
		;;
	*)
		__gitcomp "$(__git_refs)"
		;;
	esac
}

2047 2048
_git ()
{
2049 2050 2051 2052 2053 2054 2055
	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="." ;;
2056 2057
		--version|-p|--paginate) ;;
		--help) command="help"; break ;;
2058 2059 2060 2061 2062
		*) command="$i"; break ;;
		esac
		c=$((++c))
	done

2063
	if [ -z "$command" ]; then
2064
		case "${COMP_WORDS[COMP_CWORD]}" in
2065
		--*)   __gitcomp "
2066
			--paginate
2067 2068 2069 2070 2071
			--no-pager
			--git-dir=
			--bare
			--version
			--exec-path
2072
			--html-path
2073 2074
			--work-tree=
			--help
2075 2076
			"
			;;
2077
		*)     __gitcomp "$(__git_porcelain_commands) $(__git_aliases)" ;;
2078 2079
		esac
		return
2080
	fi
2081

2082 2083
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
2084

2085
	case "$command" in
2086
	am)          _git_am ;;
2087
	add)         _git_add ;;
2088
	apply)       _git_apply ;;
2089
	archive)     _git_archive ;;
2090
	bisect)      _git_bisect ;;
2091
	bundle)      _git_bundle ;;
2092 2093
	branch)      _git_branch ;;
	checkout)    _git_checkout ;;
2094
	cherry)      _git_cherry ;;
2095
	cherry-pick) _git_cherry_pick ;;
2096
	clean)       _git_clean ;;
2097
	clone)       _git_clone ;;
2098
	commit)      _git_commit ;;
2099
	config)      _git_config ;;
2100
	describe)    _git_describe ;;
2101
	diff)        _git_diff ;;
2102
	difftool)    _git_difftool ;;
2103
	fetch)       _git_fetch ;;
2104
	format-patch) _git_format_patch ;;
2105
	fsck)        _git_fsck ;;
2106
	gc)          _git_gc ;;
2107
	grep)        _git_grep ;;
2108
	help)        _git_help ;;
2109
	init)        _git_init ;;
2110
	log)         _git_log ;;
2111
	ls-files)    _git_ls_files ;;
2112 2113
	ls-remote)   _git_ls_remote ;;
	ls-tree)     _git_ls_tree ;;
2114
	merge)       _git_merge;;
2115
	mergetool)   _git_mergetool;;
2116
	merge-base)  _git_merge_base ;;
2117
	mv)          _git_mv ;;
2118
	name-rev)    _git_name_rev ;;
2119 2120
	pull)        _git_pull ;;
	push)        _git_push ;;
2121
	rebase)      _git_rebase ;;
2122
	remote)      _git_remote ;;
2123
	reset)       _git_reset ;;
2124
	revert)      _git_revert ;;
2125
	rm)          _git_rm ;;
2126
	send-email)  _git_send_email ;;
2127
	shortlog)    _git_shortlog ;;
2128
	show)        _git_show ;;
2129
	show-branch) _git_show_branch ;;
J
Junio C Hamano 已提交
2130
	stash)       _git_stash ;;
2131
	stage)       _git_add ;;
2132
	submodule)   _git_submodule ;;
2133
	svn)         _git_svn ;;
2134
	tag)         _git_tag ;;
2135 2136 2137
	whatchanged) _git_log ;;
	*)           COMPREPLY=() ;;
	esac
2138 2139 2140 2141
}

_gitk ()
{
2142 2143
	__git_has_doubledash && return

2144
	local cur="${COMP_WORDS[COMP_CWORD]}"
2145
	local g="$(__gitdir)"
2146
	local merge=""
2147
	if [ -f "$g/MERGE_HEAD" ]; then
2148 2149
		merge="--merge"
	fi
2150 2151
	case "$cur" in
	--*)
2152 2153 2154 2155 2156
		__gitcomp "
			$__git_log_common_options
			$__git_log_gitk_options
			$merge
			"
2157 2158 2159
		return
		;;
	esac
2160
	__git_complete_revlist
2161 2162
}

2163 2164 2165 2166
complete -o bashdefault -o default -o nospace -F _git git 2>/dev/null \
	|| complete -o default -o nospace -F _git git
complete -o bashdefault -o default -o nospace -F _gitk gitk 2>/dev/null \
	|| complete -o default -o nospace -F _gitk gitk
2167 2168 2169 2170 2171

# 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.
#
2172
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
2173 2174
complete -o bashdefault -o default -o nospace -F _git git.exe 2>/dev/null \
	|| complete -o default -o nospace -F _git git.exe
2175
fi