forked from defunkt/github-gem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gh-completion.sh
233 lines (196 loc) · 5.12 KB
/
gh-completion.sh
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!bash
#
# bash completion support for the github gem.
#
# Written by Pedro Melo <melo@simplicidade.org>, October 1, 2009
#
_gh_commands='
browse clone config create-from-local create
fetch fetch_all fork home ignore info network
pull-request pull track
'
_gh_trace()
{
if [ -n "$GH_TRACE_TO" ] ; then
echo "$@" >> "$GH_TRACE_TO"
fi
}
_gh_comp()
{
local possible_comps=$1
local partial=$2
_gh_trace "Got possibles '$possible_comps', partial '$partial'"
if [ -z "$partial" ] ; then
partial=${COMP_WORDS[COMP_CWORD]}
_gh_trace "No partial given, using last word '$partial'"
fi
COMPREPLY=( $( compgen -W "$possible_comps" -- $partial ))
}
_gh_comp_opts()
{
local word_comps=$1
local opts_comps=$2
local partial=$3
_gh_trace "Got words '$word_comps' and opts '$opts_comps', partial '$partial'"
if [ -z "$partial" ] ; then
partial=${COMP_WORDS[COMP_CWORD]}
_gh_trace "No partial given, using last word '$partial'"
fi
case "$partial" in
-*) _gh_comp "$opts_comps" "$partial" ;;
*) _gh_comp "$word_comps" "$partial" ;;
esac
}
_gh_next_word()
{
local c=${1:-1} i
while [ $c -lt $COMP_CWORD ]; do
i="${COMP_WORDS[c]}"
_gh_trace " iter i='$i', c is '$c'"
case "$i" in
-*) ;; # ignore options before command
*) echo $i; break ;;
esac
c=$((++c))
done
}
_gh_find_subcommand ()
{
local c=$1 subcommands=$2
_gh_trace "Find subcommand of '$2' (starts at $c)"
while [ $c -lt $COMP_CWORD ]; do
local word="${COMP_WORDS[c]}" sc
_gh_trace " iter check word '$word' at '$c'"
for sc in $2; do
_gh_trace " == '$sc'?"
if [ "$sc" = "$word" ]; then
_gh_trace "Word '$word' is a subcommand"
echo "$sc"
return
fi
done
c=$((++c))
done
}
_gh_network_users()
{
## FIXME: this is an expensive call, cache it locally
gh network list
}
_gh_remote_users()
{
git remote -v | egrep '//github.com/.+ \(fetch\)' | cut -f1
}
_gh_remote_user_branches()
{
local user=$1
git branch -r | grep "$user/" | cut -f2 -d/
}
_gh_browse_completion()
{
local user=$( _gh_next_word $1 )
if [ -z "$user" ] ; then
local users=$( _gh_remote_users )
_gh_trace "No users, complete from '$users'"
_gh_comp "$users"
else
local branches=$( _gh_remote_user_branches $user )
_gh_trace "Got user as '$user', complete from branches"
_gh_comp "$branches"
fi
}
_gh_clone_completion()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
_gh_trace "Completing 'clone', cur word is '$cur'"
case "$cur" in
-*) _gh_comp "--ssh"; return;;
esac
_gh_trace "The '$cur' is not an option, so don't complete"
COMPREPLY=()
}
_gh_create_completion()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
_gh_trace "Completing 'create', cur word is '$cur'"
case "$cur" in
-*) _gh_comp "
--rdoc
--rst
--markdown
--mdown
--textile
"; return;;
esac
_gh_trace "The '$cur' is not an option, so don't complete"
COMPREPLY=()
}
_gh_network_completion()
{
local network_subcommands="web list fetch commits"
## TODO: figure out if this are 'network' command or 'commit' subcommand options
local network_options="
--applies --before --project --after --thisbranch --noapply
--nocache --cache --sort --shas --author --common --limit
"
local subcommand=$( _gh_find_subcommand "$1" "$network_subcommands" )
if [ -z "$subcommand" ] ; then
local cur="${COMP_WORDS[COMP_CWORD]}"
_gh_trace "No subcommand, try complete '$cur'"
case "$cur" in
-*) _gh_comp "$network_options" "$cur" ;;
*) _gh_comp "$network_subcommands" "$cur" ;;
esac
return
fi
_gh_trace "Got subcommand '$subcommand'"
case "$subcommand" in
## FIXME: we should only match one user
web) users=$( _gh_network_users ); _gh_comp_opts "$users" "$network_options" ;;
*) _gh_comp "$network_options" ;;
esac
}
_gh_pull_completion()
{
local user=$( _gh_next_word $1 )
if [ -z "$user" ] ; then
local users=$( _gh_remote_users )
local opts="--merge"
_gh_trace "No user, complete from '$users' and '$opts'"
_gh_comp_opts "$users" "$opts"
else
local branches=$( _gh_remote_user_branches $user )
_gh_trace "Got user as '$user', complete from branches"
_gh_comp "$branches"
fi
}
_gh_gem_completion()
{
local c=1 command partial
_gh_trace $( set | egrep '^COMP_(CWORD|WORDS|LINE|POINT)=' )
while [ $c -lt $COMP_CWORD ]; do
i="${COMP_WORDS[c]}"
_gh_trace " iter i='$i', c is '$c'"
case "$i" in
-*) ;; # ignore options before command
*) command="$i"; c=$((++c)); break ;;
esac
c=$((++c))
done
if [ -z "$command" ] ; then
_gh_trace "No command, complete from global list of commands"
_gh_comp "$_gh_commands"
return 0
fi
_gh_trace "Got command '$command', c is '$c'"
case "$command" in
browse) _gh_browse_completion $c ;;
clone) _gh_clone_completion $c ;;
create) _gh_create_completion $c ;;
network) _gh_network_completion $c ;;
pull) _gh_pull_completion $c ;;
*) _gh_trace "Command not handled"; COMPREPLY=() ;;
esac
return 0
}
complete -F _gh_gem_completion gh