Skip to content

Commit 35a0983

Browse files
author
Hugo Dias
committed
Fix print version when we have an empty repository [patch]
1 parent 4e3e364 commit 35a0983

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/main/java/pt/com/hugodias/gradle/gitversioner/core/version/Version.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public String print(String pattern) {
2424
.replace("%c", String.valueOf(commit))
2525
.replace("%b", branch)
2626
.replace("%H", hash)
27-
.replace("%h", hash.substring(0, 7));
27+
.replace("%h", hash.isEmpty() ? "" : hash.substring(0, 7));
2828
return commit != 0 ? removeParentheses(filledVersion) : removeCommitConditionals(filledVersion);
2929
}
3030

src/test/java/pt/com/hugodias/gradle/gitversioner/tasks/PrintVersionTaskTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ void testPrintsVersionUsingDefinedConfigurationGroovy() throws IOException, GitA
5353
assertThat(result.getOutput()).contains("2.1.1-4");
5454
}
5555

56+
@DisplayName(
57+
"prints version using default configuration when none is supplied in Groovy and we have an empty repository")
58+
@Test
59+
void testPrintsVersionUsingDefaultConfigurationGroovyEmptyRepository()
60+
throws IOException, GitAPIException {
61+
project.withSettingsFile().withGit().withGroovyGradleFile("default");
62+
63+
BuildResult result = gradle.runTask("printVersion");
64+
65+
assertThat(result.getOutput()).contains("0.0.0");
66+
}
67+
5668
private void addCommits() throws GitAPIException {
5769
project
5870
.withCommit("trex")

0 commit comments

Comments
 (0)