2 # Bazaar plugin that runs etckeeper pre-commit when necessary
4 """Runs etckeeper pre-commit when necessary."""
7 from bzrlib.mutabletree import MutableTree
8 from bzrlib.errors import BzrError, NotLocalUrl
12 if not (hasattr(MutableTree, "hooks") and "start_commit" in MutableTree.hooks):
13 raise "Version of Bazaar installed does not support required hooks."
15 def etckeeper_startcommit_hook(tree):
16 abspath = getattr(tree, "abspath", None)
17 if abspath is None or not os.path.exists(abspath(".etckeeper")):
18 # Only run the commit hook when this is an etckeeper branch
20 ret = subprocess.call(["etckeeper", "pre-commit", abspath(".")])
22 raise BzrError("etckeeper pre-commit failed")
24 install_named_hook = getattr(MutableTree.hooks, 'install_named_hook', None)
25 if install_named_hook is not None:
26 install_named_hook('start_commit', etckeeper_startcommit_hook, 'etckeeper')
28 MutableTree.hooks.install_hook('start_commit', etckeeper_startcommit_hook)
29 MutableTree.hooks.name_hook(etckeeper_startcommit_hook, "etckeeper")
31 if __name__ == "__main__":
32 from distutils.core import setup
33 setup(name="bzr-etckeeper",
34 packages=["bzrlib.plugins.etckeeper"],
35 package_dir={"bzrlib.plugins.etckeeper":"etckeeper-bzr"})