constraints.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package semver
  2. import (
  3. "errors"
  4. "fmt"
  5. "regexp"
  6. "strings"
  7. )
  8. // Constraints is one or more constraint that a semantic version can be
  9. // checked against.
  10. type Constraints struct {
  11. constraints [][]*constraint
  12. }
  13. // NewConstraint returns a Constraints instance that a Version instance can
  14. // be checked against. If there is a parse error it will be returned.
  15. func NewConstraint(c string) (*Constraints, error) {
  16. // Rewrite - ranges into a comparison operation.
  17. c = rewriteRange(c)
  18. ors := strings.Split(c, "||")
  19. or := make([][]*constraint, len(ors))
  20. for k, v := range ors {
  21. cs := strings.Split(v, ",")
  22. result := make([]*constraint, len(cs))
  23. for i, s := range cs {
  24. pc, err := parseConstraint(s)
  25. if err != nil {
  26. return nil, err
  27. }
  28. result[i] = pc
  29. }
  30. or[k] = result
  31. }
  32. o := &Constraints{constraints: or}
  33. return o, nil
  34. }
  35. // Check tests if a version satisfies the constraints.
  36. func (cs Constraints) Check(v *Version) bool {
  37. // loop over the ORs and check the inner ANDs
  38. for _, o := range cs.constraints {
  39. joy := true
  40. for _, c := range o {
  41. if !c.check(v) {
  42. joy = false
  43. break
  44. }
  45. }
  46. if joy {
  47. return true
  48. }
  49. }
  50. return false
  51. }
  52. // Validate checks if a version satisfies a constraint. If not a slice of
  53. // reasons for the failure are returned in addition to a bool.
  54. func (cs Constraints) Validate(v *Version) (bool, []error) {
  55. // loop over the ORs and check the inner ANDs
  56. var e []error
  57. // Capture the prerelease message only once. When it happens the first time
  58. // this var is marked
  59. var prerelesase bool
  60. for _, o := range cs.constraints {
  61. joy := true
  62. for _, c := range o {
  63. // Before running the check handle the case there the version is
  64. // a prerelease and the check is not searching for prereleases.
  65. if c.con.pre == "" && v.pre != "" {
  66. if !prerelesase {
  67. em := fmt.Errorf("%s is a prerelease version and the constraint is only looking for release versions", v)
  68. e = append(e, em)
  69. prerelesase = true
  70. }
  71. joy = false
  72. } else {
  73. if !c.check(v) {
  74. em := fmt.Errorf(c.msg, v, c.orig)
  75. e = append(e, em)
  76. joy = false
  77. }
  78. }
  79. }
  80. if joy {
  81. return true, []error{}
  82. }
  83. }
  84. return false, e
  85. }
  86. var constraintOps map[string]cfunc
  87. var constraintMsg map[string]string
  88. var constraintRegex *regexp.Regexp
  89. func init() {
  90. constraintOps = map[string]cfunc{
  91. "": constraintTildeOrEqual,
  92. "=": constraintTildeOrEqual,
  93. "!=": constraintNotEqual,
  94. ">": constraintGreaterThan,
  95. "<": constraintLessThan,
  96. ">=": constraintGreaterThanEqual,
  97. "=>": constraintGreaterThanEqual,
  98. "<=": constraintLessThanEqual,
  99. "=<": constraintLessThanEqual,
  100. "~": constraintTilde,
  101. "~>": constraintTilde,
  102. "^": constraintCaret,
  103. }
  104. constraintMsg = map[string]string{
  105. "": "%s is not equal to %s",
  106. "=": "%s is not equal to %s",
  107. "!=": "%s is equal to %s",
  108. ">": "%s is less than or equal to %s",
  109. "<": "%s is greater than or equal to %s",
  110. ">=": "%s is less than %s",
  111. "=>": "%s is less than %s",
  112. "<=": "%s is greater than %s",
  113. "=<": "%s is greater than %s",
  114. "~": "%s does not have same major and minor version as %s",
  115. "~>": "%s does not have same major and minor version as %s",
  116. "^": "%s does not have same major version as %s",
  117. }
  118. ops := make([]string, 0, len(constraintOps))
  119. for k := range constraintOps {
  120. ops = append(ops, regexp.QuoteMeta(k))
  121. }
  122. constraintRegex = regexp.MustCompile(fmt.Sprintf(
  123. `^\s*(%s)\s*(%s)\s*$`,
  124. strings.Join(ops, "|"),
  125. cvRegex))
  126. constraintRangeRegex = regexp.MustCompile(fmt.Sprintf(
  127. `\s*(%s)\s+-\s+(%s)\s*`,
  128. cvRegex, cvRegex))
  129. }
  130. // An individual constraint
  131. type constraint struct {
  132. // The callback function for the restraint. It performs the logic for
  133. // the constraint.
  134. function cfunc
  135. msg string
  136. // The version used in the constraint check. For example, if a constraint
  137. // is '<= 2.0.0' the con a version instance representing 2.0.0.
  138. con *Version
  139. // The original parsed version (e.g., 4.x from != 4.x)
  140. orig string
  141. // When an x is used as part of the version (e.g., 1.x)
  142. minorDirty bool
  143. dirty bool
  144. patchDirty bool
  145. }
  146. // Check if a version meets the constraint
  147. func (c *constraint) check(v *Version) bool {
  148. return c.function(v, c)
  149. }
  150. type cfunc func(v *Version, c *constraint) bool
  151. func parseConstraint(c string) (*constraint, error) {
  152. m := constraintRegex.FindStringSubmatch(c)
  153. if m == nil {
  154. return nil, fmt.Errorf("improper constraint: %s", c)
  155. }
  156. ver := m[2]
  157. orig := ver
  158. minorDirty := false
  159. patchDirty := false
  160. dirty := false
  161. if isX(m[3]) {
  162. ver = "0.0.0"
  163. dirty = true
  164. } else if isX(strings.TrimPrefix(m[4], ".")) || m[4] == "" {
  165. minorDirty = true
  166. dirty = true
  167. ver = fmt.Sprintf("%s.0.0%s", m[3], m[6])
  168. } else if isX(strings.TrimPrefix(m[5], ".")) {
  169. dirty = true
  170. patchDirty = true
  171. ver = fmt.Sprintf("%s%s.0%s", m[3], m[4], m[6])
  172. }
  173. con, err := NewVersion(ver)
  174. if err != nil {
  175. // The constraintRegex should catch any regex parsing errors. So,
  176. // we should never get here.
  177. return nil, errors.New("constraint Parser Error")
  178. }
  179. cs := &constraint{
  180. function: constraintOps[m[1]],
  181. msg: constraintMsg[m[1]],
  182. con: con,
  183. orig: orig,
  184. minorDirty: minorDirty,
  185. patchDirty: patchDirty,
  186. dirty: dirty,
  187. }
  188. return cs, nil
  189. }
  190. // Constraint functions
  191. func constraintNotEqual(v *Version, c *constraint) bool {
  192. if c.dirty {
  193. // If there is a pre-release on the version but the constraint isn't looking
  194. // for them assume that pre-releases are not compatible. See issue 21 for
  195. // more details.
  196. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  197. return false
  198. }
  199. if c.con.Major() != v.Major() {
  200. return true
  201. }
  202. if c.con.Minor() != v.Minor() && !c.minorDirty {
  203. return true
  204. } else if c.minorDirty {
  205. return false
  206. }
  207. return false
  208. }
  209. return !v.Equal(c.con)
  210. }
  211. func constraintGreaterThan(v *Version, c *constraint) bool {
  212. // If there is a pre-release on the version but the constraint isn't looking
  213. // for them assume that pre-releases are not compatible. See issue 21 for
  214. // more details.
  215. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  216. return false
  217. }
  218. return v.Compare(c.con) == 1
  219. }
  220. func constraintLessThan(v *Version, c *constraint) bool {
  221. // If there is a pre-release on the version but the constraint isn't looking
  222. // for them assume that pre-releases are not compatible. See issue 21 for
  223. // more details.
  224. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  225. return false
  226. }
  227. if !c.dirty {
  228. return v.Compare(c.con) < 0
  229. }
  230. if v.Major() > c.con.Major() {
  231. return false
  232. } else if v.Minor() > c.con.Minor() && !c.minorDirty {
  233. return false
  234. }
  235. return true
  236. }
  237. func constraintGreaterThanEqual(v *Version, c *constraint) bool {
  238. // If there is a pre-release on the version but the constraint isn't looking
  239. // for them assume that pre-releases are not compatible. See issue 21 for
  240. // more details.
  241. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  242. return false
  243. }
  244. return v.Compare(c.con) >= 0
  245. }
  246. func constraintLessThanEqual(v *Version, c *constraint) bool {
  247. // If there is a pre-release on the version but the constraint isn't looking
  248. // for them assume that pre-releases are not compatible. See issue 21 for
  249. // more details.
  250. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  251. return false
  252. }
  253. if !c.dirty {
  254. return v.Compare(c.con) <= 0
  255. }
  256. if v.Major() > c.con.Major() {
  257. return false
  258. } else if v.Minor() > c.con.Minor() && !c.minorDirty {
  259. return false
  260. }
  261. return true
  262. }
  263. // ~*, ~>* --> >= 0.0.0 (any)
  264. // ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0, <3.0.0
  265. // ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0, <2.1.0
  266. // ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0, <1.3.0
  267. // ~1.2.3, ~>1.2.3 --> >=1.2.3, <1.3.0
  268. // ~1.2.0, ~>1.2.0 --> >=1.2.0, <1.3.0
  269. func constraintTilde(v *Version, c *constraint) bool {
  270. // If there is a pre-release on the version but the constraint isn't looking
  271. // for them assume that pre-releases are not compatible. See issue 21 for
  272. // more details.
  273. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  274. return false
  275. }
  276. if v.LessThan(c.con) {
  277. return false
  278. }
  279. // ~0.0.0 is a special case where all constraints are accepted. It's
  280. // equivalent to >= 0.0.0.
  281. if c.con.Major() == 0 && c.con.Minor() == 0 && c.con.Patch() == 0 &&
  282. !c.minorDirty && !c.patchDirty {
  283. return true
  284. }
  285. if v.Major() != c.con.Major() {
  286. return false
  287. }
  288. if v.Minor() != c.con.Minor() && !c.minorDirty {
  289. return false
  290. }
  291. return true
  292. }
  293. // When there is a .x (dirty) status it automatically opts in to ~. Otherwise
  294. // it's a straight =
  295. func constraintTildeOrEqual(v *Version, c *constraint) bool {
  296. // If there is a pre-release on the version but the constraint isn't looking
  297. // for them assume that pre-releases are not compatible. See issue 21 for
  298. // more details.
  299. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  300. return false
  301. }
  302. if c.dirty {
  303. c.msg = constraintMsg["~"]
  304. return constraintTilde(v, c)
  305. }
  306. return v.Equal(c.con)
  307. }
  308. // ^* --> (any)
  309. // ^2, ^2.x, ^2.x.x --> >=2.0.0, <3.0.0
  310. // ^2.0, ^2.0.x --> >=2.0.0, <3.0.0
  311. // ^1.2, ^1.2.x --> >=1.2.0, <2.0.0
  312. // ^1.2.3 --> >=1.2.3, <2.0.0
  313. // ^1.2.0 --> >=1.2.0, <2.0.0
  314. func constraintCaret(v *Version, c *constraint) bool {
  315. // If there is a pre-release on the version but the constraint isn't looking
  316. // for them assume that pre-releases are not compatible. See issue 21 for
  317. // more details.
  318. if v.Prerelease() != "" && c.con.Prerelease() == "" {
  319. return false
  320. }
  321. if v.LessThan(c.con) {
  322. return false
  323. }
  324. if v.Major() != c.con.Major() {
  325. return false
  326. }
  327. return true
  328. }
  329. var constraintRangeRegex *regexp.Regexp
  330. const cvRegex string = `v?([0-9|x|X|\*]+)(\.[0-9|x|X|\*]+)?(\.[0-9|x|X|\*]+)?` +
  331. `(-([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?` +
  332. `(\+([0-9A-Za-z\-]+(\.[0-9A-Za-z\-]+)*))?`
  333. func isX(x string) bool {
  334. switch x {
  335. case "x", "*", "X":
  336. return true
  337. default:
  338. return false
  339. }
  340. }
  341. func rewriteRange(i string) string {
  342. m := constraintRangeRegex.FindAllStringSubmatch(i, -1)
  343. if m == nil {
  344. return i
  345. }
  346. o := i
  347. for _, v := range m {
  348. t := fmt.Sprintf(">= %s, <= %s", v[1], v[11])
  349. o = strings.Replace(o, v[0], t, 1)
  350. }
  351. return o
  352. }