在没有使用JGit的父提交(第一次提交)的情况下显示更改/差异

问题描述 投票:2回答:1

[当我在存储库中进行第一次提交的git show commit时,我会看到所有文件以及文件的差异(即,所有要添加的行)]

$ git show cb5d132
commit cb5d13286cf9d14782f0e10445456dfe41072f55
Author: tw2 tw2LastName <tw2>
Date:   Thu Oct 23 05:15:09 2014 -0400

   Initial Commit

diff --git a/README.txt b/README.txt
new file mode 100644
index 0000000..96d156e
--- /dev/null
+++ b/README.txt
@@ -0,0 +1 @@
+First Line in README file!
\ No newline at end of file

jgit show似乎没有获得相似的信息,如何使用JGit API获得相似的输出?我可以使用DiffFormatter,但似乎需要baseTree和commitTree,想知道如何才能使DiffFormatter在存储库中的第一次提交上工作。

ByteArrayOutputStream os = new ByteArrayOutputStream();
DiffFormatter df = new DiffFormatter( os )
RawTextComparator cmp = RawTextComparator.DEFAULT;

df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);

// wondering how to use the API if we do not have baseCommit.
List<DiffEntry> diffEntries = df.scan(??baseCommitTree??, firstCommit.getTree());
for (DiffEntry diffEntry : diffEntries) {                
   df.format(diffEntry);
}
System.out.println(df.toString());
jgit
1个回答
4
投票
使用o.e.jgit.treewalk.EmptyTreeIterator将第一次提交与以下内容进行比较:

AbstractTreeIterator oldTreeIter = new EmptyTreeIterator(); ObjectReader reader = repository.newObjectReader(); AbstractTreeIterator newTreeIter = new CanonicalTreeParser(null, reader, firstCommit.getTree()); List<DiffEntry> diffEntries = df.scan(oldTreeIter, newTreeIter); ...

© www.soinside.com 2019 - 2024. All rights reserved.