pretxnchangegroup hook fails because it can't acquire lock

Simon King simon at simonking.org.uk
Wed Nov 6 06:14:52 CST 2013


On Wed, Nov 6, 2013 at 3:54 AM, Mick Jordan <mick.jordan at oracle.com> wrote:
> On 11/5/13 7:48 PM, Mick Jordan wrote:
>
> Actually it's worse than that. After the commands.push returns I call (in a
> sub-process):
>
> hg -q -R repo out --template '{node|short}:{author}' repo-target
>
> This should return 0 as there should be outgoing changes as repo-target has
> rolled back, but it returns 1. If I run the command from a shell after the
> it's all done, it certainly returns 0 and shows me the changeset. The
> documentation on  pretxnchangegroup at
> http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html
> is very unclear as several people have commented.
>
> By adding in a sleep before the above call, I can run the same command in a
> shell and get an rc of 1 (i.e. repo == repo_target) and at the same time see
> 'hg tip' show me the revision before the changeset!
>

I've lost sight of the setup here. Could we recap a little bit?

You have 3 repositories, we'll call them local, remote-1 and remote-2.

When you push from local to remote-1, a pretxnchangegroup hook in
remote-1 attempts to push the same changes to remote-2.

A pretxnchangegroup hook in remote-2 checks the new changes against
some rules. If the checks fail, the push from remote-1 to remote-2 is
rolled back.

The intention is then that the push from local to remote-1 is also
rolled back, but at the moment that's not working, and you are getting
confusing results from the pretxnchangegroup hook in remote-1. Is that
correct?

I wrote a script to generate the steps that we are talking about. It
turns out that commands.push will raise an exception if a hook at the
remote end fails (at least in the version I'm currently running,
2.5.2). If I catch the exception and then run 'hg outgoing', I get a
return code of 0. Perhaps you could tweak this script to demonstrate
the problem that you're having?

Simon

#!/bin/bash
set -o errexit
set -o xtrace

hg init local
hg init remote1
hg init remote2

# Set up remote1 with a hook that pushes to remote2
cat >remote1hook.py <<EOF
import subprocess
import mercurial.commands
def pushwhilelocked(ui, repo, **kwargs):
    try:
        pushresult = mercurial.commands.push(ui, repo, '../remote2')
    except Exception, e:
        pushresult = 'exception: %s' % e
    ui.write('In remote1, push returned %s\n' % pushresult)
    outgoingresult = mercurial.commands.outgoing(ui, repo, '../remote2')
    ui.write('In remote1, outgoing returned %s\n' % outgoingresult)
    subprocessresult = subprocess.call(['hg', 'outgoing', '../remote2'])
    ui.write('In remote1, subprocess outgoing returned %s\n' % subprocessresult)
    return pushresult
EOF

cat >remote1/.hg/hgrc <<EOF
[hooks]
pretxnchangegroup.push = python:../remote1hook.py:pushwhilelocked
EOF


# set up remote2 to reject pushes
cat >remote2/.hg/hgrc <<EOF
[hooks]
pretxnchangegroup.reject = echo you shall not pass && false
EOF

# now create a changeset in local and try to push it to remote1
cd local/
touch a
HGRCPATH= hg ci --user test -Am0
HGRCPATH= hg push ../remote1/





And here's the output:

+ hg init local
+ hg init remote1
+ hg init remote2
+ cat
+ cat
+ cat
+ cd local/
+ touch a
+ HGRCPATH=
+ hg ci --user test -Am0
adding a
+ HGRCPATH=
+ hg push ../remote1/
pushing to ../remote1/
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
pushing to ../remote2
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files
you shall not pass
transaction abort!
rollback completed
In remote1, push returned exception: pretxnchangegroup.reject hook
exited with status 1
comparing with ../remote2
searching for changes
changeset:   0:8809529c7ed0
tag:         tip
user:        test
date:        Wed Nov 06 12:07:30 2013 +0000
summary:     0

In remote1, outgoing returned 0
comparing with ../remote2
searching for changes
changeset:   0:8809529c7ed0
tag:         tip
user:        test
date:        Wed Nov 06 12:07:30 2013 +0000
summary:     0

In remote1, subprocess outgoing returned 0
transaction abort!
rollback completed
abort: pretxnchangegroup.push hook failed


More information about the Mercurial mailing list