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
30 for i in $($cmd "$1" 2>/dev/null); do
34 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
35 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
36 n,*) is_hash=y; echo "$i" ;;
49 for i in $($cmd "$1" 2>/dev/null); do
53 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}:${i#refs/tags/}" ;;
54 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}:${i#refs/heads/}" ;;
55 n,*) is_hash=y; echo "$i:$i" ;;
62 local i REVERTGLOB=$(shopt -p nullglob)
64 for i in .git/remotes/*; do
65 echo ${i#.git/remotes/}
70 __git_complete_file ()
72 local cur="${COMP_WORDS[COMP_CWORD]}"
75 local pfx ls ref="$(echo "$cur" | sed 's,:.*$,,')"
76 cur="$(echo "$cur" | sed 's,^.*:,,')"
79 pfx="$(echo "$cur" | sed 's,/[^/]*$,,')"
80 cur="$(echo "$cur" | sed 's,^.*/,,')"
88 COMPREPLY=($(compgen -P "$pfx" \
89 -W "$(git-ls-tree "$ls" \
90 | sed '/^100... blob /s,^.* ,,
99 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
106 local cur="${COMP_WORDS[COMP_CWORD]}"
107 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur"))
112 local cur="${COMP_WORDS[COMP_CWORD]}"
113 case "${COMP_WORDS[0]},$COMP_CWORD" in
115 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
118 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
128 local cur="${COMP_WORDS[COMP_CWORD]}"
129 COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur"))
139 local cur="${COMP_WORDS[COMP_CWORD]}"
140 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur"))
145 local cur="${COMP_WORDS[COMP_CWORD]}"
147 case "${COMP_WORDS[0]},$COMP_CWORD" in
149 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
152 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
157 cur=$(echo "$cur" | sed 's/^.*://')
158 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
162 case "${COMP_WORDS[0]}" in
163 git-fetch) remote="${COMP_WORDS[1]}" ;;
164 git) remote="${COMP_WORDS[2]}" ;;
166 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
175 local cur="${COMP_WORDS[COMP_CWORD]}"
176 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
186 local cur="${COMP_WORDS[COMP_CWORD]}"
189 local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
190 cur=$(echo "$cur" | sed 's/^.*\.\.//')
191 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur"))
194 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
201 local cur="${COMP_WORDS[COMP_CWORD]}"
202 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
207 local cur="${COMP_WORDS[COMP_CWORD]}"
209 case "${COMP_WORDS[0]},$COMP_CWORD" in
211 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
214 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
218 case "${COMP_WORDS[0]}" in
219 git-pull) remote="${COMP_WORDS[1]}" ;;
220 git) remote="${COMP_WORDS[2]}" ;;
222 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
229 local cur="${COMP_WORDS[COMP_CWORD]}"
231 case "${COMP_WORDS[0]},$COMP_CWORD" in
233 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
236 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
242 case "${COMP_WORDS[0]}" in
243 git-push) remote="${COMP_WORDS[1]}" ;;
244 git) remote="${COMP_WORDS[2]}" ;;
246 cur=$(echo "$cur" | sed 's/^.*://')
247 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
250 COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur"))
259 local cur="${COMP_WORDS[COMP_CWORD]}"
260 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
265 if [ $COMP_CWORD = 1 ]; then
266 COMPREPLY=($(compgen \
267 -W "--version $(git help -a|egrep '^ ')" \
268 -- "${COMP_WORDS[COMP_CWORD]}"))
270 case "${COMP_WORDS[1]}" in
271 branch) _git_branch ;;
272 cat-file) _git_cat_file ;;
273 checkout) _git_checkout ;;
275 diff-tree) _git_diff_tree ;;
278 ls-remote) _git_ls_remote ;;
279 ls-tree) _git_ls_tree ;;
283 show-branch) _git_log ;;
284 whatchanged) _git_log ;;
292 local cur="${COMP_WORDS[COMP_CWORD]}"
293 COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur"))
296 complete -o default -o nospace -F _git git
297 complete -o default -F _gitk gitk
298 complete -o default -F _git_branch git-branch
299 complete -o default -o nospace -F _git_cat_file git-cat-file
300 complete -o default -F _git_checkout git-checkout
301 complete -o default -o nospace -F _git_diff git-diff
302 complete -o default -F _git_diff_tree git-diff-tree
303 complete -o default -o nospace -F _git_fetch git-fetch
304 complete -o default -o nospace -F _git_log git-log
305 complete -o default -F _git_ls_remote git-ls-remote
306 complete -o default -o nospace -F _git_ls_tree git-ls-tree
307 complete -o default -F _git_merge_base git-merge-base
308 complete -o default -o nospace -F _git_pull git-pull
309 complete -o default -o nospace -F _git_push git-push
310 complete -o default -F _git_show git-show
311 complete -o default -o nospace -F _git_log git-whatchanged
313 # The following are necessary only for Cygwin, and only are needed
314 # when the user has tab-completed the executable name and consequently
315 # included the '.exe' suffix.
317 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
318 complete -o default -o nospace -F _git_diff git-diff.exe
319 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
320 complete -o default -o nospace -F _git_log git-log.exe
321 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
322 complete -o default -F _git_merge_base git-merge-base.exe
323 complete -o default -o nospace -F _git_push git-push.exe
324 complete -o default -o nospace -F _git_log git-whatchanged.exe