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

时间:2015-01-02 09:19:43

标签: 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

“jgit show”似乎没有得到类似的信息,如何使用JGit API获得类似的输出,我可以使用DiffFormatter,但似乎需要baseTree&amp; 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() )

1 个答案:

答案 0 :(得分:3)

使用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 );
...