-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbranch.completion
80 lines (71 loc) · 2.67 KB
/
branch.completion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env bash
#
# Provides autocompletion for the `branch` command.
function _remove_from_array() {
local array=("$@")
local remove=$1
local new_array=()
for i in "${array[@]}"; do
if [[ ${i} != ${remove} ]]; then
new_array+=(${i})
fi
done
echo ${new_array[@]}
unset new_array
}
function _branch() {
cur=${COMP_WORDS[${COMP_CWORD}]}
prev=${COMP_WORDS[${COMP_CWORD}-1]}
# branch [-r] [-o <owner>] [-b <base-branch>] [-d <dev-branch>] <repository> <issue-number>
# branch [-r] [-o <owner>] [-b <base-branch>] -d <dev-branch> <repository> --no-issue
options="-r -o -b -d"
option_offset=0
owner="SituDevelopment"
for ((i=1; i<${#COMP_WORDS[@]}; i++)); do
case ${COMP_WORDS[$i]} in
-r)
options=$(_remove_from_array "-r" ${options})
option_offset=$((${option_offset}+1))
;;
-o)
options=$(_remove_from_array "-o" ${options})
option_offset=$((${option_offset}+2))
owner=${COMP_WORDS[$i+1]}
;;
-b)
options=$(_remove_from_array "-b" ${options})
option_offset=$((${option_offset}+2))
base_branch=${COMP_WORDS[$i+1]}
;;
-d)
options=$(_remove_from_array "-d" ${options})
option_offset=$((${option_offset}+2))
dev_branch=${COMP_WORDS[$i+1]}
;;
esac
done
# complete option
if [[ "${cur}" == "-*" ]]; then
COMPREPLY=($(compgen -W "${options}" -- "${cur}"))
return
fi
# complete base-branch
if [[ "${prev}" == "-b" ]]; then
repo=${COMP_WORDS[$((${option_offset}+1))]}
branches=$(bkt --discard-failures -- gh api "/repos/${owner}/${repo}/branches" --jq '.[].name' 2> /dev/null)
COMPREPLY=($(compgen -W "${branches}" -- "${cur}"))
fi
# complete repository
if [[ ${COMP_CWORD} -eq $((${option_offset}+1)) ]]; then
repos=$(bkt --discard-failures -- gh repo list "${owner}" --json name --limit 100 --jq '.[].name' 2> /dev/null)
COMPREPLY=($(compgen -W "${repos} ${options}" -- "${cur}"))
fi
# complete issue number
if [[ ${COMP_CWORD} -eq $((${option_offset}+2)) ]]; then
no_issue_flag=$([[ -n ${dev_branch+placeholder} ]] && echo "--no-issue")
repo=${COMP_WORDS[$((${option_offset}+1))]}
issues=$(bkt --discard-failures -- gh issue list --repo "${owner}/${repo}" --json number --jq '.[].number' 2> /dev/null)
COMPREPLY=($(compgen -W "${issues} ${no_issue_flag}" -- "${cur}"))
fi
}
complete -F _branch branch