Skip to content

Commit 617e466

Browse files
committed
first bite of logging refactoring: now logs are in line with standard Maven logging. log message string construction to address.
1 parent c9dafa8 commit 617e466

21 files changed

+331
-481
lines changed

pom.xml

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.sonatype.oss</groupId>
66
<artifactId>oss-parent</artifactId>
7-
<version>6</version>
7+
<version>9</version>
88
</parent>
99

1010
<groupId>pl.project13.maven</groupId>
@@ -72,7 +72,6 @@
7272
<maven-plugin-api.version>3.1.1</maven-plugin-api.version>
7373

7474
<jgit.version>4.0.0.201506090130-r</jgit.version>
75-
<slf4j.version>1.7.12</slf4j.version>
7675
<junit.version>4.12</junit.version>
7776
<mockito.version>2.0.5-beta</mockito.version>
7877

@@ -129,18 +128,6 @@
129128
<version>${jgit.version}</version>
130129
</dependency>
131130

132-
<dependency>
133-
<groupId>org.slf4j</groupId>
134-
<artifactId>slf4j-api</artifactId>
135-
<version>${slf4j.version}</version>
136-
</dependency>
137-
138-
<dependency>
139-
<groupId>org.slf4j</groupId>
140-
<artifactId>slf4j-simple</artifactId>
141-
<version>${slf4j.version}</version>
142-
</dependency>
143-
144131
<!-- Test stuff -->
145132
<dependency>
146133
<groupId>junit</groupId>
@@ -184,6 +171,14 @@
184171
<version>1.0.4</version>
185172
<scope>test</scope>
186173
</dependency>
174+
175+
<!--to avoid complaints during tests-->
176+
<dependency>
177+
<groupId>org.slf4j</groupId>
178+
<artifactId>slf4j-simple</artifactId>
179+
<version>1.7.13</version>
180+
<scope>test</scope>
181+
</dependency>
187182
</dependencies>
188183

189184
<build>
@@ -212,33 +207,6 @@
212207
<target>${java.target}</target>
213208
</configuration>
214209
</plugin>
215-
<!-- we need to create a uber-jar for https://github.com/ktoso/maven-git-commit-id-plugin/issues/196 -->
216-
<plugin>
217-
<groupId>org.apache.maven.plugins</groupId>
218-
<artifactId>maven-shade-plugin</artifactId>
219-
<version>2.3</version>
220-
<executions>
221-
<execution>
222-
<phase>package</phase>
223-
<goals>
224-
<goal>shade</goal>
225-
</goals>
226-
<configuration>
227-
<createDependencyReducedPom>false</createDependencyReducedPom>
228-
<transformers>
229-
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
230-
<mainClass>pl.project13.maven.git.GitCommitIdMojo</mainClass>
231-
</transformer>
232-
</transformers>
233-
<artifactSet>
234-
<includes>
235-
<include>org.slf4j:*</include>
236-
</includes>
237-
</artifactSet>
238-
</configuration>
239-
</execution>
240-
</executions>
241-
</plugin>
242210

243211
<!--<plugin>-->
244212
<!--<artifactId>maven-plugin-plugin</artifactId>-->
@@ -339,5 +307,4 @@
339307
</build>
340308
</profile>
341309
</profiles>
342-
</project>
343-
310+
</project>

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.common.base.Optional;
2222
import com.google.common.base.Preconditions;
2323

24+
import org.apache.maven.plugin.Mojo;
2425
import org.eclipse.jgit.api.Git;
2526
import org.eclipse.jgit.api.GitCommand;
2627
import org.eclipse.jgit.api.Status;
@@ -35,8 +36,6 @@
3536

3637
import pl.project13.jgit.dummy.DatedRevTag;
3738
import pl.project13.maven.git.GitDescribeConfig;
38-
import pl.project13.maven.git.log.LoggerBridge;
39-
import pl.project13.maven.git.log.StdOutLoggerBridge;
4039
import pl.project13.maven.git.util.Pair;
4140

