bash: Support unique completion when possible.
Because our use of -o nospace prevents bash from adding a trailing space when a completion is unique and has been fully completed, we need to perform this addition on our own. This (large) change converts all existing uses of compgen to our wrapper __gitcomp which attempts to handle this by tacking a trailing space onto the end of each offered option. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Feb 4, 2007 at 02:38 UTC
b3391775e87bed073b93a0a534169a794eceebd7
1 file changed
+91
-99
contrib/completion/git-completion.bash
+91
-99
@@ -65,7 +65,7 @@ __gitcomp ()
65
{
66
local all c s=$'\n' IFS=' '$'\t'$'\n'
67
local cur="${COMP_WORDS[COMP_CWORD]}"
68
- if [ -n "$2" ]; then
68
+ if [ $# -gt 2 ]; then
69
cur="$3"
70
fi
71
for c in $1; do
@@ -219,7 +219,7 @@ __git_complete_file ()
219
-- "$cur"))
220
;;
221
*)
222
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
222
+ __gitcomp "$(__git_refs)"
223
;;
224
esac
225
}
@@ -231,15 +231,18 @@ __git_complete_revlist ()
231
*...*)
232
pfx="${cur%...*}..."
233
cur="${cur#*...}"
234
- COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
234
+ __gitcomp "$(__git_refs)" "$pfx" "$cur"
235
;;
236
*..*)
237
pfx="${cur%..*}.."
238
cur="${cur#*..}"
239
- COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
239
+ __gitcomp "$(__git_refs)" "$pfx" "$cur"
240
+ ;;
241
+ *.)
242
+ __gitcomp "$cur."
243
;;
244
*)
242
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
245
+ __gitcomp "$(__git_refs)"
246
;;
247
esac
248
}
@@ -353,22 +356,19 @@ _git_am ()
356
{
357
local cur="${COMP_WORDS[COMP_CWORD]}"
358
if [ -d .dotest ]; then
356
- COMPREPLY=($(compgen -W "
357
- --skip --resolved
358
- " -- "$cur"))
359
+ __gitcomp "--skip --resolved"
360
return
361
fi
362
case "$cur" in
363
--whitespace=*)
363
- COMPREPLY=($(compgen -W "$__git_whitespacelist" \
364
- -- "${cur##--whitespace=}"))
364
+ __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
365
return
366
;;
367
--*)
368
- COMPREPLY=($(compgen -W "
368
+ __gitcomp "
369
--signoff --utf8 --binary --3way --interactive
370
--whitespace=
371
- " -- "$cur"))
371
+ "
372
return
373
esac
374
COMPREPLY=()
@@ -379,17 +379,16 @@ _git_apply ()
379
local cur="${COMP_WORDS[COMP_CWORD]}"
380
case "$cur" in
381
--whitespace=*)
382
- COMPREPLY=($(compgen -W "$__git_whitespacelist" \
383
- -- "${cur##--whitespace=}"))
382
+ __gitcomp "$__git_whitespacelist" "" "${cur##--whitespace=}"
383
return
384
;;
385
--*)
387
- COMPREPLY=($(compgen -W "
386
+ __gitcomp "
387
--stat --numstat --summary --check --index
388
--cached --index-info --reverse --reject --unidiff-zero
389
--apply --no-add --exclude=
390
--whitespace= --inaccurate-eof --verbose
392
- " -- "$cur"))
391
+ "
392
return
393
esac
394
COMPREPLY=()
@@ -400,9 +399,7 @@ _git_add ()
399
local cur="${COMP_WORDS[COMP_CWORD]}"
400
case "$cur" in
401
--*)
403
- COMPREPLY=($(compgen -W "
404
- --interactive
405
- " -- "$cur"))
402
+ __gitcomp "--interactive"
403
return
404
esac
405
COMPREPLY=()
@@ -410,14 +407,12 @@ _git_add ()
407
408
_git_branch ()
409
{
413
- local cur="${COMP_WORDS[COMP_CWORD]}"
414
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
410
+ __gitcomp "$(__git_refs)"
411
}
412
413
_git_checkout ()
414
{
419
- local cur="${COMP_WORDS[COMP_CWORD]}"
420
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
415
+ __gitcomp "$(__git_refs)"
416
}
417
418
_git_cherry_pick ()
@@ -425,12 +420,10 @@ _git_cherry_pick ()
420
local cur="${COMP_WORDS[COMP_CWORD]}"
421
case "$cur" in
422
--*)
428
- COMPREPLY=($(compgen -W "
429
- --edit --no-commit
430
- " -- "$cur"))
423
+ __gitcomp "--edit --no-commit"
424
;;
425
*)
433
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
426
+ __gitcomp "$(__git_refs)"
427
;;
428
esac
429
}
@@ -440,10 +433,10 @@ _git_commit ()
433
local cur="${COMP_WORDS[COMP_CWORD]}"
434
case "$cur" in
435
--*)
443
- COMPREPLY=($(compgen -W "
436
+ __gitcomp "
437
--all --author= --signoff --verify --no-verify
438
--edit --amend --include --only
446
- " -- "$cur"))
439
+ "
440
return
441
esac
442
COMPREPLY=()
@@ -456,8 +449,7 @@ _git_diff ()
449
450
_git_diff_tree ()
451
{
459
- local cur="${COMP_WORDS[COMP_CWORD]}"
460
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
452
+ __gitcomp "$(__git_refs)"
453
}
454
455
_git_fetch ()
@@ -466,16 +458,15 @@ _git_fetch ()
458
459
case "${COMP_WORDS[0]},$COMP_CWORD" in
460
git-fetch*,1)
469
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
461
+ __gitcomp "$(__git_remotes)"
462
;;
463
git,2)
472
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
464
+ __gitcomp "$(__git_remotes)"
465
;;
466
*)
467
case "$cur" in
468
*:*)
477
- cur="${cur#*:}"
478
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
469
+ __gitcomp "$(__git_refs)" "" "${cur#*:}"
470
;;
471
*)
472
local remote
@@ -483,7 +474,7 @@ _git_fetch ()
474
git-fetch) remote="${COMP_WORDS[1]}" ;;
475
git) remote="${COMP_WORDS[2]}" ;;
476
esac
486
- COMPREPLY=($(compgen -W "$(__git_refs2 "$remote")" -- "$cur"))
477
+ __gitcomp "$(__git_refs2 "$remote")"
478
;;
479
esac
480
;;
@@ -495,7 +486,7 @@ _git_format_patch ()
486
local cur="${COMP_WORDS[COMP_CWORD]}"
487
case "$cur" in
488
--*)
498
- COMPREPLY=($(compgen -W "
489
+ __gitcomp "
490
--stdout --attach --thread
491
--output-directory
492
--numbered --start-number
@@ -503,7 +494,7 @@ _git_format_patch ()
494
--signoff
495
--in-reply-to=
496
--full-index --binary
506
- " -- "$cur"))
497
+ "
498
return
499
;;
500
esac
@@ -512,8 +503,7 @@ _git_format_patch ()
503
504
_git_ls_remote ()
505
{
515
- local cur="${COMP_WORDS[COMP_CWORD]}"
516
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
506
+ __gitcomp "$(__git_remotes)"
507
}
508
509
_git_ls_tree ()
@@ -526,13 +516,13 @@ _git_log ()
516
local cur="${COMP_WORDS[COMP_CWORD]}"
517
case "$cur" in
518
--pretty=*)
529
- COMPREPLY=($(compgen -W "
519
+ __gitcomp "
520
oneline short medium full fuller email raw
531
- " -- "${cur##--pretty=}"))
521
+ " "" "${cur##--pretty=}"
522
return
523
;;
524
--*)
535
- COMPREPLY=($(compgen -W "
525
+ __gitcomp "
526
--max-count= --max-age= --since= --after=
527
--min-age= --before= --until=
528
--root --not --topo-order --date-order
@@ -542,7 +532,7 @@ _git_log ()
532
--author= --committer= --grep=
533
--all-match
534
--pretty= --name-status --name-only
545
- " -- "$cur"))
535
+ "
536
return
537
;;
538
esac
@@ -554,34 +544,31 @@ _git_merge ()
544
local cur="${COMP_WORDS[COMP_CWORD]}"
545
case "${COMP_WORDS[COMP_CWORD-1]}" in
546
-s|--strategy)
557
- COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
547
+ __gitcomp "$(__git_merge_strategies)"
548
return
549
esac
550
case "$cur" in
551
--strategy=*)
562
- COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
563
- -- "${cur##--strategy=}"))
552
+ __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
553
return
554
;;
555
--*)
567
- COMPREPLY=($(compgen -W "
556
+ __gitcomp "
557
--no-commit --no-summary --squash --strategy
569
- " -- "$cur"))
558
+ "
559
return
560
esac
572
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
561
+ __gitcomp "$(__git_refs)"
562
}
563
564
_git_merge_base ()
565
{
577
- local cur="${COMP_WORDS[COMP_CWORD]}"
578
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
566
+ __gitcomp "$(__git_refs)"
567
}
568
569
_git_name_rev ()
570
{
583
- local cur="${COMP_WORDS[COMP_CWORD]}"
584
- COMPREPLY=($(compgen -W "--tags --all --stdin" -- "$cur"))
571
+ __gitcomp "--tags --all --stdin"
572
}
573
574
_git_pull ()
@@ -590,10 +577,10 @@ _git_pull ()
577
578
case "${COMP_WORDS[0]},$COMP_CWORD" in
579
git-pull*,1)
593
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
580
+ __gitcomp "$(__git_remotes)"
581
;;
582
git,2)
596
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
583
+ __gitcomp "$(__git_remotes)"
584
;;
585
*)
586
local remote
@@ -601,7 +588,7 @@ _git_pull ()
588
git-pull) remote="${COMP_WORDS[1]}" ;;
589
git) remote="${COMP_WORDS[2]}" ;;
590
esac
604
- COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
591
+ __gitcomp "$(__git_refs "$remote")"
592
;;
593
esac
594
}
@@ -612,10 +599,10 @@ _git_push ()
599
600
case "${COMP_WORDS[0]},$COMP_CWORD" in
601
git-push*,1)
615
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
602
+ __gitcomp "$(__git_remotes)"
603
;;
604
git,2)
618
- COMPREPLY=($(compgen -W "$(__git_remotes)" -- "$cur"))
605
+ __gitcomp "$(__git_remotes)"
606
;;
607
*)
608
case "$cur" in
@@ -625,11 +612,10 @@ _git_push ()
612
git-push) remote="${COMP_WORDS[1]}" ;;
613
git) remote="${COMP_WORDS[2]}" ;;
614
esac
628
- cur="${cur#*:}"
629
- COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
615
+ __gitcomp "$(__git_refs "$remote")" "" "${cur#*:}"
616
;;
617
*)
632
- COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
618
+ __gitcomp "$(__git_refs2)"
619
;;
620
esac
621
;;
@@ -640,29 +626,24 @@ _git_rebase ()
626
{
627
local cur="${COMP_WORDS[COMP_CWORD]}"
628
if [ -d .dotest ]; then
643
- COMPREPLY=($(compgen -W "
644
- --continue --skip --abort
645
- " -- "$cur"))
629
+ __gitcomp "--continue --skip --abort"
630
return
631
fi
632
case "${COMP_WORDS[COMP_CWORD-1]}" in
633
-s|--strategy)
650
- COMPREPLY=($(compgen -W "$(__git_merge_strategies)" -- "$cur"))
634
+ __gitcomp "$(__git_merge_strategies)"
635
return
636
esac
637
case "$cur" in
638
--strategy=*)
655
- COMPREPLY=($(compgen -W "$(__git_merge_strategies)" \
656
- -- "${cur##--strategy=}"))
639
+ __gitcomp "$(__git_merge_strategies)" "" "${cur##--strategy=}"
640
return
641
;;
642
--*)
660
- COMPREPLY=($(compgen -W "
661
- --onto --merge --strategy
662
- " -- "$cur"))
643
+ __gitcomp "--onto --merge --strategy"
644
return
645
esac
665
- COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
646
+ __gitcomp "$(__git_refs)"
647
}
648
649
_git_config ()
@@ -824,8 +805,13 @@ _git_config ()
805
_git_reset ()
806
{
807
local cur="${COMP_WORDS[COMP_CWORD]}"
827
- local opt="--mixed --hard --soft"
828
- COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
808
+ case "$cur" in
809
+ --*)
810
+ __gitcomp "--mixed --hard --soft"
811
+ return
812
+ ;;
813
+ esac
814
+ __gitcomp "$(__git_refs)"
815
}
816
817
_git_show ()
@@ -833,13 +819,13 @@ _git_show ()
819
local cur="${COMP_WORDS[COMP_CWORD]}"
820
case "$cur" in
821
--pretty=*)
836
- COMPREPLY=($(compgen -W "
822
+ __gitcomp "
823
oneline short medium full fuller email raw
838
- " -- "${cur##--pretty=}"))
824
+ " "" "${cur##--pretty=}"
825
return
826
;;
827
--*)
842
- COMPREPLY=($(compgen -W "--pretty=" -- "$cur"))
828
+ __gitcomp "--pretty="
829
return
830
;;
831
esac
@@ -906,32 +892,38 @@ _git ()
892
_gitk ()
893
{
894
local cur="${COMP_WORDS[COMP_CWORD]}"
909
- COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
895
+ case "$cur" in
896
+ --*)
897
+ __gitcomp "--not --all"
898
+ return
899
+ ;;
900
+ esac
901
+ __gitcomp "$(__git_refs)"
902
}
903
904
complete -o default -o nospace -F _git git
913
-complete -o default -F _gitk gitk
914
-complete -o default -F _git_am git-am
915
-complete -o default -F _git_apply git-apply
916
-complete -o default -F _git_branch git-branch
917
-complete -o default -F _git_checkout git-checkout
918
-complete -o default -F _git_cherry_pick git-cherry-pick
919
-complete -o default -F _git_commit git-commit
905
+complete -o default -o nospace -F _gitk gitk
906
+complete -o default -o nospace -F _git_am git-am
907
+complete -o default -o nospace -F _git_apply git-apply
908
+complete -o default -o nospace -F _git_branch git-branch
909
+complete -o default -o nospace -F _git_checkout git-checkout
910
+complete -o default -o nospace -F _git_cherry_pick git-cherry-pick
911
+complete -o default -o nospace -F _git_commit git-commit
912
complete -o default -o nospace -F _git_diff git-diff
921
-complete -o default -F _git_diff_tree git-diff-tree
913
+complete -o default -o nospace -F _git_diff_tree git-diff-tree
914
complete -o default -o nospace -F _git_fetch git-fetch
915
complete -o default -o nospace -F _git_format_patch git-format-patch
916
complete -o default -o nospace -F _git_log git-log
925
-complete -o default -F _git_ls_remote git-ls-remote
917
+complete -o default -o nospace -F _git_ls_remote git-ls-remote
918
complete -o default -o nospace -F _git_ls_tree git-ls-tree
927
-complete -o default -F _git_merge git-merge
928
-complete -o default -F _git_merge_base git-merge-base
929
-complete -o default -F _git_name_rev git-name-rev
919
+complete -o default -o nospace -F _git_merge git-merge
920
+complete -o default -o nospace -F _git_merge_base git-merge-base
921
+complete -o default -o nospace -F _git_name_rev git-name-rev
922
complete -o default -o nospace -F _git_pull git-pull
923
complete -o default -o nospace -F _git_push git-push
932
-complete -o default -F _git_rebase git-rebase
933
-complete -o default -F _git_config git-config
934
-complete -o default -F _git_reset git-reset
924
+complete -o default -o nospace -F _git_rebase git-rebase
925
+complete -o default -o nospace -F _git_config git-config
926
+complete -o default -o nospace -F _git_reset git-reset
927
complete -o default -o nospace -F _git_show git-show
928
complete -o default -o nospace -F _git_log git-show-branch
929
complete -o default -o nospace -F _git_log git-whatchanged
@@ -941,19 +933,19 @@ complete -o default -o nospace -F _git_log git-whatchanged
933
# included the '.exe' suffix.
934
#
935
if [ Cygwin = "$(uname -o 2>/dev/null)" ]; then
944
-complete -o default -F _git_add git-add.exe
945
-complete -o default -F _git_apply git-apply.exe
936
+complete -o default -o nospace -F _git_add git-add.exe
937
+complete -o default -o nospace -F _git_apply git-apply.exe
938
complete -o default -o nospace -F _git git.exe
947
-complete -o default -F _git_branch git-branch.exe
939
+complete -o default -o nospace -F _git_branch git-branch.exe
940
complete -o default -o nospace -F _git_diff git-diff.exe
941
complete -o default -o nospace -F _git_diff_tree git-diff-tree.exe
942
complete -o default -o nospace -F _git_format_patch git-format-patch.exe
943
complete -o default -o nospace -F _git_log git-log.exe
944
complete -o default -o nospace -F _git_ls_tree git-ls-tree.exe
953
-complete -o default -F _git_merge_base git-merge-base.exe
954
-complete -o default -F _git_name_rev git-name-rev.exe
945
+complete -o default -o nospace -F _git_merge_base git-merge-base.exe
946
+complete -o default -o nospace -F _git_name_rev git-name-rev.exe
947
complete -o default -o nospace -F _git_push git-push.exe
956
-complete -o default -F _git_config git-config
948
+complete -o default -o nospace -F _git_config git-config
949
complete -o default -o nospace -F _git_show git-show.exe
950
complete -o default -o nospace -F _git_log git-show-branch.exe
951
complete -o default -o nospace -F _git_log git-whatchanged.exe