Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gadd GitSummary value from git-describe #2

Merged
merged 1 commit into from
Aug 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`, <br/>`v1.0.1-5-g585c78f-dirty`, <br/> `fbd157c` |
| **`main.BuildDate`** | RFC3339 formatted UTC date | `2016-08-04T18:07:54Z` |
| **`main.Version`** | contents of `./VERSION` file, if exists | `2.0.0` |

Expand Down
9 changes: 9 additions & 0 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
39 changes: 37 additions & 2 deletions git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions integration-test/app-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ 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() {
fmt.Printf("BuildDate=%s\n", BuildDate)
fmt.Printf("GitCommit=%s\n", GitCommit)
fmt.Printf("GitBranch=%s\n", GitBranch)
fmt.Printf("GitState=%s\n", GitState)
fmt.Printf("GitSummary=%s\n", GitSummary)
}
1 change: 1 addition & 0 deletions integration-test/test.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
13 changes: 9 additions & 4 deletions values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down