diff --git a/README.md b/README.md
index 64c000f..478595a 100644
--- a/README.md
+++ b/README.md
@@ -13,6 +13,7 @@ Stop worrying about `-ldflags` and **`go get github.com/ahmetalpbalkan/govvv`**
| **`main.GitCommit`** | short commit hash of source tree | `0b5ed7a` |
| **`main.GitBranch`** | current branch name the code is built off | `master` |
| **`main.GitState`** | whether there are uncommitted changes | `clean` or `dirty` |
+| **`main.GitSummary`** | output of `git describe --tags --dirty --always` | `v1.0.0`,
`v1.0.1-5-g585c78f-dirty`,
`fbd157c` |
| **`main.BuildDate`** | RFC3339 formatted UTC date | `2016-08-04T18:07:54Z` |
| **`main.Version`** | contents of `./VERSION` file, if exists | `2.0.0` |
diff --git a/git.go b/git.go
index 95c967f..7669735 100644
--- a/git.go
+++ b/git.go
@@ -53,3 +53,12 @@ func (g git) Branch() string {
}
return out
}
+
+// Summary returns the output of "git describe --tags --dirty --always".
+func (g git) Summary() (string, error) {
+ out, err := g.exec("describe", "--tags", "--dirty", "--always")
+ if err != nil {
+ return "", err
+ }
+ return out, err
+}
diff --git a/git_test.go b/git_test.go
index b5bd791..809aec0 100644
--- a/git_test.go
+++ b/git_test.go
@@ -29,12 +29,12 @@ func TestCommit(t *testing.T) {
mkCommit(t, repo, "commit 1")
c1, err := repo.Commit()
require.Nil(t, err)
- require.NotEmpty(t, c1)
+ require.Regexp(t, "^[0-9a-f]{4,15}$", c1)
mkCommit(t, repo, "commit 2")
c2, err := repo.Commit()
require.Nil(t, err)
- require.NotEmpty(t, c2)
+ require.Regexp(t, "^[0-9a-f]{4,15}$", c2)
// commit hash changed
require.NotEqual(t, c1, c2)
@@ -83,6 +83,41 @@ func TestBranch(t *testing.T) {
require.EqualValues(t, "foo", repo.Branch())
}
+func TestSummary(t *testing.T) {
+ repo := newRepo(t)
+ defer os.RemoveAll(repo.dir)
+
+ // no tags yet, should be just short commit number
+ mkCommit(t, repo, "commit 1")
+ s, err := repo.Summary()
+ require.Nil(t, err)
+ require.Regexp(t, "^[0-9a-f]{4,15}$", s)
+
+ // if commit is a tag, tag is returned
+ _, err = repo.exec("tag", "v1.0.0")
+ require.Nil(t, err)
+ s, err = repo.Summary()
+ require.Nil(t, err)
+ require.EqualValues(t, "v1.0.0", s)
+
+ // add 3 more commits, it should be in format v1.0.0-2-*
+ mkCommit(t, repo, "commit 2")
+ mkCommit(t, repo, "commit 3")
+ s, err = repo.Summary()
+ require.Nil(t, err)
+ require.Regexp(t, "^v1.0.0-2-.*$", s)
+
+ // add a dirty file
+ f, err := ioutil.TempFile(repo.dir, "") // contaminate
+ require.Nil(t, err, "failed to create test file")
+ f.Close()
+ _, err = repo.exec("add", f.Name())
+ require.Nil(t, err)
+ s, err = repo.Summary()
+ require.Nil(t, err)
+ require.Regexp(t, ".*-dirty$", s)
+}
+
// Test utilities
func newRepo(t *testing.T) git {
diff --git a/integration-test/app-example/main.go b/integration-test/app-example/main.go
index ac65162..edd4705 100644
--- a/integration-test/app-example/main.go
+++ b/integration-test/app-example/main.go
@@ -4,10 +4,11 @@ import "fmt"
var (
// These fields are populated by govvv
- BuildDate string
- GitCommit string
- GitBranch string
- GitState string
+ BuildDate string
+ GitCommit string
+ GitBranch string
+ GitState string
+ GitSummary string
)
func main() {
@@ -15,4 +16,5 @@ func main() {
fmt.Printf("GitCommit=%s\n", GitCommit)
fmt.Printf("GitBranch=%s\n", GitBranch)
fmt.Printf("GitState=%s\n", GitState)
+ fmt.Printf("GitSummary=%s\n", GitSummary)
}
diff --git a/integration-test/test.bats b/integration-test/test.bats
index 7468f31..091917a 100644
--- a/integration-test/test.bats
+++ b/integration-test/test.bats
@@ -62,6 +62,7 @@
[[ "${lines[1]}" =~ ^GitCommit=[0-9a-f]{4,15}$ ]]
[[ "${lines[2]}" =~ ^GitBranch=(.*)$ ]]
[[ "${lines[3]}" =~ ^GitState=(clean|dirty)$ ]]
+ [[ "${lines[4]}" =~ ^GitSummary=(.*)$ ]]
}
@test "govvv build - preserves given -ldflags" {
diff --git a/values.go b/values.go
index 50a7fde..bf15c1c 100644
--- a/values.go
+++ b/values.go
@@ -23,12 +23,17 @@ func GetFlags(dir string) (map[string]string, error) {
if err != nil {
return nil, fmt.Errorf("failed to get repository state: %v", err)
}
+ gitSummary, err := repo.Summary()
+ if err != nil {
+ return nil, fmt.Errorf("failed to get repository summary: %v", err)
+ }
v := map[string]string{
- "main.BuildDate": date(),
- "main.GitCommit": gitCommit,
- "main.GitBranch": gitBranch,
- "main.GitState": gitState,
+ "main.BuildDate": date(),
+ "main.GitCommit": gitCommit,
+ "main.GitBranch": gitBranch,
+ "main.GitState": gitState,
+ "main.GitSummary": gitSummary,
}
if version, err := versionFromFile(dir); err != nil {
diff --git a/values_test.go b/values_test.go
index 00a3e4b..a0459c6 100644
--- a/values_test.go
+++ b/values_test.go
@@ -39,6 +39,7 @@ func TestGetValues(t *testing.T) {
require.Regexp(t, "^[0-9a-f]{4,15}$", fl["main.GitCommit"])
require.Equal(t, "master", fl["main.GitBranch"])
require.Equal(t, "clean", fl["main.GitState"])
+ require.Equal(t, fl["main.GitCommit"], fl["main.GitSummary"])
}
func TestGetValues_versionFlag(t *testing.T) {