Skip to content

Commit 4e3e364

Browse files
author
Hugo Dias
committed
Now, if we have an empty repository, it will start from the default version 0.0.0 or from the configuration. [patch]
1 parent 82cc50e commit 4e3e364

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import lombok.val;
1111
import org.eclipse.jgit.api.Git;
1212
import org.eclipse.jgit.api.errors.GitAPIException;
13+
import org.eclipse.jgit.revwalk.RevCommit;
1314
import pt.com.hugodias.gradle.gitversioner.core.exception.VersionerException;
1415

1516
@Data
@@ -25,9 +26,12 @@ public Version version(VersionerConfig config) {
2526
var commit = 0;
2627

2728
val branch = git.getRepository().getBranch();
28-
val hash = git.getRepository().findRef("HEAD").getObjectId().getName();
29-
30-
val all = reverse(git.log().call());
29+
String hash = "";
30+
List<RevCommit> all = new LinkedList<>();
31+
if (git.getRepository().findRef("HEAD").getObjectId() != null) {
32+
hash = git.getRepository().findRef("HEAD").getObjectId().getName();
33+
all = reverse(git.log().call());
34+
}
3135

3236
for (val it : all) {
3337
if (it.getFullMessage().contains(config.getMatchMajor())) {

src/test/java/pt/com/hugodias/gradle/gitversioner/core/VersionerTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,28 @@ void testVersionIncludesCommitHashFromHead() throws GitAPIException, IOException
243243
.isEqualTo(git.getRepository().findRef("HEAD").getObjectId().getName());
244244
}
245245

246+
@Test
247+
@DisplayName("Repository is empty")
248+
void testWithAnEmptyRepository() {
249+
VersionerConfig versionerConfig = VersionerConfig.builder().build();
250+
Versioner versioner = Versioner.builder().gitFolder(projectDir).build();
251+
Version version = versioner.version(versionerConfig);
252+
assertThat(version.getMajor()).isEqualTo(0);
253+
assertThat(version.getMinor()).isEqualTo(0);
254+
assertThat(version.getPatch()).isEqualTo(0);
255+
}
256+
257+
@Test
258+
@DisplayName("Repository is empty and we have a patch custom configuration")
259+
void testWithAnEmptyRepositoryWithCustomPatch() {
260+
VersionerConfig versionerConfig = VersionerConfig.builder().startFromPatch(1).build();
261+
Versioner versioner = Versioner.builder().gitFolder(projectDir).build();
262+
Version version = versioner.version(versionerConfig);
263+
assertThat(version.getMajor()).isEqualTo(0);
264+
assertThat(version.getMinor()).isEqualTo(0);
265+
assertThat(version.getPatch()).isEqualTo(1);
266+
}
267+
246268
private void givenRepositoryHasTypeCommitsNumbering(String message, int number)
247269
throws GitAPIException {
248270
createCommits(String.format("[%s]", message), number);

0 commit comments

Comments
 (0)