Skip to content

Commit 5b1e6ae

Browse files
committed
feat: update version detection to support ddev head version
1 parent c589917 commit 5b1e6ae

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

src/main/java/de/php_perfect/intellij/ddev/version/Version.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,65 @@
11
package de.php_perfect.intellij.ddev.version;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
45

56
import java.util.Arrays;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
69

710
public final class Version implements Comparable<Version> {
811

912
public final int @NotNull [] numbers;
1013

11-
1214
public final @NotNull String string;
1315

16+
/**
17+
* The build information for head versions (e.g., "36-ge74e3a95f" in "v1.24.4-36-ge74e3a95f")
18+
* Will be null for regular release versions
19+
*/
20+
@Nullable
21+
private final String buildInfo;
22+
23+
/**
24+
* Pattern to match head versions like "v1.24.4-36-ge74e3a95f"
25+
* Group 1: The semantic version part (v1.24.4)
26+
* Group 2: The commit info part (36-ge74e3a95f)
27+
*/
28+
private static final Pattern HEAD_VERSION_PATTERN = Pattern.compile("^(v?\\d+(?:\\.\\d+)*)-(\\d+-g[a-f0-9]+)$");
29+
1430
public Version(@NotNull String version) {
1531
this.string = version;
16-
final String[] split = version.replaceAll("^v", "").split("-")[0].split("\\.");
32+
33+
// Check if this is a head version
34+
Matcher headVersionMatcher = HEAD_VERSION_PATTERN.matcher(version);
35+
String versionForParsing;
36+
37+
if (headVersionMatcher.matches()) {
38+
// This is a head version, use the semantic version part for parsing
39+
versionForParsing = headVersionMatcher.group(1);
40+
this.buildInfo = headVersionMatcher.group(2);
41+
} else {
42+
// Regular version handling
43+
versionForParsing = version.split("-")[0];
44+
this.buildInfo = null;
45+
}
46+
47+
final String[] split = versionForParsing.replaceAll("^v", "").split("\\.");
1748
numbers = new int[split.length];
1849
for (int i = 0; i < split.length; i++) {
1950
numbers[i] = Integer.parseInt(split[i]);
2051
}
2152
}
2253

54+
public boolean isHeadVersion() {
55+
return buildInfo != null;
56+
}
57+
58+
@Nullable
59+
public String getBuildInfo() {
60+
return buildInfo;
61+
}
62+
2363
@Override
2464
public int compareTo(@NotNull Version another) {
2565
final int maxLength = Math.max(numbers.length, another.numbers.length);

src/test/java/de/php_perfect/intellij/ddev/cmd/DdevImplTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ void version() throws CommandFailedException {
3838
Assertions.assertEquals(expected, new DdevImpl().version("ddev", getProject()));
3939
}
4040

41+
@Test
42+
void headVersion() throws CommandFailedException {
43+
final Version expected = new Version("v1.24.4-36-ge74e3a95f");
44+
final ProcessOutput processOutput = new ProcessOutput("ddev version v1.24.4-36-ge74e3a95f", "", 0, false, false);
45+
46+
final MockProcessExecutor mockProcessExecutor = (MockProcessExecutor) ApplicationManager.getApplication().getService(ProcessExecutor.class);
47+
mockProcessExecutor.addProcessOutput("ddev --version", processOutput);
48+
49+
Version actual = new DdevImpl().version("ddev", getProject());
50+
Assertions.assertEquals(expected, actual);
51+
Assertions.assertTrue(actual.isHeadVersion());
52+
Assertions.assertEquals("36-ge74e3a95f", actual.getBuildInfo());
53+
}
54+
4155
@Test
4256
void detailedVersions() throws CommandFailedException, IOException {
4357
Versions expected = new Versions("v1.19.0", "20.10.12", "v2.2.2", "docker-desktop");

src/test/java/de/php_perfect/intellij/ddev/version/VersionTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ void versionsWithPrefixAndSuffixAreParsedCorrectly(String versionString) {
1414
Assertions.assertArrayEquals(new int[]{1, 26, 6}, version.numbers);
1515
}
1616

17+
@Test
18+
void headVersionIsParsedCorrectly() {
19+
final Version version = new Version("v1.24.4-36-ge74e3a95f");
20+
21+
Assertions.assertArrayEquals(new int[]{1, 24, 4}, version.numbers);
22+
Assertions.assertTrue(version.isHeadVersion());
23+
Assertions.assertEquals("36-ge74e3a95f", version.getBuildInfo());
24+
Assertions.assertEquals("v1.24.4-36-ge74e3a95f", version.toString());
25+
}
26+
27+
@Test
28+
void regularVersionIsNotHeadVersion() {
29+
final Version version = new Version("v1.24.4");
30+
31+
Assertions.assertFalse(version.isHeadVersion());
32+
Assertions.assertNull(version.getBuildInfo());
33+
}
34+
35+
@Test
36+
void debugVersionIsNotHeadVersion() {
37+
final Version version = new Version("v1.24.4-DEBUG");
38+
39+
Assertions.assertFalse(version.isHeadVersion());
40+
Assertions.assertNull(version.getBuildInfo());
41+
}
42+
1743
@Test
1844
void compareTo_withEarlierVersion_isGreaterThan() {
1945
Assertions.assertEquals(1, new Version("2.0.0").compareTo(new Version("1.0.0")));
@@ -43,4 +69,15 @@ void compareTo_withMorePreciseEarlierVersion_isFalse() {
4369
void compareTo_withMorePreciseLaterVersion_isLessThan() {
4470
Assertions.assertEquals(-1, new Version("1").compareTo(new Version("1.0.1")));
4571
}
72+
73+
@Test
74+
void compareTo_headVersionWithSameBaseVersion_isEqual() {
75+
Assertions.assertEquals(0, new Version("v1.24.4").compareTo(new Version("v1.24.4-36-ge74e3a95f")));
76+
}
77+
78+
@Test
79+
void compareTo_headVersionWithDifferentBaseVersion_isUnequal() {
80+
Assertions.assertEquals(-1, new Version("v1.24.3-42-gabcdef12").compareTo(new Version("v1.24.4")));
81+
Assertions.assertEquals(1, new Version("v1.24.5-42-gabcdef12").compareTo(new Version("v1.24.4")));
82+
}
4683
}

0 commit comments

Comments
 (0)