JGit - 提交到字符串的内容

JGit - Contents of Commit Into String

我需要存储提交的内容,这与例如不同。掌握最新的提交,在一个字符串中。

How to show changes between commits with JGit最下面的答案和我想要的很相似

这是来自 link

的代码
RevCommit headCommit = getHeadCommit(repository);
    RevCommit diffWith = headCommit.getParent(0);
    FileOutputStream stdout = new FileOutputStream(FileDescriptor.out);
    try (DiffFormatter diffFormatter = new DiffFormatter(stdout)) {
        diffFormatter.setRepository(repository);
        for (DiffEntry entry : diffFormatter.scan(diffWith, headCommit)) {
            diffFormatter.format(diffFormatter.toFileHeader(entry));
        }
    }
diffFormatter.format(diffFormatter.toFileHeader(entry));

format() 方法无效,似乎没有办法 return 字符串或某种输出流。

我希望会有类似

的内容
String commitDiff = diffFormatter.getCommitContents(entry).toString();

我知道库中不存在这个,但是必须有一些东西可以将提交的内容放入可以变成字符串的东西

代码已更改为

OutputStream output = new OutputStream() {
    private StringBuilder string = new StringBuilder();
    @Override
    public void write(int b) throws IOException {
        this.string.append((char) b );
    }

    public String toString(){
        return this.string.toString();
    }
};
RevCommit headCommit = getHeadCommit(repository);
RevCommit diffWith = headCommit.getParent(0);
try (DiffFormatter diffFormatter = new DiffFormatter(output)) {
    diffFormatter.setRepository(repository);
    for (DiffEntry entry : diffFormatter.scan(diffWith, headCommit)) {
        diffFormatter.format(diffFormatter.toFileHeader(entry));
    }
String strings = output.toString();
System.out.println(strings);

这是一条很长的路要走,但它证明了这是可以做到的 这现在已将差异存储到字符串

strings