v2.0.0-pre1: make compatible with GoReleaser
This commit is contained in:
parent
37c1fd4b56
commit
b667843a2b
|
@ -1,6 +1,9 @@
|
||||||
xversion.go
|
xversion.go
|
||||||
zversion.go
|
zversion.go
|
||||||
|
|
||||||
|
/go-gitver
|
||||||
|
examples/**.sum
|
||||||
|
|
||||||
# ---> Go
|
# ---> Go
|
||||||
# Binaries for programs and plugins
|
# Binaries for programs and plugins
|
||||||
*.exe
|
*.exe
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
{}
|
48
README.md
48
README.md
|
@ -1,13 +1,15 @@
|
||||||
# git-version.go
|
# [Go GitVer](https://git.rootprojects.org/root/go-gitver)
|
||||||
|
|
||||||
Use **git tags** to add [**semver**](https://semver.org/) to your go package in under 150 [lines of code](https://git.rootprojects.org/root/go-gitver/src/branch/master/gitver/gitver.go).
|
Use **git tags** to add (GoRelesear-compatible) [**semver**](https://semver.org/)
|
||||||
|
to your go package in under 150
|
||||||
|
[lines of code](https://git.rootprojects.org/root/go-gitver/src/branch/master/gitver/gitver.go).
|
||||||
|
|
||||||
```txt
|
```txt
|
||||||
Goals:
|
Goals:
|
||||||
|
|
||||||
1. Use an exact `git tag` version, like v1.0.0, when clean
|
1. Use an exact `git tag` version, like v1.0.0, when clean
|
||||||
2. Translate the `git describe` version (v1.0.0-4-g0000000)
|
2. Translate the `git describe` version (v1.0.0-4-g0000000)
|
||||||
to semver (v1.0.1-pre4+g0000000) in between releases
|
to semver (1.0.1-pre4+g0000000) in between releases
|
||||||
3. Note when `dirty` (and have build timestamp)
|
3. Note when `dirty` (and have build timestamp)
|
||||||
|
|
||||||
Fail gracefully when git repo isn't available.
|
Fail gracefully when git repo isn't available.
|
||||||
|
@ -17,20 +19,20 @@ Goals:
|
||||||
|
|
||||||
1. You define the fallback version and version printing in `main.go`:
|
1. You define the fallback version and version printing in `main.go`:
|
||||||
|
|
||||||
```
|
```go
|
||||||
//go:generate go run git.rootprojects.org/root/go-gitver
|
//go:generate go run git.rootprojects.org/root/go-gitver
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if (len(os.Args) > 1 && "version" === os.Args[1]) {
|
if (len(os.Args) > 1 && "version" === os.Args[1]) {
|
||||||
fmt.Printf("Foobar %s (%s)\n", GitVersion, GitTimestamp)
|
fmt.Printf("Foobar v%s (%s) %s\n", version, commit[:7], date)
|
||||||
}
|
}
|
||||||
// ...
|
// ...
|
||||||
}
|
}
|
||||||
|
@ -38,13 +40,13 @@ func main() {
|
||||||
|
|
||||||
2. You `go generate` or `go run git.rootprojects.org/root/go-gitver` to generate `xversion.go`:
|
2. You `go generate` or `go run git.rootprojects.org/root/go-gitver` to generate `xversion.go`:
|
||||||
|
|
||||||
```
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
GitRev = "0921ed1e"
|
commit = "0921ed1e"
|
||||||
GitVersion = "v1.1.2"
|
version = "1.1.2"
|
||||||
GitTimestamp = "2019-07-01T02:32:58-06:00"
|
date = "2019-07-01T02:32:58-06:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -62,9 +64,9 @@ cat xversion.go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
GitRev = "6dace8255b52e123297a44629bc32c015add310a"
|
commit = "6dace8255b52e123297a44629bc32c015add310a"
|
||||||
GitVersion = "v1.1.4-pre2+g6dace82"
|
version = "1.1.4-pre2+g6dace82"
|
||||||
GitTimestamp = "2020-07-16T20:48:15-06:00"
|
date = "2020-07-16T20:48:15-06:00"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -147,7 +149,7 @@ See `examples/basic`
|
||||||
1. Create a `tools` package in your project
|
1. Create a `tools` package in your project
|
||||||
2. Guard it against regular builds with `// +build tools`
|
2. Guard it against regular builds with `// +build tools`
|
||||||
3. Include `_ "git.rootprojects.org/root/go-gitver"` in the imports
|
3. Include `_ "git.rootprojects.org/root/go-gitver"` in the imports
|
||||||
4. Declare `var GitRev, GitVersion, GitTimestamp string` in your `package main`
|
4. Declare `var commit, version, date string` in your `package main`
|
||||||
5. Include `//go:generate go run -mod=vendor git.rootprojects.org/root/go-gitver` as well
|
5. Include `//go:generate go run -mod=vendor git.rootprojects.org/root/go-gitver` as well
|
||||||
|
|
||||||
`tools/tools.go`:
|
`tools/tools.go`:
|
||||||
|
@ -173,15 +175,15 @@ package main
|
||||||
import "fmt"
|
import "fmt"
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(GitRev)
|
fmt.Println(commit)
|
||||||
fmt.Println(GitVersion)
|
fmt.Println(version)
|
||||||
fmt.Println(GitTimestamp)
|
fmt.Println(date)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -18,9 +18,9 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
fmt.Println(GitRev)
|
fmt.Println(commit)
|
||||||
fmt.Println(GitVersion)
|
fmt.Println(version)
|
||||||
fmt.Println(GitTimestamp)
|
fmt.Println(date)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -18,9 +18,9 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
fmt.Println(GitRev)
|
fmt.Println(commit)
|
||||||
fmt.Println(GitVersion)
|
fmt.Println(version)
|
||||||
fmt.Println(GitTimestamp)
|
fmt.Println(date)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,9 +14,9 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
fmt.Println(version.GitRev)
|
fmt.Println(version.commit)
|
||||||
fmt.Println(version.GitVersion)
|
fmt.Println(version.version)
|
||||||
fmt.Println(version.GitTimestamp)
|
fmt.Println(version.date)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package version
|
package version
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
|
@ -12,9 +12,9 @@ func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *showVersion {
|
if *showVersion {
|
||||||
fmt.Println(version.GitRev)
|
fmt.Println(version.commit)
|
||||||
fmt.Println(version.GitVersion)
|
fmt.Println(version.version)
|
||||||
fmt.Println(version.GitTimestamp)
|
fmt.Println(version.date)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
package version
|
package version
|
||||||
|
|
||||||
var (
|
var (
|
||||||
GitRev = "0000000"
|
commit = "0000000"
|
||||||
GitVersion = "v0.0.0-pre0+0000000"
|
version = "0.0.0-pre0+0000000"
|
||||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
date = "0000-00-00T00:00:00+0000"
|
||||||
)
|
)
|
||||||
|
|
20
gitver.go
20
gitver.go
|
@ -13,7 +13,7 @@ import (
|
||||||
"git.rootprojects.org/root/go-gitver/gitver"
|
"git.rootprojects.org/root/go-gitver/gitver"
|
||||||
)
|
)
|
||||||
|
|
||||||
var GitRev, GitVersion, GitTimestamp string
|
var commit, version, date string
|
||||||
var exitCode int
|
var exitCode int
|
||||||
var verFile = "xversion.go"
|
var verFile = "xversion.go"
|
||||||
|
|
||||||
|
@ -32,9 +32,9 @@ func main() {
|
||||||
pkg = args[i+1]
|
pkg = args[i+1]
|
||||||
args[i+1] = ""
|
args[i+1] = ""
|
||||||
} else if "-V" == arg || "version" == arg || "-version" == arg || "--version" == arg {
|
} else if "-V" == arg || "version" == arg || "-version" == arg || "--version" == arg {
|
||||||
fmt.Println(GitRev)
|
fmt.Println(commit)
|
||||||
fmt.Println(GitVersion)
|
fmt.Println(version)
|
||||||
fmt.Println(GitTimestamp)
|
fmt.Println(date)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,12 +59,12 @@ func main() {
|
||||||
Package string
|
Package string
|
||||||
Timestamp string
|
Timestamp string
|
||||||
Version string
|
Version string
|
||||||
GitRev string
|
Commit string
|
||||||
}{
|
}{
|
||||||
Package: pkg,
|
Package: pkg,
|
||||||
Timestamp: v.Timestamp.Format(time.RFC3339),
|
Timestamp: v.Timestamp.Format(time.RFC3339),
|
||||||
Version: v.Version,
|
Version: v.Version,
|
||||||
GitRev: v.Rev,
|
Commit: v.Rev,
|
||||||
}); nil != err {
|
}); nil != err {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
@ -93,12 +93,12 @@ var versionTpl = template.Must(template.New("").Parse(`// Code generated by go g
|
||||||
package {{ .Package }}
|
package {{ .Package }}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
{{ if .GitRev -}}
|
{{ if .Commit -}}
|
||||||
GitRev = "{{ .GitRev }}"
|
commit = "{{ .Commit }}"
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
{{ if .Version -}}
|
{{ if .Version -}}
|
||||||
GitVersion = "{{ .Version }}"
|
version = "{{ .Version }}"
|
||||||
{{ end -}}
|
{{ end -}}
|
||||||
GitTimestamp = "{{ .Timestamp }}"
|
date = "{{ .Timestamp }}"
|
||||||
}
|
}
|
||||||
`))
|
`))
|
||||||
|
|
|
@ -21,12 +21,14 @@ func init() {
|
||||||
gitVer = regexp.MustCompile(`^(v\d+\.\d+)\.(\d+)(-(\d+))?(-(g[0-9a-f]+))?(-(dirty))?`)
|
gitVer = regexp.MustCompile(`^(v\d+\.\d+)\.(\d+)(-(\d+))?(-(g[0-9a-f]+))?(-(dirty))?`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Versions describes the various version properties
|
||||||
type Versions struct {
|
type Versions struct {
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
Version string
|
Version string
|
||||||
Rev string
|
Rev string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecAndParse will run git and parse the output
|
||||||
func ExecAndParse() (*Versions, error) {
|
func ExecAndParse() (*Versions, error) {
|
||||||
desc, err := gitDesc()
|
desc, err := gitDesc()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -58,7 +60,7 @@ func gitDesc() (string, error) {
|
||||||
out, err := cmd.CombinedOutput()
|
out, err := cmd.CombinedOutput()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
// Don't panic, just carry on
|
// Don't panic, just carry on
|
||||||
//out = []byte("v0.0.0-0-g0000000")
|
//out = []byte("0.0.0-0-g0000000")
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return strings.TrimSpace(string(out)), nil
|
return strings.TrimSpace(string(out)), nil
|
||||||
|
@ -83,7 +85,7 @@ func gitRev() (string, error) {
|
||||||
func semVer(desc string) (string, error) {
|
func semVer(desc string) (string, error) {
|
||||||
if exactVer.MatchString(desc) {
|
if exactVer.MatchString(desc) {
|
||||||
// v1.0.0
|
// v1.0.0
|
||||||
return desc, nil
|
return strings.TrimPrefix(desc, "v"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !gitVer.MatchString(desc) {
|
if !gitVer.MatchString(desc) {
|
||||||
|
@ -122,10 +124,19 @@ func semVer(desc string) (string, error) {
|
||||||
ver += vers[8]
|
ver += vers[8]
|
||||||
}
|
}
|
||||||
|
|
||||||
return ver, nil
|
return strings.TrimPrefix(ver, "v"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gitTimestamp(desc string) (time.Time, error) {
|
func gitTimestamp(desc string) (time.Time, error) {
|
||||||
|
// Other options:
|
||||||
|
//
|
||||||
|
// Commit Date
|
||||||
|
// git log -1 --format=%cd --date=format:%Y-%m-%dT%H:%M:%SZ%z
|
||||||
|
//
|
||||||
|
// Author Date
|
||||||
|
// git log -1 --format=%ad --date=format:%Y-%m-%dT%H:%M:%SZ%z
|
||||||
|
//
|
||||||
|
// I think I chose this because it would account for dirty-ness better... maybe?
|
||||||
args := []string{
|
args := []string{
|
||||||
"git",
|
"git",
|
||||||
"show", desc,
|
"show", desc,
|
||||||
|
|
|
@ -3,7 +3,7 @@ package main
|
||||||
// use recently generated version info as a fallback
|
// use recently generated version info as a fallback
|
||||||
// for when git isn't present (i.e. go run <url>)
|
// for when git isn't present (i.e. go run <url>)
|
||||||
func init() {
|
func init() {
|
||||||
GitRev = "0921ed1e6007493c886c87ee9a15f2cceecb1f9f"
|
commit = "37c1fd4b5694fd62c9f0d6ad1df47d938accbeec"
|
||||||
GitVersion = "v1.1.2"
|
version = "2.0.0-pre1-dirty"
|
||||||
GitTimestamp = "2019-07-01T02:32:58-06:00"
|
date = "2020-10-10T16:05:59-06:00"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue