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 # To use these routines:
12 # 1) Copy this file to somewhere (e.g. ~/.tg-completion.sh).
13 # 2) Source it from your ~/.bashrc.
15 # Note: Make sure the tg script is in your PATH before you source this
16 # script, so it can properly setup cached values.
18 case "$COMP_WORDBREAKS" in
20 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:"
28 if [ -n "$__tg_dir" ]; then
30 elif [ -d .git ]; then
33 git rev-parse --git-dir 2>/dev/null
35 elif [ -d "$1/.git" ]; then
44 local c IFS=' '$'\t'$'\n'
47 --*=*) printf %s$'\n' "$c$2" ;;
48 *.) printf %s$'\n' "$c$2" ;;
49 *) printf %s$'\n' "$c$2 " ;;
56 local cur="${COMP_WORDS[COMP_CWORD]}"
66 COMPREPLY=($(compgen -P "$2" \
67 -W "$(__tgcomp_1 "$1" "$4")" \
75 local cmd i is_hash=y dir="$(__tgdir "$1")"
76 if [ -d "$dir" ]; then
77 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
81 for i in $(git ls-remote "$1" 2>/dev/null); do
85 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
86 n,*) is_hash=y; echo "$i" ;;
93 local cmd i is_hash=y dir="$(__tgdir "$1")"
94 if [ -d "$dir" ]; then
95 if [ -e "$dir/HEAD" ]; then echo HEAD; fi
96 git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
97 refs/tags refs/heads refs/remotes
100 for i in $(git ls-remote "$dir" 2>/dev/null); do
101 case "$is_hash,$i" in
104 n,refs/tags/*) is_hash=y; echo "${i#refs/tags/}" ;;
105 n,refs/heads/*) is_hash=y; echo "${i#refs/heads/}" ;;
106 n,refs/remotes/*) is_hash=y; echo "${i#refs/remotes/}" ;;
107 n,*) is_hash=y; echo "$i" ;;
115 for i in $(__tg_refs "$1"); do
122 local cmd i is_hash=y
123 for i in $(git ls-remote "$1" 2>/dev/null); do
124 case "$is_hash,$i" in
127 echo "$i:refs/remotes/$1/${i#refs/heads/}"
131 n,refs/tags/*) is_hash=y;;
139 local i ngoff IFS=$'\n' d="$(__tgdir)"
140 shopt -q nullglob || ngoff=1
142 for i in "$d/remotes"/*; do
143 echo ${i#$d/remotes/}
145 [ "$ngoff" ] && shopt -u nullglob
146 for i in $(git --git-dir="$d" config --list); do
156 __tg_find_subcommand ()
158 local word subcommand c=1
160 while [ $c -lt $COMP_CWORD ]; do
161 word="${COMP_WORDS[c]}"
162 for subcommand in $1; do
163 if [ "$subcommand" = "$word" ]; then
172 __tg_complete_revlist ()
174 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
179 __tgcomp "$(__tg_refs)" "$pfx" "$cur"
184 __tgcomp "$(__tg_refs)" "$pfx" "$cur"
187 __tgcomp "$(__tg_refs)"
199 if [ -n "$__tg_all_commandlist" ]; then
200 echo "$__tg_all_commandlist"
204 for i in $(tg help | sed -n 's/^Usage:.*(\(.*\)).*/\1/p' | tr '|' ' ')
207 *--*) : helper pattern;;
212 __tg_all_commandlist=
213 __tg_all_commandlist="$(__tg_commands 2>/dev/null)"
217 if [ $COMP_CWORD -gt 2 ] && [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "$1" ]; then
229 local cur="${COMP_WORDS[COMP_CWORD]}"
231 # Name must be the first arg after the create command
232 if [ $((cmd + 1)) = $COMP_CWORD ]; then
233 __tgcomp "$(__tg_topics)"
237 __tg_complete_arg "-r" && {
238 __tgcomp "$(__tg_remotes)"
249 __tgcomp "$(__tg_refs)"
255 local cur="${COMP_WORDS[COMP_CWORD]}"
264 __tgcomp "$(__tg_topics)"
270 local subcommands="add"
271 local subcommand="$(__git_find_subcommand "$subcommands")"
272 if [ -z "$subcommand" ]; then
273 __tgcomp "$subcommands"
277 case "$subcommand" in
279 __tgcomp "$(__tg_refs)"
285 local cur="${COMP_WORDS[COMP_CWORD]}"
287 __tg_complete_arg "--collapse" && {
288 __tgcomp "$(__tg_heads)"
292 __tg_complete_arg "--quilt" && {
307 local cur="${COMP_WORDS[COMP_CWORD]}"
314 __tgcomp "$(__tg_commands)"
319 local cur="${COMP_WORDS[COMP_CWORD]}"
321 __tg_complete_arg "-p" && {
333 __tg_complete_revlist
339 local cur="${COMP_WORDS[COMP_CWORD]}"
343 __tgcomp "$(__tg_topics)"
349 local cur="${COMP_WORDS[COMP_CWORD]}"
353 __tgcomp "$(__tg_topics)"
359 local cur="${COMP_WORDS[COMP_CWORD]}"
363 __tgcomp "$(__tg_topics)"
369 local cur="${COMP_WORDS[COMP_CWORD]}"
373 __tgcomp "$(__tg_remotes)"
388 ### {{{ tg completion
392 local i c=1 command __tg_dir
394 while [ $c -lt $COMP_CWORD ]; do
399 if [ $c -lt $COMP_CWORD ]; then
400 __tgcomp "$(__tg_remotes)"
404 -h|--help) command="help"; break ;;
405 *) command="$i"; break ;;
410 if [ -z "$command" ]; then
411 case "${COMP_WORDS[COMP_CWORD]}" in
418 *) __tgcomp "$(__tg_commands)" ;;
424 create) _tg_create "$c" ;;
425 delete) _tg_delete ;;
426 depend) _tg_depend ;;
427 export) _tg_export ;;
429 import) _tg_import ;;
433 remote) _tg_remote ;;
434 summary) _tg_summary ;;
435 update) _tg_update ;;
442 complete -o default -o nospace -F _tg tg
444 # The following are necessary only for Cygwin, and only are needed
445 # when the user has tab-completed the executable name and consequently
446 # included the '.exe' suffix.
448 if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
449 complete -o default -o nospace -F _tg tg.exe