How to get changed folders in pipeline when PR is merged?

Shubham February 14, 2024

Hi

 

I'm trying to get the changed folders in bitbucket pipeline, but I'm not able to do that.

I'm using the command

 

git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH
git diff --name-only --diff-filter=d $BITBUCKET_COMMIT

When I put hardcoded commit it is working fine ie.e

git fetch origin $BITBUCKET_PR_DESTINATION_BRANCH

git diff --name-only --diff-filter=d abwjdsefd

 

I'm trying to store the output in some other variable.

Script

 

- git diff --name-only --diff-filter=d <hard_coded_commit> > changed_1.txt
- eval cat changed_1.txt
- git diff --name-only --diff-filter=d $BITBUCKET_COMMIT > changed.txt
- eval cat changed.txt
changed_1.txt is outputting correct values, whereas changed.txt is giving nothing.
My use case is to run the pipeline for PR for changed folders and when PR is merged, I want to process those folders again with branches in pipeline.

1 answer

1 vote
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 15, 2024

Hello @Shubham and welcome to the community!

The variable $BITBUCKET_COMMIT will contain the commit that triggered the pipeline. Once a pipeline starts, it will clone the repository inside the build container and checkout to that commit, which means the the head of the local repository will be moved to that commit.

Once you execute the command 

git diff --name-only --diff-filter=d $BITBUCKET_COMMIT

You are essentially comparing the current HEAD with the provided commit $BITBUCKET_COMMIT. However, since they are the same commit (pipelines resets the HEAD to the commit that triggered it), it will return nothing, as you would be comparing a commit with itself.

If your end goal is to check what folders changed in the commit that triggered the pull request compared the destination branch of the pull request, you should provide the destination branch of the PR in the git diff command instead : 

git diff --name-only --diff-filter=d $BITBUCKET_PR_DESTINATION_BRANCH

Could you try with that approach and let us know how it goes ?

Should you have any questions, feel free to ask :)

Thank you, @Shubham !

Patrik S

Shubham February 20, 2024

Hi Patrik

 

Thanks for the help.

 

I tried above approach. But it didn't work for my use case.

My pipeline is


pipelines:
pull-requests:
shubham-bb-pipeline*:
- step:
name: GetModifiedFilesPRPipeline
runs-on:
- 'self.hosted'
image:
name: 'golang:1.18'
script:
- eval echo $BITBUCKET_PR_DESTINATION_BRANCH
- git diff --name-only --diff-filter=d origin/$BITBUCKET_PR_DESTINATION_BRANCH

branches:
shubham-pipeline-test:
- step:
name: GetModifiedFilesBranchPipeline
runs-on:
- 'self.hosted'
image:
name: 'golang:1.18'
script:
- eval echo $BITBUCKET_PR_DESTINATION_BRANCH
- git diff --name-only --diff-filter=d $BITBUCKET_PR_DESTINATION_BRANCH

I'm raising a PR from shubham-bb-pipeline branch to shubham-pipeline-test.
When I'm raising a PR, there one pipeline runs for pull-requests config ie.e for shubham-bb-pipeline branch and it gives correct result as modified files as file1,file2 etc.
But when I merge the same PR, one pipeline runs for branches config i.e. for shubham-pipeline-test and in that pipeline, I want the same files in diff step but I'm getting empty result.
I want the same files in merge pipeline that showed in PR pipeline.
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 20, 2024

Hello @Shubham ,

The variable $BITBUCKET_PR_DESTINATION_BRANCH will only be available in a pull-request pipeline config. 

In the branch pipeline config you have (that runs for the merge commit) this variable will be empty, so this is the reason the diff is not showing anything.

If you want to also diff the files in the branch pipeline, you will need to compare the commit that is running the pipeline (the merge commit in this case), with one of its parents. Since the merge commit has two parents (one for the feature branch and the other for the main branch), you can use the modifier ^ to identify which parent you want.   

The first parent of a merge commit (^1) is from the branch you were on when you merged (usually main/master), while the second parent (^2) of a merge commit is from the branch that was merged (feature branch).

So in your case, since you are interested in the diffs introduced by the feature branch (the source branch of the pull request), you would need to use the following command in your branch pipeline config : 

branches:

shubham-pipeline-test:

step:

nameGetModifiedFilesBranchPipeline

runs-on:

'self.hosted'

image:

name'golang:1.18'

script:

- git diff --name-only --diff-filter=d HEAD^1

This will instruct git to calculate the diff between HEAD (the merge commit), and its first parent (the previous commit in the main branch). This would show all the files introduced by the feature branch into the merge commit. 

Hope that helps! Should you have any questions, feel free to ask

Thank you, @Shubham !

Patrik S

Like Sabine Mayer likes this
Shubham February 21, 2024

Hi Patrik,

 

Solution you suggested above worked.

Thank you for the help.

 

Like # people like this
Patrik S
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 22, 2024

You're ver welcome! 

Happy to have been of some help :)

Feel free to reach out to the community if you ever need assistance in the future.

Thank you, @Shubham !

Patrik S

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events