如何使用JGit获取提交的文件列表

Tha*_*n M 11 java jgit

我一直在研究基于Java的产品,Git功能将被集成到该产品中.使用Git功能之一,我已经通过staging将10多个文件添加到Git存储库中,然后在单个提交中提交它们.

上述过程的反向可能吗?即找到作为提交的一部分提交的文件列表.

我在git.log()命令的帮助下得到了提交,但我不知道如何获取提交的文件列表.

示例代码:

Git git = (...);
Iterable<RevCommit> logs = git.log().call();
for(RevCommit commit : logs) {
    String commitID = commit.getName();
    if(commitID != null && !commitID.isEmpty()) {
    TableItem item = new TableItem(table, SWT.None);
    item.setText(commitID);
    // Here I want to get the file list for the commit object
}
}
Run Code Online (Sandbox Code Playgroud)

Rüd*_*ann 15

每个提交都指向一个,表示构成提交的所有文件.

请注意,这不仅包括使用此特定提交添加,修改或删除的文件,还包括此修订中包含的所有文件.

为了迭代树,你可以使用JGit RevCommit:

ObjectId treeId = commit.getTree();
Run Code Online (Sandbox Code Playgroud)

如果您只对使用某个提交记录的更改感兴趣,请参阅此处:使用JGit或此处创建Diffs:使用JGit 针对上次提交创建文件差异

  • 我得到:org.eclipse.jgit.errors.IncorrectObjectTypeException:对象e5acd3dd99bfc09bf93ba4dd2db4a9ddba4c7359不是树。 (2认同)