Skip to content

Commit

Permalink
Upgrade to new Semver library
Browse files Browse the repository at this point in the history
Handle the breaking changes and deprecations in 0.10.x of the semver library.
  • Loading branch information
ajoberstar committed Feb 18, 2024
1 parent 8dbeb0b commit 23f7c33
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public Version reckon() {
throw new IllegalStateException("Reckoned version " + reckoned + " has already been released.");
}

if (inventory.getClaimedVersions().contains(reckoned.getNormal()) && !inventory.getCurrentVersion().filter(reckoned.getNormal()::equals).isPresent() && reckoned.isSignificant()) {
if (inventory.getClaimedVersions().contains(reckoned.getNormal()) && inventory.getCurrentVersion().filter(reckoned.getNormal()::equals).isEmpty() && reckoned.isSignificant()) {
throw new IllegalStateException("Reckoned target normal version " + reckoned.getNormal() + " has already been released.");
}

Expand Down Expand Up @@ -94,7 +94,7 @@ private Version reckonNormal(VcsInventory inventory) {
// if a version's already being developed on a parallel branch we'll skip it
if (inventory.getParallelNormals().contains(targetNormal) && probableStage.isPresent()) {
if (scope.compareTo(parallelBranchScope) < 0) {
logger.debug("Skipping {} as it's being developed on a parallel branch. While {} was requested, parallel branches claim a {}, using that instead.", scope, parallelBranchScope);
logger.debug("Skipping {} as it's being developed on a parallel branch. While {} was requested, parallel branches claim a {}, using that instead.", targetNormal, scope, parallelBranchScope);
targetNormal = targetNormal.incrementNormal(parallelBranchScope);
} else {
logger.debug("Skipping {} as it's being developed on a parallel branch. Incrementing again with {}", targetNormal, scope);
Expand Down
10 changes: 5 additions & 5 deletions reckon-core/src/main/java/org/ajoberstar/reckon/core/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public static Scope from(String value) {
* @throws IllegalStateException if they have an invalid increment
*/
public static Optional<Scope> infer(Version before, Version after) {
var major = after.getVersion().getMajorVersion() - before.getVersion().getMajorVersion();
var minor = after.getVersion().getMinorVersion() - before.getVersion().getMinorVersion();
var patch = after.getVersion().getPatchVersion() - before.getVersion().getPatchVersion();
if (major == 1 && after.getVersion().getMinorVersion() == 0 && after.getVersion().getPatchVersion() == 0) {
var major = after.getVersion().majorVersion() - before.getVersion().majorVersion();
var minor = after.getVersion().minorVersion() - before.getVersion().minorVersion();
var patch = after.getVersion().patchVersion() - before.getVersion().patchVersion();
if (major == 1 && after.getVersion().minorVersion() == 0 && after.getVersion().patchVersion() == 0) {
return Optional.of(Scope.MAJOR);
} else if (major == 0 && minor == 1 && after.getVersion().getPatchVersion() == 0) {
} else if (major == 0 && minor == 1 && after.getVersion().patchVersion() == 0) {
return Optional.of(Scope.MINOR);
} else if (major == 0 && minor == 0 && patch == 1) {
return Optional.of(Scope.PATCH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Optional<Version> getCurrentVersion() {
}

/**
* Number of commits between the current commmit and the base normal version tag.
* Number of commits between the current commit and the base normal version tag.
*/
public int getCommitsSinceBase() {
return commitsSinceBase;
Expand Down Expand Up @@ -116,7 +116,7 @@ public Set<Version> getClaimedVersions() {
}

/**
* All commit messages between the current HEAD commit and the base verison's commit.
* All commit messages between the current HEAD commit and the base version's commit.
*/
public List<String> getCommitMessages() {
return commitMessages;
Expand Down
41 changes: 21 additions & 20 deletions reckon-core/src/main/java/org/ajoberstar/reckon/core/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.github.zafarkhaja.semver.ParseException;
Expand All @@ -14,7 +13,7 @@ public final class Version implements Comparable<Version> {
/**
* A base version for use as a default in cases where you don't have an existing version.
*/
public static final Version IDENTITY = new Version(com.github.zafarkhaja.semver.Version.forIntegers(0, 0, 0));
public static final Version IDENTITY = new Version(com.github.zafarkhaja.semver.Version.of(0, 0, 0));

private final com.github.zafarkhaja.semver.Version version;
private final Version normal;
Expand All @@ -23,10 +22,10 @@ public final class Version implements Comparable<Version> {
private Version(com.github.zafarkhaja.semver.Version version) {
this.version = version;
// need this if logic to avoid stack overflow
if (version.getPreReleaseVersion().isEmpty()) {
if (version.preReleaseVersion().isEmpty() && version.buildMetadata().isEmpty()) {
this.normal = this;
} else {
this.normal = new Version(com.github.zafarkhaja.semver.Version.forIntegers(version.getMajorVersion(), version.getMinorVersion(), version.getPatchVersion()));
this.normal = new Version(com.github.zafarkhaja.semver.Version.of(version.majorVersion(), version.minorVersion(), version.patchVersion()));
}
this.stage = Stage.valueOf(version);
}
Expand Down Expand Up @@ -59,17 +58,17 @@ public Optional<Stage> getStage() {
* {@code false} otherwise
*/
public boolean isFinal() {
return version.getPreReleaseVersion().isEmpty();
return version.preReleaseVersion().isEmpty();
}

/**
* @return {@code true} if the version is final or any other significant stage, {@code false} if
* insignficant or snapshot
* insignificant or snapshot
*/
public boolean isSignificant() {
return isFinal() || getStage()
.filter(stage -> !"SNAPSHOT".equals(stage.getName()))
.filter(stage -> version.getBuildMetadata().isEmpty())
.filter(stage -> version.buildMetadata().isEmpty())
.isPresent();
}

Expand All @@ -82,11 +81,11 @@ public boolean isSignificant() {
public Version incrementNormal(Scope scope) {
switch (scope) {
case MAJOR:
return new Version(version.incrementMajorVersion());
return new Version(version.nextMajorVersion());
case MINOR:
return new Version(version.incrementMinorVersion());
return new Version(version.nextMinorVersion());
case PATCH:
return new Version(version.incrementPatchVersion());
return new Version(version.nextPatchVersion());
default:
throw new AssertionError("Invalid scope: " + scope);
}
Expand Down Expand Up @@ -141,14 +140,16 @@ public int getNum() {
}

private static Stage valueOf(com.github.zafarkhaja.semver.Version version) {
var matcher = STAGE_REGEX.matcher(version.getPreReleaseVersion());
if (matcher.find()) {
var name = matcher.group("name");
int num = Optional.ofNullable(matcher.group("num")).map(Integer::parseInt).orElse(0);
return new Stage(name, num);
} else {
return null;
}
var maybeMatcher = version.preReleaseVersion().map(STAGE_REGEX::matcher);
return maybeMatcher.map(matcher -> {
if (matcher.find()) {
var name = matcher.group("name");
int num = Optional.ofNullable(matcher.group("num")).map(Integer::parseInt).orElse(0);
return new Stage(name, num);
} else {
return null;
}
}).orElse(null);
}
}

Expand All @@ -161,7 +162,7 @@ private static Stage valueOf(com.github.zafarkhaja.semver.Version version) {
*/
public static Version valueOf(String versionString) {
try {
return new Version(com.github.zafarkhaja.semver.Version.valueOf(versionString));
return new Version(com.github.zafarkhaja.semver.Version.parse(versionString));
} catch (IllegalArgumentException | ParseException e) {
var message = String.format("Invalid version \"%s\": %s", versionString, e.getMessage());
throw new IllegalArgumentException(message, e);
Expand All @@ -177,7 +178,7 @@ public static Version valueOf(String versionString) {
*/
public static Optional<Version> parse(String versionString) {
try {
return Optional.of(new Version(com.github.zafarkhaja.semver.Version.valueOf(versionString)));
return Optional.of(new Version(com.github.zafarkhaja.semver.Version.parse(versionString)));
} catch (IllegalArgumentException | ParseException e) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

public class ReckonerIntegTest {
private static final Clock CLOCK = Clock.fixed(Instant.ofEpochSecond(1530724706), ZoneId.of("UTC"));
private static final String TIMESTAMP = "20180704T171826Z";

private Path repoDir;
private Git git;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,8 @@ public void doubleConflictingParallelIncrementsHigherScope() {
.clock(CLOCK)
.vcs(() -> inventory)
.parallelBranchScope(Scope.MINOR)
.scopeCalc(i -> Optional.ofNullable(Scope.PATCH))
.stageCalc((i, v) -> Optional.ofNullable("final"))
.scopeCalc(i -> Optional.of(Scope.PATCH))
.stageCalc((i, v) -> Optional.of("final"))
.defaultInferredScope(Scope.MINOR)
.stages("beta", "milestone", "rc", "final")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ private void tag(String userEmail, String userName, boolean allowRetry) {
var errorStr = error.toString(StandardCharsets.UTF_8);
if (errorStr.contains(String.format("fatal: tag '%s' already exists", getTagName().get()))) {
setDidWork(false);
} else if (allowRetry && errorStr.contains(String.format("Committer identity unknown"))) {
} else if (allowRetry && errorStr.contains("Committer identity unknown")) {
var email = getRecentUserEmail();
var name = getRecentUserName();
System.err.println(String.format("Tagging as recent committer %s <%s>, as this machine has no git identity set.", name, email));
System.err.printf("Tagging as recent committer %s <%s>, as this machine has no git identity set.%n", name, email);
tag(email, name, false);
} else {
System.err.println(errorStr);
Expand All @@ -90,7 +90,7 @@ private void tag(String userEmail, String userName, boolean allowRetry) {

private String getRecentUserEmail() {
var output = new ByteArrayOutputStream();
var result = getExecOperations().exec(spec -> {
getExecOperations().exec(spec -> {
spec.setWorkingDir(getRepoDirectory());
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%ae");
spec.setStandardOutput(output);
Expand All @@ -100,7 +100,7 @@ private String getRecentUserEmail() {

private String getRecentUserName() {
var output = new ByteArrayOutputStream();
var result = getExecOperations().exec(spec -> {
getExecOperations().exec(spec -> {
spec.setWorkingDir(getRepoDirectory());
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%an");
spec.setStandardOutput(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.gradle.api.provider.ProviderFactory;

public class ReckonExtension {
private static Logger logger = Logging.getLogger(ReckonExtension.class);
private static final Logger logger = Logging.getLogger(ReckonExtension.class);

private final DirectoryProperty repoDirectory;
private final Reckoner.Builder reckonerBuilder;
Expand Down Expand Up @@ -184,7 +184,6 @@ private Version reckonVersion() {
}

private Repository openRepo() {
Repository repo;
try {
var builder = new FileRepositoryBuilder();
builder.readEnvironment();
Expand Down

0 comments on commit 23f7c33

Please sign in to comment.