Skip to content

Commit

Permalink
Do not check for errors in Latesttag and AncestorTag
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed May 13, 2021
1 parent 1a483ce commit b158821
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 36 deletions.
14 changes: 4 additions & 10 deletions cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ const tagDefault = "0.0.0"
type gitClient interface {
CurrentBranch() (string, error)
IsRepo() bool
LatestTag() (string, error)
AncestorTag(include, exclude string) (string, error)
LatestTag() string
AncestorTag(include, exclude string) string
SourceBranch(commitHash string) (string, error)
}

Expand Down Expand Up @@ -88,10 +88,7 @@ func Tag(params Params, gc gitClient) (Result, error) {

log.Debugf("method: %q, version: %q", method, version)

latestTag, err := gc.LatestTag()
if err != nil {
return Result{}, fmt.Errorf("failed getting latest tag: %s", err)
}
latestTag := gc.LatestTag()

var (
tag *semver.Version
Expand Down Expand Up @@ -190,10 +187,7 @@ func Tag(params Params, gc gitClient) (Result, error) {
finalTag = params.Prefix + tag.FinalizeVersion()
}

ancestorTag, err = gc.AncestorTag(includePattern, excludePattern)
if err != nil {
return Result{}, fmt.Errorf("failed getting ancestor tag: %s", err)
}
ancestorTag = gc.AncestorTag(includePattern, excludePattern)

return Result{
PreviousTag: previousTag,
Expand Down
16 changes: 8 additions & 8 deletions cmd/generate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ type gitClientMock struct {
CurrentBranchFnInvoked int
IsRepoFn func() bool
IsRepoFnInvoked int
LatestTagFn func() (string, error)
LatestTagFn func() string
LatestTagFnInvoked int
AncestorTagFn func(include, exclude string) (string, error)
AncestorTagFn func(include, exclude string) string
AncestorTagFnInvoked int
SourceBranchFn func(commitHash string) (string, error)
SourceBranchFnInvoked int
Expand All @@ -294,11 +294,11 @@ func initGitClientMock(t *testing.T, latestTag, ancestorTag, currentBranch, sour
IsRepoFn: func() bool {
return true
},
LatestTagFn: func() (string, error) {
return latestTag, nil
LatestTagFn: func() string {
return latestTag
},
AncestorTagFn: func(include, exclude string) (string, error) {
return ancestorTag, nil
AncestorTagFn: func(include, exclude string) string {
return ancestorTag
},
SourceBranchFn: func(commitHash string) (string, error) {
assert.Equal(t, expectedCommitHash, commitHash)
Expand All @@ -316,12 +316,12 @@ func (m *gitClientMock) IsRepo() bool {
return m.IsRepoFn()
}

func (m *gitClientMock) LatestTag() (string, error) {
func (m *gitClientMock) LatestTag() string {
m.LatestTagFnInvoked += 1
return m.LatestTagFn()
}

func (m *gitClientMock) AncestorTag(include, exclude string) (string, error) {
func (m *gitClientMock) AncestorTag(include, exclude string) string {
m.AncestorTagFnInvoked += 1
return m.AncestorTagFn(include, exclude)
}
Expand Down
20 changes: 10 additions & 10 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ func (c *Client) SourceBranch(commitHash string) (string, error) {
}

// LatestTag returns the latest tag if found.
func (c *Client) LatestTag() (string, error) {
result, err := c.Clean(c.Run("-C", c.repoDir, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate"))
if err != nil || result == "" {
return c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0"))
func (c *Client) LatestTag() string {
result, _ := c.Clean(c.Run("-C", c.repoDir, "tag", "--points-at", "HEAD", "--sort", "-version:creatordate"))
if result == "" {
result, _ = c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0"))
}
return result, err
return result
}

// AncestorTag returns the previous tag that matches specific pattern if found.
func (c *Client) AncestorTag(include, exclude string) (string, error) {
result, err := c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0", "--match", include, "--exclude", exclude))
if err != nil || result == "" {
return c.Clean(c.Run("-C", c.repoDir, "rev-list", "--max-parents=0", "HEAD"))
func (c *Client) AncestorTag(include, exclude string) string {
result, _ := c.Clean(c.Run("-C", c.repoDir, "describe", "--tags", "--abbrev=0", "--match", include, "--exclude", exclude))
if result == "" {
result, _ = c.Clean(c.Run("-C", c.repoDir, "rev-list", "--max-parents=0", "HEAD"))
}
return result, err
return result
}
12 changes: 4 additions & 8 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func TestLatestTag(t *testing.T) {
return "v2.4.79", nil
}

value, err := gc.LatestTag()
require.NoError(t, err)
value := gc.LatestTag()

assert.Equal(t, "v2.4.79", value)
}
Expand All @@ -138,8 +137,7 @@ func TestLatestTag_NoTagFound(t *testing.T) {
return "", nil
}

value, err := gc.LatestTag()
require.NoError(t, err)
value := gc.LatestTag()

assert.Empty(t, value)
}
Expand Down Expand Up @@ -171,8 +169,7 @@ func TestAncestorTag(t *testing.T) {
return test.ExpectedTag, nil
}

value, err := gc.AncestorTag(test.IncludePattern, test.ExcludePattern)
require.NoError(t, err)
value := gc.AncestorTag(test.IncludePattern, test.ExcludePattern)

assert.Equal(t, test.ExpectedTag, value)
})
Expand All @@ -198,8 +195,7 @@ func TestAncestorTag_NoTagFound(t *testing.T) {
return "", nil
}

value, err := gc.AncestorTag("", "")
require.NoError(t, err)
value := gc.AncestorTag("", "")

assert.Empty(t, value)
}

0 comments on commit b158821

Please sign in to comment.