vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Report the source commit instead of a planted version
9103f6a83e4706df00320eeffba654df537f97a8 · Keenan Nemetz · 1d ago · parent 11b4b0f9
cmd/vole/main.go +32−18
| @@ -10,31 +10,37 @@ import ( | |||
| 10 | 10 | "fmt" | |
| 11 | 11 | "os" | |
| 12 | 12 | "runtime/debug" | |
| 13 | "strings" | ||
| 13 | 14 | ||
| 14 | 15 | "rodent.software/vole/client" | |
| 15 | 16 | ) | |
| 16 | 17 | ||
| 17 | // sourceVersion is the last release this source tree contains. | ||
| 18 | const sourceVersion = "0.2.0" | ||
| 19 | |||
| 20 | 18 | // version is set by a package or release build with | |
| 21 | 19 | // -ldflags "-X main.version=...". A build from the source tree leaves it empty | |
| 22 | // so buildVersion can describe that build instead of claiming a release number. | ||
| 20 | // so buildVersion reports the commit Go recorded instead of claiming a release. | ||
| 23 | 21 | var version string | |
| 24 | 22 | ||
| 25 | // buildVersion is what `vole version` prints. A release build prints the version | ||
| 26 | // it was stamped with. A source build prints sourceVersion with a -devel suffix | ||
| 27 | // and the revision Go recorded at build time, so an installed binary can be | ||
| 28 | // identified without rebuilding it. | ||
| 23 | // buildVersion is what `vole version` prints. A stamped build prints the tag it | ||
| 24 | // was built from, and an installation from a module proxy prints that module | ||
| 25 | // version. A build from a source checkout prints the commit it was built from, | ||
| 26 | // with .dirty when the tree had uncommitted changes. There is no in-tree version | ||
| 27 | // to drift from the tag: a source build never claims a release it does not carry. | ||
| 29 | 28 | func buildVersion() string { | |
| 30 | 29 | if version != "" { | |
| 31 | return version | ||
| 30 | return withoutV(version) | ||
| 32 | 31 | } | |
| 33 | v := sourceVersion + "-devel" | ||
| 34 | 32 | info, ok := debug.ReadBuildInfo() | |
| 35 | 33 | if !ok { | |
| 36 | return v | ||
| 34 | return "devel" | ||
| 37 | 35 | } | |
| 36 | return versionFromBuildInfo(info) | ||
| 37 | } | ||
| 38 | |||
| 39 | func versionFromBuildInfo(info *debug.BuildInfo) string { | ||
| 40 | // A source checkout wins over Main.Version: Go stamps a pseudo-version such as | ||
| 41 | // v0.1.0-rc4.0.20261202222536-cc5123a549d8 there, which is neither the tag nor | ||
| 42 | // the commit and would be a third spelling of the same build. The commit is | ||
| 43 | // what the source tree can actually vouch for. | ||
| 38 | 44 | revision, dirty := "", false | |
| 39 | 45 | for _, setting := range info.Settings { | |
| 40 | 46 | switch setting.Key { | |
| @@ -44,18 +50,26 @@ func buildVersion() string { | |||
| 44 | 50 | dirty = setting.Value == "true" | |
| 45 | 51 | } | |
| 46 | 52 | } | |
| 47 | if len(revision) > 12 { | ||
| 48 | revision = revision[:12] | ||
| 49 | } | ||
| 53 | revision = strings.TrimSpace(revision) | ||
| 50 | 54 | if revision != "" { | |
| 51 | v += "+" + revision | ||
| 55 | if len(revision) > 12 { | ||
| 56 | revision = revision[:12] | ||
| 57 | } | ||
| 58 | if dirty { | ||
| 59 | revision += ".dirty" | ||
| 60 | } | ||
| 61 | return revision | ||
| 52 | 62 | } | |
| 53 | if dirty { | ||
| 54 | v += ".dirty" | ||
| 63 | if v := strings.TrimSpace(info.Main.Version); v != "" && v != "(devel)" { | ||
| 64 | return withoutV(v) | ||
| 55 | 65 | } | |
| 56 | return v | ||
| 66 | return "devel" | ||
| 57 | 67 | } | |
| 58 | 68 | ||
| 69 | // withoutV keeps the printed version consistent with the release workflow, which | ||
| 70 | // strips the tag's leading v before stamping it. | ||
| 71 | func withoutV(v string) string { return strings.TrimPrefix(strings.TrimSpace(v), "v") } | ||
| 72 | |||
| 59 | 73 | func main() { | |
| 60 | 74 | // Every command that talks to an instance verifies its host key, including the | |
| 61 | 75 | // git children it starts. VOLE_KNOWN_HOSTS points at a known_hosts file; | |