0

DESCRIPTION

I want to send change-log messages to all colleagues when an push is made in a particular branch (test-branch) in a mercurial repository.

Mercurial Setup

  1. Local cloned repository ( Local Machine of User )
  2. Server Repository ( On a Server, Users can push or pull changes to here from their local repositories on their machines)
  3. Sand-box repository ( updated parallel to the Server to keep a track & have reference)

Idea behind the bash script.

  1. incomming-hook on Server Repository, It triggers a bash script( bash-script1) which inturns triggers another bash script(bash-script2) that checks some conditions and send emails. bash-script 1 has two variables $HG_NODE9set by mercurial) & stderr out-put from it.

the codes are below.

1.Hook on mercurial server (/var/hg/repository/.hg/hgrc)

    [hook]
     incoming=/home/user/incomming
  1. Bash-script 1 (/home/user/incomming)

     nohup /usr/bin/sudo -i -u user /home/user/bin/changelog.sh $HG_NODE &>/dev/null &
    
  2. Bash script 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
    

RESULT

I see that the hook is triggered as my BASH-SCRIPT1 gives output if i put some echos at top and bottom but my BASH-SCRIPT2 doesnt even start as it doesn't even echo at the very begining. But my BASH_SCRIPT2 runs if i run it manually with a know $HG_NODE.

Thank you for your support

Subha
  • 1
  • 3

1 Answers1

0

The problem you face is, that in the incoming hook the transaction is not yet done, thus $HG_NODE is not yet a valid changeset in the repository - which your script2 will need in order to work (the transmission is still being done, thus only available in the hook script, script1).

As you do not want to judge the changesets on their legibility, you might want to execute the operation in a hook when the transaction is already done, e.g. the txnclose hook:

"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.

In any case I suggest: have only the hook access the repository. And have it hand over all data you want to mail or process further to the script script2 as direct input so that it doesn't need to query the repo (again).

planetmaker
  • 5,884
  • 3
  • 28
  • 37
  • Thanks for the reply @planetmaker, i use now "changegroup - hook" with the idea you suggested to get "$HG_NODE_LAST". However the actual problem was there was an wrong address used between the server repository and Sand-box repository, It's not actually the issue from Mercurial. – Subha Dec 06 '18 at 10:04