Skip to content

Commit 9487285

Browse files
authored
Merge pull request #96 from ericmaino/fix-version-regex
fix: Null Version with Non-Greedy Matcher
2 parents 098a5db + a2301f1 commit 9487285

File tree

3 files changed

+158
-10
lines changed

3 files changed

+158
-10
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group 'me.qoomon'
15-
version '6.3.3'
15+
version '6.4.0'
1616
sourceCompatibility = JavaVersion.VERSION_11
1717
targetCompatibility = JavaVersion.VERSION_11
1818

src/main/java/me/qoomon/gradle/gitversioning/GitVersioningPluginExtension.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import me.qoomon.gradle.gitversioning.GitVersioningPluginConfig.RefPatchDescription;
88
import org.apache.commons.configuration2.PropertiesConfiguration;
99
import org.apache.commons.configuration2.ex.ConfigurationException;
10+
import org.apache.commons.lang3.StringUtils;
1011
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
1112
import org.eclipse.jgit.lib.Repository;
1213
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
@@ -47,7 +48,7 @@ public abstract class GitVersioningPluginExtension {
4748

4849
public static final Logger LOGGER = LoggerFactory.getLogger(GitVersioningPluginExtension.class);
4950

50-
private static final Pattern VERSION_PATTERN = Pattern.compile("(:?(?<core>(?<major>\\d+)(:?\\.(?<minor>\\d+)(:?\\.(?<patch>\\d+))?)?)(:?-(?<label>.*))?)?");
51+
private static final Pattern VERSION_PATTERN = Pattern.compile(".*?(?<core>(?<major>\\d+)(?:\\.(?<minor>\\d+)(?:\\.(?<patch>\\d+))?)?)(?:-(?<label>.*))?|");
5152

5253
private static final String OPTION_NAME_GIT_REF = "git.ref";
5354
private static final String OPTION_NAME_GIT_TAG = "git.tag";
@@ -461,10 +462,7 @@ private Map<String, Supplier<String>> generateFormatPlaceholderMap(String projec
461462
placeholderMap.put("version", Lazy.of(projectVersion));
462463

463464
final Lazy<Matcher> projectVersionMatcher = Lazy.by(() -> {
464-
Matcher matcher = VERSION_PATTERN.matcher(projectVersion);
465-
//noinspection ResultOfMethodCallIgnored
466-
matcher.find();
467-
return matcher;
465+
return matchVersion(projectVersion);
468466
});
469467

470468
placeholderMap.put("version.core", Lazy.by(() -> notNullOrDefault(projectVersionMatcher.get().group("core"), "0.0.0")));
@@ -556,10 +554,7 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
556554
}
557555

558556
final Lazy<Matcher> descriptionTagVersionMatcher = Lazy.by(() -> {
559-
Matcher matcher = VERSION_PATTERN.matcher(descriptionTag.get());
560-
//noinspection ResultOfMethodCallIgnored
561-
matcher.find();
562-
return matcher;
557+
return matchVersion(descriptionTag.get());
563558
});
564559

565560
placeholderMap.put("describe.tag.version", Lazy.by(() -> notNullOrDefault(descriptionTagVersionMatcher.get().group(), "0.0.0")));
@@ -603,6 +598,14 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
603598
return placeholderMap;
604599
}
605600

