Determine parent branch name or first commit of a feature branch
Asked by Ahmad Kabeer Shaukat:

I have a mono repo in which there are 13 spring boot microservices and whenever commits are pushed, the bitbucket pipeline runs checkstyles jobs in the pipeline that run for each microservice.  
We intend to optimize this behavior by determining the base commit of the current feature branch once we get the current commit hash and base commit hash using git diff we can determine, which microservices files have changed and hence only run check styles in those microservices resulting in optimization.  

I tried various commands to determine the parent branch and extract base commit and some of them worked in the local git repo but none of them worked for me in the bitbucket pipeline   

unless I checkout the dev branch inside the bitbucket pipeline in case the branch parent is dev   

we want a dynamic behavior   
1-Determine parent branch name   
2-Determine base commit   
3-Perform git diff   

<br />

this is the command that gives me the parent branch name in the local but not bitbucket unless I checkout to the dev branch in the pipeline   

<br />

<br />

<br />

```
git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'


```

<br />

<br />

<br />

<br />

<br />

The complete pipeline is   

<br />

```
image: gradle:7.6

pipelines:
  branches:
    "**":
      - step:
          name: Detect Changed Microservices
          caches:
            - gradle
          script:
            - git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
            - git fetch --all
            - echo $BITBUCKET_COMMIT
            - echo $BITBUCKET_BRANCH
            - echo $BITBUCKET_PR_DESTINATION_BRANCH
            - git checkout dev
            - git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
            - PARENT_BRANCH=$(git show-branch -a 2>/dev/null | grep '\*' | grep -v "$BITBUCKET_BRANCH" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//')
            - echo $PARENT_BRANCH
            - START_COMMIT=$(git merge-base HEAD "$PARENT_BRANCH")
            - echo $START_COMMIT
            - git checkout $BITBUCKET_BRANCH
            - CHANGED_MICROSERVICES=$(git diff --name-only $START_COMMIT $BITBUCKET_COMMIT | cut -d'/' -f1 | sort | uniq)
            - echo $CHANGED_MICROSERVICES
            - echo "$CHANGED_MICROSERVICES" > CHANGED_MICROSERVICES.txt
          artifacts:
            - CHANGED_MICROSERVICES.txt
      - parallel:
          - step:
              name: Run Checkstyles
              caches:
                - gradle
              script:
                - echo "Running Checkstyles for changed microservices"
                - if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
                  gradle auth-service:checkstyleMain auth-service:checkstyleTest;
                  fi
                - if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
                  gradle banking-engine:checkstyleMain banking-engine:checkstyleTest;
                  fi
                - if grep -qw "common" CHANGED_MICROSERVICES.txt; then
                  gradle common:checkstyleMain common:checkstyleTest;
                  fi
                - if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
                  gradle config-server:checkstyleMain config-server:checkstyleTest;
                  fi
                - if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
                  gradle discovery-server:checkstyleMain discovery-server:checkstyleTest;
                  fi
                - if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
                  gradle gateway:checkstyleMain gateway:checkstyleTest;
                  fi
                - if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
                  gradle invoicing-service:checkstyleMain invoicing-service:checkstyleTest;
                  fi
                - if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
                  gradle kafka-service:checkstyleMain kafka-service:checkstyleTest;
                  fi
                - if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
                  gradle notification-service:checkstyleMain notification-service:checkstyleTest;
                  fi
                - if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
                  gradle payment-card-service:checkstyleMain payment-card-service:checkstyleTest;
                  fi
                - if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
                  gradle private-gateway:checkstyleMain private-gateway:checkstyleTest;
                  fi
                - if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
                  gradle rules-engine:checkstyleMain rules-engine:checkstyleTest;
                  fi
                - if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
                  gradle third-party-config-service:checkstyleMain third-party-config-service:checkstyleTest;
                  fi
                - if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-control:checkstyleMain transaction-control:checkstyleTest;
                  fi
                - if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-map:checkstyleMain transaction-map:checkstyleTest;
                  fi
                - if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
                  gradle users-engine:checkstyleMain users-engine:checkstyleTest;
                  fi
              artifacts:
                - CHANGED_MICROSERVICES.txt

          - step:
              name: Run Tests
              caches:
                - gradle
              script:
                - echo "Running Tests for changed microservices"
                - if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
                  gradle auth-service:test;
                  fi
                - if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
                  gradle banking-engine:test;
                  fi
                - if grep -qw "common" CHANGED_MICROSERVICES.txt; then
                  gradle common:test;
                  fi
                - if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
                  gradle config-server:test;
                  fi
                - if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
                  gradle discovery-server:test;
                  fi
                - if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
                  gradle gateway:test;
                  fi
                - if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
                  gradle invoicing-service:test;
                  fi
                - if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
                  gradle kafka-service:test;
                  fi
                - if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
                  gradle notification-service:test;
                  fi
                - if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
                  gradle payment-card-service:test;
                  fi
                - if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
                  gradle private-gateway:test;
                  fi
                - if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
                  gradle rules-engine:test;
                  fi
                - if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
                  gradle third-party-config-service:test;
                  fi
                - if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-control:test;
                  fi
                - if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-map:test;
                  fi
                - if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
                  gradle users-engine:test;
                  fi
              artifacts:
                - CHANGED_MICROSERVICES.txt

          - step:
              name: Run Builds
              caches:
                - gradle
              script:
                - echo "Running Builds for changed microservices"
                - if grep -qw "auth-service" CHANGED_MICROSERVICES.txt; then
                  gradle auth-service:build;
                  fi
                - if grep -qw "banking-engine" CHANGED_MICROSERVICES.txt; then
                  gradle banking-engine:build;
                  fi
                - if grep -qw "common" CHANGED_MICROSERVICES.txt; then
                  gradle common:build;
                  fi
                - if grep -qw "config-server" CHANGED_MICROSERVICES.txt; then
                  gradle config-server:build;
                  fi
                - if grep -qw "discovery-server" CHANGED_MICROSERVICES.txt; then
                  gradle discovery-server:build;
                  fi
                - if grep -qw "gateway" CHANGED_MICROSERVICES.txt; then
                  gradle gateway:build;
                  fi
                - if grep -qw "invoicing-service" CHANGED_MICROSERVICES.txt; then
                  gradle invoicing-service:build;
                  fi
                - if grep -qw "kafka-service" CHANGED_MICROSERVICES.txt; then
                  gradle kafka-service:build;
                  fi
                - if grep -qw "notification-service" CHANGED_MICROSERVICES.txt; then
                  gradle notification-service:build;
                  fi
                - if grep -qw "payment-card-service" CHANGED_MICROSERVICES.txt; then
                  gradle payment-card-service:build;
                  fi
                - if grep -qw "private-gateway" CHANGED_MICROSERVICES.txt; then
                  gradle private-gateway:build;
                  fi
                - if grep -qw "rules-engine" CHANGED_MICROSERVICES.txt; then
                  gradle rules-engine:build;
                  fi
                - if grep -qw "third-party-config-service" CHANGED_MICROSERVICES.txt; then
                  gradle third-party-config-service:build;
                  fi
                - if grep -qw "transaction-control" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-control:build;
                  fi
                - if grep -qw "transaction-map" CHANGED_MICROSERVICES.txt; then
                  gradle transaction-map:build;
                  fi
                - if grep -qw "users-engine" CHANGED_MICROSERVICES.txt; then
                  gradle users-engine:build;
                  fi
              artifacts:
                - CHANGED_MICROSERVICES.txt
```


