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_complete_file ()
86 local cur="${COMP_WORDS[COMP_CWORD]}"
89 local pfx ls ref="$(echo "$cur" | sed 's,:.*$,,')"
90 cur="$(echo "$cur" | sed 's,^.*:,,')"
93 pfx="$(echo "$cur" | sed 's,/[^/]*$,,')"
94 cur="$(echo "$cur" | sed 's,^.*/,,')"
102 COMPREPLY=($(compgen -P "$pfx" \
103 -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
104 | sed '/^100... blob /s,^.* ,,
113 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
121 for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
131 __git_aliased_command ()
133 local word cmdline=$(git --git-dir="$(__gitdir)" \
134 repo-config --get "alias.$1")
135 for word in $cmdline; do
136 if [ "${word##-*}" ]; then
145 local cur="${COMP_WORDS[COMP_CWORD]}"
146 COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
151 local cur="${COMP_WORDS[COMP_CWORD]}"
152 case "${COMP_WORDS[0]},$COMP_CWORD" in
154 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
157 COMPREPLY=($(compgen -W "-p -t blob tree commit tag" -- "$cur"))
167 local cur="${COMP_WORDS[COMP_CWORD]}"
168 COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
178 local cur="${COMP_WORDS[COMP_CWORD]}"
179 COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
184 local cur="${COMP_WORDS[COMP_CWORD]}"
186 case "${COMP_WORDS[0]},$COMP_CWORD" in
188 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
191 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
196 cur=$(echo "$cur" | sed 's/^.*://')
197 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
201 case "${COMP_WORDS[0]}" in
202 git-fetch) remote="${COMP_WORDS[1]}" ;;
203 git) remote="${COMP_WORDS[2]}" ;;
205 COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
214 local cur="${COMP_WORDS[COMP_CWORD]}"
215 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
225 local cur="${COMP_WORDS[COMP_CWORD]}"
228 local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
229 cur=$(echo "$cur" | sed 's/^.*\.\.//')
230 COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
233 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
240 local cur="${COMP_WORDS[COMP_CWORD]}"
241 COMPREPLY=($(compgen -W "$(__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"))
257 case "${COMP_WORDS[0]}" in
258 git-pull) remote="${COMP_WORDS[1]}" ;;
259 git) remote="${COMP_WORDS[2]}" ;;
261 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
268 local cur="${COMP_WORDS[COMP_CWORD]}"
270 case "${COMP_WORDS[0]},$COMP_CWORD" in
272 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
275 COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
281 case "${COMP_WORDS[0]}" in
282 git-push) remote="${COMP_WORDS[1]}" ;;
283 git) remote="${COMP_WORDS[2]}" ;;
285 cur=$(echo "$cur" | sed 's/^.*://')
286 COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
289 COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
298 local cur="${COMP_WORDS[COMP_CWORD]}"
299 local opt="--mixed --hard --soft"
300 COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
305 local cur="${COMP_WORDS[COMP_CWORD]}"
306 COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
311 local i c=1 command __git_dir
313 while [ $c -lt $COMP_CWORD ]; do
316 --git-dir=*) __git_dir="${i#--git-dir=}" ;;
317 --bare) __git_dir="." ;;
318 --version|--help|-p|--paginate) ;;
319 *) command="$i"; break ;;
324 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
325 COMPREPLY=($(compgen \
326 -W "--git-dir= --version \
327 $(git help -a|egrep '^ ') \
329 -- "${COMP_WORDS[COMP_CWORD]}"))
333 local expansion=$(__git_aliased_command "$command")
334 [ "$expansion" ] && command="$expansion"
337 branch) _git_branch ;;
338 cat-file) _git_cat_file ;;
339 checkout) _git_checkout ;;
341 diff-tree) _git_diff_tree ;;
344 ls-remote) _git_ls_remote ;;
345 ls-tree) _git_ls_tree ;;
346 merge-base) _git_merge_base ;;
351 show-branch) _git_log ;;
352 whatchanged) _git_log ;;
359 local cur="${COMP_WORDS[COMP_CWORD]}"
360 COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
363 complete -o default -o nospace -F _git git
364 complete -o default -F _gitk gitk
365 complete -o default -F _git_branch git-branch
366 complete -o default -o nospace -F _git_cat_file git-cat-file
367 complete -o default -F _git_checkout git-checkout
368 complete -o default -o nospace -F _git_diff git-diff
369 complete -o default -F _git_diff_tree git-diff-tree
370 complete -o default -o nospace -F _git_fetch git-fetch
371 complete -o default -o nospace -F _git_log git-log
372 complete -o default -F _git_ls_remote git-ls-remote
373 complete -o default -o nospace -F _git_ls_tree git-ls-tree
374 complete -o default -F _git_merge_base git-merge-base
375 complete -o default -o nospace -F _git_pull git-pull
376 complete -o default -o nospace -F _git_push git-push
377 complete -o default -F _git_reset git-reset
378 complete -o default -F _git_show git-show
379 complete -o default -o nospace -F _git_log git-show-branch
380 complete -o default -o nospace -F _git_log git-whatchanged
382 # The following are necessary only for Cygwin, and only are needed
383 # when the user has tab-completed the executable name and consequently
384 # included the '.exe' suffix.
386 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
387 complete -o default -o nospace -F _git git.exe
388 complete -o default -F _git_branch git-branch.exe
389 complete -o default -o nospace -F _git_cat_file git-cat-file.exe
390 complete -o default -o nospace -F _git_diff git-diff.exe
391 complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
392 complete -o default -o nospace -F _git_log git-log.exe
393 complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
394 complete -o default -F _git_merge_base git-merge-base.exe
395 complete -o default -o nospace -F _git_push git-push.exe
396 complete -o default -o nospace -F _git_log git-show-branch.exe
397 complete -o default -o nospace -F _git_log git-whatchanged.exe