Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8dac3ff

Browse files
committed
start computing 'next version' when HEAD is on version tag and in dirty state
see #106
1 parent 235526b commit 8dac3ff

File tree

6 files changed

+92
-11
lines changed

6 files changed

+92
-11
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@
7979
<license-maven-plugin.version>2.11</license-maven-plugin.version>
8080
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
8181
<maven-jar-plugin.version>2.3.2</maven-jar-plugin.version>
82-
<maven-failsafe-plugin.version>2.22.0</maven-failsafe-plugin.version>
82+
<maven-failsafe-plugin.version>3.0.0-M4</maven-failsafe-plugin.version>
8383
<maven-antrun-plugin.version>1.8</maven-antrun-plugin.version>
84-
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
84+
<maven-surefire-plugin.version>3.0.0-M4</maven-surefire-plugin.version>
8585
<maven-assembly-plugin.version>3.1.1</maven-assembly-plugin.version>
8686
</properties>
8787

src/main/java/fr/brouillard/oss/jgitver/impl/ConfigurableVersionStrategy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ public Version build(Commit head, List<Commit> parents) throws VersionCalculatio
120120
}
121121
}
122122

123+
123124
if (useLongFormat || useGitCommitId) {
124125
String commitIdQualifier =
125126
(useLongFormat ? "g" : "") + head.getGitObject().getName().substring(0, useLongFormat ? 8 : gitCommitIdLength);

src/main/java/fr/brouillard/oss/jgitver/impl/MavenVersionStrategy.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ public Version build(Commit head, List<Commit> parents) throws VersionCalculatio
4242
Ref tagToUse = findTagToUse(head, base);
4343
Version baseVersion = getBaseVersionAndRegisterMetadata(base, tagToUse);
4444
boolean needSnapshot = true;
45+
boolean isDirty = GitUtils.isDirty(getGit());
46+
boolean isDetachedHead = GitUtils.isDetachedHead(getRepository());
4547

4648
if (tagToUse != null) {
4749
needSnapshot = baseVersion.isSnapshot() || !isBaseCommitOnHead(head, base)
48-
|| !GitUtils.isAnnotated(tagToUse);
50+
|| !GitUtils.isAnnotated(tagToUse) || (isDirty && !isDetachedHead);
4951
}
5052

51-
if (!isBaseCommitOnHead(head, base)) {
53+
if (!isBaseCommitOnHead(head, base) || (isDirty && !isDetachedHead)) {
5254
// we are not on head
5355
if (GitUtils.isAnnotated(tagToUse) && !baseVersion.removeQualifier("SNAPSHOT").isQualified()) {
5456
// found tag to use was a non qualified annotated one, lets' increment the version automatically
@@ -57,7 +59,7 @@ public Version build(Commit head, List<Commit> parents) throws VersionCalculatio
5759
baseVersion = baseVersion.noQualifier();
5860
}
5961

60-
if (!GitUtils.isDetachedHead(getRepository())) {
62+
if (!isDetachedHead) {
6163
String branch = getRepository().getBranch();
6264
baseVersion = enhanceVersionWithBranch(baseVersion, branch);
6365
} else {
@@ -68,7 +70,7 @@ public Version build(Commit head, List<Commit> parents) throws VersionCalculatio
6870
}
6971
}
7072

71-
if (useDirty && GitUtils.isDirty(getGit())) {
73+
if (useDirty && isDirty) {
7274
baseVersion = baseVersion.addQualifier("dirty");
7375
}
7476

src/test/java/fr/brouillard/oss/jgitver/Scenarios.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,15 @@ public static ScenarioBuilder s19_merges_in_feature_branch() {
905905
.merge("C3", "M2", MergeCommand.FastForwardMode.NO_FF)
906906
.commit("content", "E");
907907
}
908+
909+
public static ScenarioBuilder s20_release_on_head() {
910+
return new Scenarios.ScenarioBuilder()
911+
.commit("content", "A")
912+
.commit("content", "B")
913+
.commit("content", "C")
914+
.tag("1.0.0")
915+
.master();
916+
}
908917
}
909918

910919
public static class Scenario {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright (C) 2016 Matthieu Brouillard [http://oss.brouillard.fr/jgitver] ([email protected])
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 fr.brouillard.oss.jgitver.strategy.maven.defaults;
17+
18+
import static fr.brouillard.oss.jgitver.impl.Lambdas.unchecked;
19+
import static org.hamcrest.CoreMatchers.is;
20+
import static org.hamcrest.MatcherAssert.assertThat;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
import org.junit.jupiter.api.Test;
26+
27+
import fr.brouillard.oss.jgitver.ScenarioTest;
28+
import fr.brouillard.oss.jgitver.Scenarios;
29+
import fr.brouillard.oss.jgitver.Strategies;
30+
31+
32+
public class Scenario20WithDefaultsTest extends ScenarioTest {
33+
34+
public Scenario20WithDefaultsTest() {
35+
super(
36+
Scenarios.Builders.s20_release_on_head()::getScenario,
37+
calculator -> calculator.setStrategy(Strategies.MAVEN));
38+
}
39+
40+
@Test
41+
public void head_is_on_master_by_default() throws Exception {
42+
assertThat(repository.getBranch(), is("master"));
43+
}
44+
45+
@Test
46+
public void version_of_master() {
47+
// checkout the commit in scenario
48+
unchecked(() -> git.checkout().setName("master").call());
49+
assertThat(versionCalculator.getVersion(), is("1.0.0"));
50+
}
51+
52+
@Test
53+
public void version_of_master_in_dirty_state() throws IOException {
54+
File dirtyFile = null;
55+
try {
56+
// checkout the commit in scenario
57+
unchecked(() -> git.checkout().setName("master").call());
58+
dirtyFile = scenario.makeDirty();
59+
assertThat(versionCalculator.getVersion(), is("1.0.1-SNAPSHOT"));
60+
} finally {
61+
if (dirtyFile != null) {
62+
dirtyFile.delete();
63+
}
64+
}
65+
}
66+
}

src/test/java/fr/brouillard/oss/jgitver/strategy/maven/defaults/Scenario8WithDefaultsTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121

2222
import java.io.File;
23+
import java.io.IOException;
2324
import java.nio.file.Files;
2425

2526
import org.eclipse.jgit.lib.ObjectId;
@@ -106,13 +107,15 @@ public void version_of_annotated_tags() {
106107
}
107108

108109
@Test
109-
public void version_of_annotated_tags_in_dirty_state() {
110+
public void version_of_annotated_tags_in_dirty_state() throws IOException {
110111
unchecked(() -> git.checkout().setName("1.0.0").call());
111-
unchecked(() -> {
112-
File f = scenario.makeDirty();
112+
113+
File f = scenario.makeDirty();
114+
try {
113115
assertThat(versionCalculator.getVersion(), is("1.0.0-dirty"));
114-
Files.deleteIfExists(f.toPath());
115-
});
116+
} finally {
117+
Files.deleteIfExists(f.toPath());
118+
}
116119
}
117120

118121
@Test

0 commit comments

Comments
 (0)