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_git-bug_close()
247{
248    last_command="git-bug_close"
249
250    command_aliases=()
251
252    commands=()
253
254    flags=()
255    two_word_flags=()
256    local_nonpersistent_flags=()
257    flags_with_completion=()
258    flags_completion=()
259
260
261    must_have_one_flag=()
262    must_have_one_noun=()
263    noun_aliases=()
264}
265
266_git-bug_commands()
267{
268    last_command="git-bug_commands"
269
270    command_aliases=()
271
272    commands=()
273
274    flags=()
275    two_word_flags=()
276    local_nonpersistent_flags=()
277    flags_with_completion=()
278    flags_completion=()
279
280    flags+=("--pretty")
281    flags+=("-p")
282    local_nonpersistent_flags+=("--pretty")
283
284    must_have_one_flag=()
285    must_have_one_noun=()
286    noun_aliases=()
287}
288
289_git-bug_comment()
290{
291    last_command="git-bug_comment"
292
293    command_aliases=()
294
295    commands=()
296
297    flags=()
298    two_word_flags=()
299    local_nonpersistent_flags=()
300    flags_with_completion=()
301    flags_completion=()
302
303    flags+=("--file=")
304    two_word_flags+=("-F")
305    local_nonpersistent_flags+=("--file=")
306    flags+=("--message=")
307    two_word_flags+=("-m")
308    local_nonpersistent_flags+=("--message=")
309
310    must_have_one_flag=()
311    must_have_one_noun=()
312    noun_aliases=()
313}
314
315_git-bug_label()
316{
317    last_command="git-bug_label"
318
319    command_aliases=()
320
321    commands=()
322
323    flags=()
324    two_word_flags=()
325    local_nonpersistent_flags=()
326    flags_with_completion=()
327    flags_completion=()
328
329    flags+=("--remove")
330    flags+=("-r")
331    local_nonpersistent_flags+=("--remove")
332
333    must_have_one_flag=()
334    must_have_one_noun=()
335    noun_aliases=()
336}
337
338_git-bug_ls()
339{
340    last_command="git-bug_ls"
341
342    command_aliases=()
343
344    commands=()
345
346    flags=()
347    two_word_flags=()
348    local_nonpersistent_flags=()
349    flags_with_completion=()
350    flags_completion=()
351
352
353    must_have_one_flag=()
354    must_have_one_noun=()
355    noun_aliases=()
356}
357
358_git-bug_new()
359{
360    last_command="git-bug_new"
361
362    command_aliases=()
363
364    commands=()
365
366    flags=()
367    two_word_flags=()
368    local_nonpersistent_flags=()
369    flags_with_completion=()
370    flags_completion=()
371
372    flags+=("--file=")
373    two_word_flags+=("-F")
374    local_nonpersistent_flags+=("--file=")
375    flags+=("--message=")
376    two_word_flags+=("-m")
377    local_nonpersistent_flags+=("--message=")
378
379    must_have_one_flag=()
380    must_have_one_noun=()
381    noun_aliases=()
382}
383
384_git-bug_open()
385{
386    last_command="git-bug_open"
387
388    command_aliases=()
389
390    commands=()
391
392    flags=()
393    two_word_flags=()
394    local_nonpersistent_flags=()
395    flags_with_completion=()
396    flags_completion=()
397
398
399    must_have_one_flag=()
400    must_have_one_noun=()
401    noun_aliases=()
402}
403
404_git-bug_pull()
405{
406    last_command="git-bug_pull"
407
408    command_aliases=()
409
410    commands=()
411
412    flags=()
413    two_word_flags=()
414    local_nonpersistent_flags=()
415    flags_with_completion=()
416    flags_completion=()
417
418
419    must_have_one_flag=()
420    must_have_one_noun=()
421    noun_aliases=()
422}
423
424_git-bug_push()
425{
426    last_command="git-bug_push"
427
428    command_aliases=()
429
430    commands=()
431
432    flags=()
433    two_word_flags=()
434    local_nonpersistent_flags=()
435    flags_with_completion=()
436    flags_completion=()
437
438
439    must_have_one_flag=()
440    must_have_one_noun=()
441    noun_aliases=()
442}
443
444_git-bug_show()
445{
446    last_command="git-bug_show"
447
448    command_aliases=()
449
450    commands=()
451
452    flags=()
453    two_word_flags=()
454    local_nonpersistent_flags=()
455    flags_with_completion=()
456    flags_completion=()
457
458
459    must_have_one_flag=()
460    must_have_one_noun=()
461    noun_aliases=()
462}
463
464_git-bug_webui()
465{
466    last_command="git-bug_webui"
467
468    command_aliases=()
469
470    commands=()
471
472    flags=()
473    two_word_flags=()
474    local_nonpersistent_flags=()
475    flags_with_completion=()
476    flags_completion=()
477
478    flags+=("--port=")
479    two_word_flags+=("-p")
480    local_nonpersistent_flags+=("--port=")
481
482    must_have_one_flag=()
483    must_have_one_noun=()
484    noun_aliases=()
485}
486
487_git-bug_root_command()
488{
489    last_command="git-bug"
490
491    command_aliases=()
492
493    commands=()
494    commands+=("close")
495    commands+=("commands")
496    commands+=("comment")
497    commands+=("label")
498    commands+=("ls")
499    commands+=("new")
500    commands+=("open")
501    commands+=("pull")
502    commands+=("push")
503    commands+=("show")
504    commands+=("webui")
505
506    flags=()
507    two_word_flags=()
508    local_nonpersistent_flags=()
509    flags_with_completion=()
510    flags_completion=()
511
512
513    must_have_one_flag=()
514    must_have_one_noun=()
515    noun_aliases=()
516}
517
518__start_git-bug()
519{
520    local cur prev words cword
521    declare -A flaghash 2>/dev/null || :
522    declare -A aliashash 2>/dev/null || :
523    if declare -F _init_completion >/dev/null 2>&1; then
524        _init_completion -s || return
525    else
526        __git-bug_init_completion -n "=" || return
527    fi
528
529    local c=0
530    local flags=()
531    local two_word_flags=()
532    local local_nonpersistent_flags=()
533    local flags_with_completion=()
534    local flags_completion=()
535    local commands=("git-bug")
536    local must_have_one_flag=()
537    local must_have_one_noun=()
538    local last_command
539    local nouns=()
540
541    __git-bug_handle_word
542}
543
544if [[ $(type -t compopt) = "builtin" ]]; then
545    complete -o default -F __start_git-bug git-bug
546else
547    complete -o default -o nospace -F __start_git-bug git-bug
548fi
549
550# ex: ts=4 sw=4 et filetype=sh