Debian Codegraph @

progver/progver.go

raw
1 // Copyright (c) 2025 Tulir Asokan
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
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package progver
8
9 import (
10 "fmt"
11 "runtime"
12 "runtime/debug"
13 "strings"
14 "time"
15 )
16
17 type ProgramVersion struct {
18 // These should be hardcoded
19 Name string
20 URL string
21 BaseVersion string
22 SemCalVer bool
23
24 // These are set by the values passed to InitVersion
25 Commit string
26 Tag string
27 BuildTime time.Time
28
29 // These are computed by InitVersion
30 IsRelease bool
31 FormattedVersion string
32 LinkifiedVersion string
33 VersionDescription string
34 }
35
36 func (pv ProgramVersion) MarkdownDescription() string {
37 return fmt.Sprintf("[%s](%s) %s (%s)", pv.Name, pv.URL, pv.LinkifiedVersion, pv.BuildTime.Format(time.RFC1123))
38 }
39
40 func findCommitFromBuildInfo() string {
41 info, _ := debug.ReadBuildInfo()
42 if info == nil {
43 return ""
44 }
45 for _, setting := range info.Settings {
46 if setting.Key == "vcs.revision" && len(setting.Value) >= 40 {
47 return setting.Value
48 }
49 }
50 return ""
51 }
52
53 func (pv ProgramVersion) Init(tag, commit, rawBuildTime string) ProgramVersion {
54 if commit == "" || commit == "unknown" {
55 commit = findCommitFromBuildInfo()
56 }
57 if tag == "unknown" {
58 tag = ""
59 }
60 pv.Tag = tag
61 baseVersion := strings.TrimPrefix(pv.BaseVersion, "v")
62 tag = strings.TrimPrefix(tag, "v")
63 if pv.SemCalVer && len(tag) > 0 {
64 tag = semverToCalver(tag)
65 }
66 if tag == baseVersion || tag == baseVersion+".0" {
67 pv.IsRelease = true
68 pv.FormattedVersion = "v" + baseVersion
69 pv.LinkifiedVersion = fmt.Sprintf("[%s](%s/releases/%s)", pv.FormattedVersion, pv.URL, pv.Tag)
70 } else {
71 suffix := ""
72 if !strings.HasSuffix(baseVersion, "+dev") {
73 suffix = "+dev"
74 }
75 if len(commit) > 8 {
76 pv.FormattedVersion = fmt.Sprintf("v%s%s.%s", baseVersion, suffix, commit[:8])
77 } else {
78 pv.FormattedVersion = fmt.Sprintf("v%s%s.unknown", baseVersion, suffix)
79 }
80 if len(commit) > 8 {
81 pv.LinkifiedVersion = strings.Replace(pv.FormattedVersion, commit[:8], fmt.Sprintf("[%s](%s/commit/%s)", commit[:8], pv.URL, commit), 1)
82 } else {
83 pv.LinkifiedVersion = pv.FormattedVersion
84 }
85 }
86 var buildTime time.Time
87 if rawBuildTime != "unknown" {
88 buildTime, _ = time.Parse(time.RFC3339, rawBuildTime)
89 }
90 var builtWith string
91 if buildTime.IsZero() {
92 rawBuildTime = "unknown"
93 builtWith = fmt.Sprintf("built with %s", runtime.Version())
94 } else {
95 rawBuildTime = buildTime.Format(time.RFC1123)
96 builtWith = fmt.Sprintf("built at %s with %s", rawBuildTime, runtime.Version())
97 }
98 pv.VersionDescription = fmt.Sprintf("%s %s (%s)", pv.Name, pv.FormattedVersion, builtWith)
99 pv.Commit = commit
100 if pv.Commit == "" {
101 pv.Commit = "unknown"
102 }
103 pv.BuildTime = buildTime
104 return pv
105 }
106
107 func semverToCalver(semver string) string {
108 parts := strings.SplitN(semver, ".", 3)
109 if len(parts) != 3 {
110 panic(fmt.Errorf("invalid semver: %s", semver))
111 }
112 if len(parts[1]) != 4 {
113 panic(fmt.Errorf("invalid minor semver component for calendar versioning: %s", parts[1]))
114 }
115 return parts[1][:2] + "." + parts[1][2:] + "." + parts[2]
116 }