【问题标题】:Disallow hg push -f - but allow hg pull creating new head禁止 hg push -f - 但允许 hg pull 创建新头
【发布时间】:2012-10-09 11:35:26
【问题描述】:

作为Mercurial: enforce "hg pull -u" before "hg commit" 的后续行动 我已经开始使用钩子了

[hooks]
pretxnchangegroup.forbid_2heads = /usr/local/bin/forbid_2head.sh

forbid_2head.sh 看起来像这样

#!/bin/bash 
BRANCH=`hg branch`
COUNT=`hg heads --template '{branch}|{rev}\n' | grep ^${BRANCH} | wc -l` 
if [ "$COUNT" -ne "1" ] ; then 
   echo "=========================================================="
   echo "Trying to push more than one head, which is not allowed"
   echo "You seem to try to add changes to an old changeset!"
   echo "==========================================================" 
   exit 1 
fi 
exit 0

它是在http://tutorials.davidherron.com/2008/10/forbidding-multiple-heads-in-shared.html 找到的脚本的派生 我确实允许多个命名分支。

我现在的问题是

  • 它停止了我想要的 hg push -f
  • 如果有传入的变更集并且我有提交传出,它也会停止 hg pull。这确实很糟糕

我可以以任何方式重复使用相同的脚本,但更改挂钩设置并停止“hg push -f”吗? 或者我可以在 forbid_2head.sh 中知道这是在运行 push 还是 pull 命令?

【问题讨论】:

    标签: mercurial mercurial-hook


    【解决方案1】:

    首先,脚本并不完全正确:它只是计算当前在服务器上签出的分支中的头数(hg branch)报告。你可以通过使用来改进它

    hg heads tip
    

    获取tip 分支的负责人。但是有人可能一次将变更集推送到多个分支上,所以你真正想要的是

    hg heads --template '{branch}\n' $HG_NODE:tip
    

    $HG_NODE:tip(在尚未提交的事务中推送的变更集)触及的分支查找分支头。然后,您可以将其与

    hg log --template '{branch}\n' -r $HG_NODE:tip | sort -u
    

    变更组触及的分支。

    如果您不想允许现有的多个头,那么您可以将上述简化为只是

    $(hg heads --template 'x' | wc -c) -eq $(hg branches | wc -l)
    

    它只是测试分支头的数量是否等于分支的数量——即每个命名分支恰好有一个分支头。

    除此之外,让我提一下$HG_SOURCE。该环境变量由 Mercurial 在运行钩子时设置:如果更改组使用直接文件系统访问被推送到存储库,则其值为 push,如果更改组通过 SSH 或 HTTP 进入,则值为 serve .见the Mercurial book

    所以,总而言之,我相信这是一个很好的“禁止多头”脚本:

    #!/bin/sh
    HEADS=$(hg heads --template 'x' | wc -c)
    BRANCHES=$(hg branches | wc -l)
    test $HG_SOURCE = 'serve' -a $HEADS -ne $BRANCHES
    

    【讨论】:

      猜你喜欢
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多