我使用JGit获得两个分支之间的区别如下:
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
Ref master = git.getRepository().exactRef(getRefBranchName( branchMaster) );
RevTree masterTree = new RevWalk(git.getRepository()).parseTree(master.getObjectId());
oldTreeIter.reset(reader, masterTree.getId());
Ref release = git.getRepository().exactRef(getRefBranchName( branchRelease));
RevTree releaseTree = new RevWalk(git.getRepository()).parseTree(release.getObjectId());
newTreeIter.reset(reader, releaseTree.getId());
List<DiffEntry> diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
然后我可以按如下方式打印文件中的差异:
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter (outputStream);
formatter.setRepository(git.getRepository());
for (DiffEntry entry: diffs){
System.out.println("Diff Entry: " + entry + ", from: " + entry.getOldId()+", to: " + entry.getNewId());
formatter.format(entry);
String diffText = outputStream.toString("UTF-8");
System.out.println(diffText);
outputStream.reset();
}
我在这个分支中看到了两个文件之间的所有差异,但是如何针对某些DiffEntry
获取所有这些差异的提交?我还没有办法做到这一点
答案 0 :(得分:0)
p4 sync
没有DiffEntry
s&#39;的列表。它表示对文件的单个更改,并描述是否添加,修改或删除了文件。并且提交不一定是变更的来源。
但是,您已经有指向计算DiffEntries的提交的指针。它是RevCommit
和master
。
使用release
,您可以获取ref当前指向的提交的ID。
要获取包含提交的所有元数据并指向其树的ref.getObjectId()
,您需要使用RevCommit
,如下所示:
RevWalk
有关JGit差异API的更多信息,您可能需要阅读文章What’s the Difference? Creating Diffs with JGit