Skip to content

Commit ffab287

Browse files
committed
refactor: memoize tags refs
1 parent 033e3af commit ffab287

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

gitutils.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"github.com/go-git/go-git/v5/plumbing/object"
1212
)
1313

14+
var (
15+
latestTag *plumbing.Reference
16+
previousTag *plumbing.Reference
17+
)
18+
1419
// GetLatestTagFromRepository - Get the latest Tag reference from the repo
1520
func GetLatestTagFromRepository(repository *git.Repository) (*plumbing.Reference, *plumbing.Reference, error) {
1621
tagRefs, err := repository.Tags()
@@ -61,7 +66,13 @@ func GetLatestTagFromRepository(repository *git.Repository) (*plumbing.Reference
6166

6267
// isCommitToNearestTag - go through git revisions to find the latest tag and the nearest next tag
6368
func isCommitToNearestTag(repo *git.Repository, commit *object.Commit) bool {
64-
latestTag, previousTag, err := GetLatestTagFromRepository(repo)
69+
if latestTag == nil || previousTag == nil {
70+
var err error
71+
latestTag, previousTag, err = GetLatestTagFromRepository(repo)
72+
if err != nil {
73+
log.Fatal("Error getting latest tags from repository")
74+
}
75+
}
6576

6677
ref, err := repo.Head()
6778

@@ -77,9 +88,9 @@ func isCommitToNearestTag(repo *git.Repository, commit *object.Commit) bool {
7788

7889
if latestTag != nil && previousTag != nil {
7990
if tillLatest {
80-
return latestTag.Hash().String() == commit.Hash.String()
91+
return latestTag.Hash() == commit.Hash
8192
}
82-
return previousTag.Hash().String() == commit.Hash.String()
93+
return previousTag.Hash() == commit.Hash
8394

8495
}
8596
return false

0 commit comments

Comments
 (0)