Debian Codegraph @

docs/semver.md

raw
1 # Semantic Version Functions
2
3 Some version schemes are easily parseable and comparable. Sprig provides functions
4 for working with [SemVer 2](http://semver.org) versions.
5
6 ## semver
7
8 The `semver` function parses a string into a Semantic Version:
9
10 ```
11 $version := semver "1.2.3-alpha.1+123"
12 ```
13
14 _If the parser fails, it will cause template execution to halt with an error._
15
16 At this point, `$version` is a pointer to a `Version` object with the following
17 properties:
18
19 - `$version.Major`: The major number (`1` above)
20 - `$version.Minor`: The minor number (`2` above)
21 - `$version.Patch`: The patch number (`3` above)
22 - `$version.Prerelease`: The prerelease (`alpha.1` above)
23 - `$version.Metadata`: The build metadata (`123` above)
24 - `$version.Original`: The original version as a string
25
26 Additionally, you can compare a `Version` to another `version` using the `Compare`
27 function:
28
29 ```
30 semver "1.4.3" | (semver "1.2.3").Compare
31 ```
32
33 The above will return `-1`.
34
35 The return values are:
36
37 - `-1` if the given semver is greater than the semver whose `Compare` method was called
38 - `1` if the version who's `Compare` function was called is greater.
39 - `0` if they are the same version
40
41 (Note that in SemVer, the `Metadata` field is not compared during version
42 comparison operations.)
43
44 ## semverCompare
45
46 A more robust comparison function is provided as `semverCompare`. It returns `true` if
47 the constraint matches, or `false` if it does not match. This version supports version ranges:
48
49 - `semverCompare "1.2.3" "1.2.3"` checks for an exact match
50 - `semverCompare "^1.2.0" "1.2.3"` checks that the major and minor versions match, and that the patch
51 number of the second version is _greater than or equal to_ the first parameter.
52
53 The SemVer functions use the [Masterminds semver library](https://github.com/Masterminds/semver),
54 from the creators of Sprig.
55
56 ## Basic Comparisons
57
58 There are two elements to the comparisons. First, a comparison string is a list
59 of space or comma separated AND comparisons. These are then separated by || (OR)
60 comparisons. For example, `">= 1.2 < 3.0.0 || >= 4.2.3"` is looking for a
61 comparison that's greater than or equal to 1.2 and less than 3.0.0 or is
62 greater than or equal to 4.2.3.
63
64 The basic comparisons are:
65
66 - `=`: equal (aliased to no operator)
67 - `!=`: not equal
68 - `>`: greater than
69 - `<`: less than
70 - `>=`: greater than or equal to
71 - `<=`: less than or equal to
72
73 _Note, according to the Semantic Version specification pre-releases may not be
74 API compliant with their release counterpart. It says,_
75
76 ## Working With Prerelease Versions
77
78 Pre-releases, for those not familiar with them, are used for software releases
79 prior to stable or generally available releases. Examples of prereleases include
80 development, alpha, beta, and release candidate releases. A prerelease may be
81 a version such as `1.2.3-beta.1` while the stable release would be `1.2.3`. In the
82 order of precedence, prereleases come before their associated releases. In this
83 example `1.2.3-beta.1 < 1.2.3`.
84
85 According to the Semantic Version specification prereleases may not be
86 API compliant with their release counterpart. It says,
87
88 > A pre-release version indicates that the version is unstable and might not satisfy the intended compatibility requirements as denoted by its associated normal version.
89
90 SemVer comparisons using constraints without a prerelease comparator will skip
91 prerelease versions. For example, `>=1.2.3` will skip prereleases when looking
92 at a list of releases while `>=1.2.3-0` will evaluate and find prereleases.
93
94 The reason for the `0` as a pre-release version in the example comparison is
95 because pre-releases can only contain ASCII alphanumerics and hyphens (along with
96 `.` separators), per the spec. Sorting happens in ASCII sort order, again per the
97 spec. The lowest character is a `0` in ASCII sort order
98 (see an [ASCII Table](http://www.asciitable.com/))
99
100 Understanding ASCII sort ordering is important because A-Z comes before a-z. That
101 means `>=1.2.3-BETA` will return `1.2.3-alpha`. What you might expect from case
102 sensitivity doesn't apply here. This is due to ASCII sort ordering which is what
103 the spec specifies.
104
105 ## Hyphen Range Comparisons
106
107 There are multiple methods to handle ranges and the first is hyphens ranges.
108 These look like:
109
110 - `1.2 - 1.4.5` which is equivalent to `>= 1.2 <= 1.4.5`
111 - `2.3.4 - 4.5` which is equivalent to `>= 2.3.4 <= 4.5`
112
113 ## Wildcards In Comparisons
114
115 The `x`, `X`, and `*` characters can be used as a wildcard character. This works
116 for all comparison operators. When used on the `=` operator it falls
117 back to the patch level comparison (see tilde below). For example,
118
119 - `1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
120 - `>= 1.2.x` is equivalent to `>= 1.2.0`
121 - `<= 2.x` is equivalent to `< 3`
122 - `*` is equivalent to `>= 0.0.0`
123
124 ## Tilde Range Comparisons (Patch)
125
126 The tilde (`~`) comparison operator is for patch level ranges when a minor
127 version is specified and major level changes when the minor number is missing.
128 For example,
129
130 - `~1.2.3` is equivalent to `>= 1.2.3, < 1.3.0`
131 - `~1` is equivalent to `>= 1, < 2`
132 - `~2.3` is equivalent to `>= 2.3, < 2.4`
133 - `~1.2.x` is equivalent to `>= 1.2.0, < 1.3.0`
134 - `~1.x` is equivalent to `>= 1, < 2`
135
136 ## Caret Range Comparisons (Major)
137
138 The caret (`^`) comparison operator is for major level changes once a stable
139 (1.0.0) release has occurred. Prior to a 1.0.0 release the minor versions acts
140 as the API stability level. This is useful when comparisons of API versions as a
141 major change is API breaking. For example,
142
143 - `^1.2.3` is equivalent to `>= 1.2.3, < 2.0.0`
144 - `^1.2.x` is equivalent to `>= 1.2.0, < 2.0.0`
145 - `^2.3` is equivalent to `>= 2.3, < 3`
146 - `^2.x` is equivalent to `>= 2.0.0, < 3`
147 - `^0.2.3` is equivalent to `>=0.2.3 <0.3.0`
148 - `^0.2` is equivalent to `>=0.2.0 <0.3.0`
149 - `^0.0.3` is equivalent to `>=0.0.3 <0.0.4`
150 - `^0.0` is equivalent to `>=0.0.0 <0.1.0`
151 - `^0` is equivalent to `>=0.0.0 <1.0.0`