-
Notifications
You must be signed in to change notification settings - Fork 0
/
gear-import.in
executable file
·380 lines (337 loc) · 9.54 KB
/
gear-import.in
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#!/bin/sh -efu
#
# Copyright (C) 2009-2018 Alexey Gladkov <[email protected]>
# Copyright (C) 2009-2010 Alexey I. Froloff <[email protected]>
# Copyright (C) 2010-2017 Dmitry V. Levin <[email protected]>
# Copyright (C) 2019 Vladimir D. Seleznev <[email protected]>
#
# Incrementally import source packages to git repository.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#
. gear-utils-sh-functions
. shell-error
. shell-var
. shell-getopt
show_help()
{
cat <<-EOF
Usage: $PROG [options] <archive> [<archive>...]
Options:
--dry-run show parser results and exit;
--no-commit do not commit new changes;
--no-tag do not add tag;
--time=STR use time described by STR;
--time=FILE use the last modification time of FILE;
--ignore-exclude pass --ignore-exclude to gear-update;
--honor-exclude pass --honor-exclude to gear-update;
-C, --create pass --create to gear-update;
-f, --force pass --force to gear-update;
-a, --all pass --all to gear-update;
-s, --sign add sign to commit and tag;
-S, --subdir=DIR unpack archives into DIR;
-c, --commit=MSG use MSG as commit message;
-t, --tag=NAME use NAME as tag name;
-T, --tag-message=MSG use MSG as tag message;
-r, --rules=FILE override import rules file name;
-v, --verbose print a message for each action;
-V, --version print program version and exit;
-h, --help show this text and exit.
Report bugs to https://bugzilla.altlinux.org/
EOF
exit
}
print_version()
{
cat <<-EOF
$PROG version $PROG_VERSION
Written by Alexey Gladkov <[email protected]>
Copyright (C) 2009-2018 Alexey Gladkov <[email protected]>
Copyright (C) 2009-2010 Alexey I. Froloff <[email protected]>
Copyright (C) 2010-2017 Dmitry V. Levin <[email protected]>
Copyright (C) 2019 Vladimir D. Seleznev <[email protected]>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit
}
archive=
archive_name=
archive_version=
expand_string()
{
local __var="$1" __mess="$2" __oldmess=
while [ "$__mess" != "$__oldmess" ]; do
__oldmess="$__mess"
case "$__mess" in
*@archive@*)
__mess="${__mess%%@archive@*}$archive${__mess#*@archive@}"
;;
*@archive_file@*)
__mess="${__mess%%@archive_file@*}${archive_file##*/}${__mess#*@archive_file@}"
;;
*@name@*)
__mess="${__mess%%@name@*}$archive_name${__mess#*@name@}"
;;
*@version@*)
__mess="${__mess%%@version@*}$archive_version${__mess#*@version@}"
;;
esac
done
eval "$__var=\"\$__mess\""
}
remove_suffix()
{
local __outvar __archive __fn __fo=
__outvar="$1"; shift
__archive="$1"; shift
__fn="$__archive"
while [ "$__fn" != "$__fo" ]; do
__fo="$__fn"
case "$__fn" in
*.bz2) __fn="${__fn%.bz2}" ;;
*.cpio) __fn="${__fn%.cpio}" ;;
*.gem) __fn="${__fn%.gem}" ;;
*.gz) __fn="${__fn%.gz}" ;;
*.jar) __fn="${__fn%.jar}" ;;
*.lzma) __fn="${__fn%.lzma}" ;;
*.tar) __fn="${__fn%.tar}" ;;
*.tbz) __fn="${__fn%.tbz}" ;;
*.tbz2) __fn="${__fn%.tbz2}" ;;
*.tgz) __fn="${__fn%.tgz}" ;;
*.txz) __fn="${__fn%.txz}" ;;
*.xpi) __fn="${__fn%.xpi}" ;;
*.xz) __fn="${__fn%.xz}" ;;
*.zip) __fn="${__fn%.zip}" ;;
*.zst) __fn="${__fn%.zst}" ;;
*.[Zz]) __fn="${__fn%.[Zz]}" ;;
esac
done
# We should ignore the program if it does not have all the well-known suffix.
[ "$__archive" != "$__fn" ] ||
return 1
eval "$__outvar=\"\$__fn\""
}
archive_name=
archive_version=
parse_archive()
{
local archive pattern name version
archive="$1"; shift
pattern="$1"; shift
name="$1" ; shift
version="$1"; shift
archive_name=
if [ -n "$name" ]; then
archive_name="$(printf '%s' "$archive" |
sed -r -n "s#$pattern#$name#${sed_i}p")"
[ -n "$archive_name" ] ||
return 1
fi
archive_version=
if [ -n "$version" ]; then
archive_version="$(printf '%s' "$archive" |
sed -r -n "s#$pattern#$version#${sed_i}p")"
[ -n "$archive_version" ] ||
return 1
fi
}
process_rule()
{
if [ -n "$pattern" ] && parse_archive "$archive" "$pattern" "$name" "$version"; then
[ -z "$replace_name" ] ||
archive_name="$(printf '%s' "$archive_name" |
sed -r -- "$replace_name")" ||
fatal "$patterns: Unable to use expression"
[ -z "$replace_version" ] ||
archive_version="$(printf '%s' "$archive_version" |
sed -r -- "$replace_version")" ||
fatal "$patterns: Unable to use expression"
return 0
fi
# reset optional params
replace_version='' replace_name='' sed_i='i'
return 1
}
rulesfile=
parse_rules()
{
local archive="$1"
local eof='' keyword value
local pattern name version replace_version replace_name sed_i='i'
gear_config_option pattern 'archive-pattern' ""
gear_config_option name 'name-template' ""
gear_config_option version 'version-template' ""
gear_config_option replace_name 'name-transform' ""
gear_config_option replace_version 'version-transform' ""
gear_config_option case_sense 'case-sense' ""
! shell_var_is_yes "$case_sense" ||
sed_i=
while [ -z "$eof" ]; do
read -r keyword value ||
eof=1
case "$keyword" in
pattern:)
! process_rule || return 0
pattern="$value"
;;
name:)
name="$value"
;;
version:)
version="$value"
;;
replace-name:)
replace_name="$value"
;;
replace-version:)
replace_version="$value"
;;
case-sense:)
! shell_var_is_yes "$value" ||
sed_i=
;;
esac
done < "$rulesfile"
process_rule ||:
}
no_commit=
no_tag=
sign=
tag_format=
tag_msg_format=
commit_msg_format=
force=
all_files=
subdir=
dest_create=
ignore_exclude=
honor_exclude=
timestamp=
dry_run=
gear_config_option rulesfile rules \
"${IMPORT_RULES:-@geardatadir@/gear-import-rules}"
gear_config_option tag_format tag ""
gear_config_option tag_msg_format tag-message ""
gear_config_option commit_msg_format commit-message ""
TEMP=$(getopt -n $PROG -o a,f,c:,C,r:,s,S:,t:,T:,v,V,h -l all,commit:,create,dry-run,force,no-commit,no-tag,ignore-exclude,honor-exclude,rules:,sign,subdir:,tag:,tag-message:,time:,verbose,version,help -- "$@") ||
show_usage
eval set -- "$TEMP"
while :; do
case "$1" in
--no-commit) no_commit=1
;;
--no-tag) no_tag=1
;;
--time) shift; timestamp="$1"
;;
--ignore-exclude) ignore_exclude=1
;;
--honor-exclude) honor_exclude=1
;;
--dry-run) dry_run=1
;;
-c|--commit) shift; commit_msg_format="$1"
;;
-t|--tag) shift; tag_format="$1"
;;
-T|--tag-message) shift; tag_msg_format="$1"
;;
-f|--force) force=1
;;
-C|--create) dest_create=1
;;
-a|--all) all_files=1
;;
-r|--rules) shift; rulesfile="$1"
;;
-s|--sign) sign=-s
;;
-S|--subdir) shift; subdir="$1"
;;
-h|--help) show_help
;;
-V|--version) print_version
;;
-v|--verbose) verbose=-v
;;
--) shift; break
;;
*) fatal "Unrecognized option: $1"
;;
esac
shift
done
rulesfile="$(readlink -ev "$rulesfile")"
[ -s "$rulesfile" ] ||
fatal "$rulesfile: file empty."
faketime="$(which faketime 2>/dev/null)" ||
fatal "faketime not found."
if [ -n "$timestamp" ]; then
[ -e "$timestamp" ] &&
faketime="$faketime -r" ||
faketime="$faketime -d"
$faketime "$timestamp" true
else
faketime="$faketime -r"
fi
# Change to toplevel directory
chdir_to_toplevel
for archive_file; do
archive="${archive_file##*/}"
archive_name=
archive_version=
if ! remove_suffix archive "$archive"; then
message "Unknown archive format: $archive_file"
continue
fi
if ! parse_rules "$archive"; then
message "Unable to parse archive: $archive"
continue
fi
if [ -n "$dry_run" ]; then
printf 'archive: %s\n' "$archive"
printf ' name: %s\n' "$archive_name"
printf 'version: %s\n\n' "$archive_version"
continue
fi
extra_args=
if [ ! -d "${subdir:-$archive_name}" ]; then
[ -n "$dest_create" ] ||
fatal "Neither ${subdir:-$archive_name} exists nor --create specified"
extra_args='--create'
fi
[ -z "$ignore_exclude" ] ||
extra_args="$extra_args --ignore-exclude"
[ -z "$honor_exclude" ] ||
extra_args="$extra_args --honor-exclude"
[ -z "$force" ] ||
extra_args="$extra_args --force"
[ -z "$all_files" ] ||
extra_args="$extra_args --all"
git clean -f -d "${subdir:-$archive_name}"
gear-update $extra_args -- "$archive_file" "${subdir:-$archive_name}"
[ -z "$no_commit" ] ||
continue
expand_string tag "${tag_format:-v$archive_version}"
expand_string tag_msg "${tag_msg_format:-$archive_name $archive_version}"
expand_string commit_msg "${commit_msg_format:-Import archive $archive}"
$faketime "${timestamp:-$archive_file}" -- \
git commit --no-verify $sign -a -m "$commit_msg"
[ -z "$no_tag" ] ||
continue
$faketime "${timestamp:-$archive_file}" -- \
git tag $sign -f -m "$tag_msg" "$tag"
done