2 # bash completion support for core Git.
4 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
5 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
6 # Distributed under the GNU General Public License, version 2.0.
8 # The contained completion routines provide support for completing:
10 # *) local and remote branch names
11 # *) local and remote tag names
12 # *) .git/remotes file names
13 # *) git 'subcommands'
14 # *) tree paths within 'ref:path/to/file' expressions
15 # *) common --long-options
17 # To use these routines:
19 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
20 # 2) Added the following line to your .bashrc:
21 # source ~/.git-completion.sh
23 # 3) You may want to make sure the git executable is available
24 # in your PATH before this script is sourced, as some caching
25 # is performed while the script loads. If git isn't found
26 # at source time then all lookups will be done on demand,
27 # which may be slightly slower.
29 # 4) Consider changing your PS1 to also show the current branch:
30 # PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
32 # The argument to __git_ps1 will be displayed only if you
33 # are currently in a git repository. The %s token will be
34 # the name of the current branch.
38 # *) Read Documentation/SubmittingPatches
39 # *) Send all patches to the current maintainer:
41 # "Shawn O. Pearce" <spearce@spearce.org>
43 # *) Always CC the Git mailing list:
51 if [ -n "$__git_dir" ]; then
53 elif [ -d .git ]; then
56 git rev-parse --git-dir 2>/dev/null
58 elif [ -d "$1/.git" ]; then
67 local b="$(git symbolic-ref HEAD 2>/dev/null)"
70 printf "$1" "${b##refs/heads/}"
72 printf " (%s)" "${b##refs/heads/}"
79 local all c s=$'\n' IFS=' '$'\t'$'\n'
80 local cur="${COMP_WORDS[COMP_CWORD]}"
86 --*=*) all="$all$c$4$s" ;;
87 *.) all="$all$c$4$s" ;;
88 *) all="$all$c$4 $s" ;;
92 COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
98 local cmd i is_hash=y dir="$(__gitdir "$1")"
99 if [ -d "$dir" ]; then
100 for i in $(git --git-dir="$dir" \
101 for-each-ref --format='%(refname)' \
103 echo "${i#refs/heads/}"
107 for i in $(git-ls-remote "$1" 2>/dev/null); do
108 case "$is_hash,$i" in
111 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
112 n,*) is_hash=y; echo "$i" ;;
119 local cmd i is_hash=y dir="$(__gitdir "$1")"
120 if [ -d "$dir" ]; then
121 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
122 for i in $(git --git-dir="$dir" \
123 for-each-ref --format='%(refname)' \
124 refs/tags refs/heads refs/remotes); do
126 refs/tags/*) echo "${i#refs/tags/}" ;;
127 refs/heads/*) echo "${i#refs/heads/}" ;;
128 refs/remotes/*) echo "${i#refs/remotes/}" ;;
134 for i in $(git-ls-remote "$dir" 2>/dev/null); do
135 case "$is_hash,$i" in
138 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
139 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
140 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
141 n,*) is_hash=y; echo "$i" ;;
149 for i in $(__git_refs "$1"); do
154 __git_refs_remotes ()
156 local cmd i is_hash=y
157 for i in $(git-ls-remote "$1" 2>/dev/null); do
158 case "$is_hash,$i" in
161 echo "$i:refs/remotes/$1/${i#refs/heads/}"
165 n,refs/tags/*) is_hash=y;;
173 local i ngoff IFS=$'\n' d="$(__gitdir)"
174 shopt -q nullglob || ngoff=1
176 for i in "$d/remotes"/*; do
177 echo ${i#$d/remotes/}
179 [ "$ngoff" ] && shopt -u nullglob
180 for i in $(git --git-dir="$d" config --list); do
190 __git_merge_strategies ()
192 if [ -n "$__git_merge_strategylist" ]; then
193 echo "$__git_merge_strategylist"
196 sed -n "/^all_strategies='/{
197 s/^all_strategies='//
201 }" "$(git --exec-path)/git-merge"
203 __git_merge_strategylist=
204 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
206 __git_complete_file ()
208 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
224 COMPREPLY=($(compgen -P "$pfx" \
225 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
226 | sed '/^100... blob /s,^.* ,,
235 __gitcomp "$(__git_refs)"
240 __git_complete_revlist ()
242 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
247 __gitcomp "$(__git_refs)" "$pfx" "$cur"
252 __gitcomp "$(__git_refs)" "$pfx" "$cur"
258 __gitcomp "$(__git_refs)"
265 if [ -n "$__git_commandlist" ]; then
266 echo "$__git_commandlist"
270 for i in $(git help -a|egrep '^ ')
273 add--interactive) : plumbing;;
274 applymbox) : ask gittus;;
275 applypatch) : ask gittus;;
276 archimport) : import;;
277 cat-file) : plumbing;;
278 check-attr) : plumbing;;
279 check-ref-format) : plumbing;;
280 commit-tree) : plumbing;;
281 convert-objects) : plumbing;;
282 cvsexportcommit) : export;;
283 cvsimport) : import;;
284 cvsserver) : daemon;;
286 diff-files) : plumbing;;
287 diff-index) : plumbing;;
288 diff-tree) : plumbing;;
289 fast-import) : import;;
290 fsck-objects) : plumbing;;
291 fetch--tool) : plumbing;;
292 fetch-pack) : plumbing;;
293 fmt-merge-msg) : plumbing;;
294 for-each-ref) : plumbing;;
295 hash-object) : plumbing;;
296 http-*) : transport;;
297 index-pack) : plumbing;;
298 init-db) : deprecated;;
299 local-fetch) : plumbing;;
300 mailinfo) : plumbing;;
301 mailsplit) : plumbing;;
302 merge-*) : plumbing;;
305 pack-objects) : plumbing;;
306 pack-redundant) : plumbing;;
307 pack-refs) : plumbing;;
308 parse-remote) : plumbing;;
309 patch-id) : plumbing;;
310 peek-remote) : plumbing;;
312 prune-packed) : plumbing;;
313 quiltimport) : import;;
314 read-tree) : plumbing;;
315 receive-pack) : plumbing;;
317 repo-config) : plumbing;;
319 rev-list) : plumbing;;
320 rev-parse) : plumbing;;
321 runstatus) : plumbing;;
322 sh-setup) : internal;;
324 send-pack) : plumbing;;
325 show-index) : plumbing;;
327 stripspace) : plumbing;;
328 svn) : import export;;
329 svnimport) : import;;
330 symbolic-ref) : plumbing;;
331 tar-tree) : deprecated;;
332 unpack-file) : plumbing;;
333 unpack-objects) : plumbing;;
334 update-index) : plumbing;;
335 update-ref) : plumbing;;
336 update-server-info) : daemon;;
337 upload-archive) : plumbing;;
338 upload-pack) : plumbing;;
339 write-tree) : plumbing;;
340 verify-tag) : plumbing;;
346 __git_commandlist="$(__git_commands 2>/dev/null)"
351 for i in $(git --git-dir="$(__gitdir)" config --list); do
361 __git_aliased_command ()
363 local word cmdline=$(git --git-dir="$(__gitdir)" \
364 config --get "alias.$1")
365 for word in $cmdline; do
366 if [ "${word##-*}" ]; then
373 __git_whitespacelist="nowarn warn error error-all strip"
377 local cur="${COMP_WORDS[COMP_CWORD]}"
378 if [ -d .dotest ]; then
379 __gitcomp "--skip --resolved"
384 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
389 --signoff --utf8 --binary --3way --interactive
399 local cur="${COMP_WORDS[COMP_CWORD]}"
402 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
407 --stat --numstat --summary --check --index
408 --cached --index-info --reverse --reject --unidiff-zero
409 --apply --no-add --exclude=
410 --whitespace= --inaccurate-eof --verbose
419 local cur="${COMP_WORDS[COMP_CWORD]}"
422 __gitcomp "--interactive --refresh"
431 while [ $c -lt $COMP_CWORD ]; do
434 start|bad|good|reset|visualize|replay|log)
442 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
443 __gitcomp "start bad good reset visualize replay log"
449 __gitcomp "$(__git_refs)"
459 __gitcomp "$(__git_refs)"
464 local mycword="$COMP_CWORD"
465 case "${COMP_WORDS[0]}" in
467 local cmd="${COMP_WORDS[2]}"
468 mycword="$((mycword-1))"
471 local cmd="${COMP_WORDS[1]}"
476 __gitcomp "create list-heads verify unbundle"
484 __git_complete_revlist
493 __gitcomp "$(__git_refs)"
498 __gitcomp "$(__git_refs)"
503 local cur="${COMP_WORDS[COMP_CWORD]}"
506 __gitcomp "--edit --no-commit"
509 __gitcomp "$(__git_refs)"
516 local cur="${COMP_WORDS[COMP_CWORD]}"
520 --all --author= --signoff --verify --no-verify
521 --edit --amend --include --only
530 __gitcomp "$(__git_refs)"
540 __gitcomp "$(__git_refs)"
545 local cur="${COMP_WORDS[COMP_CWORD]}"
547 case "${COMP_WORDS[0]},$COMP_CWORD" in
549 __gitcomp "$(__git_remotes)"
552 __gitcomp "$(__git_remotes)"
557 __gitcomp "$(__git_refs)" "" "${cur#*:}"
561 case "${COMP_WORDS[0]}" in
562 git-fetch) remote="${COMP_WORDS[1]}" ;;
563 git) remote="${COMP_WORDS[2]}" ;;
565 __gitcomp "$(__git_refs2 "$remote")"
574 local cur="${COMP_WORDS[COMP_CWORD]}"
578 --stdout --attach --thread
580 --numbered --start-number
585 --full-index --binary
591 __git_complete_revlist
596 local cur="${COMP_WORDS[COMP_CWORD]}"
599 __gitcomp "--prune --aggressive"
608 __gitcomp "$(__git_remotes)"
618 local cur="${COMP_WORDS[COMP_CWORD]}"
622 oneline short medium full fuller email raw
623 " "" "${cur##--pretty=}"
628 relative iso8601 rfc2822 short local default
629 " "" "${cur##--date=}"
634 --max-count= --max-age= --since= --after=
635 --min-age= --before= --until=
636 --root --topo-order --date-order --reverse
638 --abbrev-commit --abbrev=
639 --relative-date --date=
640 --author= --committer= --grep=
642 --pretty= --name-status --name-only --raw
648 __git_complete_revlist
653 local cur="${COMP_WORDS[COMP_CWORD]}"
654 case "${COMP_WORDS[COMP_CWORD-1]}" in
656 __gitcomp "$(__git_merge_strategies)"
661 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
666 --no-commit --no-summary --squash --strategy
670 __gitcomp "$(__git_refs)"
675 __gitcomp "$(__git_refs)"
680 __gitcomp "--tags --all --stdin"
685 local cur="${COMP_WORDS[COMP_CWORD]}"
687 case "${COMP_WORDS[0]},$COMP_CWORD" in
689 __gitcomp "$(__git_remotes)"
692 __gitcomp "$(__git_remotes)"
696 case "${COMP_WORDS[0]}" in
697 git-pull) remote="${COMP_WORDS[1]}" ;;
698 git) remote="${COMP_WORDS[2]}" ;;
700 __gitcomp "$(__git_refs "$remote")"
707 local cur="${COMP_WORDS[COMP_CWORD]}"
709 case "${COMP_WORDS[0]},$COMP_CWORD" in
711 __gitcomp "$(__git_remotes)"
714 __gitcomp "$(__git_remotes)"
720 case "${COMP_WORDS[0]}" in
721 git-push) remote="${COMP_WORDS[1]}" ;;
722 git) remote="${COMP_WORDS[2]}" ;;
724 __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
727 __gitcomp "$(__git_refs)" + "${cur#+}"
730 __gitcomp "$(__git_refs)"
739 local cur="${COMP_WORDS[COMP_CWORD]}"
740 if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
741 __gitcomp "--continue --skip --abort"
744 case "${COMP_WORDS[COMP_CWORD-1]}" in
746 __gitcomp "$(__git_merge_strategies)"
751 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
755 __gitcomp "--onto --merge --strategy"
758 __gitcomp "$(__git_refs)"
763 local cur="${COMP_WORDS[COMP_CWORD]}"
764 local prv="${COMP_WORDS[COMP_CWORD-1]}"
767 __gitcomp "$(__git_remotes)"
771 __gitcomp "$(__git_refs)"
775 local remote="${prv#remote.}"
776 remote="${remote%.fetch}"
777 __gitcomp "$(__git_refs_remotes "$remote")"
781 local remote="${prv#remote.}"
782 remote="${remote%.push}"
783 __gitcomp "$(git --git-dir="$(__gitdir)" \
784 for-each-ref --format='%(refname):%(refname)' \
788 pull.twohead|pull.octopus)
789 __gitcomp "$(__git_merge_strategies)"
792 color.branch|color.diff|color.status)
793 __gitcomp "always never auto"
798 black red green yellow blue magenta cyan white
799 bold dim ul blink reverse
811 --global --system --file=
813 --get --get-all --get-regexp
814 --add --unset --unset-all
815 --remove-section --rename-section
820 local pfx="${cur%.*}."
822 __gitcomp "remote merge" "$pfx" "$cur"
826 local pfx="${cur%.*}."
828 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
832 local pfx="${cur%.*}."
835 url fetch push skipDefaultUpdate
836 receivepack uploadpack tagopt
841 local pfx="${cur%.*}."
843 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
852 core.preferSymlinkRefs
853 core.logAllRefUpdates
854 core.loosecompression
855 core.repositoryFormatVersion
856 core.sharedRepository
857 core.warnAmbiguousRefs
860 core.packedGitWindowSize
875 color.diff.whitespace
881 color.status.untracked
890 gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dvpass
893 gc.reflogexpireunreachable
906 i18n.logOutputEncoding
919 repack.useDeltaBaseOffset
925 receive.denyNonFastForwards
937 while [ $c -lt $COMP_CWORD ]; do
940 add|show|prune|update) command="$i"; break ;;
945 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
946 __gitcomp "add show prune update"
952 __gitcomp "$(__git_remotes)"
955 local i c='' IFS=$'\n'
956 for i in $(git --git-dir="$(__gitdir)" config --list); do
974 local cur="${COMP_WORDS[COMP_CWORD]}"
977 __gitcomp "--mixed --hard --soft"
981 __gitcomp "$(__git_refs)"
986 local cur="${COMP_WORDS[COMP_CWORD]}"
990 --max-count= --max-age= --since= --after=
991 --min-age= --before= --until=
993 --author= --committer= --grep=
1001 __git_complete_revlist
1006 local cur="${COMP_WORDS[COMP_CWORD]}"
1010 oneline short medium full fuller email raw
1011 " "" "${cur##--pretty=}"
1015 __gitcomp "--pretty="
1024 __gitcomp 'list show apply clear'
1030 while [ $c -lt $COMP_CWORD ]; do
1031 i="${COMP_WORDS[c]}"
1033 add|status|init|update) command="$i"; break ;;
1038 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1039 local cur="${COMP_WORDS[COMP_CWORD]}"
1042 __gitcomp "--quiet --cached"
1045 __gitcomp "add status init update"
1054 local i c=1 command __git_dir
1056 while [ $c -lt $COMP_CWORD ]; do
1057 i="${COMP_WORDS[c]}"
1059 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1060 --bare) __git_dir="." ;;
1061 --version|--help|-p|--paginate) ;;
1062 *) command="$i"; break ;;
1067 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1068 case "${COMP_WORDS[COMP_CWORD]}" in
1069 --*=*) COMPREPLY=() ;;
1078 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
1083 local expansion=$(__git_aliased_command "$command")
1084 [ "$expansion" ] && command="$expansion"
1089 apply) _git_apply ;;
1090 bisect) _git_bisect ;;
1091 bundle) _git_bundle ;;
1092 branch) _git_branch ;;
1093 checkout) _git_checkout ;;
1094 cherry) _git_cherry ;;
1095 cherry-pick) _git_cherry_pick ;;
1096 commit) _git_commit ;;
1097 config) _git_config ;;
1098 describe) _git_describe ;;
1100 fetch) _git_fetch ;;
1101 format-patch) _git_format_patch ;;
1104 ls-remote) _git_ls_remote ;;
1105 ls-tree) _git_ls_tree ;;
1107 merge-base) _git_merge_base ;;
1108 name-rev) _git_name_rev ;;
1111 rebase) _git_rebase ;;
1112 remote) _git_remote ;;
1113 reset) _git_reset ;;
1114 shortlog) _git_shortlog ;;
1116 show-branch) _git_log ;;
1117 stash) _git_stash ;;
1118 submodule) _git_submodule ;;
1119 whatchanged) _git_log ;;
1126 local cur="${COMP_WORDS[COMP_CWORD]}"
1129 __gitcomp "--not --all"
1133 __git_complete_revlist
1136 complete -o default -o nospace -F _git git
1137 complete -o default -o nospace -F _gitk gitk
1138 complete -o default -o nospace -F _git_am git-am
1139 complete -o default -o nospace -F _git_apply git-apply
1140 complete -o default -o nospace -F _git_bisect git-bisect
1141 complete -o default -o nospace -F _git_branch git-branch
1142 complete -o default -o nospace -F _git_bundle git-bundle
1143 complete -o default -o nospace -F _git_checkout git-checkout
1144 complete -o default -o nospace -F _git_cherry git-cherry
1145 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
1146 complete -o default -o nospace -F _git_commit git-commit
1147 complete -o default -o nospace -F _git_describe git-describe
1148 complete -o default -o nospace -F _git_diff git-diff
1149 complete -o default -o nospace -F _git_fetch git-fetch
1150 complete -o default -o nospace -F _git_format_patch git-format-patch
1151 complete -o default -o nospace -F _git_gc git-gc
1152 complete -o default -o nospace -F _git_log git-log
1153 complete -o default -o nospace -F _git_ls_remote git-ls-remote
1154 complete -o default -o nospace -F _git_ls_tree git-ls-tree
1155 complete -o default -o nospace -F _git_merge git-merge
1156 complete -o default -o nospace -F _git_merge_base git-merge-base
1157 complete -o default -o nospace -F _git_name_rev git-name-rev
1158 complete -o default -o nospace -F _git_pull git-pull
1159 complete -o default -o nospace -F _git_push git-push
1160 complete -o default -o nospace -F _git_rebase git-rebase
1161 complete -o default -o nospace -F _git_config git-config
1162 complete -o default -o nospace -F _git_remote git-remote
1163 complete -o default -o nospace -F _git_reset git-reset
1164 complete -o default -o nospace -F _git_shortlog git-shortlog
1165 complete -o default -o nospace -F _git_show git-show
1166 complete -o default -o nospace -F _git_stash git-stash
1167 complete -o default -o nospace -F _git_submodule git-submodule
1168 complete -o default -o nospace -F _git_log git-show-branch
1169 complete -o default -o nospace -F _git_log git-whatchanged
1171 # The following are necessary only for Cygwin, and only are needed
1172 # when the user has tab-completed the executable name and consequently
1173 # included the '.exe' suffix.
1175 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1176 complete -o default -o nospace -F _git_add git-add.exe
1177 complete -o default -o nospace -F _git_apply git-apply.exe
1178 complete -o default -o nospace -F _git git.exe
1179 complete -o default -o nospace -F _git_branch git-branch.exe
1180 complete -o default -o nospace -F _git_bundle git-bundle.exe
1181 complete -o default -o nospace -F _git_cherry git-cherry.exe
1182 complete -o default -o nospace -F _git_describe git-describe.exe
1183 complete -o default -o nospace -F _git_diff git-diff.exe
1184 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1185 complete -o default -o nospace -F _git_log git-log.exe
1186 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1187 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1188 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1189 complete -o default -o nospace -F _git_push git-push.exe
1190 complete -o default -o nospace -F _git_config git-config
1191 complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1192 complete -o default -o nospace -F _git_show git-show.exe
1193 complete -o default -o nospace -F _git_log git-show-branch.exe
1194 complete -o default -o nospace -F _git_log git-whatchanged.exe