diff --git a/README.md b/README.md index 738d1ac1..3a27170d 100644 --- a/README.md +++ b/README.md @@ -214,7 +214,10 @@ It's really simple to setup this plugin; below is a sample pom that you may base -dirty - + + + * + + diff --git a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java b/src/main/java/pl/project13/maven/git/GitDescribeConfig.java index 1d48ad33..a5dc0848 100644 --- a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java +++ b/src/main/java/pl/project13/maven/git/GitDescribeConfig.java @@ -148,6 +148,8 @@ public class GitDescribeConfig { * since tag v1.2 that points at object deadbee....). *

*

false
by default. + * + * @parameter default-value=false */ private boolean forceLongFormat; diff --git a/src/main/java/pl/project13/maven/git/NativeGitProvider.java b/src/main/java/pl/project13/maven/git/NativeGitProvider.java index af789105..eb454c47 100644 --- a/src/main/java/pl/project13/maven/git/NativeGitProvider.java +++ b/src/main/java/pl/project13/maven/git/NativeGitProvider.java @@ -128,11 +128,16 @@ private String getArgumentsForGitDescribeAndDescibeNotNull(GitDescribeConfig git String dirtyMark = gitDescribe.getDirty(); if (dirtyMark != null && !dirtyMark.isEmpty()) { - // Option: --dirty[=] // TODO: Code Injection? Or does the CliRunner escape Arguments? argumentsForGitDescribe.append("--dirty=" + dirtyMark + " "); } + String matchOption = gitDescribe.getMatch(); + if (matchOption != null && !matchOption.isEmpty()) { + // TODO: Code Injection? Or does the CliRunner escape Arguments? + argumentsForGitDescribe.append("--match=" + matchOption + " "); + } + argumentsForGitDescribe.append("--abbrev=" + gitDescribe.getAbbrev() + " "); if (gitDescribe.getTags()) { diff --git a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java b/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java index c8239a3f..520b2391 100644 --- a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java +++ b/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java @@ -565,6 +565,40 @@ public void shouldWorkWithNullGitDescribe(boolean useNativeGit) throws Exception assertGitPropertiesPresentInProject(targetProject.getProperties()); } + @Test + @Parameters(method = "useNativeGit") + public void runGitDescribeWithMatchOption(boolean useNativeGit) throws Exception { + // given + mavenSandbox.withParentProject("my-pom-project", "pom") + .withChildProject("my-jar-module", "jar") + .withGitRepoInChild(AvailableGitTestRepo.MAVEN_GIT_COMMIT_ID_PLUGIN) + .create(CleanUp.CLEANUP_FIRST); + MavenProject targetProject = mavenSandbox.getChildProject(); + + setProjectToExecuteMojoIn(targetProject); + + Map gitTagMap = new HashMap(); + gitTagMap.put("v2.1.8", "4f787aa37d5d9c06780278f0cf92553d304820a2"); + gitTagMap.put("v2.1.9", "a9dba4a25b64ab288d90cd503785b830d2e189a2"); + + for (Map.Entry entry : gitTagMap.entrySet()) { + String gitDescribeMatchNeedle = entry.getKey(); + String commitIdOfMatchNeedle = entry.getValue(); + + GitDescribeConfig gitDescribeConfig = new GitDescribeConfig(); + gitDescribeConfig.setMatch(gitDescribeMatchNeedle); + alterMojoSettings("gitDescribe", gitDescribeConfig); + alterMojoSettings("useNativeGit", useNativeGit); + + // when + mojo.execute(); + + // then + assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", gitDescribeMatchNeedle)); + assertThat(targetProject.getProperties().get("git.commit.id")).isNotEqualTo(commitIdOfMatchNeedle); + } + } + private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev) { GitDescribeConfig gitDescribeConfig = new GitDescribeConfig(); gitDescribeConfig.setTags(true); diff --git a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java.orig b/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java.orig deleted file mode 100644 index 3a30b339..00000000 --- a/src/test/java/pl/project13/maven/git/GitCommitIdMojoIntegrationTest.java.orig +++ /dev/null @@ -1,414 +0,0 @@ -/* - * This file is part of git-commit-id-plugin by Konrad Malawski - * - * git-commit-id-plugin is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * git-commit-id-plugin is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with git-commit-id-plugin. If not, see . - */ - -package pl.project13.maven.git; - -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.project.MavenProject; -import org.codehaus.plexus.util.FileUtils; -import org.junit.Test; -import org.junit.runner.RunWith; -import java.util.Arrays; -import java.util.Collection; -import junitparams.JUnitParamsRunner; -import junitparams.Parameters; - - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.io.Files; -import pl.project13.maven.git.FileSystemMavenSandbox.CleanUp; -import pl.project13.test.utils.AssertException; - -import java.io.File; -import java.nio.charset.Charset; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.MapAssert.entry; -import static org.mockito.internal.util.reflection.Whitebox.setInternalState; - -@RunWith(JUnitParamsRunner.class) -public class GitCommitIdMojoIntegrationTest extends GitIntegrationTest { - - public static Collection defaultParameter() { - return Arrays.asList(new Object[] { - Boolean.FALSE, - Boolean.TRUE - }); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldResolvePropertiesOnDefaultSettingsForNonPomProject(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-jar-project", "jar").withNoChildProject().withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertGitPropertiesPresentInProject(targetProject.getProperties()); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldNotRunWhenSkipIsSet(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-skip-project", "jar").withNoChildProject().withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("skip", Boolean.TRUE); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).isEmpty(); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldNotRunWhenPackagingPomAndDefaultSettingsApply(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom").withNoChildProject().withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).isEmpty(); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldRunWhenPackagingPomAndSkipPomsFalse(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom").withNoChildProject().withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("skipPoms", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).isNotEmpty(); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldUseParentProjectRepoWhenInvokedFromChild(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom").withChildProject("my-jar-module", "jar").withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getChildProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("skipPoms", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertGitPropertiesPresentInProject(targetProject.getProperties()); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldUseChildProjectRepoIfInvokedFromChild(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom").withChildProject("my-jar-module", "jar").withGitRepoInChild(AvailableGitTestRepo.WITH_ONE_COMMIT).create(CleanUp.CLEANUP_FIRST); - MavenProject targetProject = mavenSandbox.getChildProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("skipPoms", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertGitPropertiesPresentInProject(targetProject.getProperties()); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldFailWithExceptionWhenNoGitRepoFound(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withNoGitRepoAvailable() - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("skipPoms", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - AssertException.CodeBlock block = new AssertException.CodeBlock() { - @Override - public void run() throws Exception { - mojo.execute(); - } - }; - - // then - AssertException.thrown(MojoExecutionException.class, block); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateCustomPropertiesFileProperties(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - String targetFilePath = "target/classes/custom-git.properties"; - File expectedFile = new File(targetProject.getBasedir(), targetFilePath); - - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("generateGitPropertiesFile", true); - alterMojoSettings("generateGitPropertiesFilename", targetFilePath); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - try { - mojo.execute(); - - // then - assertThat(expectedFile).exists(); - } finally { - FileUtils.forceDelete(expectedFile); - } - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - String targetFilePath = "target/classes/custom-git.properties"; - File expectedFile = new File(targetProject.getBasedir(), targetFilePath); - - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("generateGitPropertiesFile", true); - alterMojoSettings("generateGitPropertiesFilename", targetFilePath); - alterMojoSettings("format", "json"); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - try { - mojo.execute(); - - // then - assertThat(expectedFile).exists(); - String json = Files.toString(expectedFile, Charset.defaultCharset()); - ObjectMapper om = new ObjectMapper(); - Map map = new HashMap(); - map = om.readValue(expectedFile, map.getClass()); - assertThat(map.size() > 10); - } finally { - FileUtils.forceDelete(expectedFile); - } - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldSkipWithoutFailOnNoGitDirectoryWhenNoGitRepoFound(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-jar-project", "jar") - .withNoChildProject() - .withNoGitRepoAvailable() - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("failOnNoGitDirectory", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).isEmpty(); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldNotSkipWithoutFailOnNoGitDirectoryWhenNoGitRepoIsPresent(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-jar-project", "jar") - .withNoChildProject() - .withGitRepoInParent(AvailableGitTestRepo.WITH_ONE_COMMIT) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getParentProject(); - setProjectToExecuteMojoIn(targetProject); - alterMojoSettings("failOnNoGitDirectory", false); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertGitPropertiesPresentInProject(targetProject.getProperties()); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateDescribeWithTagOnlyWhenForceLongFormatIsFalse(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - setProjectToExecuteMojoIn(targetProject); - GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(false,7); - alterMojoSettings("gitDescribe", gitDescribeConfig); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0")); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateDescribeWithTagOnlyWhenForceLongFormatIsFalseAndAbbrevLengthIsNonDefault(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - setProjectToExecuteMojoIn(targetProject); - GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(false,10); - alterMojoSettings("gitDescribe", gitDescribeConfig); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0")); - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe-short", "v1.0.0")); - } - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateDescribeWithTagAndZeroAndCommitIdWhenForceLongFormatIsTrue(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - setProjectToExecuteMojoIn(targetProject); - GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(true,7); - alterMojoSettings("gitDescribe", gitDescribeConfig); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0-0-gde4db35")); - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe-short", "v1.0.0-0")); - } -<<<<<<< HEAD - -======= - - @Test - @Parameters(method = "defaultParameter") - public void shouldGenerateDescribeWithTagAndZeroAndCommitIdWhenForceLongFormatIsTrueAndAbbrevLengthIsNonDefault(boolean useNativeGit) throws Exception { - // given - mavenSandbox.withParentProject("my-pom-project", "pom") - .withChildProject("my-jar-module", "jar") - .withGitRepoInChild(AvailableGitTestRepo.ON_A_TAG) - .create(CleanUp.CLEANUP_FIRST); - - MavenProject targetProject = mavenSandbox.getChildProject(); - - setProjectToExecuteMojoIn(targetProject); - GitDescribeConfig gitDescribeConfig = createGitDescribeConfig(true,10); - alterMojoSettings("gitDescribe", gitDescribeConfig); - alterMojoSettings("useNativeGit", useNativeGit); - - // when - mojo.execute(); - - // then - assertThat(targetProject.getProperties()).includes(entry("git.commit.id.describe", "v1.0.0-0-gde4db35")); - } - - private GitDescribeConfig createGitDescribeConfig(boolean forceLongFormat, int abbrev){ - GitDescribeConfig gitDescribeConfig = new GitDescribeConfig(); - gitDescribeConfig.setTags(true); - gitDescribeConfig.setForceLongFormat(forceLongFormat); - gitDescribeConfig.setAbbrev(abbrev); - return gitDescribeConfig; - } - ->>>>>>> Added some tests which modify the abbrev length of the git.discribe to identify problems with the native git thingy - private void alterMojoSettings(String parameterName, Object parameterValue) { - setInternalState(mojo, parameterName, parameterValue); - } - - private void assertGitPropertiesPresentInProject(Properties properties) { - assertThat(properties).satisfies(new ContainsKeyCondition("git.build.time")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.branch")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.email")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time")); - assertThat(properties).satisfies(new ContainsKeyCondition("git.remote.origin.url")); - } -}