简体   繁体   中英

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. 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? 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.

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:

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. See here, for example: How to obtain the RevCommit or ObjectId from a SHA1 ID string with JGit?

In order to iterate over a tree, use a 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

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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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