How to get changed folders in pipeline when PR is merged?
Asked by Shubham:

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.


Answers:
author: Patrik S
body: Hello [@Shubham](/t5/user/viewprofilepage/user-id/5429640) 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](/t5/user/viewprofilepage/user-id/5429640) !

Patrik S

comments: author: Shubham
body: 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.


author: Patrik S
body: Hello [@Shubham](/t5/user/viewprofilepage/user-id/5429640) ,

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:

name: GetModifiedFilesBranchPipeline

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](/t5/user/viewprofilepage/user-id/5429640) !

Patrik S


author: Shubham
body: Hi Patrik,

Solution you suggested above worked.

Thank you for the help.


author: Patrik S
body: 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](/t5/user/viewprofilepage/user-id/5429640) !

Patrik S


Tags: bitbucket-cloud

cloud