git-bug

  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+=("--no=")
539    two_word_flags+=("-n")
540    local_nonpersistent_flags+=("--no=")
541    flags+=("--by=")
542    two_word_flags+=("-b")
543    local_nonpersistent_flags+=("--by=")
544    flags+=("--direction=")
545    two_word_flags+=("-d")
546    local_nonpersistent_flags+=("--direction=")
547
548    must_have_one_flag=()
549    must_have_one_noun=()
550    noun_aliases=()
551}
552
553_git-bug_ls-id()
554{
555    last_command="git-bug_ls-id"
556
557    command_aliases=()
558
559    commands=()
560
561    flags=()
562    two_word_flags=()
563    local_nonpersistent_flags=()
564    flags_with_completion=()
565    flags_completion=()
566
567
568    must_have_one_flag=()
569    must_have_one_noun=()
570    noun_aliases=()
571}
572
573_git-bug_ls-label()
574{
575    last_command="git-bug_ls-label"
576
577    command_aliases=()
578
579    commands=()
580
581    flags=()
582    two_word_flags=()
583    local_nonpersistent_flags=()
584    flags_with_completion=()
585    flags_completion=()
586
587
588    must_have_one_flag=()
589    must_have_one_noun=()
590    noun_aliases=()
591}
592
593_git-bug_pull()
594{
595    last_command="git-bug_pull"
596
597    command_aliases=()
598
599    commands=()
600
601    flags=()
602    two_word_flags=()
603    local_nonpersistent_flags=()
604    flags_with_completion=()
605    flags_completion=()
606
607
608    must_have_one_flag=()
609    must_have_one_noun=()
610    noun_aliases=()
611}
612
613_git-bug_push()
614{
615    last_command="git-bug_push"
616
617    command_aliases=()
618
619    commands=()
620
621    flags=()
622    two_word_flags=()
623    local_nonpersistent_flags=()
624    flags_with_completion=()
625    flags_completion=()
626
627
628    must_have_one_flag=()
629    must_have_one_noun=()
630    noun_aliases=()
631}
632
633_git-bug_select()
634{
635    last_command="git-bug_select"
636
637    command_aliases=()
638
639    commands=()
640
641    flags=()
642    two_word_flags=()
643    local_nonpersistent_flags=()
644    flags_with_completion=()
645    flags_completion=()
646
647
648    must_have_one_flag=()
649    must_have_one_noun=()
650    noun_aliases=()
651}
652
653_git-bug_show()
654{
655    last_command="git-bug_show"
656
657    command_aliases=()
658
659    commands=()
660
661    flags=()
662    two_word_flags=()
663    local_nonpersistent_flags=()
664    flags_with_completion=()
665    flags_completion=()
666
667    flags+=("--field=")
668    two_word_flags+=("-f")
669    local_nonpersistent_flags+=("--field=")
670
671    must_have_one_flag=()
672    must_have_one_noun=()
673    noun_aliases=()
674}
675
676_git-bug_status_close()
677{
678    last_command="git-bug_status_close"
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
691    must_have_one_flag=()
692    must_have_one_noun=()
693    noun_aliases=()
694}
695
696_git-bug_status_open()
697{
698    last_command="git-bug_status_open"
699
700    command_aliases=()
701
702    commands=()
703
704    flags=()
705    two_word_flags=()
706    local_nonpersistent_flags=()
707    flags_with_completion=()
708    flags_completion=()
709
710
711    must_have_one_flag=()
712    must_have_one_noun=()
713    noun_aliases=()
714}
715
716_git-bug_status()
717{
718    last_command="git-bug_status"
719
720    command_aliases=()
721
722    commands=()
723    commands+=("close")
724    commands+=("open")
725
726    flags=()
727    two_word_flags=()
728    local_nonpersistent_flags=()
729    flags_with_completion=()
730    flags_completion=()
731
732
733    must_have_one_flag=()
734    must_have_one_noun=()
735    noun_aliases=()
736}
737
738_git-bug_termui()
739{
740    last_command="git-bug_termui"
741
742    command_aliases=()
743
744    commands=()
745
746    flags=()
747    two_word_flags=()
748    local_nonpersistent_flags=()
749    flags_with_completion=()
750    flags_completion=()
751
752
753    must_have_one_flag=()
754    must_have_one_noun=()
755    noun_aliases=()
756}
757
758_git-bug_title_edit()
759{
760    last_command="git-bug_title_edit"
761
762    command_aliases=()
763
764    commands=()
765
766    flags=()
767    two_word_flags=()
768    local_nonpersistent_flags=()
769    flags_with_completion=()
770    flags_completion=()
771
772    flags+=("--title=")
773    two_word_flags+=("-t")
774    local_nonpersistent_flags+=("--title=")
775
776    must_have_one_flag=()
777    must_have_one_noun=()
778    noun_aliases=()
779}
780
781_git-bug_title()
782{
783    last_command="git-bug_title"
784
785    command_aliases=()
786
787    commands=()
788    commands+=("edit")
789
790    flags=()
791    two_word_flags=()
792    local_nonpersistent_flags=()
793    flags_with_completion=()
794    flags_completion=()
795
796
797    must_have_one_flag=()
798    must_have_one_noun=()
799    noun_aliases=()
800}
801
802_git-bug_version()
803{
804    last_command="git-bug_version"
805
806    command_aliases=()
807
808    commands=()
809
810    flags=()
811    two_word_flags=()
812    local_nonpersistent_flags=()
813    flags_with_completion=()
814    flags_completion=()
815
816    flags+=("--number")
817    flags+=("-n")
818    local_nonpersistent_flags+=("--number")
819    flags+=("--commit")
820    flags+=("-c")
821    local_nonpersistent_flags+=("--commit")
822    flags+=("--all")
823    flags+=("-a")
824    local_nonpersistent_flags+=("--all")
825
826    must_have_one_flag=()
827    must_have_one_noun=()
828    noun_aliases=()
829}
830
831_git-bug_webui()
832{
833    last_command="git-bug_webui"
834
835    command_aliases=()
836
837    commands=()
838
839    flags=()
840    two_word_flags=()
841    local_nonpersistent_flags=()
842    flags_with_completion=()
843    flags_completion=()
844
845    flags+=("--port=")
846    two_word_flags+=("-p")
847    local_nonpersistent_flags+=("--port=")
848
849    must_have_one_flag=()
850    must_have_one_noun=()
851    noun_aliases=()
852}
853
854_git-bug_root_command()
855{
856    last_command="git-bug"
857
858    command_aliases=()
859
860    commands=()
861    commands+=("add")
862    commands+=("bridge")
863    commands+=("commands")
864    commands+=("comment")
865    commands+=("deselect")
866    commands+=("label")
867    commands+=("ls")
868    commands+=("ls-id")
869    commands+=("ls-label")
870    commands+=("pull")
871    commands+=("push")
872    commands+=("select")
873    commands+=("show")
874    commands+=("status")
875    commands+=("termui")
876    commands+=("title")
877    commands+=("version")
878    commands+=("webui")
879
880    flags=()
881    two_word_flags=()
882    local_nonpersistent_flags=()
883    flags_with_completion=()
884    flags_completion=()
885
886
887    must_have_one_flag=()
888    must_have_one_noun=()
889    noun_aliases=()
890}
891
892__start_git-bug()
893{
894    local cur prev words cword
895    declare -A flaghash 2>/dev/null || :
896    declare -A aliashash 2>/dev/null || :
897    if declare -F _init_completion >/dev/null 2>&1; then
898        _init_completion -s || return
899    else
900        __git-bug_init_completion -n "=" || return
901    fi
902
903    local c=0
904    local flags=()
905    local two_word_flags=()
906    local local_nonpersistent_flags=()
907    local flags_with_completion=()
908    local flags_completion=()
909    local commands=("git-bug")
910    local must_have_one_flag=()
911    local must_have_one_noun=()
912    local last_command
913    local nouns=()
914
915    __git-bug_handle_word
916}
917
918if [[ $(type -t compopt) = "builtin" ]]; then
919    complete -o default -F __start_git-bug git-bug
920else
921    complete -o default -o nospace -F __start_git-bug git-bug
922fi
923
924# ex: ts=4 sw=4 et filetype=sh