Merge pull request 'add functions to simplify checking upgrade types' (#3) from knoflook/tagcmp:main into main

Reviewed-on: coop-cloud/tagcmp#3
This commit is contained in:
decentral1se 2021-10-02 21:16:04 +00:00
commit 0581b1f5ed
1 changed files with 54 additions and 0 deletions

View File

@ -166,6 +166,60 @@ 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
}
// UpgradeType takes exit from UpgradeElemene and returns a numeric representation of upgrade or downgrade
// 1/-1: patch 2/-2: minor 4/-4: major 0: no change
func UpgradeType(t Tag) int {
var major, minor, patch int
major = strconv.Atoi(t.Major)
if t.MissingMinor {
minor = 0
} else {
minor = strconv.Atoi(t.Minor)
}
if t.MissingPatch {
patch = 0
} else {
patch := strconv.Atoi(t.Patch)
}
if major > 0 {
return 4
}
if major < 0 {
return -4
}
if minor > 0 {
return 2
}
if minor < 0 {
return -2
}
if patch > 0 {
return 1
}
if patch < 0 {
return -1
}
return 0
}
// CommitHashPattern matches commit-like hash tags
var CommitHashPattern = "^[a-f0-9]{7,40}$"