git-completion.bash 42.5 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
#       You can also see if currently something is stashed, by setting
#       GIT_PS1_SHOWSTASHSTATE to a nonempty value. If something is stashed,
#       then a '$' will be shown next to the branch name.
#
47 48 49 50 51 52 53 54 55 56 57
# 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
#
58

59 60 61 62 63
case "$COMP_WORDBREAKS" in
*:*) : great ;;
*)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
esac

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

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

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

				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)" ||

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

132 133
		local w
		local i
134
		local s
135
		local c
136

137
		if [ "true" = "$(git rev-parse --is-inside-git-dir 2>/dev/null)" ]; then
138
			if [ "true" = "$(git rev-parse --is-bare-repository 2>/dev/null)" ]; then
139 140 141 142
				c="BARE:"
			else
				b="GIT_DIR!"
			fi
143 144 145 146 147 148 149 150 151 152 153
		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
154 155
				fi
			fi
156 157 158
			if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ]; then
			        git rev-parse --verify refs/stash >/dev/null 2>&1 && s="$"
			fi
159 160
		fi

161
		if [ -n "${1-}" ]; then
162
			printf "$1" "$c${b##refs/heads/}$w$i$s$r"
163
		else
164
			printf " (%s)" "$c${b##refs/heads/}$w$i$s$r"
165 166 167 168
		fi
	fi
}

169
# __gitcomp_1 requires 2 arguments
170 171 172 173 174 175 176 177 178 179 180 181
__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
}

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

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

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

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

274
# __git_refs2 requires 1 argument (to pass to __git_refs)
275 276
__git_refs2 ()
{
277 278 279
	local i
	for i in $(__git_refs "$1"); do
		echo "$i:$i"
280 281 282
	done
}

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

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

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

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

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

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

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

406 407 408 409
__git_complete_remote_or_refspec ()
{
	local cmd="${COMP_WORDS[1]}"
	local cur="${COMP_WORDS[COMP_CWORD]}"
410
	local i c=2 remote="" pfx="" lhs=1 no_complete_refspec=0
411 412 413
	while [ $c -lt $COMP_CWORD ]; do
		i="${COMP_WORDS[c]}"
		case "$i" in
414
		--all|--mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
415 416 417 418 419 420 421 422 423
		-*) ;;
		*) remote="$i"; break ;;
		esac
		c=$((++c))
	done
	if [ -z "$remote" ]; then
		__gitcomp "$(__git_remotes)"
		return
	fi
424 425 426 427
	if [ $no_complete_refspec = 1 ]; then
		COMPREPLY=()
		return
	fi
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 464 465 466 467
	[ "$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
}

468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
__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
}

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

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

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

620
# __git_find_subcommand requires 1 argument
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
__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
}

637 638 639 640 641 642 643 644 645 646 647 648
__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
}

649
__git_whitespacelist="nowarn warn error error-all fix"
650 651 652

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

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

694 695
_git_add ()
{
696 697
	__git_has_doubledash && return

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

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

733 734
_git_bisect ()
{
735 736
	__git_has_doubledash && return

737
	local subcommands="start bad good skip reset visualize replay log run"
738 739 740
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
		__gitcomp "$subcommands"
741 742 743
		return
	fi

744
	case "$subcommand" in
745
	bad|good|reset|skip)
746 747 748 749 750 751 752 753
		__gitcomp "$(__git_refs)"
		;;
	*)
		COMPREPLY=()
		;;
	esac
}

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

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

804 805
_git_checkout ()
{
806 807
	__git_has_doubledash && return

808
	__gitcomp "$(__git_refs)"
809 810
}

811 812 813 814 815
_git_cherry ()
{
	__gitcomp "$(__git_refs)"
}

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

829 830 831 832 833 834 835 836 837 838 839 840 841 842
_git_clean ()
{
	__git_has_doubledash && return

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

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

868 869
_git_commit ()
{
870 871
	__git_has_doubledash && return

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

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

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

_git_diff ()
{
	__git_has_doubledash && return

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

929
__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
930
			tkdiff vimdiff gvimdiff xxdiff araxis
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
"

_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=()
}

949 950 951 952 953
__git_fetch_options="
	--quiet --verbose --append --upload-pack --force --keep --depth=
	--tags --no-tags
"

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

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

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
_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=()
}

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

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

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

1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
_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=()
}

1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
_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=()
}

