(Bash-Mercurial)执行带有Mercurial Hook的bash脚本的执行问题吗?

时间:2018-11-29 10:17:26

标签: bash mercurial mercurial-hook

说明

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

商品设置

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

bash脚本背后的想法。

    服务器存储库上的
  1. incomming-hook,它触发一个bash脚本(bash-script1),然后又触发另一个bash脚本(bash-script2),该脚本检查某些条件并发送电子邮件。 bash-script 1有两个变量$ HG_NODE9set由Mercurial提供)和stderr输出。

下面的代码。

1。挂接Merurial服务器(/var/hg/repository/.hg/hgrc)

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

     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就会运行。

感谢您的支持

1 个答案:

答案 0 :(得分:0)

您面临的问题是,在传入挂钩中,交易尚未完成,因此$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 ,这样就无需再次查询存储库。