Bazaar: Automatic file modification on commit with the modification committed

倾然丶 夕夏残阳落幕 提交于 2019-12-11 02:38:29

问题


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.


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/6925956/bazaar-automatic-file-modification-on-commit-with-the-modification-committed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!