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

前端 未结 6 1864
春和景丽
春和景丽 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:30

    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 branches = git.branchList().call();
    
        for (Ref branch : branches) {
            String branchName = branch.getName();
    
            System.out.println("Commits of branch: " + branchName);
            System.out.println("-------------------------------------");
    
            Iterable commits = git.log().add(repo.resolve(branchName)).call();
    
            List 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

提交回复
热议问题