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