turbo-editors/turbo-corepublic Fork 0
v1.0.0
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

📦 Turbo Core f3ade8d · on v1.0.0 · k33g · 13h ago
version.go · 245 lines · 8.2 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Package version tells the editor what build of itself it is.
//
// The number comes from whichever of three sources knows it, in this order:
// the linker, the Go build system, and a constant of last resort. Nothing here
// reads a file or runs a command, so it costs nothing to ask.
package version

import (
	"runtime/debug"
	"strings"
	"time"
)

// The build stamps these through the linker:
//
//	go build -ldflags "-X 'rickub.com/turbo-editors/turbo-core/version.stamp=v0.2.0'" .
//
// The Makefile and scripts/install.sh both do it, filling stamp from
// `git describe --tags --dirty`. They are unexported because nothing but the
// linker should ever write them, and package-level strings are what -X can set.
var (
	stamp  string
	commit string
	built  string
)

// unknownNumber is what a build reports when no source could name it. It is
// deliberately not a version number: a stale one printed as fact is the defect
// this package exists to remove.
const unknownNumber = "unknown"

// develVersion is what the Go build system calls a binary built from a working
// tree when it can read no version at all.
const develVersion = "(devel)"

// pseudoTailLength is the width of the tail every Go pseudo-version ends with:
// a dash, a 14-digit UTC timestamp, a dash, and twelve hex characters.
const pseudoTailLength = 1 + 14 + 1 + 12

// shortCommitLength is how much of a revision hash is worth showing. Seven
// characters is what git itself abbreviates to, and what a reader will paste
// back into `git show`.
const shortCommitLength = 7

// Info is what a binary knows about its own build.
//
// Commit and Built are empty when nothing recorded them, which is normal: a
// binary from `go install …@v0.2.0` knows its version and nothing else. A
// caller shows the fields it has and says nothing about the rest.
type Info struct {
	// Number is the version, without a leading "v": "0.2.0" for a release,
	// "0.1.0-14-g88a4c38" for a build between two of them.
	Number string
	// Commit is the abbreviated revision, when it is known separately from
	// Number.
	Commit string
	// Built is when the binary was linked, in RFC 3339, when it is known.
	Built string
}

// Current returns what this binary knows about itself.
//
//	info := version.Current()
//	fmt.Println(info.Number) // "0.1.0-14-g88a4c38"
func Current() Info {
	return resolve(stamp, commit, built, readBuildInfo())
}

// String renders the whole of what is known on one line, for `-version`.
//
// One line rather than several because the installer prints it beside a path,
// and because a caller that wants the parts has the fields.
//
//	version.Info{Number: "0.2.0", Commit: "88a4c38"}.String()
//	// "0.2.0 (88a4c38)"
func (i Info) String() string {
	switch {
	case i.Commit != "" && i.Built != "":
		return i.Number + " (" + i.Commit + ", built " + i.Built + ")"
	case i.Commit != "":
		return i.Number + " (" + i.Commit + ")"
	case i.Built != "":
		return i.Number + " (built " + i.Built + ")"
	}
	return i.Number
}

// resolve turns the raw build facts into an Info.
//
// It is separate from Current so that every combination can be tested without
// linker flags: the interesting cases are precisely the ones a test binary
// cannot be built into.
func resolve(stamp, commit, built string, info *debug.BuildInfo) Info {
	if stamp != "" {
		return Info{Number: trimV(stamp), Commit: shorten(commit), Built: built}
	}
	if info == nil {
		return Info{Number: unknownNumber}
	}
	return fromBuildInfo(info)
}

// fromBuildInfo reads what the Go build system recorded.
//
// Two quite different builds arrive here. `go install <module>@v0.2.0` records
// the module version and **no** VCS information at all, because there was no
// repository to read. A plain `go build .` in a checkout records the reverse:
// the version is "(devel)" and the revision is known. Neither can name a tag —
// the build system does not read them — which is why a build that wants to
// show one has to be stamped.
func fromBuildInfo(info *debug.BuildInfo) Info {
	settings := settingsOf(info)
	out := Info{
		Number: trimV(info.Main.Version),
		Commit: shorten(settings["vcs.revision"]),
	}

	// A pseudo-version is the Go tool naming a commit that no tag names —
	// "0.1.1-0.20260831165958-88a4c3859bf3+dirty". It is unreadable in a
	// dialog, and its "0.1.1" is a patch release that does not exist, so it is
	// reported for what it means rather than for what it says.
	if out.Number == "" || out.Number == develVersion || isPseudoVersion(out.Number) {
		out.Number = develNumber(settings)
	}
	// vcs.time is when the *commit* was made, not when this binary was linked,
	// so it is not a build date and is not reported as one.
	return out
}

