2 # bash completion support for core Git.
4 # Copyright (C) 2006,2007 Shawn Pearce
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
7 # The contained completion routines provide support for completing:
9 # *) local and remote branch names
10 # *) local and remote tag names
11 # *) .git/remotes file names
12 # *) git 'subcommands'
13 # *) tree paths within 'ref:path/to/file' expressions
15 # To use these routines:
17 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
18 # 2) Added the following line to your .bashrc:
19 # source ~/.git-completion.sh
21 # 3) You may want to make sure the git executable is available
22 # in your PATH before this script is sourced, as some caching
23 # is performed while the script loads. If git isn't found
24 # at source time then all lookups will be done on demand,
25 # which may be slightly slower.
27 # 4) Consider changing your PS1 to also show the current branch:
28 # PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
30 # The argument to __git_ps1 will be displayed only if you
31 # are currently in a git repository. The %s token will be
32 # the name of the current branch.
38 if [ -n "$__git_dir" ]; then
40 elif [ -d .git ]; then
43 git rev-parse --git-dir 2>/dev/null
45 elif [ -d "$1/.git" ]; then
54 local b="$(git symbolic-ref HEAD 2>/dev/null)"
57 printf "$1" "${b##refs/heads/}"
59 printf " (%s)" "${b##refs/heads/}"
66 local all c s=$'\n' IFS=' '$'\t'$'\n'
69 --*=*) all="$all$c$s" ;;
74 COMPREPLY=($(compgen -W "$all" -- "${COMP_WORDS[COMP_CWORD]}"))
80 local cmd i is_hash=y dir="$(__gitdir "$1")"
81 if [ -d "$dir" ]; then
82 for i in $(git --git-dir="$dir" \
83 for-each-ref --format='%(refname)' \
85 echo "${i#refs/heads/}"
89 for i in $(git-ls-remote "$1" 2>/dev/null); do
93 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
94 n,*) is_hash=y; echo "$i" ;;
101 local cmd i is_hash=y dir="$(__gitdir "$1")"
102 if [ -d "$dir" ]; then
103 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
104 for i in $(git --git-dir="$dir" \
105 for-each-ref --format='%(refname)' \
106 refs/tags refs/heads refs/remotes); do
108 refs/tags/*) echo "${i#refs/tags/}" ;;
109 refs/heads/*) echo "${i#refs/heads/}" ;;
110 refs/remotes/*) echo "${i#refs/remotes/}" ;;
116 for i in $(git-ls-remote "$dir" 2>/dev/null); do
117 case "$is_hash,$i" in
120 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
121 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
122 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
123 n,*) is_hash=y; echo "$i" ;;
131 for i in $(__git_refs "$1"); do
136 __git_refs_remotes ()
138 local cmd i is_hash=y
139 for i in $(git-ls-remote "$1" 2>/dev/null); do
140 case "$is_hash,$i" in
143 echo "$i:refs/remotes/$1/${i#refs/heads/}"
147 n,refs/tags/*) is_hash=y;;
155 local i ngoff IFS=$'\n' d="$(__gitdir)"
156 shopt -q nullglob || ngoff=1
158 for i in "$d/remotes"/*; do
159 echo ${i#$d/remotes/}
161 [ "$ngoff" ] && shopt -u nullglob
162 for i in $(git --git-dir="$d" config --list); do
172 __git_merge_strategies ()
174 if [ -n "$__git_merge_strategylist" ]; then
175 echo "$__git_merge_strategylist"
178 sed -n "/^all_strategies='/{
179 s/^all_strategies='//
183 }" "$(git --exec-path)/git-merge"
185 __git_merge_strategylist=
186 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
188 __git_complete_file ()
190 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
206 COMPREPLY=($(compgen -P "$pfx" \
207 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
208 | sed '/^100... blob /s,^.* ,,
217 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
222 __git_complete_revlist ()
224 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
229 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
234 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
237 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
244 if [ -n "$__git_commandlist" ]; then
245 echo "$__git_commandlist"
249 for i in $(git help -a|egrep '^ ')
252 add--interactive) : plumbing;;
253 applymbox) : ask gittus;;
254 applypatch) : ask gittus;;
255 archimport) : import;;
256 cat-file) : plumbing;;
257 check-ref-format) : plumbing;;
258 commit-tree) : plumbing;;
259 convert-objects) : plumbing;;
260 cvsexportcommit) : export;;
261 cvsimport) : import;;
262 cvsserver) : daemon;;
264 fsck-objects) : plumbing;;
265 fetch-pack) : plumbing;;
266 fmt-merge-msg) : plumbing;;
267 hash-object) : plumbing;;
268 http-*) : transport;;
269 index-pack) : plumbing;;
270 init-db) : deprecated;;
271 local-fetch) : plumbing;;
272 mailinfo) : plumbing;;
273 mailsplit) : plumbing;;
274 merge-*) : plumbing;;
277 pack-objects) : plumbing;;
278 pack-redundant) : plumbing;;
279 pack-refs) : plumbing;;
280 parse-remote) : plumbing;;
281 patch-id) : plumbing;;
282 peek-remote) : plumbing;;
284 prune-packed) : plumbing;;
285 quiltimport) : import;;
286 read-tree) : plumbing;;
287 receive-pack) : plumbing;;
289 repo-config) : plumbing;;
291 rev-list) : plumbing;;
292 rev-parse) : plumbing;;
293 runstatus) : plumbing;;
294 sh-setup) : internal;;
296 send-pack) : plumbing;;
297 show-index) : plumbing;;
299 stripspace) : plumbing;;
300 svn) : import export;;
301 svnimport) : import;;
302 symbolic-ref) : plumbing;;
303 tar-tree) : deprecated;;
304 unpack-file) : plumbing;;
305 unpack-objects) : plumbing;;
306 update-index) : plumbing;;
307 update-ref) : plumbing;;
308 update-server-info) : daemon;;
309 upload-archive) : plumbing;;
310 upload-pack) : plumbing;;
311 write-tree) : plumbing;;
312 verify-tag) : plumbing;;
318 __git_commandlist="$(__git_commands 2>/dev/null)"
323 for i in $(git --git-dir="$(__gitdir)" config --list); do
333 __git_aliased_command ()
335 local word cmdline=$(git --git-dir="$(__gitdir)" \
336 config --get "alias.$1")
337 for word in $cmdline; do
338 if [ "${word##-*}" ]; then
345 __git_whitespacelist="nowarn warn error error-all strip"
349 local cur="${COMP_WORDS[COMP_CWORD]}"
350 if [ -d .dotest ]; then
351 COMPREPLY=($(compgen -W "
358 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
359 -- "${cur##--whitespace=}"))
363 COMPREPLY=($(compgen -W "
364 --signoff --utf8 --binary --3way --interactive
374 local cur="${COMP_WORDS[COMP_CWORD]}"
377 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
378 -- "${cur##--whitespace=}"))
382 COMPREPLY=($(compgen -W "
383 --stat --numstat --summary --check --index
384 --cached --index-info --reverse --reject --unidiff-zero
385 --apply --no-add --exclude=
386 --whitespace= --inaccurate-eof --verbose
395 local cur="${COMP_WORDS[COMP_CWORD]}"
398 COMPREPLY=($(compgen -W "
408 local cur="${COMP_WORDS[COMP_CWORD]}"
409 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
414 local cur="${COMP_WORDS[COMP_CWORD]}"
415 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
420 local cur="${COMP_WORDS[COMP_CWORD]}"
423 COMPREPLY=($(compgen -W "
428 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
435 local cur="${COMP_WORDS[COMP_CWORD]}"
438 COMPREPLY=($(compgen -W "
439 --all --author= --signoff --verify --no-verify
440 --edit --amend --include --only
454 local cur="${COMP_WORDS[COMP_CWORD]}"
455 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
460 local cur="${COMP_WORDS[COMP_CWORD]}"
462 case "${COMP_WORDS[0]},$COMP_CWORD" in
464 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
467 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
473 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
477 case "${COMP_WORDS[0]}" in
478 git-fetch) remote="${COMP_WORDS[1]}" ;;
479 git) remote="${COMP_WORDS[2]}" ;;
481 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
490 local cur="${COMP_WORDS[COMP_CWORD]}"
493 COMPREPLY=($(compgen -W "
494 --stdout --attach --thread
496 --numbered --start-number
500 --full-index --binary
505 __git_complete_revlist
510 local cur="${COMP_WORDS[COMP_CWORD]}"
511 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
521 local cur="${COMP_WORDS[COMP_CWORD]}"
524 COMPREPLY=($(compgen -W "
525 oneline short medium full fuller email raw
526 " -- "${cur##--pretty=}"))
530 COMPREPLY=($(compgen -W "
531 --max-count= --max-age= --since= --after=
532 --min-age= --before= --until=
533 --root --not --topo-order --date-order
535 --abbrev-commit --abbrev=
537 --author= --committer= --grep=
539 --pretty= --name-status --name-only
544 __git_complete_revlist
549 local cur="${COMP_WORDS[COMP_CWORD]}"
550 case "${COMP_WORDS[COMP_CWORD-1]}" in
552 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
557 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
558 -- "${cur##--strategy=}"))
562 COMPREPLY=($(compgen -W "
563 --no-commit --no-summary --squash --strategy
567 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
572 local cur="${COMP_WORDS[COMP_CWORD]}"
573 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
578 local cur="${COMP_WORDS[COMP_CWORD]}"
579 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
584 local cur="${COMP_WORDS[COMP_CWORD]}"
586 case "${COMP_WORDS[0]},$COMP_CWORD" in
588 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
591 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
595 case "${COMP_WORDS[0]}" in
596 git-pull) remote="${COMP_WORDS[1]}" ;;
597 git) remote="${COMP_WORDS[2]}" ;;
599 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
606 local cur="${COMP_WORDS[COMP_CWORD]}"
608 case "${COMP_WORDS[0]},$COMP_CWORD" in
610 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
613 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
619 case "${COMP_WORDS[0]}" in
620 git-push) remote="${COMP_WORDS[1]}" ;;
621 git) remote="${COMP_WORDS[2]}" ;;
624 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
627 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
636 local cur="${COMP_WORDS[COMP_CWORD]}"
637 if [ -d .dotest ]; then
638 COMPREPLY=($(compgen -W "
639 --continue --skip --abort
643 case "${COMP_WORDS[COMP_CWORD-1]}" in
645 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
650 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
651 -- "${cur##--strategy=}"))
655 COMPREPLY=($(compgen -W "
656 --onto --merge --strategy
660 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
665 local cur="${COMP_WORDS[COMP_CWORD]}"
666 local prv="${COMP_WORDS[COMP_CWORD-1]}"
669 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
673 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
677 local remote="${prv#remote.}"
678 remote="${remote%.fetch}"
679 COMPREPLY=($(compgen -W "$(__git_refs_remotes "$remote")" \
684 local remote="${prv#remote.}"
685 remote="${remote%.push}"
686 COMPREPLY=($(compgen -W "$(git --git-dir="$(__gitdir)" \
687 for-each-ref --format='%(refname):%(refname)' \
688 refs/heads)" -- "$cur"))
698 COMPREPLY=($(compgen -W "
699 --global --list --replace-all
700 --get --get-all --get-regexp
706 local pfx="${cur%.*}."
708 COMPREPLY=($(compgen -P "$pfx" -W "remote merge" -- "$cur"))
712 local pfx="${cur%.*}."
714 COMPREPLY=($(compgen -P "$pfx" -S . \
715 -W "$(__git_heads)" -- "$cur"))
719 local pfx="${cur%.*}."
721 COMPREPLY=($(compgen -P "$pfx" -W "url fetch push" -- "$cur"))
725 local pfx="${cur%.*}."
727 COMPREPLY=($(compgen -P "$pfx" -S . \
728 -W "$(__git_remotes)" -- "$cur"))
732 COMPREPLY=($(compgen -W "
737 core.preferSymlinkRefs
738 core.logAllRefUpdates
739 core.repositoryFormatVersion
740 core.sharedRepository
741 core.warnAmbiguousRefs
745 i18n.logOutputEncoding
764 http.lowSpeedLimit http.lowSpeedTime
767 repack.useDeltaBaseOffset
768 pull.octopus pull.twohead
771 receive.denyNonFastForwards
782 local cur="${COMP_WORDS[COMP_CWORD]}"
783 local opt="--mixed --hard --soft"
784 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
789 local cur="${COMP_WORDS[COMP_CWORD]}"
792 COMPREPLY=($(compgen -W "
793 oneline short medium full fuller email raw
794 " -- "${cur##--pretty=}"))
798 COMPREPLY=($(compgen -W "--pretty=" -- "$cur"))
807 local i c=1 command __git_dir
809 while [ $c -lt $COMP_CWORD ]; do
812 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
813 --bare) __git_dir="." ;;
814 --version|--help|-p|--paginate) ;;
815 *) command="$i"; break ;;
820 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
821 case "${COMP_WORDS[COMP_CWORD]}" in
822 --*=*) COMPREPLY=() ;;
823 --*) __gitcomp "--git-dir= --bare --version --exec-path" ;;
824 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
829 local expansion=$(__git_aliased_command "$command")
830 [ "$expansion" ] && command="$expansion"
836 branch) _git_branch ;;
837 checkout) _git_checkout ;;
838 cherry-pick) _git_cherry_pick ;;
839 commit) _git_commit ;;
840 config) _git_config ;;
842 diff-tree) _git_diff_tree ;;
844 format-patch) _git_format_patch ;;
846 ls-remote) _git_ls_remote ;;
847 ls-tree) _git_ls_tree ;;
849 merge-base) _git_merge_base ;;
850 name-rev) _git_name_rev ;;
853 rebase) _git_rebase ;;
856 show-branch) _git_log ;;
857 whatchanged) _git_log ;;
864 local cur="${COMP_WORDS[COMP_CWORD]}"
865 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
868 complete -o default -o nospace -F _git git
869 complete -o default -F _gitk gitk
870 complete -o default -F _git_am git-am
871 complete -o default -F _git_apply git-apply
872 complete -o default -F _git_branch git-branch
873 complete -o default -F _git_checkout git-checkout
874 complete -o default -F _git_cherry_pick git-cherry-pick
875 complete -o default -F _git_commit git-commit
876 complete -o default -o nospace -F _git_diff git-diff
877 complete -o default -F _git_diff_tree git-diff-tree
878 complete -o default -o nospace -F _git_fetch git-fetch
879 complete -o default -o nospace -F _git_format_patch git-format-patch
880 complete -o default -o nospace -F _git_log git-log
881 complete -o default -F _git_ls_remote git-ls-remote
882 complete -o default -o nospace -F _git_ls_tree git-ls-tree
883 complete -o default -F _git_merge git-merge
884 complete -o default -F _git_merge_base git-merge-base
885 complete -o default -F _git_name_rev git-name-rev
886 complete -o default -o nospace -F _git_pull git-pull
887 complete -o default -o nospace -F _git_push git-push
888 complete -o default -F _git_rebase git-rebase
889 complete -o default -F _git_config git-config
890 complete -o default -F _git_reset git-reset
891 complete -o default -o nospace -F _git_show git-show
892 complete -o default -o nospace -F _git_log git-show-branch
893 complete -o default -o nospace -F _git_log git-whatchanged
895 # The following are necessary only for Cygwin, and only are needed
896 # when the user has tab-completed the executable name and consequently
897 # included the '.exe' suffix.
899 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
900 complete -o default -F _git_add git-add.exe
901 complete -o default -F _git_apply git-apply.exe
902 complete -o default -o nospace -F _git git.exe
903 complete -o default -F _git_branch git-branch.exe
904 complete -o default -o nospace -F _git_diff git-diff.exe
905 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
906 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
907 complete -o default -o nospace -F _git_log git-log.exe
908 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
909 complete -o default -F _git_merge_base git-merge-base.exe
910 complete -o default -F _git_name_rev git-name-rev.exe
911 complete -o default -o nospace -F _git_push git-push.exe
912 complete -o default -F _git_config git-config
913 complete -o default -o nospace -F _git_show git-show.exe
914 complete -o default -o nospace -F _git_log git-show-branch.exe
915 complete -o default -o nospace -F _git_log git-whatchanged.exe