-1

I'm trying to find commits on branch foo that aren't on master. I have this working for the simple cases, but once new changes have been merged from master into foo it breaks down.

In more detail:

  • If you have commits A, B, and C on master
  • then you create branch foo and add commits D and E there
  • then you create commit F on master
  • then you merge F from master into foo (using a merge commit)

now trying to get the merge base returns F instead of C. So I can't iterate over commits from C to foo's head (giving C, D, E, F, which I'll then narrow down to D & E using isMergedInto)

public RevCommit findMergeBase(ObjectId foo, ObjectId bar) throws MissingObjectException,
        IncorrectObjectTypeException, IOException {
    RevWalk walk = null;

    try {
        walk = new RevWalk(repo);
        RevCommit fooHead = walk.parseCommit(foo);
        RevCommit barHead = walk.parseCommit(bar);

        walk.setRevFilter(RevFilter.MERGE_BASE);
        walk.markStart(fooHead);
        walk.markStart(barHead);

        RevCommit base = null;
        while (true) {
            RevCommit commit = walk.next();
            if (commit == null)
                break;

            base = commit;
        }

        return base;
    }
    finally {
        if (walk != null) {
            walk.reset();
            walk.close();
            walk.dispose();
        }
    }
}
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
  • See here how to find the merge base in JGit: https://stackoverflow.com/questions/26434452/how-is-a-merge-base-done-in-jgit – Rüdiger Herrmann Nov 12 '19 at 07:08
  • Possible duplicate of [How is a merge-base done in JGit?](https://stackoverflow.com/questions/26434452/how-is-a-merge-base-done-in-jgit) – Rüdiger Herrmann Nov 12 '19 at 07:09
  • @RüdigerHerrmann yes, I believe I'm *doing* what's explained in that question, it's just not producing the expected result – Brad Mace Nov 12 '19 at 15:00
  • In the answer, the result of the first call to `walk.next()` is returned, your code does different things. – Rüdiger Herrmann Nov 12 '19 at 15:10
  • @RüdigerHerrmann Ok. I'm not sure what example I took the while loop from, but I'm getting the same result just returning the first call to `walk.next()`. – Brad Mace Nov 12 '19 at 15:28
  • Sometimes it helps to write an isolated learning test (see here for a template to help with the setup: https://gist.github.com/rherrmann/433adb44b3d15ed0f0c7) if you're stuck. And possibly, it helps others to reproduce your scenario exactly. BTW did you verify your expectations with native Git? – Rüdiger Herrmann Nov 12 '19 at 18:27

1 Answers1

0

Seems like the issue may have been that I created by feature branch off of the remote master branch? So it couldn't figure out how the feature branch connected to my local master? Once I updated my local master branch it worked as expected.

Brad Mace
  • 27,194
  • 17
  • 102
  • 148