Getting local branch name always in Git pre-commit hook

Viewed 285

I have a Git pre-commit hook, which fetches local branch name and add checks with my pre defined Regex and alerts developer to if not matching.

I am finding the local branch name using

local_branch="$(git rev-parse --abbrev-ref HEAD)"

It is working good during normal commit process. But in case, if I did a wrong commit message and I will edit it using the option "Rebase childern of abc interactively" in soucetree and edit the message. At this time my match condition on regex is failing as the local branch is not coming as expected. In happy case it is coming as origin/feature/XYZ-01 but while editing message it is coming as just "HEAD"

Tried using git branch --show-current and its giving nothing. Also tried, git branch | sed -n '/\* /s///p' and its giving "no branch, rebasing feature/XYZ-01"

Is there a way that I can get current branch in all cases like regular/Rebase etc. How to get local branch name during rebase and what other cases also should I need to consider?

0 Answers
Related