Skip to content

Commit

Permalink
Exact versions in develop/mdeps #4
Browse files Browse the repository at this point in the history
  • Loading branch information
host6 committed Sep 6, 2017
1 parent f91b301 commit e32d950
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 84 deletions.
7 changes: 4 additions & 3 deletions src/main/java/org/scm4j/wf/branch/ReleaseBranch.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import org.scm4j.vcs.api.VCSTag;
import org.scm4j.vcs.api.WalkDirection;
import org.scm4j.wf.SCMWorkflow;
import org.scm4j.wf.conf.DelayedTagsFile;
import org.scm4j.wf.conf.Component;
import org.scm4j.wf.conf.DelayedTagsFile;
import org.scm4j.wf.conf.MDepsFile;
import org.scm4j.wf.conf.VCSRepositories;
import org.scm4j.wf.conf.Version;
Expand Down Expand Up @@ -44,7 +44,8 @@ public ReleaseBranch(final Component comp, VCSRepositories repos) {
DevelopBranch db = new DevelopBranch(comp);
Version ver = db.getVersion().toRelease();

List<String> releaseBranches = new ArrayList<>(vcs.getBranches(comp.getVcsRepository().getReleaseBranchPrefix() + (comp.getVersion().isExactVersion() ? comp.getVersion().getReleaseNoPatchString() : "")));
List<String> releaseBranches = new ArrayList<>(vcs.getBranches(
comp.getVcsRepository().getReleaseBranchPrefix() + (comp.getVersion().isSnapshot() ? "" : comp.getVersion().getReleaseNoPatchString())));
if (releaseBranches.isEmpty()) {
this.version = ver;
name = computeName();
Expand Down Expand Up @@ -210,7 +211,7 @@ private Boolean mDepsFrozen() {
return true;
}
for (Component mDep : mDeps) {
if (!mDep.getVersion().isExactVersion()) {
if (mDep.getVersion().isSnapshot()) {
return false;
}
}
Expand Down
82 changes: 40 additions & 42 deletions src/main/java/org/scm4j/wf/conf/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

public class Version {

private static final String SNAPSHOT = "-SNAPSHOT";
public static final String SNAPSHOT = "-SNAPSHOT";

private final String minor;
private final String prefix;
private final String snapshot;
private final String patch;
private final String verStr;
private final Boolean isEmpty;
private final boolean isEmpty;
private final boolean isSemantic;

public Version(String verStr) {
this.verStr = verStr;
Expand All @@ -21,6 +22,7 @@ public Version(String verStr) {
minor = "";
patch = "";
isEmpty = true;
isSemantic = false;
} else {
isEmpty = false;
if (verStr.contains(SNAPSHOT)) {
Expand All @@ -39,13 +41,11 @@ public Version(String verStr) {
}
prefix = verStr.substring(0, verStr.lastIndexOf(".") + 1);
} else {
prefix = "0.";
prefix = "";
minor = verStr;
patch = "0";
}
if (!minor.isEmpty() && !StringUtils.isNumeric(minor)) {
throw new IllegalArgumentException("wrong version: " + verStr);
patch = "";
}
isSemantic = StringUtils.isNumeric(minor);
}
}

Expand All @@ -63,56 +63,46 @@ public String getSnapshot() {

@Override
public String toString() {
if (!StringUtils.isNumeric(minor)) {
if (!isSemantic) {
return verStr;
}
return prefix + minor + (patch.isEmpty() ? "" : "." + patch) + snapshot;
}

public Version toPreviousPatch() {
int i = 0;
while (i < patch.length() && !Character.isDigit(patch.charAt(i)))
i++;
int firstDigitStart = i;
while (i < patch.length() && Character.isDigit(patch.charAt(i)))
i++;
if (i == firstDigitStart) {
return new Version(prefix + minor + "." + patch + "0" + snapshot);
if (patch.isEmpty()) {
return new Version(prefix + minor + ".0" + snapshot);
}
if (!StringUtils.isNumeric(patch)) {
return this;
}
int patchInt = Integer.parseInt(patch.substring(firstDigitStart, i)) - 1;
String newPatch = patch.substring(0, firstDigitStart) + Integer.toString(patchInt)
+ patch.substring(i, patch.length());
return new Version(prefix + minor + "." + newPatch + snapshot);
int patchInt = Integer.parseInt(patch) - 1;
return new Version(prefix + minor + "." + Integer.toString(patchInt) + snapshot);
}

public Version toNextPatch() {
int i = 0;
while (i < patch.length() && !Character.isDigit(patch.charAt(i)))
i++;
int firstDigitStart = i;
while (i < patch.length() && Character.isDigit(patch.charAt(i)))
i++;
if (i == firstDigitStart) {
return new Version(prefix + minor + "." + patch + "1" + snapshot);
if (patch.isEmpty()) {
return new Version(prefix + minor + ".1" + snapshot);
}
if (!StringUtils.isNumeric(patch)) {
return this;
}
int patchInt = Integer.parseInt(patch.substring(firstDigitStart, i)) + 1;
String newPatch = patch.substring(0, firstDigitStart) + Integer.toString(patchInt)
+ patch.substring(i, patch.length());
return new Version(prefix + minor + "." + newPatch + snapshot);
int patchInt = Integer.parseInt(patch) + 1;
return new Version(prefix + minor + "." + Integer.toString(patchInt) + snapshot);
}

public Version toPreviousMinor() {
checkMinor();
checkSemantic();
return new Version(prefix + Integer.toString(Integer.parseInt(minor) - 1) + "." + patch + snapshot);
}

public Version toNextMinor() {
checkMinor();
checkSemantic();
return new Version(prefix + Integer.toString(Integer.parseInt(minor) + 1) + "." + patch + snapshot);
}

private void checkMinor() {
if (!StringUtils.isNumeric(minor)) {
private void checkSemantic() {
if (!isSemantic) {
throw new IllegalArgumentException("wrong version" + verStr);
}
}
Expand Down Expand Up @@ -146,8 +136,8 @@ public Boolean isEmpty() {
return isEmpty;
}

public boolean isExactVersion() {
return !minor.isEmpty();
public boolean isSnapshot() {
return !snapshot.isEmpty();
}

public String toSnapshotString() {
Expand All @@ -162,12 +152,16 @@ public String toReleaseString() {
}

public Boolean isGreaterThan(Version other) {
if (other.isEmpty() || !other.isExactVersion()) {
return !isEmpty() && isExactVersion();
if (other.isEmpty()) {
return !isEmpty();
}
if (!StringUtils.isNumeric(getMinor()) || !StringUtils.isNumeric(other.getMinor())) {
if (!isSemantic || !StringUtils.isNumeric(getMinor())) {
return false;
}
if (!other.isSemantic()) {
return true;
}

int minor = Integer.parseInt(getMinor());
int otherMinor = Integer.parseInt(other.getMinor());
if (minor > otherMinor) {
Expand All @@ -188,12 +182,16 @@ public Boolean isGreaterThan(Version other) {

}

private boolean isSemantic() {
return isSemantic;
}

public String getReleaseNoPatchString() {
return prefix + minor;
}

public Version toRelease() {
if (!StringUtils.isNumeric(minor)) {
if (!isSemantic) {
return this;
}
return new Version(prefix + minor + (patch.isEmpty() ? "" : "." + patch));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/scm4j/wf/branches/ReleaseBranchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testBranched() throws Exception {
action.execute(new ProgressConsole(action.getName(), ">>> ", "<<< "));

// simulate mdeps are not frozen
MDepsFile mDepsFile = new MDepsFile(Arrays.asList(compUBL, compUnTillDb));
MDepsFile mDepsFile = new MDepsFile(Arrays.asList(compUBL.cloneWithDifferentVersion(Version.SNAPSHOT), compUnTillDb.cloneWithDifferentVersion(Version.SNAPSHOT)));
env.getUnTillVCS().setFileContent(rbUnTillFixedVer.getName(), SCMWorkflow.MDEPS_FILE_NAME, mDepsFile.toFileContent(), "mdeps unversioned");
assertEquals(ReleaseBranchStatus.BRANCHED, rbUnTillFixedVer.getStatus());
}
Expand Down
52 changes: 14 additions & 38 deletions src/test/java/org/scm4j/wf/conf/VersionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testToString() {
assertEquals(new Version("11.21.31.41-SNAPSHOT").toString(), "11.21.31.41-SNAPSHOT");
assertEquals(new Version("11.21.31-SNAPSHOT").toString(), "11.21.31-SNAPSHOT");
assertEquals(new Version("11.21-SNAPSHOT").toString(), "11.21-SNAPSHOT");
assertEquals(new Version("11-SNAPSHOT").toString(), "0.11.0-SNAPSHOT");
assertEquals(new Version("11-SNAPSHOT").toString(), "11-SNAPSHOT");
assertEquals(new Version("").toString(), "");
assertEquals(new Version("1..1").toString(), "1..1");
}
Expand All @@ -41,7 +41,7 @@ public void testToReleaseString() {
assertEquals(new Version("11.21.31.41").toReleaseString(), "11.21.31.41");
assertEquals(new Version("11.21.31").toReleaseString(), "11.21.31");
assertEquals(new Version("11.21").toReleaseString(), "11.21");
assertEquals(new Version("11-SNAPSHOT").toReleaseString(), "0.11.0");
assertEquals(new Version("11-SNAPSHOT").toReleaseString(), "11");
assertEquals(new Version("-SNAPSHOT").toReleaseString(), "-SNAPSHOT");

}
Expand All @@ -54,25 +54,6 @@ public void testSnapshot() {
assertEquals(new Version("-SNAPSHOT").getSnapshot(), "-SNAPSHOT");
}

@Test
public void testIncorrectVersion() {
try {
new Version("sdfdfgdd");
fail();
} catch (IllegalArgumentException e) {
}
try {
new Version("sdfdfgdd.0");
fail();
} catch (IllegalArgumentException e) {
}
try {
new Version("1.sdfdfgdd.0");
fail();
} catch (IllegalArgumentException e) {
}
}

@Test
public void testMinorBumping() {
assertEquals(new Version("11.21.31.41").toPreviousMinor().toReleaseString(), "11.21.30.41");
Expand Down Expand Up @@ -115,13 +96,11 @@ public void testEqualsAndHashcode() {
}

@Test
public void testExactVersion() {
assertTrue(new Version("11.12.13-SNAPSHOT").isExactVersion());
assertTrue(new Version("11.12.13").isExactVersion());
assertTrue(new Version("11.13").isExactVersion());
assertTrue(new Version("11").isExactVersion());
assertFalse(new Version("").isExactVersion());
assertFalse(new Version("-SNAPSHOT").isExactVersion());
public void testIsSnapshot() {
assertTrue(new Version("11.12.13-SNAPSHOT").isSnapshot());
assertFalse(new Version("11.12.13").isSnapshot());
assertFalse(new Version("").isSnapshot());
assertTrue(new Version("-SNAPSHOT").isSnapshot());
}

@Test
Expand All @@ -135,20 +114,17 @@ public void testPatch() {
public void testToNextPatch() {
assertEquals("11.12.14-SNAPSHOT", new Version("11.12.13-SNAPSHOT").toNextPatch().toString());
assertEquals("14", new Version("11.12.13-SNAPSHOT").toNextPatch().getPatch());
assertEquals("11.12.14fgdfg-SNAPSHOT", new Version("11.12.13fgdfg-SNAPSHOT").toNextPatch().toString());
assertEquals("11.12.14fgdfg15-SNAPSHOT", new Version("11.12.13fgdfg15-SNAPSHOT").toNextPatch().toString());
assertEquals("0.13.1", new Version("13").toNextPatch().toString());
assertEquals("13.14.fgdfgd1", new Version("13.14.fgdfgd").toNextPatch().toString());
assertEquals("11.12.14fgdfg-SNAPSHOT", new Version("11.12.14fgdfg-SNAPSHOT").toNextPatch().toString());
assertEquals("13.1", new Version("13").toNextPatch().toString());
}

@Test
public void testToPreviousPatch() {
assertEquals("11.12.12-SNAPSHOT", new Version("11.12.13-SNAPSHOT").toPreviousPatch().toString());
assertEquals("12", new Version("11.12.13-SNAPSHOT").toPreviousPatch().getPatch());
assertEquals("11.12.12fgdfg-SNAPSHOT", new Version("11.12.13fgdfg-SNAPSHOT").toPreviousPatch().toString());
assertEquals("11.12.12fgdfg15-SNAPSHOT", new Version("11.12.13fgdfg15-SNAPSHOT").toPreviousPatch().toString());
assertEquals("0.13.-1", new Version("13").toPreviousPatch().toString());
assertEquals("13.14.fgdfgd0", new Version("13.14.fgdfgd").toPreviousPatch().toString());
assertEquals("11.12.12fgdfg-SNAPSHOT", new Version("11.12.12fgdfg-SNAPSHOT").toPreviousPatch().toString());
assertEquals("13.0", new Version("13").toPreviousPatch().toString());
assertEquals("13.14.fgdfgd", new Version("13.14.fgdfgd").toPreviousPatch().toString());
}

@Test
Expand All @@ -175,7 +151,7 @@ public void testGetReleaseNoPatchString() {
assertEquals("11.12.13", new Version("11.12.13.14-SNAPSHOT").getReleaseNoPatchString());
assertEquals("11.12", new Version("11.12.13-SNAPSHOT").getReleaseNoPatchString());
assertEquals("11", new Version("11.12-SNAPSHOT").getReleaseNoPatchString());
assertEquals("0.11", new Version("11-SNAPSHOT").getReleaseNoPatchString());
assertEquals("11", new Version("11-SNAPSHOT").getReleaseNoPatchString());
}

@Test
Expand All @@ -184,7 +160,7 @@ public void testToRelease() {
assertEquals(new Version("11.21.31.41"), new Version("11.21.31.41").toRelease());
assertEquals(new Version("11.21.31"), new Version("11.21.31").toRelease());
assertEquals(new Version("11.21"), new Version("11.21").toRelease());
assertEquals(new Version("0.11.0"), new Version("11-SNAPSHOT").toRelease());
assertEquals(new Version("11"), new Version("11-SNAPSHOT").toRelease());
assertEquals(new Version("-SNAPSHOT"), new Version("-SNAPSHOT").toRelease());
}
}

0 comments on commit e32d950

Please sign in to comment.