How to get a list of files to commit using JGit

I am working on a Java product for which Git functions will be integrated. Using one of the Git functions, I added 10+ files to the Git repository by placing them, followed by fixing them in a single commit.

Is it possible to repeat the above process? Ie Search for a list of files transferred as part of the commit.

I got the commit with the command git.log(), but I'm not sure how to get the list of files to commit.

Code example:

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
}
}
+4
source share
3 answers

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

, , , , , .

, JGit TreeWalk:

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

, , . : Diffs JGit : JGit

+6

, . .

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);
            }
        }
    }
}
0

You can try:

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

Or see the difference in a larger range:

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

Source: https://habr.com/ru/post/1660690/


All Articles