Skip to content

Commit

Permalink
#91 Allow to limit git properties exposed; WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
ktoso committed Feb 9, 2014
1 parent 09710fc commit 7c2bea0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 8 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0</version>
<version>15.0</version>
</dependency>

<!-- IntelliJ Annotations -->
Expand Down Expand Up @@ -187,6 +187,8 @@
</resources>

<plugins>


<!--<plugin>-->
<!--<groupId>pl.project13.maven</groupId>-->
<!--<artifactId>git-commit-id-plugin</artifactId>-->
Expand All @@ -209,6 +211,10 @@

<!--<generateGitPropertiesFile>false</generateGitPropertiesFile>-->

<!--<excludeProperties>-->
<!--<excludeProperty>git.user.*</excludeProperty>-->
<!--</excludeProperties>-->

<!--<gitDescribe>-->
<!--<skip>false</skip>-->
<!--<always>false</always>-->
Expand Down
59 changes: 53 additions & 6 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> excludeProperties = Collections.emptyList();


/**
* The properties we store our data in and then expose them
*/
Expand All @@ -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;
}
Expand All @@ -287,6 +305,7 @@ public void execute() throws MojoExecutionException {
prefixDot = prefix + ".";

loadGitData(properties);
filterNot(properties, excludeProperties);
loadBuildTimeData(properties);
logProperties(properties);

Expand All @@ -303,6 +322,30 @@ public void execute() throws MojoExecutionException {

}

private void filterNot(Properties properties, @Nullable List<String> exclusions) {
if (exclusions == null)
return;

List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
@Override
public Predicate<CharSequence> apply(String exclude) {
return Predicates.containsPattern(exclude);
}
});

Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
for (Predicate<CharSequence> 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.
Expand Down Expand Up @@ -633,4 +676,8 @@ public void setGitDescribe(GitDescribeConfig gitDescribe) {
public void setAbbrevLength(int abbrevLength) {
this.abbrevLength = abbrevLength;
}

public void setExcludeProperties(List<String> excludeProperties) {
this.excludeProperties = excludeProperties;
}
}
13 changes: 13 additions & 0 deletions src/main/java/pl/project13/maven/git/GitDescribeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
*
* 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 <http://www.gnu.org/licenses/>.
*/

package pl.project13.maven.git;

import org.fest.assertions.Condition;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

class DoesNotContainKeyCondition extends Condition<Map<?, ?>> {

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;
}

}
33 changes: 33 additions & 0 deletions src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7c2bea0

Please sign in to comment.