##// END OF EJS Templates
bash_completion: don't complete export with "garbage" when mq is not around...
Alexis S. L. Carvalho -
r3480:03932425 default
parent child Browse files
Show More
@@ -1,390 +1,395 b''
1 1 # bash completion for the Mercurial distributed SCM
2 2
3 3 # Docs:
4 4 #
5 5 # If you source this file from your .bashrc, bash should be able to
6 6 # complete a command line that uses hg with all the available commands
7 7 # and options and sometimes even arguments.
8 8 #
9 9 # Mercurial allows you to define additional commands through extensions.
10 10 # Bash should be able to automatically figure out the name of these new
11 11 # commands and their options. If you also want to tell it how to
12 12 # complete non-option arguments, see below for how to define an
13 13 # _hg_cmd_foo function.
14 14 #
15 15 #
16 16 # Notes about completion for specific commands:
17 17 #
18 18 # - the completion function for the email command from the patchbomb
19 19 # extension will try to call _hg_emails to get a list of e-mail
20 20 # addresses. It's up to the user to define this function. For
21 21 # example, put the addresses of the lists that you usually patchbomb
22 22 # in ~/.patchbomb-to and the addresses that you usually use to send
23 23 # the patchbombs in ~/.patchbomb-from and use something like this:
24 24 #
25 25 # _hg_emails()
26 26 # {
27 27 # if [ -r ~/.patchbomb-$1 ]; then
28 28 # cat ~/.patchbomb-$1
29 29 # fi
30 30 # }
31 31 #
32 32 #
33 33 # Writing completion functions for additional commands:
34 34 #
35 35 # If it exists, the function _hg_cmd_foo will be called without
36 36 # arguments to generate the completion candidates for the hg command
37 37 # "foo".
38 38 #
39 39 # In addition to the regular completion variables provided by bash,
40 40 # the following variables are also set:
41 41 # - $hg - the hg program being used (e.g. /usr/bin/hg)
42 42 # - $cmd - the name of the hg command being completed
43 43 # - $cmd_index - the index of $cmd in $COMP_WORDS
44 44 # - $cur - the current argument being completed
45 45 # - $prev - the argument before $cur
46 46 # - $global_args - "|"-separated list of global options that accept
47 47 # an argument (e.g. '--cwd|-R|--repository')
48 48 # - $canonical - 1 if we canonicalized $cmd before calling the function
49 49 # 0 otherwise
50 50 #
51 51
52 52 shopt -s extglob
53 53
54 54 _hg_commands()
55 55 {
56 56 local commands
57 57 commands="$("$hg" debugcomplete "$cur" 2>/dev/null)" || commands=""
58 58 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
59 59 }
60 60
61 61 _hg_paths()
62 62 {
63 63 local paths="$("$hg" paths 2>/dev/null | sed -e 's/ = .*$//')"
64 64 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
65 65 }
66 66
67 67 _hg_repos()
68 68 {
69 69 local i
70 70 for i in $(compgen -d -- "$cur"); do
71 71 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
72 72 done
73 73 }
74 74
75 75 _hg_status()
76 76 {
77 77 local files="$("$hg" status -n$1 . 2>/dev/null)"
78 78 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
79 79 }
80 80
81 81 _hg_tags()
82 82 {
83 83 local tags="$("$hg" tags -q 2>/dev/null)"
84 84 local IFS=$'\n'
85 85 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$tags' -- "$cur"))
86 86 }
87 87
88 88 # this is "kind of" ugly...
89 89 _hg_count_non_option()
90 90 {
91 91 local i count=0
92 92 local filters="$1"
93 93
94 94 for ((i=1; $i<=$COMP_CWORD; i++)); do
95 95 if [[ "${COMP_WORDS[i]}" != -* ]]; then
96 96 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
97 97 continue
98 98 fi
99 99 count=$(($count + 1))
100 100 fi
101 101 done
102 102
103 103 echo $(($count - 1))
104 104 }
105 105
106 106 _hg()
107 107 {
108 108 local cur prev cmd cmd_index opts i
109 109 # global options that receive an argument
110 110 local global_args='--cwd|-R|--repository'
111 111 local hg="$1"
112 112
113 113 COMPREPLY=()
114 114 cur="$2"
115 115 prev="$3"
116 116
117 117 # searching for the command
118 118 # (first non-option argument that doesn't follow a global option that
119 119 # receives an argument)
120 120 for ((i=1; $i<=$COMP_CWORD; i++)); do
121 121 if [[ ${COMP_WORDS[i]} != -* ]]; then
122 122 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
123 123 cmd="${COMP_WORDS[i]}"
124 124 cmd_index=$i
125 125 break
126 126 fi
127 127 fi
128 128 done
129 129
130 130 if [[ "$cur" == -* ]]; then
131 131 opts=$("$hg" debugcomplete --options "$cmd" 2>/dev/null)
132 132
133 133 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
134 134 return
135 135 fi
136 136
137 137 # global options
138 138 case "$prev" in
139 139 -R|--repository)
140 140 _hg_repos
141 141 return
142 142 ;;
143 143 --cwd)
144 144 # Stick with default bash completion
145 145 return
146 146 ;;
147 147 esac
148 148
149 149 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
150 150 _hg_commands
151 151 return
152 152 fi
153 153
154 154 # try to generate completion candidates for whatever command the user typed
155 155 local help
156 156 local canonical=0
157 157 if _hg_command_specific; then
158 158 return
159 159 fi
160 160
161 161 # canonicalize the command name and try again
162 162 help=$("$hg" help "$cmd" 2>/dev/null)
163 163 if [ $? -ne 0 ]; then
164 164 # Probably either the command doesn't exist or it's ambiguous
165 165 return
166 166 fi
167 167 cmd=${help#hg }
168 168 cmd=${cmd%%[$' \n']*}
169 169 canonical=1
170 170 _hg_command_specific
171 171 }
172 172
173 173 _hg_command_specific()
174 174 {
175 175 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
176 176 "_hg_cmd_$cmd"
177 177 return 0
178 178 fi
179 179
180 180 if [ "$cmd" != status ] && [ "$prev" = -r ] || [ "$prev" == --rev ]; then
181 181 if [ $canonical = 1 ]; then
182 182 _hg_tags
183 183 return 0
184 184 elif [[ status != "$cmd"* ]]; then
185 185 _hg_tags
186 186 return 0
187 187 else
188 188 return 1
189 189 fi
190 190 fi
191 191
192 192 case "$cmd" in
193 193 help)
194 194 _hg_commands
195 195 ;;
196 196 export|manifest|update)
197 197 _hg_tags
198 198 ;;
199 199 pull|push|outgoing|incoming)
200 200 _hg_paths
201 201 _hg_repos
202 202 ;;
203 203 paths)
204 204 _hg_paths
205 205 ;;
206 206 add)
207 207 _hg_status "u"
208 208 ;;
209 209 commit)
210 210 _hg_status "mar"
211 211 ;;
212 212 remove)
213 213 _hg_status "d"
214 214 ;;
215 215 forget)
216 216 _hg_status "a"
217 217 ;;
218 218 diff)
219 219 _hg_status "mar"
220 220 ;;
221 221 revert)
222 222 _hg_status "mard"
223 223 ;;
224 224 clone)
225 225 local count=$(_hg_count_non_option)
226 226 if [ $count = 1 ]; then
227 227 _hg_paths
228 228 fi
229 229 _hg_repos
230 230 ;;
231 231 debugindex|debugindexdot)
232 232 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
233 233 ;;
234 234 debugdata)
235 235 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
236 236 ;;
237 237 *)
238 238 return 1
239 239 ;;
240 240 esac
241 241
242 242 return 0
243 243 }
244 244
245 245 complete -o bashdefault -o default -F _hg hg 2>/dev/null \
246 246 || complete -o default -F _hg hg
247 247
248 248
249 249 # Completion for commands provided by extensions
250 250
251 251 # mq
252 252 _hg_ext_mq_patchlist()
253 253 {
254 local patches=$("$hg" $1 2>/dev/null)
255 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
254 local patches
255 patches=$("$hg" $1 2>/dev/null)
256 if [ $? -eq 0 ] && [ "$patches" ]; then
257 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
258 return 0
259 fi
260 return 1
256 261 }
257 262
258 263 _hg_ext_mq_queues()
259 264 {
260 265 local root=$("$hg" root 2>/dev/null)
261 266 local n
262 267 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
263 268 # I think we're usually not interested in the regular "patches" queue
264 269 # so just filter it.
265 270 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
266 271 COMPREPLY=(${COMPREPLY[@]:-} "$n")
267 272 fi
268 273 done
269 274 }
270 275
271 276 _hg_cmd_qpop()
272 277 {
273 278 if [[ "$prev" = @(-n|--name) ]]; then
274 279 _hg_ext_mq_queues
275 280 return
276 281 fi
277 282 _hg_ext_mq_patchlist qapplied
278 283 }
279 284
280 285 _hg_cmd_qpush()
281 286 {
282 287 if [[ "$prev" = @(-n|--name) ]]; then
283 288 _hg_ext_mq_queues
284 289 return
285 290 fi
286 291 _hg_ext_mq_patchlist qunapplied
287 292 }
288 293
289 294 _hg_cmd_qdelete()
290 295 {
291 296 _hg_ext_mq_patchlist qunapplied
292 297 }
293 298
294 299 _hg_cmd_qsave()
295 300 {
296 301 if [[ "$prev" = @(-n|--name) ]]; then
297 302 _hg_ext_mq_queues
298 303 return
299 304 fi
300 305 }
301 306
302 307 _hg_cmd_strip()
303 308 {
304 309 _hg_tags
305 310 }
306 311
307 312 _hg_cmd_qcommit()
308 313 {
309 314 local root=$("$hg" root 2>/dev/null)
310 315 # this is run in a sub-shell, so we can't use _hg_status
311 316 local files=$(cd "$root/.hg/patches" 2>/dev/null &&
312 317 "$hg" status -nmar 2>/dev/null)
313 318 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
314 319 }
315 320
316 321 _hg_cmd_export()
317 322 {
318 323 _hg_ext_mq_patchlist qapplied
319 324 }
320 325
321 326
322 327 # hbisect
323 328 _hg_cmd_bisect()
324 329 {
325 330 local i subcmd
326 331
327 332 # find the sub-command
328 333 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
329 334 if [[ ${COMP_WORDS[i]} != -* ]]; then
330 335 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
331 336 subcmd="${COMP_WORDS[i]}"
332 337 break
333 338 fi
334 339 fi
335 340 done
336 341
337 342 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
338 343 COMPREPLY=(${COMPREPLY[@]:-}
339 344 $(compgen -W 'bad good help init next reset' -- "$cur"))
340 345 return
341 346 fi
342 347
343 348 case "$subcmd" in
344 349 good|bad)
345 350 _hg_tags
346 351 ;;
347 352 esac
348 353
349 354 return
350 355 }
351 356
352 357
353 358 # patchbomb
354 359 _hg_cmd_email()
355 360 {
356 361 case "$prev" in
357 362 -c|--cc|-t|--to|-f|--from)
358 363 # we need an e-mail address. let the user provide a function
359 364 # to get them
360 365 if [ "$(type -t _hg_emails)" = function ]; then
361 366 local arg=to
362 367 if [[ "$prev" == @(-f|--from) ]]; then
363 368 arg=from
364 369 fi
365 370 local addresses=$(_hg_emails $arg)
366 371 COMPREPLY=(${COMPREPLY[@]:-}
367 372 $(compgen -W '$addresses' -- "$cur"))
368 373 fi
369 374 return
370 375 ;;
371 376 -m|--mbox)
372 377 # fallback to standard filename completion
373 378 return
374 379 ;;
375 380 -s|--subject)
376 381 # free form string
377 382 return
378 383 ;;
379 384 esac
380 385
381 386 _hg_tags
382 387 return
383 388 }
384 389
385 390
386 391 # gpg
387 392 _hg_cmd_sign()
388 393 {
389 394 _hg_tags
390 395 }
General Comments 0
You need to be logged in to leave comments. Login now