feat(version): print the version like gh version

tea version and --version printed "Version: development<TAB>golang:
1.27.2". They now print "tea version 1.0.0 (2026-10-01)", the release
notes URL for tagged releases, and "built with go1.27.2" with the build
tags. Builds from go install take the module version from the build
info, and the date comes from the commit the binary was built from.

Signed-off-by: bircni <bircni@icloud.com>
This commit is contained in:
bircni committed 2026-10-11 23:50:46 +02:00
1 parent 85034c524f
commit 83dd4f7938
3 files changed
+75 -13

No files matched your search

+43 -12
View File
@@ -1,12 +1,15 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
// Package version reports the version of the tea build.
package version
import (
"fmt"
"regexp"
"runtime"
"runtime/debug"
"strings"
"time"
)
// Version holds the current tea version.
@@ -18,16 +21,44 @@ var Version = "development"
// Tags holds the build tags used
var Tags = ""
// Format returns a human-readable version string including
// Go version and build tags when available.
// release matches the versions of tagged releases, such as 1.0.0 or 1.1.0-rc1; builds between
// releases carry a +commit suffix.
var release = regexp.MustCompile(`^\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$`)
// Format describes the build like gh version: the version and its date, the release notes of a
// release, and the Go version and build tags:
//
// tea version 1.0.0 (2026-10-01)
// https://gitea.com/gitea/tea/releases/tag/v1.0.0
// built with go1.27.2
func Format() string {
s := fmt.Sprintf("Version: %s\tgolang: %s",
Version,
strings.ReplaceAll(runtime.Version(), "go", ""))
if len(Tags) != 0 {
s += fmt.Sprintf("\tbuilt with: %s", strings.ReplaceAll(Tags, " ", ", "))
}
return s
info, _ := debug.ReadBuildInfo()
return format(Version, Tags, runtime.Version(), info)
}
func format(version, tags, goVersion string, info *debug.BuildInfo) string {
date := ""
if info != nil {
// go install gitea.dev/tea@v1.0.0 stamps the module version instead of the ldflags.
if version == "development" && info.Main.Version != "" && info.Main.Version != "(devel)" {
version = strings.TrimPrefix(info.Main.Version, "v")
}
for _, setting := range info.Settings {
if setting.Key != "vcs.time" {
continue
}
if built, err := time.Parse(time.RFC3339, setting.Value); err == nil {
date = " (" + built.UTC().Format(time.DateOnly) + ")"
}
}
}
text := "tea version " + version + date + "\n"
if release.MatchString(version) {
text += "https://gitea.com/gitea/tea/releases/tag/v" + version + "\n"
}
text += "built with " + goVersion
if tags = strings.Join(strings.Fields(tags), ", "); tags != "" {
text += " (tags: " + tags + ")"
}
return text
}