Merge show-rev-in-dev commit 'c922dc1ec32d5e3a470c22036891e239f4ab1350' into stable-fixes
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -3,6 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"runtime/debug"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/steveyegge/beads/internal/beads"
|
"github.com/steveyegge/beads/internal/beads"
|
||||||
@@ -14,6 +17,9 @@ 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 and branch the git revision the binary was built from (optional ldflag)
|
||||||
|
Commit = ""
|
||||||
|
Branch = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
var versionCmd = &cobra.Command{
|
var versionCmd = &cobra.Command{
|
||||||
@@ -27,13 +33,29 @@ var versionCmd = &cobra.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
commit := resolveCommitHash()
|
||||||
|
branch := resolveBranch()
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
if branch != "" {
|
||||||
|
result["branch"] = branch
|
||||||
|
}
|
||||||
|
outputJSON(result)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("bd version %s (%s)\n", Version, Build)
|
if commit != "" && branch != "" {
|
||||||
|
fmt.Printf("bd version %s (%s: %s@%s)\n", Version, Build, branch, shortCommit(commit))
|
||||||
|
} else if commit != "" {
|
||||||
|
fmt.Printf("bd version %s (%s: %s)\n", Version, Build, shortCommit(commit))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("bd version %s (%s)\n", Version, Build)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -90,3 +112,52 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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", "rev-parse", "--abbrev-ref", "HEAD")
|
||||||
|
cmd.Dir = "."
|
||||||
|
if output, err := cmd.Output(); err == nil {
|
||||||
|
if branch := strings.TrimSpace(string(output)); branch != "" && branch != "HEAD" {
|
||||||
|
return branch
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user