-
Notifications
You must be signed in to change notification settings - Fork 2
/
selective-checkout
executable file
·1266 lines (1176 loc) · 42.4 KB
/
selective-checkout
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# This scriptlet enhances the pull step that will only build
# development snapshots snaps if the latest tagged release has been
# promoted to the stable channel. This ensures that there's always
# a revision of the stable release snap available in the edge channel
# for the publisher to promote to stable as currently the build
# infrastructure only supports build on code push (but not new tagged
# releases) at this time.
# https://forum.snapcraft.io/t/selective-checkout-check-out-the-tagged-release-revision-if-it-isnt-promoted-to-the-stable-channel/10617
# 林博仁(Buo-ren, Lin) <Buo.Ren.Lin@gmail.com> © 2020
set \
-o errexit \
-o errtrace \
-o nounset \
-o pipefail
for required_command in \
curl \
cut \
head \
jq \
sed \
sort \
tail \
tr; do
if ! command -v "${required_command}" >/dev/null; then
printf -- \
'Fatal: This script requires the "%s" command in your command search PATHs.\n' \
"${required_command}" \
>&2
exit 1
fi
done
init(){
# checkout_mode:
# - snapshot: Build as-is
# - release: Build the latest tagged release
# tag_pattern_release: We assume all tags contains dots or underscores release tags
local \
checkout_mode \
flag_append_packaging_version=false \
flag_dry_run=false \
flag_debug_tracing=false \
flag_force_snapshot=false \
flag_force_stable=false \
packaging_revision \
postfix_dirty_marker_packaging=-d \
postfix_dirty_marker_upstream=-dirty \
tag_pattern_beta='-beta[[:digit:]]+$' \
tag_pattern_release='.*[._].*' \
tag_pattern_release_candidate='-rc[[:digit:]]+$' \
tag_pattern_stable \
tag_prefix_release=v \
revision_minimal_length_packaging=4 \
revision_minimal_length_upstream=7 \
snap_version \
snap_version_postfix_seperator=+ \
upstream_version
while true; do
if test "${#}" -eq 0; then
break
else
case "${1}" in
# Append packaging revision after snap version
--append-packaging-revision)
flag_append_packaging_version=true
;;
--beta-tag-pattern*)
if test "${1}" != --beta-tag-pattern; then
tag_pattern_beta="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --beta-tag-pattern requires one argument.\n' \
>&2
exit 1
fi
tag_pattern_beta="${2}"
shift 1
fi
;;
# Enable execution tracing
--debug)
flag_debug_tracing=true
;;
# Don't run snapcraftctl for testing purpose
--dry-run)
flag_dry_run=true
;;
# Force building development snapshot regardless the status of the snap
--force-snapshot)
flag_force_snapshot=true
;;
--force-stable)
flag_force_stable=true
;;
--packaging-dirty-marker-postfix*)
if test "${1}" != --packaging-dirty-marker-postfix; then
postfix_dirty_marker_packaging="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --packaging-dirty-marker-postfix requires one argument.\n' \
>&2
exit 1
fi
postfix_dirty_marker_packaging="${2}"
shift 1
fi
;;
--packaging-revision-minimal-length*)
if test "${1}" != --packaging-revision-minimal-length; then
revision_minimal_length_packaging="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --packaging-revision-minimal-length requires one argument.\n' \
>&2
exit 1
fi
revision_minimal_length_packaging="${2}"
shift 1
fi
;;
--release-candidate-tag-pattern*)
if test "${1}" != --release-candidate-tag-pattern; then
tag_pattern_release_candidate="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --release-candidate-tag-pattern requires one argument.\n' \
>&2
exit 1
fi
tag_pattern_release_candidate="${2}"
shift 1
fi
;;
--release-tag-pattern*)
if test "${1}" != --release-tag-pattern; then
tag_pattern_release="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --release-tag-pattern requires one argument.\n' \
>&2
exit 1
fi
tag_pattern_release="${2}"
shift 1
fi
;;
# Set the prefix for all release tags(default: `v`), the prefix will be stripped from snap version string
--release-tag-prefix*)
if test "${1}" != --release-tag-prefix; then
tag_prefix_release="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --release-tag-prefix requires one argument.\n' \
>&2
exit 1
fi
tag_prefix_release="${2}"
shift 1
fi
;;
# Set the seperator for the postfixed string in the snap version string
# the postfixed string will be stripped before comparing with the stripped
# uptream release version
--snap-postfix-seperator*)
if test "${1}" != --snap-postfix-seperator; then
snap_version_postfix_seperator="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --snap-postfix-seperator requires one argument.\n' \
>&2
exit 1
fi
snap_version_postfix_seperator="${2}"
shift 1
fi
;;
--stable-tag-pattern*)
if test "${1}" != --stable-tag-pattern; then
tag_pattern_stable="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --stable-tag-pattern requires one argument.\n' \
>&2
exit 1
fi
tag_pattern_stable="${2}"
shift 1
fi
;;
--upstream-dirty-marker-postfix*)
if test "${1}" != --upstream-dirty-marker-postfix; then
postfix_dirty_marker_upstream="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --upstream-dirty-marker-postfix requires one argument.\n' \
>&2
exit 1
fi
postfix_dirty_marker_upstream="${2}"
shift 1
fi
;;
--upstream-revision-minimal-length*)
if test "${1}" != --upstream-revision-minimal-length; then
revision_minimal_length_upstream="$(
cut \
--delimiter== \
--fields=2 \
<<< "${1}"
)"
else
if test "${#}" -eq 0; then
printf -- \
'selective-checkout: Error: --upstream-revision-minimal-length requires one argument.\n' \
>&2
exit 1
fi
revision_minimal_length_upstream="${2}"
shift 1
fi
;;
*)
printf -- \
'selective-checkout: Error: Invalid command-line argument.\n' \
>&2
exit 1
;;
esac
shift 1
fi
done
if test "${flag_debug_tracing}" = true; then
set -o xtrace
fi
vcs_check_runtime_dependencies \
"${PWD}"
if test "${flag_force_snapshot}" = true; then
printf -- 'selective-checkout: Force building development snapshots\n' >&2
checkout_mode=snapshot
elif test "$(vcs_detect "${PWD}")" = not_found; then
printf -- 'selective-checkout: Build from source archive\n' >&2
checkout_mode=snapshot
elif vcs_is_dirty "${PWD}"; then
# If tracked files are modified
# or staging area not empty
printf -- 'selective-checkout: Working tree is dirty, building development snapshot with additional changes\n' >&2
checkout_mode=snapshot
elif ! \
vcs_has_release_tags \
"${PWD}" \
"${tag_pattern_release}"; then
printf -- \
'Warning: No release tags found, assuming building from development snapshots.\n' \
1>&2
checkout_mode=snapshot
else
local \
all_release_tags \
release_type_to_build=development-snapshot \
flag_version_mismatch_stable=false \
flag_version_mismatch_beta=false \
flag_version_mismatch_release_candidate=false \
last_beta_tag \
last_beta_version \
last_beta_version_on_the_snap_store \
selected_release_tag \
selected_release_version \
selected_snap_channel_version
local -A \
map_of_normalized_version_to_tag \
map_of_release_type_to_snap_channel
map_of_release_type_to_snap_channel=(
[stable]=stable
[release-candidate]=candidate
[beta]=beta
[development-snapshot]=edge
)
if ! test -v SNAPCRAFT_PROJECT_NAME; then
printf -- \
"selective-checkout: Error: This script requires SNAPCRAFT_PROJECT_NAME environment variable set to the snap's identifier.\\n" \
>&2
exit 1
fi
all_release_tags="$(
vcs_query_release_tags \
"${PWD}" \
"${tag_pattern_release}"
)"
local normalized_release_version
for tag in ${all_release_tags}; do \
normalized_release_version="$(
normalize_version <<< "${tag}"
)"
map_of_normalized_version_to_tag[${normalized_release_version}]="${tag}"
done
unset \
normalized_release_version \
tag
local stable_pattern_grep_matching_option
if test -v tag_pattern_stable; then
stable_pattern_grep_matching_option=
else
stable_pattern_grep_matching_option=--invert-match
tag_pattern_stable="${tag_pattern_beta}|${tag_pattern_release_candidate}"
fi
if echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
${stable_pattern_grep_matching_option} \
--quiet \
--regexp="(${tag_pattern_stable})"; then
local \
last_stable_tag \
last_stable_version \
last_stable_version_on_the_snap_store
last_stable_version="$(
echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
${stable_pattern_grep_matching_option} \
--regexp="(${tag_pattern_stable})" \
| sort --version-sort \
| tail --lines=1
)"
last_stable_tag="${map_of_normalized_version_to_tag[${last_stable_version}]}"
last_stable_version_on_the_snap_store="$(
snap_query_version \
"${SNAPCRAFT_PROJECT_NAME}" \
stable \
"${snap_version_postfix_seperator}"
)"
if [ "${last_stable_version}" != "${last_stable_version_on_the_snap_store}" ]; then
flag_version_mismatch_stable=true
fi
else
if test "${flag_force_stable}" = true; then
printf -- \
'selective-checkout: Error: --force-stable specified, but no stable release tags are found.\n' \
>&2
exit 1
fi
fi
unset \
stable_pattern_grep_matching_option \
tag_pattern_stable
if echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
--quiet \
--regexp="${tag_pattern_release_candidate}"; then
local \
last_release_candidate_tag \
last_release_candidate_version \
last_release_candidate_version_on_the_snap_store
last_release_candidate_version="$(
echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
--regexp="${tag_pattern_release_candidate}" \
| sort --version-sort \
| tail --lines=1
)"
last_release_candidate_tag="${map_of_normalized_version_to_tag[${last_release_candidate_version}]}"
last_release_candidate_version_on_the_snap_store="$(
snap_query_version \
"${SNAPCRAFT_PROJECT_NAME}" \
candidate \
"${snap_version_postfix_seperator}"
)"
if test "${last_release_candidate_version_on_the_snap_store}" != "${last_release_candidate_version}"; then
flag_version_mismatch_release_candidate=true
fi
fi
unset \
tag_pattern_release_candidate
if echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
--quiet \
--regexp="${tag_pattern_beta}"; then
local \
last_beta_tag \
last_beta_version \
last_beta_version_on_the_snap_store
last_beta_version="$(
echo -n "${!map_of_normalized_version_to_tag[@]}" \
| tr ' ' "\\n" \
| grep \
--extended-regexp \
--regexp="${tag_pattern_beta}" \
| sort --version-sort \
| tail --lines=1
)"
last_beta_tag="${map_of_normalized_version_to_tag[${last_beta_version}]}"
last_beta_version_on_the_snap_store="$(
snap_query_version \
"${SNAPCRAFT_PROJECT_NAME}" \
beta \
"${snap_version_postfix_seperator}"
)"
if test "${last_beta_version_on_the_snap_store}" != "${last_beta_version}"; then
flag_version_mismatch_beta=true
fi
fi
unset \
map_of_normalized_version_to_tag \
snap_version_postfix_seperator \
tag_pattern_beta
if test "${flag_version_mismatch_stable}" = true \
|| test "${flag_force_stable}" = true; then
release_type_to_build=stable
selected_release_tag="${last_stable_tag}"
selected_release_version="${last_stable_version}"
selected_snap_channel_version="${last_stable_version_on_the_snap_store}"
elif test "${flag_version_mismatch_release_candidate}" = true \
&& (
test -v last_stable_version \
&& version_former_is_greater_than_latter \
"${last_release_candidate_version}" \
"${last_stable_version}"
); then
release_type_to_build=release-candidate
selected_release_tag="${last_release_candidate_tag}"
selected_release_version="${last_release_candidate_version}"
selected_snap_channel_version="${last_release_candidate_version_on_the_snap_store}"
elif test "${flag_version_mismatch_beta}" = true \
&& (
test -v last_release_candidate_version \
&& version_former_is_greater_than_latter \
"${last_beta_version}" \
"${last_release_candidate_version}"
) \
&& (
test -v last_stable_version \
&& version_former_is_greater_than_latter \
"${last_release_candidate_version}" \
"${last_stable_version}"
); then
release_type_to_build=beta
selected_release_tag="${last_beta_tag}"
selected_release_version="${last_beta_version}"
selected_snap_channel_version="${last_beta_version_on_the_snap_store}"
else
release_type_to_build=development-snapshot
fi
unset \
flag_version_mismatch_stable \
flag_version_mismatch_beta \
flag_version_mismatch_release_candidate \
last_stable_tag \
last_stable_version \
last_stable_version_on_the_snap_store \
last_release_candidate_tag \
last_release_candidate_version \
last_release_candidate_version_on_the_snap_store \
last_beta_tag \
last_beta_version \
last_beta_version_on_the_snap_store
if test "${release_type_to_build}" != development-snapshot; then
printf -- \
"selective-checkout: The last tagged %s release(%s) hasn't been promoted to the %s channel(%s) on the Snap Store yet, checking out %s.\\n" \
"${release_type_to_build}" \
"${selected_release_version}" \
"${map_of_release_type_to_snap_channel["${release_type_to_build}"]}" \
"${selected_snap_channel_version}" \
"${selected_release_version}" \
>&2
checkout_mode=release
else
printf -- 'selective-checkout: Last tagged releases is all in their respective channels, building development snapshot\n' \
>&2
checkout_mode=snapshot
fi
unset \
all_release_tags \
map_of_release_type_to_snap_channel \
release_type_to_build \
selected_release_version \
selected_snap_channel_version
fi
unset \
tag_pattern_release
case "${checkout_mode}" in
snapshot)
: # do nothing
;;
release)
vcs_checkout_tag \
"${PWD}" \
"${selected_release_tag}"
;;
*)
printf -- 'Error: Invalid checkout_mode selected.\n' >&2
exit 1
;;
esac
unset \
checkout_mode \
selected_release_tag
upstream_version="$(
vcs_describe_version \
"${PWD}" \
"${revision_minimal_length_upstream}" \
"${postfix_dirty_marker_upstream}" \
| normalize_version
)"
if test "${flag_append_packaging_version}" = true; then
packaging_revision="$(
vcs_describe_revision \
"${SNAPCRAFT_PROJECT_DIR}" \
"${revision_minimal_length_packaging}" \
"${postfix_dirty_marker_packaging}"
)"
snap_version="${upstream_version}+pkg-${packaging_revision}"
unset \
packaging_revision \
postfix_dirty_marker_packaging \
revision_minimal_length_packaging
else
snap_version="${upstream_version}"
fi
unset \
postfix_dirty_marker_upstream \
revision_minimal_length_upstream \
tag_prefix_release \
upstream_version
printf -- 'selective-checkout: Snap version determined to be "%s".\n' \
"${snap_version}" >&2
if test "${flag_dry_run}" = false; then
snapcraftctl \
set-version \
"${snap_version}"
fi
exit 0
}
normalize_version(){
# This is not a simple substitution
# shellcheck disable=SC2001
sed "s#^${tag_prefix_release}##" \
| tr _ .
}
# Query snap version for specific channel, we use the Snap Store API
# http://api.snapcraft.io/docs/info.html#snap_info
snap_query_version(){
if test $# -ne 3; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
# positional parameters
local snap_identifier="${1}"; shift
local release_channel="${1}"; shift
local snap_version_postfix_seperator="${1}"; shift
local snap_revision_in_release_channel
snap_revision_in_release_channel="$(
curl \
--fail \
--silent \
--show-error \
--header 'Snap-Device-Series: 16' \
https://api.snapcraft.io/v2/snaps/info/"${snap_identifier}" \
| jq \
".\"channel-map\"[]
| select(.channel.name == \"${release_channel}\").revision" \
| sort \
--reverse \
| head \
--lines=1
)"
if test "${?}" != 0; then
printf -- \
'%s: Error: Unable to query the snap revision in the %s channel from the Snap Store.\n' \
"${FUNCNAME[0]}" \
"${release_channel}" \
1>&2
return 1
fi
local snap_version_in_release_channel
snap_version_in_release_channel="$(
curl \
--fail \
--silent \
--show-error \
--header 'Snap-Device-Series: 16' \
https://api.snapcraft.io/v2/snaps/info/"${snap_identifier}" \
| jq \
--raw-output \
".\"channel-map\"[]
| select(
.channel.name == \"${release_channel}\"
and .revision == ${snap_revision_in_release_channel}
).version" \
| cut \
--delimiter="${snap_version_postfix_seperator}" \
--fields=1
)"
if test "${?}" != 0; then
printf -- \
'%s: Error: Unable to query the snap version of the %s revision from the Snap Store.\n' \
"${FUNCNAME[0]}" \
"${snap_revision_in_release_channel}" \
1>&2
return 2
fi
printf %s "${snap_version_in_release_channel}"
}
# Determine which VCS is used
# FIXME: Allow specifying any node under the working directory, not just the root node
vcs_detect(){
if test $# -ne 1; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
local source_tree_root_dir="${1}"; shift 1
if test -e "${source_tree_root_dir}"/.git; then
printf git
elif test -e "${source_tree_root_dir}"/.hg; then
printf mercurial
elif test -e "${source_tree_root_dir}"/.svn; then
printf subversion
else
printf not_found
fi
}
# Ensure depending software is available before using them
vcs_check_runtime_dependencies(){
if test $# -ne 1; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
local -r source_tree_root_dir="${1}"; shift 1
case "$(vcs_detect "${source_tree_root_dir}")" in
git)
if ! command -v git &>/dev/null; then
# Markdown code markup is not Bash tilde expression
# shellcheck disable=SC2016
printf -- \
'%s: Error: `git` command not found in the command search PATHs.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
fi
;;
mercurial)
if ! command -v hg &>/dev/null; then
# Markdown code markup is not Bash tilde expression
# shellcheck disable=SC2016
printf -- \
'%s: Error: `hg` command not found in the command search PATHs.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
fi
;;
subversion)
if ! command -v svn &>/dev/null; then
# Markdown code markup is not Bash tilde expression
# shellcheck disable=SC2016
printf -- \
'%s: Error: `svn` command not found in the command search PATHs.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
fi
;;
*)
printf -- \
'%s: Warning: Unknown VCS type, assuming none.\n' \
"${FUNCNAME[0]}" \
>&2
return 0
;;
esac
}
vcs_is_dirty(){
if test $# -ne 1; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
local -r source_tree_root_dir="${1}"; shift 1
case "$(vcs_detect "${source_tree_root_dir}")" in
git)
# If tracked files are modified
# or staging area not empty
if ! \
git -C "${source_tree_root_dir}" diff \
--quiet \
|| ! \
git -C "${source_tree_root_dir}" diff \
--staged \
--quiet; then
return 0
else
return 1
fi
;;
mercurial)
# NOTE:
# The existence of untracked files is not consider dirty,
# imitating Git's `--dirty` option of the describe
# subcommand
if test -n "$(
hg --cwd "${source_tree_root_dir}" status \
--added \
--deleted \
--modified \
--removed
)"; then
return 0
else
return 1
fi
;;
subversion)
# NOTE: Is there any straightforward way to check this?
if test -n "$(
svn status -q
)"; then
return 0
else
return 1
fi
;;
*)
printf -- \
'%s: Warning: Unknown VCS type, assuming dirty.\n' \
"${FUNCNAME[0]}" \
>&2
return 0
;;
esac
}
vcs_has_release_tags() {
if test $# -ne 2; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
local -r source_tree_root_dir="${1}"; shift 1
# Pattern to match a release tag, in extended regular expression(ERE)
local -r tag_pattern_release="${1}"; shift 1
case "$(vcs_detect "${source_tree_root_dir}")" in
git)
if git -C "${source_tree_root_dir}" tag \
--list \
| grep \
--extended-regexp \
--quiet \
--regexp="${tag_pattern_release}"; then
return 0
else
return 1
fi
;;
mercurial)
if hg --cwd "${source_tree_root_dir}" tags \
| grep \
--extended-regexp \
--quiet \
--regexp="${tag_pattern_release}"; then
return 0
else
return 1
fi
;;
subversion)
local \
source_tree_root_url \
tags_dir_url
source_tree_root_url="$(
svn info \
--show-item url \
"${source_tree_root_dir}"
)"
# Supported source URLs:
# * /trunk
# * /tags/_tag_name_
case "${source_tree_root_url}" in
*/trunk)
# Strip trailing shortest matched pattern
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
tags_dir_url="${source_tree_root_url%/trunk}/tags"
;;
*/tags/*)
tags_dir_url="${source_tree_root_url%/*}"
;;
*)
printf -- \
'Warning: %s: Unsupported SVN source URL, assuming no release tags found.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
;;
esac
if svn list \
"${tags_dir_url}" \
| sed 's#/$##' \
| grep \
--extended-regexp \
--quiet \
--regexp="${tag_pattern_release}"; then
return 0
else
return 1
fi
;;
*)
printf -- \
'%s: Warning: Unknown VCS type, assuming no release tags found.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
;;
esac
}
vcs_query_release_tags(){
if test $# -ne 2; then
printf 'FATAL: %s: Parameter quantity mismatch.\n' "${FUNCNAME[0]}" >&2
exit 1
fi
local -r source_tree_root_dir="${1}"; shift 1
# Pattern to match a release tag, in extended regular expression(ERE)
local -r tag_pattern_release="${1}"; shift 1
case "$(vcs_detect "${source_tree_root_dir}")" in
git)
if git -C "${source_tree_root_dir}" tag \
--list \
| grep \
--extended-regexp \
--regexp="${tag_pattern_release}"; then
return 0
else
return 1
fi
;;
mercurial)
if hg --cwd "${source_tree_root_dir}" tags \
--quiet \
| grep \
--extended-regexp \
--regexp="${tag_pattern_release}"; then
return 0
else
return 1
fi
;;
subversion)
local \
source_tree_root_url \
tags_dir_url
source_tree_root_url="$(
svn info \
--show-item url \
"${source_tree_root_dir}"
)"
# Supported source URLs:
# * /trunk
# * /tags/_tag_name_
case "${source_tree_root_url}" in
*/trunk)
# Strip trailing shortest matched pattern
# https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html
tags_dir_url="${source_tree_root_url%/trunk}/tags"
;;
*/tags/*)
tags_dir_url="${source_tree_root_url%/*}"
;;
*)
printf -- \
'Warning: %s: Unsupported SVN source URL, assuming no release tags found.\n' \
"${FUNCNAME[0]}" \
>&2
return 1
;;
esac
if svn list \
"${tags_dir_url}" \
| sed 's#/$##' \