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