The Ops Community ⚙️

Jason Purdy
Jason Purdy

Posted on

Local git commit hooks

I mentioned in a previous post about how GitHub can automatically check your code for syntax errors whenever you push an update to the server, but you can also catch those on your local development box before you commit code. You can just add a script in the code repo's .git/hooks/pre-commit path. What we do is have our script in a docs directory and then in our composer.json file, when the developer is installing/updating the php libraries, there is a script that (re)establishes the commit hook:

...
        "post-install-cmd": [
            "rm -rf .git/hooks", "ln -s ../docs/hooks .git/hooks"
        ],
        "post-update-cmd": [
            "rm -rf .git/hooks", "ln -s ../docs/hooks .git/hooks"
        ],
...
Enter fullscreen mode Exit fullscreen mode

As for the script itself, I cannot take credit for it. I think it originally came from this post from Carlos Buenosvinos. It doesn't look like that post is still working, but here is the script I use:



This is for a Drupal project, which already has Symfony installed, so you may need to require that (and there may be other requirements), if you aren't already using that.

You can see by some of the commented-out areas, I'm not doing the full verification because I just haven't had the time to work on that. I kind of want to redo it using Robo, but that's a project for another day.

There are times where you want to bypass the verification. If you're in a hurry (not a good sign ;)), or if there's something wrong with the verification hook, you can skip the verification with the --no-verify argument:

git commit -m 'gotta git this code in place! ;)' --no-verify

Oldest comments (0)