Rev 2560: (robertc) Show the names of commit hooks during commit. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu Jun 28 08:08:30 BST 2007


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 2560
revision-id: pqm at pqm.ubuntu.com-20070628070827-h5s313dg5tnag9vj
parent: pqm at pqm.ubuntu.com-20070628051830-re1rvfieof32tnux
parent: robertc at robertcollins.net-20070628063203-lr39jqb65223ts09
committer: Canonical.com Patch Queue Manager<pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2007-06-28 08:08:27 +0100
message:
  (robertc) Show the names of commit hooks during commit.
added:
  bzrlib/tests/test_hooks.py     test_hooks.py-20070628030849-89rtsbe5dmer5npz-1
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/commit.py               commit.py-20050511101309-79ec1a0168e0e825
  bzrlib/hooks.py                hooks.py-20070325015548-ix4np2q0kd8452au-1
  bzrlib/tests/__init__.py       selftest.py-20050531073622-8d0e3c8845c97a64
  bzrlib/tests/test_branch.py    test_branch.py-20060116013032-97819aa07b8ab3b5
  bzrlib/tests/workingtree_implementations/test_commit.py test_commit.py-20060421013633-1610ec2331c8190f
    ------------------------------------------------------------
    revno: 2553.1.3
    merged: robertc at robertcollins.net-20070628063203-lr39jqb65223ts09
    parent: robertc at robertcollins.net-20070628032159-ru3ztg712ns2dpip
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: integration
    timestamp: Thu 2007-06-28 16:32:03 +1000
    message:
      Increase docs in response to review feedback.
    ------------------------------------------------------------
    revno: 2553.1.2
    merged: robertc at robertcollins.net-20070628032159-ru3ztg712ns2dpip
    parent: robertc at robertcollins.net-20070628030853-fjeb7ii5euyvi7c1
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: commit-hook-names
    timestamp: Thu 2007-06-28 13:21:59 +1000
    message:
      Show hook names during commit.
    ------------------------------------------------------------
    revno: 2553.1.1
    merged: robertc at robertcollins.net-20070628030853-fjeb7ii5euyvi7c1
    parent: pqm at pqm.ubuntu.com-20070626191000-flte155pupv54bcs
    committer: Robert Collins <robertc at robertcollins.net>
    branch nick: commit-hook-names
    timestamp: Thu 2007-06-28 13:08:53 +1000
    message:
      Give Hooks names.
=== added file 'bzrlib/tests/test_hooks.py'
--- a/bzrlib/tests/test_hooks.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/tests/test_hooks.py	2007-06-28 03:08:53 +0000
@@ -0,0 +1,55 @@
+# Copyright (C) 2007 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# 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 the core Hooks logic."""
+
+from bzrlib.hooks import (
+    Hooks,
+    )
+from bzrlib.errors import (
+    UnknownHook,
+    )
+
+from bzrlib.tests import TestCase
+
+
+class TestHooks(TestCase):
+
+    def test_install_hook_raises_unknown_hook(self):
+        """install_hook should raise UnknownHook if a hook is unknown."""
+        hooks = Hooks()
+        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
+
+    def test_install_hook_appends_known_hook(self):
+        """install_hook should append the callable for known hooks."""
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        self.assertEqual(hooks['set_rh'], [None])
+
+    def test_name_hook_and_retrieve_name(self):
+        """name_hook puts the name in the names mapping."""
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        hooks.name_hook(None, 'demo')
+        self.assertEqual("demo", hooks.get_hook_name(None))
+
+    def test_get_unnamed_hook_name_is_unnamed(self):
+        hooks = Hooks()
+        hooks['set_rh'] = []
+        hooks.install_hook('set_rh', None)
+        self.assertEqual("No hook name", hooks.get_hook_name(None))

=== modified file 'NEWS'
--- a/NEWS	2007-06-27 01:43:03 +0000
+++ b/NEWS	2007-06-28 07:08:27 +0000
@@ -38,7 +38,10 @@
       
       If there is no master branch, the 3rd stage is omitted and the total
       number of stages is adjusted accordingly.
-      (Ian Clatworthy)
+
+      Each hook that is run after commit is listed with a name (as hooks
+      can be slow it is useful feedback).
+      (Ian Clatworthy, Robert Collins)
 
     * Various operations that are now faster due to avoiding unnecessary
       topological sorts. (Aaron Bentley)
@@ -54,6 +57,10 @@
 
     * New SMTPConnection class to unify email handling.  (Adeodato Simó)
 
+    * ``bzrlib`` Hooks are now nameable using ``Hooks.name_hook``. This 
+      allows a nicer UI when hooks are running as the current hook can
+      be displayed. (Robert Collins)
+
   TESTING:
 
     * Removed the ``--keep-output`` option from selftest and clean up test

=== modified file 'bzrlib/commit.py'
--- a/bzrlib/commit.py	2007-06-21 00:16:13 +0000
+++ b/bzrlib/commit.py	2007-06-28 06:32:03 +0000
@@ -338,9 +338,6 @@
             rev_tree = self.builder.revision_tree()
             self.work_tree.set_parent_trees([(self.rev_id, rev_tree)])
             self.reporter.completed(new_revno, self.rev_id)
-
-            # Process the post commit hooks, if any
-            self._emit_progress_set_stage("Running post commit hooks")
             self._process_hooks(old_revno, new_revno)
         finally:
             self._cleanup()
