Skip to content
Giovanni Mascellani edited this page May 25, 2013 · 6 revisions

Git hooks

When you work on the CMS git repository, please install pep8 and pyflakes and add the following pre-commit hook:

#!/usr/bin/env bash

COPYRIGHT="`date +%Y` `git config user.name`"
FILES=$(git diff --color=never --cached --name-status | awk '$1 $2 { print $2}' | grep --color=never -e \.py$)
    
if [ -n "$FILES" ]; then
    pep8 -r $FILES
    p8=$?
    pyflakes $FILES
    pf=$?
    if [ "$p8" != "0" -o "$pf" != "0" ]; then
        exit 1
    fi

    MISSCP=0
    for i in $FILES; do
        git show ":$i" | grep -q "$COPYRIGHT"
        CP=$?
        if [ "$CP" != 0 ]; then
            echo "Missing copyright for file $i."
            MISSCP=1
        fi
    done
    if [ "$MISSCP" != "0" ]; then
        echo "Some copyright notices are missing for you and for the current year."
        echo "If you have done non-trivial contribution in those files, consider"
        echo "adding your copyright to it."
        echo "Otherwise, use git commit --no-verify to commit."
        exit 1
    fi
fi

exit 0

You just have to write this content in .git/hooks/pre-commit and make it executable (chmod 755 .git/hooks/pre-commit). It does a few sanity checks in the files you're changing and warns you about potential errors and bad styles.

You're usually advised not to ignore these warnings. If you really want to do so (for example, for implementing them in a separate commit), pass the -n option to git commit.

Clone this wiki locally