如何使用JGit获取提交的文件列表 [英] How to get the file list for a commit with JGit

查看:3138
本文介绍了如何使用JGit获取提交的文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been working on a Java based product for which the Git features are going to be integrated. Using one of the Git features, I have done adding 10+ files into the Git repository by staging followed by committing them in a single commit.

与上面相反过程可能?即找到作为提交的一部分提交的文件列表。

Is the reverse of the above process possible? I.e Finding the list of files committed as part of a commit.

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

I got the commit with the help of the git.log() command but I am not sure how to get the file list for the commit.

示例代码:

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
}
}


推荐答案

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

Each commit points to a tree that denotes all files that make up the commit.

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

Note, that this not only includes the files that were added, modified, or removed with this particular commit but all files contained in this revision.

为了迭代树,你可以使用JGit的 TreeWalk

In order to iterate over a tree, you can use JGit's TreeWalk:

try( TreeWalk treeWalk = new TreeWalk( repository ) ) {
  treeWalk.reset( commit.getId() );
  while( treeWalk.next() ) {
    String path = treeWalk.getPathString();
    // ...
  }
}

如果你只对通过特定提交记录的更改感兴趣,请参阅此处:使用JGit创建Diffs 或此处:文件差异对抗最后一个使用JGit提交

If you are only interested in the changes that were recorded with a certain commit, see here: Creating Diffs with JGit or here: File diff against the last commit with JGit

这篇关于如何使用JGit获取提交的文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