parse.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package build
  7. import (
  8. "errors"
  9. "regexp"
  10. "strings"
  11. )
  12. // syncthing v1.1.4-rc.1+30-g6aaae618-dirty-crashrep "Erbium Earthworm" (go1.12.5 darwin-amd64) jb@kvin.kastelo.net 2019-05-23 16:08:14 UTC [foo, bar]
  13. // or, somewhere along the way the "+" in the version tag disappeared:
  14. // syncthing v1.23.7-dev.26.gdf7b56ae.dirty-stversionextra "Fermium Flea" (go1.20.5 darwin-arm64) jb@ok.kastelo.net 2023-07-12 06:55:26 UTC [Some Wrapper, purego, stnoupgrade]
  15. var (
  16. longVersionRE = regexp.MustCompile(`syncthing\s+(v[^\s]+)\s+"([^"]+)"\s\(([^\s]+)\s+([^-]+)-([^)]+)\)\s+([^\s]+)[^\[]*(?:\[(.+)\])?$`)
  17. gitExtraRE = regexp.MustCompile(`\.\d+\.g[0-9a-f]+`) // ".1.g6aaae618"
  18. gitExtraSepRE = regexp.MustCompile(`[.-]`) // dot or dash
  19. )
  20. type VersionParts struct {
  21. Version string // "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep"
  22. Tag string // "v1.1.4-rc.1"
  23. Commit string // "6aaae618", blank when absent
  24. Codename string // "Erbium Earthworm"
  25. Runtime string // "go1.12.5"
  26. GOOS string // "darwin"
  27. GOARCH string // "amd64"
  28. Builder string // "jb@kvin.kastelo.net"
  29. Extra []string // "foo", "bar"
  30. }
  31. func (v VersionParts) Environment() string {
  32. if v.Commit != "" {
  33. return "Development"
  34. }
  35. if strings.Contains(v.Tag, "-rc.") {
  36. return "Candidate"
  37. }
  38. if strings.Contains(v.Tag, "-") {
  39. return "Beta"
  40. }
  41. return "Stable"
  42. }
  43. func ParseVersion(line string) (VersionParts, error) {
  44. m := longVersionRE.FindStringSubmatch(line)
  45. if len(m) == 0 {
  46. return VersionParts{}, errors.New("unintelligeble version string")
  47. }
  48. v := VersionParts{
  49. Version: m[1],
  50. Codename: m[2],
  51. Runtime: m[3],
  52. GOOS: m[4],
  53. GOARCH: m[5],
  54. Builder: m[6],
  55. }
  56. // Split the version tag into tag and commit. This is old style
  57. // v1.2.3-something.4+11-g12345678 or newer with just dots
  58. // v1.2.3-something.4.11.g12345678 or v1.2.3-dev.11.g12345678.
  59. parts := []string{v.Version}
  60. if strings.Contains(v.Version, "+") {
  61. parts = strings.Split(v.Version, "+")
  62. } else {
  63. idxs := gitExtraRE.FindStringIndex(v.Version)
  64. if len(idxs) > 0 {
  65. parts = []string{v.Version[:idxs[0]], v.Version[idxs[0]+1:]}
  66. }
  67. }
  68. v.Tag = parts[0]
  69. if len(parts) > 1 {
  70. fields := gitExtraSepRE.Split(parts[1], -1)
  71. if len(fields) >= 2 && strings.HasPrefix(fields[1], "g") {
  72. v.Commit = fields[1][1:]
  73. }
  74. }
  75. if len(m) >= 8 && m[7] != "" {
  76. tags := strings.Split(m[7], ",")
  77. for i := range tags {
  78. tags[i] = strings.TrimSpace(tags[i])
  79. }
  80. v.Extra = tags
  81. }
  82. return v, nil
  83. }