git-describe

  • Runs git describe --abbrev=7 --dirty
  • Generates .go file containing binary version.
  • Alternative to go build -ldflags "-X=xxx.Version=$(git describe --abbrev=7 --dirty)" command.

Prerequisites

  • git

How to use

go run github.com/thatInfrastructureGuy/[email protected] \
   && go build <<your-program-with-flags>>

Please see How can I track tool dependencies for a module?

What happens on running this program?

go run github.com/thatInfrastructureGuy/[email protected]

A new file gets created version/const.go with following contents:

// THIS IS GENERATED CODE; DO NOT EDIT.
// Generated at 2022-06-25 14:58:06.865286 -0700 PDT m=+0.000388855
package version

// Version is generated by running 'git describe --abbrev=7 --dirty'
const Version = "v0.0.1"

FAQs

I get an ERROR and the file has UNKNOWN version?

const Version = "UNKNOWN"

  1. Please make sure you have git binary installed.
  2. Check the output of git describe --abbrev=7 --dirty manually for your repository.
How do I change generated filename / variable name /package name ?

go run github.com/thatInfrastructureGuy/[email protected] \
   --filepath=version/version.go --package=version --variable=Version
Why?
  • I wanted the build to error out if I forgot to generate version. Just make sure you using the Version variable somewhere in your program for it to error out.
Go 1.18 already has build information.
Git-Describe Features Go 1.18
Git SHA
Git Tag ❌ Works with go install.go build returns (devel). golang/go#50603
Number of commits since last tag
Dirty Flag
Is this method better than using ldflags?

¯\_(ツ)_/¯

I do not use `git`. Can I use this program?

No. PRs welcome. 🙂

GitHub

View Github