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 cat-file) : plumbing;;
254 check-ref-format) : plumbing;;
255 commit-tree) : plumbing;;
256 convert-objects) : plumbing;;
257 cvsserver) : daemon;;
259 fetch-pack) : plumbing;;
260 hash-object) : plumbing;;
261 http-*) : transport;;
262 index-pack) : plumbing;;
263 local-fetch) : plumbing;;
264 mailinfo) : plumbing;;
265 mailsplit) : plumbing;;
266 merge-*) : plumbing;;
269 pack-objects) : plumbing;;
270 pack-redundant) : plumbing;;
271 pack-refs) : plumbing;;
272 parse-remote) : plumbing;;
273 patch-id) : plumbing;;
274 peek-remote) : plumbing;;
275 read-tree) : plumbing;;
276 receive-pack) : plumbing;;
279 rev-list) : plumbing;;
280 rev-parse) : plumbing;;
281 runstatus) : plumbing;;
282 sh-setup) : internal;;
284 send-pack) : plumbing;;
285 show-index) : plumbing;;
287 stripspace) : plumbing;;
288 symbolic-ref) : plumbing;;
289 unpack-file) : plumbing;;
290 unpack-objects) : plumbing;;
291 update-ref) : plumbing;;
292 update-server-info) : daemon;;
293 upload-archive) : plumbing;;
294 upload-pack) : plumbing;;
295 write-tree) : plumbing;;
301 __git_commandlist="$(__git_commands 2>/dev/null)"
306 for i in $(git --git-dir="$(__gitdir)" config --list); do
316 __git_aliased_command ()
318 local word cmdline=$(git --git-dir="$(__gitdir)" \
319 config --get "alias.$1")
320 for word in $cmdline; do
321 if [ "${word##-*}" ]; then
328 __git_whitespacelist="nowarn warn error error-all strip"
332 local cur="${COMP_WORDS[COMP_CWORD]}"
333 if [ -d .dotest ]; then
334 COMPREPLY=($(compgen -W "
341 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
342 -- "${cur##--whitespace=}"))
346 COMPREPLY=($(compgen -W "
347 --signoff --utf8 --binary --3way --interactive
357 local cur="${COMP_WORDS[COMP_CWORD]}"
360 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
361 -- "${cur##--whitespace=}"))
365 COMPREPLY=($(compgen -W "
366 --stat --numstat --summary --check --index
367 --cached --index-info --reverse --reject --unidiff-zero
368 --apply --no-add --exclude=
369 --whitespace= --inaccurate-eof --verbose
378 local cur="${COMP_WORDS[COMP_CWORD]}"
381 COMPREPLY=($(compgen -W "
391 local cur="${COMP_WORDS[COMP_CWORD]}"
392 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
397 local cur="${COMP_WORDS[COMP_CWORD]}"
398 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
403 local cur="${COMP_WORDS[COMP_CWORD]}"
406 COMPREPLY=($(compgen -W "
411 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
418 local cur="${COMP_WORDS[COMP_CWORD]}"
421 COMPREPLY=($(compgen -W "
422 --all --author= --signoff --verify --no-verify
423 --edit --amend --include --only
437 local cur="${COMP_WORDS[COMP_CWORD]}"
438 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
443 local cur="${COMP_WORDS[COMP_CWORD]}"
445 case "${COMP_WORDS[0]},$COMP_CWORD" in
447 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
450 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
456 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
460 case "${COMP_WORDS[0]}" in
461 git-fetch) remote="${COMP_WORDS[1]}" ;;
462 git) remote="${COMP_WORDS[2]}" ;;
464 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
473 local cur="${COMP_WORDS[COMP_CWORD]}"
476 COMPREPLY=($(compgen -W "
477 --stdout --attach --thread
479 --numbered --start-number
483 --full-index --binary
488 __git_complete_revlist
493 local cur="${COMP_WORDS[COMP_CWORD]}"
494 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
504 local cur="${COMP_WORDS[COMP_CWORD]}"
507 COMPREPLY=($(compgen -W "
508 oneline short medium full fuller email raw
509 " -- "${cur##--pretty=}"))
513 COMPREPLY=($(compgen -W "
514 --max-count= --max-age= --since= --after=
515 --min-age= --before= --until=
516 --root --not --topo-order --date-order
518 --abbrev-commit --abbrev=
520 --author= --committer= --grep=
522 --pretty= --name-status --name-only
527 __git_complete_revlist
532 local cur="${COMP_WORDS[COMP_CWORD]}"
533 case "${COMP_WORDS[COMP_CWORD-1]}" in
535 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
540 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
541 -- "${cur##--strategy=}"))
545 COMPREPLY=($(compgen -W "
546 --no-commit --no-summary --squash --strategy
550 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
555 local cur="${COMP_WORDS[COMP_CWORD]}"
556 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
561 local cur="${COMP_WORDS[COMP_CWORD]}"
562 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
567 local cur="${COMP_WORDS[COMP_CWORD]}"
569 case "${COMP_WORDS[0]},$COMP_CWORD" in
571 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
574 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
578 case "${COMP_WORDS[0]}" in
579 git-pull) remote="${COMP_WORDS[1]}" ;;
580 git) remote="${COMP_WORDS[2]}" ;;
582 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
589 local cur="${COMP_WORDS[COMP_CWORD]}"
591 case "${COMP_WORDS[0]},$COMP_CWORD" in
593 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
596 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
602 case "${COMP_WORDS[0]}" in
603 git-push) remote="${COMP_WORDS[1]}" ;;
604 git) remote="${COMP_WORDS[2]}" ;;
607 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
610 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
619 local cur="${COMP_WORDS[COMP_CWORD]}"
620 if [ -d .dotest ]; then
621 COMPREPLY=($(compgen -W "
622 --continue --skip --abort
626 case "${COMP_WORDS[COMP_CWORD-1]}" in
628 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
633 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
634 -- "${cur##--strategy=}"))
638 COMPREPLY=($(compgen -W "
639 --onto --merge --strategy
643 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
648 local cur="${COMP_WORDS[COMP_CWORD]}"
649 local prv="${COMP_WORDS[COMP_CWORD-1]}"
652 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
656 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
660 local remote="${prv#remote.}"
661 remote="${remote%.fetch}"
662 COMPREPLY=($(compgen -W "$(__git_refs_remotes "$remote")" \
667 local remote="${prv#remote.}"
668 remote="${remote%.push}"
669 COMPREPLY=($(compgen -W "$(git --git-dir="$(__gitdir)" \
670 for-each-ref --format='%(refname):%(refname)' \
671 refs/heads)" -- "$cur"))
681 COMPREPLY=($(compgen -W "
682 --global --list --replace-all
683 --get --get-all --get-regexp
689 local pfx="${cur%.*}."
691 COMPREPLY=($(compgen -P "$pfx" -W "remote merge" -- "$cur"))
695 local pfx="${cur%.*}."
697 COMPREPLY=($(compgen -P "$pfx" -S . \
698 -W "$(__git_heads)" -- "$cur"))
702 local pfx="${cur%.*}."
704 COMPREPLY=($(compgen -P "$pfx" -W "url fetch push" -- "$cur"))
708 local pfx="${cur%.*}."
710 COMPREPLY=($(compgen -P "$pfx" -S . \
711 -W "$(__git_remotes)" -- "$cur"))
715 COMPREPLY=($(compgen -W "
720 core.preferSymlinkRefs
721 core.logAllRefUpdates
722 core.repositoryFormatVersion
723 core.sharedRepository
724 core.warnAmbiguousRefs
728 i18n.logOutputEncoding
747 http.lowSpeedLimit http.lowSpeedTime
750 repack.useDeltaBaseOffset
751 pull.octopus pull.twohead
754 receive.denyNonFastForwards
765 local cur="${COMP_WORDS[COMP_CWORD]}"
766 local opt="--mixed --hard --soft"
767 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
772 local cur="${COMP_WORDS[COMP_CWORD]}"
775 COMPREPLY=($(compgen -W "
776 oneline short medium full fuller email raw
777 " -- "${cur##--pretty=}"))
781 COMPREPLY=($(compgen -W "--pretty=" -- "$cur"))
790 local i c=1 command __git_dir
792 while [ $c -lt $COMP_CWORD ]; do
795 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
796 --bare) __git_dir="." ;;
797 --version|--help|-p|--paginate) ;;
798 *) command="$i"; break ;;
803 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
804 case "${COMP_WORDS[COMP_CWORD]}" in
805 --*=*) COMPREPLY=() ;;
806 --*) __gitcomp "--git-dir= --bare --version --exec-path" ;;
807 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
812 local expansion=$(__git_aliased_command "$command")
813 [ "$expansion" ] && command="$expansion"
819 branch) _git_branch ;;
820 checkout) _git_checkout ;;
821 cherry-pick) _git_cherry_pick ;;
822 commit) _git_commit ;;
823 config) _git_config ;;
825 diff-tree) _git_diff_tree ;;
827 format-patch) _git_format_patch ;;
829 ls-remote) _git_ls_remote ;;
830 ls-tree) _git_ls_tree ;;
832 merge-base) _git_merge_base ;;
833 name-rev) _git_name_rev ;;
836 rebase) _git_rebase ;;
837 repo-config) _git_config ;;
840 show-branch) _git_log ;;
841 whatchanged) _git_log ;;
848 local cur="${COMP_WORDS[COMP_CWORD]}"
849 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
852 complete -o default -o nospace -F _git git
853 complete -o default -F _gitk gitk
854 complete -o default -F _git_am git-am
855 complete -o default -F _git_apply git-apply
856 complete -o default -F _git_branch git-branch
857 complete -o default -F _git_checkout git-checkout
858 complete -o default -F _git_cherry_pick git-cherry-pick
859 complete -o default -F _git_commit git-commit
860 complete -o default -o nospace -F _git_diff git-diff
861 complete -o default -F _git_diff_tree git-diff-tree
862 complete -o default -o nospace -F _git_fetch git-fetch
863 complete -o default -o nospace -F _git_format_patch git-format-patch
864 complete -o default -o nospace -F _git_log git-log
865 complete -o default -F _git_ls_remote git-ls-remote
866 complete -o default -o nospace -F _git_ls_tree git-ls-tree
867 complete -o default -F _git_merge git-merge
868 complete -o default -F _git_merge_base git-merge-base
869 complete -o default -F _git_name_rev git-name-rev
870 complete -o default -o nospace -F _git_pull git-pull
871 complete -o default -o nospace -F _git_push git-push
872 complete -o default -F _git_rebase git-rebase
873 complete -o default -F _git_config git-config
874 complete -o default -F _git_reset git-reset
875 complete -o default -o nospace -F _git_show git-show
876 complete -o default -o nospace -F _git_log git-show-branch
877 complete -o default -o nospace -F _git_log git-whatchanged
879 # The following are necessary only for Cygwin, and only are needed
880 # when the user has tab-completed the executable name and consequently
881 # included the '.exe' suffix.
883 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
884 complete -o default -F _git_add git-add.exe
885 complete -o default -F _git_apply git-apply.exe
886 complete -o default -o nospace -F _git git.exe
887 complete -o default -F _git_branch git-branch.exe
888 complete -o default -o nospace -F _git_diff git-diff.exe
889 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
890 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
891 complete -o default -o nospace -F _git_log git-log.exe
892 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
893 complete -o default -F _git_merge_base git-merge-base.exe
894 complete -o default -F _git_name_rev git-name-rev.exe
895 complete -o default -o nospace -F _git_push git-push.exe
896 complete -o default -F _git_config git-config
897 complete -o default -o nospace -F _git_show git-show.exe
898 complete -o default -o nospace -F _git_log git-show-branch.exe
899 complete -o default -o nospace -F _git_log git-whatchanged.exe