IsCompatible reports whether pinVersion is semver-compatible with requestedVersion. Semver compatibility is defined as both versions sharing the same major version. Examples: - IsCompatible("v5.0.0", "v5") → true - IsCompatible("v5.1.0", "v5.0.0") → true - IsCompatible("v6.0.0", "v5") → false
(pinVersion, requestedVersion string)
| 160 | // - IsCompatible("v5.1.0", "v5.0.0") → true |
| 161 | // - IsCompatible("v6.0.0", "v5") → false |
| 162 | func IsCompatible(pinVersion, requestedVersion string) bool { |
| 163 | pinVersion = EnsureVPrefix(pinVersion) |
| 164 | requestedVersion = EnsureVPrefix(requestedVersion) |
| 165 | |
| 166 | // Guard: both versions must be valid semver before comparing majors. |
| 167 | // semver.Major returns "" for invalid input, causing "" == "" to |
| 168 | // incorrectly report two invalid versions as compatible. |
| 169 | if !IsValid(pinVersion) || !IsValid(requestedVersion) { |
| 170 | semverLog.Printf("IsCompatible: one or both versions are invalid: pin=%s, requested=%s", |
| 171 | pinVersion, requestedVersion) |
| 172 | return false |
| 173 | } |
| 174 | |
| 175 | pinMajor := semver.Major(pinVersion) |
| 176 | requestedMajor := semver.Major(requestedVersion) |
| 177 | |
| 178 | compatible := pinMajor == requestedMajor |
| 179 | semverLog.Printf("Checking semver compatibility: pin=%s (major=%s), requested=%s (major=%s) -> %v", |
| 180 | pinVersion, pinMajor, requestedVersion, requestedMajor, compatible) |
| 181 | |
| 182 | return compatible |
| 183 | } |