4

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 --merged

Is this possible in JGit?

Brian Kent
  • 3,754
  • 1
  • 26
  • 31

1 Answers1

6

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<Ref> branches = Git.wrap(repository).listBranches().setListMode(ListMode.ALL).call();
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79