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_commands()
281{
282 last_command="git-bug_commands"
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 flags+=("--pretty")
295 flags+=("-p")
296 local_nonpersistent_flags+=("--pretty")
297
298 must_have_one_flag=()
299 must_have_one_noun=()
300 noun_aliases=()
301}
302
303_git-bug_comment_add()
304{
305 last_command="git-bug_comment_add"
306
307 command_aliases=()
308
309 commands=()
310
311 flags=()
312 two_word_flags=()
313 local_nonpersistent_flags=()
314 flags_with_completion=()
315 flags_completion=()
316
317
318 must_have_one_flag=()
319 must_have_one_noun=()
320 noun_aliases=()
321}
322
323_git-bug_comment()
324{
325 last_command="git-bug_comment"
326
327 command_aliases=()
328
329 commands=()
330 commands+=("add")
331
332 flags=()
333 two_word_flags=()
334 local_nonpersistent_flags=()
335 flags_with_completion=()
336 flags_completion=()
337
338 flags+=("--file=")
339 two_word_flags+=("-F")
340 local_nonpersistent_flags+=("--file=")
341 flags+=("--message=")
342 two_word_flags+=("-m")
343 local_nonpersistent_flags+=("--message=")
344
345 must_have_one_flag=()
346 must_have_one_noun=()
347 noun_aliases=()
348}
349
350_git-bug_label_add()
351{
352 last_command="git-bug_label_add"
353
354 command_aliases=()
355
356 commands=()
357
358 flags=()
359 two_word_flags=()
360 local_nonpersistent_flags=()
361 flags_with_completion=()
362 flags_completion=()
363
364
365 must_have_one_flag=()
366 must_have_one_noun=()
367 noun_aliases=()
368}
369
370_git-bug_label_rm()
371{
372 last_command="git-bug_label_rm"
373
374 command_aliases=()
375
376 commands=()
377
378 flags=()
379 two_word_flags=()
380 local_nonpersistent_flags=()
381 flags_with_completion=()
382 flags_completion=()
383
384
385 must_have_one_flag=()
386 must_have_one_noun=()
387 noun_aliases=()
388}
389
390_git-bug_label()
391{
392 last_command="git-bug_label"
393
394 command_aliases=()
395
396 commands=()
397 commands+=("add")
398 commands+=("rm")
399
400 flags=()
401 two_word_flags=()
402 local_nonpersistent_flags=()
403 flags_with_completion=()
404 flags_completion=()
405
406
407 must_have_one_flag=()
408 must_have_one_noun=()
409 noun_aliases=()
410}
411
412_git-bug_ls()
413{
414 last_command="git-bug_ls"
415
416 command_aliases=()
417
418 commands=()
419
420 flags=()
421 two_word_flags=()
422 local_nonpersistent_flags=()
423 flags_with_completion=()
424 flags_completion=()
425
426 flags+=("--status=")
427 two_word_flags+=("-s")
428 local_nonpersistent_flags+=("--status=")
429 flags+=("--author=")
430 two_word_flags+=("-a")
431 local_nonpersistent_flags+=("--author=")
432 flags+=("--label=")
433 two_word_flags+=("-l")
434 local_nonpersistent_flags+=("--label=")
435 flags+=("--no=")
436 two_word_flags+=("-n")
437 local_nonpersistent_flags+=("--no=")
438 flags+=("--by=")
439 two_word_flags+=("-b")
440 local_nonpersistent_flags+=("--by=")
441 flags+=("--direction=")
442 two_word_flags+=("-d")
443 local_nonpersistent_flags+=("--direction=")
444
445 must_have_one_flag=()
446 must_have_one_noun=()
447 noun_aliases=()
448}
449
450_git-bug_ls-label()
451{
452 last_command="git-bug_ls-label"
453
454 command_aliases=()
455
456 commands=()
457
458 flags=()
459 two_word_flags=()
460 local_nonpersistent_flags=()
461 flags_with_completion=()
462 flags_completion=()
463
464
465 must_have_one_flag=()
466 must_have_one_noun=()
467 noun_aliases=()
468}
469
470_git-bug_pull()
471{
472 last_command="git-bug_pull"
473
474 command_aliases=()
475
476 commands=()
477
478 flags=()
479 two_word_flags=()
480 local_nonpersistent_flags=()
481 flags_with_completion=()
482 flags_completion=()
483
484
485 must_have_one_flag=()
486 must_have_one_noun=()
487 noun_aliases=()
488}
489
490_git-bug_push()
491{
492 last_command="git-bug_push"
493
494 command_aliases=()
495
496 commands=()
497
498 flags=()
499 two_word_flags=()
500 local_nonpersistent_flags=()
501 flags_with_completion=()
502 flags_completion=()
503
504
505 must_have_one_flag=()
506 must_have_one_noun=()
507 noun_aliases=()
508}
509
510_git-bug_select()
511{
512 last_command="git-bug_select"
513
514 command_aliases=()
515
516 commands=()
517
518 flags=()
519 two_word_flags=()
520 local_nonpersistent_flags=()
521 flags_with_completion=()
522 flags_completion=()
523
524
525 must_have_one_flag=()
526 must_have_one_noun=()
527 noun_aliases=()
528}
529
530_git-bug_show()
531{
532 last_command="git-bug_show"
533
534 command_aliases=()
535
536 commands=()
537
538 flags=()
539 two_word_flags=()
540 local_nonpersistent_flags=()
541 flags_with_completion=()
542 flags_completion=()
543
544
545 must_have_one_flag=()
546 must_have_one_noun=()
547 noun_aliases=()
548}
549
550_git-bug_status_close()
551{
552 last_command="git-bug_status_close"
553
554 command_aliases=()
555
556 commands=()
557
558 flags=()
559 two_word_flags=()
560 local_nonpersistent_flags=()
561 flags_with_completion=()
562 flags_completion=()
563
564
565 must_have_one_flag=()
566 must_have_one_noun=()
567 noun_aliases=()
568}
569
570_git-bug_status_open()
571{
572 last_command="git-bug_status_open"
573
574 command_aliases=()
575
576 commands=()
577
578 flags=()
579 two_word_flags=()
580 local_nonpersistent_flags=()
581 flags_with_completion=()
582 flags_completion=()
583
584
585 must_have_one_flag=()
586 must_have_one_noun=()
587 noun_aliases=()
588}
589
590_git-bug_status()
591{
592 last_command="git-bug_status"
593
594 command_aliases=()
595
596 commands=()
597 commands+=("close")
598 commands+=("open")
599
600 flags=()
601 two_word_flags=()
602 local_nonpersistent_flags=()
603 flags_with_completion=()
604 flags_completion=()
605
606
607 must_have_one_flag=()
608 must_have_one_noun=()
609 noun_aliases=()
610}
611
612_git-bug_termui()
613{
614 last_command="git-bug_termui"
615
616 command_aliases=()
617
618 commands=()
619
620 flags=()
621 two_word_flags=()
622 local_nonpersistent_flags=()
623 flags_with_completion=()
624 flags_completion=()
625
626
627 must_have_one_flag=()
628 must_have_one_noun=()
629 noun_aliases=()
630}
631
632_git-bug_title_edit()
633{
634 last_command="git-bug_title_edit"
635
636 command_aliases=()
637
638 commands=()
639
640 flags=()
641 two_word_flags=()
642 local_nonpersistent_flags=()
643 flags_with_completion=()
644 flags_completion=()
645
646 flags+=("--title=")
647 two_word_flags+=("-t")
648 local_nonpersistent_flags+=("--title=")
649
650 must_have_one_flag=()
651 must_have_one_noun=()
652 noun_aliases=()
653}
654
655_git-bug_title()
656{
657 last_command="git-bug_title"
658
659 command_aliases=()
660
661 commands=()
662 commands+=("edit")
663
664 flags=()
665 two_word_flags=()
666 local_nonpersistent_flags=()
667 flags_with_completion=()
668 flags_completion=()
669
670
671 must_have_one_flag=()
672 must_have_one_noun=()
673 noun_aliases=()
674}
675
676_git-bug_webui()
677{
678 last_command="git-bug_webui"
679
680 command_aliases=()
681
682 commands=()
683
684 flags=()
685 two_word_flags=()
686 local_nonpersistent_flags=()
687 flags_with_completion=()
688 flags_completion=()
689
690 flags+=("--port=")
691 two_word_flags+=("-p")
692 local_nonpersistent_flags+=("--port=")
693
694 must_have_one_flag=()
695 must_have_one_noun=()
696 noun_aliases=()
697}
698
699_git-bug_root_command()
700{
701 last_command="git-bug"
702
703 command_aliases=()
704
705 commands=()
706 commands+=("add")
707 commands+=("commands")
708 commands+=("comment")
709 commands+=("label")
710 commands+=("ls")
711 commands+=("ls-label")
712 commands+=("pull")
713 commands+=("push")
714 commands+=("select")
715 commands+=("show")
716 commands+=("status")
717 commands+=("termui")
718 commands+=("title")
719 commands+=("webui")
720
721 flags=()
722 two_word_flags=()
723 local_nonpersistent_flags=()
724 flags_with_completion=()
725 flags_completion=()
726
727
728 must_have_one_flag=()
729 must_have_one_noun=()
730 noun_aliases=()
731}
732
733__start_git-bug()
734{
735 local cur prev words cword
736 declare -A flaghash 2>/dev/null || :
737 declare -A aliashash 2>/dev/null || :
738 if declare -F _init_completion >/dev/null 2>&1; then
739 _init_completion -s || return
740 else
741 __git-bug_init_completion -n "=" || return
742 fi
743
744 local c=0
745 local flags=()
746 local two_word_flags=()
747 local local_nonpersistent_flags=()
748 local flags_with_completion=()
749 local flags_completion=()
750 local commands=("git-bug")
751 local must_have_one_flag=()
752 local must_have_one_noun=()
753 local last_command
754 local nouns=()
755
756 __git-bug_handle_word
757}
758
759if [[ $(type -t compopt) = "builtin" ]]; then
760 complete -o default -F __start_git-bug git-bug
761else
762 complete -o default -o nospace -F __start_git-bug git-bug
763fi
764
765# ex: ts=4 sw=4 et filetype=sh