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
24 echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
29 local cmd i is_hash=y dir="${1:-$(__gitdir)}"
30 if [ -d "$dir" ]; then
35 for i in $($cmd "$dir" 2>/dev/null); do
39 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
40 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
41 n,*) is_hash=y; echo "$i" ;;
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/}:${i#refs/tags/}" ;;
59 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
60 n,*) is_hash=y; echo "$i:$i" ;;
67 local i ngoff IFS=$'\n' d="$(__gitdir)"
68 shopt -q nullglob || ngoff=1
70 for i in "$d/remotes"/*; do
73 [ "$ngoff" ] && shopt -u nullglob
74 for i in $(git --git-dir="$d" repo-config --list); do
84 __git_merge_strategies ()
86 sed -n "/^all_strategies='/{
91 }" "$(git --exec-path)/git-merge"
94 __git_complete_file ()
96 local pfx ls ref cur="${COMP_WORDS[COMP_CWORD]}"
112 COMPREPLY=($(compgen -P "$pfx" \
113 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
114 | sed '/^100... blob /s,^.* ,,
123 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
131 for i in $(git help -a|egrep '^ ')
134 check-ref-format) : plumbing;;
135 commit-tree) : plumbing;;
136 convert-objects) : plumbing;;
137 cvsserver) : daemon;;
139 fetch-pack) : plumbing;;
140 hash-object) : plumbing;;
141 http-*) : transport;;
142 index-pack) : plumbing;;
143 local-fetch) : plumbing;;
144 mailinfo) : plumbing;;
145 mailsplit) : plumbing;;
146 merge-*) : plumbing;;
149 pack-objects) : plumbing;;
150 pack-redundant) : plumbing;;
151 pack-refs) : plumbing;;
152 parse-remote) : plumbing;;
153 patch-id) : plumbing;;
154 peek-remote) : plumbing;;
155 read-tree) : plumbing;;
156 receive-pack) : plumbing;;
158 rev-list) : plumbing;;
159 rev-parse) : plumbing;;
160 runstatus) : plumbing;;
161 sh-setup) : internal;;
163 send-pack) : plumbing;;
164 show-index) : plumbing;;
166 stripspace) : plumbing;;
167 symbolic-ref) : plumbing;;
168 unpack-file) : plumbing;;
169 unpack-objects) : plumbing;;
170 update-ref) : plumbing;;
171 update-server-info) : daemon;;
172 upload-archive) : plumbing;;
173 upload-pack) : plumbing;;
174 write-tree) : plumbing;;
183 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
193 __git_aliased_command ()
195 local word cmdline=$(git --git-dir="$(__gitdir)" \
196 repo-config --get "alias.$1")
197 for word in $cmdline; do
198 if [ "${word##-*}" ]; then
207 local cur="${COMP_WORDS[COMP_CWORD]}"
208 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
213 local cur="${COMP_WORDS[COMP_CWORD]}"
214 case "${COMP_WORDS[0]},$COMP_CWORD" in
216 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
219 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
229 local cur="${COMP_WORDS[COMP_CWORD]}"
230 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
240 local cur="${COMP_WORDS[COMP_CWORD]}"
241 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
246 local cur="${COMP_WORDS[COMP_CWORD]}"
248 case "${COMP_WORDS[0]},$COMP_CWORD" in
250 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
253 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
259 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
263 case "${COMP_WORDS[0]}" in
264 git-fetch) remote="${COMP_WORDS[1]}" ;;
265 git) remote="${COMP_WORDS[2]}" ;;
267 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
276 local cur="${COMP_WORDS[COMP_CWORD]}"
277 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
287 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
292 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
297 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
300 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
307 local cur="${COMP_WORDS[COMP_CWORD]}"
310 COMPREPLY=($(compgen -W "
311 --no-commit --no-summary --squash
315 if [ $COMP_CWORD -gt 1 -a X-s = "X${COMP_WORDS[COMP_CWORD-1]}" ]
317 COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
319 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
325 local cur="${COMP_WORDS[COMP_CWORD]}"
326 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
331 local cur="${COMP_WORDS[COMP_CWORD]}"
332 COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
337 local cur="${COMP_WORDS[COMP_CWORD]}"
339 case "${COMP_WORDS[0]},$COMP_CWORD" in
341 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
344 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
348 case "${COMP_WORDS[0]}" in
349 git-pull) remote="${COMP_WORDS[1]}" ;;
350 git) remote="${COMP_WORDS[2]}" ;;
352 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
359 local cur="${COMP_WORDS[COMP_CWORD]}"
361 case "${COMP_WORDS[0]},$COMP_CWORD" in
363 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
366 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
372 case "${COMP_WORDS[0]}" in
373 git-push) remote="${COMP_WORDS[1]}" ;;
374 git) remote="${COMP_WORDS[2]}" ;;
377 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
380 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
389 local cur="${COMP_WORDS[COMP_CWORD]}"
390 local opt="--mixed --hard --soft"
391 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
396 local cur="${COMP_WORDS[COMP_CWORD]}"
397 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
402 local i c=1 command __git_dir
404 while [ $c -lt $COMP_CWORD ]; do
407 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
408 --bare) __git_dir="." ;;
409 --version|--help|-p|--paginate) ;;
410 *) command="$i"; break ;;
415 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
416 COMPREPLY=($(compgen -W "
417 --git-dir= --version --exec-path
420 " -- "${COMP_WORDS[COMP_CWORD]}"))
424 local expansion=$(__git_aliased_command "$command")
425 [ "$expansion" ] && command="$expansion"
428 branch) _git_branch ;;
429 cat-file) _git_cat_file ;;
430 checkout) _git_checkout ;;
432 diff-tree) _git_diff_tree ;;
435 ls-remote) _git_ls_remote ;;
436 ls-tree) _git_ls_tree ;;
438 merge-base) _git_merge_base ;;
439 name-rev) _git_name_rev ;;
444 show-branch) _git_log ;;
445 whatchanged) _git_log ;;
452 local cur="${COMP_WORDS[COMP_CWORD]}"
453 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
456 complete -o default -o nospace -F _git git
457 complete -o default -F _gitk gitk
458 complete -o default -F _git_branch git-branch
459 complete -o default -o nospace -F _git_cat_file git-cat-file
460 complete -o default -F _git_checkout git-checkout
461 complete -o default -o nospace -F _git_diff git-diff
462 complete -o default -F _git_diff_tree git-diff-tree
463 complete -o default -o nospace -F _git_fetch git-fetch
464 complete -o default -o nospace -F _git_log git-log
465 complete -o default -F _git_ls_remote git-ls-remote
466 complete -o default -o nospace -F _git_ls_tree git-ls-tree
467 complete -o default -F _git_merge git-merge
468 complete -o default -F _git_merge_base git-merge-base
469 complete -o default -F _git_name_rev git-name-rev
470 complete -o default -o nospace -F _git_pull git-pull
471 complete -o default -o nospace -F _git_push git-push
472 complete -o default -F _git_reset git-reset
473 complete -o default -F _git_show git-show
474 complete -o default -o nospace -F _git_log git-show-branch
475 complete -o default -o nospace -F _git_log git-whatchanged
477 # The following are necessary only for Cygwin, and only are needed
478 # when the user has tab-completed the executable name and consequently
479 # included the '.exe' suffix.
481 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
482 complete -o default -o nospace -F _git git.exe
483 complete -o default -F _git_branch git-branch.exe
484 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
485 complete -o default -o nospace -F _git_diff git-diff.exe
486 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
487 complete -o default -o nospace -F _git_log git-log.exe
488 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
489 complete -o default -F _git_merge_base git-merge-base.exe
490 complete -o default -F _git_name_rev git-name-rev.exe
491 complete -o default -o nospace -F _git_push git-push.exe
492 complete -o default -o nospace -F _git_log git-show-branch.exe
493 complete -o default -o nospace -F _git_log git-whatchanged.exe