3

I'm trying to know how I can get the branch in which a commit was created in (not the head). I know a branch is just a 'tag' attached to a commit (the head of that branch)

enter image description here

For example, in this image, what would I need to do to get the branch of the node E using nodegit or another git library.

I looked in the documentation, but I didn't find an equivalent to git branch --contains

Thanks !

R00t
  • 186
  • 1
  • 2
  • 14
  • What would you expect, when the feature branch cool-new-feature was merged and then deleted? The commit will still be there, but onlycontained in the master bracnh. – milbrandt May 01 '18 at 20:33
  • @milbrandt Yeah, but what if the branch wasn't deleted – R00t May 01 '18 at 21:34
  • `git branch --contains` doesn't tell the branch where a commit was created. It tells from which (local) branches a commit now is reachable. I took a quick look at the documenation and I think you could use apis to 1)get a branch;2)list its commits;3)test if a commit is among them;4)loop to local/remote/all branches, if there isn't an existing single api to do the job. – ElpieKay May 01 '18 at 23:35

1 Answers1

1

You'd have to:

  1. Iterate over all the branches.
  2. Find the ancestor of each branch's tip against the commit with NodeGit.Merge.base(repository, branchTip, interestedCommit).
  3. Then if the returnedCommit === interestedCommit then you're fine
rcjsuen
  • 873
  • 1
  • 6
  • 12