From 9e13685040360ed0a4ae7e4ee4def7c582a424dc Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 15 Apr 2017 17:04:26 -0300 Subject: [PATCH] improved git pipe --- pipeline/git/git.go | 65 ++++++++++++++++++++-------------------- pipeline/git/git_test.go | 2 +- 2 files changed, 34 insertions(+), 33 deletions(-) diff --git a/pipeline/git/git.go b/pipeline/git/git.go index 3a7569d80..172f48046 100644 --- a/pipeline/git/git.go +++ b/pipeline/git/git.go @@ -3,6 +3,7 @@ package git import ( + "fmt" "regexp" "strings" @@ -15,7 +16,7 @@ type ErrInvalidVersionFormat struct { } func (e ErrInvalidVersionFormat) Error() string { - return e.version + " is not in a valid version format" + return fmt.Sprintf("%v is not in a valid version format", e.version) } // ErrDirty happens when the repo has uncommitted/unstashed changes @@ -24,16 +25,16 @@ type ErrDirty struct { } func (e ErrDirty) Error() string { - return "git is currently in a dirty state:\n" + e.status + return fmt.Sprintf("git is currently in a dirty state:\n%v", e.status) } // ErrWrongRef happens when the HEAD reference is different from the tag being built type ErrWrongRef struct { - status string + commit, tag string } func (e ErrWrongRef) Error() string { - return e.status + return fmt.Sprintf("git tag %v was not made against commit %v", e.tag, e.commit) } // Pipe for brew deployment @@ -46,46 +47,54 @@ func (Pipe) Description() string { // Run the pipe func (Pipe) Run(ctx *context.Context) (err error) { - tag, err := cleanGit("describe", "--tags", "--abbrev=0", "--always") + tag, prev, commit, log, err := getInfo() if err != nil { return } - prev, err := previous(tag) - if err != nil { - return - } - - log, err := git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) - if err != nil { - return - } - ctx.Git = context.GitInfo{ CurrentTag: tag, PreviousTag: prev, Diff: log, + Commit: commit, } // removes usual `v` prefix ctx.Version = strings.TrimPrefix(tag, "v") - if versionErr := isVersionValid(ctx.Version); versionErr != nil { - return versionErr + return validate(commit, tag, ctx.Version) +} + +func validate(commit, tag, version string) error { + matches, err := regexp.MatchString("^[0-9.]+", version) + if err != nil || !matches { + return ErrInvalidVersionFormat{version} } - commit, err := cleanGit("show", "--format='%H'", "HEAD") - if err != nil { - return - } - ctx.Git.Commit = commit - out, err := git("diff") + out, err := git("status", "-s") if strings.TrimSpace(out) != "" || err != nil { return ErrDirty{out} } _, err = cleanGit("describe", "--exact-match", "--tags", "--match", tag) if err != nil { - return ErrWrongRef{err.Error()} + return ErrWrongRef{commit, tag} } return nil } +func getInfo() (tag, prev, commit, log string, err error) { + tag, err = cleanGit("describe", "--tags", "--abbrev=0", "--always") + if err != nil { + return + } + prev, err = previous(tag) + if err != nil { + return + } + log, err = git("log", "--pretty=oneline", "--abbrev-commit", prev+".."+tag) + if err != nil { + return + } + commit, err = cleanGit("show", "--format='%H'", "HEAD") + return +} + func previous(tag string) (previous string, err error) { previous, err = cleanGit("describe", "--tags", "--abbrev=0", "--always", tag+"^") if err != nil { @@ -93,11 +102,3 @@ func previous(tag string) (previous string, err error) { } return } - -func isVersionValid(version string) error { - matches, err := regexp.MatchString("^[0-9.]+", version) - if err != nil || !matches { - return ErrInvalidVersionFormat{version} - } - return nil -} diff --git a/pipeline/git/git_test.go b/pipeline/git/git_test.go index a6b61a362..39b5d1adb 100644 --- a/pipeline/git/git_test.go +++ b/pipeline/git/git_test.go @@ -96,7 +96,7 @@ func TestTagIsNotLastCommit(t *testing.T) { } err := Pipe{}.Run(ctx) assert.Error(err) - assert.Contains(err.Error(), "fatal: no tag exactly matches") + assert.Contains(err.Error(), "git tag v0.0.1 was not made against commit") } //