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 git repo-config --list | grep '^alias\.' \
107 | sed -e 's/^alias\.//' -e 's/=.*$//'
110 __git_aliased_command ()
112 local cmdline=$(git repo-config alias.$1)
113 for word in $cmdline; do
114 if [ "${word##-*}" ]; then
123 local cur="${COMP_WORDS[COMP_CWORD]}"
124 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur"))
129 local cur="${COMP_WORDS[COMP_CWORD]}"
130 case "${COMP_WORDS[0]},$COMP_CWORD" in
132 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
135 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
145 local cur="${COMP_WORDS[COMP_CWORD]}"
146 COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur"))
156 local cur="${COMP_WORDS[COMP_CWORD]}"
157 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur"))
162 local cur="${COMP_WORDS[COMP_CWORD]}"
164 case "${COMP_WORDS[0]},$COMP_CWORD" in
166 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
169 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
174 cur=$(echo "$cur" | sed 's/^.*://')
175 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
179 case "${COMP_WORDS[0]}" in
180 git-fetch) remote="${COMP_WORDS[1]}" ;;
181 git) remote="${COMP_WORDS[2]}" ;;
183 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
192 local cur="${COMP_WORDS[COMP_CWORD]}"
193 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
203 local cur="${COMP_WORDS[COMP_CWORD]}"
206 local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
207 cur=$(echo "$cur" | sed 's/^.*\.\.//')
208 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur"))
211 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
218 local cur="${COMP_WORDS[COMP_CWORD]}"
219 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
224 local cur="${COMP_WORDS[COMP_CWORD]}"
226 case "${COMP_WORDS[0]},$COMP_CWORD" in
228 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
231 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
235 case "${COMP_WORDS[0]}" in
236 git-pull) remote="${COMP_WORDS[1]}" ;;
237 git) remote="${COMP_WORDS[2]}" ;;
239 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$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 case "${COMP_WORDS[0]}" in
260 git-push) remote="${COMP_WORDS[1]}" ;;
261 git) remote="${COMP_WORDS[2]}" ;;
263 cur=$(echo "$cur" | sed 's/^.*://')
264 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
267 COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur"))
276 local cur="${COMP_WORDS[COMP_CWORD]}"
277 local opt="--mixed --hard --soft"
278 COMPREPLY=($(compgen -W "$opt $(__git_refs .)" -- "$cur"))
283 local cur="${COMP_WORDS[COMP_CWORD]}"
284 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
289 if [ $COMP_CWORD = 1 ]; then
290 COMPREPLY=($(compgen \
291 -W "--version $(git help -a|egrep '^ ') \
293 -- "${COMP_WORDS[COMP_CWORD]}"))
295 local command="${COMP_WORDS[1]}"
296 local expansion=$(__git_aliased_command "$command")
298 if [ "$expansion" ]; then
303 branch) _git_branch ;;
304 cat-file) _git_cat_file ;;
305 checkout) _git_checkout ;;
307 diff-tree) _git_diff_tree ;;
310 ls-remote) _git_ls_remote ;;
311 ls-tree) _git_ls_tree ;;
316 show-branch) _git_log ;;
317 whatchanged) _git_log ;;
325 local cur="${COMP_WORDS[COMP_CWORD]}"
326 COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur"))
329 complete -o default -o nospace -F _git git
330 complete -o default -F _gitk gitk
331 complete -o default -F _git_branch git-branch
332 complete -o default -o nospace -F _git_cat_file git-cat-file
333 complete -o default -F _git_checkout git-checkout
334 complete -o default -o nospace -F _git_diff git-diff
335 complete -o default -F _git_diff_tree git-diff-tree
336 complete -o default -o nospace -F _git_fetch git-fetch
337 complete -o default -o nospace -F _git_log git-log
338 complete -o default -F _git_ls_remote git-ls-remote
339 complete -o default -o nospace -F _git_ls_tree git-ls-tree
340 complete -o default -F _git_merge_base git-merge-base
341 complete -o default -o nospace -F _git_pull git-pull
342 complete -o default -o nospace -F _git_push git-push
343 complete -o default -F _git_reset git-reset
344 complete -o default -F _git_show git-show
345 complete -o default -o nospace -F _git_log git-whatchanged
347 # The following are necessary only for Cygwin, and only are needed
348 # when the user has tab-completed the executable name and consequently
349 # included the '.exe' suffix.
351 complete -o default -F _git_branch git-branch.exe
352 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
353 complete -o default -o nospace -F _git_diff git-diff.exe
354 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
355 complete -o default -o nospace -F _git_log git-log.exe
356 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
357 complete -o default -F _git_merge_base git-merge-base.exe
358 complete -o default -o nospace -F _git_push git-push.exe
359 complete -o default -o nospace -F _git_log git-whatchanged.exe