-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·124 lines (100 loc) · 3.44 KB
/
test.sh
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
# Handle updates and upgrades.
__should_update=0
# __should_update=0: No update needed or versions are the same.
# __should_update=1: Higher patch version.
# __should_update=2: Higher minor, major, or lexicographically greater suffix.
compare_versions() {
current=$1
new=$2
echo "comparing ${current} and ${new}"
# Remove leading 'v' if present
ver_current="${current#v}"
ver_new="${new#v}"
# Extract major and minor from the first two dot-separated fields
major_current=$(echo "$ver_current" | cut -d. -f1)
minor_current=$(echo "$ver_current" | cut -d. -f2)
major_new=$(echo "$ver_new" | cut -d. -f1)
minor_new=$(echo "$ver_new" | cut -d. -f2)
# For the third field, we might have patch plus possible suffix
patch_part_current=$(echo "$ver_current" | cut -d. -f3)
patch_part_new=$(echo "$ver_new" | cut -d. -f3)
# Now split the patch from the suffix at the first dash
patch_current="${patch_part_current%%-*}"
suffix_current="${patch_part_current#*-}"
if [ "$suffix_current" = "$patch_part_current" ]; then
# Means there was no dash
suffix_current=""
fi
patch_new="${patch_part_new%%-*}"
suffix_new="${patch_part_new#*-}"
if [ "$suffix_new" = "$patch_part_new" ]; then
suffix_new=""
fi
# Convert major/minor/patch to numbers
major_current=$((major_current))
minor_current=$((minor_current))
patch_current=$((patch_current))
major_new=$((major_new))
minor_new=$((minor_new))
patch_new=$((patch_new))
# Compare major/minor/patch
if [ "$major_new" -gt "$major_current" ]; then
__should_update=2
return
elif [ "$major_new" -lt "$major_current" ]; then
__should_update=0
return
fi
if [ "$minor_new" -gt "$minor_current" ]; then
__should_update=2
return
elif [ "$minor_new" -lt "$minor_current" ]; then
__should_update=0
return
fi
if [ "$patch_new" -gt "$patch_current" ]; then
__should_update=2
return
elif [ "$patch_new" -lt "$patch_current" ]; then
__should_update=0
return
fi
# If major/minor/patch are identical, check suffix difference
if [ "$suffix_current" != "$suffix_new" ]; then
__should_update=1
return
fi
# Otherwise, exact match
__should_update=0
}
# Example usage
compare_versions "v6.0.0" "v6.0.1"
echo $__should_update # should be 2
echo "expected: 2\n"
compare_versions "v6.0.0" "v6.0.0-hotfix"
echo $__should_update # should be 1
echo "expected: 1\n"
compare_versions "v6.0.0-hotfix" "v6.0.0-hotfix-3"
echo $__should_update # should be 1
echo "expected: 1\n"
compare_versions "v6.0.0-hotfix-3" "v6.0.1"
echo $__should_update # should be 2
echo "expected: 2\n"
compare_versions "v6.0.1" "v6.0.1-hotfix-rpc-9"
echo $__should_update # should be 1
echo "expected: 1\n"
compare_versions "v6.0.1-hotfix-rpc-9" "v6.0.1-hotfix-rpc-10"
echo $__should_update # should be 1
echo "expected: 1\n"
compare_versions "v6.0.1-hotfix-rpc-9" "v6.0.1-hotfix-rpc-13"
echo $__should_update # should be 1
echo "expected: 1\n"
compare_versions "v6.0.1-hotfix-rpc-10" "v6.0.2"
echo $__should_update # should be 2
echo "expected: 2\n"
compare_versions "v6.0.2" "v6.0.3"
echo $__should_update # should be 2
echo "expected: 2\n"
echo "# __should_update=0: No update needed or versions are the same.
# __should_update=1: Higher patch version.
# __should_update=2: Higher minor or major version."