JGit: How to get all commits of a branch? (Without

2020-02-09 10:50发布

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

Unfortunately the JGit docs are not very good ...

In ruby with grit it is very easy:

repo = Grit::Repo.new(pathToRepo)

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

Bye, hurik

标签: java jgit
6条回答
该账号已被封号
2楼-- · 2020-02-09 11:16

I was just looking for a way to list all commits under each branch and I found this thread. I finally did something a bit different from some of the answers I found here and worked.

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

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

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

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

        Iterable<RevCommit> commits = git.log().add(repo.resolve(branchName)).call();

        List<RevCommit> commitsList = Lists.newArrayList(commits.iterator());

        for (RevCommit commit : commitsList) {
            System.out.println(commit.getName());
            System.out.println(commit.getAuthorIdent().getName());
            System.out.println(new Date(commit.getCommitTime() * 1000L));
            System.out.println(commit.getFullMessage());
        }
    }

    git.close();
}

Thanks for the help

查看更多
家丑人穷心不美
3楼-- · 2020-02-09 11:18

Tried this here first: https://stackoverflow.com/a/13925765/2246865

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());
            }
        }
    }
}
查看更多
叼着烟拽天下
4楼-- · 2020-02-09 11:18

You are right, the docs should really be better.

You could use the JGit Log command or use a library like gitective which makes iterating over commits easy. You can look at the source to learn more about JGit.

Other (more complicated) ways are described in this SO-question.

查看更多
男人必须洒脱
5楼-- · 2020-02-09 11:20

With the code below you can get all commits of a branch or tag:

Repository repository = new FileRepository("/path/to/repository/.git");
String treeName = "refs/heads/master"; // tag or branch
for (RevCommit commit : git.log().add(repository.resolve(treeName)).call()) {
    System.out.println(commit.getName());
}

The varialbe treeName will define the tag or branch. This treeName is the complete name of the branch or tag, for example refs/heads/master for the master branch or refs/tags/v1.0 for a tag called v1.0.

Alternatively, you can use the gitective API. The following code does the same as the code above:

Repository repository = new FileRepository("/path/to/repository/.git");

AndCommitFilter filters = new AndCommitFilter();
CommitListFilter revCommits = new CommitListFilter();
filters.add(revCommits);

CommitFinder finder = new CommitFinder(repository);
finder.setFilter(filters);
String treeName = "refs/heads/master"; // tag or branch
finder.findFrom(treeName);

for (RevCommit commit : revCommits) {
    System.out.println(commit.getName());
}

Some try/catch will be necessary, I hide them to make the code shorter. Good luck.

查看更多
放荡不羁爱自由
6楼-- · 2020-02-09 11:24

A much shorter and clean solution can be done via the "log" command that JGit provides:

    Repository repository = new FileRepository("pathToRepo/.git");
    logs = new Git(repository).log()
            .add(repository.resolve("remotes/origin/testbranch"))
            .call();
    count = 0;
    for (RevCommit rev : logs) {
        System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
        count++;
    }
    System.out.println("Had " + count + " commits overall on test-branch");

See the full snippet at the jgit-cookbook

查看更多
混吃等死
7楼-- · 2020-02-09 11:24

This seems to work but I thought add(ObjectId branch) to provide a gate to the active branch, but I appear to require a range

Git git = ...
ObjectId master = git.repository.resolve("refs/heads/master")
ObjectId branch = git.repository.resolve("refs/heads/mybranch")

git.log().addRange(master,branch).call()
查看更多
登录 后发表回答