JGit: How to get all commits of a branch? (Without changes to the working directory …)

前端 未结 6 1861
春和景丽
春和景丽 2020-12-15 23:43

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 i

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 00:11

    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

提交回复
热议问题