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 diff-files) : plumbing;;
274 diff-index) : plumbing;;
275 diff-tree) : plumbing;;
276 fast-import) : import;;
277 fsck-objects) : plumbing;;
278 fetch--tool) : plumbing;;
279 fetch-pack) : plumbing;;
280 fmt-merge-msg) : plumbing;;
281 for-each-ref) : plumbing;;
282 hash-object) : plumbing;;
283 http-*) : transport;;
284 index-pack) : plumbing;;
285 init-db) : deprecated;;
286 local-fetch) : plumbing;;
287 mailinfo) : plumbing;;
288 mailsplit) : plumbing;;
289 merge-*) : plumbing;;
292 pack-objects) : plumbing;;
293 pack-redundant) : plumbing;;
294 pack-refs) : plumbing;;
295 parse-remote) : plumbing;;
296 patch-id) : plumbing;;
297 peek-remote) : plumbing;;
299 prune-packed) : plumbing;;
300 quiltimport) : import;;
301 read-tree) : plumbing;;
302 receive-pack) : plumbing;;
304 repo-config) : plumbing;;
306 rev-list) : plumbing;;
307 rev-parse) : plumbing;;
308 runstatus) : plumbing;;
309 sh-setup) : internal;;
311 send-pack) : plumbing;;
312 show-index) : plumbing;;
314 stripspace) : plumbing;;
315 svn) : import export;;
316 svnimport) : import;;
317 symbolic-ref) : plumbing;;
318 tar-tree) : deprecated;;
319 unpack-file) : plumbing;;
320 unpack-objects) : plumbing;;
321 update-index) : plumbing;;
322 update-ref) : plumbing;;
323 update-server-info) : daemon;;
324 upload-archive) : plumbing;;
325 upload-pack) : plumbing;;
326 write-tree) : plumbing;;
327 verify-tag) : plumbing;;
333 __git_commandlist="$(__git_commands 2>/dev/null)"
338 for i in $(git --git-dir="$(__gitdir)" config --list); do
348 __git_aliased_command ()
350 local word cmdline=$(git --git-dir="$(__gitdir)" \
351 config --get "alias.$1")
352 for word in $cmdline; do
353 if [ "${word##-*}" ]; then
360 __git_whitespacelist="nowarn warn error error-all strip"
364 local cur="${COMP_WORDS[COMP_CWORD]}"
365 if [ -d .dotest ]; then
366 __gitcomp "--skip --resolved"
371 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
376 --signoff --utf8 --binary --3way --interactive
386 local cur="${COMP_WORDS[COMP_CWORD]}"
389 __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
394 --stat --numstat --summary --check --index
395 --cached --index-info --reverse --reject --unidiff-zero
396 --apply --no-add --exclude=
397 --whitespace= --inaccurate-eof --verbose
406 local cur="${COMP_WORDS[COMP_CWORD]}"
409 __gitcomp "--interactive"
418 while [ $c -lt $COMP_CWORD ]; do
421 start|bad|good|reset|visualize|replay|log)
429 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
430 __gitcomp "start bad good reset visualize replay log"
436 __gitcomp "$(__git_refs)"
446 __gitcomp "$(__git_refs)"
451 __gitcomp "$(__git_refs)"
456 __gitcomp "$(__git_refs)"
461 local cur="${COMP_WORDS[COMP_CWORD]}"
464 __gitcomp "--edit --no-commit"
467 __gitcomp "$(__git_refs)"
474 local cur="${COMP_WORDS[COMP_CWORD]}"
478 --all --author= --signoff --verify --no-verify
479 --edit --amend --include --only
493 __gitcomp "$(__git_refs)"
498 local cur="${COMP_WORDS[COMP_CWORD]}"
500 case "${COMP_WORDS[0]},$COMP_CWORD" in
502 __gitcomp "$(__git_remotes)"
505 __gitcomp "$(__git_remotes)"
510 __gitcomp "$(__git_refs)" "" "${cur#*:}"
514 case "${COMP_WORDS[0]}" in
515 git-fetch) remote="${COMP_WORDS[1]}" ;;
516 git) remote="${COMP_WORDS[2]}" ;;
518 __gitcomp "$(__git_refs2 "$remote")"
527 local cur="${COMP_WORDS[COMP_CWORD]}"
531 --stdout --attach --thread
533 --numbered --start-number
537 --full-index --binary
543 __git_complete_revlist
548 local cur="${COMP_WORDS[COMP_CWORD]}"
560 __gitcomp "$(__git_remotes)"
570 local cur="${COMP_WORDS[COMP_CWORD]}"
574 oneline short medium full fuller email raw
575 " "" "${cur##--pretty=}"
580 --max-count= --max-age= --since= --after=
581 --min-age= --before= --until=
582 --root --not --topo-order --date-order
584 --abbrev-commit --abbrev=
586 --author= --committer= --grep=
588 --pretty= --name-status --name-only
594 __git_complete_revlist
599 local cur="${COMP_WORDS[COMP_CWORD]}"
600 case "${COMP_WORDS[COMP_CWORD-1]}" in
602 __gitcomp "$(__git_merge_strategies)"
607 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
612 --no-commit --no-summary --squash --strategy
616 __gitcomp "$(__git_refs)"
621 __gitcomp "$(__git_refs)"
626 __gitcomp "--tags --all --stdin"
631 local cur="${COMP_WORDS[COMP_CWORD]}"
633 case "${COMP_WORDS[0]},$COMP_CWORD" in
635 __gitcomp "$(__git_remotes)"
638 __gitcomp "$(__git_remotes)"
642 case "${COMP_WORDS[0]}" in
643 git-pull) remote="${COMP_WORDS[1]}" ;;
644 git) remote="${COMP_WORDS[2]}" ;;
646 __gitcomp "$(__git_refs "$remote")"
653 local cur="${COMP_WORDS[COMP_CWORD]}"
655 case "${COMP_WORDS[0]},$COMP_CWORD" in
657 __gitcomp "$(__git_remotes)"
660 __gitcomp "$(__git_remotes)"
666 case "${COMP_WORDS[0]}" in
667 git-push) remote="${COMP_WORDS[1]}" ;;
668 git) remote="${COMP_WORDS[2]}" ;;
670 __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
673 __gitcomp "$(__git_refs2)"
682 local cur="${COMP_WORDS[COMP_CWORD]}"
683 if [ -d .dotest ] || [ -d .git/.dotest-merge ]; then
684 __gitcomp "--continue --skip --abort"
687 case "${COMP_WORDS[COMP_CWORD-1]}" in
689 __gitcomp "$(__git_merge_strategies)"
694 __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
698 __gitcomp "--onto --merge --strategy"
701 __gitcomp "$(__git_refs)"
706 local cur="${COMP_WORDS[COMP_CWORD]}"
707 local prv="${COMP_WORDS[COMP_CWORD-1]}"
710 __gitcomp "$(__git_remotes)"
714 __gitcomp "$(__git_refs)"
718 local remote="${prv#remote.}"
719 remote="${remote%.fetch}"
720 __gitcomp "$(__git_refs_remotes "$remote")"
724 local remote="${prv#remote.}"
725 remote="${remote%.push}"
726 __gitcomp "$(git --git-dir="$(__gitdir)" \
727 for-each-ref --format='%(refname):%(refname)' \
731 pull.twohead|pull.octopus)
732 __gitcomp "$(__git_merge_strategies)"
735 color.branch|color.diff|color.status)
736 __gitcomp "always never auto"
741 black red green yellow blue magenta cyan white
742 bold dim ul blink reverse
754 --global --list --replace-all
755 --get --get-all --get-regexp
756 --add --unset --unset-all
761 local pfx="${cur%.*}."
763 __gitcomp "remote merge" "$pfx" "$cur"
767 local pfx="${cur%.*}."
769 __gitcomp "$(__git_heads)" "$pfx" "$cur" "."
773 local pfx="${cur%.*}."
775 __gitcomp "url fetch push" "$pfx" "$cur"
779 local pfx="${cur%.*}."
781 __gitcomp "$(__git_remotes)" "$pfx" "$cur" "."
790 core.preferSymlinkRefs
791 core.logAllRefUpdates
792 core.repositoryFormatVersion
793 core.sharedRepository
794 core.warnAmbiguousRefs
797 core.packedGitWindowSize
812 color.diff.whitespace
818 color.status.untracked
826 gc.reflogexpireunreachable
839 i18n.logOutputEncoding
846 repack.useDeltaBaseOffset
852 receive.denyNonFastForwards
864 while [ $c -lt $COMP_CWORD ]; do
867 add|show|prune) command="$i"; break ;;
872 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
873 __gitcomp "add show prune"
879 __gitcomp "$(__git_remotes)"
889 local cur="${COMP_WORDS[COMP_CWORD]}"
892 __gitcomp "--mixed --hard --soft"
896 __gitcomp "$(__git_refs)"
901 local cur="${COMP_WORDS[COMP_CWORD]}"
905 --max-count= --max-age= --since= --after=
906 --min-age= --before= --until=
908 --author= --committer= --grep=
916 __git_complete_revlist
921 local cur="${COMP_WORDS[COMP_CWORD]}"
925 oneline short medium full fuller email raw
926 " "" "${cur##--pretty=}"
930 __gitcomp "--pretty="
939 local i c=1 command __git_dir
941 while [ $c -lt $COMP_CWORD ]; do
944 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
945 --bare) __git_dir="." ;;
946 --version|--help|-p|--paginate) ;;
947 *) command="$i"; break ;;
952 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
953 case "${COMP_WORDS[COMP_CWORD]}" in
954 --*=*) COMPREPLY=() ;;
955 --*) __gitcomp "--git-dir= --bare --version --exec-path" ;;
956 *) __gitcomp "$(__git_commands) $(__git_aliases)" ;;
961 local expansion=$(__git_aliased_command "$command")
962 [ "$expansion" ] && command="$expansion"
968 bisect) _git_bisect ;;
969 branch) _git_branch ;;
970 checkout) _git_checkout ;;
971 cherry) _git_cherry ;;
972 cherry-pick) _git_cherry_pick ;;
973 commit) _git_commit ;;
974 config) _git_config ;;
977 format-patch) _git_format_patch ;;
980 ls-remote) _git_ls_remote ;;
981 ls-tree) _git_ls_tree ;;
983 merge-base) _git_merge_base ;;
984 name-rev) _git_name_rev ;;
987 rebase) _git_rebase ;;
988 remote) _git_remote ;;
990 shortlog) _git_shortlog ;;
992 show-branch) _git_log ;;
993 whatchanged) _git_log ;;
1000 local cur="${COMP_WORDS[COMP_CWORD]}"
1003 __gitcomp "--not --all"
1007 __git_complete_revlist
1010 complete -o default -o nospace -F _git git
1011 complete -o default -o nospace -F _gitk gitk
1012 complete -o default -o nospace -F _git_am git-am
1013 complete -o default -o nospace -F _git_apply git-apply
1014 complete -o default -o nospace -F _git_bisect git-bisect
1015 complete -o default -o nospace -F _git_branch git-branch
1016 complete -o default -o nospace -F _git_checkout git-checkout
1017 complete -o default -o nospace -F _git_cherry git-cherry
1018 complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
1019 complete -o default -o nospace -F _git_commit git-commit
1020 complete -o default -o nospace -F _git_diff git-diff
1021 complete -o default -o nospace -F _git_fetch git-fetch
1022 complete -o default -o nospace -F _git_format_patch git-format-patch
1023 complete -o default -o nospace -F _git_gc git-gc
1024 complete -o default -o nospace -F _git_log git-log
1025 complete -o default -o nospace -F _git_ls_remote git-ls-remote
1026 complete -o default -o nospace -F _git_ls_tree git-ls-tree
1027 complete -o default -o nospace -F _git_merge git-merge
1028 complete -o default -o nospace -F _git_merge_base git-merge-base
1029 complete -o default -o nospace -F _git_name_rev git-name-rev
1030 complete -o default -o nospace -F _git_pull git-pull
1031 complete -o default -o nospace -F _git_push git-push
1032 complete -o default -o nospace -F _git_rebase git-rebase
1033 complete -o default -o nospace -F _git_config git-config
1034 complete -o default -o nospace -F _git_remote git-remote
1035 complete -o default -o nospace -F _git_reset git-reset
1036 complete -o default -o nospace -F _git_shortlog git-shortlog
1037 complete -o default -o nospace -F _git_show git-show
1038 complete -o default -o nospace -F _git_log git-show-branch
1039 complete -o default -o nospace -F _git_log git-whatchanged
1041 # The following are necessary only for Cygwin, and only are needed
1042 # when the user has tab-completed the executable name and consequently
1043 # included the '.exe' suffix.
1045 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
1046 complete -o default -o nospace -F _git_add git-add.exe
1047 complete -o default -o nospace -F _git_apply git-apply.exe
1048 complete -o default -o nospace -F _git git.exe
1049 complete -o default -o nospace -F _git_branch git-branch.exe
1050 complete -o default -o nospace -F _git_cherry git-cherry.exe
1051 complete -o default -o nospace -F _git_diff git-diff.exe
1052 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
1053 complete -o default -o nospace -F _git_log git-log.exe
1054 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
1055 complete -o default -o nospace -F _git_merge_base git-merge-base.exe
1056 complete -o default -o nospace -F _git_name_rev git-name-rev.exe
1057 complete -o default -o nospace -F _git_push git-push.exe
1058 complete -o default -o nospace -F _git_config git-config
1059 complete -o default -o nospace -F _git_shortlog git-shortlog.exe
1060 complete -o default -o nospace -F _git_show git-show.exe
1061 complete -o default -o nospace -F _git_log git-show-branch.exe
1062 complete -o default -o nospace -F _git_log git-whatchanged.exe