java - 在 JGit 中,是否为单个 DiffEntry 调用 DiffFormatter.format(DiffEntry) 应该为整个提交提供差异?
问题描述
当我通过 DiffEntrys 迭代工作区和头部提交之间的差异时,看起来每个 DiffEntry 代表一个文件。但是,当我调用 formatter.format(DiffEntry) 时,我得到一个字符串,表示工作区和头部提交之间的所有差异。不仅仅是与 DiffEntry 关联的文件。那是它应该如何工作的吗?如何获得与 DiffEntry 关联的一个文件的差异?我需要自己解析字符串吗?
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.DiffEntry;
...
repository=m_git.getRepository();
outputStream=new ByteArrayOutputStream()
headObjectId=repository.resolve(Constants.HEAD);
revWalk!=new RevWalk(repository);
headRevCommit=revWalk.parseCommit(headObjectId);`
// Get a list of diffs between the working tree and head commit
formatter = new DiffFormatter(outputStream);
formatter.setRepository(repository);
commitTreeIterator = prepareTreeParser(headRevCommit);
workTreeIterator = new FileTreeIterator(repository);
diffEntries = formatter.scan(commitTreeIterator, workTreeIterator);
while (diffIter.hasNext()) {
// Capture all the differences into a string
entry=cast(DiffEntry,diffIter.next());
formatter.format(entry);
x=outputStream.toString();
// x has diffs for all files and not just the file this DiffEntry represents!
System.out.println(entry.getNewPath());
System.out.println(x);
}
它甚至在文档中声明它为一个文件条目格式化补丁脚本,所以我不明白为什么我将所有文件条目都放在一个字符串中:https://download.eclipse.org/jgit/site/5.8。 1.202007141445-r/apidocs/org/eclipse/jgit/diff/DiffFormatter.html#format-org.eclipse.jgit.diff.DiffEntry-
解决方案
正如@Rüdiger Herrmann 指出的那样,我的代码将每个 DiffEntry 附加到 OutputStream。因为我使用的是 ByteArrayOutputStream,所以我可以在条目之间调用 ByteArrayOutputStream.reset(),这样每次调用 ByteOutputStream.toString() 时我只会得到一个 DiffEntry。
在我的代码示例中,它看起来像这样:
while (diffIter.hasNext()) {
// Capture all the differences into a string
entry=cast(DiffEntry,diffIter.next());
formatter.format(entry);
x=outputStream.toString();
outputStream.reset();
// x has diffs for all files and not just the file this DiffEntry represents!
System.out.println(entry.getNewPath());
System.out.println(x);
}
推荐阅读
- sapui5 - UI5:检索并显示 sap.m.Table 中的数千个项目
- node.js - 我一直在尝试用 Mongoose 解决 nodeJS 中的重复键问题,但没有任何效果
- c++ - 减少编译时间
- c# - 如何使用 DBContext 优化 EF 中的 CRUD 操作?
- python - 如果底部参数为 0,则 Matplotlib stackbarchart 无法绘制下一个条形图
- java - Spring 应用程序无法在外部库中获取 ResourceAsStream 但可以从自己的类中加载
- html - 视频 1 结束后自动播放下一个视频
- django - 如何使用 django 显示从数据库中检索到的数据
- python - AttributeError:“NoneType”对象没有属性“get_text”。为什么我会收到此错误?
- android - 即使应用程序关闭,如何在 API >= 26 中执行定期后台工作?