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 flags+=("--title=")
379 two_word_flags+=("-t")
380 local_nonpersistent_flags+=("--title=")
381
382 must_have_one_flag=()
383 must_have_one_noun=()
384 noun_aliases=()
385}
386
387_git-bug_open()
388{
389 last_command="git-bug_open"
390
391 command_aliases=()
392
393 commands=()
394
395 flags=()
396 two_word_flags=()
397 local_nonpersistent_flags=()
398 flags_with_completion=()
399 flags_completion=()
400
401
402 must_have_one_flag=()
403 must_have_one_noun=()
404 noun_aliases=()
405}
406
407_git-bug_pull()
408{
409 last_command="git-bug_pull"
410
411 command_aliases=()
412
413 commands=()
414
415 flags=()
416 two_word_flags=()
417 local_nonpersistent_flags=()
418 flags_with_completion=()
419 flags_completion=()
420
421
422 must_have_one_flag=()
423 must_have_one_noun=()
424 noun_aliases=()
425}
426
427_git-bug_push()
428{
429 last_command="git-bug_push"
430
431 command_aliases=()
432
433 commands=()
434
435 flags=()
436 two_word_flags=()
437 local_nonpersistent_flags=()
438 flags_with_completion=()
439 flags_completion=()
440
441
442 must_have_one_flag=()
443 must_have_one_noun=()
444 noun_aliases=()
445}
446
447_git-bug_show()
448{
449 last_command="git-bug_show"
450
451 command_aliases=()
452
453 commands=()
454
455 flags=()
456 two_word_flags=()
457 local_nonpersistent_flags=()
458 flags_with_completion=()
459 flags_completion=()
460
461
462 must_have_one_flag=()
463 must_have_one_noun=()
464 noun_aliases=()
465}
466
467_git-bug_termui()
468{
469 last_command="git-bug_termui"
470
471 command_aliases=()
472
473 commands=()
474
475 flags=()
476 two_word_flags=()
477 local_nonpersistent_flags=()
478 flags_with_completion=()
479 flags_completion=()
480
481
482 must_have_one_flag=()
483 must_have_one_noun=()
484 noun_aliases=()
485}
486
487_git-bug_webui()
488{
489 last_command="git-bug_webui"
490
491 command_aliases=()
492
493 commands=()
494
495 flags=()
496 two_word_flags=()
497 local_nonpersistent_flags=()
498 flags_with_completion=()
499 flags_completion=()
500
501 flags+=("--port=")
502 two_word_flags+=("-p")
503 local_nonpersistent_flags+=("--port=")
504
505 must_have_one_flag=()
506 must_have_one_noun=()
507 noun_aliases=()
508}
509
510_git-bug_root_command()
511{
512 last_command="git-bug"
513
514 command_aliases=()
515
516 commands=()
517 commands+=("close")
518 commands+=("commands")
519 commands+=("comment")
520 commands+=("label")
521 commands+=("ls")
522 commands+=("new")
523 commands+=("open")
524 commands+=("pull")
525 commands+=("push")
526 commands+=("show")
527 commands+=("termui")
528 commands+=("webui")
529
530 flags=()
531 two_word_flags=()
532 local_nonpersistent_flags=()
533 flags_with_completion=()
534 flags_completion=()
535
536
537 must_have_one_flag=()
538 must_have_one_noun=()
539 noun_aliases=()
540}
541
542__start_git-bug()
543{
544 local cur prev words cword
545 declare -A flaghash 2>/dev/null || :
546 declare -A aliashash 2>/dev/null || :
547 if declare -F _init_completion >/dev/null 2>&1; then
548 _init_completion -s || return
549 else
550 __git-bug_init_completion -n "=" || return
551 fi
552
553 local c=0
554 local flags=()
555 local two_word_flags=()
556 local local_nonpersistent_flags=()
557 local flags_with_completion=()
558 local flags_completion=()
559 local commands=("git-bug")
560 local must_have_one_flag=()
561 local must_have_one_noun=()
562 local last_command
563 local nouns=()
564
565 __git-bug_handle_word
566}
567
568if [[ $(type -t compopt) = "builtin" ]]; then
569 complete -o default -F __start_git-bug git-bug
570else
571 complete -o default -o nospace -F __start_git-bug git-bug
572fi
573
574# ex: ts=4 sw=4 et filetype=sh