-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo-utils-funcs.pl
86 lines (82 loc) · 2.07 KB
/
repo-utils-funcs.pl
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
=head2 compare_version_numbers(ver1, ver2)
Compares to version "number" strings, and returns -1 if ver1 is older than ver2,
0 if they are equal, or 1 if ver1 is newer than ver2.
=cut
sub compare_version_numbers
{
my ($ver1, $ver2) = @_;
my @sp1 = split(/[\.\-\+\~\_]/, $ver1);
my @sp2 = split(/[\.\-\+\~\_]/, $ver2);
my $tmp;
for(my $i=0; $i<@sp1 || $i<@sp2; $i++) {
my $v1 = $sp1[$i];
my $v2 = $sp2[$i];
my $comp;
$v1 =~ s/^ubuntu//g;
$v2 =~ s/^ubuntu//g;
if ($v1 =~ /^\d+$/ && $v2 =~ /^\d+$/) {
# Numeric only
# ie. 5 vs 7
$comp = $v1 <=> $v2;
}
elsif ($v1 =~ /^(\d+[^0-9]+)(\d+)$/ && ($tmp = $1) &&
$v2 =~ /^(\d+[^0-9]+)(\d+)$/ &&
$tmp eq $1) {
# Numeric followed by a string followed by a number, where
# the first two components are the same
# ie. 4ubuntu8 vs 4ubuntu10
$v1 =~ /^(\d+[^0-9]+)(\d+)$/;
my $num1 = $2;
$v2 =~ /^(\d+[^0-9]+)(\d+)$/;
my $num2 = $2;
$comp = $num1 <=> $num2;
}
elsif ($v1 =~ /^\d+\S*$/ && $v2 =~ /^\d+\S*$/) {
# Numeric followed by string
# ie. 6redhat vs 8redhat
$v1 =~ /^(\d+)(\S*)$/;
my ($v1n, $v1s) = ($1, $2);
$v2 =~ /^(\d+)(\S*)$/;
my ($v2n, $v2s) = ($1, $2);
$comp = $v1n <=> $v2n;
if (!$comp) {
# X.rcN is always older than X
if ($v1s =~ /^rc\d+$/i && $v2s =~ /^\d*$/) {
$comp = -1;
}
elsif ($v1s =~ /^\d*$/ && $v2s =~ /^rc\d+$/i) {
$comp = 1;
}
else {
$comp = $v1s cmp $v2s;
}
}
}
elsif ($v1 =~ /^(\S+[^0-9]+)(\d+)$/ && ($tmp = $1) &&
$v2 =~ /^(\S+[^0-9]+)(\d+)$/ &&
$tmp eq $1) {
# String followed by a number, where the strings are the same
# ie. centos7 vs centos8
$v1 =~ /^(\S+[^0-9]+)(\d+)$/;
my $num1 = $2;
$v2 =~ /^(\S+[^0-9]+)(\d+)$/;
my $num2 = $2;
$comp = $num1 <=> $num2;
}
elsif ($v1 =~ /^\d+$/ && $v2 !~ /^\d+$/) {
# Numeric compared to non-numeric - numeric is always higher
$comp = 1;
}
elsif ($v1 !~ /^\d+$/ && $v2 =~ /^\d+$/) {
# Non-numeric compared to numeric - numeric is always higher
$comp = -1;
}
else {
# String compare only
$comp = $v1 cmp $v2;
}
return $comp if ($comp);
}
return 0;
}
1;