DiffStat stat = null;
String diff = null;
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
RawTextComparator cmp = RawTextComparator.DEFAULT;
DiffFormatter df;
switch (outputType) {
case HTML:
df = new GitBlitDiffFormatter(os, commit.getName());
break;
case PLAIN:
default:
df = new DiffFormatter(os);
break;
}
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
rw.dispose();
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
if (df instanceof GitBlitDiffFormatter) {
// workaround for complex private methods in DiffFormatter
diff = ((GitBlitDiffFormatter) df).getHtml();
stat = ((GitBlitDiffFormatter) df).getDiffStat();
} else {
diff = os.toString();
}
df.flush();
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return new DiffOutput(outputType, diff, stat);