-
Notifications
You must be signed in to change notification settings - Fork 0
/
revision.go
116 lines (102 loc) · 3.21 KB
/
revision.go
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
/*
Copyright 2024 Nokia.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pkgid
import (
"strconv"
"golang.org/x/mod/semver"
)
const (
NoRevision = "v0"
Anyrevision = "*"
)
// LatestRevisionNumber computes the latest revision number of a given list.
// This function only understands strict versioning format, e.g. v1, v2, etc. It will
// ignore all revision numbers it finds that do not adhere to this format.
// If there are no published revisions (in the recognized format), the revision
// number returned here will be "v0".
func LatestRevisionNumber(revs []string) string {
latestRev := NoRevision
for _, currentRev := range revs {
if !semver.IsValid(currentRev) {
// ignore this revision
continue
}
// collect the major version. i.e. if we find that the latest published
// version is v3.1.1, we will end up returning v4
currentRev = semver.Major(currentRev)
switch cmp := semver.Compare(currentRev, latestRev); {
case cmp == 0:
// Same revision.
case cmp < 0:
// current < latest; no change
case cmp > 0:
// current > latest; update latest
latestRev = currentRev
}
}
return latestRev
}
func IsRevLater(currentRev, latestRev string) bool {
if !semver.IsValid(currentRev) {
// ignore this revision
return false
}
// collect the major version. i.e. if we find that the latest published
// version is v3.1.1, we will end up returning v4
currentRev = semver.Major(currentRev)
switch cmp := semver.Compare(currentRev, latestRev); {
case cmp == 0:
// Same revision.
return false
case cmp < 0:
// current < latest; no change
return false
case cmp > 0:
// current > latest; update latest
return true
}
return false
}
// NextRevisionNumber computes the next revision number as the latest revision number + 1.
// This function only understands strict versioning format, e.g. v1, v2, etc. It will
// ignore all revision numbers it finds that do not adhere to this format.
// If there are no published revisions (in the recognized format), the revision
// number returned here will be "v1".
func NextRevisionNumber(revs []string) (string, error) {
latestRev := NoRevision
for _, currentRev := range revs {
if !semver.IsValid(currentRev) {
// ignore this revision
continue
}
// collect the major version. i.e. if we find that the latest published
// version is v3.1.1, we will end up returning v4
currentRev = semver.Major(currentRev)
switch cmp := semver.Compare(currentRev, latestRev); {
case cmp == 0:
// Same revision.
case cmp < 0:
// current < latest; no change
case cmp > 0:
// current > latest; update latest
latestRev = currentRev
}
}
i, err := strconv.Atoi(latestRev[1:])
if err != nil {
return "", err
}
i++
next := "v" + strconv.Itoa(i)
return next, nil
}