Rev 2247: Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list. in file:///home/robertc/source/baz/in-review/branch-hook/

Robert Collins robertc at robertcollins.net
Tue Jan 30 10:41:07 GMT 2007


------------------------------------------------------------
revno: 2247
revision-id: robertc at robertcollins.net-20070130104104-wsikhrgretspg86m
parent: robertc at robertcollins.net-20070129165849-409f5714fa7ebe48
committer: Robert Collins <robertc at robertcollins.net>
branch nick: branch-hook
timestamp: Tue 2007-01-30 21:41:04 +1100
message:
  Remove the static DefaultHooks method from Branch, replacing it with a derived dict BranchHooks object, which is easier to use and provides a place to put the policy-checking add method discussed on list.
modified:
  bzrlib/branch.py               branch.py-20050309040759-e4baf4e0d046576e
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/branch_implementations/test_hooks.py test_hooks.py-20070129154855-blhpwxmvjs07waei-1
  bzrlib/tests/test_branch.py    test_branch.py-20060116013032-97819aa07b8ab3b5
  bzrlib/tests/test_selftest.py  test_selftest.py-20051202044319-c110a115d8c0456a
=== modified file 'bzrlib/branch.py'
--- a/bzrlib/branch.py	2007-01-29 16:58:49 +0000
+++ b/bzrlib/branch.py	2007-01-30 10:41:04 +0000
@@ -82,9 +82,7 @@
     base
         Base directory/url of the branch.
 
-    hooks: A dictionary mapping hook actions to callables to invoke.
-        e.g. 'set_rh':[] is the set_rh hook list. See DefaultHooks for
-        full details.
+    hooks: An instance of BranchHooks.
     """
     # this is really an instance variable - FIXME move it there
     # - RBC 20060112
@@ -108,16 +106,6 @@
             master.break_lock()
 
     @staticmethod
-    def DefaultHooks():
-        """Return a dict of the default branch hook settings."""
-        return {
-            'set_rh':[], # invoked whenever the revision history has been set
-                         # with set_revision_history. The api signature is
-                         # (branch, revision_history), and the branch will
-                         # be write-locked.
-            }
-
-    @staticmethod
     @deprecated_method(zero_eight)
     def open_downlevel(base):
         """Open a branch which may be of an old format."""
@@ -643,8 +631,28 @@
         return checkout.create_workingtree(revision_id)
 
 
+class BranchHooks(dict):
+    """A dictionary mapping hook name to a list of callables for branch hooks.
+    
+    e.g. ['set_rh'] Is the list of items to be called when the
+    set_revision_history function is invoked.
+    """
+
+    def __init__(self):
+        """Create the default hooks.
+
+        These are all empty initially, because by default nothing should get
+        notified.
+        """
+        dict.__init__(self)
+        # invoked whenever the revision history has been set
+        # with set_revision_history. The api signature is
+        # (branch, revision_history), and the branch will
+        # be write-locked.
+        self['set_rh'] = []
+
 # install the default hooks into the class.
-Branch.hooks = Branch.DefaultHooks()
+Branch.hooks = BranchHooks()
 
 
 class BranchFormat(object):

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-01-29 16:58:49 +0000
+++ b/bzrlib/tests/__init__.py	2007-01-30 10:41:04 +0000
@@ -574,7 +574,7 @@
         self.addCleanup(self._restoreHooks)
         # this list of hooks must be kept in sync with the defaults
         # in branch.py
-        bzrlib.branch.Branch.hooks = bzrlib.branch.Branch.DefaultHooks()
+        bzrlib.branch.Branch.hooks = bzrlib.branch.BranchHooks()
 
     def _silenceUI(self):
         """Turn off UI for duration of test"""

=== modified file 'bzrlib/tests/branch_implementations/test_hooks.py'
--- a/bzrlib/tests/branch_implementations/test_hooks.py	2007-01-29 16:58:49 +0000
+++ b/bzrlib/tests/branch_implementations/test_hooks.py	2007-01-30 10:41:04 +0000
@@ -14,7 +14,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
-"""Tests for branch hooking operations."""
+"""Tests that branch classes implement hook callouts correctly."""
 
 from bzrlib.branch import Branch
 from bzrlib.tests import TestCaseWithMemoryTransport

=== modified file 'bzrlib/tests/test_branch.py'
--- a/bzrlib/tests/test_branch.py	2007-01-29 16:58:49 +0000
+++ b/bzrlib/tests/test_branch.py	2007-01-30 10:41:04 +0000
@@ -166,13 +166,12 @@
 
 class TestHooks(TestCase):
 
-    def test_set_rh_in_defaults(self):
-        """Check that the set_rh hook exists in the defaults."""
-        default_hooks = bzrlib.branch.Branch.DefaultHooks()
-        self.assertTrue("set_rh" in default_hooks,
-            "set_rh not in %s" % default_hooks)
+    def test_constructor(self):
+        """Check that creating a BranchHooks instance has the right defaults."""
+        hooks = bzrlib.branch.BranchHooks()
+        self.assertTrue("set_rh" in hooks, "set_rh not in %s" % hooks)
 
-    def test_set_rh_in_actual_hooks(self):
-        """Check that the set_rh hook exists in the saved hook set."""
-        self.assertTrue("set_rh" in self._preserved_hooks,
-            "set_rh not in %s" % self._preserved_hooks)
+    def test_installed_hooks_are_BranchHooks(self):
+        """The installed hooks object should be a BranchHooks."""
+        # the installed hooks are saved in self._preserved_hooks.
+        self.assertIsInstance(self._preserved_hooks, bzrlib.branch.BranchHooks)

=== modified file 'bzrlib/tests/test_selftest.py'
--- a/bzrlib/tests/test_selftest.py	2007-01-29 16:58:49 +0000
+++ b/bzrlib/tests/test_selftest.py	2007-01-30 10:41:04 +0000
@@ -855,7 +855,7 @@
 
     def test_hooks_sanitised(self):
         """The bzrlib hooks should be sanitised by setUp."""
-        self.assertEqual(bzrlib.branch.Branch.DefaultHooks(),
+        self.assertEqual(bzrlib.branch.BranchHooks(),
             bzrlib.branch.Branch.hooks)
 
     def test__gather_lsprof_in_benchmarks(self):



More information about the bazaar-commits mailing list