forked from openSUSE/osc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosc.zsh
286 lines (254 loc) · 8.43 KB
/
osc.zsh
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#compdef osc
#
# Copyright (C) 2009,2010 Holger Macht <[email protected]>
# Copyright (C) 2023 Björn Bidar <[email protected]>
#
# This file is released under the GPLv2.
#
# Based on the zsh guide from http://zsh.dotsrc.org/Guide/zshguide06.html
#
# Toggle verbose completions: zstyle ':completion:*:osc:*' verbose no
# zstyle ':completion:*:osc-subcommand:*' verbose no
#
# version 0.2
#
# Main dispatcher
_osc() {
# Variables shared by all internal functions
local osc_projects osc_rc osc_cmd osc_alias
_osc_complete_prepare
osc_projects="${XDG_CACHE_HOME}/osc.projects"
osc_rc="${XDG_CONFIG_HOME}/osc/oscrc"
osc_cmd=osc
if [[ "${words[0]}" = "isc" ]] ; then
osc_alias=internal
fi
if [ -s "${PWD}/.osc/_apiurl" -a -s "${osc_rc}" ]; then
local osc_apiurl
read osc_apiurl < "${PWD}/.osc/_apiurl"
# We prefer to match an apiurl with an alias so that the project list
# cache would match also when -A was passed with said alias.
# If there's no alias for that api url match to use the plain apiurl instead.
osc_alias=$(sed -rn '\@^\['${apiurl}'@,\@=@{\@^aliases=@{s@[^=]+=([^,]+),.*@\1@p};}' < "${osc_rc}" 2> /dev/null)
if [ -z $osc_alias ] ; then
osc_alias=${osc_apiurl}
fi
fi
if (( CURRENT > 2 )) && [[ ${words[2]} != "help" ]]; then
# Remember the subcommand name
local cmd=${words[2]}
# Set the context for the subcommand.
curcontext="${curcontext%:*:*}:osc-subcommand"
# Narrow the range of words we are looking at to exclude `osc'
(( CURRENT-- ))
shift words
# Run the completion for the subcommand
if [ $cmd = -A -o $cmd = --apiurl ] ; then
if [[ -s "${osc_rc}" ]] ; then
local hints=($(sed -rn '/^(aliases=|\[http)/{s/,/ /g;s/(aliases=|\[|\])//gp}' < "${osc_rc}" 2> /dev/null))
if [[ -n "${words[2]}" ]]; then
for h in ${hints[@]} ; do
case "$h" in
http*)
local tmp=$(sed -rn '\@^\['${h}'@,\@=@{\@^aliases=@{s@[^=]+=([^,]+),.*@\1@p};}' < "${osc_rc}" 2> /dev/null)
if [[ "${words[2]}" = "$h" ]]; then
osc_alias=$tmp
break
fi
;;
*)
if [[ "${words[2]}" = "$h" ]]; then
osc_alias=$h
break
fi
esac
done
else
_arguments '1:ALIAS:( `echo $hints`)'
return
fi
fi
fi
if [[ -n "$osc_alias" ]] ; then
osc_projects="${osc_projects}.${osc_alias//\//_}"
osc_command="$osc_command -A ${osc_alias}"
fi
_osc_update_project_list
case $cmd in
submitrequest|submitreq|sr) _osc_cmd_submitreq ;;
getbinaries) _osc_cmd_getbinaries ;;
build) _osc_cmd_build ;;
checkout|co|branch|getpac|bco|branchco) _osc_cmd_checkout ;;
buildlog|buildinfo|bl|blt|buildlogtail) _osc_cmd_buildlog ;;
*) _osc_cmd_do $cmd
esac
else
local hline
local -a cmdlist
local tag=0
_call_program help-commands osc help | while read -A hline; do
# start parsing with "commands:"
[[ $hline[1] = "commands:" ]] && tag=1
# stop parsing at the line starting with "For"
[[ $hline[1] = "For" ]] && tag=0
[[ $tag = 0 ]] && continue
# all commands have to start with lower case letters
[[ $hline[1] =~ ^[A-Z] ]] && continue
(( ${#hline} < 2 )) && continue
# ${hline[1]%,} truncates the last ','
cmdlist=($cmdlist "${hline[1]%,}:${hline[2,-1]}")
done
_describe -t osc-commands 'osc command' cmdlist
fi
}
_osc_call_me_maybe()
{
typeset -i ctime=$(command date -d "$(command stat -c '%z' ${1})" +'%s')
typeset -i now=$(command date -d now +'%s')
if ((now - ctime < 86400)) ; then
return 1
fi
return 0
}
_osc_complete_prepare() {
local xdg_dir
for xdg_dir in "${XDG_CACHE_HOME:=$HOME/.cache}" "${XDG_CONFIG_HOME:=$HOME/.config}"; do
if [[ ! -d "${xdg_dir}" ]]; then
mkdir -p "${xdg_dir}"
fi
done
if [[ -f ~/.osc.projects ]]; then
rm ~/.osc.projects -f
fi
}
_osc_update_project_list() {
if [[ -s "${osc_projects}" ]] ; then
if _osc_call_me_maybe "$osc_projects" ; then
if tmp=$(mktemp ${osc_projects}.XXXXXX) ; then
command ${osc_cmd} ls / >| $tmp
mv -uf $tmp ${osc_projects}
fi
fi
else
command ${osc_cmd} ls / >| "${osc_projects}"
fi
}
_osc_project_repositories() {
if [ ! -s $PWD/.osc/_build_repositories ] || \
_osc_call_me_maybe $PWD/.osc/_build_repositories ; then
osc repositories > /dev/null
fi
# Just check if file exist in case the call to the api failed
if [ -s $PWD/.osc/_build_repositories ] ; then
cat $PWD/.osc/_build_repositories | while read build_repository ; do
# Only output first word of each line
echo ${build_repository%\ *}
done | sort -u
fi
}
_osc_project_repositories_arches() {
if [ ! -s $PWD/.osc/_build_repositories ] || \
_osc_call_me_maybe $PWD/.osc/_build_repositories ; then
osc repositories > /dev/null
fi
# Just check if file exist in case the call to the api failed
if [ -s $PWD/.osc/_build_repositories ] ; then
grep -- $1 $PWD/.osc/_build_repositories | while read build_repository ; do
# Only output second word of each line
echo ${build_repository#*\ }
done | sort -u
fi
}
_osc_cmd_getbinaries() {
if [ "$words[2]" = "-" ]; then
_osc_complete_help_commands 'options' 'option'
return
else
if [ -n "$words[2]" ] ; then
local osc_project_repository_arch=$(_osc_project_repositories_arches \
"${words[2]}")
fi
_arguments \
'1:PROJECT:( `cat $osc_projects` )' \
'2:PACKAGE:(PACKAGE)' \
'3:REPOSITORY:( `_osc_project_repositories`' \
'4:ARCHITECTURE:(`echo $osc_project_repository_arch`)'
fi
}
_osc_cmd_checkout() {
if [ "$words[2]" = "-" ]; then
_osc_complete_help_commands 'options' 'option'
return
else
_arguments \
'1:PROJECT:( `cat $osc_projects` )' \
'2:PACKAGE:(PACKAGE)'
fi
}
_osc_cmd_buildlog() {
if [ "$words[2]" = "-" ]; then
_osc_complete_help_commands 'options' 'option'
return
else
if [ -n "$words[2]" ] ; then
local osc_project_repository_arch=$(_osc_project_repositories_arches \
"${words[2]}")
fi
_arguments \
'1:REPOSITORY:( `_osc_project_repositories` )' \
'2:ARCHITECTURE:(`echo $osc_project_repository_arch`)'
fi
}
_osc_cmd_build() {
if [ "$words[2]" = "-" ]; then
_osc_complete_help_commands 'options' 'option'
return
else
if [ -n "$words[2]" ] ; then
local osc_project_repository_arch=$(_osc_project_repositories_arches \
"${words[2]}")
fi
_arguments \
'1:REPOSITORY:( `_osc_project_repositories` )' \
'2:ARCHITECTURE:(`echo $osc_project_repository_arch`)' \
'3:Build Description:_files'
fi
}
_osc_cmd_submitreq() {
_osc_complete_help_commands 'options' 'option'
}
_osc_complete_help_commands() {
local hline
local -a cmdlist
local tag=0
_call_program help-commands osc help $cmd | while read -A hline; do
# start parsing from "usage:"
[[ $hline[1] = "${1}:" ]] && tag=1
[[ $tag = 0 ]] && continue
if [[ $hline[1] =~ ^osc ]]; then
shift hline; shift hline
elif ! [[ $hline[1] =~ ^- ]]; then
# Option has to start with a '-' or 'osc submitrequest'
continue
fi
(( ${#hline} < 2 )) && continue
cmdlist=($cmdlist "${hline[1]%,}:${hline[2,-1]}")
done
if [ -n "$cmdlist" ] ; then
_describe -t osc-commands "osc $2" cmdlist
else
return 1
fi
}
_osc_cmd_do() {
# only start completion if there's some '-' on the line
if ! [ "$words[2]" = "-" ]; then
_complete
return
fi
if ! _osc_complete_help_commands 'options' 'option'; then
_complete
fi
}
# Code to make sure _osc is run when we load it
_osc "$@"