How to determine with JGit which branches have been merged to master?

前端 未结 1 1212
执念已碎
执念已碎 2020-12-21 04:12

How do I use JGit to determine which branches have been merged to master?

I want to do the equivalent of the normal git command:

git branch -a --merg         


        
1条回答
  •  清酒与你
    2020-12-21 05:09

    RevWalk::isMergedInto() can be used to determine if a commit was merged into another. That is, isMergedInto returns true if the commit given in the first argument is an ancestor of the second given commit.

    try (RevWalk revWalk = new RevWalk(repository)) {
      RevCommit masterHead = revWalk.parseCommit(repository.resolve("refs/heads/master");
      RevCommit otherHead = revWalk.parseCommit( repository.resolve("refs/heads/other-branch");
      if (revWalk.isMergedInto(otherHead, masterHead)) {
        ...
      }
    }
    

    To get a list of all branches the ListBranchesCommand is used:

    List branches = Git.wrap(repository).listBranches().setListMode(ListMode.ALL).call();
    

    0 讨论(0)
提交回复
热议问题