Skip to content

Commit 7319382

Browse files
committed
test fixes
1 parent 70a17c8 commit 7319382

File tree

8 files changed

+713
-253
lines changed

8 files changed

+713
-253
lines changed

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,8 @@
2828
import org.apache.maven.plugin.AbstractMojo;
2929
import org.apache.maven.plugin.MojoExecutionException;
3030
import org.apache.maven.project.MavenProject;
31-
import org.eclipse.jgit.api.errors.GitAPIException;
32-
import org.eclipse.jgit.lib.AbbreviatedObjectId;
33-
import org.eclipse.jgit.lib.Constants;
34-
import org.eclipse.jgit.lib.ObjectReader;
35-
import org.eclipse.jgit.lib.Ref;
36-
import org.eclipse.jgit.lib.Repository;
37-
import org.eclipse.jgit.revwalk.RevCommit;
38-
import org.eclipse.jgit.revwalk.RevWalk;
39-
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
4031
import org.jetbrains.annotations.NotNull;
4132
import org.jetbrains.annotations.Nullable;
42-
import pl.project13.jgit.DescribeCommand;
43-
import pl.project13.jgit.DescribeResult;
4433
import pl.project13.maven.git.log.LoggerBridge;
4534
import pl.project13.maven.git.log.MavenLoggerBridge;
4635
import pl.project13.maven.git.util.PropertyManager;
@@ -49,7 +38,10 @@
4938
import java.io.FileWriter;
5039
import java.io.IOException;
5140
import java.text.SimpleDateFormat;
52-
import java.util.*;
41+
import java.util.Collections;
42+
import java.util.Date;
43+
import java.util.List;
44+
import java.util.Properties;
5345

