Document shortcoming of stg-k and stg-unnew.
[stgit] / contrib / stgit-completion.bash
1 # bash completion support for StGIT -*- shell-script -*-
2 #
3 # Copyright (C) 2006, Karl Hasselström <kha@treskal.com>
4 # Based on git-completion.sh
5 #
6 # To use these routines:
7 #
8 # 1. Copy this file to somewhere (e.g. ~/.stgit-completion.bash).
9 #
10 # 2. Add the following line to your .bashrc:
11 # . ~/.stgit-completion.bash
12
13 _stg_commands="
14 add
15 applied
16 assimilate
17 branch
18 delete
19 diff
20 clean
21 clone
22 commit
23 cp
24 export
25 files
26 float
27 fold
28 goto
29 hide
30 id
31 import
32 init
33 log
34 mail
35 new
36 patches
37 pick
38 pop
39 pull
40 push
41 rebase
42 refresh
43 rename
44 resolved
45 rm
46 series
47 show
48 sink
49 status
50 sync
51 top
52 unapplied
53 uncommit
54 unhide
55 "
56
57 # The path to .git, or empty if we're not in a repository.
58 _gitdir ()
59 {
60 echo "$(git rev-parse --git-dir 2>/dev/null)"
61 }
62
63 # Name of the current branch, or empty if there isn't one.
64 _current_branch ()
65 {
66 local b=$(git symbolic-ref HEAD 2>/dev/null)
67 echo ${b#refs/heads/}
68 }
69
70 # List of all applied patches.
71 _applied_patches ()
72 {
73 local g=$(_gitdir)
74 [ "$g" ] && cat "$g/patches/$(_current_branch)/applied"
75 }
76
77 # List of all unapplied patches.
78 _unapplied_patches ()
79 {
80 local g=$(_gitdir)
81 [ "$g" ] && cat "$g/patches/$(_current_branch)/unapplied"
82 }
83
84 # List of all applied patches.
85 _hidden_patches ()
86 {
87 local g=$(_gitdir)
88 [ "$g" ] && cat "$g/patches/$(_current_branch)/hidden"
89 }
90
91 # List of all patches.
92 _all_patches ()
93 {
94 local b=$(_current_branch)
95 local g=$(_gitdir)
96 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied"
97 }
98
99 # List of all patches except the current patch.
100 _all_other_patches ()
101 {
102 local b=$(_current_branch)
103 local g=$(_gitdir)
104 [ "$g" ] && cat "$g/patches/$b/applied" "$g/patches/$b/unapplied" \
105 | grep -v "^$(cat $g/patches/$b/current 2> /dev/null)$"
106 }
107
108 _all_branches ()
109 {
110 local g=$(_gitdir)
111 [ "$g" ] && (cd .git/patches/ && echo *)
112 }
113
114 # List the command options
115 _cmd_options ()
116 {
117 stg $1 --help 2>/dev/null | grep -e " --[A-Za-z]" | sed -e "s/.*\(--[^ =]\+\).*/\1/"
118 }
119
120 # Generate completions for patches and patch ranges from the given
121 # patch list function, and options from the given list.
122 _complete_patch_range ()
123 {
124 local patchlist="$1" options="$2"
125 local pfx cur="${COMP_WORDS[COMP_CWORD]}"
126 case "$cur" in
127 *..*)
128 pfx="${cur%..*}.."
129 cur="${cur#*..}"
130 COMPREPLY=($(compgen -P "$pfx" -W "$($patchlist)" -- "$cur"))
131 ;;
132 *)
133 COMPREPLY=($(compgen -W "$options $($patchlist)" -- "$cur"))
134 ;;
135 esac
136 }
137
138 _complete_patch_range_options ()
139 {
140 local patchlist="$1" options="$2" patch_options="$3"
141 local prev="${COMP_WORDS[COMP_CWORD-1]}"
142 local cur="${COMP_WORDS[COMP_CWORD]}"
143 local popt
144 for popt in $patch_options; do
145 if [ $prev == $popt ]; then
146 _complete_patch_range $patchlist
147 return
148 fi
149 done
150 COMPREPLY=($(compgen -W "$options" -- "$cur"))
151 }
152
153 _complete_branch ()
154 {
155 COMPREPLY=($(compgen -W "$(_cmd_options $1) $($2)" -- "${COMP_WORDS[COMP_CWORD]}"))
156 }
157
158 # Generate completions for options from the given list.
159 _complete_options ()
160 {
161 local options="$1"
162 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[COMP_CWORD]}"))
163 }
164
165 _stg_common ()
166 {
167 _complete_options "$(_cmd_options $1)"
168 }
169
170 _stg_patches ()
171 {
172 _complete_patch_range "$2" "$(_cmd_options $1)"
173 }
174
175 _stg_patches_options ()
176 {
177 _complete_patch_range_options "$2" "$(_cmd_options $1)" "$3"
178 }
179
180 _stg_help ()
181 {
182 _complete_options "$_stg_commands"
183 }
184
185 _stg ()
186 {
187 local i c=1 command
188
189 while [ $c -lt $COMP_CWORD ]; do
190 if [ $c == 1 ]; then
191 command="${COMP_WORDS[c]}"
192 fi
193 c=$((++c))
194 done
195
196 # Complete name of subcommand.
197 if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
198 COMPREPLY=($(compgen \
199 -W "--help --version copyright help $_stg_commands" \
200 -- "${COMP_WORDS[COMP_CWORD]}"))
201 return;
202 fi
203
204 # Complete arguments to subcommands.
205 case "$command" in
206 # generic commands
207 help) _stg_help ;;
208 # repository commands
209 id) _stg_patches $command _all_patches ;;
210 # stack commands
211 float) _stg_patches $command _all_patches ;;
212 goto) _stg_patches $command _all_other_patches ;;
213 hide) _stg_patches $command _unapplied_patches ;;
214 pop) _stg_patches $command _applied_patches ;;
215 push) _stg_patches $command _unapplied_patches ;;
216 series) _stg_patches $command _all_patches ;;
217 sink) _stg_patches $command _all_patches ;;
218 unhide) _stg_patches $command _hidden_patches ;;
219 # patch commands
220 delete) _stg_patches $command _all_patches ;;
221 export) _stg_patches $command _applied_patches ;;
222 files) _stg_patches $command _all_patches ;;
223 log) _stg_patches $command _all_patches ;;
224 mail) _stg_patches $command _all_patches ;;
225 pick) _stg_patches $command _unapplied_patches ;;
226 refresh)_stg_patches_options $command _applied_patches "-p --patch" ;;
227 rename) _stg_patches $command _all_patches ;;
228 show) _stg_patches $command _all_patches ;;
229 sync) _stg_patches $command _applied_patches ;;
230 # working-copy commands
231 diff) _stg_patches_options $command _applied_patches "-r --range" ;;
232 # commands that usually raher accept branches
233 branch) _complete_branch $command _all_branches ;;
234 rebase) _complete_branch $command _all_branches ;;
235 # all the other commands
236 *) _stg_common $command ;;
237 esac
238 }
239
240 complete -o default -F _stg stg