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 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
51 for i in $(git --git-dir="$dir" \
52 for-each-ref --format='%(refname)' \
53 refs/tags refs/heads refs/remotes); do
55 refs/tags/*) echo "${i#refs/tags/}" ;;
56 refs/heads/*) echo "${i#refs/heads/}" ;;
57 refs/remotes/*) echo "${i#refs/remotes/}" ;;
63 for i in $(git-ls-remote "$dir" 2>/dev/null); do
67 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
68 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
69 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
70 n,*) is_hash=y; echo "$i" ;;
77 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
78 if [ -d "$dir" ]; then
83 for i in $($cmd "$dir" 2>/dev/null); do
87 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
88 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
89 n,*) is_hash=y; echo "$i:$i" ;;
96 local i ngoff IFS=$'\n' d="$(__gitdir)"
97 shopt -q nullglob || ngoff=1
99 for i in "$d/remotes"/*; do
100 echo ${i#$d/remotes/}
102 [ "$ngoff" ] && shopt -u nullglob
103 for i in $(git --git-dir="$d" repo-config --list); do
113 __git_merge_strategies ()
115 sed -n "/^all_strategies='/{
116 s/^all_strategies='//
120 }" "$(git --exec-path)/git-merge"
123 __git_complete_file ()
125 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
141 COMPREPLY=($(compgen -P "$pfx" \
142 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
143 | sed '/^100... blob /s,^.* ,,
152 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
157 __git_complete_revlist ()
159 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
164 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
169 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
172 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
180 for i in $(git help -a|egrep '^ ')
183 check-ref-format) : plumbing;;
184 commit-tree) : plumbing;;
185 convert-objects) : plumbing;;
186 cvsserver) : daemon;;
188 fetch-pack) : plumbing;;
189 hash-object) : plumbing;;
190 http-*) : transport;;
191 index-pack) : plumbing;;
192 local-fetch) : plumbing;;
193 mailinfo) : plumbing;;
194 mailsplit) : plumbing;;
195 merge-*) : plumbing;;
198 pack-objects) : plumbing;;
199 pack-redundant) : plumbing;;
200 pack-refs) : plumbing;;
201 parse-remote) : plumbing;;
202 patch-id) : plumbing;;
203 peek-remote) : plumbing;;
204 read-tree) : plumbing;;
205 receive-pack) : plumbing;;
207 rev-list) : plumbing;;
208 rev-parse) : plumbing;;
209 runstatus) : plumbing;;
210 sh-setup) : internal;;
212 send-pack) : plumbing;;
213 show-index) : plumbing;;
215 stripspace) : plumbing;;
216 symbolic-ref) : plumbing;;
217 unpack-file) : plumbing;;
218 unpack-objects) : plumbing;;
219 update-ref) : plumbing;;
220 update-server-info) : daemon;;
221 upload-archive) : plumbing;;
222 upload-pack) : plumbing;;
223 write-tree) : plumbing;;
232 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
242 __git_aliased_command ()
244 local word cmdline=$(git --git-dir="$(__gitdir)" \
245 repo-config --get "alias.$1")
246 for word in $cmdline; do
247 if [ "${word##-*}" ]; then
256 local cur="${COMP_WORDS[COMP_CWORD]}"
257 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
262 local cur="${COMP_WORDS[COMP_CWORD]}"
263 case "${COMP_WORDS[0]},$COMP_CWORD" in
265 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
268 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
278 local cur="${COMP_WORDS[COMP_CWORD]}"
279 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
284 local cur="${COMP_WORDS[COMP_CWORD]}"
287 COMPREPLY=($(compgen -W "
292 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
304 local cur="${COMP_WORDS[COMP_CWORD]}"
305 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
310 local cur="${COMP_WORDS[COMP_CWORD]}"
312 case "${COMP_WORDS[0]},$COMP_CWORD" in
314 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
317 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
323 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
327 case "${COMP_WORDS[0]}" in
328 git-fetch) remote="${COMP_WORDS[1]}" ;;
329 git) remote="${COMP_WORDS[2]}" ;;
331 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
340 local cur="${COMP_WORDS[COMP_CWORD]}"
343 COMPREPLY=($(compgen -W "
344 --stdout --attach --thread
346 --numbered --start-number
350 --full-index --binary
355 __git_complete_revlist
360 local cur="${COMP_WORDS[COMP_CWORD]}"
361 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
371 local cur="${COMP_WORDS[COMP_CWORD]}"
374 COMPREPLY=($(compgen -W "
375 oneline short medium full fuller email raw
376 " -- "${cur##--pretty=}"))
380 COMPREPLY=($(compgen -W "
381 --max-count= --max-age= --since= --after=
382 --min-age= --before= --until=
383 --root --not --topo-order --date-order
385 --abbrev-commit --abbrev=
387 --author= --committer= --grep=
389 --pretty= --name-status --name-only
394 __git_complete_revlist
399 local cur="${COMP_WORDS[COMP_CWORD]}"
402 COMPREPLY=($(compgen -W "
403 --no-commit --no-summary --squash --strategy
407 case "${COMP_WORDS[COMP_CWORD-1]}" in
409 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
412 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
417 local cur="${COMP_WORDS[COMP_CWORD]}"
418 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
423 local cur="${COMP_WORDS[COMP_CWORD]}"
424 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
429 local cur="${COMP_WORDS[COMP_CWORD]}"
431 case "${COMP_WORDS[0]},$COMP_CWORD" in
433 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
436 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
440 case "${COMP_WORDS[0]}" in
441 git-pull) remote="${COMP_WORDS[1]}" ;;
442 git) remote="${COMP_WORDS[2]}" ;;
444 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
451 local cur="${COMP_WORDS[COMP_CWORD]}"
453 case "${COMP_WORDS[0]},$COMP_CWORD" in
455 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
458 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
464 case "${COMP_WORDS[0]}" in
465 git-push) remote="${COMP_WORDS[1]}" ;;
466 git) remote="${COMP_WORDS[2]}" ;;
469 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
472 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
481 local cur="${COMP_WORDS[COMP_CWORD]}"
482 if [ -d .dotest ]; then
483 COMPREPLY=($(compgen -W "
484 --continue --skip --abort
490 COMPREPLY=($(compgen -W "
491 --onto --merge --strategy
495 case "${COMP_WORDS[COMP_CWORD-1]}" in
497 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
500 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
505 local cur="${COMP_WORDS[COMP_CWORD]}"
506 local opt="--mixed --hard --soft"
507 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
512 local i c=1 command __git_dir
514 while [ $c -lt $COMP_CWORD ]; do
517 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
518 --bare) __git_dir="." ;;
519 --version|--help|-p|--paginate) ;;
520 *) command="$i"; break ;;
525 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
526 COMPREPLY=($(compgen -W "
527 --git-dir= --version --exec-path
530 " -- "${COMP_WORDS[COMP_CWORD]}"))
534 local expansion=$(__git_aliased_command "$command")
535 [ "$expansion" ] && command="$expansion"
538 branch) _git_branch ;;
539 cat-file) _git_cat_file ;;
540 checkout) _git_checkout ;;
541 cherry-pick) _git_cherry_pick ;;
543 diff-tree) _git_diff_tree ;;
545 format-patch) _git_format_patch ;;
547 ls-remote) _git_ls_remote ;;
548 ls-tree) _git_ls_tree ;;
550 merge-base) _git_merge_base ;;
551 name-rev) _git_name_rev ;;
554 rebase) _git_rebase ;;
557 show-branch) _git_log ;;
558 whatchanged) _git_log ;;
565 local cur="${COMP_WORDS[COMP_CWORD]}"
566 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
569 complete -o default -o nospace -F _git git
570 complete -o default -F _gitk gitk
571 complete -o default -F _git_branch git-branch
572 complete -o default -o nospace -F _git_cat_file git-cat-file
573 complete -o default -F _git_checkout git-checkout
574 complete -o default -F _git_cherry_pick git-cherry-pick
575 complete -o default -o nospace -F _git_diff git-diff
576 complete -o default -F _git_diff_tree git-diff-tree
577 complete -o default -o nospace -F _git_fetch git-fetch
578 complete -o default -o nospace -F _git_format_patch git-format-patch
579 complete -o default -o nospace -F _git_log git-log
580 complete -o default -F _git_ls_remote git-ls-remote
581 complete -o default -o nospace -F _git_ls_tree git-ls-tree
582 complete -o default -F _git_merge git-merge
583 complete -o default -F _git_merge_base git-merge-base
584 complete -o default -F _git_name_rev git-name-rev
585 complete -o default -o nospace -F _git_pull git-pull
586 complete -o default -o nospace -F _git_push git-push
587 complete -o default -F _git_rebase git-rebase
588 complete -o default -F _git_reset git-reset
589 complete -o default -F _git_log git-show
590 complete -o default -o nospace -F _git_log git-show-branch
591 complete -o default -o nospace -F _git_log git-whatchanged
593 # The following are necessary only for Cygwin, and only are needed
594 # when the user has tab-completed the executable name and consequently
595 # included the '.exe' suffix.
597 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
598 complete -o default -o nospace -F _git git.exe
599 complete -o default -F _git_branch git-branch.exe
600 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
601 complete -o default -o nospace -F _git_diff git-diff.exe
602 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
603 complete -o default -o nospace -F _git_format_patch git-format-patch.exe
604 complete -o default -o nospace -F _git_log git-log.exe
605 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
606 complete -o default -F _git_merge_base git-merge-base.exe
607 complete -o default -F _git_name_rev git-name-rev.exe
608 complete -o default -o nospace -F _git_push git-push.exe
609 complete -o default -o nospace -F _git_log git-show.exe
610 complete -o default -o nospace -F _git_log git-show-branch.exe
611 complete -o default -o nospace -F _git_log git-whatchanged.exe