Answers:
author: Patrik S
body: Hello [@Ahmad Kabeer Shaukat](/t5/user/viewprofilepage/user-id/5440350) ,

thank you for reaching out to community!

The reason why it worked locally but not in pipelines is likely because pipelines execute a shallow (partial) clone of the repository by default, that only includes the last 50 commits.

As usually builds do not have a requirement to access all the history of the repository, doing a partial clone helps to speed up the build setup phase on pipelines, making the clone operation to be completed faster.

However, in use cases where you need access to the full repository in your build, you can configure the clone depth according to your requirements by adding the [clone depth attributes](https://support.atlassian.com/bitbucket-cloud/docs/git-clone-behavior/#Depth) in your YML file.  

Following is an example configuring pipelines to clone the full repository:

```
clone:
  depth: full

pipelines:
  default:
    - step:
        script:
          - ls $BITBUCKET_CLONE_DIR
```

Could you try including that parameter in your YML file and check if your commands to find the parent branch then work?

Should you have any questions, let us know.

Thank you, [@Ahmad Kabeer Shaukat](/t5/user/viewprofilepage/user-id/5440350) !

Patrik S

comments: author: Ahmad Kabeer Shaukat
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) i already tried this way   
but it does not work


author: Ahmad Kabeer Shaukat
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) can you try at your side


author: Patrik S
body: Hey [@Ahmad Kabeer Shaukat](/t5/user/viewprofilepage/user-id/5440350) ,

I'm not sure the command you're using to find the parent branch is accurate.

I say this because in Git a branch is essentially a pointer to a specific commit in the repository's history. When you create a new branch, you're not creating it from another branch per se, but rather from the specific commit to which that branch currently points.

This means that the history of how a branch was created (i.e., whether it was created from another branch or directly from a commit) is not recorded in the repository.

Git doesn't inherently track "parent branches", so it's tricky to determine the exact parent branch. In my research I've found the potential [thread](https://gist.github.com/joechrysler/6073741) where got that command from, and a couple of users reported it doesn't return accurate values, so it doesn't seem to be reliable using that command to try finding the parent branches.

Thank you, [@Ahmad Kabeer Shaukat](/t5/user/viewprofilepage/user-id/5440350) !

Patrik S


Tags: bitbucket-cloud

cloud