2 # bash completion support for TopGit.
4 # Copyright (C) 2008 Jonas Fonseca <fonseca@diku.dk>
5 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
6 # Based git's git-completion.sh: http://repo.or.cz/w/git/fastimport.git
7 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
8 # Distributed under the GNU General Public License, version 2.0.
10 # The contained completion routines provide support for completing:
12 # *) local and remote branch names
13 # *) local and remote tag names
14 # *) .git/remotes file names
15 # *) git 'subcommands'
16 # *) tree paths within 'ref:path/to/file' expressions
17 # *) common --long-options
19 # To use these routines:
21 # 1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
22 # 2) Added the following line to your .bashrc:
23 # source ~/.git-completion.sh
25 # 3) You may want to make sure the git executable is available
26 # in your PATH before this script is sourced, as some caching
27 # is performed while the script loads. If git isn't found
28 # at source time then all lookups will be done on demand,
29 # which may be slightly slower.
31 # 4) Consider changing your PS1 to also show the current branch:
32 # PS1='[\u@\h \W$(__tg_ps1 " (%s)")]\$ '
34 # The argument to __tg_ps1 will be displayed only if you
35 # are currently in a git repository. The %s token will be
37 case "$COMP_WORDBREAKS" in
39 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
47 if [ -n "$__tg_dir" ]; then
49 elif [ -d .git ]; then
52 git rev-parse --git-dir 2>/dev/null
54 elif [ -d "$1/.git" ]; then
63 local c IFS=' '$'\t'$'\n'
66 --*=*) printf %s$'\n' "$c$2" ;;
67 *.) printf %s$'\n' "$c$2" ;;
68 *) printf %s$'\n' "$c$2 " ;;
75 local cur="${COMP_WORDS[COMP_CWORD]}"
85 COMPREPLY=($(compgen -P "$2" \
86 -W "$(__tgcomp_1 "$1" "$4")" \
94 local cmd i is_hash=y dir="$(__tgdir "$1")"
95 if [ -d "$dir" ]; then
96 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
100 for i in $(git ls-remote "$1" 2>/dev/null); do
101 case "$is_hash,$i" in
104 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
105 n,*) is_hash=y; echo "$i" ;;
112 local cmd i is_hash=y dir="$(__tgdir "$1")"
113 if [ -d "$dir" ]; then
114 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
115 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
116 refs/tags refs/heads refs/remotes
119 for i in $(git ls-remote "$dir" 2>/dev/null); do
120 case "$is_hash,$i" in
123 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
124 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
125 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
126 n,*) is_hash=y; echo "$i" ;;
134 for i in $(__tg_refs "$1"); do
141 local cmd i is_hash=y
142 for i in $(git ls-remote "$1" 2>/dev/null); do
143 case "$is_hash,$i" in
146 echo "$i:refs/remotes/$1/${i#refs/heads/}"
150 n,refs/tags/*) is_hash=y;;
158 local i ngoff IFS=$'\n' d="$(__tgdir)"
159 shopt -q nullglob || ngoff=1
161 for i in "$d/remotes"/*; do
162 echo ${i#$d/remotes/}
164 [ "$ngoff" ] && shopt -u nullglob
165 for i in $(git --git-dir="$d" config --list); do
175 __tg_complete_revlist ()
177 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
182 __tgcomp "$(__tg_refs)" "$pfx" "$cur"
187 __tgcomp "$(__tg_refs)" "$pfx" "$cur"
190 __tgcomp "$(__tg_refs)"
197 tg summary | cut -f 2
202 if [ -n "$__tg_all_commandlist" ]; then
203 echo "$__tg_all_commandlist"
207 for i in $(tg help | sed -n 's/^Usage:.*(\(.*\)).*/\1/p' | tr '|' ' ')
210 *--*) : helper pattern;;
215 __tg_all_commandlist=
216 __tg_all_commandlist="$(__tg_commands 2>/dev/null)"
220 if [ $COMP_CWORD -gt 2 ] && [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "$1" ]; then
232 local cur="${COMP_WORDS[COMP_CWORD]}"
234 # Name must be the first arg after the create command
235 if [ $((cmd + 1)) = $COMP_CWORD ]; then
236 __tgcomp "$(__tg_topics)"
240 __tg_complete_arg "-r" && {
241 __tgcomp "$(__tg_remotes)"
252 __tgcomp "$(__tg_refs)"
258 local cur="${COMP_WORDS[COMP_CWORD]}"
267 __tgcomp "$(__tg_topics)"
273 local cur="${COMP_WORDS[COMP_CWORD]}"
275 __tg_complete_arg "--collapse" && {
276 __tgcomp "$(__tg_heads)"
280 __tg_complete_arg "--quilt" && {
295 local cur="${COMP_WORDS[COMP_CWORD]}"
302 __tgcomp "$(__tg_commands)"
307 local cur="${COMP_WORDS[COMP_CWORD]}"
309 __tg_complete_arg "-p" && {
321 __tg_complete_revlist
327 local cur="${COMP_WORDS[COMP_CWORD]}"
331 __tgcomp "$(__tg_topics)"
337 local cur="${COMP_WORDS[COMP_CWORD]}"
341 __tgcomp "$(__tg_topics)"
347 local cur="${COMP_WORDS[COMP_CWORD]}"
351 __tgcomp "$(__tg_topics)"
357 local cur="${COMP_WORDS[COMP_CWORD]}"
361 __tgcomp "$(__tg_remotes)"
376 ### {{{ tg completion
380 local i c=1 command __tg_dir
382 while [ $c -lt $COMP_CWORD ]; do
387 if [ $c -lt $COMP_CWORD ]; then
388 __tgcomp "$(__tg_remotes)"
392 -h|--help) command="help"; break ;;
393 *) command="$i"; break ;;
398 if [ -z "$command" ]; then
399 case "${COMP_WORDS[COMP_CWORD]}" in
406 *) __tgcomp "$(__tg_commands)" ;;
412 create) _tg_create "$c" ;;
413 delete) _tg_delete ;;
414 export) _tg_export ;;
416 import) _tg_import ;;
420 remote) _tg_remote ;;
421 summary) _tg_summary ;;
422 update) _tg_update ;;
429 __tgcomp "$(__tg_refs top-bases)"
430 complete -o default -o nospace -F _tg tg
432 # The following are necessary only for Cygwin, and only are needed
433 # when the user has tab-completed the executable name and consequently
434 # included the '.exe' suffix.
436 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
437 complete -o default -o nospace -F _tg tg.exe