简体   繁体   English

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

[英]How to get the file list for a commit with JGit

I have been working on a Java based product for which the Git features are going to be integrated.我一直在开发一个基于 Java 的产品,它将集成 Git 功能。 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.使用其中一项 Git 功能,我通过暂存并在一次提交中提交这些文件,将 10 多个文件添加到 Git 存储库中。

Is the reverse of the above process possible?上述过程的逆过程是否可能? Ie Finding the list of files committed as part of a commit.即查找作为提交的一部分提交的文件列表。

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.log()命令的帮助下得到了提交,但我不确定如何获取提交的文件列表。

Example Code:示例代码:

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.请注意,这不仅包括在此特定提交中添加、修改或删除的文件,还包括此修订版中包含的所有文件。

If the commit is represented as a RevCommit , the ID of the tree can be obtained like this:如果提交表示为RevCommit ,则可以像这样获取树的 ID:

ObjectId treeId = commit.getTree().getId();

If the commit ID originates from another source, it needs to be resolved first to get the associated tree ID.如果提交 ID 来自另一个来源,则需要首先解析它以获取关联的树 ID。 See here, for example: How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?例如,请参见此处: 如何使用 JGit 从 SHA1 ID 字符串获取 RevCommit 或 ObjectId?

In order to iterate over a tree, use a TreeWalk :为了迭代一棵树,请使用TreeWalk

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

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 创建差异或此处: 使用 JGit 与上次提交的文件差异

I edited a little from the code given in this link .我从这个链接中给出的代码中编辑了一些。 You can try using the below code.您可以尝试使用以下代码。

public void commitHistory(Git git) throws NoHeadException, GitAPIException, IncorrectObjectTypeException, CorruptObjectException, IOException, UnirestException 
{
    Iterable<RevCommit> logs = git.log().call();
    int k = 0;
    for (RevCommit commit : logs) {
        String commitID = commit.getName();
        if (commitID != null && !commitID.isEmpty())
        {
            LogCommand logs2 = git.log().all();
            Repository repository = logs2.getRepository();
            tw = new TreeWalk(repository);
            tw.setRecursive(true);
            RevCommit commitToCheck = commit;
            tw.addTree(commitToCheck.getTree());
            for (RevCommit parent : commitToCheck.getParents())
            {
                tw.addTree(parent.getTree());
            }
            while (tw.next())
            {
                int similarParents = 0;
                for (int i = 1; i < tw.getTreeCount(); i++)
                    if (tw.getFileMode(i) == tw.getFileMode(0) && tw.getObjectId(0).equals(tw.getObjectId(i)))
                        similarParents++;
                if (similarParents == 0) 
                        System.out.println("File names: " + fileName);
            }
        }
    }
}

You can try with: 您可以尝试:

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

Or to see difference on larger range: 或者看到更大范围的差异:

 git diff --stat --name-only ${hash1} ${hash2}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM