Bazaar: Automatic file modification on commit with the modification committed
I would like bazaar to write revision number on commit to a file in the committed branch so that this modification is included in the commit.
I looked at hooks but the pre_commit hook is only开发者_开发问答 run after the changeset is created, thus the modification performed by it is not committed.
I found a related question: Bazaar: Modify file content before commit via hook? , however, the proposed bzr-keywords solution does not work either as its write conversion is not applied on commit:
``bzr commit`` does not implicitly apply write converters after comitting files. If this makes sense for a given plugin providing a content filter, the plugin can usually achieve this effect by using a ``start_commit`` or ``post_commit`` hook.
which gets me back to the pre_commit hook problem.
My reason for doing this: my software reads its version from a version file on compilation. The version is composed of main number, branch number and revision number (eg 5.3.78). I want bazaar to write the actual version to the version file automatically on commit.
You should use start_commit
hook because this is the only way to change file before commit: http://doc.bazaar.canonical.com/bzr.2.3/en/user-reference/hooks-help.html#start-commit
start_commit
Called before a commit is performed on a tree. The start commit hook is able to change the tree before the commit takes place. start_commit is called with the bzrlib.mutabletree.MutableTree that the commit is being performed on.
I have a plugin script that hooks into start_commit called start_commit.py. This calls a shell script named .startcommit from the base of the project tree each time a commit occurs. I use this with Ledger data to dump all my balances for verification before each commit.
I didn't write this plugin, and I can't locate where I got it from with a quick search, so here's the source (~/.bazaar/plugins/start_commit.py):
from bzrlib import errors, mutabletree
def start_commit(tree):
"""This hook will execute tree/on-commit."""
import os,subprocess
from bzrlib import errors
abspath = tree.abspath('.startcommit')
# if there is no on-commit, bail
if not os.path.exists(abspath):
return
try:
subprocess.check_call(abspath)
# if precommit fails (process return not zero) cancel commit.
except subprocess.CalledProcessError:
raise errors.BzrError("on-commit failed")
mutabletree.MutableTree.hooks.install_named_hook('start_commit', start_commit,
"tree on-commit")
If someone knows, I'd be happy to credit the original author for this snippet. Otherwise I hope it helps!
精彩评论