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?
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 :
git diff $BITBUCKET_PR_DESTINATION_BRANCH...HEAD
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.