Skip to content

Commit 7c2bea0

Browse files
committed
#91 Allow to limit git properties exposed; WIP
1 parent 09710fc commit 7c2bea0

File tree

6 files changed

+150
-8
lines changed

6 files changed

+150
-8
lines changed

pom.xml

+7-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
<dependency>
118118
<groupId>com.google.guava</groupId>
119119
<artifactId>guava</artifactId>
120-
<version>13.0</version>
120+
<version>15.0</version>
121121
</dependency>
122122

123123
<!-- IntelliJ Annotations -->
@@ -187,6 +187,8 @@
187187
</resources>
188188

189189
<plugins>
190+
191+
190192
<!--<plugin>-->
191193
<!--<groupId>pl.project13.maven</groupId>-->
192194
<!--<artifactId>git-commit-id-plugin</artifactId>-->
@@ -209,6 +211,10 @@
209211

210212
<!--<generateGitPropertiesFile>false</generateGitPropertiesFile>-->
211213

214+
<!--<excludeProperties>-->
215+
<!--<excludeProperty>git.user.*</excludeProperty>-->
216+
<!--</excludeProperties>-->
217+
212218
<!--<gitDescribe>-->
213219
<!--<skip>false</skip>-->
214220
<!--<always>false</always>-->

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

+53-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919

2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.google.common.annotations.VisibleForTesting;
22+
import com.google.common.base.Function;
23+
import com.google.common.base.Predicate;
24+
import com.google.common.base.Predicates;
25+
import com.google.common.collect.Lists;
2226
import com.google.common.io.Closeables;
2327
import com.google.common.io.Files;
2428
import org.apache.maven.plugin.AbstractMojo;
@@ -40,10 +44,7 @@
4044
import java.io.FileWriter;
4145
import java.io.IOException;
4246
import java.text.SimpleDateFormat;
43-
import java.util.Date;
44-
import java.util.List;
45-
import java.util.Map;
46-
import java.util.Properties;
47+
import java.util.*;
4748

4849
import static com.google.common.base.Strings.isNullOrEmpty;
4950

