Mercurial Hook-在提交前更改提交消息 [英] Mercurial Hook - change a commit message pre commit

查看:93
本文介绍了Mercurial Hook-在提交前更改提交消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑做了这个基本的钩子,以防止分支名称&提交消息bugID不匹配. https://gist.github.com/2583189

Edit Made this basic hook to prevent branch name & commit message bugID mismatches. https://gist.github.com/2583189

因此,基本上的想法是,如果分支名称类似于bug_123或feature_123,则该挂钩应在提交消息的末尾附加"BugID:xyz".但是,我很难找出如何执行此操作的方法,因为大多数pretxncommit的人都不想更改变更集的描述.

So basically the idea is that the hook should append " BugID:xyz" to the end of the commit messages if the branch name is like bug_123, or feature_123. However I'm having problems finding out how to do this, as most examples of pretxncommit people don't want to mutate the changeset description.

这是我到目前为止所拥有的.它使用正确的消息更新.hg/commit.save,但是此消息永远不会传输到提交.但是,它将显示在下一次提交的默认消息框中(tortoisehg).也许pretxncommit不是正确的钩子?

This is what I have so far. It updates .hg/commit.save with the right message, but this message is never transferred to the commit. It is however displayed in the default message box (tortoisehg) of the next commit. Perhaps pretxncommit isn't the right hook?

我可以使用预提交钩子,读取commit.save和repo ['tip'].branch()文件并进行更改 那,如果是的话,我将从哪里获取分支名称?

Could I use a precommit hook, read the commit.save and repo['tip'].branch() file and change that, if so where would I fetch the branch name from?

#
# Fogbugz automaticically add BugID:123 to commit messages based on branch names.
# Your branch name must be in the format feature_123_description or bug_123_description
#

import re
import mercurial, sys, os

_branch_regex = re.compile('(feature|bug|case|bugid|fogbugz)_(\d+)')
_commit_regex = re.compile(r'\b(?P<case>(review|case|bug[zs]?(\s| )*(id)?:?)s?(\s| )*([#:; ]| )+)((([ ,:;#]|and)*)(?P<bugid>\d+))+',re.I)

def pretxncommithook(ui, repo, **kwargs):
    ui.write('hook pretxncommithook running from fogbugz.py\n')
    """
    Checks a single commit message for adherence to commit message rules.

    To use add the following to your project .hg/hgrc for each
    project you want to check, or to your user hgrc to apply to all projects.

    [hooks]
    pretxncommit.fogbugz = python:fogbugz.pretxncommithook
    """
    hg_commit_message = repo['tip'].description()
    commit_has_bugid = _commit_regex.match(hg_commit_message) is not None

    match = _branch_regex.match(repo['tip'].branch())
    if match:
        hg_commit_message = hg_commit_message + ' BugID:'+ match.groups()[1]
            #hg_commit_message needs to be escaped for characters like >
        os.system('echo ' + hg_commit_message + ' > .hg/commit.save')

有一点不相关的注释是,如果Fogbugz/Kiln团队的任何人看到了此信息……请更新您的软件以读取分支名称,我不需要在每次该死的提交上都放置一个BugID:x.首先,这浪费了我的时间.其次,如果案例ID输入不正确,则不会引起很多混乱,而不会显示在错误中.很多开发人员针对每个错误/功能系统使用一个分支.这是我工作的公司政策. Fogbugz很烂.

On a slightly unrelated note, if anyone from the Fogbugz/Kiln team sees this... please update your software to read the branch name, I should not need to put a BugID:x on every damn commit. First of all it wastes my time. Secondly if a the case ID is typed incorrectly it will not show up on the bug without a lot of messing about. A lot of developers use a branch per bug/feature system. It's company policy where I work. Fogbugz sucks.

推荐答案

我认为这里的问题是pretxncommit挂钩是在您无法再进行任何更改的时候执行的.而且,此时您也无法真正获得提交消息,因为它没有在任何可访问的上下文对象上设置.

i think the problem here is that the pretxncommit hook is executed at a point where you can't really change anything anymore. And you can't really get the commit message at that point either, because it's not set on any context object accessible.

repo['tip'].description()不是指已提交的变更日志,而是指已提交的旧提示,即为repo[None],但是正如在源中进行的一些挖掘显示的那样,它不是要持久化的同一个上下文对象,因此不是指向更改它.

repo['tip'].description() doesn't refer to the changelog being committed but to old tip already committed, that would be repo[None], but as some digging in the source revealed it's not the same context object that's being persisted, so it's no point in changing it.

我能找到的唯一方法是使用一个较早的钩子-如precommit-并像这样对存储库的commitctx方法进行monkeypatch:

the only way i could find would be to use an earlier hook - like precommit - and monkeypatch the commitctx method of the repository like this:

def precommit_hook(repo, **kwargs):

    # keep a copy of repo.commitctx
    commitctx = repo.commitctx

    def updatectx(ctx, error):

        # check if `ctx.branch()` matches ...

        # update commit text
        ctx._text += " ... additional text"

        # call original
        return commitctx(ctx, error)

    # monkeypatch the commit method
    repo.commitctx = updatectx

这样,cou可以在提交上下文对象之前就访问它.

this way cou can access the context object just before it's committed.

这篇关于Mercurial Hook-在提交前更改提交消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