gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 02:22:05 28 4
gpt4 key购买 nike

当我对存储库中的第一个提交执行 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com