From fe193c304801ff3aa5d920cfc4d10b1523cbafc6 Mon Sep 17 00:00:00 2001 From: Steve Yegge Date: Fri, 26 Dec 2025 17:06:32 -0800 Subject: [PATCH] gt version: Match bd version output format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Single-line output showing version, build type, branch and commit: gt version 0.1.0 (dev: main@62413d55cbb0) Uses runtime/debug to get VCS info from build, with fallback to git commands at runtime. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- internal/cmd/version.go | 76 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/internal/cmd/version.go b/internal/cmd/version.go index 6635aae1..16bc8930 100644 --- a/internal/cmd/version.go +++ b/internal/cmd/version.go @@ -2,16 +2,21 @@ package cmd import ( "fmt" + "os/exec" + "runtime/debug" + "strings" "github.com/spf13/cobra" - "github.com/steveyegge/gastown/internal/style" ) // Version information - set at build time via ldflags var ( - Version = "0.1.0" - BuildTime = "unknown" - GitCommit = "unknown" + Version = "0.1.0" + // Build can be set via ldflags at compile time + Build = "dev" + // Commit and Branch - the git revision the binary was built from (optional ldflag) + Commit = "" + Branch = "" ) var versionCmd = &cobra.Command{ @@ -19,13 +24,68 @@ var versionCmd = &cobra.Command{ GroupID: GroupDiag, Short: "Print version information", Run: func(cmd *cobra.Command, args []string) { - fmt.Println(style.Bold.Render("gt") + " - Gas Town CLI") - fmt.Printf("Version: %s\n", Version) - fmt.Printf("Commit: %s\n", style.Dim.Render(GitCommit)) - fmt.Printf("Built: %s\n", style.Dim.Render(BuildTime)) + commit := resolveCommitHash() + branch := resolveBranch() + + if commit != "" && branch != "" { + fmt.Printf("gt version %s (%s: %s@%s)\n", Version, Build, branch, shortCommit(commit)) + } else if commit != "" { + fmt.Printf("gt version %s (%s: %s)\n", Version, Build, shortCommit(commit)) + } else { + fmt.Printf("gt version %s (%s)\n", Version, Build) + } }, } func init() { rootCmd.AddCommand(versionCmd) } + +func resolveCommitHash() string { + if Commit != "" { + return Commit + } + + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" && setting.Value != "" { + return setting.Value + } + } + } + + return "" +} + +func shortCommit(hash string) string { + if len(hash) > 12 { + return hash[:12] + } + return hash +} + +func resolveBranch() string { + if Branch != "" { + return Branch + } + + // Try to get branch from build info (build-time VCS detection) + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.branch" && setting.Value != "" { + return setting.Value + } + } + } + + // Fallback: try to get branch from git at runtime + cmd := exec.Command("git", "symbolic-ref", "--short", "HEAD") + cmd.Dir = "." + if output, err := cmd.Output(); err == nil { + if branch := strings.TrimSpace(string(output)); branch != "" && branch != "HEAD" { + return branch + } + } + + return "" +}