2 # bash completion support for core Git.
4 # Copyright (C) 2006 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 cmd i is_hash=y dir="$(__gitdir "$1")"
67 if [ -d "$dir" ]; then
68 for i in $(git --git-dir="$dir" \
69 for-each-ref --format='%(refname)' \
71 echo "${i#refs/heads/}"
75 for i in $(git-ls-remote "$1" 2>/dev/null); do
79 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
80 n,*) is_hash=y; echo "$i" ;;
87 local cmd i is_hash=y dir="$(__gitdir "$1")"
88 if [ -d "$dir" ]; then
89 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
90 for i in $(git --git-dir="$dir" \
91 for-each-ref --format='%(refname)' \
92 refs/tags refs/heads refs/remotes); do
94 refs/tags/*) echo "${i#refs/tags/}" ;;
95 refs/heads/*) echo "${i#refs/heads/}" ;;
96 refs/remotes/*) echo "${i#refs/remotes/}" ;;
102 for i in $(git-ls-remote "$dir" 2>/dev/null); do
103 case "$is_hash,$i" in
106 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
107 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
108 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
109 n,*) is_hash=y; echo "$i" ;;
117 for i in $(__git_refs "$1"); do
122 __git_refs_remotes ()
124 local cmd i is_hash=y
125 for i in $(git-ls-remote "$1" 2>/dev/null); do
126 case "$is_hash,$i" in
129 echo "$i:refs/remotes/$1/${i#refs/heads/}"
133 n,refs/tags/*) is_hash=y;;
141 local i ngoff IFS=$'\n' d="$(__gitdir)"
142 shopt -q nullglob || ngoff=1
144 for i in "$d/remotes"/*; do
145 echo ${i#$d/remotes/}
147 [ "$ngoff" ] && shopt -u nullglob
148 for i in $(git --git-dir="$d" repo-config --list); do
158 __git_merge_strategies ()
160 if [ -n "$__git_merge_strategylist" ]; then
161 echo "$__git_merge_strategylist"
164 sed -n "/^all_strategies='/{
165 s/^all_strategies='//
169 }" "$(git --exec-path)/git-merge"
171 __git_merge_strategylist=
172 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
174 __git_complete_file ()
176 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
192 COMPREPLY=($(compgen -P "$pfx" \
193 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
194 | sed '/^100... blob /s,^.* ,,
203 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
208 __git_complete_revlist ()
210 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
215 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
220 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
223 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
230 if [ -n "$__git_commandlist" ]; then
231 echo "$__git_commandlist"
235 for i in $(git help -a|egrep '^ ')
238 check-ref-format) : plumbing;;
239 commit-tree) : plumbing;;
240 convert-objects) : plumbing;;
241 cvsserver) : daemon;;
243 fetch-pack) : plumbing;;
244 hash-object) : plumbing;;
245 http-*) : transport;;
246 index-pack) : plumbing;;
247 local-fetch) : plumbing;;
248 mailinfo) : plumbing;;
249 mailsplit) : plumbing;;
250 merge-*) : plumbing;;
253 pack-objects) : plumbing;;
254 pack-redundant) : plumbing;;
255 pack-refs) : plumbing;;
256 parse-remote) : plumbing;;
257 patch-id) : plumbing;;
258 peek-remote) : plumbing;;
259 read-tree) : plumbing;;
260 receive-pack) : plumbing;;
262 rev-list) : plumbing;;
263 rev-parse) : plumbing;;
264 runstatus) : plumbing;;
265 sh-setup) : internal;;
267 send-pack) : plumbing;;
268 show-index) : plumbing;;
270 stripspace) : plumbing;;
271 symbolic-ref) : plumbing;;
272 unpack-file) : plumbing;;
273 unpack-objects) : plumbing;;
274 update-ref) : plumbing;;
275 update-server-info) : daemon;;
276 upload-archive) : plumbing;;
277 upload-pack) : plumbing;;
278 write-tree) : plumbing;;
284 __git_commandlist="$(__git_commands 2>/dev/null)"
289 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
299 __git_aliased_command ()
301 local word cmdline=$(git --git-dir="$(__gitdir)" \
302 repo-config --get "alias.$1")
303 for word in $cmdline; do
304 if [ "${word##-*}" ]; then
311 __git_whitespacelist="nowarn warn error error-all strip"
315 local cur="${COMP_WORDS[COMP_CWORD]}"
316 if [ -d .dotest ]; then
317 COMPREPLY=($(compgen -W "
324 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
325 -- "${cur##--whitespace=}"))
329 COMPREPLY=($(compgen -W "
330 --signoff --utf8 --binary --3way --interactive
340 local cur="${COMP_WORDS[COMP_CWORD]}"
343 COMPREPLY=($(compgen -W "$__git_whitespacelist" \
344 -- "${cur##--whitespace=}"))
348 COMPREPLY=($(compgen -W "
349 --stat --numstat --summary --check --index
350 --cached --index-info --reverse --reject --unidiff-zero
351 --apply --no-add --exclude=
352 --whitespace= --inaccurate-eof --verbose
361 local cur="${COMP_WORDS[COMP_CWORD]}"
362 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
367 local cur="${COMP_WORDS[COMP_CWORD]}"
368 case "${COMP_WORDS[0]},$COMP_CWORD" in
370 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
373 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
383 local cur="${COMP_WORDS[COMP_CWORD]}"
384 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
389 local cur="${COMP_WORDS[COMP_CWORD]}"
392 COMPREPLY=($(compgen -W "
397 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
404 local cur="${COMP_WORDS[COMP_CWORD]}"
407 COMPREPLY=($(compgen -W "
408 --all --author= --signoff --verify --no-verify
409 --edit --amend --include --only
423 local cur="${COMP_WORDS[COMP_CWORD]}"
424 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
429 local cur="${COMP_WORDS[COMP_CWORD]}"
431 case "${COMP_WORDS[0]},$COMP_CWORD" in
433 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
436 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
442 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
446 case "${COMP_WORDS[0]}" in
447 git-fetch) remote="${COMP_WORDS[1]}" ;;
448 git) remote="${COMP_WORDS[2]}" ;;
450 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
459 local cur="${COMP_WORDS[COMP_CWORD]}"
462 COMPREPLY=($(compgen -W "
463 --stdout --attach --thread
465 --numbered --start-number
469 --full-index --binary
474 __git_complete_revlist
479 local cur="${COMP_WORDS[COMP_CWORD]}"
480 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
490 local cur="${COMP_WORDS[COMP_CWORD]}"
493 COMPREPLY=($(compgen -W "
494 oneline short medium full fuller email raw
495 " -- "${cur##--pretty=}"))
499 COMPREPLY=($(compgen -W "
500 --max-count= --max-age= --since= --after=
501 --min-age= --before= --until=
502 --root --not --topo-order --date-order
504 --abbrev-commit --abbrev=
506 --author= --committer= --grep=
508 --pretty= --name-status --name-only
513 __git_complete_revlist
518 local cur="${COMP_WORDS[COMP_CWORD]}"
519 case "${COMP_WORDS[COMP_CWORD-1]}" in
521 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
526 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
527 -- "${cur##--strategy=}"))
531 COMPREPLY=($(compgen -W "
532 --no-commit --no-summary --squash --strategy
536 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
541 local cur="${COMP_WORDS[COMP_CWORD]}"
542 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
547 local cur="${COMP_WORDS[COMP_CWORD]}"
548 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
553 local cur="${COMP_WORDS[COMP_CWORD]}"
555 case "${COMP_WORDS[0]},$COMP_CWORD" in
557 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
560 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
564 case "${COMP_WORDS[0]}" in
565 git-pull) remote="${COMP_WORDS[1]}" ;;
566 git) remote="${COMP_WORDS[2]}" ;;
568 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
575 local cur="${COMP_WORDS[COMP_CWORD]}"
577 case "${COMP_WORDS[0]},$COMP_CWORD" in
579 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
582 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
588 case "${COMP_WORDS[0]}" in
589 git-push) remote="${COMP_WORDS[1]}" ;;
590 git) remote="${COMP_WORDS[2]}" ;;
593 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
596 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
605 local cur="${COMP_WORDS[COMP_CWORD]}"
606 if [ -d .dotest ]; then
607 COMPREPLY=($(compgen -W "
608 --continue --skip --abort
612 case "${COMP_WORDS[COMP_CWORD-1]}" in
614 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
619 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
620 -- "${cur##--strategy=}"))
624 COMPREPLY=($(compgen -W "
625 --onto --merge --strategy
629 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
634 local cur="${COMP_WORDS[COMP_CWORD]}"
635 local prv="${COMP_WORDS[COMP_CWORD-1]}"
638 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
642 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
646 local remote="${prv#remote.}"
647 remote="${remote%.fetch}"
648 COMPREPLY=($(compgen -W "$(__git_refs_remotes "$remote")" \
653 local remote="${prv#remote.}"
654 remote="${remote%.push}"
655 COMPREPLY=($(compgen -W "$(git --git-dir="$(__gitdir)" \
656 for-each-ref --format='%(refname):%(refname)' \
657 refs/heads)" -- "$cur"))
667 COMPREPLY=($(compgen -W "
668 --global --list --replace-all
669 --get --get-all --get-regexp
675 local pfx="${cur%.*}."
677 COMPREPLY=($(compgen -P "$pfx" -W "remote merge" -- "$cur"))
681 local pfx="${cur%.*}."
683 COMPREPLY=($(compgen -P "$pfx" -S . \
684 -W "$(__git_heads)" -- "$cur"))
688 local pfx="${cur%.*}."
690 COMPREPLY=($(compgen -P "$pfx" -W "url fetch push" -- "$cur"))
694 local pfx="${cur%.*}."
696 COMPREPLY=($(compgen -P "$pfx" -S . \
697 -W "$(__git_remotes)" -- "$cur"))
701 COMPREPLY=($(compgen -W "
706 core.preferSymlinkRefs
707 core.logAllRefUpdates
708 core.repositoryFormatVersion
709 core.sharedRepository
710 core.warnAmbiguousRefs
714 i18n.logOutputEncoding
733 http.lowSpeedLimit http.lowSpeedTime
736 repack.useDeltaBaseOffset
737 pull.octopus pull.twohead
740 receive.denyNonFastForwards
751 local cur="${COMP_WORDS[COMP_CWORD]}"
752 local opt="--mixed --hard --soft"
753 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
758 local cur="${COMP_WORDS[COMP_CWORD]}"
761 COMPREPLY=($(compgen -W "
762 oneline short medium full fuller email raw
763 " -- "${cur##--pretty=}"))
767 COMPREPLY=($(compgen -W "--pretty=" -- "$cur"))
776 local i c=1 command __git_dir
778 while [ $c -lt $COMP_CWORD ]; do
781 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
782 --bare) __git_dir="." ;;
783 --version|--help|-p|--paginate) ;;
784 *) command="$i"; break ;;
789 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
790 COMPREPLY=($(compgen -W "
791 --git-dir= --version --exec-path
794 " -- "${COMP_WORDS[COMP_CWORD]}"))
798 local expansion=$(__git_aliased_command "$command")
799 [ "$expansion" ] && command="$expansion"
804 branch) _git_branch ;;
805 cat-file) _git_cat_file ;;
806 checkout) _git_checkout ;;
807 cherry-pick) _git_cherry_pick ;;
808 commit) _git_commit ;;
810 diff-tree) _git_diff_tree ;;
812 format-patch) _git_format_patch ;;
814 ls-remote) _git_ls_remote ;;
815 ls-tree) _git_ls_tree ;;
817 merge-base) _git_merge_base ;;
818 name-rev) _git_name_rev ;;
821 rebase) _git_rebase ;;
822 repo-config) _git_repo_config ;;
825 show-branch) _git_log ;;
826 whatchanged) _git_log ;;
833 local cur="${COMP_WORDS[COMP_CWORD]}"
834 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
837 complete -o default -o nospace -F _git git
838 complete -o default -F _gitk gitk
839 complete -o default -F _git_am git-am
840 complete -o default -F _git_apply git-apply
841 complete -o default -F _git_branch git-branch
842 complete -o default -o nospace -F _git_cat_file git-cat-file
843 complete -o default -F _git_checkout git-checkout
844 complete -o default -F _git_cherry_pick git-cherry-pick
845 complete -o default -F _git_commit git-commit
846 complete -o default -o nospace -F _git_diff git-diff
847 complete -o default -F _git_diff_tree git-diff-tree
848 complete -o default -o nospace -F _git_fetch git-fetch
849 complete -o default -o nospace -F _git_format_patch git-format-patch
850 complete -o default -o nospace -F _git_log git-log
851 complete -o default -F _git_ls_remote git-ls-remote
852 complete -o default -o nospace -F _git_ls_tree git-ls-tree
853 complete -o default -F _git_merge git-merge
854 complete -o default -F _git_merge_base git-merge-base
855 complete -o default -F _git_name_rev git-name-rev
856 complete -o default -o nospace -F _git_pull git-pull
857 complete -o default -o nospace -F _git_push git-push
858 complete -o default -F _git_rebase git-rebase
859 complete -o default -F _git_repo_config git-repo-config
860 complete -o default -F _git_reset git-reset
861 complete -o default -o nospace -F _git_show git-show
862 complete -o default -o nospace -F _git_log git-show-branch
863 complete -o default -o nospace -F _git_log git-whatchanged
865 # The following are necessary only for Cygwin, and only are needed
866 # when the user has tab-completed the executable name and consequently
867 # included the '.exe' suffix.
869 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
870 complete -o default -F _git_apply git-apply.exe
871 complete -o default -o nospace -F _git git.exe
872 complete -o default -F _git_branch git-branch.exe
873 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
874 complete -o default -o nospace -F _git_diff git-diff.exe
875 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
876 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
877 complete -o default -o nospace -F _git_log git-log.exe
878 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
879 complete -o default -F _git_merge_base git-merge-base.exe
880 complete -o default -F _git_name_rev git-name-rev.exe
881 complete -o default -o nospace -F _git_push git-push.exe
882 complete -o default -F _git_repo_config git-repo-config
883 complete -o default -o nospace -F _git_show git-show.exe
884 complete -o default -o nospace -F _git_log git-show-branch.exe
885 complete -o default -o nospace -F _git_log git-whatchanged.exe