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"))
150 for i in $(git help -a|egrep '^ ')
153 check-ref-format) : plumbing;;
154 commit-tree) : plumbing;;
155 convert-objects) : plumbing;;
156 cvsserver) : daemon;;
158 fetch-pack) : plumbing;;
159 hash-object) : plumbing;;
160 http-*) : transport;;
161 index-pack) : plumbing;;
162 local-fetch) : plumbing;;
163 mailinfo) : plumbing;;
164 mailsplit) : plumbing;;
165 merge-*) : plumbing;;
168 pack-objects) : plumbing;;
169 pack-redundant) : plumbing;;
170 pack-refs) : plumbing;;
171 parse-remote) : plumbing;;
172 patch-id) : plumbing;;
173 peek-remote) : plumbing;;
174 read-tree) : plumbing;;
175 receive-pack) : plumbing;;
177 rev-list) : plumbing;;
178 rev-parse) : plumbing;;
179 runstatus) : plumbing;;
180 sh-setup) : internal;;
182 send-pack) : plumbing;;
183 show-index) : plumbing;;
185 stripspace) : plumbing;;
186 symbolic-ref) : plumbing;;
187 unpack-file) : plumbing;;
188 unpack-objects) : plumbing;;
189 update-ref) : plumbing;;
190 update-server-info) : daemon;;
191 upload-archive) : plumbing;;
192 upload-pack) : plumbing;;
193 write-tree) : plumbing;;
202 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
212 __git_aliased_command ()
214 local word cmdline=$(git --git-dir="$(__gitdir)" \
215 repo-config --get "alias.$1")
216 for word in $cmdline; do
217 if [ "${word##-*}" ]; then
226 local cur="${COMP_WORDS[COMP_CWORD]}"
227 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
232 local cur="${COMP_WORDS[COMP_CWORD]}"
233 case "${COMP_WORDS[0]},$COMP_CWORD" in
235 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
238 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
248 local cur="${COMP_WORDS[COMP_CWORD]}"
249 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
259 local cur="${COMP_WORDS[COMP_CWORD]}"
260 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
265 local cur="${COMP_WORDS[COMP_CWORD]}"
267 case "${COMP_WORDS[0]},$COMP_CWORD" in
269 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
272 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
278 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
282 case "${COMP_WORDS[0]}" in
283 git-fetch) remote="${COMP_WORDS[1]}" ;;
284 git) remote="${COMP_WORDS[2]}" ;;
286 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
295 local cur="${COMP_WORDS[COMP_CWORD]}"
296 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
306 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
311 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
316 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
319 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
326 local cur="${COMP_WORDS[COMP_CWORD]}"
329 COMPREPLY=($(compgen -W "
330 --no-commit --no-summary --squash
334 if [ $COMP_CWORD -gt 1 -a X-s = "X${COMP_WORDS[COMP_CWORD-1]}" ]
336 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
338 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
344 local cur="${COMP_WORDS[COMP_CWORD]}"
345 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
350 local cur="${COMP_WORDS[COMP_CWORD]}"
351 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
356 local cur="${COMP_WORDS[COMP_CWORD]}"
358 case "${COMP_WORDS[0]},$COMP_CWORD" in
360 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
363 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
367 case "${COMP_WORDS[0]}" in
368 git-pull) remote="${COMP_WORDS[1]}" ;;
369 git) remote="${COMP_WORDS[2]}" ;;
371 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
378 local cur="${COMP_WORDS[COMP_CWORD]}"
380 case "${COMP_WORDS[0]},$COMP_CWORD" in
382 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
385 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
391 case "${COMP_WORDS[0]}" in
392 git-push) remote="${COMP_WORDS[1]}" ;;
393 git) remote="${COMP_WORDS[2]}" ;;
396 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
399 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
408 local cur="${COMP_WORDS[COMP_CWORD]}"
409 local opt="--mixed --hard --soft"
410 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
415 local cur="${COMP_WORDS[COMP_CWORD]}"
416 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
421 local i c=1 command __git_dir
423 while [ $c -lt $COMP_CWORD ]; do
426 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
427 --bare) __git_dir="." ;;
428 --version|--help|-p|--paginate) ;;
429 *) command="$i"; break ;;
434 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
435 COMPREPLY=($(compgen -W "
436 --git-dir= --version --exec-path
439 " -- "${COMP_WORDS[COMP_CWORD]}"))
443 local expansion=$(__git_aliased_command "$command")
444 [ "$expansion" ] && command="$expansion"
447 branch) _git_branch ;;
448 cat-file) _git_cat_file ;;
449 checkout) _git_checkout ;;
451 diff-tree) _git_diff_tree ;;
454 ls-remote) _git_ls_remote ;;
455 ls-tree) _git_ls_tree ;;
457 merge-base) _git_merge_base ;;
458 name-rev) _git_name_rev ;;
463 show-branch) _git_log ;;
464 whatchanged) _git_log ;;
471 local cur="${COMP_WORDS[COMP_CWORD]}"
472 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
475 complete -o default -o nospace -F _git git
476 complete -o default -F _gitk gitk
477 complete -o default -F _git_branch git-branch
478 complete -o default -o nospace -F _git_cat_file git-cat-file
479 complete -o default -F _git_checkout git-checkout
480 complete -o default -o nospace -F _git_diff git-diff
481 complete -o default -F _git_diff_tree git-diff-tree
482 complete -o default -o nospace -F _git_fetch git-fetch
483 complete -o default -o nospace -F _git_log git-log
484 complete -o default -F _git_ls_remote git-ls-remote
485 complete -o default -o nospace -F _git_ls_tree git-ls-tree
486 complete -o default -F _git_merge git-merge
487 complete -o default -F _git_merge_base git-merge-base
488 complete -o default -F _git_name_rev git-name-rev
489 complete -o default -o nospace -F _git_pull git-pull
490 complete -o default -o nospace -F _git_push git-push
491 complete -o default -F _git_reset git-reset
492 complete -o default -F _git_show git-show
493 complete -o default -o nospace -F _git_log git-show-branch
494 complete -o default -o nospace -F _git_log git-whatchanged
496 # The following are necessary only for Cygwin, and only are needed
497 # when the user has tab-completed the executable name and consequently
498 # included the '.exe' suffix.
500 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
501 complete -o default -o nospace -F _git git.exe
502 complete -o default -F _git_branch git-branch.exe
503 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
504 complete -o default -o nospace -F _git_diff git-diff.exe
505 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
506 complete -o default -o nospace -F _git_log git-log.exe
507 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
508 complete -o default -F _git_merge_base git-merge-base.exe
509 complete -o default -F _git_name_rev git-name-rev.exe
510 complete -o default -o nospace -F _git_push git-push.exe
511 complete -o default -o nospace -F _git_log git-show-branch.exe
512 complete -o default -o nospace -F _git_log git-whatchanged.exe