【问题标题】:( Bash - Mercurial ) Problem with Execution of a bash script enabling it with Mercurial Hook?( Bash - Mercurial ) 使用 Mercurial Hook 启用 bash 脚本的执行问题?
【发布时间】:2018-11-29 10:17:25
【问题描述】:

说明

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

Mercurial 设置

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

bash 脚本背后的想法。

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

代码如下。

1.Hook mercurial 服务器 (/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。

感谢您的支持

【问题讨论】:

    标签: bash mercurial mercurial-hook


    【解决方案1】:

    您面临的问题是,在 incoming 钩子中,事务尚未完成,因此 $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。

    【讨论】:

    • 感谢@planetmaker 的回复,我现在使用“changegroup - hook”,您建议获得“$HG_NODE_LAST”。然而实际问题是服务器存储库和沙盒存储库之间使用了错误的地址,这实际上不是 Mercurial 的问题。
    猜你喜欢
    • 2011-12-14
    • 2023-04-03
    • 2018-05-04
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2021-08-15
    相关资源
    最近更新 更多