预提交 mercurial 钩子以停止提交到错误的分支 [英] precommit mercurial hook to stop commits to the wrong branch

查看:43
本文介绍了预提交 mercurial 钩子以停止提交到错误的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Mercurial 存储库中有一个软件.

I have a piece of software in a Mercurial repository.

我将我的软件项目打包为 Debian 软件包.似乎这样做的标准方法是有一个Debian 软件包文件的单独分支,位于 debian 子目录中.

I'm packaging my software project as a Debian package. It seems the standard way to do this is to have a separate branch for the Debian package files, which live in the debian sub-directory.

我一直遇到的一个问题是我忘记了我是哪个分支并且不小心提交到错误的分支.有时候是这样的经常,而且真的很烦人.当这种情况发生时,我通常在意识到问题之前推送到远程,然后有手动修复本地和远程存储库,即痛苦.

One problem I keep having is that I forget which branch I am on and accidentally commit to the wrong branch. This happens frequently, and is really annoying. When this happens I usually push to remote before realising the problem, and then have to fix up the local and remote repositories manually, which is a pain.

我能想到的唯一选择是有一个预提交钩子如果我试图提交到错误的分支,它就会中止.

The only option I can think of is to have a pre-commit hook that aborts if I am trying to make commits to the wrong branch.

具体来说,假设主分支名为default,分支名为default包含 Debian 文件的文件称为 debian.然后我想提交到 default只有当提交中的文件都不是来自 debian 时,分支才能成功目录.我希望提交到 debian 目录只有在所有提交中的文件位于 debian 目录中.

To be concrete, let's say the main branch is called default and the branch containing Debian files is called debian. Then I want commits to the default branch to succeed only if none of the files in the commit are from the debian directory. I want commits to the debian directory to succeed only if all the files in the commit are in the debian directory.

我花了一些时间阅读有关 Mercurial Hooks 的章节并查看示例在 Hg Book 中,但仍然不知道如何解决这个问题.我确实得到了强烈的印象对于这样的事情,我应该调用外部 Python 脚本,可能在 .hg/.

I spent some time reading the chapter on Mercurial Hooks and going through the examples in the Hg Book, but still have no idea how to go about this. I did get the strong impression that for something like this I should be calling out to an external Python script, probably in .hg/.

推荐答案

是的,您发现 precommit 钩子可以做到这一点.如果您想在 bash 中执行此操作,则可以使用以下方法:

Yeah, you're spot on that a precommit hook can do this. If you wanted to do it in bash you could go with something like:

#!/bin/bash
revs=$(hg log -r "$HG_NODE:tip" --template '{rev} ') #Intentional space after {rev}
rc=0
for rev in $revs
do
    files=$(hg log -r $rev --template '{files}')
    #Above will include discards. So you cannot 'hg cat' them all. So you may want
    #  files=$(hg log -r $rev --template '{file_mods} {file_adds}')
    branch=$(hg log -r $rev --template '{branch}')
    for file in $files
    do
        if [ branch == "debian" ] &&  [ "$(echo $file | grep -v "debian")" != "" ] ; then
          echo "ERROR: Non debian file in debian branch."
          exit 1
        fi
        if [ branch != "debian" ] &&  [ "$(echo $file | grep "debian")" != "" ] ; then
          echo "ERROR: debian file in non-debian branch."
          exit 1
        fi
    done
done
exit $rc

那些 if/grep 行几乎肯定是错误的,但你明白了.

Those if/grep lines are almost certainly wrong, but you get the picture.

这篇关于预提交 mercurial 钩子以停止提交到错误的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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