feat: add Tag.UpgradeElement(newTag Tag) which returns a difference between two tags

This commit is contained in:
knoflook 2021-10-01 19:11:21 +02:00
parent 2a8edd82d7
commit 05b56c0aad
Signed by: knoflook
GPG Key ID: D6A1D0E8FC4FEF1C
1 changed files with 22 additions and 0 deletions

View File

@ -166,6 +166,28 @@ 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(Itoa(Atoi(newTag.Major) - Atoi(curTag.Major)))
if curTag.MissingMinor {
diff.Minor = nil
} else {
diff.Minor(Itoa(Atoi(newTag.Minor) - Atoi(curTag.Minor)))
}
if curTag.MissingPatch {
diff.Patch = nil
} else {
diff.Patch(Itoa(Atoi(newTag.Patch) - Atoi(curTag.Patch)))
}
return diff, nil
}
// CommitHashPattern matches commit-like hash tags
var CommitHashPattern = "^[a-f0-9]{7,40}$"