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 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
227 __git_complete_revlist
()
229 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
234 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
239 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
242 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
249 if [ -n "$__git_commandlist" ]; then
250 echo "$__git_commandlist"
254 for i
in $
(git
help -a|
egrep '^ ')
257 add--interactive
) : plumbing
;;
258 applymbox
) : ask gittus
;;
259 applypatch
) : ask gittus
;;
260 archimport
) : import
;;
261 cat-file
) : plumbing
;;
262 check-ref-format
) : plumbing
;;
263 commit-tree
) : plumbing
;;
264 convert-objects
) : plumbing
;;
265 cvsexportcommit
) : export;;
266 cvsimport
) : import
;;
267 cvsserver
) : daemon
;;
269 fsck-objects
) : plumbing
;;
270 fetch-pack
) : plumbing
;;
271 fmt-merge-msg
) : plumbing
;;
272 hash-object
) : plumbing
;;
273 http-
*) : transport
;;
274 index-pack
) : plumbing
;;
275 init-db
) : deprecated
;;
276 local-fetch
) : plumbing
;;
277 mailinfo
) : plumbing
;;
278 mailsplit
) : plumbing
;;
279 merge-
*) : plumbing
;;
282 pack-objects
) : plumbing
;;
283 pack-redundant
) : plumbing
;;
284 pack-refs
) : plumbing
;;
285 parse-remote
) : plumbing
;;
286 patch-id
) : plumbing
;;
287 peek-remote
) : plumbing
;;
289 prune-packed
) : plumbing
;;
290 quiltimport
) : import
;;
291 read-tree
) : plumbing
;;
292 receive-pack
) : plumbing
;;
294 repo-config
) : plumbing
;;
296 rev-list
) : plumbing
;;
297 rev-parse
) : plumbing
;;
298 runstatus
) : plumbing
;;
299 sh-setup
) : internal
;;
301 send-pack
) : plumbing
;;
302 show-index
) : plumbing
;;
304 stripspace
) : plumbing
;;
305 svn
) : import
export;;
306 svnimport
) : import
;;
307 symbolic-ref
) : plumbing
;;
308 tar-tree
) : deprecated
;;
309 unpack-file
) : plumbing
;;
310 unpack-objects
) : plumbing
;;
311 update-index
) : plumbing
;;
312 update-ref
) : plumbing
;;
313 update-server-info
) : daemon
;;
314 upload-archive
) : plumbing
;;
315 upload-pack
) : plumbing
;;
316 write-tree
) : plumbing
;;
317 verify-tag
) : plumbing
;;
323 __git_commandlist
="$(__git_commands 2>/dev/null)"
328 for i
in $
(git
--git-dir="$(__gitdir)" config
--list); do
338 __git_aliased_command
()
340 local word cmdline
=$
(git
--git-dir="$(__gitdir)" \
341 config
--get "alias.$1")
342 for word
in $cmdline; do
343 if [ "${word##-*}" ]; then
350 __git_whitespacelist
="nowarn warn error error-all strip"
354 local cur
="${COMP_WORDS[COMP_CWORD]}"
355 if [ -d .dotest
]; then
356 COMPREPLY
=($
(compgen
-W "
363 COMPREPLY
=($
(compgen
-W "$__git_whitespacelist" \
364 -- "${cur##--whitespace=}"))
368 COMPREPLY
=($
(compgen
-W "
369 --signoff --utf8 --binary --3way --interactive
379 local cur
="${COMP_WORDS[COMP_CWORD]}"
382 COMPREPLY
=($
(compgen
-W "$__git_whitespacelist" \
383 -- "${cur##--whitespace=}"))
387 COMPREPLY
=($
(compgen
-W "
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 COMPREPLY
=($
(compgen
-W "
413 local cur
="${COMP_WORDS[COMP_CWORD]}"
414 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
419 local cur
="${COMP_WORDS[COMP_CWORD]}"
420 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
425 local cur
="${COMP_WORDS[COMP_CWORD]}"
428 COMPREPLY
=($
(compgen
-W "
433 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
440 local cur
="${COMP_WORDS[COMP_CWORD]}"
443 COMPREPLY
=($
(compgen
-W "
444 --all --author= --signoff --verify --no-verify
445 --edit --amend --include --only
459 local cur
="${COMP_WORDS[COMP_CWORD]}"
460 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
465 local cur
="${COMP_WORDS[COMP_CWORD]}"
467 case "${COMP_WORDS[0]},$COMP_CWORD" in
469 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
472 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
478 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
482 case "${COMP_WORDS[0]}" in
483 git-fetch
) remote
="${COMP_WORDS[1]}" ;;
484 git
) remote
="${COMP_WORDS[2]}" ;;
486 COMPREPLY
=($
(compgen
-W "$(__git_refs2 "$remote")" -- "$cur"))
495 local cur
="${COMP_WORDS[COMP_CWORD]}"
498 COMPREPLY
=($
(compgen
-W "
499 --stdout --attach --thread
501 --numbered --start-number
505 --full-index --binary
510 __git_complete_revlist
515 local cur
="${COMP_WORDS[COMP_CWORD]}"
516 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
526 local cur
="${COMP_WORDS[COMP_CWORD]}"
529 COMPREPLY
=($
(compgen
-W "
530 oneline short medium full fuller email raw
531 " -- "${cur##--pretty=}"))
535 COMPREPLY
=($
(compgen
-W "
536 --max-count= --max-age= --since= --after=
537 --min-age= --before= --until=
538 --root --not --topo-order --date-order
540 --abbrev-commit --abbrev=
542 --author= --committer= --grep=
544 --pretty= --name-status --name-only
549 __git_complete_revlist
554 local cur
="${COMP_WORDS[COMP_CWORD]}"
555 case "${COMP_WORDS[COMP_CWORD-1]}" in
557 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
562 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" \
563 -- "${cur##--strategy=}"))
567 COMPREPLY
=($
(compgen
-W "
568 --no-commit --no-summary --squash --strategy
572 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
577 local cur
="${COMP_WORDS[COMP_CWORD]}"
578 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
583 local cur
="${COMP_WORDS[COMP_CWORD]}"
584 COMPREPLY
=($
(compgen
-W "--tags --all --stdin" -- "$cur"))
589 local cur
="${COMP_WORDS[COMP_CWORD]}"
591 case "${COMP_WORDS[0]},$COMP_CWORD" in
593 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
596 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
600 case "${COMP_WORDS[0]}" in
601 git-pull
) remote
="${COMP_WORDS[1]}" ;;
602 git
) remote
="${COMP_WORDS[2]}" ;;
604 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
611 local cur
="${COMP_WORDS[COMP_CWORD]}"
613 case "${COMP_WORDS[0]},$COMP_CWORD" in
615 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
618 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
624 case "${COMP_WORDS[0]}" in
625 git-push
) remote
="${COMP_WORDS[1]}" ;;
626 git
) remote
="${COMP_WORDS[2]}" ;;
629 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
632 COMPREPLY
=($
(compgen
-W "$(__git_refs2)" -- "$cur"))
641 local cur
="${COMP_WORDS[COMP_CWORD]}"
642 if [ -d .dotest
]; then
643 COMPREPLY
=($
(compgen
-W "
644 --continue --skip --abort
648 case "${COMP_WORDS[COMP_CWORD-1]}" in
650 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
655 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" \
656 -- "${cur##--strategy=}"))
660 COMPREPLY
=($
(compgen
-W "
661 --onto --merge --strategy
665 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
670 local cur
="${COMP_WORDS[COMP_CWORD]}"
671 local prv
="${COMP_WORDS[COMP_CWORD-1]}"
674 __gitcomp
"$(__git_remotes)"
678 __gitcomp
"$(__git_refs)"
682 local remote
="${prv#remote.}"
683 remote
="${remote%.fetch}"
684 __gitcomp
"$(__git_refs_remotes "$remote")"
688 local remote
="${prv#remote.}"
689 remote
="${remote%.push}"
690 __gitcomp
"$(git --git-dir="$
(__gitdir
)" \
691 for-each-ref --format='%(refname):%(refname)' \
695 pull.twohead|pull.octopus
)
696 __gitcomp
"$(__git_merge_strategies)"
699 color.branch|color.
diff|color.status
)
700 __gitcomp
"always never auto"
705 black red green yellow blue magenta cyan white
706 bold dim ul blink reverse
718 --global --list --replace-all
719 --get --get-all --get-regexp
725 local pfx
="${cur%.*}."
727 __gitcomp
"remote merge" "$pfx" "$cur"
731 local pfx
="${cur%.*}."
733 __gitcomp
"$(__git_heads)" "$pfx" "$cur" "."
737 local pfx
="${cur%.*}."
739 __gitcomp
"url fetch push" "$pfx" "$cur"
743 local pfx
="${cur%.*}."
745 __gitcomp
"$(__git_remotes)" "$pfx" "$cur" "."
754 core.preferSymlinkRefs
755 core.logAllRefUpdates
756 core.repositoryFormatVersion
757 core.sharedRepository
758 core.warnAmbiguousRefs
761 core.packedGitWindowSize
775 color.diff.whitespace
781 color.status.untracked
789 gc.reflogexpireunreachable
802 i18n.logOutputEncoding
809 repack.useDeltaBaseOffset
815 receive.denyNonFastForwards
826 local cur
="${COMP_WORDS[COMP_CWORD]}"
827 local opt
="--mixed --hard --soft"
828 COMPREPLY
=($
(compgen
-W "$opt $(__git_refs)" -- "$cur"))
833 local cur
="${COMP_WORDS[COMP_CWORD]}"
836 COMPREPLY
=($
(compgen
-W "
837 oneline short medium full fuller email raw
838 " -- "${cur##--pretty=}"))
842 COMPREPLY
=($
(compgen
-W "--pretty=" -- "$cur"))
851 local i c
=1 command __git_dir
853 while [ $c -lt $COMP_CWORD ]; do
856 --git-dir=*) __git_dir
="${i#--git-dir=}" ;;
857 --bare) __git_dir
="." ;;
858 --version|
--help|
-p|
--paginate) ;;
859 *) command="$i"; break ;;
864 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
865 case "${COMP_WORDS[COMP_CWORD]}" in
866 --*=*) COMPREPLY
=() ;;
867 --*) __gitcomp
"--git-dir= --bare --version --exec-path" ;;
868 *) __gitcomp
"$(__git_commands) $(__git_aliases)" ;;
873 local expansion
=$
(__git_aliased_command
"$command")
874 [ "$expansion" ] && command="$expansion"
880 branch
) _git_branch
;;
881 checkout
) _git_checkout
;;
882 cherry-pick
) _git_cherry_pick
;;
883 commit
) _git_commit
;;
884 config
) _git_config
;;
886 diff-tree
) _git_diff_tree
;;
888 format-patch
) _git_format_patch
;;
890 ls-remote
) _git_ls_remote
;;
891 ls-tree
) _git_ls_tree
;;
893 merge-base
) _git_merge_base
;;
894 name-rev
) _git_name_rev
;;
897 rebase
) _git_rebase
;;
900 show-branch
) _git_log
;;
901 whatchanged
) _git_log
;;
908 local cur
="${COMP_WORDS[COMP_CWORD]}"
909 COMPREPLY
=($
(compgen
-W "--all $(__git_refs)" -- "$cur"))
912 complete
-o default
-o nospace
-F _git git
913 complete
-o default
-F _gitk gitk
914 complete
-o default
-F _git_am git-am
915 complete
-o default
-F _git_apply git-apply
916 complete
-o default
-F _git_branch git-branch
917 complete
-o default
-F _git_checkout git-checkout
918 complete
-o default
-F _git_cherry_pick git-cherry-pick
919 complete
-o default
-F _git_commit git-commit
920 complete
-o default
-o nospace
-F _git_diff git-diff
921 complete
-o default
-F _git_diff_tree git-diff-tree
922 complete
-o default
-o nospace
-F _git_fetch git-fetch
923 complete
-o default
-o nospace
-F _git_format_patch git-format-patch
924 complete
-o default
-o nospace
-F _git_log git-log
925 complete
-o default
-F _git_ls_remote git-ls-remote
926 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree
927 complete
-o default
-F _git_merge git-merge
928 complete
-o default
-F _git_merge_base git-merge-base
929 complete
-o default
-F _git_name_rev git-name-rev
930 complete
-o default
-o nospace
-F _git_pull git-pull
931 complete
-o default
-o nospace
-F _git_push git-push
932 complete
-o default
-F _git_rebase git-rebase
933 complete
-o default
-F _git_config git-config
934 complete
-o default
-F _git_reset git-reset
935 complete
-o default
-o nospace
-F _git_show git-show
936 complete
-o default
-o nospace
-F _git_log git-show-branch
937 complete
-o default
-o nospace
-F _git_log git-whatchanged
939 # The following are necessary only for Cygwin, and only are needed
940 # when the user has tab-completed the executable name and consequently
941 # included the '.exe' suffix.
943 if [ Cygwin
= "$(uname -o 2>/dev/null)" ]; then
944 complete
-o default
-F _git_add git-add.exe
945 complete
-o default
-F _git_apply git-apply.exe
946 complete
-o default
-o nospace
-F _git git.exe
947 complete
-o default
-F _git_branch git-branch.exe
948 complete
-o default
-o nospace
-F _git_diff git-diff.exe
949 complete
-o default
-o nospace
-F _git_diff_tree git-diff-tree.exe
950 complete
-o default
-o nospace
-F _git_format_patch git-format-patch.exe
951 complete
-o default
-o nospace
-F _git_log git-log.exe
952 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree.exe
953 complete
-o default
-F _git_merge_base git-merge-base.exe
954 complete
-o default
-F _git_name_rev git-name-rev.exe
955 complete
-o default
-o nospace
-F _git_push git-push.exe
956 complete
-o default
-F _git_config git-config
957 complete
-o default
-o nospace
-F _git_show git-show.exe
958 complete
-o default
-o nospace
-F _git_log git-show-branch.exe
959 complete
-o default
-o nospace
-F _git_log git-whatchanged.exe