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
401    must_have_one_flag=()
402    must_have_one_noun=()
403    noun_aliases=()
404}
405
406_git-bug_comment()
407{
408    last_command="git-bug_comment"
409
410    command_aliases=()
411
412    commands=()
413    commands+=("add")
414
415    flags=()
416    two_word_flags=()
417    local_nonpersistent_flags=()
418    flags_with_completion=()
419    flags_completion=()
420
421    flags+=("--file=")
422    two_word_flags+=("-F")
423    local_nonpersistent_flags+=("--file=")
424    flags+=("--message=")
425    two_word_flags+=("-m")
426    local_nonpersistent_flags+=("--message=")
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-label()
554{
555    last_command="git-bug_ls-label"
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_pull()
574{
575    last_command="git-bug_pull"
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_push()
594{
595    last_command="git-bug_push"
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_select()
614{
615    last_command="git-bug_select"
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_show()
634{
635    last_command="git-bug_show"
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_status_close()
654{
655    last_command="git-bug_status_close"
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
668    must_have_one_flag=()
669    must_have_one_noun=()
670    noun_aliases=()
671}
672
673_git-bug_status_open()
674{
675    last_command="git-bug_status_open"
676
677    command_aliases=()
678
679    commands=()
680
681    flags=()
682    two_word_flags=()
683    local_nonpersistent_flags=()
684    flags_with_completion=()
685    flags_completion=()
686
687
688    must_have_one_flag=()
689    must_have_one_noun=()
690    noun_aliases=()
691}
692
693_git-bug_status()
694{
695    last_command="git-bug_status"
696
697    command_aliases=()
698
699    commands=()
700    commands+=("close")
701    commands+=("open")
702
703    flags=()
704    two_word_flags=()
705    local_nonpersistent_flags=()
706    flags_with_completion=()
707    flags_completion=()
708
709
710    must_have_one_flag=()
711    must_have_one_noun=()
712    noun_aliases=()
713}
714
715_git-bug_termui()
716{
717    last_command="git-bug_termui"
718
719    command_aliases=()
720
721    commands=()
722
723    flags=()
724    two_word_flags=()
725    local_nonpersistent_flags=()
726    flags_with_completion=()
727    flags_completion=()
728
729
730    must_have_one_flag=()
731    must_have_one_noun=()
732    noun_aliases=()
733}
734
735_git-bug_title_edit()
736{
737    last_command="git-bug_title_edit"
738
739    command_aliases=()
740
741    commands=()
742
743    flags=()
744    two_word_flags=()
745    local_nonpersistent_flags=()
746    flags_with_completion=()
747    flags_completion=()
748
749    flags+=("--title=")
750    two_word_flags+=("-t")
751    local_nonpersistent_flags+=("--title=")
752
753    must_have_one_flag=()
754    must_have_one_noun=()
755    noun_aliases=()
756}
757
758_git-bug_title()
759{
760    last_command="git-bug_title"
761
762    command_aliases=()
763
764    commands=()
765    commands+=("edit")
766
767    flags=()
768    two_word_flags=()
769    local_nonpersistent_flags=()
770    flags_with_completion=()
771    flags_completion=()
772
773
774    must_have_one_flag=()
775    must_have_one_noun=()
776    noun_aliases=()
777}
778
779_git-bug_webui()
780{
781    last_command="git-bug_webui"
782
783    command_aliases=()
784
785    commands=()
786
787    flags=()
788    two_word_flags=()
789    local_nonpersistent_flags=()
790    flags_with_completion=()
791    flags_completion=()
792
793    flags+=("--port=")
794    two_word_flags+=("-p")
795    local_nonpersistent_flags+=("--port=")
796
797    must_have_one_flag=()
798    must_have_one_noun=()
799    noun_aliases=()
800}
801
802_git-bug_root_command()
803{
804    last_command="git-bug"
805
806    command_aliases=()
807
808    commands=()
809    commands+=("add")
810    commands+=("bridge")
811    commands+=("commands")
812    commands+=("comment")
813    commands+=("deselect")
814    commands+=("label")
815    commands+=("ls")
816    commands+=("ls-label")
817    commands+=("pull")
818    commands+=("push")
819    commands+=("select")
820    commands+=("show")
821    commands+=("status")
822    commands+=("termui")
823    commands+=("title")
824    commands+=("webui")
825
826    flags=()
827    two_word_flags=()
828    local_nonpersistent_flags=()
829    flags_with_completion=()
830    flags_completion=()
831
832
833    must_have_one_flag=()
834    must_have_one_noun=()
835    noun_aliases=()
836}
837
838__start_git-bug()
839{
840    local cur prev words cword
841    declare -A flaghash 2>/dev/null || :
842    declare -A aliashash 2>/dev/null || :
843    if declare -F _init_completion >/dev/null 2>&1; then
844        _init_completion -s || return
845    else
846        __git-bug_init_completion -n "=" || return
847    fi
848
849    local c=0
850    local flags=()
851    local two_word_flags=()
852    local local_nonpersistent_flags=()
853    local flags_with_completion=()
854    local flags_completion=()
855    local commands=("git-bug")
856    local must_have_one_flag=()
857    local must_have_one_noun=()
858    local last_command
859    local nouns=()
860
861    __git-bug_handle_word
862}
863
864if [[ $(type -t compopt) = "builtin" ]]; then
865    complete -o default -F __start_git-bug git-bug
866else
867    complete -o default -o nospace -F __start_git-bug git-bug
868fi
869
870# ex: ts=4 sw=4 et filetype=sh