1103 1104
_git_ls_remote ()
{
1105
	__gitcomp "$(__git_remotes)"
1106 1107 1108 1109 1110 1111 1112
}

_git_ls_tree ()
{
	__git_complete_file
}

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
# 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
"

1134
__git_log_pretty_formats="oneline short medium full fuller email raw format:"
1135
__git_log_date_formats="relative iso8601 rfc2822 short local default raw"
1136

1137 1138
_git_log ()
{
1139 1140
	__git_has_doubledash && return

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

1187 1188 1189 1190 1191
__git_merge_options="
	--no-commit --no-stat --log --no-log --squash --strategy
	--commit --stat --no-squash --ff --no-ff
"

1192 1193
_git_merge ()
{
1194 1195
	__git_complete_strategy && return

1196 1197 1198
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
1199
		__gitcomp "$__git_merge_options"
1200 1201
		return
	esac
1202
	__gitcomp "$(__git_refs)"
1203 1204
}

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

1221 1222
_git_merge_base ()
{
1223
	__gitcomp "$(__git_refs)"
1224 1225
}

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

1238 1239
_git_name_rev ()
{
1240
	__gitcomp "--tags --all --stdin"
1241 1242
}

1243 1244
_git_pull ()
{
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
	__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
1258
	__git_complete_remote_or_refspec
1259 1260 1261 1262
}

_git_push ()
{
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	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
1282
	__git_complete_remote_or_refspec
1283 1284
}

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

1301
__git_send_email_confirm_options="always never auto cc compose"
1302
__git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
1303

1304 1305 1306 1307
_git_send_email ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
	--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
		;;
1325
	--*)
1326
		__gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
1327 1328
			--compose --confirm= --dry-run --envelope-sender
			--from --identity
1329 1330 1331
			--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
1332 1333
			--smtp-server-port --smtp-encryption= --smtp-user
			--subject --suppress-cc= --suppress-from --thread --to
1334
			--validate --no-validate"
1335 1336 1337 1338 1339 1340
		return
		;;
	esac
	COMPREPLY=()
}

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 1366 1367 1368 1369
__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
}

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

1741 1742
_git_remote ()
{
1743
	local subcommands="add rename rm show prune update set-head"
1744 1745
	local subcommand="$(__git_find_subcommand "$subcommands")"
	if [ -z "$subcommand" ]; then
1746
		__gitcomp "$subcommands"
1747 1748 1749
		return
	fi

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

1772 1773
_git_reset ()
{
1774 1775
	__git_has_doubledash && return

1776
	local cur="${COMP_WORDS[COMP_CWORD]}"
1777 1778
	case "$cur" in
	--*)
1779
		__gitcomp "--merge --mixed --hard --soft"
1780 1781 1782 1783
		return
		;;
	esac
	__gitcomp "$(__git_refs)"
1784 1785
}

1786 1787 1788 1789 1790 1791 1792 1793 1794
_git_revert ()
{
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "--edit --mainline --no-edit --no-commit --signoff"
		return
		;;
	esac
1795
	__gitcomp "$(__git_refs)"
1796 1797
}

1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811
_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=()
}

1812 1813
_git_shortlog ()
{
1814 1815
	__git_has_doubledash && return

1816 1817 1818 1819
	local cur="${COMP_WORDS[COMP_CWORD]}"
	case "$cur" in
	--*)
		__gitcomp "
1820 1821
			$__git_log_common_options
			$__git_log_shortlog_options
1822 1823 1824 1825 1826 1827 1828 1829
			--numbered --summary
			"
		return
		;;
	esac
	__git_complete_revlist
}

1830 1831
_git_show ()
{
1832 1833
	__git_has_doubledash && return

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

1856 1857 1858 1859 1860 1861 1862 1863
_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
1864
			--color --no-color
1865
			--sha1-name --sparse --topics --reflog
1866 1867 1868 1869 1870 1871 1872
			"
		return
		;;
	esac
	__git_complete_revlist
}

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

1902 1903
_git_submodule ()
{
1904 1905
	__git_has_doubledash && return

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

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

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

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

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

2087 2088
	local expansion=$(__git_aliased_command "$command")
	[ "$expansion" ] && command="$expansion"
2089

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

_gitk ()
{
2147 2148
	__git_has_doubledash && return

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

2168 2169 2170 2171
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
2172 2173 2174 2175 2176

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