normalize.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package version
  2. import (
  3. "regexp"
  4. "strings"
  5. )
  6. var modifierRegex = `[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?`
  7. var regexpMasterLikeBranches = regexp.MustCompile(`^(?:dev-)?(?:master|trunk|default)$`)
  8. var regexpBranchNormalize = regexp.MustCompile(`(?i)^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$`)
  9. // Normalizes a version string to be able to perform comparisons on it
  10. //
  11. // Example:
  12. // version.Normalize("10.4.13-b")
  13. // Returns: 10.4.13.0-beta
  14. //
  15. func Normalize(version string) string {
  16. // ignore aliases and just assume the alias is required instead of the source
  17. result := RegFind(`^([^,\s]+) +as +([^,\s]+)$`, version)
  18. if result != nil {
  19. version = result[1]
  20. }
  21. // match master-like branches
  22. if regexpMasterLikeBranches.MatchString(strings.ToLower(version)) {
  23. return "9999999-dev"
  24. }
  25. if strings.HasPrefix(strings.ToLower(version), "dev-") {
  26. return "dev-" + version[4:len(version)]
  27. }
  28. index := 0
  29. // match classical versioning
  30. result = RegFind(`(?i)^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?`+modifierRegex+`$`, version)
  31. if result != nil {
  32. version = ""
  33. for _, val := range result[1:5] {
  34. if val != "" {
  35. version = version + val
  36. } else {
  37. version = version + ".0"
  38. }
  39. }
  40. index = 5
  41. } else {
  42. // match date-based versioning
  43. result = RegFind(`(?i)^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)`+modifierRegex+`$`, version)
  44. if result != nil {
  45. version = regexp.MustCompile(`\D`).ReplaceAllString(result[1], "-")
  46. index = 2
  47. }
  48. }
  49. if index != 0 {
  50. if result[index] != "" {
  51. if result[index] == "stable" {
  52. return version
  53. }
  54. version = version + "-" + expandStability(result[index])
  55. if result[index+1] != "" {
  56. version = version + result[index+1]
  57. }
  58. }
  59. if result[index+2] != "" {
  60. version = version + "-dev"
  61. }
  62. return version
  63. }
  64. result = RegFind(`(?i)(.*?)[.-]?dev$`, version)
  65. if result != nil {
  66. return normalizeBranch(result[1])
  67. }
  68. return version
  69. }
  70. func normalizeBranch(name string) string {
  71. name = strings.Trim(name, " ")
  72. if name == "master" || name == "trunk" || name == "default" {
  73. return Normalize(name)
  74. }
  75. replace := strings.NewReplacer("*", "9999999", "x", "9999999")
  76. matched := regexpBranchNormalize.FindAllStringSubmatch(name, -1)
  77. if matched != nil {
  78. name = ""
  79. for _, val := range matched[0][1:5] {
  80. if val != "" {
  81. name = name + replace.Replace(val)
  82. } else {
  83. name = name + ".9999999"
  84. }
  85. }
  86. return name + "-dev"
  87. }
  88. if strings.HasSuffix(strings.ToLower(name), "-dev") {
  89. return name
  90. }
  91. return "dev-" + name
  92. }