How do I determine if a commit descends from another commit?

We would like to reject merge commits on a branch A unless they are

  1. from some branch B to A or
  2. on branch B

In other words, a merge commit is ok if it merges B into A, or if it is an ancestor of the head of B.

The first condition is easy to check. I am a bit stymied with the second. I would like to ask, "does the head of B descend from the merge commit in question."

You would think that it would be a matter of repeatedly calling Commit.getParents, but that method returns a MinimalCommit, which is not very useful. Is there a more reasonable way to walk the ancestors of a commit than this?

I've also considered using CommitsBetweenRequest, but without a more clear definition of the "between" relation for two commits, I'm not sure that will work in all cases. Should I use that instead?

1 answer

0 vote

It's a bit confusing what you mean by 2), taken as a whole it reads:

"like to reject merge commits on a branch A unless they are on branch B"

Have a look at the docn for getCommitsBetween: it will let you see if a commit is reachable from another, which I think is what you want.

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published Dec 27, 2017 in Bitbucket

Recipe: Deploying AWS Lambda functions with Bitbucket Pipelines

Bitbucket Pipelines helps me manage and automate a number of serverless deployments to AWS Lambda and this is how I do it. I'm building Node.js Lambda functions using node-lambda&nbsp...

2,049 views 1 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you