-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgear-sh-functions.in
951 lines (803 loc) · 20.8 KB
/
gear-sh-functions.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
#!/bin/sh -efu
#
# Copyright (C) 2006-2021 Dmitry V. Levin <ldv@altlinux.org>
# Copyright (C) 2006-2020 Alexey Gladkov <legion@altlinux.org>
# Copyright (C) 2006 Sergey Vlasov <vsu@altlinux.org>
# Copyright (C) 2008 Alexey I. Froloff <raorn@altlinux.org>
#
# gear common shell functions.
#
# 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.
#
if [ -z "${__included_gear_sh_functions-}" ]; then
__included_gear_sh_functions=1
. shell-error
. shell-quote
. shell-args
PROG="${0##*/}"
PROG_VERSION='@VERSION@'
gear_short_options='r:,t:,h,q,v,V'
gear_long_options='no-compress,commit,bzip2,gzip,lz4,lzop,xz,zstd,disable-specsubst,command,hasher,rpmbuild,update-spec,export-dir:,describe,rules:,tree-ish:,help,quiet,verbose,version'
__cleanup_handler_name=
__cleanup_handler()
{
trap - EXIT
[ -z "$__cleanup_handler_name" ] ||
"$__cleanup_handler_name" "$@" ||:
exit "$@"
}
__exit_handler()
{
__cleanup_handler $?
}
__signal_handler()
{
__cleanup_handler 1
}
install_cleanup_handler()
{
__cleanup_handler_name="${1-}"
trap __exit_handler EXIT
trap __signal_handler HUP PIPE INT QUIT TERM
}
uninstall_cleanup_handler()
{
trap - EXIT HUP PIPE INT QUIT TERM
__cleanup_handler_name=
}
# set specified variable to the value stored in another specified variable
# 1st arg is the name of variable that should be assigned,
# 2ns arg is the name of variable that contains the value to be assigned.
set_var_value()
{
eval "$1=\"\${$2-}\""
}
lineno=
rules=
rules_error()
{
local lineno_text=
[ -z "$lineno" ] || lineno_text=" line $lineno"
fatal "$rules$lineno_text: $*"
}
rules_info()
{
local lineno_text=
[ -z "$lineno" ] || lineno_text=" line $lineno"
message "$rules$lineno_text: $*"
}
: "${__gear_exported_vars:=__gear_exported_vars}"
export __gear_exported_vars
gear_export_vars()
{
export "$@"
__gear_exported_vars="$__gear_exported_vars $*"
}
gear_unset_exported_vars()
{
unset $__gear_exported_vars
}
is_hex_sha1()
{
[ "${#1}" -eq 40 ] || return 1
# "${1##*[![:xdigit:]]*}" does not work in ash
[ -n "${1##*[!0123456789abcdefABCDEF]*}" ] || return 1
return 0
}
# Change the current working directory to the top-level directory
# of the git working tree if the former is a subdirectory of the latter.
gear_saved_working_directory=''
chdir_to_toplevel()
{
local cdup
cdup="$(git rev-parse --show-cdup)"
if [ -z "$cdup" ]; then
gear_saved_working_directory=''
else
gear_saved_working_directory="$PWD"
cd "$cdup" ||
fatal "Cannot chdir to $cdup, the toplevel of the working tree"
fi
}
# Change the current working directory back if it was changed earlier
# by chdir_to_toplevel.
chdir_back_from_toplevel()
{
[ -z "$gear_saved_working_directory" ] ||
cd "$gear_saved_working_directory"
}
get_object_sha1()
{
local name="$1"; shift
local sha1
sha1="$(git rev-parse --verify "$name")" || return 1
# Verify that we really got a SHA-1 (git rev-parse --verify accepts
# things like ^COMMIT and returns ^SHA1 for them).
is_hex_sha1 "$sha1" || return 1
printf %s "$sha1"
}
get_tag_sha1()
{
local name="$1"; shift
local sha1
sha1="$(get_object_sha1 "$name")" || return
[ "$(git cat-file -t "$sha1")" = tag ] || return
printf %s "$sha1"
}
get_commit_sha1()
{
local name="$1"; shift
local sha1
sha1="$(get_object_sha1 "$name")" || return 1
git rev-parse --verify "$sha1^0" || return 1
}
get_tree_sha1()
{
local name="$1"; shift
local sha1
sha1="$(get_object_sha1 "$name")" || return 1
git rev-parse --verify "$sha1^{tree}" || return 1
}
tree_entry_name()
{
local tree="$1"; shift
local path="$1"; shift
if [ -z "${path#.}" ]; then
printf %s "$tree"
elif [ -z "${tree##*:*}" ]; then
printf %s "$tree/$path"
else
printf %s "$tree:$path"
fi
}
traverse_tree()
{
local tree="$1"; shift
local path="$1"; shift
local optional="$1"; shift
local id
id="$(tree_entry_name "$tree" "$path")"
if git cat-file tree "$id" >/dev/null 2>&1; then
printf %s "$id"
elif [ "$optional" = 1 ]; then
return 2
else
rules_error "tree \"$path\" not found in \"$tree\""
fi
}
# fetch blob by id+name.
cat_blob()
{
local tree="$1"; shift
local name="$1"; shift
local id
id="$(tree_entry_name "$tree" "$name")"
git cat-file blob "$id" ||
rules_error "blob \"$name\" not found in \"$tree\""
}
# Check that given variable contains non-empty path without directory
# traversal.
check_pathname()
{
local name="$1"; shift
local value="$1"; shift
[ -n "$value" ] ||
rules_error "Empty $name \"$value\" specified"
[ "$value" != '..' ] &&
[ -n "${value##/*}" ] &&
[ -n "${value##../*}" ] &&
[ -n "${value%%*/..}" ] &&
[ -n "${value##*/../*}" ] ||
rules_error "Invalid $name \"$value\" specified"
}
# Check that given variable contains non-empty file name without directory
# separators or directory traversal.
check_filename()
{
local name="$1"; shift
local value="$1"; shift
[ -n "$value" ] ||
rules_error "Empty $name \"$value\" specified"
[ "$value" != '..' ] && [ -n "${value##*/*}" ] ||
rules_error "Invalid $name \"$value\" specified"
}
rules_cleanup()
{
if [ -s "$1" ]; then
echo >> "$1"
sed -i 's/^[[:space:]]*\([^[:space:]:]\+\)[[:space:]]*:[[:space:]]*/\1: /' "$1"
fi
}
workdir=
cleanup_workdir()
{
[ -z "${workdir-}" ] || {
rm -rf -- "$workdir"
workdir=
}
}
main_tree_id=
gear_find_rules_check_tree()
{
[ -n "$(git ls-tree "$main_tree_id" "$1")" ]
}
gear_find_rules_check_cwd()
{
[ -s "$1" ]
}
gear_find_rules_cat_tree()
{
cat_blob "$main_tree_id" "$rules"
}
gear_find_rules_cat_cwd()
{
cat -- "$rules"
}
gear_find_rules_common()
{
local check_rules="$1"; shift
local cat_rules="$1"; shift
if [ -f "$workdir/rules" ]; then
return 0
fi
local r=
if [ -n "$rules" ]; then
if $check_rules "$rules"; then
r="$rules"
fi
else
local n
for n in .gear-rules .gear/rules; do
if $check_rules "$n"; then
rules="$n"
r="$rules"
break
fi
done
fi
if [ -n "$r" ]; then
$cat_rules
fi >"$workdir/rules"
rules_cleanup "$workdir/rules"
[ -n "$rules" ] || rules='.gear/rules'
}
find_rules_in_tree()
{
gear_find_rules_common gear_find_rules_check_tree gear_find_rules_cat_tree
}
find_rules_in_cwd()
{
gear_find_rules_common gear_find_rules_check_cwd gear_find_rules_cat_cwd
}
get_uniq_directive_from_rules()
{
local var_name="$1"; shift
local directive="$1"; shift
local value=
set_var_value "$var_name" value
find_rules_in_tree
[ -s "$workdir/rules" ] || return 0
value="$(sed -rn "s/^$directive:[[:space:]]+([^[:space:]].*)/\\1/p" "$workdir/rules" |
sed 's/[[:space:]]\+$//')"
[ -n "$value" ] || return 0
[ "$(printf %s "$value" |wc -l)" -le 0 ] ||
rules_error "More than one \"$directive\" directive specified"
set_var_value "$var_name" value
}
get_uniq_filename_from_rules()
{
local var_name="$1"; shift
local directive="$1"; shift
local filename=
set_var_value "$var_name" filename
get_uniq_directive_from_rules filename "$directive" || return
[ -n "$filename" ] || return 0
check_pathname "$directive" "$filename"
set_var_value "$var_name" filename
}
expand_rpm_macros()
{
local spec="$1"; shift
local var_name="$1"; shift
local var_value
set_var_value var_value "$var_name"
[ -n "$var_value" ] && [ -z "${var_value##*%*}" ] || return 0
var_value="$(awk '
BEGIN {
macros["nil"] = ""
}
match($0, /^%(define|global)[[:space:]]+([^[:space:]]+)[[:space:]]+(.*)$/, f) {
macros[f[2]] = expand(f[3])
}
END {
print expand(value)
}
function expand(input,
output, pos, macro, f)
{
output = ""
while ((pos = index(input, "%")) != 0) {
output = output substr(input, 1, pos - 1)
input = substr(input, pos + 1)
if (input ~ /^%/) {
output = output "%"
input = substr(input, 2)
continue
}
if (match(input, /^([[:alpha:]_][[:alnum:]_]*)(.*)$/, f) ||
match(input, /^\{([[:alpha:]_][[:alnum:]_]*)\}(.*)$/, f)) {
macro = f[1]
if (macro in macros) {
output = output macros[macro]
input = f[2]
continue
}
}
output = output "%"
}
return output input
}
' value="$var_value" "$spec" )"
set_var_value "$var_name" var_value
}
spec_name=
spec_epoch=
spec_version=
spec_release=
get_NVR_from_spec()
{
local spec="$1"; shift
spec_name="$(sed '/^name:[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"
spec_epoch="$(sed '/^\(epoch\|serial\):[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"
spec_version="$(sed '/^version:[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"
spec_release="$(sed '/^release:[[:space:]]*/I!d;s///;q' "$spec" |sed 's/[[:space:]]\+$//')"
expand_rpm_macros "$spec" spec_name
expand_rpm_macros "$spec" spec_epoch
expand_rpm_macros "$spec" spec_version
expand_rpm_macros "$spec" spec_release
}
substitute_vars_in_spec()
{
[ -n "${specsubst-}" ] || return 0
local tag spec opts var qvar value qvalue
tag="$workdir/main_tag"
spec="$workdir/specfile"
git cat-file tag "$main_tag_sha1" > "$tag"
quote_shell_args opts "$specsubst"
eval "set -- $opts"
for var; do
quote_sed_regexp_variable qvar "$var"
if grep -qs "^X-gear-specsubst:[[:space:]]\\+$qvar=" "$tag"; then
value="$(sed "/^X-gear-specsubst:[[:space:]]\\+$qvar=/I!d;s///;q" "$tag")"
elif grep -qs "^X-girar-specsubst:[[:space:]]\\+$qvar=" "$tag"; then
value="$(sed "/^X-girar-specsubst:[[:space:]]\\+$qvar=/I!d;s///;q" "$tag")"
else
fatal "specsubst variable \"$var\" is not defined in tag \"$main_tag_id\""
fi
quote_sed_regexp_variable qvalue "$value"
sed -i "s/@$qvar@/$qvalue/g" -- "$spec"
done
}
specsubst=
disable_specsubst=
get_specsubst_from_rules()
{
if [ -z "$disable_specsubst" ]; then
get_uniq_directive_from_rules specsubst specsubst
if [ -n "$specsubst" ]; then
[ -z "$(printf %s "$specsubst" |LANG=C tr -d '[:alnum:]_[:space:]')" ] ||
rules_error "Invalid specsubst value \"$specsubst\" specified"
fi
fi
}
spec_type=
get_spec_type_from_rules()
{
get_uniq_directive_from_rules spec_type type
if [ -n "$spec_type" ] &&
[ -n "$(printf %s "$spec_type" |LANG=C tr -d '[:alpha:]')" ]; then
rules_error "Invalid spec type \"$spec_type\" specified"
fi
}
specfile=
pkg_name=
pkg_epoch=
pkg_version=
pkg_release=
find_specfile()
{
local fail_ok="${1-}"
get_spec_type_from_rules
get_specsubst_from_rules
if [ -n "$specsubst" ]; then
if [ -z "${main_tag_sha1-}" ]; then
[ "$fail_ok" = fail_ok ] && specsubst= ||
fatal 'specsubst directive requires a tag'
fi
fi
# first try specfile defined in $rules if any.
get_uniq_filename_from_rules specfile spec
# second try specfile in toplevel tree.
if [ -z "$specfile" ]; then
specfile="$(git ls-tree "$main_tree_id" |
sed -ne 's/^[^[:space:]]\+[[:space:]]\+blob[[:space:]]\+[^[:space:]]\+[[:space:]]\+\([^/[:space:]]\+\.spec\)$/\1/p')"
[ "$(printf %s "$specfile" |wc -l)" -le 0 ] || {
message "Too many specfiles found${GIT_DIR:+ in $GIT_DIR}"
[ "$fail_ok" = fail_ok ] && return 1 || exit
}
fi
[ -n "$specfile" ] || {
[ "$fail_ok" = fail_ok ] && return 1 ||
fatal "No specfiles found${GIT_DIR:+ in $GIT_DIR}"
}
cat_blob "$main_tree_id" "$specfile" >"$workdir/specfile"
substitute_vars_in_spec
local spec_name spec_epoch spec_version spec_release
get_NVR_from_spec "$workdir/specfile"
pkg_name="$spec_name"
pkg_epoch="$spec_epoch"
pkg_version="$spec_version"
pkg_release="$spec_release"
}
tag_dir=
gear_find_tags_check_tree()
{
local t="$1"; shift
local id type
id="$(tree_entry_name "$main_tree_id" "$t/list")"
type="$(git cat-file -t "$id" 2>/dev/null)" || type=
if [ "$type" = 'blob' ]; then
tag_dir="$t"
return 0
fi
return 1
}
gear_find_tags_check_cwd()
{
local t="$1"; shift
if [ -s "$t/list" ]; then
tag_dir="$t"
return 0
fi
return 1
}
gear_find_tags_cat_tree()
{
cat_blob "$main_tree_id" "$tag_dir/list"
}
gear_find_tags_cat_cwd()
{
cat -- "$tag_dir/list"
}
tag_dir_default=
gear_find_tags_common()
{
local check_tags="$1"; shift
local cat_tags="$1"; shift
# first try tag directory defined in rules if any.
get_uniq_filename_from_rules tag_dir tags
# second try tag directory in toplevel tree.
if [ -z "$tag_dir" ]; then
if [ -n "$tag_dir_default" ]; then
if $check_tags "$tag_dir_default"; then
:
fi
else
local t
for t in .gear-tags .gear/tags; do
if $check_tags "$t"; then
break
fi
done
fi
fi
if [ -n "$tag_dir" ]; then
$cat_tags
fi >"$workdir/tags"
[ -n "$tag_dir" ] || tag_dir='.gear/tags'
}
find_tags_in_tree()
{
gear_find_tags_common gear_find_tags_check_tree gear_find_tags_cat_tree
}
find_tags_in_cwd()
{
gear_find_tags_common gear_find_tags_check_cwd gear_find_tags_cat_cwd
}
tag_list_lineno=
tag_list_error()
{
local lineno_text=
[ -z "$tag_list_lineno" ] || lineno_text=" line $tag_list_lineno"
fatal "$tag_dir/list$lineno_text: $*"
}
lookup_tag()
{
local tag_list_file="$1"; shift
local requested_tag_name="$1"; shift
local variable="$1"; shift
local sha1 name
tag_list_lineno=0
while read -r sha1 name; do
tag_list_lineno="$((tag_list_lineno+1))"
if [ "$name" = "$requested_tag_name" ]; then
set_var_value "$variable" sha1
return 0
fi
done <"$tag_list_file"
return 1
}
extract_stored_tag_chain()
{
local sha1="$1"; shift
local id type real_sha1 next
id="$(tree_entry_name "$main_tree_id" "$tag_dir/$sha1")"
type="$(git cat-file -t "$id" 2>/dev/null)" || type=
if [ "$type" = "blob" ]; then
next="$(git cat-file blob "$id" | sed -ne '1s/^object \(.*\)$/\1/p')" ||
tag_list_error "Bad stored tag $sha1: parse failed"
is_hex_sha1 "$next" ||
tag_list_error "Bad stored tag $sha1: invalid format"
real_sha1="$(git cat-file blob "$id" | git hash-object -t tag -w --stdin)" ||
tag_list_error "Bad stored tag $sha1: extract failed"
[ "$real_sha1" = "$sha1" ] ||
tag_list_error "Bad stored tag $sha1: hash mismatch (got $real_sha1)"
extract_stored_tag_chain "$next"
elif [ -n "$type" ]; then
tag_list_error "Bad stored tag $sha1: type=$type"
else
printf %s "$sha1"
fi
}
is_ancestor_commit()
{
local ancestor="$1"; shift
local descendant="$1"; shift
local bases
bases="$(git log -n1 --pretty=format:1 ^"$descendant" "$ancestor")" ||
return 1
[ -z "$bases" ] || return 1
}
check_tag_name()
{
local name="$1"; shift
[ -n "$name" ] ||
rules_error 'Empty tag name is not allowed'
[ -n "${name##/*}" ] ||
rules_error "Invalid tag name \"$name\": initial '/' is not allowed"
[ -n "${name%%*/}" ] ||
rules_error "Invalid tag name \"$name\": trailing '/' is not allowed"
[ -n "${name##*[][*?^~:@[:space:]]*}" ] ||
rules_error "Invalid tag name \"$name\": invalid characters found"
git check-ref-format "tags/$name" ||
rules_error "Invalid tag name \"$name\""
}
main_commit_sha1=
resolve_commit_name()
{
local name="$1"; shift
local result found_commit type final_sha1
if is_hex_sha1 "$name"; then
result="$name"
found_commit="$name"
else
check_tag_name "$name"
lookup_tag "$workdir/tags" "$name" result ||
rules_error "Name \"$name\" not found in tag list"
is_hex_sha1 "$result" ||
tag_list_error "Invalid SHA-1 \"$result\" for name \"$name\""
found_commit="$(extract_stored_tag_chain "$result")" ||
tag_list_error "Broken tag chain for name \"$name\""
fi
type="$(git cat-file -t "$found_commit")" ||
rules_error "Name \"$name\" specifies a nonexistent object $found_commit"
if [ "$type" = "tag" ]; then
rules_error "Tag object $found_commit for name \"$name\" is not stored"
elif [ "$type" != "commit" ]; then
rules_error "Name \"$name\" specifies a $type, not a commit"
fi
# Clean up possible upper/lower case differences in raw SHA-1 ids
final_sha1="$(git rev-parse --verify "$found_commit")" ||
rules_error "Name \"$name\" resolved to invalid object ID \"$found_commit\""
# Ensure than $final_sha1 is an ancestor of $main_commit_sha1
is_ancestor_commit "$final_sha1" "$main_commit_sha1" ||
rules_error "Name \"$name\" resolved to $final_sha1, which is not an ancestor of commit $main_commit_sha1"
printf %s "$result"
}
resolve_tree_base_name()
{
local name="$1"; shift
if [ "$name" = '.' ]; then
printf %s "$main_tree_id"
else
resolve_commit_name "$name"
fi
}
parse_tree_path()
{
local name="$1"; shift
local value="$1"; shift
local path
[ -n "$value" ] ||
rules_error "Empty $name specified"
if [ -z "${value##*:*}" ]; then
[ -n "${value%%:*}" ] ||
rules_error "Empty commit name in $name specified"
path="${value#*:}"
[ -n "$path" ] ||
rules_error "Empty path in $name specified"
else
path="$value"
value=".:$value"
fi
check_pathname "$name" "$path"
printf %s "$value"
}
def_spec_pattern='*.spec'
spec_pattern="$def_spec_pattern"
find_specfile_in_cwd()
{
local prefix="$1"; shift
local pattern f spec='' many_specs=''
for pattern in ${spec_pattern}; do
for f; do
[ -z "${f##$pattern}" ] || [ -z "${f%%$pattern}" ] ||
continue
[ ! -L "$f" ] && [ -f "$f" ] ||
continue
[ -z "$spec" ] ||
many_specs=1
spec="$f"
done
[ -z "$spec" ] ||
break
done
if [ -z "$spec" ]; then
message "${prefix}Spec file not found."
elif [ -n "$many_specs" ]; then
message "${prefix}Too many spec files - ignored all."
spec=
fi
if [ -n "$spec" ]; then
printf %s "$spec"
return 0
else
return 1
fi
}
guess_specfile_from_checkout()
{
local rules="${RULES-}"
local workdir __cleanup_handler_name
install_cleanup_handler cleanup_workdir
workdir="$(mktemp -dt "$PROG.XXXXXXXXXX")"
find_rules_in_cwd
get_uniq_filename_from_rules specfile spec
[ -s "$specfile" ] || specfile=
cleanup_workdir
uninstall_cleanup_handler
}
guess_specfile_from_tree()
{
local rules="${RULES-}"
local main_tree_id="${TREE_ID:-HEAD}"
local workdir __cleanup_handler_name
git cat-file tree "$main_tree_id" >/dev/null 2>&1 || return 0
install_cleanup_handler cleanup_workdir
workdir="$(mktemp -dt "$PROG.XXXXXXXXXX")"
find_specfile fail_ok ||:
cleanup_workdir
uninstall_cleanup_handler
}
# specfile guess logic:
#
# If specfile is not specified, then
# if TREE_ID is specified, then
# first try the rules-based guess from the tree,
# next try the rules-based guess the checkout;
# otherwise
# first try the rules-based guess from checkout,
# next try the rules-based guess from HEAD.
# If nothing of the above helped to find a specfile, then
# try a wild guess from the checkout.
#
guess_specfile()
{
if [ -z "$specfile" ]; then
if [ -n "${TREE_ID-}" ]; then
guess_specfile_from_tree
else
guess_specfile_from_checkout
fi
fi
if [ -z "$specfile" ]; then
if [ -n "${TREE_ID-}" ]; then
guess_specfile_from_checkout
else
guess_specfile_from_tree
fi
fi
if [ -z "$specfile" ]; then
specfile="$(set +f; find_specfile_in_cwd '' *)" ||:
fi
}
run_command()
{
verbose "Executing: $*"
"$@"
}
subst_key()
{
local var_name="$1"; shift
local key_name="$1"; shift
local key_value="$1"; shift
local var_value qkey_name qkey_value
set_var_value var_value "$var_name"
if [ "$var_value" != "${var_value#*"$key_name"*}" ]; then
check_filename "$key_name" "$key_value"
quote_sed_regexp_variable qkey_name "$key_name"
quote_sed_regexp_variable qkey_value "$key_value"
var_value="$(printf %s "$var_value" |
sed "s/$qkey_name/$qkey_value/g")"
set_var_value "$var_name" var_value
fi
}
subst_key_in_vars()
{
local key_name="$1"; shift
local key_value="$1"; shift
local variable
for variable; do
subst_key "$variable" "$key_name" "$key_value"
done
}
subst_spec_keywords()
{
local name="$1"; shift
local version="$1"; shift
local release="$1"; shift
subst_key_in_vars '@name@' "$name" "$@"
subst_key_in_vars '@version@' "$version" "$@"
subst_key_in_vars '@release@' "$release" "$@"
}
subst_NVR_from_spec_file()
{
local spec="$1"; shift
local spec_name='' spec_version='' spec_release=''
if [ -n "$spec" ]; then
get_NVR_from_spec "$spec"
fi
[ -n "$spec_name" ] || spec_name="$pkg_name"
[ -n "$spec_version" ] || spec_version="$pkg_version"
[ -n "$spec_release" ] || spec_release="$pkg_release"
subst_spec_keywords "$spec_name" "$spec_version" "$spec_release" "$@"
}
gear_config_option()
{
local var_name="$1"; shift
local opt_name="$1"; shift
local def_val="$1"; shift
local subcmd gear_config_option_val
subcmd=".${PROG#gear-}"
[ "$subcmd" != '.gear' ] || subcmd=
gear_config_option_val="$(git config --get "gear$subcmd.$opt_name")" ||
gear_config_option_val="$def_val"
set_var_value "$var_name" gear_config_option_val
}
valid_string()
{
[ -z "$(printf %s "$1" |LANG=C tr -d '[:graph:]')" ]
}
fi #__included_gear_sh_functions