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 g="$(git rev-parse --git-dir 2>/dev/null)"
71 if [ -d "$g/../.dotest" ]
74 b="$(git symbolic-ref HEAD 2>/dev/null)"
75 elif [ -f "$g/.dotest-merge/interactive" ]
78 b="$(cat $g/.dotest-merge/head-name)"
79 elif [ -d "$g/.dotest-merge" ]
82 b="$(cat $g/.dotest-merge/head-name)"
83 elif [ -f "$g/MERGE_HEAD" ]
86 b="$(git symbolic-ref HEAD 2>/dev/null)"
88 if [ -f $g/BISECT_LOG ]
92 if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
94 b="$(cut -c1-7 $g/HEAD)..."
99 printf "$1" "${b##refs/heads/}$r"
101 printf " (%s)" "${b##refs/heads/}$r"
108 local all c s=$'\n' IFS=' '$'\t'$'\n'
109 local cur="${COMP_WORDS[COMP_CWORD]}"
110 if [ $# -gt 2 ]; then
115 --*=*) all="$all$c$4$s" ;;
116 *.) all="$all$c$4$s" ;;
117 *) all="$all$c$4 $s" ;;
121 COMPREPLY=($(compgen -P "$2" -W "$all" -- "$cur"))
127 local cmd i is_hash=y dir="$(__gitdir "$1")"
128 if [ -d "$dir" ]; then
129 for i in $(git --git-dir="$dir" \
130 for-each-ref --format='%(refname)' \
132 echo "${i#refs/heads/}"
136 for i in $(git-ls-remote "$1" 2>/dev/null); do
137 case "$is_hash,$i" in
140 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
141 n,*) is_hash=y; echo "$i" ;;
148 local cmd i is_hash=y dir="$(__gitdir "$1")"
149 if [ -d "$dir" ]; then
150 for i in $(git --git-dir="$dir" \
151 for-each-ref --format='%(refname)' \
153 echo "${i#refs/tags/}"
157 for i in $(git-ls-remote "$1" 2>/dev/null); do
158 case "$is_hash,$i" in
161 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
162 n,*) is_hash=y; echo "$i" ;;
169 local cmd i is_hash=y dir="$(__gitdir "$1")"
170 if [ -d "$dir" ]; then
171 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
172 for i in $(git --git-dir="$dir" \
173 for-each-ref --format='%(refname)' \
174 refs/tags refs/heads refs/remotes); do
176 refs/tags/*) echo "${i#refs/tags/}" ;;
177 refs/heads/*) echo "${i#refs/heads/}" ;;
178 refs/remotes/*) echo "${i#refs/remotes/}" ;;
184 for i in $(git-ls-remote "$dir" 2>/dev/null); do
185 case "$is_hash,$i" in
188 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
189 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
190 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
191 n,*) is_hash=y; echo "$i" ;;
199 for i in $(__git_refs "$1"); do
204 __git_refs_remotes ()
206 local cmd i is_hash=y
207 for i in $(git-ls-remote "$1" 2>/dev/null); do
208 case "$is_hash,$i" in
211 echo "$i:refs/remotes/$1/${i#refs/heads/}"
215 n,refs/tags/*) is_hash=y;;
223 local i ngoff IFS=$'\n' d="$(__gitdir)"
224 shopt -q nullglob || ngoff=1
226 for i in "$d/remotes"/*; do
227 echo ${i#$d/remotes/}
229 [ "$ngoff" ] && shopt -u nullglob
230 for i in $(git --git-dir="$d" config --list); do
240 __git_merge_strategies ()
242 if [ -n "$__git_merge_strategylist" ]; then
243 echo "$__git_merge_strategylist"
246 sed -n "/^all_strategies='/{
247 s/^all_strategies='//
251 }" "$(git --exec-path)/git-merge"
253 __git_merge_strategylist=
254 __git_merge_strategylist="$(__git_merge_strategies 2>/dev/null)"
256 __git_complete_file ()
258 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
274 COMPREPLY=($(compgen -P "$pfx" \
275 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
276 | sed '/^100... blob /s,^.* ,,
285 __gitcomp "$(__git_refs)"
290 __git_complete_revlist ()
292 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
297 __gitcomp "$(__git_refs)" "$pfx" "$cur"
302 __gitcomp "$(__git_refs)" "$pfx" "$cur"
308 __gitcomp "$(__git_refs)"
315 if [ -n "$__git_commandlist" ]; then
316 echo "$__git_commandlist"
320 for i in $(git help -a|egrep '^ ')
323 *--*) : helper pattern;;
324 applymbox) : ask gittus;;
325 applypatch) : ask gittus;;
326 archimport) : import;;
327 cat-file) : plumbing;;
328 check-attr) : plumbing;;
329 check-ref-format) : plumbing;;
330 commit-tree) : plumbing;;
331 cvsexportcommit) : export;;
332 cvsimport) : import;;
333 cvsserver) : daemon;;
335 diff-files) : plumbing;;
336 diff-index) : plumbing;;
337 diff-tree) : plumbing;;
338 fast-import) : import;;
339 fsck-objects) : plumbing;;
340 fetch-pack) : plumbing;;
341 fmt-merge-msg) : plumbing;;
342 for-each-ref) : plumbing;;
343 hash-object) : plumbing;;
344 http-*) : transport;;
345 index-pack) : plumbing;;
346 init-db) : deprecated;;
347 local-fetch) : plumbing;;
348 mailinfo) : plumbing;;
349 mailsplit) : plumbing;;
350 merge-*) : plumbing;;
353 pack-objects) : plumbing;;
354 pack-redundant) : plumbing;;
355 pack-refs) : plumbing;;
356 parse-remote) : plumbing;;
357 patch-id) : plumbing;;
358 peek-remote) : plumbing;;
360 prune-packed) : plumbing;;
361 quiltimport) : import;;
362 read-tree) : plumbing;;
363 receive-pack) : plumbing;;
365 repo-config) : deprecated;;
367 rev-list) : plumbing;;
368 rev-parse) : plumbing;;
369 runstatus) : plumbing;;
370 sh-setup) : internal;;
372 send-pack) : plumbing;;
373 show-index) : plumbing;;
375 stripspace) : plumbing;;
376 svn) : import export;;
377 symbolic-ref) : plumbing;;
378 tar-tree) : deprecated;;
379 unpack-file) : plumbing;;
380 unpack-objects) : plumbing;;
381 update-index) : plumbing;;
382 update-ref) : plumbing;;
383 update-server-info) : daemon;;
384 upload-archive) : plumbing;;
385 upload-pack) : plumbing;;
386 write-tree) : plumbing;;
387 verify-tag) : plumbing;;
393 __git_commandlist="$(__git_commands 2>/dev/null)"
398 for i in $(git --git-dir="$(__gitdir)" config --list); do
408 __git_aliased_command ()
410 local word cmdline=$(git --git-dir="$(__gitdir)" \
411 config --get "alias.$1")
412 for word in $cmdline; do
413 if [ "${word##-*}" ]; then
420 __git_whitespacelist="nowarn warn error error-all strip"
424 local cur="${COMP_WORDS[COMP_CWORD]}"
425 if [ -d .dotest ]; then
426 __gitcomp "--skip --resolved"
431 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
436 --signoff --utf8 --binary --3way --interactive
446 local cur="${COMP_WORDS[COMP_CWORD]}"
449 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
454 --stat --numstat --summary --check --index
455 --cached --index-info --reverse --reject --unidiff-zero
456 --apply --no-add --exclude=
457 --whitespace= --inaccurate-eof --verbose
466 local cur="${COMP_WORDS[COMP_CWORD]}"
469 __gitcomp "--interactive --refresh"
478 while [ $c -lt $COMP_CWORD ]; do
481 start|bad|good|reset|visualize|replay|log)
489 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
490 __gitcomp "start bad good reset visualize replay log"
496 __gitcomp "$(__git_refs)"
506 __gitcomp "$(__git_refs)"
511 local mycword="$COMP_CWORD"
512 case "${COMP_WORDS[0]}" in
514 local cmd="${COMP_WORDS[2]}"
515 mycword="$((mycword-1))"
518 local cmd="${COMP_WORDS[1]}"
523 __gitcomp "create list-heads verify unbundle"
531 __git_complete_revlist
540 __gitcomp "$(__git_refs)"
545 __gitcomp "$(__git_refs)"
550 local cur="${COMP_WORDS[COMP_CWORD]}"
553 __gitcomp "--edit --no-commit"
556 __gitcomp "$(__git_refs)"
563 local cur="${COMP_WORDS[COMP_CWORD]}"
567 --all --author= --signoff --verify --no-verify
568 --edit --amend --include --only
577 __gitcomp "$(__git_refs)"
582 local cur="${COMP_WORDS[COMP_CWORD]}"
585 __gitcomp "--cached --stat --numstat --shortstat --summary
586 --patch-with-stat --name-only --name-status --color
587 --no-color --color-words --no-renames --check
588 --full-index --binary --abbrev --diff-filter
589 --find-copies-harder --pickaxe-all --pickaxe-regex
590 --text --ignore-space-at-eol --ignore-space-change
591 --ignore-all-space --exit-code --quiet --ext-diff
601 __gitcomp "$(__git_refs)"
606 local cur="${COMP_WORDS[COMP_CWORD]}"
608 case "${COMP_WORDS[0]},$COMP_CWORD" in
610 __gitcomp "$(__git_remotes)"
613 __gitcomp "$(__git_remotes)"
618 __gitcomp "$(__git_refs)" "" "${cur#*:}"
622 case "${COMP_WORDS[0]}" in
623 git-fetch) remote="${COMP_WORDS[1]}" ;;
624 git) remote="${COMP_WORDS[2]}" ;;
626 __gitcomp "$(__git_refs2 "$remote")"
635 local cur="${COMP_WORDS[COMP_CWORD]}"
639 --stdout --attach --thread
641 --numbered --start-number
646 --full-index --binary
652 __git_complete_revlist
657 local cur="${COMP_WORDS[COMP_CWORD]}"
660 __gitcomp "--prune --aggressive"
669 __gitcomp "$(__git_remotes)"
679 local cur="${COMP_WORDS[COMP_CWORD]}"
683 oneline short medium full fuller email raw
684 " "" "${cur##--pretty=}"
689 relative iso8601 rfc2822 short local default
690 " "" "${cur##--date=}"
695 --max-count= --max-age= --since= --after=
696 --min-age= --before= --until=
697 --root --topo-order --date-order --reverse
699 --abbrev-commit --abbrev=
700 --relative-date --date=
701 --author= --committer= --grep=
703 --pretty= --name-status --name-only --raw
705 --left-right --cherry-pick
710 __git_complete_revlist
715 local cur="${COMP_WORDS[COMP_CWORD]}"
716 case "${COMP_WORDS[COMP_CWORD-1]}" in
718 __gitcomp "$(__git_merge_strategies)"
723 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
728 --no-commit --no-summary --squash --strategy
732 __gitcomp "$(__git_refs)"
737 __gitcomp "$(__git_refs)"
742 __gitcomp "--tags --all --stdin"
747 local cur="${COMP_WORDS[COMP_CWORD]}"
749 case "${COMP_WORDS[0]},$COMP_CWORD" in
751 __gitcomp "$(__git_remotes)"
754 __gitcomp "$(__git_remotes)"
758 case "${COMP_WORDS[0]}" in
759 git-pull) remote="${COMP_WORDS[1]}" ;;
760 git) remote="${COMP_WORDS[2]}" ;;
762 __gitcomp "$(__git_refs "$remote")"
769 local cur="${COMP_WORDS[COMP_CWORD]}"
771 case "${COMP_WORDS[0]},$COMP_CWORD" in
773 __gitcomp "$(__git_remotes)"
776 __gitcomp "$(__git_remotes)"
782 case "${COMP_WORDS[0]}" in
783 git-push) remote="${COMP_WORDS[1]}" ;;
784 git) remote="${COMP_WORDS[2]}" ;;
786 __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
789 __gitcomp "$(__git_refs)" + "${cur#+}"
792 __gitcomp "$(__git_refs)"
801 local cur="${COMP_WORDS[COMP_CWORD]}"
802 if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
803 __gitcomp "--continue --skip --abort"
806 case "${COMP_WORDS[COMP_CWORD-1]}" in
808 __gitcomp "$(__git_merge_strategies)"
813 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
817 __gitcomp "--onto --merge --strategy"
820 __gitcomp "$(__git_refs)"
825 local cur="${COMP_WORDS[COMP_CWORD]}"
826 local prv="${COMP_WORDS[COMP_CWORD-1]}"
829 __gitcomp "$(__git_remotes)"
833 __gitcomp "$(__git_refs)"
837 local remote="${prv#remote.}"
838 remote="${remote%.fetch}"
839 __gitcomp "$(__git_refs_remotes "$remote")"
843 local remote="${prv#remote.}"
844 remote="${remote%.push}"
845 __gitcomp "$(git --git-dir="$(__gitdir)" \
846 for-each-ref --format='%(refname):%(refname)' \
850 pull.twohead|pull.octopus)
851 __gitcomp "$(__git_merge_strategies)"
854 color.branch|color.diff|color.status)
855 __gitcomp "always never auto"
860 black red green yellow blue magenta cyan white
861 bold dim ul blink reverse
873 --global --system --file=
875 --get --get-all --get-regexp
876 --add --unset --unset-all
877 --remove-section --rename-section
882 local pfx="${cur%.*}."
884 __gitcomp "remote merge" "$pfx" "$cur"
888 local pfx="${cur%.*}."
890 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
894 local pfx="${cur%.*}."
897 url fetch push skipDefaultUpdate
898 receivepack uploadpack tagopt
903 local pfx="${cur%.*}."
905 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
914 core.preferSymlinkRefs
915 core.logAllRefUpdates
916 core.loosecompression
917 core.repositoryFormatVersion
918 core.sharedRepository
919 core.warnAmbiguousRefs
922 core.packedGitWindowSize
937 color.diff.whitespace
943 color.status.untracked
952 gitcvs.dbname gitcvs.dbdriver gitcvs.dbuser gitcvs.dvpass
955 gc.reflogexpireunreachable
968 i18n.logOutputEncoding
981 repack.useDeltaBaseOffset
987 receive.denyNonFastForwards
999 while [ $c -lt $COMP_CWORD ]; do
1000 i="${COMP_WORDS[c]}"
1002 add|rm|show|prune|update) command="$i"; break ;;
1007 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1008 __gitcomp "add rm show prune update"
1014 __gitcomp "$(__git_remotes)"
1017 local i c='' IFS=$'\n'
1018 for i in $(git --git-dir="$(__gitdir)" config --list); do
1036 local cur="${COMP_WORDS[COMP_CWORD]}"
1039 __gitcomp "--mixed --hard --soft"
1043 __gitcomp "$(__git_refs)"
1048 local cur="${COMP_WORDS[COMP_CWORD]}"
1052 --max-count= --max-age= --since= --after=
1053 --min-age= --before= --until=
1055 --author= --committer= --grep=
1058 --numbered --summary
1063 __git_complete_revlist
1068 local cur="${COMP_WORDS[COMP_CWORD]}"
1072 oneline short medium full fuller email raw
1073 " "" "${cur##--pretty=}"
1077 __gitcomp "--pretty="
1086 __gitcomp 'list show apply clear'
1092 while [ $c -lt $COMP_CWORD ]; do
1093 i="${COMP_WORDS[c]}"
1095 add|status|init|update) command="$i"; break ;;
1100 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1101 local cur="${COMP_WORDS[COMP_CWORD]}"
1104 __gitcomp "--quiet --cached"
1107 __gitcomp "add status init update"
1117 while [ $c -lt $COMP_CWORD ]; do
1118 i="${COMP_WORDS[c]}"
1121 __gitcomp "$(__git_tags)"
1131 case "${COMP_WORDS[COMP_CWORD-1]}" in
1137 __gitcomp "$(__git_tags)"
1143 __gitcomp "$(__git_refs)"
1150 local i c=1 command __git_dir
1152 while [ $c -lt $COMP_CWORD ]; do
1153 i="${COMP_WORDS[c]}"
1155 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
1156 --bare) __git_dir="." ;;
1157 --version|--help|-p|--paginate) ;;
1158 *) command="$i"; break ;;
1163 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
1164 case "${COMP_WORDS[COMP_CWORD]}" in
1165 --*=*) COMPREPLY=() ;;
1174 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
1179 local expansion=$(__git_aliased_command "$command")
1180 [ "$expansion" ] && command="$expansion"
1185 apply) _git_apply ;;
1186 bisect) _git_bisect ;;
1187 bundle) _git_bundle ;;
1188 branch) _git_branch ;;
1189 checkout) _git_checkout ;;
1190 cherry) _git_cherry ;;
1191 cherry-pick) _git_cherry_pick ;;
1192 commit) _git_commit ;;
1193 config) _git_config ;;
1194 describe) _git_describe ;;
1196 fetch) _git_fetch ;;
1197 format-patch) _git_format_patch ;;
1200 ls-remote) _git_ls_remote ;;
1201 ls-tree) _git_ls_tree ;;
1203 merge-base) _git_merge_base ;;
1204 name-rev) _git_name_rev ;;
1207 rebase) _git_rebase ;;
1208 remote) _git_remote ;;
1209 reset) _git_reset ;;
1210 shortlog) _git_shortlog ;;
1212 show-branch) _git_log ;;
1213 stash) _git_stash ;;
1214 submodule) _git_submodule ;;
1216 whatchanged) _git_log ;;
1223 local cur="${COMP_WORDS[COMP_CWORD]}"
1226 __gitcomp "--not --all"
1230 __git_complete_revlist
1233 complete -o default -o nospace -F _git git
1234 complete -o default -o nospace -F _gitk gitk
1235 complete -o default -o nospace -F _git_am git-am
1236 complete -o default -o nospace -F _git_apply git-apply
1237 complete -o default -o nospace -F _git_bisect git-bisect
1238 complete -o default -o nospace -F _git_branch git-branch
1239 complete -o default -o nospace -F _git_bundle git-bundle
1240 complete -o default -o nospace -F _git_checkout git-checkout
1241 complete -o default -o nospace -F _git_cherry git-cherry
1242 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
1243 complete -o default -o nospace -F _git_commit git-commit
1244 complete -o default -o nospace -F _git_describe git-describe
1245 complete -o default -o nospace -F _git_diff git-diff
1246 complete -o default -o nospace -F _git_fetch git-fetch
1247 complete -o default -o nospace -F _git_format_patch git-format-patch
1248 complete -o default -o nospace -F _git_gc git-gc
1249 complete -o default -o nospace -F _git_log git-log
1250 complete -o default -o nospace -F _git_ls_remote git-ls-remote
1251 complete -o default -o nospace -F _git_ls_tree git-ls-tree
1252 complete -o default -o nospace -F _git_merge git-merge
1253 complete -o default -o nospace -F _git_merge_base git-merge-base
1254 complete -o default -o nospace -F _git_name_rev git-name-rev
1255 complete -o default -o nospace -F _git_pull git-pull
1256 complete -o default -o nospace -F _git_push git-push
1257 complete -o default -o nospace -F _git_rebase git-rebase
1258 complete -o default -o nospace -F _git_config git-config
1259 complete -o default -o nospace -F _git_remote git-remote
1260 complete -o default -o nospace -F _git_reset git-reset
1261 complete -o default -o nospace -F _git_shortlog git-shortlog
1262 complete -o default -o nospace -F _git_show git-show
1263 complete -o default -o nospace -F _git_stash git-stash
1264 complete -o default -o nospace -F _git_submodule git-submodule
1265 complete -o default -o nospace -F _git_log git-show-branch
1266 complete -o default -o nospace -F _git_tag git-tag
1267 complete -o default -o nospace -F _git_log git-whatchanged
1269 # The following are necessary only for Cygwin, and only are needed
1270 # when the user has tab-completed the executable name and consequently
1271 # included the '.exe' suffix.
1273 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1274 complete -o default -o nospace -F _git_add git-add.exe
1275 complete -o default -o nospace -F _git_apply git-apply.exe
1276 complete -o default -o nospace -F _git git.exe
1277 complete -o default -o nospace -F _git_branch git-branch.exe
1278 complete -o default -o nospace -F _git_bundle git-bundle.exe
1279 complete -o default -o nospace -F _git_cherry git-cherry.exe
1280 complete -o default -o nospace -F _git_describe git-describe.exe
1281 complete -o default -o nospace -F _git_diff git-diff.exe
1282 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1283 complete -o default -o nospace -F _git_log git-log.exe
1284 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1285 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1286 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1287 complete -o default -o nospace -F _git_push git-push.exe
1288 complete -o default -o nospace -F _git_config git-config
1289 complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1290 complete -o default -o nospace -F _git_show git-show.exe
1291 complete -o default -o nospace -F _git_log git-show-branch.exe
1292 complete -o default -o nospace -F _git_tag git-tag.exe
1293 complete -o default -o nospace -F _git_log git-whatchanged.exe