Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4155d78440 | |||
| 6e21a0798a |
28
Makefile
28
Makefile
@@ -1,24 +1,14 @@
|
|||||||
.PHONY: build install test clean version
|
.PHONY: build install test coverage clean
|
||||||
|
|
||||||
# Get version from git tag, fallback to dev
|
# Build the binary (Go automatically embeds VCS info)
|
||||||
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:
|
build:
|
||||||
@echo "Building gitlocal $(VERSION)..."
|
@echo "Building gitlocal..."
|
||||||
go build $(LDFLAGS) -o gitlocal .
|
go build -o gitlocal .
|
||||||
|
|
||||||
# Install to $GOPATH/bin
|
# Install to $GOPATH/bin
|
||||||
install:
|
install:
|
||||||
@echo "Installing gitlocal $(VERSION)..."
|
@echo "Installing gitlocal..."
|
||||||
go install $(LDFLAGS) .
|
go install .
|
||||||
|
|
||||||
# Run tests
|
# Run tests
|
||||||
test:
|
test:
|
||||||
@@ -35,9 +25,3 @@ coverage:
|
|||||||
clean:
|
clean:
|
||||||
@echo "Cleaning..."
|
@echo "Cleaning..."
|
||||||
rm -f gitlocal coverage.out
|
rm -f gitlocal coverage.out
|
||||||
|
|
||||||
# Show version that would be built
|
|
||||||
version:
|
|
||||||
@echo "Version: $(VERSION)"
|
|
||||||
@echo "Commit: $(COMMIT)"
|
|
||||||
@echo "Date: $(DATE)"
|
|
||||||
|
|||||||
28
README.md
28
README.md
@@ -134,23 +134,27 @@ cd gitlocal
|
|||||||
# Install dependencies
|
# Install dependencies
|
||||||
go mod download
|
go mod download
|
||||||
|
|
||||||
# Build with version info (recommended - uses Makefile)
|
# Build the binary
|
||||||
make build
|
make build
|
||||||
|
# or
|
||||||
# Or install to $GOPATH/bin with version info
|
|
||||||
make install
|
|
||||||
|
|
||||||
# Build without version info (not recommended)
|
|
||||||
go build -o gitlocal
|
go build -o gitlocal
|
||||||
|
|
||||||
# Check version that would be built
|
# Install to $GOPATH/bin
|
||||||
make version
|
make install
|
||||||
|
# or
|
||||||
|
go install
|
||||||
```
|
```
|
||||||
|
|
||||||
The Makefile automatically injects version information from git tags:
|
**Version Information:**
|
||||||
- `Version`: Git tag (e.g., `v0.1.0`) or commit hash if no tag exists
|
Go automatically embeds version information from git when building. The `gitlocal version` command reads this embedded metadata to display:
|
||||||
- `Commit`: Short commit hash
|
- Version (from git tag, e.g., `v0.1.0`)
|
||||||
- `Date`: Build timestamp
|
- 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:
|
Check the version after installing:
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -2,27 +2,42 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"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{
|
var versionCmd = &cobra.Command{
|
||||||
Use: "version",
|
Use: "version",
|
||||||
Short: "Print version information",
|
Short: "Print version information",
|
||||||
Long: `Display the version, commit hash, and build date of gitlocal.`,
|
Long: `Display the version, commit hash, and build date of gitlocal.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
fmt.Printf("gitlocal version %s\n", Version)
|
version := "dev"
|
||||||
fmt.Printf(" Commit: %s\n", Commit)
|
commit := "unknown"
|
||||||
fmt.Printf(" Built: %s\n", Date)
|
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)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user