From 7c96142432d2f0f1e0e08dba8ca6216647343999 Mon Sep 17 00:00:00 2001 From: Codex Agent Date: Thu, 13 Nov 2025 13:45:52 -0700 Subject: [PATCH] Add commit hash to bd version output (bd-hpt5) --- cmd/bd/version.go | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/cmd/bd/version.go b/cmd/bd/version.go index 9bedcd9f..8ebf2fac 100644 --- a/cmd/bd/version.go +++ b/cmd/bd/version.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "runtime/debug" "github.com/spf13/cobra" "github.com/steveyegge/beads/internal/beads" @@ -14,6 +15,8 @@ var ( Version = "0.23.1" // Build can be set via ldflags at compile time Build = "dev" + // Commit is the git revision the binary was built from (optional ldflag) + Commit = "" ) var versionCmd = &cobra.Command{ @@ -27,13 +30,23 @@ var versionCmd = &cobra.Command{ return } + commit := resolveCommitHash() + if jsonOutput { - outputJSON(map[string]string{ + result := map[string]string{ "version": Version, "build": Build, - }) + } + if commit != "" { + result["commit"] = commit + } + outputJSON(result) } else { - fmt.Printf("bd version %s (%s)\n", Version, Build) + if commit != "" { + fmt.Printf("bd version %s (%s, commit %s)\n", Version, Build, shortCommit(commit)) + } else { + fmt.Printf("bd version %s (%s)\n", Version, Build) + } } }, } @@ -90,3 +103,26 @@ func init() { versionCmd.Flags().Bool("daemon", false, "Check daemon version and compatibility") 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 +}