Debian Codegraph @

contrib/bash_completion

raw
1 # bash completion for the Mercurial distributed SCM -*- sh -*-
2
3 # Docs:
4 #
5 # If you source this file from your .bashrc, bash should be able to
6 # complete a command line that uses hg with all the available commands
7 # and options and sometimes even arguments.
8 #
9 # Mercurial allows you to define additional commands through extensions.
10 # Bash should be able to automatically figure out the name of these new
11 # commands and their options. See below for how to define _hg_opt_foo
12 # and _hg_cmd_foo functions to fine-tune the completion for option and
13 # non-option arguments, respectively.
14 #
15 #
16 # Notes about completion for specific commands:
17 #
18 # - the completion function for the email command from the patchbomb
19 # extension will try to call _hg_emails to get a list of e-mail
20 # addresses. It's up to the user to define this function. For
21 # example, put the addresses of the lists that you usually patchbomb
22 # in ~/.patchbomb-to and the addresses that you usually use to send
23 # the patchbombs in ~/.patchbomb-from and use something like this:
24 #
25 # _hg_emails()
26 # {
27 # if [ -r ~/.patchbomb-$1 ]; then
28 # cat ~/.patchbomb-$1
29 # fi
30 # }
31 #
32 #
33 # Writing completion functions for additional commands:
34 #
35 # If it exists, the function _hg_cmd_foo will be called without
36 # arguments to generate the completion candidates for the hg command
37 # "foo". If the command receives some arguments that aren't options
38 # even though they start with a "-", you can define a function called
39 # _hg_opt_foo to generate the completion candidates. If _hg_opt_foo
40 # doesn't return 0, regular completion for options is attempted.
41 #
42 # In addition to the regular completion variables provided by bash,
43 # the following variables are also set:
44 # - $hg - the hg program being used (e.g. /usr/bin/hg)
45 # - $cmd - the name of the hg command being completed
46 # - $cmd_index - the index of $cmd in $COMP_WORDS
47 # - $cur - the current argument being completed
48 # - $prev - the argument before $cur
49 # - $global_args - "|"-separated list of global options that accept
50 # an argument (e.g. '--cwd|-R|--repository')
51 # - $canonical - 1 if we canonicalized $cmd before calling the function
52 # 0 otherwise
53 #
54
55 shopt -s extglob
56
57 _hg_cmd()
58 {
59 HGPLAIN=1 "$hg" "$@" 2>/dev/null
60 }
61
62 _hg_commands()
63 {
64 local commands
65 commands="$(HGPLAINEXCEPT=alias _hg_cmd debugcomplete "$cur")" || commands=""
66 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$commands' -- "$cur"))
67 }
68
69 _hg_paths()
70 {
71 local paths="$(_hg_cmd paths -q)"
72 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$paths' -- "$cur"))
73 }
74
75 _hg_repos()
76 {
77 local i
78 for i in $(compgen -d -- "$cur"); do
79 test ! -d "$i"/.hg || COMPREPLY=(${COMPREPLY[@]:-} "$i")
80 done
81 }
82
83 _hg_debugpathcomplete()
84 {
85 local files="$(_hg_cmd debugpathcomplete $1 "$cur")"
86 local IFS=$'\n'
87 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
88 }
89
90 _hg_status()
91 {
92 if [ -z "$HGCOMPLETE_NOSTATUS" ]; then
93 local files="$(_hg_cmd status -n$1 "glob:$cur**")"
94 local IFS=$'\n'
95 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
96 fi
97 }
98
99 _hg_branches()
100 {
101 local branches="$(_hg_cmd branches -q)"
102 local IFS=$'\n'
103 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$branches' -- "$cur"))
104 }
105
106 _hg_bookmarks()
107 {
108 local bookmarks="$(_hg_cmd bookmarks -q)"
109 local IFS=$'\n'
110 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$bookmarks' -- "$cur"))
111 }
112
113 _hg_labels()
114 {
115 local labels="$(_hg_cmd debugnamecomplete "$cur")"
116 local IFS=$'\n'
117 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$labels' -- "$cur"))
118 }
119
120 # this is "kind of" ugly...
121 _hg_count_non_option()
122 {
123 local i count=0
124 local filters="$1"
125
126 for ((i=1; $i<=$COMP_CWORD; i++)); do
127 if [[ "${COMP_WORDS[i]}" != -* ]]; then
128 if [[ ${COMP_WORDS[i-1]} == @($filters|$global_args) ]]; then
129 continue
130 fi
131 count=$(($count + 1))
132 fi
133 done
134
135 echo $(($count - 1))
136 }
137
138 _hg_fix_wordlist()
139 {
140 local LASTCHAR=' '
141 if [ ${#COMPREPLY[@]} = 1 ]; then
142 [ -d "$COMPREPLY" ] && LASTCHAR=/
143 COMPREPLY=$(printf %q%s "$COMPREPLY" "$LASTCHAR")
144 else
145 for ((i=0; i < ${#COMPREPLY[@]}; i++)); do
146 [ -d "${COMPREPLY[$i]}" ] && COMPREPLY[$i]=${COMPREPLY[$i]}/
147 done
148 fi
149 }
150
151 _hg()
152 {
153 local cur prev cmd cmd_index opts i aliashg
154 # global options that receive an argument
155 local global_args='--cwd|-R|--repository|--color|--config|--encoding|--encodingmode|--pager'
156 local hg="$1"
157 local canonical=0
158
159 aliashg=$(alias $hg 2>/dev/null)
160 if [[ -n "$aliashg" ]]; then
161 aliashg=${aliashg#"alias $hg='"}
162 aliashg=${aliashg%"'"}
163 # `source`d aliases break completion, so ignore them
164 if [[ "${aliashg:0:7}" != "source " ]]; then
165 hg=$aliashg
166 fi
167 fi
168
169 COMPREPLY=()
170 cur="$2"
171 prev="$3"
172
173 # searching for the command
174 # (first non-option argument that doesn't follow a global option that
175 # receives an argument)
176 for ((i=1; $i<=$COMP_CWORD; i++)); do
177 if [[ ${COMP_WORDS[i]} != -* ]]; then
178 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
179 cmd="${COMP_WORDS[i]}"
180 cmd_index=$i
181 break
182 fi
183 fi
184 done
185
186 if [[ "$cur" == -* ]]; then
187 if [ "$(type -t "_hg_opt_$cmd")" = function ] && "_hg_opt_$cmd"; then
188 _hg_fix_wordlist
189 return
190 fi
191
192 opts=$(HGPLAINEXCEPT=alias _hg_cmd debugcomplete --options "$cmd")
193
194 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$opts' -- "$cur"))
195 _hg_fix_wordlist
196 return
197 fi
198
199 # global options
200 case "$prev" in
201 -R|--repository)
202 _hg_paths
203 _hg_repos
204 _hg_fix_wordlist
205 return
206 ;;
207 --cwd)
208 # Stick with default bash completion
209 _hg_fix_wordlist
210 return
211 ;;
212 --color)
213 local choices='true false yes no always auto never debug'
214 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$choices' -- "$cur"))
215 _hg_fix_wordlist
216 return
217 ;;
218 --pager)
219 local choices='true false yes no always auto never'
220 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$choices' -- "$cur"))
221 _hg_fix_wordlist
222 return
223 ;;
224 esac
225
226 if [ -z "$cmd" ] || [ $COMP_CWORD -eq $i ]; then
227 _hg_commands
228 _hg_fix_wordlist
229 return
230 fi
231
232 # try to generate completion candidates for whatever command the user typed
233 local help
234 if _hg_command_specific; then
235 _hg_fix_wordlist
236 return
237 fi
238
239 # canonicalize the command name and try again
240 help=$(_hg_cmd help "$cmd")
241 if [ $? -ne 0 ]; then
242 # Probably either the command doesn't exist or it's ambiguous
243 return
244 fi
245 cmd=${help#hg }
246 cmd=${cmd%%[$' \n']*}
247 canonical=1
248 _hg_command_specific
249 _hg_fix_wordlist
250 }
251
252 _hg_command_specific()
253 {
254 if [ "$(type -t "_hg_cmd_$cmd")" = function ]; then
255 "_hg_cmd_$cmd"
256 return 0
257 fi
258
259 if [ "$cmd" != status ]; then
260 case "$prev" in
261 -r|--rev)
262 if [[ $canonical = 1 || status != "$cmd"* ]]; then
263 _hg_labels
264 return 0
265 fi
266 return 1
267 ;;
268 -B|--bookmark)
269 if [[ $canonical = 1 || status != "$cmd"* ]]; then
270 _hg_bookmarks
271 return 0
272 fi
273 return 1
274 ;;
275 -b|--branch)
276 if [[ $canonical = 1 || status != "$cmd"* ]]; then
277 _hg_branches
278 return 0
279 fi
280 return 1
281 ;;
282 esac
283 fi
284
285 local aliascmd=$(_hg_cmd showconfig alias.$cmd | awk '{print $1}')
286 [ -n "$aliascmd" ] && cmd=$aliascmd
287
288 case "$cmd" in
289 help)
290 _hg_commands
291 ;;
292 export)
293 if _hg_ext_mq_patchlist qapplied && [ "${COMPREPLY[*]}" ]; then
294 return 0
295 fi
296 _hg_labels
297 ;;
298 manifest|update|up|checkout|co)
299 _hg_labels
300 ;;
301 pull|push|outgoing|incoming)
302 _hg_paths
303 _hg_repos
304 ;;
305 paths)
306 _hg_paths
307 ;;
308 add)
309 _hg_status "u"
310 ;;
311 merge)
312 _hg_labels
313 ;;
314 commit|ci|record|amend)
315 _hg_status "mar"
316 ;;
317 remove|rm)
318 _hg_debugpathcomplete -n
319 ;;
320 forget)
321 _hg_debugpathcomplete -fa
322 ;;
323 diff)
324 _hg_status "mar"
325 ;;
326 revert)
327 _hg_status "mard"
328 ;;
329 clone)
330 local count=$(_hg_count_non_option)
331 if [ $count = 1 ]; then
332 _hg_paths
333 fi
334 _hg_repos
335 ;;
336 debugindex|debugindexdot)
337 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.i" -- "$cur"))
338 ;;
339 debugdata)
340 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -f -X "!*.d" -- "$cur"))
341 ;;
342 *)
343 return 1
344 ;;
345 esac
346
347 return 0
348 }
349
350 complete -o bashdefault -o default -o nospace -F _hg hg \
351 || complete -o default -o nospace -F _hg hg
352
353
354 # Completion for commands provided by extensions
355
356 # bookmarks
357 _hg_cmd_bookmarks()
358 {
359 _hg_bookmarks
360 return
361 }
362
363 # mq
364 _hg_ext_mq_patchlist()
365 {
366 local patches
367 patches=$(_hg_cmd $1)
368 if [ $? -eq 0 ] && [ "$patches" ]; then
369 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$patches' -- "$cur"))
370 return 0
371 fi
372 return 1
373 }
374
375 _hg_ext_mq_queues()
376 {
377 local root=$(_hg_cmd root)
378 local n
379 for n in $(cd "$root"/.hg && compgen -d -- "$cur"); do
380 # I think we're usually not interested in the regular "patches" queue
381 # so just filter it.
382 if [ "$n" != patches ] && [ -e "$root/.hg/$n/series" ]; then
383 COMPREPLY=(${COMPREPLY[@]:-} "$n")
384 fi
385 done
386 }
387
388 _hg_cmd_qpop()
389 {
390 if [[ "$prev" = @(-n|--name) ]]; then
391 _hg_ext_mq_queues
392 return
393 fi
394 _hg_ext_mq_patchlist qapplied
395 }
396
397 _hg_cmd_qpush()
398 {
399 if [[ "$prev" = @(-n|--name) ]]; then
400 _hg_ext_mq_queues
401 return
402 fi
403 _hg_ext_mq_patchlist qunapplied
404 }
405
406 _hg_cmd_qgoto()
407 {
408 if [[ "$prev" = @(-n|--name) ]]; then
409 _hg_ext_mq_queues
410 return
411 fi
412 _hg_ext_mq_patchlist qseries
413 }
414
415 _hg_cmd_qdelete()
416 {
417 local qcmd=qunapplied
418 if [[ "$prev" = @(-r|--rev) ]]; then
419 qcmd=qapplied
420 fi
421 _hg_ext_mq_patchlist $qcmd
422 }
423
424 _hg_cmd_qfinish()
425 {
426 if [[ "$prev" = @(-a|--applied) ]]; then
427 return
428 fi
429 _hg_ext_mq_patchlist qapplied
430 }
431
432 _hg_cmd_qsave()
433 {
434 if [[ "$prev" = @(-n|--name) ]]; then
435 _hg_ext_mq_queues
436 return
437 fi
438 }
439
440 _hg_cmd_rebase() {
441 if [[ "$prev" = @(-s|--source|-d|--dest|-b|--base|-r|--rev) ]]; then
442 _hg_labels
443 return
444 fi
445 }
446
447 _hg_cmd_strip()
448 {
449 if [[ "$prev" = @(-B|--bookmark) ]]; then
450 _hg_bookmarks
451 return
452 fi
453 _hg_labels
454 }
455
456 _hg_cmd_qcommit()
457 {
458 local root=$(_hg_cmd root)
459 # this is run in a sub-shell, so we can't use _hg_status
460 local files=$(cd "$root/.hg/patches" && _hg_cmd status -nmar)
461 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$files' -- "$cur"))
462 }
463
464 _hg_cmd_qfold()
465 {
466 _hg_ext_mq_patchlist qunapplied
467 }
468
469 _hg_cmd_qrename()
470 {
471 _hg_ext_mq_patchlist qseries
472 }
473
474 _hg_cmd_qheader()
475 {
476 _hg_ext_mq_patchlist qseries
477 }
478
479 _hg_cmd_qclone()
480 {
481 local count=$(_hg_count_non_option)
482 if [ $count = 1 ]; then
483 _hg_paths
484 fi
485 _hg_repos
486 }
487
488 _hg_ext_mq_guards()
489 {
490 _hg_cmd qselect --series | sed -e 's/^.//'
491 }
492
493 _hg_cmd_qselect()
494 {
495 local guards=$(_hg_ext_mq_guards)
496 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$guards' -- "$cur"))
497 }
498
499 _hg_cmd_qguard()
500 {
501 local prefix=''
502
503 if [[ "$cur" == +* ]]; then
504 prefix=+
505 elif [[ "$cur" == -* ]]; then
506 prefix=-
507 fi
508 local ncur=${cur#[-+]}
509
510 if ! [ "$prefix" ]; then
511 _hg_ext_mq_patchlist qseries
512 return
513 fi
514
515 local guards=$(_hg_ext_mq_guards)
516 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -P $prefix -W '$guards' -- "$ncur"))
517 }
518
519 _hg_opt_qguard()
520 {
521 local i
522 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
523 if [[ ${COMP_WORDS[i]} != -* ]]; then
524 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
525 _hg_cmd_qguard
526 return 0
527 fi
528 elif [ "${COMP_WORDS[i]}" = -- ]; then
529 _hg_cmd_qguard
530 return 0
531 fi
532 done
533 return 1
534 }
535
536 _hg_cmd_qqueue()
537 {
538 local q
539 local queues
540 local opts="--list --create --rename --delete --purge"
541
542 queues=$( _hg_cmd qqueue --quiet )
543
544 COMPREPLY=( $( compgen -W "${opts} ${queues}" "${cur}" ) )
545 }
546
547
548 # hbisect
549 _hg_cmd_bisect()
550 {
551 local i subcmd
552
553 # find the sub-command
554 for ((i=cmd_index+1; i<=COMP_CWORD; i++)); do
555 if [[ ${COMP_WORDS[i]} != -* ]]; then
556 if [[ ${COMP_WORDS[i-1]} != @($global_args) ]]; then
557 subcmd="${COMP_WORDS[i]}"
558 break
559 fi
560 fi
561 done
562
563 if [ -z "$subcmd" ] || [ $COMP_CWORD -eq $i ] || [ "$subcmd" = help ]; then
564 COMPREPLY=(${COMPREPLY[@]:-}
565 $(compgen -W 'bad good help init next reset' -- "$cur"))
566 return
567 fi
568
569 case "$subcmd" in
570 good|bad)
571 _hg_labels
572 ;;
573 esac
574
575 return
576 }
577
578
579 # patchbomb
580 _hg_cmd_email()
581 {
582 case "$prev" in
583 -c|--cc|-t|--to|-f|--from|--bcc)
584 # we need an e-mail address. let the user provide a function
585 # to get them
586 if [ "$(type -t _hg_emails)" = function ]; then
587 local arg=to
588 if [[ "$prev" == @(-f|--from) ]]; then
589 arg=from
590 fi
591 local addresses=$(_hg_emails $arg)
592 COMPREPLY=(${COMPREPLY[@]:-}
593 $(compgen -W '$addresses' -- "$cur"))
594 fi
595 return
596 ;;
597 -m|--mbox)
598 # fallback to standard filename completion
599 return
600 ;;
601 -s|--subject)
602 # free form string
603 return
604 ;;
605 esac
606
607 _hg_labels
608 return
609 }
610
611
612 # gpg
613 _hg_cmd_sign()
614 {
615 _hg_labels
616 }
617
618
619 # transplant
620 _hg_cmd_transplant()
621 {
622 case "$prev" in
623 -s|--source)
624 _hg_paths
625 _hg_repos
626 return
627 ;;
628 --filter)
629 # standard filename completion
630 return
631 ;;
632 esac
633
634 # all other transplant options values and command parameters are revisions
635 _hg_labels
636 return
637 }
638
639 # shelve
640 _hg_shelves()
641 {
642 local shelves="$(_hg_cmd shelve -ql)"
643 local IFS=$'\n'
644 COMPREPLY=(${COMPREPLY[@]:-} $(compgen -W '$shelves' -- "$cur"))
645 }
646
647 _hg_cmd_shelve()
648 {
649 if [[ "$prev" = @(-d|--delete|-l|--list|-p|--patch|--stat) ]]; then
650 _hg_shelves
651 else
652 _hg_status "mard"
653 fi
654 }
655
656 _hg_cmd_unshelve()
657 {
658 _hg_shelves
659 }