Skip to content

Developerhints

wil93 edited this page Jan 4, 2015 · 8 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.

Branch-specific configuration files

If you'd like to keep different configuration files (config/cms.conf, config/cms.ranking.conf) for each branch (e.g. you need an empty db to test a new feature but don't want to lose your current db), you can use this trick: create multiple configuration files having @branchname as a suffix (e.g config/cms.conf@master, config/cms.conf.ranking@master) and use this post-checkout hook:

#!/bin/sh

current_branch=$(git rev-parse --abbrev-ref HEAD)
cd config
ln -s -f cms.conf@$current_branch cms.conf
ln -s -f cms.ranking.conf@$current_branch cms.ranking.conf

This will "change" your CMS configuration each time you do a git checkout branchname. Make sure to do a backup of your config folder before trying this method, because if misused it can overwrite your configuration. Note that the "suffixed" configuration file must exist (you can however adapt the script to, say, use the @master version when there's no @branchname version).