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 ( ...@@ -11,7 +11,6 @@ import (
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "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/object"
"github.com/go-git/go-git/v5/plumbing/storer"
) )
type CommitType int type CommitType int
...@@ -43,15 +42,31 @@ func GetCommitType(path string) (CommitType, error) { ...@@ -43,15 +42,31 @@ func GetCommitType(path string) (CommitType, error) {
} }
func getLatestSemanticTag(path string) (string, error) { func getLatestSemanticTag(path string) (string, error) {
r, err := git.PlainOpen(path) r, err := git.PlainOpen(path)
if err != nil { if err != nil {
return "", fmt.Errorf("failed to open repository: %v", err) 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() tags, err := r.Tags()
if err != nil { 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 var tagList []string
...@@ -64,18 +79,10 @@ func getLatestSemanticTag(path string) (string, error) { ...@@ -64,18 +79,10 @@ func getLatestSemanticTag(path string) (string, error) {
}) })
if err != nil { if err != nil {
return "", fmt.Errorf("failed to iterate over tags: %v", err) return nil, fmt.Errorf("failed to iterate over tags: %v", err)
} }
if len(tagList) == 0 { return tagList, nil
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 isSemanticVersion(tagName string) bool { func isSemanticVersion(tagName string) bool {
...@@ -109,10 +116,8 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) { ...@@ -109,10 +116,8 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) {
return nil, fmt.Errorf("failed to get tags: %v", err) return nil, fmt.Errorf("failed to get tags: %v", err)
} }
var ( var mostRecentTag *plumbing.Reference
mostRecentTag *plumbing.Reference var mostRecentCommit *object.Commit
mostRecentCommit *object.Commit
)
err = tags.ForEach(func(tag *plumbing.Reference) error { err = tags.ForEach(func(tag *plumbing.Reference) error {
obj, err := r.TagObject(tag.Hash()) obj, err := r.TagObject(tag.Hash())
...@@ -141,6 +146,7 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) { ...@@ -141,6 +146,7 @@ func getLatestTagCommit(r *git.Repository) (*object.Commit, error) {
return nil return nil
}) })
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to iterate over tags: %v", err) return nil, fmt.Errorf("failed to iterate over tags: %v", err)
} }
...@@ -156,6 +162,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT ...@@ -156,6 +162,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT
var commitType CommitType var commitType CommitType
found := false found := false
err = cIter.ForEach(func(commit *object.Commit) error { err = cIter.ForEach(func(commit *object.Commit) error {
if commit.Hash == tagCommit.Hash { if commit.Hash == tagCommit.Hash {
found = true found = true
...@@ -176,6 +183,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT ...@@ -176,6 +183,7 @@ func getCommitTypeSinceTag(r *git.Repository, tagCommit *object.Commit) (CommitT
return nil return nil
}) })
if err != nil { if err != nil {
return OtherCommit, fmt.Errorf("failed to iterate over commit log: %v", err) 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