@@ -469,6 +466,8 @@
 
     def _process_hooks(self, old_revno, new_revno):
         """Process any registered commit hooks."""
+        # Process the post commit hooks, if any
+        self._emit_progress_set_stage("Running post commit hooks")
         # old style commit hooks - should be deprecated ? (obsoleted in
         # 0.15)
         if self.config.post_commit() is not None:
@@ -494,6 +493,14 @@
         else:
             old_revid = bzrlib.revision.NULL_REVISION
         for hook in Branch.hooks['post_commit']:
+            # show the running hook in the progress bar. As hooks may
+            # end up doing nothing (e.g. because they are not configured by
+            # the user) this is still showing progress, not showing overall
+            # actions - its up to each plugin to show a UI if it want's to
+            # (such as 'Emailing diff to foo at example.com').
+            self.pb_stage_name = "Running post commit hooks [%s]" % \
+                Branch.hooks.get_hook_name(hook)
+            self._emit_progress()
             hook(hook_local, hook_master, old_revno, old_revid, new_revno,
                 self.rev_id)
 

=== modified file 'bzrlib/hooks.py'
--- a/bzrlib/hooks.py	2007-03-25 08:59:56 +0000
+++ b/bzrlib/hooks.py	2007-06-28 06:32:03 +0000
@@ -31,6 +31,20 @@
     FOO hook is triggered.
     """
 
+    def __init__(self):
+        dict.__init__(self)
+        self._callable_names = {}
+
+    def get_hook_name(self, a_callable):
+        """Get the name for a_callable for UI display.
+
+        If no name has been registered, the string 'No hook name' is returned.
+        We use a fixed string rather than repr or the callables module because
+        the code names are rarely meaningful for end users and this is not 
+        intended for debugging.
+        """
+        return self._callable_names.get(a_callable, "No hook name")
+
     def install_hook(self, hook_name, a_callable):
         """Install a_callable in to the hook hook_name.
 
@@ -44,3 +58,7 @@
             self[hook_name].append(a_callable)
         except KeyError:
             raise errors.UnknownHook(self.__class__.__name__, hook_name)
+
+    def name_hook(self, a_callable, name):
+        """Associate name with a_callable to show users what is running."""
+        self._callable_names[a_callable] = name

=== modified file 'bzrlib/tests/__init__.py'
--- a/bzrlib/tests/__init__.py	2007-06-27 01:43:03 +0000
+++ b/bzrlib/tests/__init__.py	2007-06-28 07:08:27 +0000
@@ -2281,6 +2281,7 @@
                    'bzrlib.tests.test_graph',
                    'bzrlib.tests.test_hashcache',
                    'bzrlib.tests.test_help',
+                   'bzrlib.tests.test_hooks',
                    'bzrlib.tests.test_http',
                    'bzrlib.tests.test_http_response',
                    'bzrlib.tests.test_https_ca_bundle',

=== modified file 'bzrlib/tests/test_branch.py'
--- a/bzrlib/tests/test_branch.py	2007-04-10 15:54:15 +0000
+++ b/bzrlib/tests/test_branch.py	2007-06-28 03:08:53 +0000
@@ -326,17 +326,6 @@
         # the installed hooks are saved in self._preserved_hooks.
         self.assertIsInstance(self._preserved_hooks[_mod_branch.Branch], BranchHooks)
 
-    def test_install_hook_raises_unknown_hook(self):
-        """install_hook should raise UnknownHook if a hook is unknown."""
-        hooks = BranchHooks()
-        self.assertRaises(UnknownHook, hooks.install_hook, 'silly', None)
-
-    def test_install_hook_appends_known_hook(self):
-        """install_hook should append the callable for known hooks."""
-        hooks = BranchHooks()
-        hooks.install_hook('set_rh', None)
-        self.assertEqual(hooks['set_rh'], [None])
-
 
 class TestPullResult(TestCase):
 

=== modified file 'bzrlib/tests/workingtree_implementations/test_commit.py'
--- a/bzrlib/tests/workingtree_implementations/test_commit.py	2007-06-20 03:32:32 +0000
+++ b/bzrlib/tests/workingtree_implementations/test_commit.py	2007-06-28 03:21:59 +0000
@@ -359,3 +359,27 @@
              ('update', 4, 4, 'Running post commit hooks - Stage')],
             factory._calls
            )
+
+    def test_commit_progress_shows_hook_names(self):
+        tree = self.make_branch_and_tree('.')
+        # set a progress bar that captures the calls so we can see what is 
+        # emitted
+        self.old_ui_factory = ui.ui_factory
+        self.addCleanup(self.restoreDefaults)
+        factory = CapturingUIFactory()
+        ui.ui_factory = factory
+        def a_hook(_, _2, _3, _4, _5, _6):
+            pass
+        branch.Branch.hooks.install_hook('post_commit', a_hook)
+        branch.Branch.hooks.name_hook(a_hook, 'hook name')
+        tree.commit('first post')
+        self.assertEqual(
+            [('update', 1, 4, 'Collecting changes [Entry 0/?] - Stage'),
+             ('update', 1, 4, 'Collecting changes [Entry 1/1] - Stage'),
+             ('update', 2, 4, 'Saving data locally - Stage'),
+             ('update', 3, 4, 'Updating the working tree - Stage'),
+             ('update', 4, 4, 'Running post commit hooks - Stage'),
+             ('update', 4, 4, 'Running post commit hooks [hook name] - Stage'),
+             ],
+            factory._calls
+           )




More information about the bazaar-commits mailing list