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

Vin*_*lla 4 jgit

当我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
Run Code Online (Sandbox Code Playgroud)

似乎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());
Run Code Online (Sandbox Code Playgroud)

Rüd*_*ann 6

使用 ano.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);
...
Run Code Online (Sandbox Code Playgroud)