Add commit hash to bd version output (bd-hpt5)

This commit is contained in:
Codex Agent
2025-11-13 13:45:52 -07:00
parent 2ffeb35ec5
commit 7c96142432

View File

@@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"os" "os"
"runtime/debug"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/beads" "github.com/steveyegge/beads/internal/beads"
@@ -14,6 +15,8 @@ var (
Version = "0.23.1" Version = "0.23.1"
// Build can be set via ldflags at compile time // Build can be set via ldflags at compile time
Build = "dev" Build = "dev"
// Commit is the git revision the binary was built from (optional ldflag)
Commit = ""
) )
var versionCmd = &cobra.Command{ var versionCmd = &cobra.Command{
@@ -27,14 +30,24 @@ var versionCmd = &cobra.Command{
return return
} }
commit := resolveCommitHash()
if jsonOutput { if jsonOutput {
outputJSON(map[string]string{ result := map[string]string{
"version": Version, "version": Version,
"build": Build, "build": Build,
}) }
if commit != "" {
result["commit"] = commit
}
outputJSON(result)
} else {
if commit != "" {
fmt.Printf("bd version %s (%s, commit %s)\n", Version, Build, shortCommit(commit))
} else { } else {
fmt.Printf("bd version %s (%s)\n", Version, Build) 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") versionCmd.Flags().Bool("daemon", false, "Check daemon version and compatibility")
rootCmd.AddCommand(versionCmd) 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
}