Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git commit hooks per branch

Tags:

I'm working on getting into some more advanced usage of git, and I think hooks are the way that I want to go, perhaps somebody can give me some advice here.

My plan is to have a git repository with 3 branches (development, staging, and production). I want commits to each of these 3 branches to trigger a different script post-commit.

Does git have the capability to do this or am I barking up the wrong tree?

Thanks in advance.

like image 870
Eugene Avatar asked Jun 16 '11 13:06

Eugene


People also ask

Can Git hooks be committed?

The commit-msg hook takes one parameter, which again is the path to a temporary file that contains the commit message written by the developer. If this script exits non-zero, Git aborts the commit process, so you can use it to validate your project state or commit message before allowing a commit to go through.

How do pre-commit hooks work?

pre-commit hooks are a mechanism of the version control system git. They let you execute code right before the commit. Confusingly, there is also a Python package called pre-commit which allows you to create and use pre-commit hooks with a way simpler interface.

What are commit hooks?

The commit-msg hook is much like the prepare-commit-msg hook, but it's called after the user enters a commit message. This is an appropriate place to warn developers that their message doesn't adhere to your team's standards. The only argument passed to this hook is the name of the file that contains the message.


2 Answers

in a post-commit hook you could do the following:

if [ `git rev-parse --abbrev-ref HEAD` == "development" ]; then    echo "development-script" elif [ `git rev-parse --abbrev-ref HEAD` == "staging" ]; then    echo "staging-script" elif [ `git rev-parse --abbrev-ref HEAD` == "production" ]; then    echo "production-script" fi 
like image 132
Simon Stender Boisen Avatar answered Oct 15 '22 23:10

Simon Stender Boisen


I had written a script for myself to do this functionality.

https://github.com/fotuzlab/githubdump-php

Host this file on your server, preferably repo root and define the url in github webhooks. Change 'allcommits' on line 8 with your branch name and add your code/function at line 18.

You will need separate files and webhooks for all your 3 instances.

Hope this helps!

like image 37
fotuzlab Avatar answered Oct 15 '22 23:10

fotuzlab