6

我是 jGit 和 Git 的新手。我正在尝试查询 git 存储库的提交历史,但我只想获取特定用户的提交。阅读文档我看到 RevWalk 将允许我添加 RevFilters 以限制搜索。

首先,我使用 Git 对象及其 log 方法来列出提交,并且效果很好。但是使用 RevWalk,什么也没有发生。

public static void main(String[] args) throws IOException, GitAPIException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(new File("/home/joan/testGit/testMockito/.git"))
    .readEnvironment().findGitDir().build();

    //This works but I get all the commits
    Git git = new Git(repository);
    Iterable<RevCommit> log = git.log().call();
    for (Iterator<RevCommit> iterator = log.iterator(); iterator.hasNext();) {
        RevCommit rev = iterator.next();
        System.out.println(rev.getAuthorIdent().getName());
        System.out.println(rev.getFullMessage());
    }

    RevWalk walk = new RevWalk(repository);
    for (Iterator<RevCommit> iterator = walk.iterator(); iterator.hasNext();) {
        //It never cames in this block

        RevCommit rev = iterator.next();
        System.out.println(rev.getAuthorIdent().getName());
        System.out.println(rev.getFullMessage());
    }

}

有什么建议吗?难道我做错了什么?

谢谢。

4

2 回答 2

11

You need to add commits to the RevWalk by callingmarkStart before iterating over it.

For example to start the RevWalk with the current repository's HEAD commit:

walk.markStart(walk.parseCommit(repository.resolve("HEAD")));

于 2012-08-08T20:35:03.623 回答
2

为什么不直接使用LogCommandfrom org.eclipse.jgit.api

Git git = new Git(db);

Iterable<RevCommit> log = git.log().call();

然后,您可以根据返回的提交过滤作者。我还建议查看 Gittive,它是 JGit 之上的一层。你可以学习如何用它来实现 JGit 相关的东西:https ://github.com/kevinsawicki/gitective/

You could do something like this:

PersonIdent person = new PersonIdent("Chris", "chris@chris.com");

filters.add(new AuthorFilter(person));

And then you can call CommitFinder... with the desired filters...

于 2012-08-08T19:58:32.093 回答