Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the first commit of a branch with JGit?

I want to find the first commit for a specific branch, using Eclipse's JGit library. For example if I have

master -- a -- c -- d -- e
          \
  feature  b -- f -- g -- h
            \
    another  j -- k -- l

// findBranchRoot() is the magic method we want to create
repository.resolve("feature").findBranchRoot().getId(); // this would return b
repository.resolve("another").findBranchRoot().getId(); // this would return j

Does anyone know how to do that?

like image 636
TheCrafter Avatar asked Oct 15 '25 18:10

TheCrafter


1 Answers

The problem you're going to have is that—and bear with me, here—commits aren't "on" branches. To see this, consider the graph you drew. It's ambiguous. Unavoidably so. You drew

A---C---D---E          master
 \
  B---F---G---H        feature
   \
    J---k---L          another

and there is simply no way to determine whether B was made "on" feature or another (or, for that matter, which of the three A was made "on").

A---C---D---E          master
 \
  B---J---K---L        another
   \
    F---G---H          feature

shows exactly the same history. It's all in how you choose to interpret it.

If you want to tie a commit to some external administrative record, put a marker in the commit message, that'll do it, but in Git itself (and to the actual work) it's the history structure that matters, not how bits of it are referred to in this repo or that.

If it comes time to publish feature or another, you're going to need to push commit B either way, unless it's already been pushed as part of some other work. Ancestry matters. Branch names don't.

like image 127
jthill Avatar answered Oct 18 '25 09:10

jthill