我调用了 jgit log 命令并取回了一些 RevCommit 对象。我可以从中取回一些基本信息,并使用以下代码获取更改的文件列表。不过,我还需要两件事:
1)当提交没有父母时,我如何获得以下信息?
2)如何获取每个文件中更改的内容的差异
RevCommit commit = null;
RevWalk rw = new RevWalk(repository);
RevCommit parent = null;
if (commit.getParent(0) != null) {
parent = rw.parseCommit(commit.getParent(0).getId());
}
DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE);
df.setRepository(repository);
df.setDiffComparator(RawTextComparator.DEFAULT);
df.setDetectRenames(true);
List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree());
for (DiffEntry diff : diffs) {
System.out.println(getCommitMessage());
System.out.println("changeType=" + diff.getChangeType().name()
+ " newMode=" + diff.getNewMode().getBits()
+ " newPath=" + diff.getNewPath()
+ " id=" + getHash());
}