Adds version command and Makefile for build info

Provides a `gitlocal version` command to display the application's version, commit hash, and build date.

Introduces a `Makefile` to automate builds, installations, and tests. The Makefile dynamically extracts version, commit, and date from git tags, commit hashes, and build timestamps, injecting this metadata into the Go binary via `ldflags`.

Updates the `README.md` to document the new command and recommended build process using the Makefile.
This commit is contained in:
2026-04-11 14:55:43 +01:00
parent b5f1495680
commit d00ffdb37c
3 changed files with 106 additions and 3 deletions

43
Makefile Normal file
View File

@@ -0,0 +1,43 @@
.PHONY: build install test clean version
# 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:
@echo "Building gitlocal $(VERSION)..."
go build $(LDFLAGS) -o gitlocal .
# Install to $GOPATH/bin
install:
@echo "Installing gitlocal $(VERSION)..."
go install $(LDFLAGS) .
# Run tests
test:
@echo "Running tests..."
go test -v ./...
# Run tests with coverage
coverage:
@echo "Running tests with coverage..."
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
# Clean build artifacts
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

@@ -74,6 +74,19 @@ Converted Repositories (2):
Branch: master
```
### Show version information
```bash
gitlocal version
```
Example output:
```
gitlocal version v0.1.0
Commit: a1b2c3d
Built: 2026-04-11T14:30:00Z
```
## Using git with .gitlocal
Add this alias to your `.zshrc` or `.bashrc`:
@@ -121,11 +134,27 @@ cd gitlocal
# Install dependencies
go mod download
# Build the binary
# Build with version info (recommended - uses Makefile)
make build
# Or install to $GOPATH/bin with version info
make install
# Build without version info (not recommended)
go build -o gitlocal
# Or install directly to $GOPATH/bin
go install
# Check version that would be built
make version
```
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
Check the version after installing:
```bash
gitlocal version
```
### Running Tests

31
cmd/version.go Normal file
View File

@@ -0,0 +1,31 @@
package cmd
import (
"fmt"
"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)
},
}
func init() {
rootCmd.AddCommand(versionCmd)
}