Bazaar:提交时自动修改文件并提交修改

发布于 2024-11-27 13:40:15 字数 547 浏览 2 评论 0原文

我希望集市在提交时将修订号写入已提交分支中的文件,以便此修改包含在提交中。

我查看了钩子,但 pre_commit 钩子仅在创建变更集后运行,因此它执行的修改不会提交。

我发现了一个相关问题: Bazaar:通过钩子提交之前修改文件内容? 但是,建议的 bzr-keywords 解决方案也不起作用,因为其写入转换未应用于提交:

``bzr commit`` 不会在之后隐式应用写入转换器 提交文件。如果这对于给定的插件有意义,则提供 内容过滤器,插件通常可以通过使用 “start_commit” 或 “post_commit” 钩子。

这让我回到了 pre_commit 挂钩问题。

我这样做的原因是:我的软件在编译时从版本文件中读取其版本。版本号由主号、分支号和修订号组成(如5.3.78)。我希望 bazaar 在提交时自动将实际版本写入版本文件。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美男兮 2024-12-04 13:40:15

您应该使用 start_commit 挂钩,因为这是在提交之前更改文件的唯一方法:http://doc.bazaar.canonical.com/bzr.2.3/en/user-reference/hooks-help.html#start-commit

开始提交

在树上执行提交之前调用。开始提交挂钩能够在提交发生之前更改树。 start_commit 使用正在执行提交的 bzrlib.mutabletree.MutableTree 来调用。

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.

白色秋天 2024-12-04 13:40:15

我有一个挂接到 start_commit 的插件脚本,名为 start_commit.py。每次提交发生时,这都会从项目树的基础调用名为 .startcommit 的 shell 脚本。我将其与账本数据一起使用,在每次提交之前转储所有余额以进行验证。

我没有写这个插件,我无法通过快速搜索找到它的来源,所以这里是源代码(~/.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")

如果有人知道,我会很高兴将此片段归功于原作者。否则我希望它有帮助!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文