// develNumber names a build made from a working tree, where no tag is
// available: "devel", and "devel-dirty" when the tree had uncommitted changes.
func develNumber(settings map[string]string) string {
	if settings["vcs.modified"] == "true" {
		return "devel-dirty"
	}
	if _, built := settings["vcs.revision"]; built {
		return "devel"
	}
	return unknownNumber
}

// isPseudoVersion reports whether a module version is one the Go tool invented
// for a commit that no tag names.
//
// Every form of one ends the same way, whatever base version precedes it, and
// build metadata such as "+dirty" is not part of that tail.
//
//	isPseudoVersion("0.1.1-0.20260831165958-88a4c3859bf3+dirty") // true
//	isPseudoVersion("0.2.0")                                     // false
func isPseudoVersion(number string) bool {
	number = withoutBuildMetadata(number)
	if len(number) <= pseudoTailLength {
		return false // nothing left for the base version it was derived from
	}
	return hasPseudoTail(number[len(number)-pseudoTailLength:])
}

// hasPseudoTail reports whether a tail of exactly pseudoTailLength characters
// is the "-yyyymmddhhmmss-abcdefabcdef" a pseudo-version ends with.
func hasPseudoTail(tail string) bool {
	if !isAll(tail[1:15], isDigit) || !isAll(tail[16:], isHex) {
		return false
	}
	// The character before the timestamp is a dash when no tag precedes the
	// commit ("v0.0.0-<ts>-<hash>") and a dot when one does, because the base
	// then ends in "-0." or "-pre.0.".
	separatesTimestamp := tail[0] == '-' || tail[0] == '.'
	return separatesTimestamp && tail[15] == '-'
}

// withoutBuildMetadata drops the "+dirty" or "+incompatible" a version may
// carry, which is not part of the pseudo-version tail.
func withoutBuildMetadata(number string) string {
	if plus := strings.IndexByte(number, '+'); plus >= 0 {
		return number[:plus]
	}
	return number
}

// isAll reports whether every byte of s satisfies want.
func isAll(s string, want func(byte) bool) bool {
	for i := 0; i < len(s); i++ {
		if !want(s[i]) {
			return false
		}
	}
	return true
}

func isDigit(c byte) bool { return c >= '0' && c <= '9' }

func isHex(c byte) bool { return isDigit(c) || (c >= 'a' && c <= 'f') }

// settingsOf turns a build's settings into a map, which is how every caller
// here wants them.
func settingsOf(info *debug.BuildInfo) map[string]string {
	out := make(map[string]string, len(info.Settings))
	for _, setting := range info.Settings {
		out[setting.Key] = setting.Value
	}
	return out
}

// trimV drops the leading "v" a git tag carries. The tag is `v0.2.0`; what a
// person reads in an About box is `0.2.0`.
func trimV(number string) string {
	if len(number) > 1 && number[0] == 'v' && number[1] >= '0' && number[1] <= '9' {
		return number[1:]
	}
	return number
}

// shorten abbreviates a revision hash the way git does.
func shorten(revision string) string {
	if len(revision) <= shortCommitLength {
		return revision
	}
	return revision[:shortCommitLength]
}

// BuiltAt renders Built for a person: "2026-08-31 18:04 UTC".
//
// A stamp that does not parse is shown as it is rather than dropped — a build
// whose date is malformed should say so, not appear to have none.
//
//	version.Info{Built: "2026-08-31T18:04:05Z"}.BuiltAt() // "2026-08-31 18:04 UTC"
func (i Info) BuiltAt() string {
	if i.Built == "" {
		return ""
	}
	moment, err := time.Parse(time.RFC3339, i.Built)
	if err != nil {
		return i.Built
	}
	return moment.UTC().Format("2006-01-02 15:04") + " UTC"
}

// readBuildInfo is debug.ReadBuildInfo, with the "ok" folded into a nil.
func readBuildInfo() *debug.BuildInfo {
	info, ok := debug.ReadBuildInfo()
	if !ok {
		return nil
	}
	return info
}