如何检查钩子中的变基提交?

发布于 2024-12-20 02:07:29 字数 1625 浏览 2 评论 0原文

下面是我的提交消息挂钩,它适用于所有手动完成的合并和提交。当我尝试将它与 rebase 一起使用时,它不再显示

“此分支仅用于合并提交,无法直接在此处提交代码”。

它直接将代码提交为默认值。这不是合并吗?

钩子的需要是避免对默认分支的任何直接提交,并且只能对功能分支(默认以外的其他分支)进行提交。此外,如果分支中未遵循正确的命名约定,它将失败。

请让我知道如何允许变基提交或者我是否遗漏了任何东西?

import re

def commitmessage(ui, repo, *args, **kwargs):
    changectx = repo[kwargs['node']]
    if changectx.branch() == 'default' :
        if kwargs['parent2'] == '':
            if changectx.rev() == 0:
                return 0
            tagsfilepresent = 0
            totalfiles = 0
            for aFile in changectx.files():
                totalfiles = totalfiles + 1
                if aFile == '.hgtags':                  
                    tagsfilepresent = 1
            if totalfiles == 1 and tagsfilepresent == 1:
                return 0
            ui.write(changectx.branch() + ' This branch is only for Merge Commits, cannot commit the code directly here\n\n')
            return 1
        secondarybranchname = repo[kwargs['parent2']].branch()
        ui.write('Merging ' + secondarybranchname + ' to default\n')
        ui.write('Merge Commit Successful to default for ticket: ' +  secondarybranchname[1:] + '\n')
        return 0
    m = re.match('^t(\d{4,5})$', changectx.branch())
    if m:
        ui.write('Commit Successful to ' + m.group(1) + '\n')
        return 0
    else:
        ui.write('Branch name is not Acceptable, it should be either default or t(numeric)\n')
        return 1
    ui.write('If you think this special case is not handled here, please notify' + '\n')
    return 1

Below is my hook for commit message it works fine for all merges and commits which is manually done. When I try to use it with rebase it stop saying

"This branch is only for Merge Commits, cannot commit the code directly here" .

It is directly committing the code to default. Is that not a merge?

The need of hook is to avoid any direct commits to default branch and commit should only be done to the feature branch (other branches other than default). Also if a proper naming convention is not followed in the branch it will fail.

Please let me know how can I allow rebase commits or if I'm missing anything on the hook?

import re

def commitmessage(ui, repo, *args, **kwargs):
    changectx = repo[kwargs['node']]
    if changectx.branch() == 'default' :
        if kwargs['parent2'] == '':
            if changectx.rev() == 0:
                return 0
            tagsfilepresent = 0
            totalfiles = 0
            for aFile in changectx.files():
                totalfiles = totalfiles + 1
                if aFile == '.hgtags':                  
                    tagsfilepresent = 1
            if totalfiles == 1 and tagsfilepresent == 1:
                return 0
            ui.write(changectx.branch() + ' This branch is only for Merge Commits, cannot commit the code directly here\n\n')
            return 1
        secondarybranchname = repo[kwargs['parent2']].branch()
        ui.write('Merging ' + secondarybranchname + ' to default\n')
        ui.write('Merge Commit Successful to default for ticket: ' +  secondarybranchname[1:] + '\n')
        return 0
    m = re.match('^t(\d{4,5})
, changectx.branch())
    if m:
        ui.write('Commit Successful to ' + m.group(1) + '\n')
        return 0
    else:
        ui.write('Branch name is not Acceptable, it should be either default or t(numeric)\n')
        return 1
    ui.write('If you think this special case is not handled here, please notify' + '\n')
    return 1

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

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

发布评论

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

评论(1

我家小可爱 2024-12-27 02:07:29

似乎 RebaseExtension 手册清楚地显示了发生的情况。

Rebase 不是合并(不是 hg merge)。如果是,为什么要变基,为什么不合并?实际上,在大多数情况,它更接近于使用补丁队列从其位置获取一些修订并移动到另一个父级。对于修订历史记录,看起来您是否已经删除了多个修订并在新父级上提交了其他修订(非常相似但实际上不相同)。这就是为什么 kwargs['parent2'] == '' 为 true。

要了解历史记录到底发生了什么,您最好仔细阅读 RebaseExtension 手册

该操作涉及合并更改,以解决原始父级与目标父级之间的冲突。这就是当补丁队列无法提供帮助时扩展可用的原因:如果补丁中的任何块与目标文件内容不完全匹配,则无法通过队列应用补丁。但它只与文件内容有关,与历史记录无关。毕竟,TortoiseHg 也可以在更新操作期间合并本地更改,但它不是“hg merge”。

因此,变基与您禁止提交到“默认”分支的政策相矛盾,如果它停在这里,您似乎有一个相当好的钩子。我认为您应该使用常规合并命令将更改带到分支。

Seems like RebaseExtension manual clearly shows what happens.

Rebase is not merge (not hg merge). If it was, why rebase, why not merge? Actually, in most cases it is closer to using the patch queue to take some revisions from their place and move to another parent. For the revision history it looks like if you've stripped several revisions and committed others (very similar but not the same actually) over the new parent. That's why kwargs['parent2'] == '' is true.

To see what exactly happens to history, you'd better read the RebaseExtension manual carefully.

The operation involves merging of changes in order to resolve conflicts between the original parent and the target one. It is what makes the extension usable when the patch queue cannot help: you cannot apply a patch through the queue if any hunk in the patch doesn't match exactly with the target file contents. But it only relates to files contents, not to the history. After all, TortoiseHg can also merge local changes during an update operation, but it's not a 'hg merge'.

So, rebasing contradicts with your policy of prohibiting commits to the 'default' branch, and you seem to have a rather good hook if it stops here. I think you should use regular merge command to bring your changes to the branch.

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