5446
/**
5547
* Goal which puts git build-time information into property files or maven's properties.
@@ -376,7 +368,7 @@ private void handlePluginFailure(Exception e) throws MojoExecutionException {
376368
if (failOnUnableToExtractRepoInfo) {
377369
throw new MojoExecutionException("Could not complete Mojo execution...", e);
378370
} else {
379-
loggerBridge.error(e.getMessage());
371+
loggerBridge.error(e.getMessage(), com.google.common.base.Throwables.getStackTraceAsString(e));
380372
}
381373
}
382374

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

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,10 @@
11
package pl.project13.maven.git;
22

3-
import java.io.ByteArrayInputStream;
4-
import java.io.File;
5-
import java.io.IOException;
6-
import java.io.InputStream;
7-
import java.text.ParseException;
8-
import java.text.SimpleDateFormat;
9-
import java.util.Date;
10-
import java.util.LinkedHashMap;
11-
import java.util.Locale;
12-
import java.util.Map;
13-
import org.jetbrains.annotations.NotNull;
14-
import org.jetbrains.annotations.Nullable;
3+
import com.google.common.base.Splitter;
154
import org.apache.maven.plugin.MojoExecutionException;
5+
import org.jetbrains.annotations.NotNull;
166
import pl.project13.maven.git.log.LoggerBridge;
17-
import pl.project13.maven.git.log.MavenLoggerBridge;
7+
188
import java.io.*;
199

2010

@@ -40,12 +30,12 @@ public static NativeGitProvider on(@NotNull File dotGitDirectory) {
4030
return new NativeGitProvider(dotGitDirectory);
4131
}
4232

43-
NativeGitProvider(@NotNull File dotGitDirectory){
33+
NativeGitProvider(@NotNull File dotGitDirectory) {
4434
this.dotGitDirectory = dotGitDirectory;
4535
}
4636

4737

48-
@NotNull
38+
@NotNull
4939
public NativeGitProvider withLoggerBridge(LoggerBridge bridge) {
5040
super.loggerBridge = bridge;
5141
return this;
@@ -73,36 +63,36 @@ public NativeGitProvider setDateFormat(String dateFormat) {
7363
return this;
7464
}
7565

76-
public NativeGitProvider setGitDescribe(GitDescribeConfig gitDescribe){
66+
public NativeGitProvider setGitDescribe(GitDescribeConfig gitDescribe) {
7767
super.gitDescribe = gitDescribe;
7868
return this;
7969
}
8070

8171
@Override
82-
protected void init() throws MojoExecutionException{
83-
try{
72+
protected void init() throws MojoExecutionException {
73+
try {
8474
canonical = dotGitDirectory.getCanonicalFile();
8575
} catch (Exception ex) {
8676
throw new MojoExecutionException("Passed a invalid directory, not a GIT repository: " + dotGitDirectory, ex);
8777
}
8878
}
8979

9080
@Override
91-
protected String getBuildAuthorName(){
81+
protected String getBuildAuthorName() {
9282
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%an\"");
9383
}
9484

9585
@Override
96-
protected String getBuildAuthorEmail(){
86+
protected String getBuildAuthorEmail() {
9787
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%ae\"");
9888
}
9989

10090
@Override
101-
protected void prepareGitToExtractMoreDetailedReproInformation() throws MojoExecutionException{
91+
protected void prepareGitToExtractMoreDetailedReproInformation() throws MojoExecutionException {
10292
}
10393

10494
@Override
105-
protected String getBranchName() throws IOException{
95+
protected String getBranchName() throws IOException {
10696
return getBranch(canonical);
10797
}
10898

@@ -115,115 +105,118 @@ private String getBranch(File canonical) {
115105
}
116106

117107
@Override
118-
protected String getGitDescribe() throws MojoExecutionException{
108+
protected String getGitDescribe() throws MojoExecutionException {
119109
String argumentsForGitDescribe = getArgumentsForGitDescribe(super.gitDescribe);
120110
String gitDescribe = tryToRunGitCommand(canonical, "describe " + argumentsForGitDescribe);
121111
return gitDescribe;
122112
}
123113

124-
private String getArgumentsForGitDescribe(GitDescribeConfig gitDescribe){
125-
if(gitDescribe != null){
114+
private String getArgumentsForGitDescribe(GitDescribeConfig gitDescribe) {
115+
if (gitDescribe != null) {
126116
return getArgumentsForGitDescribeAndDescibeNotNull(gitDescribe);
127-
}else{
117+
} else {
128118
return "";
129119
}
130120
}
131-
132-
private String getArgumentsForGitDescribeAndDescibeNotNull(GitDescribeConfig gitDescribe){
121+
122+
private String getArgumentsForGitDescribeAndDescibeNotNull(GitDescribeConfig gitDescribe) {
133123
StringBuilder argumentsForGitDescribe = new StringBuilder();
134124

135-
if(gitDescribe.isAlways()){
125+
if (gitDescribe.isAlways()) {
136126
argumentsForGitDescribe.append("--always ");
137127
}
138128

139129
String dirtyMark = gitDescribe.getDirty();
140-
if(dirtyMark != null && !dirtyMark.isEmpty()){
130+
if (dirtyMark != null && !dirtyMark.isEmpty()) {
141131
// Option: --dirty[=<mark>]
142132
// TODO: Code Injection? Or does the CliRunner escape Arguments?
143133
argumentsForGitDescribe.append("--dirty=" + dirtyMark + " ");
144134
}
145135

146136
argumentsForGitDescribe.append("--abbrev=" + gitDescribe.getAbbrev() + " ");
147137

148-
if(gitDescribe.getTags()){
138+
if (gitDescribe.getTags()) {
149139
argumentsForGitDescribe.append("--tags ");
150140
}
151141

152-
if(gitDescribe.getForceLongFormat()){
142+
if (gitDescribe.getForceLongFormat()) {
153143
argumentsForGitDescribe.append("--long ");
154144
}
155145
return argumentsForGitDescribe.toString();
156146
}
157147

158148
@Override
159-
protected String getCommitId(){
149+
protected String getCommitId() {
160150
return tryToRunGitCommand(canonical, "rev-parse HEAD");
161151
}
162152

163153
@Override
164-
protected String getAbbrevCommitId() throws MojoExecutionException{
154+
protected String getAbbrevCommitId() throws MojoExecutionException {
165155
// we could run: tryToRunGitCommand(canonical, "rev-parse --short="+abbrevLength+" HEAD");
166156
// but minimum length for --short is 4, our abbrevLength could be 2
167157
String commitId = getCommitId();
168158
String abbrevCommitId = "";
169159

170-
if(commitId != null && !commitId.isEmpty()){
160+
if (commitId != null && !commitId.isEmpty()) {
171161
abbrevCommitId = commitId.substring(0, abbrevLength);
172162
}
173163

174164
return abbrevCommitId;
175165
}
176166

177167
@Override
178-
protected String getCommitAuthorName(){
168+
protected String getCommitAuthorName() {
179169
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%cn\"");
180170
}
181171

182172
@Override
183-
protected String getCommitAuthorEmail(){
173+
protected String getCommitAuthorEmail() {
184174
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%ce\"");
185175
}
186176

187177
@Override
188-
protected String getCommitMessageFull(){
189-
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%B\"");
178+
protected String getCommitMessageFull() {
179+
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%B\"");
190180
}
191181

192182
@Override
193-
protected String getCommitMessageShort(){
183+
protected String getCommitMessageShort() {
194184
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%s\"");
195185
}
196186

197187
@Override
198-
protected String getCommitTime(){
188+
protected String getCommitTime() {
199189
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%ci\"");
200190
}
201191

202192
@Override
203-
protected String getRemoteOriginUrl() throws MojoExecutionException{
193+
protected String getRemoteOriginUrl() throws MojoExecutionException {
204194
return getOriginRemote(canonical);
205195
}
206196

207197
@Override
208-
protected void finalCleanUp(){
198+
protected void finalCleanUp() {
209199
}
210200

211201
private String getOriginRemote(File directory) throws MojoExecutionException {
212202
String remoteUrl = null;
213-
try{
203+
try {
214204
String remotes = runGitCommand(directory, "remote -v");
215-
for (String line : remotes.split("\n")) {
205+
206+
// welcome to text output parsing hell! - no `\n` is not enough
207+
for (String line : Splitter.onPattern("\\((fetch|push)\\)?").split(remotes)) {
216208
String trimmed = line.trim();
209+
217210
if (trimmed.startsWith("origin")) {
218211
String[] splited = trimmed.split("\\s+");
219-
if (splited.length != REMOTE_COLS) {
220-
throw new MojoExecutionException("Unsupported GIT output - verbose remote address:" + line);
212+
if (splited.length != REMOTE_COLS - 1) { // because (fetch/push) was trimmed
213+
throw new MojoExecutionException("Unsupported GIT output (verbose remote address): " + line);
221214
}
222-
remoteUrl = splited[1];
215+
remoteUrl = splited[1];
223216
}
224217
}
225-
}catch(Exception e){
226-
throw new MojoExecutionException("Error ", e);
218+
} catch (Exception e) {
219+
throw new MojoExecutionException("Error while obtaining origin remote", e);
227220
}
228221
return remoteUrl;
229222
}
@@ -246,7 +239,7 @@ private String runGitCommand(File directory, String gitCommand) throws MojoExecu
246239

247240
String result = getRunner().run(directory, command).trim();
248241
return result;
249-
}catch(IOException ex) {
242+
} catch (IOException ex) {
250243
throw new MojoExecutionException("Could not run GIT command - GIT is not installed or not exists in system path? " + "Tried to run: 'git " + gitCommand + "'", ex);
251244
}
252245
}
@@ -269,7 +262,7 @@ protected static class Runner implements CliRunner {
269262
@Override
270263
public String run(File directory, String command) throws IOException {
271264
String output = "";
272-
try{
265+
try {
273266
ProcessBuilder builder = new ProcessBuilder(command.split("\\s"));
274267
final Process proc = builder.directory(directory).start();
275268
proc.waitFor();
@@ -282,7 +275,7 @@ public String run(File directory, String command) throws IOException {
282275
}
283276

284277
if (proc.exitValue() != 0) {
285-
String message = String.format("Git command exited with invalid status [%d]: `%s`", proc.exitValue(),output);
278+
String message = String.format("Git command exited with invalid status [%d]: `%s`", proc.exitValue(), output);
286279
throw new IOException(message);
287280
}
288281
output = commandResult.toString();
@@ -291,5 +284,5 @@ public String run(File directory, String command) throws IOException {
291284
}
292285
return output;
293286
}
294-
}
287+
}
295288
}

src/main/java/pl/project13/maven/git/log/MavenLoggerBridge.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ public MavenLoggerBridge(Log logger, boolean verbose) {
3333
@Override
3434
public void log(Object... parts) {
3535
if (verbose) {
36-
logger.info(Joiner.on(" ").join(parts));
36+
logger.info(Joiner.on(" ").useForNull("null").join(parts));
3737
}
3838
}
3939

4040
@Override
4141
public void error(Object... parts) {
4242
if (verbose) {
43-
logger.error(Joiner.on(" ").join(parts));
43+
logger.error(Joiner.on(" ").useForNull("null").join(parts));
4444
}
4545
}
4646

4747
@Override
4848
public void debug(Object... parts) {
4949
if (verbose) {
50-
logger.debug(Joiner.on(" ").join(parts));
50+
logger.debug(Joiner.on(" ").useForNull("null").join(parts));
5151
}
5252
}
5353

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public ContainsKeyCondition(String key) {
3434
public boolean matches(@NotNull Map<?, ?> map) {
3535
boolean containsKey = map.containsKey(key);
3636
if (!containsKey) {
37-
throw new RuntimeException(String.format("Map did not contain [%s] key! Map is: %s", key, map));
37+
throw new RuntimeException(String.format("Map did not contain [%s] key! Map is: %s\nValue for [%s] was: %s", key, map, key, map.get(key)));
3838
}
3939
return true;
4040
}

0 commit comments

Comments
 (0)