Git Post Commit hooks to find name of branch

1.9k views Asked by At

I want to create a post commit hook through which I need to send a notification to the committing developer that branch pushed is not following our naming convention. So what I would like to know is:

  1. How to read the pushed branch name
  2. How to check if the branch name follows a certain pattern like "origin/name/issueid"

We want to inform user when their branch isn't follow the naming convention.

2

There are 2 answers

1
lostphilosopher On BEST ANSWER
  1. Assuming you decided to go the post-receive route that jthill suggests you should look at this answer to get the branch name: https://stackoverflow.com/a/13057643/1504372. There's other solutions there that might be more appropriate depending on your specific situation. Here's the official doco: http://git-scm.com/docs/githooks. (Start by trying to echo $1 and oldrev to get a feel for what's available to you.)
  2. Your second issue calls for a regex. http://www.itworld.com/article/2693361/unix-tip-using-bash-s-regular-expressions.html
0
Jyotirmaya Vasaniwal On

This code will be written in post-commit of git hooks i.e. vim .git/hooks/post-commit

if git branch --show current | grep developer
then
        echo " i am developer"
        git push -u origin developer
else
        echo "i am master"
        git push -u origin master
fi

The above code will push into developer branch if commit is done by developer else push into master branch. In my git , I had two branches : -master (main branch) -developer (feature branch)