From 7c2bea0cac8e645af9a4bb0e4271170140df5738 Mon Sep 17 00:00:00 2001 From: Konrad Malawski Date: Sun, 9 Feb 2014 04:28:46 +0000 Subject: [PATCH] #91 Allow to limit git properties exposed; WIP --- pom.xml | 8 ++- .../project13/maven/git/GitCommitIdMojo.java | 59 +++++++++++++++++-- .../maven/git/GitDescribeConfig.java | 13 ++++ .../maven/git/ContainsKeyCondition.java | 2 +- .../maven/git/DoesNotContainKeyCondition.java | 43 ++++++++++++++ .../maven/git/GitCommitIdMojoTest.java | 33 +++++++++++ 6 files changed, 150 insertions(+), 8 deletions(-) create mode 100644 src/test/java/pl/project13/maven/git/DoesNotContainKeyCondition.java diff --git a/pom.xml b/pom.xml index a0f305ed..701f62b9 100644 --- a/pom.xml +++ b/pom.xml @@ -117,7 +117,7 @@ com.google.guava guava - 13.0 + 15.0 @@ -187,6 +187,8 @@ + + @@ -209,6 +211,10 @@ + + + + diff --git a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java index b821df5d..3b0c4d0a 100644 --- a/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java +++ b/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java @@ -19,6 +19,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Predicates; +import com.google.common.collect.Lists; import com.google.common.io.Closeables; import com.google.common.io.Files; import org.apache.maven.plugin.AbstractMojo; @@ -40,10 +44,7 @@ import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Properties; +import java.util.*; import static com.google.common.base.Strings.isNullOrEmpty; @@ -119,7 +120,7 @@ public class GitCommitIdMojo extends AbstractMojo { * Specifies whether the execution in pom projects should be skipped. * Override this value to false if you want to force the plugin to run on 'pom' packaged projects. * - * @parameter property="git.skipPoms" default-value="true" + * @parameter default-value="true" */ @SuppressWarnings("UnusedDeclaration") private boolean skipPoms; @@ -248,6 +249,23 @@ public class GitCommitIdMojo extends AbstractMojo { @SuppressWarnings("UnusedDeclaration") private boolean skip = false; + /** + * Can be used to exclude certain properties from being emited into the resulting file. + * May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?), + * or the email of the committer etc. + * + * Each value may be globbing, that is, you can write {@code git.commit.user.*} to exclude both, the {@code name}, + * as well as {@code email} properties from being emitted into the resulting files. + * + * Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}). + * + * @parameter + * @since 2.1.9 + */ + @SuppressWarnings("UnusedDeclaration") + private List excludeProperties = Collections.emptyList(); + + /** * The properties we store our data in and then expose them */ @@ -267,7 +285,7 @@ public void execute() throws MojoExecutionException { return; } - if (isPomProject(project) && skipPoms) { + if (isPomProject(project)) { log("isPomProject is true and skipPoms is true, return"); return; } @@ -287,6 +305,7 @@ public void execute() throws MojoExecutionException { prefixDot = prefix + "."; loadGitData(properties); + filterNot(properties, excludeProperties); loadBuildTimeData(properties); logProperties(properties); @@ -303,6 +322,30 @@ public void execute() throws MojoExecutionException { } + private void filterNot(Properties properties, @Nullable List exclusions) { + if (exclusions == null) + return; + + List> excludePredicates = Lists.transform(exclusions, new Function>() { + @Override + public Predicate apply(String exclude) { + return Predicates.containsPattern(exclude); + } + }); + + Predicate shouldExclude = Predicates.alwaysFalse(); + for (Predicate predicate : excludePredicates) { + shouldExclude = Predicates.or(shouldExclude, predicate); + } + + for (String key : properties.stringPropertyNames()) { + if (shouldExclude.apply(key)) { + System.out.println("shouldExclude.apply(" + key +") = " + shouldExclude.apply(key)); + properties.remove(key); + } + } + } + /** * Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting. * If it's true, an MojoExecutionException will be throw, otherwise we just log an error message. @@ -633,4 +676,8 @@ public void setGitDescribe(GitDescribeConfig gitDescribe) { public void setAbbrevLength(int abbrevLength) { this.abbrevLength = abbrevLength; } + + public void setExcludeProperties(List excludeProperties) { + this.excludeProperties = excludeProperties; + } } diff --git a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java b/src/main/java/pl/project13/maven/git/GitDescribeConfig.java index 3c8e9756..e3d049ba 100644 --- a/src/main/java/pl/project13/maven/git/GitDescribeConfig.java +++ b/src/main/java/pl/project13/maven/git/GitDescribeConfig.java @@ -218,4 +218,17 @@ public Boolean getTags() { public void setTags(Boolean tags) { this.tags = tags; } + + @Override + public String toString() { + return "GitDescribeConfig{" + + "skip=" + skip + + ", always=" + always + + ", dirty='" + dirty + '\'' + + ", match='" + match + '\'' + + ", abbrev=" + abbrev + + ", tags=" + tags + + ", forceLongFormat=" + forceLongFormat + + '}'; + } } diff --git a/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java b/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java index 47353287..9d5616f3 100644 --- a/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java +++ b/src/test/java/pl/project13/maven/git/ContainsKeyCondition.java @@ -36,7 +36,7 @@ public boolean matches(@NotNull Map map) { if (!containsKey) { throw new RuntimeException(String.format("Map did not contain [%s] key! Map is: %s", key, map)); } - return containsKey; + return true; } } diff --git a/src/test/java/pl/project13/maven/git/DoesNotContainKeyCondition.java b/src/test/java/pl/project13/maven/git/DoesNotContainKeyCondition.java new file mode 100644 index 00000000..ae198c78 --- /dev/null +++ b/src/test/java/pl/project13/maven/git/DoesNotContainKeyCondition.java @@ -0,0 +1,43 @@ +/* + * 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.fest.assertions.Condition; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +class DoesNotContainKeyCondition extends Condition> { + + private String key; + + public DoesNotContainKeyCondition(String key) { + this.key = key; + } + + @Override + public boolean matches(@NotNull Map map) { + boolean containsKey = map.containsKey(key); + if (containsKey) { + System.out.println(String.format("Map contained [%s] key! Map is: %s", key, map)); + throw new RuntimeException(String.format("Map contained [%s] key! Map is: %s", key, map)); + } + return true; + } + +} diff --git a/src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java b/src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java index 94188ee2..aee00fc0 100644 --- a/src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java +++ b/src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java @@ -17,6 +17,7 @@ package pl.project13.maven.git; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; import org.apache.maven.project.MavenProject; import org.eclipse.jgit.lib.Repository; @@ -78,6 +79,38 @@ public void shouldIncludeExpectedProperties() throws Exception { verify(mojo).putGitDescribe(any(Properties.class), any(Repository.class)); } + @Test + public void shouldExcludeAsConfiguredProperties() throws Exception { + // given + mojo.setExcludeProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*")); + + // when + mojo.execute(); + + // then + Properties properties = mojo.getProperties(); + + // explicitly excluded + assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.remote.origin.url")); + + // glob excluded + assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.name")); + assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.email")); + assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.name")); + assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.email")); + + // these stay + 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.commit.message.full")); + assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short")); + assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time")); + + verify(mojo).maybePutGitDescribe(any(Properties.class), any(Repository.class)); + verify(mojo).putGitDescribe(any(Properties.class), any(Repository.class)); + } + @Test public void shouldSkipDescribeWhenConfiguredToDoSo() throws Exception { // given