I am trying to create a git pre-commit hook. If the current branch is already merged with the integration branch, I don't want to allow new commits to that branch. I have this:
#!/usr/bin/env bash
git fetch origin dev
current_branch=`git rev-parse --abbrev-ref HEAD`
branches="$(git branch --merged dev | tr '@squashed' '')"
say I have these two branches:
me/feature/x
me/feature/x@squashed
What I want to do is replace '@squashed', so I get this:
me/feature/x
me/feature/x
then if the current branch starts with either of those names, then exit with code 1.
The problem is:
I can't figure out how to use tr/sed to only replace '@squashed' if the branch name ends with that string.
I can't figure out how to loop over the elements and exit with non-zero if the current branch starts with one of the branch names in the list.
git branch
to an array than a scalar? Bash also has some amount of variable expansion capabilities, where you can replace text in variables.