diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..076689d --- /dev/null +++ b/Makefile @@ -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)" diff --git a/README.md b/README.md index d5016cc..8d59ca9 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..a0b6b1f --- /dev/null +++ b/cmd/version.go @@ -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) +}