2 # bash completion support for tig
4 # Copyright (C) 2007-2010 Jonas fonseca
5 # Copyright (C) 2006,2007 Shawn Pearce
7 # Based git's git-completion.sh: http://repo.or.cz/w/git/fastimport.git
9 # The contained completion routines provide support for completing:
11 # *) local and remote branch names
12 # *) local and remote tag names
13 # *) tig 'subcommands'
14 # *) tree paths within 'ref:path/to/file' expressions
16 # To use these routines:
18 # 1) Copy this file to somewhere (e.g. ~/.tig-completion.sh).
19 # 2) Added the following line to your .bashrc:
20 # source ~/.tig-completion.sh
22 # 3) You may want to make sure the git executable is available
23 # in your PATH before this script is sourced, as some caching
24 # is performed while the script loads. If git isn't found
25 # at source time then all lookups will be done on demand,
26 # which may be slightly slower.
32 if [ -n "$__git_dir" ]; then
34 elif [ -d .git
]; then
37 git rev-parse
--git-dir 2>/dev
/null
39 elif [ -d "$1/.git" ]; then
48 local all c s
=$
'\n' IFS
=' '$
'\t'$
'\n'
49 local cur
="${COMP_WORDS[COMP_CWORD]}"
55 --*=*) all
="$all$c$4$s" ;;
56 *.
) all
="$all$c$4$s" ;;
57 *) all
="$all$c$4 $s" ;;
61 COMPREPLY
=($
(compgen
-P "$2" -W "$all" -- "$cur"))
67 local cmd i is_hash
=y dir
="$(__tigdir "$1")"
68 if [ -d "$dir" ]; then
69 for i
in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD
; do
70 if [ -e "$dir/$i" ]; then echo $i; fi
72 for i
in $
(git
--git-dir="$dir" \
73 for-each-ref
--format='%(refname)' \
74 refs
/tags refs
/heads refs
/remotes
); do
76 refs
/tags
/*) echo "${i#refs/tags/}" ;;
77 refs
/heads
/*) echo "${i#refs/heads/}" ;;
78 refs
/remotes
/*) echo "${i#refs/remotes/}" ;;
84 for i
in $
(git-ls-remote
"$dir" 2>/dev
/null
); do
88 n
,refs
/tags
/*) is_hash
=y
; echo "${i#refs/tags/}" ;;
89 n
,refs
/heads
/*) is_hash
=y
; echo "${i#refs/heads/}" ;;
90 n
,refs
/remotes
/*) is_hash
=y
; echo "${i#refs/remotes/}" ;;
91 n
,*) is_hash
=y
; echo "$i" ;;
96 __tig_complete_file
()
98 local pfx
ls ref cur
="${COMP_WORDS[COMP_CWORD]}"
114 COMPREPLY
=($
(compgen
-P "$pfx" \
115 -W "$(git --git-dir="$
(__tigdir
)" ls-tree "$ls" \
116 | sed '/^100... blob /s,^.* ,,
125 _tigcomp
"$(__tig_refs)"
130 __tig_complete_revlist
()
132 local pfx cur
="${COMP_WORDS[COMP_CWORD]}"
137 _tigcomp
"$(__tig_refs)" "$pfx" "$cur"
142 _tigcomp
"$(__tig_refs)" "$pfx" "$cur"
148 _tigcomp
"$(__tig_refs)"
155 local cur
="${COMP_WORDS[COMP_CWORD]}"
159 oneline short medium full fuller email raw
160 " "" "${cur##--pretty=}"
165 --max-count= --max-age= --since= --after=
166 --min-age= --before= --until=
167 --root --not --topo-order --date-order
169 --abbrev-commit --abbrev=
171 --author= --committer= --grep=
173 --pretty= --name-status --name-only
184 __tig_complete_revlist
189 local reply
="" ref
=HEAD cur
="${COMP_WORDS[COMP_CWORD]}"
191 if test "$COMP_CWORD" -lt 3; then
192 reply
="$(__tig_refs)"
194 ref
="${COMP_WORDS[2]}"
197 reply
="$reply $(git --git-dir="$
(__tigdir
)" ls-tree "$ref" \
198 | sed '/^100... blob /s,^.* ,,
209 local cur
="${COMP_WORDS[COMP_CWORD]}"
213 oneline short medium full fuller email raw
214 " "" "${cur##--pretty=}"
227 local i c
=1 command __tig_dir
229 while [ $c -lt $COMP_CWORD ]; do
232 --) command="log"; break;;
234 *) command="$i"; break ;;
239 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
240 case "${COMP_WORDS[COMP_CWORD]}" in
241 --*=*) COMPREPLY
=() ;;
243 *) _tigcomp
"blame status show log stash grep $(__tig_refs)" ;;
253 $(__tig_complete_file)
259 # Detect if current shell is ZSH, and if so, load this file in bash
260 # compatibility mode.
261 if [ -n "$ZSH_VERSION" ]; then
262 autoload bashcompinit
266 complete
-o default
-o nospace
-F _tig tig
268 # The following are necessary only for Cygwin, and only are needed
269 # when the user has tab-completed the executable name and consequently
270 # included the '.exe' suffix.
271 if [ Cygwin
= "$(uname -o 2>/dev/null)" ]; then
272 complete
-o default
-o nospace
-F _tig tig.exe