@@ -119,7 +120,7 @@ public class GitCommitIdMojo extends AbstractMojo {
119120
* Specifies whether the execution in pom projects should be skipped.
120121
* Override this value to false if you want to force the plugin to run on 'pom' packaged projects.
121122
*
122-
* @parameter property="git.skipPoms" default-value="true"
123+
* @parameter default-value="true"
123124
*/
124125
@SuppressWarnings("UnusedDeclaration")
125126
private boolean skipPoms;
@@ -248,6 +249,23 @@ public class GitCommitIdMojo extends AbstractMojo {
248249
@SuppressWarnings("UnusedDeclaration")
249250
private boolean skip = false;
250251

252+
/**
253+
* Can be used to exclude certain properties from being emited into the resulting file.
254+
* May be useful when you want to hide {@code git.remote.origin.url} (maybe because it contains your repo password?),
255+
* or the email of the committer etc.
256+
*
257+
* Each value may be globbing, that is, you can write {@code git.commit.user.*} to exclude both, the {@code name},
258+
* as well as {@code email} properties from being emitted into the resulting files.
259+
*
260+
* Please note that the strings here are Java regexes ({@code .*} is globbing, not plain {@code *}).
261+
*
262+
* @parameter
263+
* @since 2.1.9
264+
*/
265+
@SuppressWarnings("UnusedDeclaration")
266+
private List<String> excludeProperties = Collections.emptyList();
267+
268+
251269
/**
252270
* The properties we store our data in and then expose them
253271
*/
@@ -267,7 +285,7 @@ public void execute() throws MojoExecutionException {
267285
return;
268286
}
269287

270-
if (isPomProject(project) && skipPoms) {
288+
if (isPomProject(project)) {
271289
log("isPomProject is true and skipPoms is true, return");
272290
return;
273291
}
@@ -287,6 +305,7 @@ public void execute() throws MojoExecutionException {
287305
prefixDot = prefix + ".";
288306

289307
loadGitData(properties);
308+
filterNot(properties, excludeProperties);
290309
loadBuildTimeData(properties);
291310
logProperties(properties);
292311

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

304323
}
305324

325+
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
326+
if (exclusions == null)
327+
return;
328+
329+
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
330+
@Override
331+
public Predicate<CharSequence> apply(String exclude) {
332+
return Predicates.containsPattern(exclude);
333+
}
334+
});
335+
336+
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
337+
for (Predicate<CharSequence> predicate : excludePredicates) {
338+
shouldExclude = Predicates.or(shouldExclude, predicate);
339+
}
340+
341+
for (String key : properties.stringPropertyNames()) {
342+
if (shouldExclude.apply(key)) {
343+
System.out.println("shouldExclude.apply(" + key +") = " + shouldExclude.apply(key));
344+
properties.remove(key);
345+
}
346+
}
347+
}
348+
306349
/**
307350
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
308351
* 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) {
633676
public void setAbbrevLength(int abbrevLength) {
634677
this.abbrevLength = abbrevLength;
635678
}
679+
680+
public void setExcludeProperties(List<String> excludeProperties) {
681+
this.excludeProperties = excludeProperties;
682+
}
636683
}

src/main/java/pl/project13/maven/git/GitDescribeConfig.java

+13
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,17 @@ public Boolean getTags() {
218218
public void setTags(Boolean tags) {
219219
this.tags = tags;
220220
}
221+
222+
@Override
223+
public String toString() {
224+
return "GitDescribeConfig{" +
225+
"skip=" + skip +
226+
", always=" + always +
227+
", dirty='" + dirty + '\'' +
228+
", match='" + match + '\'' +
229+
", abbrev=" + abbrev +
230+
", tags=" + tags +
231+
", forceLongFormat=" + forceLongFormat +
232+
'}';
233+
}
221234
}

src/test/java/pl/project13/maven/git/ContainsKeyCondition.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public boolean matches(@NotNull Map<?, ?> map) {
3636
if (!containsKey) {
3737
throw new RuntimeException(String.format("Map did not contain [%s] key! Map is: %s", key, map));
3838
}
39-
return containsKey;
39+
return true;
4040
}
4141

4242
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad Malawski <[email protected]>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.git;
19+
20+
import org.fest.assertions.Condition;
21+
import org.jetbrains.annotations.NotNull;
22+
23+
import java.util.Map;
24+
25+
class DoesNotContainKeyCondition extends Condition<Map<?, ?>> {
26+
27+
private String key;
28+
29+
public DoesNotContainKeyCondition(String key) {
30+
this.key = key;
31+
}
32+
33+
@Override
34+
public boolean matches(@NotNull Map<?, ?> map) {
35+
boolean containsKey = map.containsKey(key);
36+
if (containsKey) {
37+
System.out.println(String.format("Map contained [%s] key! Map is: %s", key, map));
38+
throw new RuntimeException(String.format("Map contained [%s] key! Map is: %s", key, map));
39+
}
40+
return true;
41+
}
42+
43+
}

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

+33
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package pl.project13.maven.git;
1919

20+
import com.google.common.collect.ImmutableList;
2021
import com.google.common.collect.Maps;
2122
import org.apache.maven.project.MavenProject;
2223
import org.eclipse.jgit.lib.Repository;
@@ -78,6 +79,38 @@ public void shouldIncludeExpectedProperties() throws Exception {
7879
verify(mojo).putGitDescribe(any(Properties.class), any(Repository.class));
7980
}
8081

82+
@Test
83+
public void shouldExcludeAsConfiguredProperties() throws Exception {
84+
// given
85+
mojo.setExcludeProperties(ImmutableList.of("git.remote.origin.url", ".*.user.*"));
86+
87+
// when
88+
mojo.execute();
89+
90+
// then
91+
Properties properties = mojo.getProperties();
92+
93+
// explicitly excluded
94+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.remote.origin.url"));
95+
96+
// glob excluded
97+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.name"));
98+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.build.user.email"));
99+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.name"));
100+
assertThat(properties).satisfies(new DoesNotContainKeyCondition("git.commit.user.email"));
101+
102+
// these stay
103+
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
104+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
105+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
106+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.full"));
107+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.message.short"));
108+
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.time"));
109+
110+
verify(mojo).maybePutGitDescribe(any(Properties.class), any(Repository.class));
111+
verify(mojo).putGitDescribe(any(Properties.class), any(Repository.class));
112+
}
113+
81114
@Test
82115
public void shouldSkipDescribeWhenConfiguredToDoSo() throws Exception {
83116
// given

0 commit comments

Comments
 (0)