kopia lustrzana https://github.com/therootcompany/go-gitver.git
v2.0.0-pre1: make compatible with GoReleaser
This commit is contained in:
rodzic
37c1fd4b56
commit
b667843a2b
|
@ -1,6 +1,9 @@
|
|||
xversion.go
|
||||
zversion.go
|
||||
|
||||
/go-gitver
|
||||
examples/**.sum
|
||||
|
||||
# ---> Go
|
||||
# Binaries for programs and plugins
|
||||
*.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
|
||||
Goals:
|
||||
Goals:
|
||||
|
||||
1. Use an exact `git tag` version, like v1.0.0, when clean
|
||||
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)
|
||||
|
||||
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`:
|
||||
|
||||
```
|
||||
```go
|
||||
//go:generate go run git.rootprojects.org/root/go-gitver
|
||||
|
||||
package main
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
date = "0000-00-00T00:00:00+0000"
|
||||
)
|
||||
|
||||
func main() {
|
||||
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`:
|
||||
|
||||
```
|
||||
```go
|
||||
package main
|
||||
|
||||
func init() {
|
||||
GitRev = "0921ed1e"
|
||||
GitVersion = "v1.1.2"
|
||||
GitTimestamp = "2019-07-01T02:32:58-06:00"
|
||||
commit = "0921ed1e"
|
||||
version = "1.1.2"
|
||||
date = "2019-07-01T02:32:58-06:00"
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -62,9 +64,9 @@ cat xversion.go
|
|||
package main
|
||||
|
||||
func init() {
|
||||
GitRev = "6dace8255b52e123297a44629bc32c015add310a"
|
||||
GitVersion = "v1.1.4-pre2+g6dace82"
|
||||
GitTimestamp = "2020-07-16T20:48:15-06:00"
|
||||
commit = "6dace8255b52e123297a44629bc32c015add310a"
|
||||
version = "1.1.4-pre2+g6dace82"
|
||||
date = "2020-07-16T20:48:15-06:00"
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -147,7 +149,7 @@ See `examples/basic`
|
|||
1. Create a `tools` package in your project
|
||||
2. Guard it against regular builds with `// +build tools`
|
||||
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
|
||||
|
||||
`tools/tools.go`:
|
||||
|
@ -173,15 +175,15 @@ package main
|
|||
import "fmt"
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
date = "0000-00-00T00:00:00+0000"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(GitRev)
|
||||
fmt.Println(GitVersion)
|
||||
fmt.Println(GitTimestamp)
|
||||
fmt.Println(commit)
|
||||
fmt.Println(version)
|
||||
fmt.Println(date)
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -8,9 +8,9 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
date = "0000-00-00T00:00:00+0000"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -18,9 +18,9 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println(GitRev)
|
||||
fmt.Println(GitVersion)
|
||||
fmt.Println(GitTimestamp)
|
||||
fmt.Println(commit)
|
||||
fmt.Println(version)
|
||||
fmt.Println(date)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -8,9 +8,9 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
date = "0000-00-00T00:00:00+0000"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -18,9 +18,9 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println(GitRev)
|
||||
fmt.Println(GitVersion)
|
||||
fmt.Println(GitTimestamp)
|
||||
fmt.Println(commit)
|
||||
fmt.Println(version)
|
||||
fmt.Println(date)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -14,9 +14,9 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println(version.GitRev)
|
||||
fmt.Println(version.GitVersion)
|
||||
fmt.Println(version.GitTimestamp)
|
||||
fmt.Println(version.commit)
|
||||
fmt.Println(version.version)
|
||||
fmt.Println(version.date)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package version
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
date = "0000-00-00T00:00:00+0000"
|
||||
)
|
||||
|
|
|
@ -12,9 +12,9 @@ func main() {
|
|||
flag.Parse()
|
||||
|
||||
if *showVersion {
|
||||
fmt.Println(version.GitRev)
|
||||
fmt.Println(version.GitVersion)
|
||||
fmt.Println(version.GitTimestamp)
|
||||
fmt.Println(version.commit)
|
||||
fmt.Println(version.version)
|
||||
fmt.Println(version.date)
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
package version
|
||||
|
||||
var (
|
||||
GitRev = "0000000"
|
||||
GitVersion = "v0.0.0-pre0+0000000"
|
||||
GitTimestamp = "0000-00-00T00:00:00+0000"
|
||||
commit = "0000000"
|
||||
version = "0.0.0-pre0+0000000"
|
||||
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"
|
||||
)
|
||||
|
||||
var GitRev, GitVersion, GitTimestamp string
|
||||
var commit, version, date string
|
||||
var exitCode int
|
||||
var verFile = "xversion.go"
|
||||
|
||||
|
@ -32,9 +32,9 @@ func main() {
|
|||
pkg = args[i+1]
|
||||
args[i+1] = ""
|
||||
} else if "-V" == arg || "version" == arg || "-version" == arg || "--version" == arg {
|
||||
fmt.Println(GitRev)
|
||||
fmt.Println(GitVersion)
|
||||
fmt.Println(GitTimestamp)
|
||||
fmt.Println(commit)
|
||||
fmt.Println(version)
|
||||
fmt.Println(date)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
@ -59,12 +59,12 @@ func main() {
|
|||
Package string
|
||||
Timestamp string
|
||||
Version string
|
||||
GitRev string
|
||||
Commit string
|
||||
}{
|
||||
Package: pkg,
|
||||
Timestamp: v.Timestamp.Format(time.RFC3339),
|
||||
Version: v.Version,
|
||||
GitRev: v.Rev,
|
||||
Commit: v.Rev,
|
||||
}); nil != err {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -93,12 +93,12 @@ var versionTpl = template.Must(template.New("").Parse(`// Code generated by go g
|
|||
package {{ .Package }}
|
||||
|
||||
func init() {
|
||||
{{ if .GitRev -}}
|
||||
GitRev = "{{ .GitRev }}"
|
||||
{{ if .Commit -}}
|
||||
commit = "{{ .Commit }}"
|
||||
{{ end -}}
|
||||
{{ if .Version -}}
|
||||
GitVersion = "{{ .Version }}"
|
||||
version = "{{ .Version }}"
|
||||
{{ end -}}
|
||||
GitTimestamp = "{{ .Timestamp }}"
|
||||
date = "{{ .Timestamp }}"
|
||||
}
|
||||
`))
|
||||
|
|
|
@ -21,12 +21,14 @@ func init() {
|
|||
gitVer = regexp.MustCompile(`^(v\d+\.\d+)\.(\d+)(-(\d+))?(-(g[0-9a-f]+))?(-(dirty))?`)
|
||||
}
|
||||
|
||||
// Versions describes the various version properties
|
||||
type Versions struct {
|
||||
Timestamp time.Time
|
||||
Version string
|
||||
Rev string
|
||||
}
|
||||
|
||||
// ExecAndParse will run git and parse the output
|
||||
func ExecAndParse() (*Versions, error) {
|
||||
desc, err := gitDesc()
|
||||
if nil != err {
|
||||
|
@ -58,7 +60,7 @@ func gitDesc() (string, error) {
|
|||
out, err := cmd.CombinedOutput()
|
||||
if nil != err {
|
||||
// Don't panic, just carry on
|
||||
//out = []byte("v0.0.0-0-g0000000")
|
||||
//out = []byte("0.0.0-0-g0000000")
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(out)), nil
|
||||
|
@ -83,7 +85,7 @@ func gitRev() (string, error) {
|
|||
func semVer(desc string) (string, error) {
|
||||
if exactVer.MatchString(desc) {
|
||||
// v1.0.0
|
||||
return desc, nil
|
||||
return strings.TrimPrefix(desc, "v"), nil
|
||||
}
|
||||
|
||||
if !gitVer.MatchString(desc) {
|
||||
|
@ -122,10 +124,19 @@ func semVer(desc string) (string, error) {
|
|||
ver += vers[8]
|
||||
}
|
||||
|
||||
return ver, nil
|
||||
return strings.TrimPrefix(ver, "v"), nil
|
||||
}
|
||||
|
||||
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{
|
||||
"git",
|
||||
"show", desc,
|
||||
|
|
|
@ -3,7 +3,7 @@ package main
|
|||
// use recently generated version info as a fallback
|
||||
// for when git isn't present (i.e. go run <url>)
|
||||
func init() {
|
||||
GitRev = "0921ed1e6007493c886c87ee9a15f2cceecb1f9f"
|
||||
GitVersion = "v1.1.2"
|
||||
GitTimestamp = "2019-07-01T02:32:58-06:00"
|
||||
commit = "37c1fd4b5694fd62c9f0d6ad1df47d938accbeec"
|
||||
version = "2.0.0-pre1-dirty"
|
||||
date = "2020-10-10T16:05:59-06:00"
|
||||
}
|
||||
|
|
Ładowanie…
Reference in New Issue