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 ngoff IFS=$'\n'
63 shopt -q nullglob || ngoff=1
65 for i in .git/remotes/*; do
66 echo ${i#.git/remotes/}
68 [ "$ngoff" ] && shopt -u nullglob
69 for i in $(git repo-config --list); do
79 __git_complete_file ()
81 local cur="${COMP_WORDS[COMP_CWORD]}"
84 local pfx ls ref="$(echo "$cur" | sed 's,:.*$,,')"
85 cur="$(echo "$cur" | sed 's,^.*:,,')"
88 pfx="$(echo "$cur" | sed 's,/[^/]*$,,')"
89 cur="$(echo "$cur" | sed 's,^.*/,,')"
97 COMPREPLY=($(compgen -P "$pfx" \
98 -W "$(git-ls-tree "$ls" \
99 | sed '/^100... blob /s,^.* ,,
108 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
116 for i in $(git repo-config --list); do
126 __git_aliased_command ()
128 local word cmdline=$(git repo-config --get "alias.$1")
129 for word in $cmdline; do
130 if [ "${word##-*}" ]; then
139 local cur="${COMP_WORDS[COMP_CWORD]}"
140 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur"))
145 local cur="${COMP_WORDS[COMP_CWORD]}"
146 case "${COMP_WORDS[0]},$COMP_CWORD" in
148 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
151 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
161 local cur="${COMP_WORDS[COMP_CWORD]}"
162 COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur"))
172 local cur="${COMP_WORDS[COMP_CWORD]}"
173 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur"))
178 local cur="${COMP_WORDS[COMP_CWORD]}"
180 case "${COMP_WORDS[0]},$COMP_CWORD" in
182 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
185 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
190 cur=$(echo "$cur" | sed 's/^.*://')
191 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
195 case "${COMP_WORDS[0]}" in
196 git-fetch) remote="${COMP_WORDS[1]}" ;;
197 git) remote="${COMP_WORDS[2]}" ;;
199 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
208 local cur="${COMP_WORDS[COMP_CWORD]}"
209 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
219 local cur="${COMP_WORDS[COMP_CWORD]}"
222 local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
223 cur=$(echo "$cur" | sed 's/^.*\.\.//')
224 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur"))
227 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
234 local cur="${COMP_WORDS[COMP_CWORD]}"
235 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
240 local cur="${COMP_WORDS[COMP_CWORD]}"
242 case "${COMP_WORDS[0]},$COMP_CWORD" in
244 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
247 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
251 case "${COMP_WORDS[0]}" in
252 git-pull) remote="${COMP_WORDS[1]}" ;;
253 git) remote="${COMP_WORDS[2]}" ;;
255 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
262 local cur="${COMP_WORDS[COMP_CWORD]}"
264 case "${COMP_WORDS[0]},$COMP_CWORD" in
266 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
269 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
275 case "${COMP_WORDS[0]}" in
276 git-push) remote="${COMP_WORDS[1]}" ;;
277 git) remote="${COMP_WORDS[2]}" ;;
279 cur=$(echo "$cur" | sed 's/^.*://')
280 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
283 COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur"))
292 local cur="${COMP_WORDS[COMP_CWORD]}"
293 local opt="--mixed --hard --soft"
294 COMPREPLY=($(compgen -W "$opt $(__git_refs .)" -- "$cur"))
299 local cur="${COMP_WORDS[COMP_CWORD]}"
300 COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
305 if [ $COMP_CWORD = 1 ]; then
306 COMPREPLY=($(compgen \
307 -W "--version $(git help -a|egrep '^ ') \
309 -- "${COMP_WORDS[COMP_CWORD]}"))
311 local command="${COMP_WORDS[1]}"
312 local expansion=$(__git_aliased_command "$command")
314 if [ "$expansion" ]; then
319 branch) _git_branch ;;
320 cat-file) _git_cat_file ;;
321 checkout) _git_checkout ;;
323 diff-tree) _git_diff_tree ;;
326 ls-remote) _git_ls_remote ;;
327 ls-tree) _git_ls_tree ;;
328 merge-base) _git_merge_base ;;
333 show-branch) _git_log ;;
334 whatchanged) _git_log ;;
342 local cur="${COMP_WORDS[COMP_CWORD]}"
343 COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur"))
346 complete -o default -o nospace -F _git git
347 complete -o default -F _gitk gitk
348 complete -o default -F _git_branch git-branch
349 complete -o default -o nospace -F _git_cat_file git-cat-file
350 complete -o default -F _git_checkout git-checkout
351 complete -o default -o nospace -F _git_diff git-diff
352 complete -o default -F _git_diff_tree git-diff-tree
353 complete -o default -o nospace -F _git_fetch git-fetch
354 complete -o default -o nospace -F _git_log git-log
355 complete -o default -F _git_ls_remote git-ls-remote
356 complete -o default -o nospace -F _git_ls_tree git-ls-tree
357 complete -o default -F _git_merge_base git-merge-base
358 complete -o default -o nospace -F _git_pull git-pull
359 complete -o default -o nospace -F _git_push git-push
360 complete -o default -F _git_reset git-reset
361 complete -o default -F _git_show git-show
362 complete -o default -o nospace -F _git_log git-show-branch
363 complete -o default -o nospace -F _git_log git-whatchanged
365 # The following are necessary only for Cygwin, and only are needed
366 # when the user has tab-completed the executable name and consequently
367 # included the '.exe' suffix.
369 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
370 complete -o default -o nospace -F _git git.exe
371 complete -o default -F _git_branch git-branch.exe
372 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
373 complete -o default -o nospace -F _git_diff git-diff.exe
374 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
375 complete -o default -o nospace -F _git_log git-log.exe
376 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
377 complete -o default -F _git_merge_base git-merge-base.exe
378 complete -o default -o nospace -F _git_push git-push.exe
379 complete -o default -o nospace -F _git_log git-show-branch.exe
380 complete -o default -o nospace -F _git_log git-whatchanged.exe