From 48c95eb41d7e393b585e488ec2b9cc82c6b8f467 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 12 Jul 2023 09:27:34 +0200 Subject: [PATCH] cmd/stcrashreceiver: Correct parsing of current version string --- cmd/stcrashreceiver/sentry.go | 23 ++++++++++++++++++++--- cmd/stcrashreceiver/sentry_test.go | 14 ++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/cmd/stcrashreceiver/sentry.go b/cmd/stcrashreceiver/sentry.go index de8d91e57..73bfdc969 100644 --- a/cmd/stcrashreceiver/sentry.go +++ b/cmd/stcrashreceiver/sentry.go @@ -215,7 +215,13 @@ func crashReportFingerprint(message string) []string { } // 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] -var longVersionRE = regexp.MustCompile(`syncthing\s+(v[^\s]+)\s+"([^"]+)"\s\(([^\s]+)\s+([^-]+)-([^)]+)\)\s+([^\s]+)[^\[]*(?:\[(.+)\])?$`) +// or, somewhere along the way the "+" in the version tag disappeared: +// 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] +var ( + longVersionRE = regexp.MustCompile(`syncthing\s+(v[^\s]+)\s+"([^"]+)"\s\(([^\s]+)\s+([^-]+)-([^)]+)\)\s+([^\s]+)[^\[]*(?:\[(.+)\])?$`) + gitExtraRE = regexp.MustCompile(`\.\d+\.g[0-9a-f]+`) // ".1.g6aaae618" + gitExtraSepRE = regexp.MustCompile(`[.-]`) // dot or dash +) type version struct { version string // "v1.1.4-rc.1+30-g6aaae618-dirty-crashrep" @@ -257,10 +263,21 @@ func parseVersion(line string) (version, error) { builder: m[6], } - parts := strings.Split(v.version, "+") + // Split the version tag into tag and commit. This is old style + // v1.2.3-something.4+11-g12345678 or newer with just dots + // v1.2.3-something.4.11.g12345678 or v1.2.3-dev.11.g12345678. + parts := []string{v.version} + if strings.Contains(v.version, "+") { + parts = strings.Split(v.version, "+") + } else { + idxs := gitExtraRE.FindStringIndex(v.version) + if len(idxs) > 0 { + parts = []string{v.version[:idxs[0]], v.version[idxs[0]+1:]} + } + } v.tag = parts[0] if len(parts) > 1 { - fields := strings.Split(parts[1], "-") + fields := gitExtraSepRE.Split(parts[1], -1) if len(fields) >= 2 && strings.HasPrefix(fields[1], "g") { v.commit = fields[1][1:] } diff --git a/cmd/stcrashreceiver/sentry_test.go b/cmd/stcrashreceiver/sentry_test.go index e052197c9..9fa30f262 100644 --- a/cmd/stcrashreceiver/sentry_test.go +++ b/cmd/stcrashreceiver/sentry_test.go @@ -44,6 +44,20 @@ func TestParseVersion(t *testing.T) { extra: []string{"foo", "bar"}, }, }, + { + longVersion: `syncthing v1.23.7-dev.26.gdf7b56ae-stversionextra "Fermium Flea" (go1.20.5 darwin-arm64) jb@ok.kastelo.net 2023-07-12 06:55:26 UTC [Some Wrapper, purego, stnoupgrade]`, + parsed: version{ + version: "v1.23.7-dev.26.gdf7b56ae-stversionextra", + tag: "v1.23.7-dev", + commit: "df7b56ae", + codename: "Fermium Flea", + runtime: "go1.20.5", + goos: "darwin", + goarch: "arm64", + builder: "jb@ok.kastelo.net", + extra: []string{"Some Wrapper", "purego", "stnoupgrade"}, + }, + }, } for _, tc := range cases {