Skip to content

Commit

Permalink
bridging logging to address Konrad and Michael comments regarding mod…
Browse files Browse the repository at this point in the history
…ularization (git-commit-id#92)
  • Loading branch information
autayeu committed Jan 3, 2016
1 parent a8ca586 commit 5f76cde
Show file tree
Hide file tree
Showing 19 changed files with 1,417 additions and 323 deletions.
49 changes: 22 additions & 27 deletions src/main/java/pl/project13/jgit/DescribeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

import org.apache.maven.plugin.Mojo;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.Status;
Expand All @@ -36,6 +35,7 @@

import pl.project13.jgit.dummy.DatedRevTag;
import pl.project13.maven.git.GitDescribeConfig;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.util.Pair;

import java.io.IOException;
Expand All @@ -46,7 +46,7 @@
*/
public class DescribeCommand extends GitCommand<DescribeResult> {

private Mojo mojo;
private LoggerBridge log;
private JGitCommon jGitCommon;

// TODO not yet implemented options:
Expand Down Expand Up @@ -84,27 +84,23 @@ public class DescribeCommand extends GitCommand<DescribeResult> {
/**
* Creates a new describe command which interacts with a single repository
*
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
* @param repo the {@link Repository} this command should interact with
* @param log logger bridge to direct logs to
*/
@NotNull
public static DescribeCommand on(Repository repo) {
return new DescribeCommand(repo);
public static DescribeCommand on(Repository repo, LoggerBridge log) {
return new DescribeCommand(repo, log);
}

/**
* Creates a new describe command which interacts with a single repository
*
* @param repo the {@link org.eclipse.jgit.lib.Repository} this command should interact with
*/
private DescribeCommand(Repository repo) {
private DescribeCommand(Repository repo, @NotNull LoggerBridge log) {
super(repo);
this.jGitCommon = new JGitCommon();
}

@NotNull
public DescribeCommand withMojo(Mojo mojo) {
this.mojo = mojo;
return this;
this.jGitCommon = new JGitCommon(log);
this.log = log;
}

/**
Expand All @@ -117,7 +113,7 @@ public DescribeCommand withMojo(Mojo mojo) {
@NotNull
public DescribeCommand always(boolean always) {
this.alwaysFlag = always;
mojo.getLog().info("--always = " + always);
log.info("--always = {}", always);
return this;
}

Expand All @@ -136,7 +132,7 @@ public DescribeCommand always(boolean always) {
public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
if (forceLongFormat != null && forceLongFormat) {
this.forceLongFormat = true;
mojo.getLog().info("--long = " + true);
log.info("--long = {}", true);
}
return this;
}
Expand All @@ -152,9 +148,9 @@ public DescribeCommand forceLongFormat(@Nullable Boolean forceLongFormat) {
@NotNull
public DescribeCommand abbrev(@Nullable Integer n) {
if (n != null) {
Preconditions.checkArgument(n < 41, String.format("N (commit abbres length) must be < 41. (Was:[%s])", n));
Preconditions.checkArgument(n < 41, String.format("N (commit abbrev length) must be < 41. (Was:[%s])", n));
Preconditions.checkArgument(n >= 0, String.format("N (commit abbrev length) must be positive! (Was [%s])", n));
mojo.getLog().info("--abbrev = " + n);
log.info("--abbrev = {}", n);
abbrev = n;
}
return this;
Expand Down Expand Up @@ -192,7 +188,7 @@ public DescribeCommand abbrev(@Nullable Integer n) {
public DescribeCommand tags(@Nullable Boolean includeLightweightTagsInSearch) {
if (includeLightweightTagsInSearch != null && includeLightweightTagsInSearch) {
tagsFlag = includeLightweightTagsInSearch;
mojo.getLog().info("--tags = " + includeLightweightTagsInSearch);
log.info("--tags = {}", includeLightweightTagsInSearch);
}
return this;
}
Expand Down Expand Up @@ -234,7 +230,7 @@ public DescribeCommand apply(@Nullable GitDescribeConfig config) {
@NotNull
public DescribeCommand dirty(@Nullable String dirtyMarker) {
Optional<String> option = Optional.fromNullable(dirtyMarker);
mojo.getLog().info("--dirty = " + option.or(""));
log.info("--dirty = {}", option.or(""));
this.dirtyOption = option;
return this;
}
Expand All @@ -250,7 +246,7 @@ public DescribeCommand dirty(@Nullable String dirtyMarker) {
public DescribeCommand match(@Nullable String pattern) {
if (!"*".equals(pattern)) {
matchOption = Optional.fromNullable(pattern);
mojo.getLog().info("--match =" + matchOption.or(""));
log.info("--match = {}", matchOption.or(""));
}
return this;
}
Expand All @@ -272,7 +268,7 @@ public DescribeResult call() throws GitAPIException {

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

return new DescribeResult(tagName, dirty, dirtyOption);
}
Expand Down Expand Up @@ -345,7 +341,7 @@ boolean findDirtyState(Repository repo) throws GitAPIException {
&& status.getModified().isEmpty()
&& status.getConflicting().isEmpty());

mojo.getLog().info("Repo is in dirty state [" + isDirty + "]");
log.info("Repo is in dirty state [{}]", isDirty);
return isDirty;
}

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

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

return commitIdsToTagNames;
}
Expand All @@ -388,5 +384,4 @@ private String createMatchPattern() {
matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
"\\E$";
}
}

}
38 changes: 22 additions & 16 deletions src/main/java/pl/project13/jgit/JGitCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.*;
import java.util.regex.Pattern;

import org.apache.maven.plugin.Mojo;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
Expand All @@ -43,8 +42,16 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import pl.project13.maven.git.log.LoggerBridge;

public class JGitCommon {

private final LoggerBridge log;

public JGitCommon(LoggerBridge log) {
this.log = log;
}

public Collection<String> getTags(Repository repo, final ObjectId headId) throws GitAPIException{
RevWalk walk = null;
try {
Expand Down Expand Up @@ -80,16 +87,16 @@ public Collection<String> getTags(Repository repo, final ObjectId headId) throws
}
}

public String getClosestTagName(@NotNull Repository repo, @NotNull Mojo mojo){
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo, mojo);
public String getClosestTagName(@NotNull Repository repo){
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo);
for(Map.Entry<ObjectId, List<DatedRevTag>> entry : map.entrySet()){
return trimFullTagName(entry.getValue().get(0).tagName);
}
return "";
}

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

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

private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo, @NotNull Mojo mojo){
private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo){
Map<ObjectId, List<DatedRevTag>> mapWithClosestTagOnly = newHashMap();
boolean includeLightweightTags = true;
String matchPattern = ".*";
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern, mojo);
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, true, matchPattern);
LinkedHashMap<ObjectId, List<DatedRevTag>> sortedCommitIdsToTags = sortByDatedRevTag(commitIdsToTags);

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

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

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

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

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

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

DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);

Expand All @@ -188,16 +194,16 @@ protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repositor
}
}
} catch (Exception ignored) {
mojo.getLog().info("Failed while parsing [" + tagRef + "] -- ", ignored);
log.info("Failed while parsing [{}] -- ", tagRef, ignored);
}
}

for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
mojo.getLog().info("key [" + entry.getKey() + "], tags => [" + entry.getValue() + "] ");
log.info("key [{}], tags => [{}] ", entry.getKey(), entry.getValue());
}
return commitIdsToTags;
} catch (Exception e) {
mojo.getLog().info("Unable to locate tags", e);
log.info("Unable to locate tags", e);
}
return Collections.emptyMap();
}
Expand Down
Loading

0 comments on commit 5f76cde

Please sign in to comment.