Patchwork [3,of,6,events,v3] exchange: add pushbegin event

login
register
mail settings
Submitter Gregory Szorc
Date Sept. 28, 2014, 9:01 p.m.
Message ID <33cbfe6b815d88ebee22.1411938101@vm-ubuntu-main.gateway.sonic.net>
Download mbox | patch
Permalink /patch/6016/
State Changes Requested
Headers show

Comments

Gregory Szorc - Sept. 28, 2014, 9:01 p.m.
# HG changeset patch
# User Gregory Szorc <gregory.szorc@gmail.com>
# Date 1408423943 25200
#      Mon Aug 18 21:52:23 2014 -0700
# Node ID 33cbfe6b815d88ebee229e372af69c8276fce29c
# Parent  9fbeb3c3c9d508813398d6fc800b253cd1698407
exchange: add pushbegin event

Extensions may wish to influence what is being done by a push
operation. To facilitate this, we establish the "pushbegin"
event, which fires immediately after a pushoperation is created
and gives extensions the ability to inspect and/or change
the pushoperation before any additional action is taken.

Patch

diff --git a/mercurial/exchange.py b/mercurial/exchange.py
--- a/mercurial/exchange.py
+++ b/mercurial/exchange.py
@@ -154,8 +154,9 @@  def push(repo, remote, force=False, revs
         we have outgoing changesets but refused to push
       - other values as described by addchangegroup()
     '''
     pushop = pushoperation(repo, remote, force, revs, newbranch)
+    repo.events.pushbegin(pushop=pushop)
     if pushop.remote.local():
         missing = (set(pushop.repo.requirements)
                    - pushop.remote.local().supported)
         if missing:
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -1782,4 +1782,14 @@  def islocal(path):
     return True
 
 class localrepoevents(util.eventmanager):
     '''Defines events for localrepository instances.'''
+
+    def pushbegin(pushop):
+        '''Event that signals a push operation is about to be performed.
+
+        Event handlers receive an exchange.pushoperation as a single argument.
+
+        Expected use cases include policy checking a push's parameters and
+        modifying what a push attempts to do.
+        '''
+
diff --git a/tests/test-events.t b/tests/test-events.t
new file mode 100644
--- /dev/null
+++ b/tests/test-events.t
@@ -0,0 +1,30 @@ 
+  $ cat >> events.py << EOF
+  > from mercurial import exchange
+  > def pushbegin(pushop):
+  >     assert isinstance(pushop, exchange.pushoperation)
+  >     pushop.ui.write('pushbegin %s\n' % pushop.repo.path)
+  > def reposetup(ui, repo):
+  >     repo.events.pushbegin += pushbegin
+  > EOF
+
+  $ hg init server
+  $ hg init client
+  $ cd client
+  $ cat >> .hg/hgrc << EOF
+  > [extensions]
+  > events = $TESTTMP/events.py
+  > EOF
+
+Ensure push-related events are firing
+
+  $ touch foo
+  $ hg commit -A -m 'initial'
+  adding foo
+  $ hg push ../server
+  pushing to ../server
+  pushbegin $TESTTMP/client/.hg
+  searching for changes
+  adding changesets
+  adding manifests
+  adding file changes
+  added 1 changesets with 1 changes to 1 files