Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
Community Members
Community Events
Community Groups

How do I get the commit SHA merged in in Pipelines pull-request builds?

Felix Atlassian Team Jun 10, 2022

I'm trying to set up a pull request build that calculates the files changed in a pull request on a certain path.

I'm trying to do this so that I can exercise any new tests introduced in the PR by running them multiple times (to ensure they are not flakey).

I know that the BITBUCKET_PR_DESTINATION_BRANCH variable exists, but it seems like that does not allow me to do what I want, because the merge that pipelines does for a pull-request build is not always the tip of the destination branch.

If the merge commit that happens on that build produces a merge, I can get the commit SHA using:

git rev-list --parents -n 1 HEAD | cut -d " " -f 3

I use that to grab the files like this:

git diff --name-only HEAD $(git rev-list --parents -n 1 HEAD | cut -d " " -f 3) -- test/

However, if the pull-request branch is up to date with the destination branch, no merge commit is produced, and the above does not work.

Is there any way to get the commit SHA Bitbucket Pipelines uses in the Build setup?

Or another easy way to produce a list of the changed files of a certain path in that PR?

Screen Shot 2022-06-10 at 2.54.08 PM.png

1 answer

0 votes
Patrik S Atlassian Team Jun 14, 2022

Hello @Felix ,

Welcome to Atlassian Community!

In bitbucket pipelines, when you are using pull-request triggers pipelines it should always merge the tip of the destination branch with the current working branch (the source of the pull request). So in this case, to get the commit hash that is shown in the merge command at Build Setup stage you could use git log as below : 

git log -n 1 --pretty=format:"%H" $BITBUCKET_PR_DESTINATION_BRANCH

This will print the tip of the destination branch of the pull request, which should match the commit hash presented in Build Setup.

As for getting the files that were changed in the source branch of the pull request, I think you could make use of the git three-way diff, as below : 


This will list all the files that were changed in the source branch of the Pull Request starting at the last common commit between the source and destination branch of the pull request.

Hope that helps! Let me know in case you have any questions.

Thank you, @Felix .

Kind regards,

Patrik S

Suggest an answer

Log in or Sign up to answer
Community showcase
Published in Bitbucket

Git push size limits are coming to Bitbucket Cloud starting April 4th, 2022

Beginning on April 4th, we will be implementing push limits. This means that your push cannot be completed if it is over 3.5 GB. If you do attempt to complete a push that is over 3.5 GB, it will fail...

2,158 views 2 9
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you