jeudi 28 octobre 2021

Bash script to check merged branches

I have a bash script that checks for all merged branches in the repositories.
I want to add some logic to it: in case there are no merged branches (either all branches were merged or deleted) it should print according message, but I can't figure out why the condition that I'm passing to it doesn't work(?).
I have tried different conditions (-z, -n), but it doesn't work.
Thank you.

script.sh

  for repo in "${REPOS[@]}"; do
    if [ -d "$repo" ]; then
      cd "$repo" || exit
      echo "Branches that were already merged into ${BRANCH_NAME} branch in $repo repository and could be deleted:"
      for branch in $(git branch -a --merged | grep -v ${BRANCH_NAME} | grep -Evw 'test' | awk 'BEGIN{FS="remotes/origin"} {print $2}') ; do
        if [ -z "$branch" ]; then
          echo "Nothing to delete."
        else
          echo "$branch" | cut -c 2-
          cd ../ || exit
        fi
      done;
    fi;
  done;

The output I'm receiving:

------------------------------------------
     Branches that were already merged into master branch in second-private repository and could be deleted:
     
     merged_branch

------------------------------------------

     Branches that were already merged into master branch in third-private repository and could be deleted:

    It prints out empty space but I want something like this:

    **no branches to delete** (message that I want to be displayed) 

------------------------------------------

Aucun commentaire:

Enregistrer un commentaire