How can I tell with JGit which branches have been merged for mastering?

How can I use JGit to determine which branches have been merged for mastering?

I want to do the equivalent of a regular git command:

git branch -a --merged

      

Is this possible in JGit?

+3


source to share


1 answer


RevWalk::isMergedInto()

can be used to determine if a union has been merged into another. That is, it isMergedInto

returns true if the value passed in the first argument is an ancestor of the second specified 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, use ListBranchesCommand

:

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

      

+5


source







All Articles