4241
import java.io.IOException;
@@ -47,7 +46,7 @@
4746
*/
4847
public class DescribeCommand extends GitCommand<DescribeResult> {
4948

50-
private LoggerBridge loggerBridge;
49+
private Mojo mojo;
5150
private JGitCommon jGitCommon;
5251

5352
// TODO not yet implemented options:
@@ -98,29 +97,13 @@ public static DescribeCommand on(Repository repo) {
9897
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
9998
*/
10099
private DescribeCommand(Repository repo) {
101-
this(repo, true);
102-
}
103-
104-
private DescribeCommand(Repository repo, boolean verbose) {
105100
super(repo);
106-
initDefaultLoggerBridge(verbose);
107-
setVerbose(verbose);
108101
this.jGitCommon = new JGitCommon();
109102
}
110103

111-
private void initDefaultLoggerBridge(boolean verbose) {
112-
loggerBridge = new StdOutLoggerBridge(verbose);
113-
}
114-
115-
@NotNull
116-
public DescribeCommand setVerbose(boolean verbose) {
117-
loggerBridge.setVerbose(verbose);
118-
return this;
119-
}
120-
121104
@NotNull
122-
public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
123-
this.loggerBridge = bridge;
105+
public DescribeCommand withMojo(Mojo mojo) {
106+
this.mojo = mojo;
124107
return this;
125108
}
126109

@@ -134,7 +117,7 @@ public DescribeCommand withLoggerBridge(LoggerBridge bridge) {
134117
@NotNull
135118
public DescribeCommand always(boolean always) {
136119
this.alwaysFlag = always;
137-
log("--always =", always);
120+
mojo.getLog().info("--always = " + always);
138121
return this;
139122
}
140123

@@ -153,7 +136,7 @@ public DescribeCommand always(boolean always) {
153136
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
154137
if (forceLongFormat != null && forceLongFormat) {
155138
this.forceLongFormat = true;
156-
log("--long =", true);
139+
mojo.getLog().info("--long = " + true);
157140
}
158141
return this;
159142
}
@@ -171,7 +154,7 @@ public DescribeCommand abbrev(@Nullable Integer n) {
171154
if (n != null) {
172155
Preconditions.checkArgument(n < 41, String.format("N (commit abbres length) must be < 41. (Was:[%s])", n));
173156
Preconditions.checkArgument(n >= 0, String.format("N (commit abbrev length) must be positive! (Was [%s])", n));
174-
log("--abbrev =", n);
157+
mojo.getLog().info("--abbrev = " + n);
175158
abbrev = n;
176159
}
177160
return this;
@@ -209,7 +192,7 @@ public DescribeCommand abbrev(@Nullable Integer n) {
209192
public DescribeCommand tags(@Nullable Boolean includeLightweightTagsInSearch) {
210193
if (includeLightweightTagsInSearch != null && includeLightweightTagsInSearch) {
211194
tagsFlag = includeLightweightTagsInSearch;
212-
log("--tags =", includeLightweightTagsInSearch);
195+
mojo.getLog().info("--tags = " + includeLightweightTagsInSearch);
213196
}
214197
return this;
215198
}
@@ -251,7 +234,7 @@ public DescribeCommand apply(@Nullable GitDescribeConfig config) {
251234
@NotNull
252235
public DescribeCommand dirty(@Nullable String dirtyMarker) {
253236
Optional<String> option = Optional.fromNullable(dirtyMarker);
254-
log("--dirty =", option.or(""));
237+
mojo.getLog().info("--dirty = " + option.or(""));
255238
this.dirtyOption = option;
256239
return this;
257240
}
@@ -267,7 +250,7 @@ public DescribeCommand dirty(@Nullable String dirtyMarker) {
267250
public DescribeCommand match(@Nullable String pattern) {
268251
if (!"*".equals(pattern)) {
269252
matchOption = Optional.fromNullable(pattern);
270-
log("--match =", matchOption.or(""));
253+
mojo.getLog().info("--match =" + matchOption.or(""));
271254
}
272255
return this;
273256
}
@@ -289,7 +272,7 @@ public DescribeResult call() throws GitAPIException {
289272

290273
if (hasTags(headCommit, tagObjectIdToName) && !forceLongFormat) {
291274
String tagName = tagObjectIdToName.get(headCommit).iterator().next();
292-
log("The commit we're on is a Tag ([",tagName,"]) and forceLongFormat == false, returning.");
275+
mojo.getLog().info("The commit we're on is a Tag ([" + tagName + "]) and forceLongFormat == false, returning.");
293276

294277
return new DescribeResult(tagName, dirty, dirtyOption);
295278
}
@@ -362,7 +345,7 @@ boolean findDirtyState(Repository repo) throws GitAPIException {
362345
&& status.getModified().isEmpty()
363346
&& status.getConflicting().isEmpty());
364347

365-
log("Repo is in dirty state [", isDirty, "]");
348+
mojo.getLog().info("Repo is in dirty state [" + isDirty + "]");
366349
return isDirty;
367350
}
368351

@@ -379,7 +362,7 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
379362
RevCommit headCommit = walk.lookupCommit(headId);
380363
walk.dispose();
381364

382-
log("HEAD is [",headCommit.getName(),"] ");
365+
mojo.getLog().info("HEAD is [" + headCommit.getName() + "] ");
383366
return headCommit;
384367
} catch (IOException ex) {
385368
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
@@ -389,9 +372,9 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
389372
// git commit id -> its tag (or tags)
390373
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
391374
String matchPattern = createMatchPattern();
392-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(loggerBridge, repo, tagsFlag, matchPattern);
375+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(repo, tagsFlag, matchPattern, mojo);
393376
Map<ObjectId, List<String>> commitIdsToTagNames = jGitCommon.transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
394-
log("Created map: [",commitIdsToTagNames,"] ");
377+
mojo.getLog().info("Created map: [" + commitIdsToTagNames + "] ");
395378

396379
return commitIdsToTagNames;
397380
}
@@ -407,9 +390,5 @@ private String createMatchPattern() {
407390
buf.append("\\E$");
408391
return buf.toString();
409392
}
410-
411-
private void log(Object... parts) {
412-
loggerBridge.log(parts);
413-
}
414393
}
415394

src/main/java/pl/project13/jgit/JGitCommon.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Set;
3434
import java.util.regex.Pattern;
3535

36+
import org.apache.maven.plugin.Mojo;
3637
import org.eclipse.jgit.api.Git;
3738
import org.eclipse.jgit.api.errors.GitAPIException;
3839
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
@@ -45,18 +46,14 @@
4546
import org.jetbrains.annotations.NotNull;
4647

4748
import pl.project13.jgit.dummy.DatedRevTag;
48-
import pl.project13.maven.git.log.LoggerBridge;
4949

5050
import com.google.common.annotations.VisibleForTesting;
5151
import com.google.common.base.Function;
5252
import com.google.common.base.Predicate;
53-
import com.google.common.base.Throwables;
5453
import com.google.common.collect.Collections2;
5554
import com.google.common.collect.Lists;
5655

57-
import static com.google.common.collect.Lists.newArrayList;
5856
import static com.google.common.collect.Lists.newLinkedList;
59-
import static com.google.common.collect.Maps.newHashMap;
6057
import static com.google.common.collect.Sets.newHashSet;
6158

6259
public class JGitCommon {
@@ -97,16 +94,16 @@ public Collection<String> getTags(Repository repo, final ObjectId headId) throws
9794
}
9895
}
9996

100-
public String getClosestTagName(@NotNull LoggerBridge loggerBridge,@NotNull Repository repo){
101-
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(loggerBridge,repo);
97+
public String getClosestTagName(@NotNull Repository repo, @NotNull Mojo mojo){
98+
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo, mojo);
10299
for(Map.Entry<ObjectId, List<DatedRevTag>> entry : map.entrySet()){
103100
return trimFullTagName(entry.getValue().get(0).tagName);
104101
}
105102
return "";
106103
}
107104

108-
public String getClosestTagCommitCount(@NotNull LoggerBridge loggerBridge,@NotNull Repository repo, RevCommit headCommit){
109-
HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(loggerBridge,repo));
105+
public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headCommit, @NotNull Mojo mojo){
106+
HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(repo, mojo));
110107
ObjectId obj = (ObjectId) map.keySet().toArray()[0];
111108

112109
RevWalk walk = new RevWalk(repo);
@@ -117,11 +114,11 @@ public String getClosestTagCommitCount(@NotNull LoggerBridge loggerBridge,@NotNu
117114
return String.valueOf(distance);
118115
}
119116

120-
private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull LoggerBridge loggerBridge,@NotNull Repository repo){
117+
private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo, @NotNull Mojo mojo){
121118
Map<ObjectId, List<DatedRevTag>> mapWithClosestTagOnly = newHashMap();
122119
boolean includeLightweightTags = true;
123120
String matchPattern = ".*";
124-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(loggerBridge,repo,includeLightweightTags,matchPattern);
121+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern, mojo);
125122
LinkedHashMap<ObjectId, List<DatedRevTag>> sortedCommitIdsToTags = sortByDatedRevTag(commitIdsToTags);
126123

127124
for(Map.Entry<ObjectId, List<DatedRevTag>> entry: sortedCommitIdsToTags.entrySet()){
@@ -154,7 +151,7 @@ public int compare(Map.Entry<ObjectId, List<DatedRevTag>> m1, Map.Entry<ObjectId
154151
return result;
155152
}
156153

157-
protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBridge loggerBridge,@NotNull Repository repo, boolean includeLightweightTags, String matchPattern){
154+
protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern, @NotNull Mojo mojo){
158155
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = newHashMap();
159156

160157
RevWalk walk = new RevWalk(repo);
@@ -163,13 +160,13 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBri
163160

164161
List<Ref> tagRefs = Git.wrap(repo).tagList().call();
165162
Pattern regex = Pattern.compile(matchPattern);
166-
loggerBridge.log("Tag refs [", tagRefs, "]");
163+
mojo.getLog().info("Tag refs [" + tagRefs + "]");
167164

168165
for (Ref tagRef : tagRefs) {
169166
walk.reset();
170167
String name = tagRef.getName();
171168
if (!regex.matcher(name).matches()) {
172-
loggerBridge.log("Skipping tagRef with name [", name, "] as it doesn't match [", matchPattern, "]");
169+
mojo.getLog().info("Skipping tagRef with name [" + name + "] as it doesn't match [" + matchPattern + "]");
173170
continue;
174171
}
175172
ObjectId resolvedCommitId = repo.resolve(name);
@@ -178,7 +175,7 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBri
178175
try {
179176
final RevTag revTag = walk.parseTag(resolvedCommitId);
180177
ObjectId taggedCommitId = revTag.getObject().getId();
181-
loggerBridge.log("Resolved tag [",revTag.getTagName(),"] [",revTag.getTaggerIdent(),"], points at [",taggedCommitId,"] ");
178+
mojo.getLog().info("Resolved tag [" + revTag.getTagName() + "] [" + revTag.getTaggerIdent() + "], points at [" + taggedCommitId + "] ");
182179

183180
// sometimes a tag, may point to another tag, so we need to unpack it
184181
while (isTagId(taggedCommitId)) {
@@ -195,7 +192,7 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBri
195192
// it's an lightweight tag! (yeah, really)
196193
if (includeLightweightTags) {
197194
// --tags means "include lightweight tags"
198-
loggerBridge.log("Including lightweight tag [", name, "]");
195+
mojo.getLog().info("Including lightweight tag [" + name + "]");
199196

200197
DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);
201198

@@ -206,16 +203,16 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBri
206203
}
207204
}
208205
} catch (Exception ignored) {
209-
loggerBridge.error("Failed while parsing [",tagRef,"] -- ", Throwables.getStackTraceAsString(ignored));
206+
mojo.getLog().info("Failed while parsing [" + tagRef + "] -- ", ignored);
210207
}
211208
}
212209

213210
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
214-
loggerBridge.log("key [",entry.getKey(),"], tags => [",entry.getValue(),"] ");
211+
mojo.getLog().info("key [" + entry.getKey() + "], tags => [" + entry.getValue() + "] ");
215212
}
216213
return commitIdsToTags;
217214
} catch (Exception e) {
218-
loggerBridge.log("Unable to locate tags\n[",Throwables.getStackTraceAsString(e),"]");
215+
mojo.getLog().info("Unable to locate tags", e);
219216
} finally {
220217
walk.close();
221218
}

0 commit comments

Comments
 (0)