#!/bin/sh
set -e
echo "$@"
# put me in the project root, call me ".precommit".
# And put this here-document in ~/.bazaar/plugins/precommit_script.py:
<<EOF
import os
import subprocess
from bzrlib.mutabletree import MutableTree
from bzrlib import errors

def start_commit_hook(*_):
    """This hook will execute '.precommit' script from root path of the bazaar
    branch. Commit will be canceled if precommit fails."""

    # this hook only makes sense if a precommit file exist.
    if not os.path.exists(".precommit"):
        return
    try:
        subprocess.check_call(os.path.abspath(".precommit"))
    # if precommit fails (process return not zero) cancel commit.
    except subprocess.CalledProcessError:
        raise errors.BzrError("pre commit check failed.")

MutableTree.hooks.install_named_hook('start_commit', start_commit_hook,
                                     'Run "precommit" script on start_commit')
EOF

make check-format # or whatever
