Skip to content
Snippets Groups Projects
Verified Commit a0608f85 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

chore: optimize

parent 164906dc
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,6 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
)
type CommitType int
......@@ -43,15 +42,31 @@ func GetCommitType(path string) (CommitType, error) {
}
func getLatestSemanticTag(path string) (string, error) {
r, err := git.PlainOpen(path)
if err != nil {
return "", fmt.Errorf("failed to open repository: %v", err)
}
tagList, err := getSemanticTags(r)
if err != nil {
return "", err
}
if len(tagList) == 0 {
return "", fmt.Errorf("no semantic tags found")
}
sort.Slice(tagList, func(i, j int) bool {
return compareSemanticVersions(tagList[i], tagList[j])
})
return tagList[len(tagList)-1], nil
}
func getSemanticTags(r *git.Repository) ([]string, error) {
tags, err := r.Tags()
if err != nil {
return "", fmt.Errorf("failed to get tags: %v", err)
return nil, fmt.Errorf("failed to get tags: %v", err)
}
var tagList []string
......@@ -64,18 +79,10 @@ func getLatestSemanticTag(path string) (string, error) {
})
if err != nil {
return "", fmt.Errorf("failed to iterate over tags: %v", err)
}
if len(tagList) == 0 {
return "", fmt.Errorf("no semantic tags found")
return nil, fmt.Errorf("failed to iterate over tags: %v", err)
}
sort.Slice(tagList, func(i, j int) bool {
return compareSemanticVersions(tagList[i], tagList[j])
})
return tagList[len(tagList)-1], nil
return tagList, nil
}
func isSemanticVersion(tagName string) bool {
......@@ -109,10 +116,8 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) {
return nil, fmt.Errorf("failed to get tags: %v", err)
}
var (
mostRecentTag *plumbing.Reference
mostRecentCommit *object.Commit
)
var mostRecentTag *plumbing.Reference
var mostRecentCommit *object.Commit
err = tags.ForEach(func(tag *plumbing.Reference) error {
obj, err := r.TagObject(tag.Hash())
......@@ -141,6 +146,7 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) {
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate over tags: %v", err)
}
......@@ -156,6 +162,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT
var commitType CommitType
found := false
err = cIter.ForEach(func(commit *object.Commit) error {
if commit.Hash == tagCommit.Hash {
found = true
......@@ -176,6 +183,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT
return nil
})
if err != nil {
return OtherCommit, fmt.Errorf("failed to iterate over commit log: %v", err)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment