Skip to content

Go package that embeds a git tag/version string in your application

License

Notifications You must be signed in to change notification settings

aletheia7/gogitver

Repository files navigation

gogitver and mage (go make)

A go package that inserts a git tag into your binary. Uses mage to efficiently compile your go application when the git tag changes. Other source code changes will still trigger a compile. The git version/tag is created beforehand with:

git tag -a

# View the git tag: 
git describe --tags --long

# Instead of "go install" execute:
mage

Install and mage

gogitver must be installed as a package under your application main directory. E.g. if your application resides under ~/go/src/app, gogitver must be installed under ~/go/src/app/gogitver. ~/go/src/app/vendor/gogitver is preferred.

# Example
mkdir ~/go/src/app/vendor/gogitver
cd ~/go/src/app
git clone --depth 1 https://github.com/aletheia7/gogitver.git vendor/gogitver
rm -fr vendor/gogitver/.git
# link mage_gogitver.go 
ln -s vendor/gogitver/mage_gogitver.go .
Add a mg.Dep(Gogitver) to your main magefile.go that should reside under ~/go/src/app .

Create a git tag:

git tag -a 1.0.7

Run mage:

mage

gogitver and GNUMake

A go package that inserts a git tag into your binary. Uses make to efficiently compile your go application when the git tag changes. Other source code changes will still trigger a compile. The git version/tag is created beforehand with:

git tag -a

# View the git tag: 
git describe --tags --long

# Instead of "go install" execute:
make
# Run "go install" and strip to remove debug symbols:
make strip
# Instead of "go clean" execute:
make clean

Install and GNUMake

gogitver must be installed as a package under your application main directory. E.g. if your application resides under ~/go/src/app, gogitver must be installed under ~/go/src/app/gogitver. ~/go/src/app/vendor/gogitver is preferred.

# Example
mkdir ~/go/src/app/vendor/gogitver
cd ~/go/src/app
git clone --depth 1 https://github.com/aletheia7/gogitver.git vendor/gogitver
rm -fr vendor/gogitver/.git
# link makefile
ln -s vendor/gogitver/makefile .

Create a git tag:

git tag -a 1.0.7

Run make:

make

GNUMake

Software Version Guidelines: Semantic Versioning

Compare & parse semver version strings: godoc go-semver, code: go-semver

Example

package main
import (
	"fmt"
	// Do not use "github.com/alethiea7/gogitver"
	// It will not work
	"app/gogitver"
)

func main() {

	fmt.Println(gogitver.Git())	
	fmt.Println(gogitver.Tag())
}
Ouput
1.0.7-0-g8e9e07b
1.0.7

License

Use of this source code is governed by a BSD-2-Clause license that can be found in the LICENSE file.

BSD-2-Clause License