2 # bash completion support for core Git.
4 # Copyright (C) 2006 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) Consider changing your PS1 to also show the current branch:
22 # PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
24 # The argument to __git_ps1 will be displayed only if you
25 # are currently in a git repository. The %s token will be
26 # the name of the current branch.
31 echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
36 local b
="$(git symbolic-ref HEAD 2>/dev/null)"
39 printf "$1" "${b##refs/heads/}"
41 printf " (%s)" "${b##refs/heads/}"
48 local cmd i is_hash
=y dir
="${1:-$(__gitdir)}"
49 if [ -d "$dir" ]; then
50 for i
in $
(git
--git-dir="$dir" \
51 for-each-ref
--format='%(refname)' \
53 echo "${i#refs/heads/}"
57 for i
in $
(git-ls-remote
"$dir" 2>/dev
/null
); do
61 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}" ;;
62 n
,*) is_hash
=y
; echo "$i" ;;
69 local cmd i is_hash
=y dir
="${1:-$(__gitdir)}"
70 if [ -d "$dir" ]; then
71 if [ -e "$dir/HEAD" ]; then echo HEAD
; fi
72 for i
in $
(git
--git-dir="$dir" \
73 for-each-ref
--format='%(refname)' \
74 refs
/tags refs
/heads refs
/remotes
); do
76 refs
/tags
/*) echo "${i#refs/tags/}" ;;
77 refs
/heads
/*) echo "${i#refs/heads/}" ;;
78 refs
/remotes
/*) echo "${i#refs/remotes/}" ;;
84 for i
in $
(git-ls-remote
"$dir" 2>/dev
/null
); do
88 n
,refs
/tags
/*) is_hash
=y
; echo "${i#refs/tags/}" ;;
89 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}" ;;
90 n
,refs
/remotes
/*) is_hash
=y
; echo "${i#refs/remotes/}" ;;
91 n
,*) is_hash
=y
; echo "$i" ;;
98 local cmd i is_hash
=y dir
="${1:-$(__gitdir)}"
99 if [ -d "$dir" ]; then
104 for i
in $
($cmd "$dir" 2>/dev
/null
); do
105 case "$is_hash,$i" in
108 n
,refs
/tags
/*) is_hash
=y
; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
109 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
110 n
,*) is_hash
=y
; echo "$i:$i" ;;
115 __git_refs_remotes
()
117 local cmd i is_hash
=y
118 for i
in $
(git-ls-remote
"$1" 2>/dev
/null
); do
119 case "$is_hash,$i" in
122 echo "$i:refs/remotes/$1/${i#refs/heads/}"
126 n
,refs
/tags
/*) is_hash
=y
;;
134 local i ngoff IFS
=$
'\n' d
="$(__gitdir)"
135 shopt -q nullglob || ngoff
=1
137 for i
in "$d/remotes"/*; do
138 echo ${i#$d/remotes/}
140 [ "$ngoff" ] && shopt -u nullglob
141 for i
in $
(git
--git-dir="$d" repo-config
--list); do
151 __git_merge_strategies
()
153 sed -n "/^all_strategies='/{
154 s/^all_strategies='//
158 }" "$(git --exec-path)/git-merge"
161 __git_complete_file
()
163 local pfx
ls ref cur
="${COMP_WORDS[COMP_CWORD]}"
179 COMPREPLY
=($
(compgen
-P "$pfx" \
180 -W "$(git --git-dir="$
(__gitdir
)" ls-tree "$ls" \
181 | sed '/^100... blob /s,^.* ,,
190 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
195 __git_complete_revlist
()
197 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
202 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
207 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
210 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
218 for i
in $
(git
help -a|
egrep '^ ')
221 check-ref-format
) : plumbing
;;
222 commit-tree
) : plumbing
;;
223 convert-objects
) : plumbing
;;
224 cvsserver
) : daemon
;;
226 fetch-pack
) : plumbing
;;
227 hash-object
) : plumbing
;;
228 http-
*) : transport
;;
229 index-pack
) : plumbing
;;
230 local-fetch
) : plumbing
;;
231 mailinfo
) : plumbing
;;
232 mailsplit
) : plumbing
;;
233 merge-
*) : plumbing
;;
236 pack-objects
) : plumbing
;;
237 pack-redundant
) : plumbing
;;
238 pack-refs
) : plumbing
;;
239 parse-remote
) : plumbing
;;
240 patch-id
) : plumbing
;;
241 peek-remote
) : plumbing
;;
242 read-tree
) : plumbing
;;
243 receive-pack
) : plumbing
;;
245 rev-list
) : plumbing
;;
246 rev-parse
) : plumbing
;;
247 runstatus
) : plumbing
;;
248 sh-setup
) : internal
;;
250 send-pack
) : plumbing
;;
251 show-index
) : plumbing
;;
253 stripspace
) : plumbing
;;
254 symbolic-ref
) : plumbing
;;
255 unpack-file
) : plumbing
;;
256 unpack-objects
) : plumbing
;;
257 update-ref
) : plumbing
;;
258 update-server-info
) : daemon
;;
259 upload-archive
) : plumbing
;;
260 upload-pack
) : plumbing
;;
261 write-tree
) : plumbing
;;
270 for i
in $
(git
--git-dir="$(__gitdir)" repo-config
--list); do
280 __git_aliased_command
()
282 local word cmdline
=$
(git
--git-dir="$(__gitdir)" \
283 repo-config
--get "alias.$1")
284 for word
in $cmdline; do
285 if [ "${word##-*}" ]; then
294 local cur
="${COMP_WORDS[COMP_CWORD]}"
295 COMPREPLY
=($
(compgen
-W "-l -f -d -D $(__git_refs)" -- "$cur"))
300 local cur
="${COMP_WORDS[COMP_CWORD]}"
301 case "${COMP_WORDS[0]},$COMP_CWORD" in
303 COMPREPLY
=($
(compgen
-W "-p -t blob tree commit tag" -- "$cur"))
306 COMPREPLY
=($
(compgen
-W "-p -t blob tree commit tag" -- "$cur"))
316 local cur
="${COMP_WORDS[COMP_CWORD]}"
317 COMPREPLY
=($
(compgen
-W "-l -b $(__git_refs)" -- "$cur"))
322 local cur
="${COMP_WORDS[COMP_CWORD]}"
325 COMPREPLY
=($
(compgen
-W "
330 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
342 local cur
="${COMP_WORDS[COMP_CWORD]}"
343 COMPREPLY
=($
(compgen
-W "-r -p -M $(__git_refs)" -- "$cur"))
348 local cur
="${COMP_WORDS[COMP_CWORD]}"
350 case "${COMP_WORDS[0]},$COMP_CWORD" in
352 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
355 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
361 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
365 case "${COMP_WORDS[0]}" in
366 git-fetch
) remote
="${COMP_WORDS[1]}" ;;
367 git
) remote
="${COMP_WORDS[2]}" ;;
369 COMPREPLY
=($
(compgen
-W "$(__git_refs2 "$remote")" -- "$cur"))
378 local cur
="${COMP_WORDS[COMP_CWORD]}"
381 COMPREPLY
=($
(compgen
-W "
382 --stdout --attach --thread
384 --numbered --start-number
388 --full-index --binary
393 __git_complete_revlist
398 local cur
="${COMP_WORDS[COMP_CWORD]}"
399 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
409 local cur
="${COMP_WORDS[COMP_CWORD]}"
412 COMPREPLY
=($
(compgen
-W "
413 oneline short medium full fuller email raw
414 " -- "${cur##--pretty=}"))
418 COMPREPLY
=($
(compgen
-W "
419 --max-count= --max-age= --since= --after=
420 --min-age= --before= --until=
421 --root --not --topo-order --date-order
423 --abbrev-commit --abbrev=
425 --author= --committer= --grep=
427 --pretty= --name-status --name-only
432 __git_complete_revlist
437 local cur
="${COMP_WORDS[COMP_CWORD]}"
438 case "${COMP_WORDS[COMP_CWORD-1]}" in
440 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
445 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" \
446 -- "${cur##--strategy=}"))
450 COMPREPLY
=($
(compgen
-W "
451 --no-commit --no-summary --squash --strategy
455 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
460 local cur
="${COMP_WORDS[COMP_CWORD]}"
461 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
466 local cur
="${COMP_WORDS[COMP_CWORD]}"
467 COMPREPLY
=($
(compgen
-W "--tags --all --stdin" -- "$cur"))
472 local cur
="${COMP_WORDS[COMP_CWORD]}"
474 case "${COMP_WORDS[0]},$COMP_CWORD" in
476 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
479 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
483 case "${COMP_WORDS[0]}" in
484 git-pull
) remote
="${COMP_WORDS[1]}" ;;
485 git
) remote
="${COMP_WORDS[2]}" ;;
487 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
494 local cur
="${COMP_WORDS[COMP_CWORD]}"
496 case "${COMP_WORDS[0]},$COMP_CWORD" in
498 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
501 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
507 case "${COMP_WORDS[0]}" in
508 git-push
) remote
="${COMP_WORDS[1]}" ;;
509 git
) remote
="${COMP_WORDS[2]}" ;;
512 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
515 COMPREPLY
=($
(compgen
-W "$(__git_refs2)" -- "$cur"))
524 local cur
="${COMP_WORDS[COMP_CWORD]}"
525 if [ -d .dotest
]; then
526 COMPREPLY
=($
(compgen
-W "
527 --continue --skip --abort
531 case "${COMP_WORDS[COMP_CWORD-1]}" in
533 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
538 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" \
539 -- "${cur##--strategy=}"))
543 COMPREPLY
=($
(compgen
-W "
544 --onto --merge --strategy
548 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
553 local cur
="${COMP_WORDS[COMP_CWORD]}"
554 local prv
="${COMP_WORDS[COMP_CWORD-1]}"
557 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
561 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
565 local remote
="${prv#remote.}"
566 remote
="${remote%.fetch}"
567 COMPREPLY
=($
(compgen
-W "$(__git_refs_remotes "$remote")" \
572 local remote
="${prv#remote.}"
573 remote
="${remote%.push}"
574 COMPREPLY
=($
(compgen
-W "$(git --git-dir="$
(__gitdir
)" \
575 for-each-ref --format='%(refname):%(refname)' \
576 refs/heads)" -- "$cur"))
586 COMPREPLY
=($
(compgen
-W "
587 --global --list --replace-all
588 --get --get-all --get-regexp
594 local pfx
="${cur%.*}."
596 COMPREPLY
=($
(compgen
-P "$pfx" -W "remote merge" -- "$cur"))
600 local pfx
="${cur%.*}."
602 COMPREPLY
=($
(compgen
-P "$pfx" -S . \
603 -W "$(__git_heads)" -- "$cur"))
607 local pfx
="${cur%.*}."
609 COMPREPLY
=($
(compgen
-P "$pfx" -W "url fetch push" -- "$cur"))
613 local pfx
="${cur%.*}."
615 COMPREPLY
=($
(compgen
-P "$pfx" -S . \
616 -W "$(__git_remotes)" -- "$cur"))
620 COMPREPLY
=($
(compgen
-W "
625 core.preferSymlinkRefs
626 core.logAllRefUpdates
627 core.repositoryFormatVersion
628 core.sharedRepository
629 core.warnAmbiguousRefs
648 http.lowSpeedLimit http.lowSpeedTime
651 repack.useDeltaBaseOffset
652 pull.octopus pull.twohead
655 receive.denyNonFastForwards
666 local cur
="${COMP_WORDS[COMP_CWORD]}"
667 local opt
="--mixed --hard --soft"
668 COMPREPLY
=($
(compgen
-W "$opt $(__git_refs)" -- "$cur"))
673 local i c
=1 command __git_dir
675 while [ $c -lt $COMP_CWORD ]; do
678 --git-dir=*) __git_dir
="${i#--git-dir=}" ;;
679 --bare) __git_dir
="." ;;
680 --version|
--help|
-p|
--paginate) ;;
681 *) command="$i"; break ;;
686 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
687 COMPREPLY
=($
(compgen
-W "
688 --git-dir= --version --exec-path
691 " -- "${COMP_WORDS[COMP_CWORD]}"))
695 local expansion
=$
(__git_aliased_command
"$command")
696 [ "$expansion" ] && command="$expansion"
699 branch
) _git_branch
;;
700 cat-file
) _git_cat_file
;;
701 checkout
) _git_checkout
;;
702 cherry-pick
) _git_cherry_pick
;;
704 diff-tree
) _git_diff_tree
;;
706 format-patch
) _git_format_patch
;;
708 ls-remote
) _git_ls_remote
;;
709 ls-tree
) _git_ls_tree
;;
711 merge-base
) _git_merge_base
;;
712 name-rev
) _git_name_rev
;;
715 rebase
) _git_rebase
;;
716 repo-config
) _git_repo_config
;;
719 show-branch
) _git_log
;;
720 whatchanged
) _git_log
;;
727 local cur
="${COMP_WORDS[COMP_CWORD]}"
728 COMPREPLY
=($
(compgen
-W "--all $(__git_refs)" -- "$cur"))
731 complete
-o default
-o nospace
-F _git git
732 complete
-o default
-F _gitk gitk
733 complete
-o default
-F _git_branch git-branch
734 complete
-o default
-o nospace
-F _git_cat_file git-cat-file
735 complete
-o default
-F _git_checkout git-checkout
736 complete
-o default
-F _git_cherry_pick git-cherry-pick
737 complete
-o default
-o nospace
-F _git_diff git-diff
738 complete
-o default
-F _git_diff_tree git-diff-tree
739 complete
-o default
-o nospace
-F _git_fetch git-fetch
740 complete
-o default
-o nospace
-F _git_format_patch git-format-patch
741 complete
-o default
-o nospace
-F _git_log git-log
742 complete
-o default
-F _git_ls_remote git-ls-remote
743 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree
744 complete
-o default
-F _git_merge git-merge
745 complete
-o default
-F _git_merge_base git-merge-base
746 complete
-o default
-F _git_name_rev git-name-rev
747 complete
-o default
-o nospace
-F _git_pull git-pull
748 complete
-o default
-o nospace
-F _git_push git-push
749 complete
-o default
-F _git_rebase git-rebase
750 complete
-o default
-F _git_repo_config git-repo-config
751 complete
-o default
-F _git_reset git-reset
752 complete
-o default
-F _git_log git-show
753 complete
-o default
-o nospace
-F _git_log git-show-branch
754 complete
-o default
-o nospace
-F _git_log git-whatchanged
756 # The following are necessary only for Cygwin, and only are needed
757 # when the user has tab-completed the executable name and consequently
758 # included the '.exe' suffix.
760 if [ Cygwin
= "$(uname -o 2>/dev/null)" ]; then
761 complete
-o default
-o nospace
-F _git git.exe
762 complete
-o default
-F _git_branch git-branch.exe
763 complete
-o default
-o nospace
-F _git_cat_file git-cat-file.exe
764 complete
-o default
-o nospace
-F _git_diff git-diff.exe
765 complete
-o default
-o nospace
-F _git_diff_tree git-diff-tree.exe
766 complete
-o default
-o nospace
-F _git_format_patch git-format-patch.exe
767 complete
-o default
-o nospace
-F _git_log git-log.exe
768 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree.exe
769 complete
-o default
-F _git_merge_base git-merge-base.exe
770 complete
-o default
-F _git_name_rev git-name-rev.exe
771 complete
-o default
-o nospace
-F _git_push git-push.exe
772 complete
-o default
-F _git_repo_config git-repo-config
773 complete
-o default
-o nospace
-F _git_log git-show.exe
774 complete
-o default
-o nospace
-F _git_log git-show-branch.exe
775 complete
-o default
-o nospace
-F _git_log git-whatchanged.exe