Rev 5176: (andrew) Add example merge_file_content hook. (#532435) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Fri Apr 23 02:51:51 BST 2010


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

------------------------------------------------------------
revno: 5176 [merge]
revision-id: pqm at pqm.ubuntu.com-20100423015148-zim5rd4e1bwxfbxb
parent: pqm at pqm.ubuntu.com-20100422203235-9effhwzpxg7d3rze
parent: andrew.bennetts at canonical.com-20100421075231-2n8pfxbhlhqxic6w
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Fri 2010-04-23 02:51:48 +0100
message:
  (andrew) Add example merge_file_content hook. (#532435)
modified:
  doc/en/user-guide/hooks.txt    hooks.txt-20070829200551-7nr6e5a1io6x78uf-1
  doc/en/user-guide/writing_a_plugin.txt writing_a_plugin.txt-20071114035000-q36a9h57ps06uvnl-7
=== modified file 'doc/en/user-guide/hooks.txt'
--- a/doc/en/user-guide/hooks.txt	2010-01-03 03:49:07 +0000
+++ b/doc/en/user-guide/hooks.txt	2010-04-21 07:52:31 +0000
@@ -57,6 +57,58 @@
 Debugging hooks
 ---------------
 
-To get a list of installed hooks, use the hidden ``hooks`` command::
+To get a list of installed hooks (and available hook points), use the hidden
+``hooks`` command::
 
     bzr hooks
+
+
+Example: a merge plugin
+-----------------------
+
+Here's a complete plugin that demonstrates the ``Merger.merge_file_content``
+hook.  It installs a hook that forces any merge of a file named ``*.xml``
+to be a conflict, even if Bazaar thinks it can merge it cleanly.
+
+``merge_xml.py``::
+
+  """Custom 'merge' logic for *.xml files.
+  
+  Always conflicts if both branches have changed the file.
+  """
+  
+  from bzrlib.merge import AbstractPerFileMerger, Merger
+  
+  def merge_xml_files_hook(merger):
+      """Hook to merge *.xml files"""
+      return MergeXMLFiles(merger)
+  
+  class MergeXMLFiles(AbstractPerFileMerger):
+  
+      def filename_matches(self, params):
+          inventory = self.merger.this_tree.inventory
+          filename = inventory[params.file_id].name
+          if filename.endswith('.xml'):
+              return filename
+  
+      def merge_contents(self, params):
+          """Merge the contents of a single file."""
+          # First, check whether this custom merge logic should be used. We
+          # expect most files should not be merged by this handler.
+          if (
+              # OTHER is a straight winner, rely on default merge.
+              params.winner == 'other' or
+              # THIS and OTHER aren't both files.
+              not params.is_file_merge() or
+              # The filename doesn't match *.xml
+              not self.filename_matches(params)):
+              return 'not_applicable', None
+          return 'conflicted', params.this_lines
+  
+  Merger.hooks.install_named_hook(
+      'merge_file_content', merge_xml_files_hook, '*.xml file merge')
+
+``merge_file_content`` hooks are executed for each file to be merged.  For
+a more a complex example look at the ``news_merge`` plugin that's bundled with
+Bazaar in the ``bzrlib/plugins`` directory.
+

=== modified file 'doc/en/user-guide/writing_a_plugin.txt'
--- a/doc/en/user-guide/writing_a_plugin.txt	2009-08-20 13:26:36 +0000
+++ b/doc/en/user-guide/writing_a_plugin.txt	2010-04-21 07:52:31 +0000
@@ -22,6 +22,14 @@
 ``bzrlib.commands.register_command(cmd_foo)``.  You must register the
 command when your file is imported, otherwise bzr will not see it.
 
+Installing a hook
+-----------------
+
+See `Using hooks`_.
+
+ .. _Using hooks: hooks.txt
+
+
 Specifying a plugin version number
 ----------------------------------
 Simply define ``version_info`` to be a tuple defining the current version




More information about the bazaar-commits mailing list