This repository has been archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
normalize.go
116 lines (92 loc) · 2.54 KB
/
normalize.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
package version
import (
"regexp"
"strings"
)
var modifierRegex = `[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?`
var regexpMasterLikeBranches = regexp.MustCompile(`^(?:dev-)?(?:master|trunk|default)$`)
var regexpBranchNormalize = regexp.MustCompile(`(?i)^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$`)
// Normalizes a version string to be able to perform comparisons on it
//
// Example:
// version.Normalize("10.4.13-b")
// Returns: 10.4.13.0-beta
//
func Normalize(version string) string {
// ignore aliases and just assume the alias is required instead of the source
result := RegFind(`^([^,\s]+) +as +([^,\s]+)$`, version)
if result != nil {
version = result[1]
}
// match master-like branches
if regexpMasterLikeBranches.MatchString(strings.ToLower(version)) {
return "9999999-dev"
}
if strings.HasPrefix(strings.ToLower(version), "dev-") {
return "dev-" + version[4:len(version)]
}
index := 0
// match classical versioning
result = RegFind(`(?i)^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?`+modifierRegex+`$`, version)
if result != nil {
version = ""
for _, val := range result[1:5] {
if val != "" {
version = version + val
} else {
version = version + ".0"
}
}
index = 5
} else {
// match date-based versioning
result = RegFind(`(?i)^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)`+modifierRegex+`$`, version)
if result != nil {
version = regexp.MustCompile(`\D`).ReplaceAllString(result[1], "-")
index = 2
}
}
if index != 0 {
if result[index] != "" {
if result[index] == "stable" {
return version
}
version = version + "-" + expandStability(result[index])
if result[index+1] != "" {
version = version + result[index+1]
}
}
if result[index+2] != "" {
version = version + "-dev"
}
return version
}
result = RegFind(`(?i)(.*?)[.-]?dev$`, version)
if result != nil {
return normalizeBranch(result[1])
}
return version
}
func normalizeBranch(name string) string {
name = strings.Trim(name, " ")
if name == "master" || name == "trunk" || name == "default" {
return Normalize(name)
}
replace := strings.NewReplacer("*", "9999999", "x", "9999999")
matched := regexpBranchNormalize.FindAllStringSubmatch(name, -1)
if matched != nil {
name = ""
for _, val := range matched[0][1:5] {
if val != "" {
name = name + replace.Replace(val)
} else {
name = name + ".9999999"
}
}
return name + "-dev"
}
if strings.HasSuffix(strings.ToLower(name), "-dev") {
return name
}
return "dev-" + name
}