-
Notifications
You must be signed in to change notification settings - Fork 19
/
version_helper.go
37 lines (30 loc) · 1.1 KB
/
version_helper.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
package depgraph
import (
"regexp"
"github.com/blang/semver"
)
var (
// Regular expressions according to https://golang.org/cmd/go/#hdr-Pseudo_versions.
versionRE = regexp.MustCompile(`^v?(\d+\.\d+\.\d+)(?:-(.*))?$`)
pseudoVersionRE = regexp.MustCompile(`^(?:(?:.*.)?0.)?(\d{14})-[0-9a-f]{12}$`)
)
func moduleMoreRecentThan(lhs string, rhs string) bool {
lhsParsed := versionRE.FindStringSubmatch(lhs)
rhsParsed := versionRE.FindStringSubmatch(rhs)
if len(lhsParsed) == 0 || len(rhsParsed) == 0 {
return len(lhsParsed) != 0
}
lhsSemVer := semver.MustParse(lhsParsed[1])
rhsSemVer := semver.MustParse(rhsParsed[1])
if lhsSemVer.GT(rhsSemVer) || lhsSemVer.LT(rhsSemVer) {
return lhsSemVer.GT(rhsSemVer)
}
if len(lhsParsed[2]) == 0 || len(rhsParsed[2]) == 0 {
// At least one of the version is a release so this comes down to whether it is the LHS.
return len(lhsParsed[2]) == 0
}
// We are comparing two pre-release versions.
pseudoLHS := pseudoVersionRE.FindStringSubmatch(lhsParsed[2])
pseudoRHS := pseudoVersionRE.FindStringSubmatch(rhsParsed[2])
return pseudoLHS[1] > pseudoRHS[1]
}