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'
67 local cur="${COMP_WORDS[COMP_CWORD]}"
73 --*=*) all="$all$c$4$s" ;;
74 *.) all="$all$c$4$s" ;;
75 *) all="$all$c$4 $s" ;;
79 COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
85 local cmd i is_hash=y dir="$(__gitdir "$1")"
86 if [ -d "$dir" ]; then
87 for i in $(git --git-dir="$dir" \
88 for-each-ref --format='%(refname)' \
90 echo "${i#refs/heads/}"
94 for i in $(git-ls-remote "$1" 2>/dev/null); do
98 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
99 n,*) is_hash=y; echo "$i" ;;
106 local cmd i is_hash=y dir="$(__gitdir "$1")"
107 if [ -d "$dir" ]; then
108 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
109 for i in $(git --git-dir="$dir" \
110 for-each-ref --format='%(refname)' \
111 refs/tags refs/heads refs/remotes); do
113 refs/tags/*) echo "${i#refs/tags/}" ;;
114 refs/heads/*) echo "${i#refs/heads/}" ;;
115 refs/remotes/*) echo "${i#refs/remotes/}" ;;
121 for i in $(git-ls-remote "$dir" 2>/dev/null); do
122 case "$is_hash,$i" in
125 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
126 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
127 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
128 n,*) is_hash=y; echo "$i" ;;
136 for i in $(__git_refs "$1"); do
141 __git_refs_remotes ()
143 local cmd i is_hash=y
144 for i in $(git-ls-remote "$1" 2>/dev/null); do
145 case "$is_hash,$i" in
148 echo "$i:refs/remotes/$1/${i#refs/heads/}"
152 n,refs/tags/*) is_hash=y;;
160 local i ngoff IFS=$'\n' d="$(__gitdir)"
161 shopt -q nullglob || ngoff=1
163 for i in "$d/remotes"/*; do
164 echo ${i#$d/remotes/}
166 [ "$ngoff" ] && shopt -u nullglob
167 for i in $(git --git-dir="$d" config --list); do
177 __git_merge_strategies ()
179 if [ -n "$__git_merge_strategylist" ]; then
180 echo "$__git_merge_strategylist"
183 sed -n "/^all_strategies='/{
184 s/^all_strategies='//
188 }" "$(git --exec-path)/git-merge"
190 __git_merge_strategylist=
191 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
193 __git_complete_file ()
195 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
211 COMPREPLY=($(compgen -P "$pfx" \
212 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
213 | sed '/^100... blob /s,^.* ,,
222 __gitcomp "$(__git_refs)"
227 __git_complete_revlist ()
229 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
234 __gitcomp "$(__git_refs)" "$pfx" "$cur"
239 __gitcomp "$(__git_refs)" "$pfx" "$cur"
245 __gitcomp "$(__git_refs)"
252 if [ -n "$__git_commandlist" ]; then
253 echo "$__git_commandlist"
257 for i in $(git help -a|egrep '^ ')
260 add--interactive) : plumbing;;
261 applymbox) : ask gittus;;
262 applypatch) : ask gittus;;
263 archimport) : import;;
264 cat-file) : plumbing;;
265 check-attr) : plumbing;;
266 check-ref-format) : plumbing;;
267 commit-tree) : plumbing;;
268 convert-objects) : plumbing;;
269 cvsexportcommit) : export;;
270 cvsimport) : import;;
271 cvsserver) : daemon;;
273 fast-import) : import;;
274 fsck-objects) : plumbing;;
275 fetch--tool) : plumbing;;
276 fetch-pack) : plumbing;;
277 fmt-merge-msg) : plumbing;;
278 for-each-ref) : plumbing;;
279 hash-object) : plumbing;;
280 http-*) : transport;;
281 index-pack) : plumbing;;
282 init-db) : deprecated;;
283 local-fetch) : plumbing;;
284 mailinfo) : plumbing;;
285 mailsplit) : plumbing;;
286 merge-*) : plumbing;;
289 pack-objects) : plumbing;;
290 pack-redundant) : plumbing;;
291 pack-refs) : plumbing;;
292 parse-remote) : plumbing;;
293 patch-id) : plumbing;;
294 peek-remote) : plumbing;;
296 prune-packed) : plumbing;;
297 quiltimport) : import;;
298 read-tree) : plumbing;;
299 receive-pack) : plumbing;;
301 repo-config) : plumbing;;
303 rev-list) : plumbing;;
304 rev-parse) : plumbing;;
305 runstatus) : plumbing;;
306 sh-setup) : internal;;
308 send-pack) : plumbing;;
309 show-index) : plumbing;;
311 stripspace) : plumbing;;
312 svn) : import export;;
313 svnimport) : import;;
314 symbolic-ref) : plumbing;;
315 tar-tree) : deprecated;;
316 unpack-file) : plumbing;;
317 unpack-objects) : plumbing;;
318 update-index) : plumbing;;
319 update-ref) : plumbing;;
320 update-server-info) : daemon;;
321 upload-archive) : plumbing;;
322 upload-pack) : plumbing;;
323 write-tree) : plumbing;;
324 verify-tag) : plumbing;;
330 __git_commandlist="$(__git_commands 2>/dev/null)"
335 for i in $(git --git-dir="$(__gitdir)" config --list); do
345 __git_aliased_command ()
347 local word cmdline=$(git --git-dir="$(__gitdir)" \
348 config --get "alias.$1")
349 for word in $cmdline; do
350 if [ "${word##-*}" ]; then
357 __git_whitespacelist="nowarn warn error error-all strip"
361 local cur="${COMP_WORDS[COMP_CWORD]}"
362 if [ -d .dotest ]; then
363 __gitcomp "--skip --resolved"
368 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
373 --signoff --utf8 --binary --3way --interactive
383 local cur="${COMP_WORDS[COMP_CWORD]}"
386 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
391 --stat --numstat --summary --check --index
392 --cached --index-info --reverse --reject --unidiff-zero
393 --apply --no-add --exclude=
394 --whitespace= --inaccurate-eof --verbose
403 local cur="${COMP_WORDS[COMP_CWORD]}"
406 __gitcomp "--interactive"
415 while [ $c -lt $COMP_CWORD ]; do
418 start|bad|good|reset|visualize|replay|log)
426 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
427 __gitcomp "start bad good reset visualize replay log"
433 __gitcomp "$(__git_refs)"
443 __gitcomp "$(__git_refs)"
448 __gitcomp "$(__git_refs)"
453 __gitcomp "$(__git_refs)"
458 local cur="${COMP_WORDS[COMP_CWORD]}"
461 __gitcomp "--edit --no-commit"
464 __gitcomp "$(__git_refs)"
471 local cur="${COMP_WORDS[COMP_CWORD]}"
475 --all --author= --signoff --verify --no-verify
476 --edit --amend --include --only
490 __gitcomp "$(__git_refs)"
495 local cur="${COMP_WORDS[COMP_CWORD]}"
497 case "${COMP_WORDS[0]},$COMP_CWORD" in
499 __gitcomp "$(__git_remotes)"
502 __gitcomp "$(__git_remotes)"
507 __gitcomp "$(__git_refs)" "" "${cur#*:}"
511 case "${COMP_WORDS[0]}" in
512 git-fetch) remote="${COMP_WORDS[1]}" ;;
513 git) remote="${COMP_WORDS[2]}" ;;
515 __gitcomp "$(__git_refs2 "$remote")"
524 local cur="${COMP_WORDS[COMP_CWORD]}"
528 --stdout --attach --thread
530 --numbered --start-number
534 --full-index --binary
540 __git_complete_revlist
545 local cur="${COMP_WORDS[COMP_CWORD]}"
557 __gitcomp "$(__git_remotes)"
567 local cur="${COMP_WORDS[COMP_CWORD]}"
571 oneline short medium full fuller email raw
572 " "" "${cur##--pretty=}"
577 --max-count= --max-age= --since= --after=
578 --min-age= --before= --until=
579 --root --not --topo-order --date-order
581 --abbrev-commit --abbrev=
583 --author= --committer= --grep=
585 --pretty= --name-status --name-only
591 __git_complete_revlist
596 local cur="${COMP_WORDS[COMP_CWORD]}"
597 case "${COMP_WORDS[COMP_CWORD-1]}" in
599 __gitcomp "$(__git_merge_strategies)"
604 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
609 --no-commit --no-summary --squash --strategy
613 __gitcomp "$(__git_refs)"
618 __gitcomp "$(__git_refs)"
623 __gitcomp "--tags --all --stdin"
628 local cur="${COMP_WORDS[COMP_CWORD]}"
630 case "${COMP_WORDS[0]},$COMP_CWORD" in
632 __gitcomp "$(__git_remotes)"
635 __gitcomp "$(__git_remotes)"
639 case "${COMP_WORDS[0]}" in
640 git-pull) remote="${COMP_WORDS[1]}" ;;
641 git) remote="${COMP_WORDS[2]}" ;;
643 __gitcomp "$(__git_refs "$remote")"
650 local cur="${COMP_WORDS[COMP_CWORD]}"
652 case "${COMP_WORDS[0]},$COMP_CWORD" in
654 __gitcomp "$(__git_remotes)"
657 __gitcomp "$(__git_remotes)"
663 case "${COMP_WORDS[0]}" in
664 git-push) remote="${COMP_WORDS[1]}" ;;
665 git) remote="${COMP_WORDS[2]}" ;;
667 __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
670 __gitcomp "$(__git_refs2)"
679 local cur="${COMP_WORDS[COMP_CWORD]}"
680 if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
681 __gitcomp "--continue --skip --abort"
684 case "${COMP_WORDS[COMP_CWORD-1]}" in
686 __gitcomp "$(__git_merge_strategies)"
691 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
695 __gitcomp "--onto --merge --strategy"
698 __gitcomp "$(__git_refs)"
703 local cur="${COMP_WORDS[COMP_CWORD]}"
704 local prv="${COMP_WORDS[COMP_CWORD-1]}"
707 __gitcomp "$(__git_remotes)"
711 __gitcomp "$(__git_refs)"
715 local remote="${prv#remote.}"
716 remote="${remote%.fetch}"
717 __gitcomp "$(__git_refs_remotes "$remote")"
721 local remote="${prv#remote.}"
722 remote="${remote%.push}"
723 __gitcomp "$(git --git-dir="$(__gitdir)" \
724 for-each-ref --format='%(refname):%(refname)' \
728 pull.twohead|pull.octopus)
729 __gitcomp "$(__git_merge_strategies)"
732 color.branch|color.diff|color.status)
733 __gitcomp "always never auto"
738 black red green yellow blue magenta cyan white
739 bold dim ul blink reverse
751 --global --list --replace-all
752 --get --get-all --get-regexp
753 --add --unset --unset-all
758 local pfx="${cur%.*}."
760 __gitcomp "remote merge" "$pfx" "$cur"
764 local pfx="${cur%.*}."
766 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
770 local pfx="${cur%.*}."
772 __gitcomp "url fetch push" "$pfx" "$cur"
776 local pfx="${cur%.*}."
778 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
787 core.preferSymlinkRefs
788 core.logAllRefUpdates
789 core.repositoryFormatVersion
790 core.sharedRepository
791 core.warnAmbiguousRefs
794 core.packedGitWindowSize
809 color.diff.whitespace
815 color.status.untracked
823 gc.reflogexpireunreachable
836 i18n.logOutputEncoding
843 repack.useDeltaBaseOffset
849 receive.denyNonFastForwards
861 while [ $c -lt $COMP_CWORD ]; do
864 add|show|prune) command="$i"; break ;;
869 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
870 __gitcomp "add show prune"
876 __gitcomp "$(__git_remotes)"
886 local cur="${COMP_WORDS[COMP_CWORD]}"
889 __gitcomp "--mixed --hard --soft"
893 __gitcomp "$(__git_refs)"
898 local cur="${COMP_WORDS[COMP_CWORD]}"
902 oneline short medium full fuller email raw
903 " "" "${cur##--pretty=}"
907 __gitcomp "--pretty="
916 local i c=1 command __git_dir
918 while [ $c -lt $COMP_CWORD ]; do
921 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
922 --bare) __git_dir="." ;;
923 --version|--help|-p|--paginate) ;;
924 *) command="$i"; break ;;
929 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
930 case "${COMP_WORDS[COMP_CWORD]}" in
931 --*=*) COMPREPLY=() ;;
932 --*) __gitcomp "--git-dir= --bare --version --exec-path" ;;
933 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
938 local expansion=$(__git_aliased_command "$command")
939 [ "$expansion" ] && command="$expansion"
945 bisect) _git_bisect ;;
946 branch) _git_branch ;;
947 checkout) _git_checkout ;;
948 cherry) _git_cherry ;;
949 cherry-pick) _git_cherry_pick ;;
950 commit) _git_commit ;;
951 config) _git_config ;;
953 diff-tree) _git_diff_tree ;;
955 format-patch) _git_format_patch ;;
958 ls-remote) _git_ls_remote ;;
959 ls-tree) _git_ls_tree ;;
961 merge-base) _git_merge_base ;;
962 name-rev) _git_name_rev ;;
965 rebase) _git_rebase ;;
966 remote) _git_remote ;;
969 show-branch) _git_log ;;
970 whatchanged) _git_log ;;
977 local cur="${COMP_WORDS[COMP_CWORD]}"
980 __gitcomp "--not --all"
984 __git_complete_revlist
987 complete -o default -o nospace -F _git git
988 complete -o default -o nospace -F _gitk gitk
989 complete -o default -o nospace -F _git_am git-am
990 complete -o default -o nospace -F _git_apply git-apply
991 complete -o default -o nospace -F _git_bisect git-bisect
992 complete -o default -o nospace -F _git_branch git-branch
993 complete -o default -o nospace -F _git_checkout git-checkout
994 complete -o default -o nospace -F _git_cherry git-cherry
995 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
996 complete -o default -o nospace -F _git_commit git-commit
997 complete -o default -o nospace -F _git_diff git-diff
998 complete -o default -o nospace -F _git_diff_tree git-diff-tree
999 complete -o default -o nospace -F _git_fetch git-fetch
1000 complete -o default -o nospace -F _git_format_patch git-format-patch
1001 complete -o default -o nospace -F _git_gc git-gc
1002 complete -o default -o nospace -F _git_log git-log
1003 complete -o default -o nospace -F _git_ls_remote git-ls-remote
1004 complete -o default -o nospace -F _git_ls_tree git-ls-tree
1005 complete -o default -o nospace -F _git_merge git-merge
1006 complete -o default -o nospace -F _git_merge_base git-merge-base
1007 complete -o default -o nospace -F _git_name_rev git-name-rev
1008 complete -o default -o nospace -F _git_pull git-pull
1009 complete -o default -o nospace -F _git_push git-push
1010 complete -o default -o nospace -F _git_rebase git-rebase
1011 complete -o default -o nospace -F _git_config git-config
1012 complete -o default -o nospace -F _git_remote git-remote
1013 complete -o default -o nospace -F _git_reset git-reset
1014 complete -o default -o nospace -F _git_show git-show
1015 complete -o default -o nospace -F _git_log git-show-branch
1016 complete -o default -o nospace -F _git_log git-whatchanged
1018 # The following are necessary only for Cygwin, and only are needed
1019 # when the user has tab-completed the executable name and consequently
1020 # included the '.exe' suffix.
1022 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1023 complete -o default -o nospace -F _git_add git-add.exe
1024 complete -o default -o nospace -F _git_apply git-apply.exe
1025 complete -o default -o nospace -F _git git.exe
1026 complete -o default -o nospace -F _git_branch git-branch.exe
1027 complete -o default -o nospace -F _git_cherry git-cherry.exe
1028 complete -o default -o nospace -F _git_diff git-diff.exe
1029 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
1030 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1031 complete -o default -o nospace -F _git_log git-log.exe
1032 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1033 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1034 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1035 complete -o default -o nospace -F _git_push git-push.exe
1036 complete -o default -o nospace -F _git_config git-config
1037 complete -o default -o nospace -F _git_show git-show.exe
1038 complete -o default -o nospace -F _git_log git-show-branch.exe
1039 complete -o default -o nospace -F _git_log git-whatchanged.exe