diff --git a/README.md b/README.md index 0fbdead..b5312e2 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,8 @@ Then run it from inside any git repository: git_pruner ``` -To print the build's commit and date (from Go's automatic VCS stamping): +To print the build's version (git tag), commit, and date (from Go's automatic VCS +stamping): ```sh git_pruner version # also --version, -v diff --git a/main.go b/main.go index 98c0628..1e26c1d 100644 --- a/main.go +++ b/main.go @@ -8,18 +8,22 @@ import ( tea "github.com/charmbracelet/bubbletea" ) -// Build metadata. buildTime and gitCommit can be injected at link time via -// -ldflags "-X main.buildTime=... -X main.gitCommit=..." (or -X main.buildDate=...); -// when unset, buildInfo falls back to the VCS data the Go toolchain embeds. +// Build metadata. version, buildTime and gitCommit can be injected at link time via +// -ldflags "-X main.version=... -X main.buildTime=... -X main.gitCommit=..." (or +// -X main.buildDate=...); when unset, buildInfo falls back to the VCS data the Go +// toolchain embeds. var ( + version string buildTime string buildDate string gitCommit string ) -// buildInfo returns the commit the binary was built from and when it was -// compiled, preferring -ldflags values and falling back to Go's embedded VCS info. -func buildInfo() (commit, date string) { +// buildInfo returns the version (git tag), the commit the binary was built from, +// and when it was compiled, preferring -ldflags values and falling back to Go's +// embedded VCS info. +func buildInfo() (ver, commit, date string) { + ver = version commit = gitCommit date = buildTime if date == "" { @@ -27,6 +31,12 @@ func buildInfo() (commit, date string) { } dirty := false if info, ok := debug.ReadBuildInfo(); ok { + // go build stamps this from git tags: "v0.1.0" on a tagged commit, a + // "v0.1.1-0.