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

标签 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 和 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());

最佳答案

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

关于jgit - 使用 JGit 显示没有父提交(第一次提交)的更改/差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27739840/

相关文章:

java - jgitblame 文件的某些行具有 null 提交 shas

java - JGit 将 Repo A merge 到 fork 版本 repo B 中,其中 A 领先于 B

java - 为什么我的 JGit checkout 返回 null ref?

java - 无法使用 File delete() 方法删除 git repo 中的 .pack 文件

java - JGIT:git 结账 --

linux - 错误 : Creating directories for/. niogit/system.git 在 Wildfly-8.1.0.Final 上部署 jbpm-console.war 失败

eclipse-plugin - Egit 已安装(与 Juno 一起提供),但根本不显示

jgit - 为 JGit 克隆命令关闭 SSL 验证

java - JGit:在分支中的提交处读取文件的内容

java - 如何计算 JGit 中插入/删除的行数