github.com/safedep/dry@v0.0.0-20241016050132-a15651f0548b/semver/diff.go (about)

     1  package semver
     2  
     3  import (
     4  	mver "github.com/Masterminds/semver/v3"
     5  )
     6  
     7  type SemverDrift int
     8  
     9  const (
    10  	NoDrift      = 0
    11  	MajorDrift   = 1
    12  	MinorDrift   = 2
    13  	PatchDrift   = 3
    14  	UnknownDrift = 100
    15  )
    16  
    17  // Diff calculates the differnce between two semver
    18  // string and returns the drift type and delta
    19  // Major > Minor > Patch precedence is followed
    20  func Diff(base, head string) (SemverDrift, uint64) {
    21  	v1, err := mver.NewVersion(base)
    22  	if err != nil {
    23  		return UnknownDrift, 0
    24  	}
    25  
    26  	v2, err := mver.NewVersion(head)
    27  	if err != nil {
    28  		return UnknownDrift, 0
    29  	}
    30  
    31  	if n := v2.Major() - v1.Major(); n != 0 {
    32  		return MajorDrift, n
    33  	}
    34  
    35  	if n := v2.Minor() - v1.Minor(); n != 0 {
    36  		return MinorDrift, n
    37  	}
    38  
    39  	if n := v2.Patch() - v1.Patch(); n != 0 {
    40  		return PatchDrift, n
    41  	}
    42  
    43  	return NoDrift, 0
    44  }
    45  
    46  func (d SemverDrift) IsNone() bool {
    47  	return d == NoDrift
    48  }
    49  
    50  func (d SemverDrift) IsMajor() bool {
    51  	return d == MajorDrift
    52  }
    53  
    54  func (d SemverDrift) IsMinor() bool {
    55  	return d == MinorDrift
    56  }
    57  
    58  func (d SemverDrift) IsPatch() bool {
    59  	return d == PatchDrift
    60  }