Skip to content

Commit 5f76cde

Browse files
committed
bridging logging to address Konrad and Michael comments regarding modularization (#92)
1 parent a8ca586 commit 5f76cde

19 files changed

+1417
-323
lines changed

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

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

24-
import org.apache.maven.plugin.Mojo;
2524
import org.eclipse.jgit.api.Git;
2625
import org.eclipse.jgit.api.GitCommand;
2726
import org.eclipse.jgit.api.Status;
@@ -36,6 +35,7 @@
3635

3736
import pl.project13.jgit.dummy.DatedRevTag;
3837
import pl.project13.maven.git.GitDescribeConfig;
38+
import pl.project13.maven.git.log.LoggerBridge;
3939
import pl.project13.maven.git.util.Pair;
4040

4141
import java.io.IOException;
@@ -46,7 +46,7 @@
4646
*/
4747
public class DescribeCommand extends GitCommand<DescribeResult> {
4848

49-
private Mojo mojo;
49+
private LoggerBridge log;
5050
private JGitCommon jGitCommon;
5151

5252
// TODO not yet implemented options:
@@ -84,27 +84,23 @@ public class DescribeCommand extends GitCommand<DescribeResult> {
8484
/**
8585
* Creates a new describe command which interacts with a single repository
8686
*
87-
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
87+
* @param repo the {@link Repository} this command should interact with
88+
* @param log logger bridge to direct logs to
8889
*/
8990
@NotNull
90-
public static DescribeCommand on(Repository repo) {
91-
return new DescribeCommand(repo);
91+
public static DescribeCommand on(Repository repo, LoggerBridge log) {
92+
return new DescribeCommand(repo, log);
9293
}
9394

9495
/**
9596
* Creates a new describe command which interacts with a single repository
9697
*
9798
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
9899
*/
99-
private DescribeCommand(Repository repo) {
100+
private DescribeCommand(Repository repo, @NotNull LoggerBridge log) {
100101
super(repo);
101-
this.jGitCommon = new JGitCommon();
102-
}
103-
104-
@NotNull
105-
public DescribeCommand withMojo(Mojo mojo) {
106-
this.mojo = mojo;
107-
return this;
102+
this.jGitCommon = new JGitCommon(log);
103+
this.log = log;
108104
}
109105

110106
/**
@@ -117,7 +113,7 @@ public DescribeCommand withMojo(Mojo mojo) {
117113
@NotNull
118114
public DescribeCommand always(boolean always) {
119115
this.alwaysFlag = always;
120-
mojo.getLog().info("--always = " + always);
116+
log.info("--always = {}", always);
121117
return this;
122118
}
123119

@@ -136,7 +132,7 @@ public DescribeCommand always(boolean always) {
136132
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
137133
if (forceLongFormat != null && forceLongFormat) {
138134
this.forceLongFormat = true;
139-
mojo.getLog().info("--long = " + true);
135+
log.info("--long = {}", true);
140136
}
141137
return this;
142138
}
@@ -152,9 +148,9 @@ public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
152148
@NotNull
153149
public DescribeCommand abbrev(@Nullable Integer n) {
154150
if (n != null) {
155-
Preconditions.checkArgument(n < 41, String.format("N (commit abbres length) must be < 41. (Was:[%s])", n));
151+
Preconditions.checkArgument(n < 41, String.format("N (commit abbrev length) must be < 41. (Was:[%s])", n));
156152
Preconditions.checkArgument(n >= 0, String.format("N (commit abbrev length) must be positive! (Was [%s])", n));
157-
mojo.getLog().info("--abbrev = " + n);
153+
log.info("--abbrev = {}", n);
158154
abbrev = n;
159155
}
160156
return this;
@@ -192,7 +188,7 @@ public DescribeCommand abbrev(@Nullable Integer n) {
192188
public DescribeCommand tags(@Nullable Boolean includeLightweightTagsInSearch) {
193189
if (includeLightweightTagsInSearch != null && includeLightweightTagsInSearch) {
194190
tagsFlag = includeLightweightTagsInSearch;
195-
mojo.getLog().info("--tags = " + includeLightweightTagsInSearch);
191+
log.info("--tags = {}", includeLightweightTagsInSearch);
196192
}
197193
return this;
198194
}
@@ -234,7 +230,7 @@ public DescribeCommand apply(@Nullable GitDescribeConfig config) {
234230
@NotNull
235231
public DescribeCommand dirty(@Nullable String dirtyMarker) {
236232
Optional<String> option = Optional.fromNullable(dirtyMarker);
237-
mojo.getLog().info("--dirty = " + option.or(""));
233+
log.info("--dirty = {}", option.or(""));
238234
this.dirtyOption = option;
239235
return this;
240236
}
@@ -250,7 +246,7 @@ public DescribeCommand dirty(@Nullable String dirtyMarker) {
250246
public DescribeCommand match(@Nullable String pattern) {
251247
if (!"*".equals(pattern)) {
252248
matchOption = Optional.fromNullable(pattern);
253-
mojo.getLog().info("--match =" + matchOption.or(""));
249+
log.info("--match = {}", matchOption.or(""));
254250
}
255251
return this;
256252
}
@@ -272,7 +268,7 @@ public DescribeResult call() throws GitAPIException {
272268

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

277273
return new DescribeResult(tagName, dirty, dirtyOption);
278274
}
@@ -345,7 +341,7 @@ boolean findDirtyState(Repository repo) throws GitAPIException {
345341
&& status.getModified().isEmpty()
346342
&& status.getConflicting().isEmpty());
347343

348-
mojo.getLog().info("Repo is in dirty state [" + isDirty + "]");
344+
log.info("Repo is in dirty state [{}]", isDirty);
349345
return isDirty;
350346
}
351347

@@ -362,7 +358,7 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
362358
RevCommit headCommit = walk.lookupCommit(headId);
363359
walk.dispose();
364360

365-
mojo.getLog().info("HEAD is [" + headCommit.getName() + "] ");
361+
log.info("HEAD is [{}]", headCommit.getName());
366362
return headCommit;
367363
} catch (IOException ex) {
368364
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
@@ -372,9 +368,9 @@ RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
372368
// git commit id -> its tag (or tags)
373369
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
374370
String matchPattern = createMatchPattern();
375-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(repo, tagsFlag, matchPattern, mojo);
371+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(repo, tagsFlag, matchPattern);
376372
Map<ObjectId, List<String>> commitIdsToTagNames = jGitCommon.transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
377-
mojo.getLog().info("Created map: [" + commitIdsToTagNames + "] ");
373+
log.info("Created map: [{}]", commitIdsToTagNames);
378374

379375
return commitIdsToTagNames;
380376
}
@@ -388,5 +384,4 @@ private String createMatchPattern() {
388384
matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
389385
"\\E$";
390386
}
391-
}
392-
387+
}

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.*;
2525
import java.util.regex.Pattern;
2626

27-
import org.apache.maven.plugin.Mojo;
2827
import org.eclipse.jgit.api.Git;
2928
import org.eclipse.jgit.api.errors.GitAPIException;
3029
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
@@ -43,8 +42,16 @@
4342
import com.google.common.base.Predicate;
4443
import com.google.common.collect.Collections2;
4544
import com.google.common.collect.Lists;
45+
import pl.project13.maven.git.log.LoggerBridge;
4646

4747
public class JGitCommon {
48+
49+
private final LoggerBridge log;
50+
51+
public JGitCommon(LoggerBridge log) {
52+
this.log = log;
53+
}
54+
4855
public Collection<String> getTags(Repository repo, final ObjectId headId) throws GitAPIException{
4956
RevWalk walk = null;
5057
try {
@@ -80,16 +87,16 @@ public Collection<String> getTags(Repository repo, final ObjectId headId) throws
8087
}
8188
}
8289

83-
public String getClosestTagName(@NotNull Repository repo, @NotNull Mojo mojo){
84-
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo, mojo);
90+
public String getClosestTagName(@NotNull Repository repo){
91+
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo);
8592
for(Map.Entry<ObjectId, List<DatedRevTag>> entry : map.entrySet()){
8693
return trimFullTagName(entry.getValue().get(0).tagName);
8794
}
8895
return "";
8996
}
9097

91-
public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headCommit, @NotNull Mojo mojo){
92-
HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(repo, mojo));
98+
public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headCommit){
99+
HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(repo));
93100
ObjectId obj = (ObjectId) map.keySet().toArray()[0];
94101

95102
RevWalk walk = new RevWalk(repo);
@@ -100,11 +107,10 @@ public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headC
100107
return String.valueOf(distance);
101108
}
102109

103-
private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo, @NotNull Mojo mojo){
110+
private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo){
104111
Map<ObjectId, List<DatedRevTag>> mapWithClosestTagOnly = newHashMap();
105-
boolean includeLightweightTags = true;
106112
String matchPattern = ".*";
107-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern, mojo);
113+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, true, matchPattern);
108114
LinkedHashMap<ObjectId, List<DatedRevTag>> sortedCommitIdsToTags = sortByDatedRevTag(commitIdsToTags);
109115

110116
for (Map.Entry<ObjectId, List<DatedRevTag>> entry: sortedCommitIdsToTags.entrySet()){
@@ -137,21 +143,21 @@ public int compare(Map.Entry<ObjectId, List<DatedRevTag>> m1, Map.Entry<ObjectId
137143
return result;
138144
}
139145

140-
protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern, @NotNull Mojo mojo){
146+
protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern){
141147
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = newHashMap();
142148

143149
try (RevWalk walk = new RevWalk(repo)) {
144150
walk.markStart(walk.parseCommit(repo.resolve("HEAD")));
145151

146152
List<Ref> tagRefs = Git.wrap(repo).tagList().call();
147153
Pattern regex = Pattern.compile(matchPattern);
148-
mojo.getLog().info("Tag refs [" + tagRefs + "]");
154+
log.info("Tag refs [{}]", tagRefs);
149155

150156
for (Ref tagRef : tagRefs) {
151157
walk.reset();
152158
String name = tagRef.getName();
153159
if (!regex.matcher(name).matches()) {
154-
mojo.getLog().info("Skipping tagRef with name [" + name + "] as it doesn't match [" + matchPattern + "]");
160+
log.info("Skipping tagRef with name [{}] as it doesn't match [{}]", name, matchPattern);
155161
continue;
156162
}
157163
ObjectId resolvedCommitId = repo.resolve(name);
@@ -160,7 +166,7 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repositor
160166
try {
161167
final RevTag revTag = walk.parseTag(resolvedCommitId);
162168
ObjectId taggedCommitId = revTag.getObject().getId();
163-
mojo.getLog().info("Resolved tag [" + revTag.getTagName() + "] [" + revTag.getTaggerIdent() + "], points at [" + taggedCommitId + "] ");
169+
log.info("Resolved tag [{}] [{}], points at [{}] ", revTag.getTagName(), revTag.getTaggerIdent(), taggedCommitId);
164170

165171
// sometimes a tag, may point to another tag, so we need to unpack it
166172
while (isTagId(taggedCommitId)) {
@@ -177,7 +183,7 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repositor
177183
// it's an lightweight tag! (yeah, really)
178184
if (includeLightweightTags) {
179185
// --tags means "include lightweight tags"
180-
mojo.getLog().info("Including lightweight tag [" + name + "]");
186+
log.info("Including lightweight tag [{}]", name);
181187

182188
DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);
183189

@@ -188,16 +194,16 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repositor
188194
}
189195
}
190196
} catch (Exception ignored) {
191-
mojo.getLog().info("Failed while parsing [" + tagRef + "] -- ", ignored);
197+
log.info("Failed while parsing [{}] -- ", tagRef, ignored);
192198
}
193199
}
194200

195201
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
196-
mojo.getLog().info("key [" + entry.getKey() + "], tags => [" + entry.getValue() + "] ");
202+
log.info("key [{}], tags => [{}] ", entry.getKey(), entry.getValue());
197203
}
198204
return commitIdsToTags;
199205
} catch (Exception e) {
200-
mojo.getLog().info("Unable to locate tags", e);
206+
log.info("Unable to locate tags", e);
201207
}
202208
return Collections.emptyMap();
203209
}

0 commit comments

Comments
 (0)