26 de marzo de 2014

Create a global GIT hook to check Flake8 before each commit

First, let's install Flake8:

Note It is important to set the right version when installing Flake8, as there there are some versions that are bugged and will prevent the GIT hook to work (like the 2.1.0 version).

Now, let's setup global git commit hooks:

1. Enable git templates:

This tells git to copy everything in ~/.git-templates to your per-project .git/ directory when you run git init

2. Create a directory to hold the global hooks:

3. Write your hooks in ~/.git-templates/hooks.

For example, here's a post-commit hook (located in ~/.git-templates/hooks/post-commit):

4. Make sure the hook is executable.

5. Re-initialize git in each existing repo you'd like to use this in:

NOTE if you already have a hook defined in your local git repo, this will not overwrite it.

Now, let's create a global git pre-commit hook:

Finally, let's take it for a spin!

Go to some of your repos and re-initialize it to take the pre-commit hook:

Now edit some of your Python files, introducing some violation (like a >80 columns line, only one blank line between function/class definitions, etc.) and try to commit it: