1# bash completion for git-bug -*- shell-script -*-
2
3__git-bug_debug()
4{
5 if [[ -n ${BASH_COMP_DEBUG_FILE} ]]; then
6 echo "$*" >> "${BASH_COMP_DEBUG_FILE}"
7 fi
8}
9
10# Homebrew on Macs have version 1.3 of bash-completion which doesn't include
11# _init_completion. This is a very minimal version of that function.
12__git-bug_init_completion()
13{
14 COMPREPLY=()
15 _get_comp_words_by_ref "$@" cur prev words cword
16}
17
18__git-bug_index_of_word()
19{
20 local w word=$1
21 shift
22 index=0
23 for w in "$@"; do
24 [[ $w = "$word" ]] && return
25 index=$((index+1))
26 done
27 index=-1
28}
29
30__git-bug_contains_word()
31{
32 local w word=$1; shift
33 for w in "$@"; do
34 [[ $w = "$word" ]] && return
35 done
36 return 1
37}
38
39__git-bug_handle_reply()
40{
41 __git-bug_debug "${FUNCNAME[0]}"
42 case $cur in
43 -*)
44 if [[ $(type -t compopt) = "builtin" ]]; then
45 compopt -o nospace
46 fi
47 local allflags
48 if [ ${#must_have_one_flag[@]} -ne 0 ]; then
49 allflags=("${must_have_one_flag[@]}")
50 else
51 allflags=("${flags[*]} ${two_word_flags[*]}")
52 fi
53 COMPREPLY=( $(compgen -W "${allflags[*]}" -- "$cur") )
54 if [[ $(type -t compopt) = "builtin" ]]; then
55 [[ "${COMPREPLY[0]}" == *= ]] || compopt +o nospace
56 fi
57
58 # complete after --flag=abc
59 if [[ $cur == *=* ]]; then
60 if [[ $(type -t compopt) = "builtin" ]]; then
61 compopt +o nospace
62 fi
63
64 local index flag
65 flag="${cur%=*}"
66 __git-bug_index_of_word "${flag}" "${flags_with_completion[@]}"
67 COMPREPLY=()
68 if [[ ${index} -ge 0 ]]; then
69 PREFIX=""
70 cur="${cur#*=}"
71 ${flags_completion[${index}]}
72 if [ -n "${ZSH_VERSION}" ]; then
73 # zsh completion needs --flag= prefix
74 eval "COMPREPLY=( \"\${COMPREPLY[@]/#/${flag}=}\" )"
75 fi
76 fi
77 fi
78 return 0;
79 ;;
80 esac
81
82 # check if we are handling a flag with special work handling
83 local index
84 __git-bug_index_of_word "${prev}" "${flags_with_completion[@]}"
85 if [[ ${index} -ge 0 ]]; then
86 ${flags_completion[${index}]}
87 return
88 fi
89
90 # we are parsing a flag and don't have a special handler, no completion
91 if [[ ${cur} != "${words[cword]}" ]]; then
92 return
93 fi
94
95 local completions
96 completions=("${commands[@]}")
97 if [[ ${#must_have_one_noun[@]} -ne 0 ]]; then
98 completions=("${must_have_one_noun[@]}")
99 fi
100 if [[ ${#must_have_one_flag[@]} -ne 0 ]]; then
101 completions+=("${must_have_one_flag[@]}")
102 fi
103 COMPREPLY=( $(compgen -W "${completions[*]}" -- "$cur") )
104
105 if [[ ${#COMPREPLY[@]} -eq 0 && ${#noun_aliases[@]} -gt 0 && ${#must_have_one_noun[@]} -ne 0 ]]; then
106 COMPREPLY=( $(compgen -W "${noun_aliases[*]}" -- "$cur") )
107 fi
108
109 if [[ ${#COMPREPLY[@]} -eq 0 ]]; then
110 declare -F __custom_func >/dev/null && __custom_func
111 fi
112
113 # available in bash-completion >= 2, not always present on macOS
114 if declare -F __ltrim_colon_completions >/dev/null; then
115 __ltrim_colon_completions "$cur"
116 fi
117
118 # If there is only 1 completion and it is a flag with an = it will be completed
119 # but we don't want a space after the =
120 if [[ "${#COMPREPLY[@]}" -eq "1" ]] && [[ $(type -t compopt) = "builtin" ]] && [[ "${COMPREPLY[0]}" == --*= ]]; then
121 compopt -o nospace
122 fi
123}
124
125# The arguments should be in the form "ext1|ext2|extn"
126__git-bug_handle_filename_extension_flag()
127{
128 local ext="$1"
129 _filedir "@(${ext})"
130}
131
132__git-bug_handle_subdirs_in_dir_flag()
133{
134 local dir="$1"
135 pushd "${dir}" >/dev/null 2>&1 && _filedir -d && popd >/dev/null 2>&1
136}
137
138__git-bug_handle_flag()
139{
140 __git-bug_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
141
142 # if a command required a flag, and we found it, unset must_have_one_flag()
143 local flagname=${words[c]}
144 local flagvalue
145 # if the word contained an =
146 if [[ ${words[c]} == *"="* ]]; then
147 flagvalue=${flagname#*=} # take in as flagvalue after the =
148 flagname=${flagname%=*} # strip everything after the =
149 flagname="${flagname}=" # but put the = back
150 fi
151 __git-bug_debug "${FUNCNAME[0]}: looking for ${flagname}"
152 if __git-bug_contains_word "${flagname}" "${must_have_one_flag[@]}"; then
153 must_have_one_flag=()
154 fi
155
156 # if you set a flag which only applies to this command, don't show subcommands
157 if __git-bug_contains_word "${flagname}" "${local_nonpersistent_flags[@]}"; then
158 commands=()
159 fi
160
161 # keep flag value with flagname as flaghash
162 # flaghash variable is an associative array which is only supported in bash > 3.
163 if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
164 if [ -n "${flagvalue}" ] ; then
165 flaghash[${flagname}]=${flagvalue}
166 elif [ -n "${words[ $((c+1)) ]}" ] ; then
167 flaghash[${flagname}]=${words[ $((c+1)) ]}
168 else
169 flaghash[${flagname}]="true" # pad "true" for bool flag
170 fi
171 fi
172
173 # skip the argument to a two word flag
174 if __git-bug_contains_word "${words[c]}" "${two_word_flags[@]}"; then
175 c=$((c+1))
176 # if we are looking for a flags value, don't show commands
177 if [[ $c -eq $cword ]]; then
178 commands=()
179 fi
180 fi
181
182 c=$((c+1))
183
184}
185
186__git-bug_handle_noun()
187{
188 __git-bug_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
189
190 if __git-bug_contains_word "${words[c]}" "${must_have_one_noun[@]}"; then
191 must_have_one_noun=()
192 elif __git-bug_contains_word "${words[c]}" "${noun_aliases[@]}"; then
193 must_have_one_noun=()
194 fi
195
196 nouns+=("${words[c]}")
197 c=$((c+1))
198}
199
200__git-bug_handle_command()
201{
202 __git-bug_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
203
204 local next_command
205 if [[ -n ${last_command} ]]; then
206 next_command="_${last_command}_${words[c]//:/__}"
207 else
208 if [[ $c -eq 0 ]]; then
209 next_command="_git-bug_root_command"
210 else
211 next_command="_${words[c]//:/__}"
212 fi
213 fi
214 c=$((c+1))
215 __git-bug_debug "${FUNCNAME[0]}: looking for ${next_command}"
216 declare -F "$next_command" >/dev/null && $next_command
217}
218
219__git-bug_handle_word()
220{
221 if [[ $c -ge $cword ]]; then
222 __git-bug_handle_reply
223 return
224 fi
225 __git-bug_debug "${FUNCNAME[0]}: c is $c words[c] is ${words[c]}"
226 if [[ "${words[c]}" == -* ]]; then
227 __git-bug_handle_flag
228 elif __git-bug_contains_word "${words[c]}" "${commands[@]}"; then
229 __git-bug_handle_command
230 elif [[ $c -eq 0 ]]; then
231 __git-bug_handle_command
232 elif __git-bug_contains_word "${words[c]}" "${command_aliases[@]}"; then
233 # aliashash variable is an associative array which is only supported in bash > 3.
234 if [[ -z "${BASH_VERSION}" || "${BASH_VERSINFO[0]}" -gt 3 ]]; then
235 words[c]=${aliashash[${words[c]}]}
236 __git-bug_handle_command
237 else
238 __git-bug_handle_noun
239 fi
240 else
241 __git-bug_handle_noun
242 fi
243 __git-bug_handle_word
244}
245
246
247_git_bug() {
248 __start_git-bug "$@"
249}
250
251_git-bug_add()
252{
253 last_command="git-bug_add"
254
255 command_aliases=()
256
257 commands=()
258
259 flags=()
260 two_word_flags=()
261 local_nonpersistent_flags=()
262 flags_with_completion=()
263 flags_completion=()
264
265 flags+=("--title=")
266 two_word_flags+=("-t")
267 local_nonpersistent_flags+=("--title=")
268 flags+=("--message=")
269 two_word_flags+=("-m")
270 local_nonpersistent_flags+=("--message=")
271 flags+=("--file=")
272 two_word_flags+=("-F")
273 local_nonpersistent_flags+=("--file=")
274
275 must_have_one_flag=()
276 must_have_one_noun=()
277 noun_aliases=()
278}
279
280_git-bug_bridge_configure()
281{
282 last_command="git-bug_bridge_configure"
283
284 command_aliases=()
285
286 commands=()
287
288 flags=()
289 two_word_flags=()
290 local_nonpersistent_flags=()
291 flags_with_completion=()
292 flags_completion=()
293
294
295 must_have_one_flag=()
296 must_have_one_noun=()
297 noun_aliases=()
298}
299
300_git-bug_bridge_pull()
301{
302 last_command="git-bug_bridge_pull"
303
304 command_aliases=()
305
306 commands=()
307
308 flags=()
309 two_word_flags=()
310 local_nonpersistent_flags=()
311 flags_with_completion=()
312 flags_completion=()
313
314
315 must_have_one_flag=()
316 must_have_one_noun=()
317 noun_aliases=()
318}
319
320_git-bug_bridge_rm()
321{
322 last_command="git-bug_bridge_rm"
323
324 command_aliases=()
325
326 commands=()
327
328 flags=()
329 two_word_flags=()
330 local_nonpersistent_flags=()
331 flags_with_completion=()
332 flags_completion=()
333
334
335 must_have_one_flag=()
336 must_have_one_noun=()
337 noun_aliases=()
338}
339
340_git-bug_bridge()
341{
342 last_command="git-bug_bridge"
343
344 command_aliases=()
345
346 commands=()
347 commands+=("configure")
348 commands+=("pull")
349 commands+=("rm")
350
351 flags=()
352 two_word_flags=()
353 local_nonpersistent_flags=()
354 flags_with_completion=()
355 flags_completion=()
356
357
358 must_have_one_flag=()
359 must_have_one_noun=()
360 noun_aliases=()
361}
362
363_git-bug_commands()
364{
365 last_command="git-bug_commands"
366
367 command_aliases=()
368
369 commands=()
370
371 flags=()
372 two_word_flags=()
373 local_nonpersistent_flags=()
374 flags_with_completion=()
375 flags_completion=()
376
377 flags+=("--pretty")
378 flags+=("-p")
379 local_nonpersistent_flags+=("--pretty")
380
381 must_have_one_flag=()
382 must_have_one_noun=()
383 noun_aliases=()
384}
385
386_git-bug_comment_add()
387{
388 last_command="git-bug_comment_add"
389
390 command_aliases=()
391
392 commands=()
393
394 flags=()
395 two_word_flags=()
396 local_nonpersistent_flags=()
397 flags_with_completion=()
398 flags_completion=()
399
400 flags+=("--file=")
401 two_word_flags+=("-F")
402 local_nonpersistent_flags+=("--file=")
403 flags+=("--message=")
404 two_word_flags+=("-m")
405 local_nonpersistent_flags+=("--message=")
406
407 must_have_one_flag=()
408 must_have_one_noun=()
409 noun_aliases=()
410}
411
412_git-bug_comment()
413{
414 last_command="git-bug_comment"
415
416 command_aliases=()
417
418 commands=()
419 commands+=("add")
420
421 flags=()
422 two_word_flags=()
423 local_nonpersistent_flags=()
424 flags_with_completion=()
425 flags_completion=()
426
427
428 must_have_one_flag=()
429 must_have_one_noun=()
430 noun_aliases=()
431}
432
433_git-bug_deselect()
434{
435 last_command="git-bug_deselect"
436
437 command_aliases=()
438
439 commands=()
440
441 flags=()
442 two_word_flags=()
443 local_nonpersistent_flags=()
444 flags_with_completion=()
445 flags_completion=()
446
447
448 must_have_one_flag=()
449 must_have_one_noun=()
450 noun_aliases=()
451}
452
453_git-bug_label_add()
454{
455 last_command="git-bug_label_add"
456
457 command_aliases=()
458
459 commands=()
460
461 flags=()
462 two_word_flags=()
463 local_nonpersistent_flags=()
464 flags_with_completion=()
465 flags_completion=()
466
467
468 must_have_one_flag=()
469 must_have_one_noun=()
470 noun_aliases=()
471}
472
473_git-bug_label_rm()
474{
475 last_command="git-bug_label_rm"
476
477 command_aliases=()
478
479 commands=()
480
481 flags=()
482 two_word_flags=()
483 local_nonpersistent_flags=()
484 flags_with_completion=()
485 flags_completion=()
486
487
488 must_have_one_flag=()
489 must_have_one_noun=()
490 noun_aliases=()
491}
492
493_git-bug_label()
494{
495 last_command="git-bug_label"
496
497 command_aliases=()
498
499 commands=()
500 commands+=("add")
501 commands+=("rm")
502
503 flags=()
504 two_word_flags=()
505 local_nonpersistent_flags=()
506 flags_with_completion=()
507 flags_completion=()
508
509
510 must_have_one_flag=()
511 must_have_one_noun=()
512 noun_aliases=()
513}
514
515_git-bug_ls()
516{
517 last_command="git-bug_ls"
518
519 command_aliases=()
520
521 commands=()
522
523 flags=()
524 two_word_flags=()
525 local_nonpersistent_flags=()
526 flags_with_completion=()
527 flags_completion=()
528
529 flags+=("--status=")
530 two_word_flags+=("-s")
531 local_nonpersistent_flags+=("--status=")
532 flags+=("--author=")
533 two_word_flags+=("-a")
534 local_nonpersistent_flags+=("--author=")
535 flags+=("--label=")
536 two_word_flags+=("-l")
537 local_nonpersistent_flags+=("--label=")
538 flags+=("--title=")
539 two_word_flags+=("-t")
540 local_nonpersistent_flags+=("--title=")
541 flags+=("--no=")
542 two_word_flags+=("-n")
543 local_nonpersistent_flags+=("--no=")
544 flags+=("--by=")
545 two_word_flags+=("-b")
546 local_nonpersistent_flags+=("--by=")
547 flags+=("--direction=")
548 two_word_flags+=("-d")
549 local_nonpersistent_flags+=("--direction=")
550
551 must_have_one_flag=()
552 must_have_one_noun=()
553 noun_aliases=()
554}
555
556_git-bug_ls-id()
557{
558 last_command="git-bug_ls-id"
559
560 command_aliases=()
561
562 commands=()
563
564 flags=()
565 two_word_flags=()
566 local_nonpersistent_flags=()
567 flags_with_completion=()
568 flags_completion=()
569
570
571 must_have_one_flag=()
572 must_have_one_noun=()
573 noun_aliases=()
574}
575
576_git-bug_ls-label()
577{
578 last_command="git-bug_ls-label"
579
580 command_aliases=()
581
582 commands=()
583
584 flags=()
585 two_word_flags=()
586 local_nonpersistent_flags=()
587 flags_with_completion=()
588 flags_completion=()
589
590
591 must_have_one_flag=()
592 must_have_one_noun=()
593 noun_aliases=()
594}
595
596_git-bug_pull()
597{
598 last_command="git-bug_pull"
599
600 command_aliases=()
601
602 commands=()
603
604 flags=()
605 two_word_flags=()
606 local_nonpersistent_flags=()
607 flags_with_completion=()
608 flags_completion=()
609
610
611 must_have_one_flag=()
612 must_have_one_noun=()
613 noun_aliases=()
614}
615
616_git-bug_push()
617{
618 last_command="git-bug_push"
619
620 command_aliases=()
621
622 commands=()
623
624 flags=()
625 two_word_flags=()
626 local_nonpersistent_flags=()
627 flags_with_completion=()
628 flags_completion=()
629
630
631 must_have_one_flag=()
632 must_have_one_noun=()
633 noun_aliases=()
634}
635
636_git-bug_select()
637{
638 last_command="git-bug_select"
639
640 command_aliases=()
641
642 commands=()
643
644 flags=()
645 two_word_flags=()
646 local_nonpersistent_flags=()
647 flags_with_completion=()
648 flags_completion=()
649
650
651 must_have_one_flag=()
652 must_have_one_noun=()
653 noun_aliases=()
654}
655
656_git-bug_show()
657{
658 last_command="git-bug_show"
659
660 command_aliases=()
661
662 commands=()
663
664 flags=()
665 two_word_flags=()
666 local_nonpersistent_flags=()
667 flags_with_completion=()
668 flags_completion=()
669
670 flags+=("--field=")
671 two_word_flags+=("-f")
672 local_nonpersistent_flags+=("--field=")
673
674 must_have_one_flag=()
675 must_have_one_noun=()
676 noun_aliases=()
677}
678
679_git-bug_status_close()
680{
681 last_command="git-bug_status_close"
682
683 command_aliases=()
684
685 commands=()
686
687 flags=()
688 two_word_flags=()
689 local_nonpersistent_flags=()
690 flags_with_completion=()
691 flags_completion=()
692
693
694 must_have_one_flag=()
695 must_have_one_noun=()
696 noun_aliases=()
697}
698
699_git-bug_status_open()
700{
701 last_command="git-bug_status_open"
702
703 command_aliases=()
704
705 commands=()
706
707 flags=()
708 two_word_flags=()
709 local_nonpersistent_flags=()
710 flags_with_completion=()
711 flags_completion=()
712
713
714 must_have_one_flag=()
715 must_have_one_noun=()
716 noun_aliases=()
717}
718
719_git-bug_status()
720{
721 last_command="git-bug_status"
722
723 command_aliases=()
724
725 commands=()
726 commands+=("close")
727 commands+=("open")
728
729 flags=()
730 two_word_flags=()
731 local_nonpersistent_flags=()
732 flags_with_completion=()
733 flags_completion=()
734
735
736 must_have_one_flag=()
737 must_have_one_noun=()
738 noun_aliases=()
739}
740
741_git-bug_termui()
742{
743 last_command="git-bug_termui"
744
745 command_aliases=()
746
747 commands=()
748
749 flags=()
750 two_word_flags=()
751 local_nonpersistent_flags=()
752 flags_with_completion=()
753 flags_completion=()
754
755
756 must_have_one_flag=()
757 must_have_one_noun=()
758 noun_aliases=()
759}
760
761_git-bug_title_edit()
762{
763 last_command="git-bug_title_edit"
764
765 command_aliases=()
766
767 commands=()
768
769 flags=()
770 two_word_flags=()
771 local_nonpersistent_flags=()
772 flags_with_completion=()
773 flags_completion=()
774
775 flags+=("--title=")
776 two_word_flags+=("-t")
777 local_nonpersistent_flags+=("--title=")
778
779 must_have_one_flag=()
780 must_have_one_noun=()
781 noun_aliases=()
782}
783
784_git-bug_title()
785{
786 last_command="git-bug_title"
787
788 command_aliases=()
789
790 commands=()
791 commands+=("edit")
792
793 flags=()
794 two_word_flags=()
795 local_nonpersistent_flags=()
796 flags_with_completion=()
797 flags_completion=()
798
799
800 must_have_one_flag=()
801 must_have_one_noun=()
802 noun_aliases=()
803}
804
805_git-bug_user_adopt()
806{
807 last_command="git-bug_user_adopt"
808
809 command_aliases=()
810
811 commands=()
812
813 flags=()
814 two_word_flags=()
815 local_nonpersistent_flags=()
816 flags_with_completion=()
817 flags_completion=()
818
819
820 must_have_one_flag=()
821 must_have_one_noun=()
822 noun_aliases=()
823}
824
825_git-bug_user_create()
826{
827 last_command="git-bug_user_create"
828
829 command_aliases=()
830
831 commands=()
832
833 flags=()
834 two_word_flags=()
835 local_nonpersistent_flags=()
836 flags_with_completion=()
837 flags_completion=()
838
839
840 must_have_one_flag=()
841 must_have_one_noun=()
842 noun_aliases=()
843}
844
845_git-bug_user_ls()
846{
847 last_command="git-bug_user_ls"
848
849 command_aliases=()
850
851 commands=()
852
853 flags=()
854 two_word_flags=()
855 local_nonpersistent_flags=()
856 flags_with_completion=()
857 flags_completion=()
858
859
860 must_have_one_flag=()
861 must_have_one_noun=()
862 noun_aliases=()
863}
864
865_git-bug_user()
866{
867 last_command="git-bug_user"
868
869 command_aliases=()
870
871 commands=()
872 commands+=("adopt")
873 commands+=("create")
874 commands+=("ls")
875
876 flags=()
877 two_word_flags=()
878 local_nonpersistent_flags=()
879 flags_with_completion=()
880 flags_completion=()
881
882
883 must_have_one_flag=()
884 must_have_one_noun=()
885 noun_aliases=()
886}
887
888_git-bug_version()
889{
890 last_command="git-bug_version"
891
892 command_aliases=()
893
894 commands=()
895
896 flags=()
897 two_word_flags=()
898 local_nonpersistent_flags=()
899 flags_with_completion=()
900 flags_completion=()
901
902 flags+=("--number")
903 flags+=("-n")
904 local_nonpersistent_flags+=("--number")
905 flags+=("--commit")
906 flags+=("-c")
907 local_nonpersistent_flags+=("--commit")
908 flags+=("--all")
909 flags+=("-a")
910 local_nonpersistent_flags+=("--all")
911
912 must_have_one_flag=()
913 must_have_one_noun=()
914 noun_aliases=()
915}
916
917_git-bug_webui()
918{
919 last_command="git-bug_webui"
920
921 command_aliases=()
922
923 commands=()
924
925 flags=()
926 two_word_flags=()
927 local_nonpersistent_flags=()
928 flags_with_completion=()
929 flags_completion=()
930
931 flags+=("--port=")
932 two_word_flags+=("-p")
933 local_nonpersistent_flags+=("--port=")
934
935 must_have_one_flag=()
936 must_have_one_noun=()
937 noun_aliases=()
938}
939
940_git-bug_root_command()
941{
942 last_command="git-bug"
943
944 command_aliases=()
945
946 commands=()
947 commands+=("add")
948 commands+=("bridge")
949 commands+=("commands")
950 commands+=("comment")
951 commands+=("deselect")
952 commands+=("label")
953 commands+=("ls")
954 commands+=("ls-id")
955 commands+=("ls-label")
956 commands+=("pull")
957 commands+=("push")
958 commands+=("select")
959 commands+=("show")
960 commands+=("status")
961 commands+=("termui")
962 commands+=("title")
963 commands+=("user")
964 commands+=("version")
965 commands+=("webui")
966
967 flags=()
968 two_word_flags=()
969 local_nonpersistent_flags=()
970 flags_with_completion=()
971 flags_completion=()
972
973
974 must_have_one_flag=()
975 must_have_one_noun=()
976 noun_aliases=()
977}
978
979__start_git-bug()
980{
981 local cur prev words cword
982 declare -A flaghash 2>/dev/null || :
983 declare -A aliashash 2>/dev/null || :
984 if declare -F _init_completion >/dev/null 2>&1; then
985 _init_completion -s || return
986 else
987 __git-bug_init_completion -n "=" || return
988 fi
989
990 local c=0
991 local flags=()
992 local two_word_flags=()
993 local local_nonpersistent_flags=()
994 local flags_with_completion=()
995 local flags_completion=()
996 local commands=("git-bug")
997 local must_have_one_flag=()
998 local must_have_one_noun=()
999 local last_command
1000 local nouns=()
1001
1002 __git-bug_handle_word
1003}
1004
1005if [[ $(type -t compopt) = "builtin" ]]; then
1006 complete -o default -F __start_git-bug git-bug
1007else
1008 complete -o default -o nospace -F __start_git-bug git-bug
1009fi
1010
1011# ex: ts=4 sw=4 et filetype=sh