From aa81b0306c80d477e33e6e55156c631b45bf4bbc Mon Sep 17 00:00:00 2001 From: knoflook Date: Fri, 1 Oct 2021 19:11:21 +0200 Subject: [PATCH] feat: add Tag.UpgradeElement(newTag Tag) which returns a difference between two tags --- tagcmp.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tagcmp.go b/tagcmp.go index c898c55..3d80f5c 100644 --- a/tagcmp.go +++ b/tagcmp.go @@ -166,6 +166,24 @@ func (t Tag) IsCompatible(tag Tag) bool { return true } +// UpgradeElement returns a Tag object which is the difference between an old and new tag +// It can contain negative numbers if comparing with an older tag. +func (curTag Tag) UpgradeElement(newTag Tag) (Tag, error) { + if !curTag.IsCompatible(newTag) { + return Tag{}, fmt.Errorf("%s and %s are not compatible with each other", curTag.String(), newTag.String()) + } + diff := curTag + diff.Major(strconv.Itoa(strconv.Atoi(newTag.Major) - strconv.Atoi(curTag.Major))) + if !curTag.MissingMinor { + diff.Minor(strconv.Itoa(strconv.Atoi(newTag.Minor) - strconv.Atoi(curTag.Minor))) + } + if !curTag.MissingPatch { + diff.Patch(strconv.Itoa(strconv.Atoi(newTag.Patch) - strconv.Atoi(curTag.Patch))) + } + + return diff, nil +} + // CommitHashPattern matches commit-like hash tags var CommitHashPattern = "^[a-f0-9]{7,40}$"