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-ref-format) : plumbing;;
266 commit-tree) : plumbing;;
267 convert-objects) : plumbing;;
268 cvsexportcommit) : export;;
269 cvsimport) : import;;
270 cvsserver) : daemon;;
272 diff-stages) : nobody uses it;;
273 fsck-objects) : plumbing;;
274 fetch-pack) : plumbing;;
275 fmt-merge-msg) : plumbing;;
276 hash-object) : plumbing;;
277 http-*) : transport;;
278 index-pack) : plumbing;;
279 init-db) : deprecated;;
280 local-fetch) : plumbing;;
281 mailinfo) : plumbing;;
282 mailsplit) : plumbing;;
283 merge-*) : plumbing;;
286 pack-objects) : plumbing;;
287 pack-redundant) : plumbing;;
288 pack-refs) : plumbing;;
289 parse-remote) : plumbing;;
290 patch-id) : plumbing;;
291 peek-remote) : plumbing;;
293 prune-packed) : plumbing;;
294 quiltimport) : import;;
295 read-tree) : plumbing;;
296 receive-pack) : plumbing;;
298 repo-config) : plumbing;;
300 rev-list) : plumbing;;
301 rev-parse) : plumbing;;
302 runstatus) : plumbing;;
303 sh-setup) : internal;;
305 send-pack) : plumbing;;
306 show-index) : plumbing;;
308 stripspace) : plumbing;;
309 svn) : import export;;
310 svnimport) : import;;
311 symbolic-ref) : plumbing;;
312 tar-tree) : deprecated;;
313 unpack-file) : plumbing;;
314 unpack-objects) : plumbing;;
315 update-index) : plumbing;;
316 update-ref) : plumbing;;
317 update-server-info) : daemon;;
318 upload-archive) : plumbing;;
319 upload-pack) : plumbing;;
320 write-tree) : plumbing;;
321 verify-tag) : plumbing;;
327 __git_commandlist="$(__git_commands 2>/dev/null)"
332 for i in $(git --git-dir="$(__gitdir)" config --list); do
342 __git_aliased_command ()
344 local word cmdline=$(git --git-dir="$(__gitdir)" \
345 config --get "alias.$1")
346 for word in $cmdline; do
347 if [ "${word##-*}" ]; then
354 __git_whitespacelist="nowarn warn error error-all strip"
358 local cur="${COMP_WORDS[COMP_CWORD]}"
359 if [ -d .dotest ]; then
360 __gitcomp "--skip --resolved"
365 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
370 --signoff --utf8 --binary --3way --interactive
380 local cur="${COMP_WORDS[COMP_CWORD]}"
383 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
388 --stat --numstat --summary --check --index
389 --cached --index-info --reverse --reject --unidiff-zero
390 --apply --no-add --exclude=
391 --whitespace= --inaccurate-eof --verbose
400 local cur="${COMP_WORDS[COMP_CWORD]}"
403 __gitcomp "--interactive"
411 __gitcomp "$(__git_refs)"
416 __gitcomp "$(__git_refs)"
421 __gitcomp "$(__git_refs)"
426 local cur="${COMP_WORDS[COMP_CWORD]}"
429 __gitcomp "--edit --no-commit"
432 __gitcomp "$(__git_refs)"
439 local cur="${COMP_WORDS[COMP_CWORD]}"
443 --all --author= --signoff --verify --no-verify
444 --edit --amend --include --only
458 __gitcomp "$(__git_refs)"
463 local cur="${COMP_WORDS[COMP_CWORD]}"
465 case "${COMP_WORDS[0]},$COMP_CWORD" in
467 __gitcomp "$(__git_remotes)"
470 __gitcomp "$(__git_remotes)"
475 __gitcomp "$(__git_refs)" "" "${cur#*:}"
479 case "${COMP_WORDS[0]}" in
480 git-fetch) remote="${COMP_WORDS[1]}" ;;
481 git) remote="${COMP_WORDS[2]}" ;;
483 __gitcomp "$(__git_refs2 "$remote")"
492 local cur="${COMP_WORDS[COMP_CWORD]}"
496 --stdout --attach --thread
498 --numbered --start-number
502 --full-index --binary
508 __git_complete_revlist
513 __gitcomp "$(__git_remotes)"
523 local cur="${COMP_WORDS[COMP_CWORD]}"
527 oneline short medium full fuller email raw
528 " "" "${cur##--pretty=}"
533 --max-count= --max-age= --since= --after=
534 --min-age= --before= --until=
535 --root --not --topo-order --date-order
537 --abbrev-commit --abbrev=
539 --author= --committer= --grep=
541 --pretty= --name-status --name-only
547 __git_complete_revlist
552 local cur="${COMP_WORDS[COMP_CWORD]}"
553 case "${COMP_WORDS[COMP_CWORD-1]}" in
555 __gitcomp "$(__git_merge_strategies)"
560 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
565 --no-commit --no-summary --squash --strategy
569 __gitcomp "$(__git_refs)"
574 __gitcomp "$(__git_refs)"
579 __gitcomp "--tags --all --stdin"
584 local cur="${COMP_WORDS[COMP_CWORD]}"
586 case "${COMP_WORDS[0]},$COMP_CWORD" in
588 __gitcomp "$(__git_remotes)"
591 __gitcomp "$(__git_remotes)"
595 case "${COMP_WORDS[0]}" in
596 git-pull) remote="${COMP_WORDS[1]}" ;;
597 git) remote="${COMP_WORDS[2]}" ;;
599 __gitcomp "$(__git_refs "$remote")"
606 local cur="${COMP_WORDS[COMP_CWORD]}"
608 case "${COMP_WORDS[0]},$COMP_CWORD" in
610 __gitcomp "$(__git_remotes)"
613 __gitcomp "$(__git_remotes)"
619 case "${COMP_WORDS[0]}" in
620 git-push) remote="${COMP_WORDS[1]}" ;;
621 git) remote="${COMP_WORDS[2]}" ;;
623 __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
626 __gitcomp "$(__git_refs2)"
635 local cur="${COMP_WORDS[COMP_CWORD]}"
636 if [ -d .dotest ]; then
637 __gitcomp "--continue --skip --abort"
640 case "${COMP_WORDS[COMP_CWORD-1]}" in
642 __gitcomp "$(__git_merge_strategies)"
647 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
651 __gitcomp "--onto --merge --strategy"
654 __gitcomp "$(__git_refs)"
659 local cur="${COMP_WORDS[COMP_CWORD]}"
660 local prv="${COMP_WORDS[COMP_CWORD-1]}"
663 __gitcomp "$(__git_remotes)"
667 __gitcomp "$(__git_refs)"
671 local remote="${prv#remote.}"
672 remote="${remote%.fetch}"
673 __gitcomp "$(__git_refs_remotes "$remote")"
677 local remote="${prv#remote.}"
678 remote="${remote%.push}"
679 __gitcomp "$(git --git-dir="$(__gitdir)" \
680 for-each-ref --format='%(refname):%(refname)' \
684 pull.twohead|pull.octopus)
685 __gitcomp "$(__git_merge_strategies)"
688 color.branch|color.diff|color.status)
689 __gitcomp "always never auto"
694 black red green yellow blue magenta cyan white
695 bold dim ul blink reverse
707 --global --list --replace-all
708 --get --get-all --get-regexp
714 local pfx="${cur%.*}."
716 __gitcomp "remote merge" "$pfx" "$cur"
720 local pfx="${cur%.*}."
722 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
726 local pfx="${cur%.*}."
728 __gitcomp "url fetch push" "$pfx" "$cur"
732 local pfx="${cur%.*}."
734 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
743 core.preferSymlinkRefs
744 core.logAllRefUpdates
745 core.repositoryFormatVersion
746 core.sharedRepository
747 core.warnAmbiguousRefs
750 core.packedGitWindowSize
764 color.diff.whitespace
770 color.status.untracked
778 gc.reflogexpireunreachable
791 i18n.logOutputEncoding
798 repack.useDeltaBaseOffset
804 receive.denyNonFastForwards
815 local cur="${COMP_WORDS[COMP_CWORD]}"
818 __gitcomp "--mixed --hard --soft"
822 __gitcomp "$(__git_refs)"
827 local cur="${COMP_WORDS[COMP_CWORD]}"
831 oneline short medium full fuller email raw
832 " "" "${cur##--pretty=}"
836 __gitcomp "--pretty="
845 local i c=1 command __git_dir
847 while [ $c -lt $COMP_CWORD ]; do
850 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
851 --bare) __git_dir="." ;;
852 --version|--help|-p|--paginate) ;;
853 *) command="$i"; break ;;
858 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
859 case "${COMP_WORDS[COMP_CWORD]}" in
860 --*=*) COMPREPLY=() ;;
861 --*) __gitcomp "--git-dir= --bare --version --exec-path" ;;
862 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
867 local expansion=$(__git_aliased_command "$command")
868 [ "$expansion" ] && command="$expansion"
874 branch) _git_branch ;;
875 checkout) _git_checkout ;;
876 cherry) _git_cherry ;;
877 cherry-pick) _git_cherry_pick ;;
878 commit) _git_commit ;;
879 config) _git_config ;;
881 diff-tree) _git_diff_tree ;;
883 format-patch) _git_format_patch ;;
885 ls-remote) _git_ls_remote ;;
886 ls-tree) _git_ls_tree ;;
888 merge-base) _git_merge_base ;;
889 name-rev) _git_name_rev ;;
892 rebase) _git_rebase ;;
895 show-branch) _git_log ;;
896 whatchanged) _git_log ;;
903 local cur="${COMP_WORDS[COMP_CWORD]}"
906 __gitcomp "--not --all"
910 __git_complete_revlist
913 complete -o default -o nospace -F _git git
914 complete -o default -o nospace -F _gitk gitk
915 complete -o default -o nospace -F _git_am git-am
916 complete -o default -o nospace -F _git_apply git-apply
917 complete -o default -o nospace -F _git_branch git-branch
918 complete -o default -o nospace -F _git_checkout git-checkout
919 complete -o default -o nospace -F _git_cherry git-cherry
920 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
921 complete -o default -o nospace -F _git_commit git-commit
922 complete -o default -o nospace -F _git_diff git-diff
923 complete -o default -o nospace -F _git_diff_tree git-diff-tree
924 complete -o default -o nospace -F _git_fetch git-fetch
925 complete -o default -o nospace -F _git_format_patch git-format-patch
926 complete -o default -o nospace -F _git_log git-log
927 complete -o default -o nospace -F _git_ls_remote git-ls-remote
928 complete -o default -o nospace -F _git_ls_tree git-ls-tree
929 complete -o default -o nospace -F _git_merge git-merge
930 complete -o default -o nospace -F _git_merge_base git-merge-base
931 complete -o default -o nospace -F _git_name_rev git-name-rev
932 complete -o default -o nospace -F _git_pull git-pull
933 complete -o default -o nospace -F _git_push git-push
934 complete -o default -o nospace -F _git_rebase git-rebase
935 complete -o default -o nospace -F _git_config git-config
936 complete -o default -o nospace -F _git_reset git-reset
937 complete -o default -o nospace -F _git_show git-show
938 complete -o default -o nospace -F _git_log git-show-branch
939 complete -o default -o nospace -F _git_log git-whatchanged
941 # The following are necessary only for Cygwin, and only are needed
942 # when the user has tab-completed the executable name and consequently
943 # included the '.exe' suffix.
945 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
946 complete -o default -o nospace -F _git_add git-add.exe
947 complete -o default -o nospace -F _git_apply git-apply.exe
948 complete -o default -o nospace -F _git git.exe
949 complete -o default -o nospace -F _git_branch git-branch.exe
950 complete -o default -o nospace -F _git_cherry git-cherry.exe
951 complete -o default -o nospace -F _git_diff git-diff.exe
952 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
953 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
954 complete -o default -o nospace -F _git_log git-log.exe
955 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
956 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
957 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
958 complete -o default -o nospace -F _git_push git-push.exe
959 complete -o default -o nospace -F _git_config git-config
960 complete -o default -o nospace -F _git_show git-show.exe
961 complete -o default -o nospace -F _git_log git-show-branch.exe
962 complete -o default -o nospace -F _git_log git-whatchanged.exe