Skip to content

Commit c6facf6

Browse files
committed
Add ability to increment different digit in hotfix version - closes #186
1 parent 641ec83 commit c6facf6

File tree

4 files changed

+104
-19
lines changed

4 files changed

+104
-19
lines changed

src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public class GitFlowHotfixStartMojo extends AbstractGitFlowMojo {
7171
@Parameter(property = "useSnapshotInHotfix", defaultValue = "false")
7272
private boolean useSnapshotInHotfix;
7373

74+
/**
75+
* Which digit to increment in the next hotfix version. Starts from zero.
76+
*
77+
*/
78+
@Parameter(property = "hotfixVersionDigitToIncrement")
79+
private Integer hotfixVersionDigitToIncrement;
80+
7481
/** {@inheritDoc} */
7582
@Override
7683
public void execute() throws MojoExecutionException, MojoFailureException {
@@ -154,8 +161,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
154161
final String currentVersion = getCurrentProjectVersion();
155162

156163
// get default hotfix version
157-
final String defaultVersion = new GitFlowVersionInfo(currentVersion)
158-
.hotfixVersion(tychoBuild);
164+
final String defaultVersion = new GitFlowVersionInfo(currentVersion).hotfixVersion(tychoBuild, hotfixVersionDigitToIncrement);
159165

160166
if (defaultVersion == null) {
161167
throw new MojoFailureException(

src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionInfo.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,46 @@ public String nextSnapshotVersion() {
6868
}
6969

7070
/**
71-
* Gets next SNAPSHOT version. If index is <code>null</code> or not valid
72-
* then it delegates to {@link #getNextVersion()} method.
71+
* Gets next SNAPSHOT version.
7372
*
7473
* @param index
7574
* Which part of version to increment.
7675
* @return Next SNAPSHOT version.
7776
*/
7877
public String nextSnapshotVersion(final Integer index) {
78+
return nextVersion(index, true);
79+
}
80+
81+
/**
82+
* Gets next version. If index is <code>null</code> or not valid then it
83+
* delegates to {@link #getNextVersion()} method.
84+
*
85+
* @param index
86+
* Which part of version to increment.
87+
* @param snapshot
88+
* Whether to use SNAPSHOT version.
89+
* @return Next version.
90+
*/
91+
private String nextVersion(final Integer index, boolean snapshot) {
7992
List<String> digits = getDigits();
8093

8194
String nextVersion = null;
8295

8396
if (digits != null) {
8497
if (index != null && index >= 0 && index < digits.size()) {
8598
int origDigitsLength = joinDigitString(digits).length();
86-
digits.set(index,
87-
incrementVersionString((String) digits.get(index)));
99+
digits.set(index, incrementVersionString((String) digits.get(index)));
88100
for (int i = index + 1; i < digits.size(); i++) {
89101
digits.set(i, "0");
90102
}
91103
String digitsStr = joinDigitString(digits);
92-
nextVersion = digitsStr
93-
+ getSnapshotVersionString()
94-
.substring(origDigitsLength);
104+
nextVersion = digitsStr + (snapshot ? getSnapshotVersionString().substring(origDigitsLength)
105+
: getReleaseVersionString().substring(origDigitsLength));
95106
} else {
96-
nextVersion = getNextVersion().getSnapshotVersionString();
107+
nextVersion = snapshot ? getNextVersion().getSnapshotVersionString() : getNextVersion().getReleaseVersionString();
97108
}
98109
} else {
99-
nextVersion = getSnapshotVersionString();
110+
nextVersion = snapshot ? getSnapshotVersionString() : getReleaseVersionString();
100111
}
101112
return nextVersion;
102113
}
@@ -122,11 +133,11 @@ public String featureVersion(final String featureName) {
122133
*
123134
* @param preserveSnapshot
124135
* Whether to preserve SNAPSHOT in the version.
136+
* @param index
137+
* Which part of version to increment.
125138
* @return Next version.
126139
*/
127-
public String hotfixVersion(boolean preserveSnapshot) {
128-
return (preserveSnapshot && isSnapshot()) ? getNextVersion()
129-
.getSnapshotVersionString() : getNextVersion()
130-
.getReleaseVersionString();
140+
public String hotfixVersion(boolean preserveSnapshot, final Integer index) {
141+
return nextVersion(index, (preserveSnapshot && isSnapshot()));
131142
}
132143
}

src/test/java/com/amashchenko/maven/plugin/gitflow/GitFlowVersionInfoTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,28 @@ public void testIsValidVersion() throws Exception {
5151
public void testHotfixVersion() throws Exception {
5252
GitFlowVersionInfo info = new GitFlowVersionInfo("0.9");
5353
Assert.assertNotNull(info);
54-
Assert.assertEquals("0.10", info.hotfixVersion(false));
54+
Assert.assertEquals("0.10", info.hotfixVersion(false, null));
5555
}
5656

5757
@Test
5858
public void testHotfixVersion2() throws Exception {
5959
GitFlowVersionInfo info = new GitFlowVersionInfo("0.9-SNAPSHOT");
6060
Assert.assertNotNull(info);
61-
Assert.assertEquals("0.10", info.hotfixVersion(false));
61+
Assert.assertEquals("0.10", info.hotfixVersion(false, null));
6262
}
6363

6464
@Test
6565
public void testHotfixVersion3() throws Exception {
6666
GitFlowVersionInfo info = new GitFlowVersionInfo("0.9");
6767
Assert.assertNotNull(info);
68-
Assert.assertEquals("0.10", info.hotfixVersion(true));
68+
Assert.assertEquals("0.10", info.hotfixVersion(true, null));
6969
}
7070

7171
@Test
7272
public void testHotfixVersion4() throws Exception {
7373
GitFlowVersionInfo info = new GitFlowVersionInfo("0.9-SNAPSHOT");
7474
Assert.assertNotNull(info);
75-
Assert.assertEquals("0.10-SNAPSHOT", info.hotfixVersion(true));
75+
Assert.assertEquals("0.10-SNAPSHOT", info.hotfixVersion(true, null));
7676
}
7777

7878
@Test
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2014-2021 Aleksandr Mashchenko.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.amashchenko.maven.plugin.gitflow;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
import org.junit.runners.Parameterized;
25+
import org.junit.runners.Parameterized.Parameters;
26+
27+
@RunWith(Parameterized.class)
28+
public class NextHotfixVersionTest {
29+
private final String version;
30+
private final Integer index;
31+
private final String expectedVersion;
32+
33+
public NextHotfixVersionTest(final String version, final Integer index, final String expectedVersion) {
34+
this.version = version;
35+
this.index = index;
36+
this.expectedVersion = expectedVersion;
37+
}
38+
39+
@Parameters(name = "{0}-({1})->{2}")
40+
public static Collection<Object[]> data() {
41+
return Arrays
42+
.asList(new Object[][] {
43+
{ "some-SNAPSHOT", null, "some" },
44+
{ "some-SNAPSHOT", 0, "some" },
45+
{ "0.58", null, "0.59" },
46+
{ "0.58", -1, "0.59" },
47+
{ "0.58", 0, "1.0" },
48+
{ "0.58", 1, "0.59" },
49+
{ "0.58", 100, "0.59" },
50+
{ "0.9", 1, "0.10" },
51+
{ "0.09", 1, "0.10" },
52+
{ "0.0009", 0, "1.0" },
53+
{ "0.0009", 1, "0.0010" },
54+
{ "0.09-RC2", null, "0.09-RC3" },
55+
{ "0.09-RC3", 0, "1.0-RC3" },
56+
{ "0.09-RC3-SNAPSHOT", 0, "1.0-RC3" },
57+
{ "0.9-SNAPSHOT", null, "0.10" },
58+
{ "0.09-RC3-feature-SNAPSHOT", 0, "1.0-RC3-feature" },
59+
{ "0.09-RC3-feature", null, "0.09-RC4-feature" },
60+
{ "2.3.4", 0, "3.0.0" },
61+
{ "2.3.4", 1, "2.4.0" } });
62+
}
63+
64+
@Test
65+
public void testNextSnapshotVersion() throws Exception {
66+
Assert.assertEquals(expectedVersion, new GitFlowVersionInfo(version).hotfixVersion(false, index));
67+
}
68+
}

0 commit comments

Comments
 (0)