Question

I'm trying to show file change history using JGit. I managed to get the history of commit messages. But couldn't get changes related to each commit (like git log -p) , Basically I need to check what was the change... (like + , - in log command)

public void test() {

    try {
        File gitWorkDir = new File("/home/test/GITTEST/");
        Git git = null;
        git = Git.open(gitWorkDir);
        Repository repo = git.getRepository();
        LogCommand log = git.log();
        log.setMaxCount(2);
        Iterable<RevCommit> logMsgs = log.call();
        for (RevCommit commit : logMsgs) {
            System.out.println("----------------------------------------");
            System.out.println(commit);
            System.out.println(commit.getAuthorIdent().getName());
            System.out.println(commit.getAuthorIdent().getWhen());
            System.out.println(" ---- " + commit.getFullMessage());
            System.out.println("----------------------------------------");
            RevTree tree = commit.getTree();

TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(TreeFilter.ANY_DIFF);

//treeWalk.setFilter(PathFilter.create("."));
if (!treeWalk.next()) 
{
  System.out.println("Nothing found!");
  return;
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repo.open(objectId);
ByteArrayOutputStream out = new ByteArrayOutputStream();
loader.copyTo(out);
System.out.println("----" + out.toString());
        System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>----------------------------------------");    
        }
    } catch (Exception e) {
        System.out.println("no head exception : " + e);
    }
}
Was it helpful?

Solution

resolved in following using DiffFormatter http://codesnippetx.blogspot.com/

OTHER TIPS

If there is no easy way to display the patches through pure java LogCommand, you can take idea with the org.eclipse.jgit.pgm.Log class, which wraps calls to git.

As a wrapper, it does have the -p option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top