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

时间:2016-11-14 13:36:38

标签: java jgit

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

上述过程的反转是否可行? I.e查找作为提交的一部分提交的文件列表。

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

3 个答案:

答案 0 :(得分:11)

每个提交都指向,表示构成提交的所有文件。请注意,这不仅包括使用此特定提交添加,修改或删除的文件,还包括此修订中包含的所有文件。

如果提交表示为RevCommit,则可以像这样获取树的ID:

ObjectId treeId = commit.getTree();

如果提交ID来自另一个源,则需要首先解析它以获取关联的树ID。请参阅此处,例如:How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

要迭代树,请使用TreeWalk

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

如果您只对通过特定提交记录的更改感兴趣,请参阅此处:Creating Diffs with JGit或此处:File diff against the last commit with JGit

答案 1 :(得分:0)

我在此link中给出的代码中编辑了一点。 您可以尝试使用以下代码。

Notifier.welcome(User.first).deliver_later(wait: 1.hour)

答案 2 :(得分:-2)

您可以尝试:

 git diff --stat --name-only ${hash} ${hash}~1

或者看到更大范围的差异:

 git diff --stat --name-only ${hash1} ${hash2}
相关问题