Over a million developers have joined DZone.

Keep Your Hook Script in Source Control: Python

DZone's Guide to

Keep Your Hook Script in Source Control: Python

· Web Dev Zone
Free Resource

Discover how to focus on operators for Reactive Programming and how they are essential to react to data in your application.  Brought to you in partnership with Wakanda

Commit your hook script (say pre-commit.sh) at the root of your project and include the installation instructions in your README/documentation to encourage all developers use it.

Installation is nothing more than:

ln -s ../../pre-commit.sh .git/hooks/pre-commit

Then everyone benefits from running the same set of tests before committing and updates are picked up automatically.

Stash unstaged changes before running tests

Ensure that code that isn't part of the prospective commit isn't tested within your pre-commit script. This is missed by many sample pre-commit scripts but is easily acheived with git stash:


# pre-commit.sh
git stash -q --keep-index

# Test prospective commit

git stash pop -q

The -q flags specify quiet mode.

Run your test suite before each commit


It's best to have a script (say run_tests.sh) that encapsulates the standard arguments to your test runner so your pre-commit script doesn't fall out of date. Something like:

# pre-commit.sh
git stash -q --keep-index
git stash pop -q
[ $RESULT -ne 0 ] && exit 1
exit 0


 where a sample run_tests.sh implementation for a Django project may look like:

# run_tests.sh
./manage.py test --settings=settings_test -v 2

Skip the pre-commit hook sometimes

Be aware of the --no-verify option to git commit. This bypasses the pre-commit hook when committing, which is useful is you have just manually run your test suite and don't need to see it run again when committing.

I use a git aliases to make this easy:

# ~/.bash_aliases
alias gc='git commit'
alias gcv='git commit --no-verify'

Search your sourcecode for debugging code

At some point, someone will try and commit a file containing

import pdb; pdb.set_trace()

or some other debugging code. This can be easily avoided using the pre-commit.sh file to grep the staged codebase and abort the commit if forbiden strings are found.

Here's an example that looks for console.log:

git diff --cached --name-only | \
    grep -E $FILES_PATTERN | \
    GREP_COLOR='4;5;37;41' xargs grep --color --with-filename -n $FORBIDDEN && echo 'COMMIT REJECTED Found "$FORBIDDEN" references. Please remove them before commiting' && exit 1

It's straightforward to extend this code block to search for other terms.

Learn how divergent branches can appear in your repository and how to better understand why they are called “branches".  Brought to you in partnership with Wakanda


Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}