2 Commits
v0.1.1 ... main

Author SHA1 Message Date
4155d78440 Switches to native Go build info for versioning
Simplifies the version command and Makefile by removing manual ldflags injection. The application now relies entirely on Go's built-in VCS metadata embedding to extract version, commit, and date information, ensuring accurate reporting across all build methods.
2026-04-11 15:06:11 +01:00
6e21a0798a Enhances version info retrieval
Improves the `version` command to automatically extract build details (version, commit, date) from Go's `debug.ReadBuildInfo` when `ldflags` are not provided.

This allows users installing via `go install` to receive accurate version information without requiring a Makefile or explicit build flags. Updates the README to reflect this new capability.
2026-04-11 15:00:06 +01:00
3 changed files with 49 additions and 46 deletions

View File

@@ -1,24 +1,14 @@
.PHONY: build install test clean version
.PHONY: build install test coverage clean
# Get version from git tag, fallback to dev
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
COMMIT ?= $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")
DATE ?= $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
# Ldflags to inject version information
LDFLAGS := -ldflags "-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Version=$(VERSION) \
-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Commit=$(COMMIT) \
-X git.membo.co.uk/dtomlinson/gitlocal/cmd.Date=$(DATE)"
# Build the binary
# Build the binary (Go automatically embeds VCS info)
build:
@echo "Building gitlocal $(VERSION)..."
go build $(LDFLAGS) -o gitlocal .
@echo "Building gitlocal..."
go build -o gitlocal .
# Install to $GOPATH/bin
install:
@echo "Installing gitlocal $(VERSION)..."
go install $(LDFLAGS) .
@echo "Installing gitlocal..."
go install .
# Run tests
test:
@@ -35,9 +25,3 @@ coverage:
clean:
@echo "Cleaning..."
rm -f gitlocal coverage.out
# Show version that would be built
version:
@echo "Version: $(VERSION)"
@echo "Commit: $(COMMIT)"
@echo "Date: $(DATE)"

View File

@@ -134,23 +134,27 @@ cd gitlocal
# Install dependencies
go mod download
# Build with version info (recommended - uses Makefile)
# Build the binary
make build
# Or install to $GOPATH/bin with version info
make install
# Build without version info (not recommended)
# or
go build -o gitlocal
# Check version that would be built
make version
# Install to $GOPATH/bin
make install
# or
go install
```
The Makefile automatically injects version information from git tags:
- `Version`: Git tag (e.g., `v0.1.0`) or commit hash if no tag exists
- `Commit`: Short commit hash
- `Date`: Build timestamp
**Version Information:**
Go automatically embeds version information from git when building. The `gitlocal version` command reads this embedded metadata to display:
- Version (from git tag, e.g., `v0.1.0`)
- Commit hash (short SHA)
- Build timestamp
This works automatically for:
- `go install` (remote installation)
- `go build` (local builds)
- Pre-built binaries (when built from a git repo)
Check the version after installing:
```bash

View File

@@ -2,27 +2,42 @@ package cmd
import (
"fmt"
"runtime/debug"
"github.com/spf13/cobra"
)
var (
// Version is set via -ldflags during build
Version = "dev"
// Commit is set via -ldflags during build
Commit = "unknown"
// Date is set via -ldflags during build
Date = "unknown"
)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Print version information",
Long: `Display the version, commit hash, and build date of gitlocal.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("gitlocal version %s\n", Version)
fmt.Printf(" Commit: %s\n", Commit)
fmt.Printf(" Built: %s\n", Date)
version := "dev"
commit := "unknown"
date := "unknown"
// Read version from build info (works for both 'go install' and 'go build')
if info, ok := debug.ReadBuildInfo(); ok {
version = info.Main.Version
// Extract commit and date from build settings
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
if len(setting.Value) > 7 {
commit = setting.Value[:7]
} else {
commit = setting.Value
}
case "vcs.time":
date = setting.Value
}
}
}
fmt.Printf("gitlocal version %s\n", version)
fmt.Printf(" Commit: %s\n", commit)
fmt.Printf(" Built: %s\n", date)
},
}