com.github.danielflower.mavenplugins.gitlog.Generator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of maven-gitlog-plugin Show documentation
Show all versions of maven-gitlog-plugin Show documentation
Generates a changelog based on commits to a git repository in text and HTML format showing the changes
that are included in each version. A possible use of this is to include these changelogs when packaging your
maven project so that you have an accurate list of commits that the current package includes.
package com.github.danielflower.mavenplugins.gitlog;
import com.github.danielflower.mavenplugins.gitlog.filters.CommitFilter;
import com.github.danielflower.mavenplugins.gitlog.renderers.ChangeLogRenderer;
import org.apache.maven.plugin.logging.Log;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Generator {
private final List renderers;
private RevWalk walk;
private Map> commitIDToTagsMap;
private final List commitFilters;
private final Log log;
public Generator(List renderers, List commitFilters, Log log) {
this.renderers = renderers;
this.commitFilters = (commitFilters == null) ? new ArrayList() : commitFilters;
this.log = log;
}
public void openRepository() throws IOException, NoGitRepositoryException {
log.debug("About to open git repository.");
Repository repository;
try {
repository = new RepositoryBuilder().findGitDir().build();
} catch (IllegalArgumentException iae) {
throw new NoGitRepositoryException();
}
log.debug("Opened " + repository + ". About to load the commits.");
walk = createWalk(repository);
log.debug("Loaded commits. about to load the tags.");
commitIDToTagsMap = createCommitIDToTagsMap(repository, walk);
log.debug("Loaded tag map: " + commitIDToTagsMap);
}
public void generate(String reportTitle) throws IOException {
generate(reportTitle, new Date(0l));
}
public void generate(String reportTitle, Date includeCommitsAfter) throws IOException {
for (ChangeLogRenderer renderer : renderers) {
renderer.renderHeader(reportTitle);
}
long dateInSecondsSinceEpoch = includeCommitsAfter.getTime() / 1000;
for (RevCommit commit : walk) {
int commitTimeInSecondsSinceEpoch = commit.getCommitTime();
if (dateInSecondsSinceEpoch < commitTimeInSecondsSinceEpoch) {
List revTags = commitIDToTagsMap.get(commit.name());
for (ChangeLogRenderer renderer : renderers) {
if (revTags != null) {
for (RevTag revTag : revTags) {
renderer.renderTag(revTag);
}
}
}
if (show(commit)) {
for (ChangeLogRenderer renderer : renderers) {
renderer.renderCommit(commit);
}
}
}
}
walk.dispose();
for (ChangeLogRenderer renderer : renderers) {
renderer.renderFooter();
renderer.close();
}
}
private boolean show(RevCommit commit) {
for (CommitFilter commitFilter : commitFilters) {
if (!commitFilter.renderCommit(commit)) {
log.debug("Commit filtered out by " + commitFilter.getClass().getSimpleName());
return false;
}
}
return true;
}
private static RevWalk createWalk(Repository repository) throws IOException {
RevWalk walk = new RevWalk(repository);
ObjectId head = repository.resolve("HEAD");
if (head != null) {
// if head is null, it means there are no commits in the repository. The walk will be empty.
RevCommit mostRecentCommit = walk.parseCommit(head);
walk.markStart(mostRecentCommit);
}
return walk;
}
private Map> createCommitIDToTagsMap(Repository repository, RevWalk revWalk) throws IOException {
Map allTags = repository.getTags();
Map> revTags = new HashMap>();
for (Ref ref : allTags.values()) {
try {
RevTag revTag = revWalk.parseTag(ref.getObjectId());
String commitID = revTag.getObject().getId().getName();
if (!revTags.containsKey(commitID)) {
revTags.put(commitID, new ArrayList());
}
revTags.get(commitID).add(revTag);
} catch (IncorrectObjectTypeException e) {
log.debug("Light-weight tags not supported. Skipping " + ref.getName());
}
}
return revTags;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy