在JGit中记录更改历史记录

时间:2013-10-28 07:00:01

标签: java git svn jgit

我正在尝试使用JGit显示文件更改历史记录。我设法获得了提交消息的历史记录。但无法获得与每次提交相关的更改(如git log -p),基本上我需要检查更改内容...(如+, - 在日志命令中)

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

2 个答案:

答案 0 :(得分:2)

使用DiffFormatter解决以下问题 http://codesnippetx.blogspot.com/

答案 1 :(得分:0)

如果没有简单的方法可以通过纯java LogCommand显示补丁,你可以使用org.eclipse.jgit.pgm.Log类,它包含对git的调用。

作为包装器,它具有-p选项。