JGit equivalent to "git branch -a --contains <commit id> --merged"
<p>How can I replicate <code>git branch -a --contains &lt;commit id&gt; --merged</code> in JGit?</p>

<p>Below, I replicate without <code>--merged</code> but I'm not sure how to add the filter to only report branches to which the change was merged and not include all branches created after merge point.</p>

<p>If the command line can do, I expect JGit can too but I'm not sure how.</p>

<p><strong>Note</strong>: the code below uses our GitController which is a wrapper around JGit, so some methods listed aren't pure JGit.</p>

<p><strong>Code</strong></p>

<pre class="lang-java prettyprint-override"><code>public List&lt;String&gt; getBranchesContainingCommit(File localWorkingCopy, String id, boolean onlymerged = true) {
  List&lt;String&gt; branches = new ArrayList&lt;&gt;()

  // Access Repository
  Git git = openExistingRepository(localWorkingCopy)

  // Find commit
  RevWalk walk = new RevWalk(git.repository)
  ObjectId foundId = git.repository.resolve(id + "^0")
  RevCommit commit = walk.parseCommit(foundId)

  // For all Remote Branches
  getBranches(git,true).each{ Ref ref -&gt;

    if (walk.isMergedInto(
            walk.parseCommit(ref.getObjectId()), commit)) {
      branches.add(branchNameFromRefName(ref.name))
    }
  }
  walk.close()
  walk.dispose()
  return branches
}
</code></pre>

<p><strong>Test Error</strong></p>

<pre><code>java.lang.AssertionError: Wrong number of branches found
Expected:  [feature/branchContainsMerged, feature/branchContainsBase]
Actual:    [feature/branchContainsAfterMerge, feature/branchContainsBase, feature/branchContainsMerged, master] 
Expected :2
Actual   :4
  &lt;Click to see difference&gt;
</code></pre>

<p><strong>Test Case</strong></p>

<pre class="lang-java prettyprint-override"><code>  /**
   * 1. Create baseBranch
   * 2. branch branchMergedChangeSrc
   * 3. Make change
   * 4. merge branchMergedChangeSrc to baseBranch
   * 5. branch branchUnmergedChangeSrc
   * 6. Make change
   * 7. Branch branchAfterMerge from baseBranch
   *
   * Expect: with merges filter
   *   idMerged on branchMergedChangeSrc, baseBranch
   *   idUnmerged on branchUnmergedChangeSrc
   */
  String baseBranch="feature/branchContainsBase"
  String branchMergedChangeSrc="feature/branchContainsMerged"
  String idMerged = "f342575605aa297778047a940ac1f47b74c29d81"
  String branchUnmergedChangeSrc="feature/branchContainsUnMerged"
  String idUnmerged = "e1dbd1e371d60af235a1a0e82f95c25a9c0049b6"
  String branchAfterMerge="feature/branchContainsAfterMerge"

  @Before
  void setup() {
    controller = (GitController) getSCM(SCMType.Git)
    workspace  = createEmptyTestDir()

    // Clone Skeleton
    controller.cloneRepository(new File("testdata/skeleton.git").toURI().toString(), workspace)
  }

  @Test
  void mergedBranchesForMergedCommitTest() {
    List&lt;String&gt; containingBranches = controller.getBranchesContainingCommit(workspace, idMerged)

    // Unmerged Change on Branch
    checkBranches([branchMergedChangeSrc, baseBranch], containingBranches)
  }


  @Test
  void unmergedBranchesForMergedCommitTest() {
    List&lt;String&gt; containingBranches = controller.getBranchesContainingCommit(workspace, idUnmerged)

    // Unmerged Change on Branch
    checkBranches([branchUnmergedChangeSrc], containingBranches)
  }


  void checkBranches(List&lt;String&gt; expectedBranchNames, List&lt;String&gt; actual) {
    assertEquals("""Wrong number of branches found
Expected:  ${expectedBranchNames}
Actual:    ${actual}""", expectedBranchNames.size(), actual.size())

    expectedBranchNames.each { String branch -&gt;
      assertTrue("Expected ${branch} in containing: ${actual}", actual.contains(branch))
    }

  }
</code></pre>