JGit:如何获得分支的所有提交? (无需更改工作目录......) [英] JGit: How to get all commits of a branch? (Without changes to the working directory ...)

查看:1727
本文介绍了JGit:如何获得分支的所有提交? (无需更改工作目录......)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不更改工作目录的情况下获取JGit分支的所有提交?

how do I get all commits of a branch with JGit, without changing the working directory?

不幸的是JGit文档不是很好......

Unfortunately the JGit docs are not very good ...

在ruby中使用砂砾非常容易:

In ruby with grit it is very easy:

repo = Grit::Repo.new(pathToRepo)

repo.commits(branchName, false).each do |commit|
    doSomethingWithTheCommit
end

再见,
hurik

Bye, hurik

推荐答案

首先在此尝试: https:// stackoverflow.com/a/13925765/2246865

但它总是没有工作,所以我在这里找到了: http://www.eclipse.org/forums/index.php/t/280339/

But it wasn't was working always, so I found this here: http://www.eclipse.org/forums/index.php/t/280339/

在这里我的解决方案,它不是很好,但它正在工作......

Here my solution, it isn't really nice, but it's working ...

public static void main(String[] args) throws IOException, GitAPIException {
    Repository repo = new FileRepository("pathToRepo/.git");
    Git git = new Git(repo);
    RevWalk walk = new RevWalk(repo);

    List<Ref> branches = git.branchList().call();

    for (Ref branch : branches) {
        String branchName = branch.getName();

        System.out.println("Commits of branch: " + branch.getName());
        System.out.println("-------------------------------------");

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

        for (RevCommit commit : commits) {
            boolean foundInThisBranch = false;

            RevCommit targetCommit = walk.parseCommit(repo.resolve(
                    commit.getName()));
            for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) {
                if (e.getKey().startsWith(Constants.R_HEADS)) {
                    if (walk.isMergedInto(targetCommit, walk.parseCommit(
                            e.getValue().getObjectId()))) {
                        String foundInBranch = e.getValue().getName();
                        if (branchName.equals(foundInBranch)) {
                            foundInThisBranch = true;
                            break;
                        }
                    }
                }
            }

            if (foundInThisBranch) {
                System.out.println(commit.getName());
                System.out.println(commit.getAuthorIdent().getName());
                System.out.println(new Date(commit.getCommitTime() * 1000L));
                System.out.println(commit.getFullMessage());
            }
        }
    }
}

这篇关于JGit:如何获得分支的所有提交? (无需更改工作目录......)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