601+
private Matcher matchVersion(String input) {
602+
Matcher matcher = VERSION_PATTERN.matcher(input);
603+
//noinspection ResultOfMethodCallIgnored
604+
matcher.find();
605+
606+
return matcher;
607+
}
608+
606609
private static Map<String, String> generateGitProjectProperties(GitSituation gitSituation, GitVersionDetails gitVersionDetails) {
607610
final Map<String, String> properties = new HashMap<>();
608611

src/test/java/me/qoomon/gradle/gitversioning/GitVersioningPluginTest.java

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,4 +321,149 @@ void apply_CommitWithLabelTagGivesExpectedLabel() throws GitAPIException, IOExce
321321
// then
322322
assertThat(project.getVersion()).isEqualTo("677-SNAPSHOT");
323323
}
324+
325+
@Test
326+
void apply_TagWithInvalidFormatGiveInitialVersion() throws GitAPIException, IOException {
327+
// given
328+
Git git = Git.init().setInitialBranch(MASTER).setDirectory(projectDir.toFile()).call();
329+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
330+
String givenTag = "successfulBuild";
331+
git.tag().setName(givenTag).call();
332+
333+
Project project = ProjectBuilder.builder().withProjectDir(projectDir.toFile()).build();
334+
335+
project.getPluginManager().apply(GitVersioningPlugin.class);
336+
337+
GitVersioningPluginExtension extension = (GitVersioningPluginExtension) project.getExtensions()
338+
.getByName("gitVersioning");
339+
340+
GitVersioningPluginConfig config = new GitVersioningPluginConfig() {{
341+
refs.branch(".*", patch -> {
342+
patch.version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}";
343+
});
344+
}};
345+
346+
// when
347+
extension.apply(config);
348+
349+
// then
350+
assertThat(project.getVersion()).isEqualTo("0.0.0");
351+
}
352+
353+
@Test
354+
void apply_TagWithSingleNonDigitPrefixGivesExpectedVersion() throws GitAPIException, IOException {
355+
// given
356+
Git git = Git.init().setInitialBranch(MASTER).setDirectory(projectDir.toFile()).call();
357+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
358+
String givenTag = "v2.0.4";
359+
git.tag().setName(givenTag).call();
360+
361+
Project project = ProjectBuilder.builder().withProjectDir(projectDir.toFile()).build();
362+
363+
project.getPluginManager().apply(GitVersioningPlugin.class);
364+
365+
GitVersioningPluginExtension extension = (GitVersioningPluginExtension) project.getExtensions()
366+
.getByName("gitVersioning");
367+
368+
GitVersioningPluginConfig config = new GitVersioningPluginConfig() {{
369+
refs.branch(".*", patch -> {
370+
patch.version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}";
371+
});
372+
}};
373+
374+
// when
375+
extension.apply(config);
376+
377+
// then
378+
assertThat(project.getVersion()).isEqualTo("2.0.4");
379+
}
380+
381+
@Test
382+
void apply_TagWithSingleWordPrefixGivesExpectedVersion() throws GitAPIException, IOException {
383+
// given
384+
Git git = Git.init().setInitialBranch(MASTER).setDirectory(projectDir.toFile()).call();
385+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
386+
String givenTag = "alpha1.2.3";
387+
git.tag().setName(givenTag).call();
388+
389+
Project project = ProjectBuilder.builder().withProjectDir(projectDir.toFile()).build();
390+
391+
project.getPluginManager().apply(GitVersioningPlugin.class);
392+
393+
GitVersioningPluginExtension extension = (GitVersioningPluginExtension) project.getExtensions()
394+
.getByName("gitVersioning");
395+
396+
GitVersioningPluginConfig config = new GitVersioningPluginConfig() {{
397+
refs.branch(".*", patch -> {
398+
patch.version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch}";
399+
});
400+
}};
401+
402+
// when
403+
extension.apply(config);
404+
405+
// then
406+
assertThat(project.getVersion()).isEqualTo("1.2.3");
407+
}
408+
409+
@Test
410+
void apply_TwoCommitsSinceLastTagGivesExpectedPatchDistanceAndBranch() throws GitAPIException, IOException {
411+
// given
412+
Git git = Git.init().setInitialBranch("featureA").setDirectory(projectDir.toFile()).call();
413+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
414+
String givenTag = "v2.0.4";
415+
git.tag().setName(givenTag).call();
416+
git.commit().setMessage("commit two").setAllowEmpty(true).call();
417+
git.commit().setMessage("commit three").setAllowEmpty(true).call();
418+
419+
Project project = ProjectBuilder.builder().withProjectDir(projectDir.toFile()).build();
420+
421+
project.getPluginManager().apply(GitVersioningPlugin.class);
422+
423+
GitVersioningPluginExtension extension = (GitVersioningPluginExtension) project.getExtensions()
424+
.getByName("gitVersioning");
425+
426+
GitVersioningPluginConfig config = new GitVersioningPluginConfig() {{
427+
refs.branch(".*", patch -> {
428+
patch.version = "${describe.tag.version.major}.${describe.tag.version.minor}.${describe.tag.version.patch.next}-${describe.distance}-${ref.slug}";
429+
});
430+
}};
431+
432+
// when
433+
extension.apply(config);
434+
435+
// then
436+
assertThat(project.getVersion()).isEqualTo("2.0.5-2-featureA");
437+
}
438+
439+
@Test
440+
void apply_GivenTwoVersionTagsUseTagMatchingDescribePattern() throws GitAPIException, IOException {
441+
// given
442+
Git git = Git.init().setInitialBranch("featureA").setDirectory(projectDir.toFile()).call();
443+
git.commit().setMessage("initial commit").setAllowEmpty(true).call();
444+
String givenTag = "v2.0.4";
445+
git.tag().setName(givenTag).call();
446+
String givenSecondaryTag = "35.1.3";
447+
git.tag().setName(givenSecondaryTag).call();
448+
449+
Project project = ProjectBuilder.builder().withProjectDir(projectDir.toFile()).build();
450+
451+
project.getPluginManager().apply(GitVersioningPlugin.class);
452+
453+
GitVersioningPluginExtension extension = (GitVersioningPluginExtension) project.getExtensions()
454+
.getByName("gitVersioning");
455+
456+
GitVersioningPluginConfig config = new GitVersioningPluginConfig() {{
457+
describeTagPattern = "v2.0.*";
458+
refs.branch(".*", patch -> {
459+
patch.version = "${describe.tag.version.core}";
460+
});
461+
}};
462+
463+
// when
464+
extension.apply(config);
465+
466+
// then
467+
assertThat(project.getVersion()).isEqualTo("2.0.4");
468+
}
324469
}

0 commit comments

Comments
 (0)