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_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_pull()
451{
452    last_command="git-bug_pull"
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_push()
471{
472    last_command="git-bug_push"
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_select()
491{
492    last_command="git-bug_select"
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_show()
511{
512    last_command="git-bug_show"
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_status_close()
531{
532    last_command="git-bug_status_close"
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_open()
551{
552    last_command="git-bug_status_open"
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()
571{
572    last_command="git-bug_status"
573
574    command_aliases=()
575
576    commands=()
577    commands+=("close")
578    commands+=("open")
579
580    flags=()
581    two_word_flags=()
582    local_nonpersistent_flags=()
583    flags_with_completion=()
584    flags_completion=()
585
586
587    must_have_one_flag=()
588    must_have_one_noun=()
589    noun_aliases=()
590}
591
592_git-bug_termui()
593{
594    last_command="git-bug_termui"
595
596    command_aliases=()
597
598    commands=()
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_title_edit()
613{
614    last_command="git-bug_title_edit"
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    flags+=("--title=")
627    two_word_flags+=("-t")
628    local_nonpersistent_flags+=("--title=")
629
630    must_have_one_flag=()
631    must_have_one_noun=()
632    noun_aliases=()
633}
634
635_git-bug_title()
636{
637    last_command="git-bug_title"
638
639    command_aliases=()
640
641    commands=()
642    commands+=("edit")
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_webui()
657{
658    last_command="git-bug_webui"
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+=("--port=")
671    two_word_flags+=("-p")
672    local_nonpersistent_flags+=("--port=")
673
674    must_have_one_flag=()
675    must_have_one_noun=()
676    noun_aliases=()
677}
678
679_git-bug_root_command()
680{
681    last_command="git-bug"
682
683    command_aliases=()
684
685    commands=()
686    commands+=("add")
687    commands+=("commands")
688    commands+=("comment")
689    commands+=("label")
690    commands+=("ls")
691    commands+=("pull")
692    commands+=("push")
693    commands+=("select")
694    commands+=("show")
695    commands+=("status")
696    commands+=("termui")
697    commands+=("title")
698    commands+=("webui")
699
700    flags=()
701    two_word_flags=()
702    local_nonpersistent_flags=()
703    flags_with_completion=()
704    flags_completion=()
705
706
707    must_have_one_flag=()
708    must_have_one_noun=()
709    noun_aliases=()
710}
711
712__start_git-bug()
713{
714    local cur prev words cword
715    declare -A flaghash 2>/dev/null || :
716    declare -A aliashash 2>/dev/null || :
717    if declare -F _init_completion >/dev/null 2>&1; then
718        _init_completion -s || return
719    else
720        __git-bug_init_completion -n "=" || return
721    fi
722
723    local c=0
724    local flags=()
725    local two_word_flags=()
726    local local_nonpersistent_flags=()
727    local flags_with_completion=()
728    local flags_completion=()
729    local commands=("git-bug")
730    local must_have_one_flag=()
731    local must_have_one_noun=()
732    local last_command
733    local nouns=()
734
735    __git-bug_handle_word
736}
737
738if [[ $(type -t compopt) = "builtin" ]]; then
739    complete -o default -F __start_git-bug git-bug
740else
741    complete -o default -o nospace -F __start_git-bug git-bug
742fi
743
744# ex: ts=4 sw=4 et filetype=sh