mirror of
https://github.com/lukaszraczylo/semver-generator.git
synced 2026-06-05 22:49:25 +00:00
fix(semver): skip non-semver tags when picking latest existing tag
Rolling tags like 'v1' (and any other tag that doesn't parse as x.y.z after prefix stripping) used to enter the latest-tag candidate set in CalculateSemver. When such a tag shared a commit with a real semver tag (e.g. v1 and v1.16.3 both pointing at the same release commit), go-git's alphabetical tag iteration made 'v1' win, ParseExistingSemver bailed out because '1' has only one component, and the calculator silently reset the baseline to 0.0.0 — producing nonsense like 0.0.5 on this branch. ListExistingTags now filters tags through a new IsParseableSemverTag helper before recording them, so non-semver tags never participate in latest-tag selection. The behavior change is invisible for repos that only use proper vX.Y.Z tags, and it's covered by a new test table.
This commit is contained in:
+11
-6
@@ -163,10 +163,13 @@ func ListCommits(repo *GitRepository) ([]CommitDetails, error) {
|
||||
}
|
||||
|
||||
// ListExistingTags lists all tags in the repository
|
||||
func ListExistingTags(repo *GitRepository) {
|
||||
// ListExistingTags lists all tags in the repository.
|
||||
// Tags that don't parse as proper semver (rolling tags like "v1" or "latest")
|
||||
// are skipped so they can't out-rank real semver tags pointing to the same
|
||||
// commit during latest-tag selection.
|
||||
func ListExistingTags(repo *GitRepository, tagPrefixes []string) {
|
||||
Debug("Listing existing tags", nil)
|
||||
|
||||
// Check if Handler is nil to avoid panic
|
||||
if repo.Handler == nil {
|
||||
Debug("Repository handler is nil, skipping tag listing", nil)
|
||||
return
|
||||
@@ -180,15 +183,17 @@ func ListExistingTags(repo *GitRepository) {
|
||||
|
||||
if err := refs.ForEach(func(ref *plumbing.Reference) error {
|
||||
tagName := ref.Name().Short()
|
||||
commitHash := ref.Hash().String()
|
||||
|
||||
// For annotated tags, dereference to get the actual commit hash
|
||||
if !IsParseableSemverTag(tagName, tagPrefixes) {
|
||||
Debug("Skipping non-semver tag", map[string]interface{}{"tag": tagName})
|
||||
return nil
|
||||
}
|
||||
|
||||
commitHash := ref.Hash().String()
|
||||
tagObj, err := repo.Handler.TagObject(ref.Hash())
|
||||
if err == nil {
|
||||
// This is an annotated tag - get the commit it points to
|
||||
commitHash = tagObj.Target.String()
|
||||
}
|
||||
// If err != nil, it's a lightweight tag - ref.Hash() is already the commit hash
|
||||
|
||||
repo.Tags = append(repo.Tags, TagDetails{
|
||||
Name: tagName,
|
||||
|
||||
Reference in New Issue
Block a user