(Bash-Mercurial)使用Mercurial Hook执行Bash脚本有问题吗



描述

当在mercurial存储库中的特定分支(测试分支(中进行推送时,我想向所有同事发送更改日志消息。

Mercurial设置

  1. 本地克隆存储库(用户的本地机器(
  2. 服务器存储库(在服务器上,用户可以将更改从其计算机上的本地存储库推送或拉到此处(
  3. 沙盒存储库(与服务器并行更新以保持跟踪和参考(

bash脚本背后的想法

  1. 在服务器存储库上插入钩子,它触发了一个bash脚本(bash-script1(,该脚本直接触发了另一个检查某些条件并发送电子邮件的bash-script2。bash脚本1有两个变量$HG_NODE9set by mercurial(&stderr从中输出

代码如下。

1.挂接mercurial服务器(/var/hg/repository/.hg/hgrc(

[hook]
incoming=/home/user/incomming
  1. Bash脚本1(/home/user/inconting(

    nohup /usr/bin/sudo -i -u user /home/user/bin/changelog.sh $HG_NODE &>/dev/null &
    
  2. Bash脚本2(/home/user/bin/changelog.sh(

    #we go to the sandbox repository directory
    cd /home/user/hg/repository
    L_BRANCH_SANDBOX=$($HG branches | $GREP testbranch | $SORT -Vr |$AWK '{print $1}')
    P_BRANCH=$($HG log -r $HG_NODE | $HEAD -n 4   | $GREP branch: | $AWK '{print $2}')
    if [[ "$L_BRANCH_SANDBOX" == "$P_BRANCH" ]] ; then
    Some commands and send mail
    fi
    

结果

我看到,如果我在顶部和底部放置一些回声,当我的BASH-SCRIPT1给出输出时,钩子被触发,但我的BASH-SCRIPT2甚至没有启动,因为它甚至在一开始就没有回声。但是,如果我使用已知的$HG_NODE手动运行BASH_SCRIPT2,它就会运行。

感谢您对的支持

您面临的问题是,在传入钩子中,事务尚未完成,因此$HG_NODE在存储库中还不是有效的变更集-您的script2需要它才能工作(传输仍在进行中,因此仅在钩子脚本script1中可用(。

由于您不想根据变更集的易读性来判断它们,因此您可能希望在事务已经完成时在挂钩中执行操作,例如txnclose挂钩:

"hooks.txnclose"
Run after any repository transaction has been committed. At this point,
the transaction can no longer be rolled back. The hook will run after
the lock is released. See 'hg help config.hooks.pretxnclose' for details
about available variables.
"hooks.pretxnclose"
Run right before the transaction is actually finalized. Any repository
change will be visible to the hook program. This lets you validate the
transaction content or change it. Exit status 0 allows the commit to
proceed. A non-zero status will cause the transaction to be rolled back.
The reason for the transaction opening will be in "$HG_TXNNAME", and a
unique identifier for the transaction will be in "HG_TXNID". The rest of
the available data will vary according the transaction type. New
changesets will add "$HG_NODE" (the ID of the first added changeset),
"$HG_NODE_LAST" (the ID of the last added changeset), "$HG_URL" and
"$HG_SOURCE" variables.  Bookmark and phase changes will set
"HG_BOOKMARK_MOVED" and "HG_PHASES_MOVED" to "1" respectively, etc.

在任何情况下,我建议:只让钩子访问存储库。让它将您想要邮寄或进一步处理的所有数据作为直接输入移交给脚本script2,这样它就不需要(再次(查询repo。

最新更新