gv
A standalone tool to get version information from git repository
go install github.com/yougg/gv@latest
# show help
gv -h
# only get version from git repo
gv -r /path/to/repo
cd /path/to/repo && gv
# get all version information from git repo
gv -a -r /path/to/repo
cd /path/to/repo && gv -a
gv -r /path/to/gv
main-20240102183907-759ac82df558
cd /path/to/gv; gv -a
Version: main-20240102183907-759ac82df558
Tag:
Branch: main
CommitTime: 20240102183907
CommitID: 759ac82df558dbabbc1890c108bdff9ebd5a8c79
Ignore error log output
gv 2> /dev/null
add one source file hello.go
package main
import "fmt"
var Version string
func main() {
fmt.Println("Version:", Version)
}
commit and build the source file with gv
version info
git init
git add hello.go
git commit -m 'initial commit'
go build -ldflags "-s -w -X main.Version=$(gv)" -o hello hello.go
./hello
# Version: main-20240102234342-eab50ab71e12
gv -a
# Version: main-20240102234342-eab50ab71e12
# Tag:
# Branch: main
# CommitTime: 20240102234342
# CommitID: eab50ab71e12b13b0030ecc05565dddc62f82af6
add tag then build and run again
git tag v0.0.1
go build -ldflags "-s -w -X main.Version=$(gv)" -o hello hello.go
./hello
# Version: v0.0.1
gv -a
# Version: v0.0.1
# Tag: v0.0.1
# Branch: main
# CommitTime: 20240102234342
# CommitID: eab50ab71e12b13b0030ecc05565dddc62f82af6