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
54 for i
in $
($cmd "$dir" 2>/dev
/null
); do
58 n
,refs
/tags
/*) is_hash
=y
; echo "${i#refs/tags/}" ;;
59 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}" ;;
60 n
,*) is_hash
=y
; echo "$i" ;;
67 local cmd i is_hash
=y dir
="${1:-$(__gitdir)}"
68 if [ -d "$dir" ]; then
73 for i
in $
($cmd "$dir" 2>/dev
/null
); do
77 n
,refs
/tags
/*) is_hash
=y
; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
78 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
79 n
,*) is_hash
=y
; echo "$i:$i" ;;
86 local i ngoff IFS
=$
'\n' d
="$(__gitdir)"
87 shopt -q nullglob || ngoff
=1
89 for i
in "$d/remotes"/*; do
92 [ "$ngoff" ] && shopt -u nullglob
93 for i
in $
(git
--git-dir="$d" repo-config
--list); do
103 __git_merge_strategies
()
105 sed -n "/^all_strategies='/{
106 s/^all_strategies='//
110 }" "$(git --exec-path)/git-merge"
113 __git_complete_file
()
115 local pfx
ls ref cur
="${COMP_WORDS[COMP_CWORD]}"
131 COMPREPLY
=($
(compgen
-P "$pfx" \
132 -W "$(git --git-dir="$
(__gitdir
)" ls-tree "$ls" \
133 | sed '/^100... blob /s,^.* ,,
142 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
147 __git_complete_revlist
()
149 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
154 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
159 COMPREPLY
=($
(compgen
-P "$pfx" -W "$(__git_refs)" -- "$cur"))
162 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
170 for i
in $
(git
help -a|
egrep '^ ')
173 check-ref-format
) : plumbing
;;
174 commit-tree
) : plumbing
;;
175 convert-objects
) : plumbing
;;
176 cvsserver
) : daemon
;;
178 fetch-pack
) : plumbing
;;
179 hash-object
) : plumbing
;;
180 http-
*) : transport
;;
181 index-pack
) : plumbing
;;
182 local-fetch
) : plumbing
;;
183 mailinfo
) : plumbing
;;
184 mailsplit
) : plumbing
;;
185 merge-
*) : plumbing
;;
188 pack-objects
) : plumbing
;;
189 pack-redundant
) : plumbing
;;
190 pack-refs
) : plumbing
;;
191 parse-remote
) : plumbing
;;
192 patch-id
) : plumbing
;;
193 peek-remote
) : plumbing
;;
194 read-tree
) : plumbing
;;
195 receive-pack
) : plumbing
;;
197 rev-list
) : plumbing
;;
198 rev-parse
) : plumbing
;;
199 runstatus
) : plumbing
;;
200 sh-setup
) : internal
;;
202 send-pack
) : plumbing
;;
203 show-index
) : plumbing
;;
205 stripspace
) : plumbing
;;
206 symbolic-ref
) : plumbing
;;
207 unpack-file
) : plumbing
;;
208 unpack-objects
) : plumbing
;;
209 update-ref
) : plumbing
;;
210 update-server-info
) : daemon
;;
211 upload-archive
) : plumbing
;;
212 upload-pack
) : plumbing
;;
213 write-tree
) : plumbing
;;
222 for i
in $
(git
--git-dir="$(__gitdir)" repo-config
--list); do
232 __git_aliased_command
()
234 local word cmdline
=$
(git
--git-dir="$(__gitdir)" \
235 repo-config
--get "alias.$1")
236 for word
in $cmdline; do
237 if [ "${word##-*}" ]; then
246 local cur
="${COMP_WORDS[COMP_CWORD]}"
247 COMPREPLY
=($
(compgen
-W "-l -f -d -D $(__git_refs)" -- "$cur"))
252 local cur
="${COMP_WORDS[COMP_CWORD]}"
253 case "${COMP_WORDS[0]},$COMP_CWORD" in
255 COMPREPLY
=($
(compgen
-W "-p -t blob tree commit tag" -- "$cur"))
258 COMPREPLY
=($
(compgen
-W "-p -t blob tree commit tag" -- "$cur"))
268 local cur
="${COMP_WORDS[COMP_CWORD]}"
269 COMPREPLY
=($
(compgen
-W "-l -b $(__git_refs)" -- "$cur"))
274 local cur
="${COMP_WORDS[COMP_CWORD]}"
277 COMPREPLY
=($
(compgen
-W "
282 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
294 local cur
="${COMP_WORDS[COMP_CWORD]}"
295 COMPREPLY
=($
(compgen
-W "-r -p -M $(__git_refs)" -- "$cur"))
300 local cur
="${COMP_WORDS[COMP_CWORD]}"
302 case "${COMP_WORDS[0]},$COMP_CWORD" in
304 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
307 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
313 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
317 case "${COMP_WORDS[0]}" in
318 git-fetch
) remote
="${COMP_WORDS[1]}" ;;
319 git
) remote
="${COMP_WORDS[2]}" ;;
321 COMPREPLY
=($
(compgen
-W "$(__git_refs2 "$remote")" -- "$cur"))
330 local cur
="${COMP_WORDS[COMP_CWORD]}"
333 COMPREPLY
=($
(compgen
-W "
334 --stdout --attach --thread
336 --numbered --start-number
340 --full-index --binary
345 __git_complete_revlist
350 local cur
="${COMP_WORDS[COMP_CWORD]}"
351 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
361 local cur
="${COMP_WORDS[COMP_CWORD]}"
364 COMPREPLY
=($
(compgen
-W "
365 oneline short medium full fuller email raw
366 " -- "${cur##--pretty=}"))
370 COMPREPLY
=($
(compgen
-W "
371 --max-count= --max-age= --since= --after=
372 --min-age= --before= --until=
373 --root --not --topo-order --date-order
375 --abbrev-commit --abbrev=
377 --author= --committer= --grep=
379 --pretty= --name-status --name-only
384 __git_complete_revlist
389 local cur
="${COMP_WORDS[COMP_CWORD]}"
392 COMPREPLY
=($
(compgen
-W "
393 --no-commit --no-summary --squash --strategy
397 case "${COMP_WORDS[COMP_CWORD-1]}" in
399 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
402 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
407 local cur
="${COMP_WORDS[COMP_CWORD]}"
408 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
413 local cur
="${COMP_WORDS[COMP_CWORD]}"
414 COMPREPLY
=($
(compgen
-W "--tags --all --stdin" -- "$cur"))
419 local cur
="${COMP_WORDS[COMP_CWORD]}"
421 case "${COMP_WORDS[0]},$COMP_CWORD" in
423 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
426 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
430 case "${COMP_WORDS[0]}" in
431 git-pull
) remote
="${COMP_WORDS[1]}" ;;
432 git
) remote
="${COMP_WORDS[2]}" ;;
434 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
441 local cur
="${COMP_WORDS[COMP_CWORD]}"
443 case "${COMP_WORDS[0]},$COMP_CWORD" in
445 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
448 COMPREPLY
=($
(compgen
-W "$(__git_remotes)" -- "$cur"))
454 case "${COMP_WORDS[0]}" in
455 git-push
) remote
="${COMP_WORDS[1]}" ;;
456 git
) remote
="${COMP_WORDS[2]}" ;;
459 COMPREPLY
=($
(compgen
-W "$(__git_refs "$remote")" -- "$cur"))
462 COMPREPLY
=($
(compgen
-W "$(__git_refs2)" -- "$cur"))
471 local cur
="${COMP_WORDS[COMP_CWORD]}"
472 if [ -d .dotest
]; then
473 COMPREPLY
=($
(compgen
-W "
474 --continue --skip --abort
480 COMPREPLY
=($
(compgen
-W "
481 --onto --merge --strategy
485 case "${COMP_WORDS[COMP_CWORD-1]}" in
487 COMPREPLY
=($
(compgen
-W "$(__git_merge_strategies)" -- "$cur"))
490 COMPREPLY
=($
(compgen
-W "$(__git_refs)" -- "$cur"))
495 local cur
="${COMP_WORDS[COMP_CWORD]}"
496 local opt
="--mixed --hard --soft"
497 COMPREPLY
=($
(compgen
-W "$opt $(__git_refs)" -- "$cur"))
502 local i c
=1 command __git_dir
504 while [ $c -lt $COMP_CWORD ]; do
507 --git-dir=*) __git_dir
="${i#--git-dir=}" ;;
508 --bare) __git_dir
="." ;;
509 --version|
--help|
-p|
--paginate) ;;
510 *) command="$i"; break ;;
515 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
516 COMPREPLY
=($
(compgen
-W "
517 --git-dir= --version --exec-path
520 " -- "${COMP_WORDS[COMP_CWORD]}"))
524 local expansion
=$
(__git_aliased_command
"$command")
525 [ "$expansion" ] && command="$expansion"
528 branch
) _git_branch
;;
529 cat-file
) _git_cat_file
;;
530 checkout
) _git_checkout
;;
531 cherry-pick
) _git_cherry_pick
;;
533 diff-tree
) _git_diff_tree
;;
535 format-patch
) _git_format_patch
;;
537 ls-remote
) _git_ls_remote
;;
538 ls-tree
) _git_ls_tree
;;
540 merge-base
) _git_merge_base
;;
541 name-rev
) _git_name_rev
;;
544 rebase
) _git_rebase
;;
547 show-branch
) _git_log
;;
548 whatchanged
) _git_log
;;
555 local cur
="${COMP_WORDS[COMP_CWORD]}"
556 COMPREPLY
=($
(compgen
-W "--all $(__git_refs)" -- "$cur"))
559 complete
-o default
-o nospace
-F _git git
560 complete
-o default
-F _gitk gitk
561 complete
-o default
-F _git_branch git-branch
562 complete
-o default
-o nospace
-F _git_cat_file git-cat-file
563 complete
-o default
-F _git_checkout git-checkout
564 complete
-o default
-F _git_cherry_pick git-cherry-pick
565 complete
-o default
-o nospace
-F _git_diff git-diff
566 complete
-o default
-F _git_diff_tree git-diff-tree
567 complete
-o default
-o nospace
-F _git_fetch git-fetch
568 complete
-o default
-o nospace
-F _git_format_patch git-format-patch
569 complete
-o default
-o nospace
-F _git_log git-log
570 complete
-o default
-F _git_ls_remote git-ls-remote
571 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree
572 complete
-o default
-F _git_merge git-merge
573 complete
-o default
-F _git_merge_base git-merge-base
574 complete
-o default
-F _git_name_rev git-name-rev
575 complete
-o default
-o nospace
-F _git_pull git-pull
576 complete
-o default
-o nospace
-F _git_push git-push
577 complete
-o default
-F _git_rebase git-rebase
578 complete
-o default
-F _git_reset git-reset
579 complete
-o default
-F _git_log git-show
580 complete
-o default
-o nospace
-F _git_log git-show-branch
581 complete
-o default
-o nospace
-F _git_log git-whatchanged
583 # The following are necessary only for Cygwin, and only are needed
584 # when the user has tab-completed the executable name and consequently
585 # included the '.exe' suffix.
587 if [ Cygwin
= "$(uname -o 2>/dev/null)" ]; then
588 complete
-o default
-o nospace
-F _git git.exe
589 complete
-o default
-F _git_branch git-branch.exe
590 complete
-o default
-o nospace
-F _git_cat_file git-cat-file.exe
591 complete
-o default
-o nospace
-F _git_diff git-diff.exe
592 complete
-o default
-o nospace
-F _git_diff_tree git-diff-tree.exe
593 complete
-o default
-o nospace
-F _git_format_patch git-format-patch.exe
594 complete
-o default
-o nospace
-F _git_log git-log.exe
595 complete
-o default
-o nospace
-F _git_ls_tree git-ls-tree.exe
596 complete
-o default
-F _git_merge_base git-merge-base.exe
597 complete
-o default
-F _git_name_rev git-name-rev.exe
598 complete
-o default
-o nospace
-F _git_push git-push.exe
599 complete
-o default
-o nospace
-F _git_log git-show.exe
600 complete
-o default
-o nospace
-F _git_log git-show-branch.exe
601 complete
-o default
-o nospace
-F _git_log git-whatchanged.exe