Merge lp:~spiv/bzr/merge-file-content-hook-docs into lp:bzr

Proposed by Andrew Bennetts
Status: Merged
Merged at revision: not available
Proposed branch: lp:~spiv/bzr/merge-file-content-hook-docs
Merge into: lp:bzr
Diff against target: 82 lines (+61/-1)
2 files modified
doc/en/user-guide/hooks.txt (+53/-1)
doc/en/user-guide/writing_a_plugin.txt (+8/-0)
To merge this branch: bzr merge lp:~spiv/bzr/merge-file-content-hook-docs
Reviewer Review Type Date Requested Status
Vincent Ladeuil Approve
Review via email: mp+23830@code.launchpad.net

Description of the change

This adds an example of the merge_file_content hook to our docs. It's a lightly-tweaked variant of the answer I gave to <https://answers.edge.launchpad.net/bzr/+question/103163>. I also point to the news_merge plugin.

To post a comment you must log in.
Revision history for this message
Vincent Ladeuil (vila) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'doc/en/user-guide/hooks.txt'
2--- doc/en/user-guide/hooks.txt 2010-01-03 03:49:07 +0000
3+++ doc/en/user-guide/hooks.txt 2010-04-21 07:59:15 +0000
4@@ -57,6 +57,58 @@
5 Debugging hooks
6 ---------------
7
8-To get a list of installed hooks, use the hidden ``hooks`` command::
9+To get a list of installed hooks (and available hook points), use the hidden
10+``hooks`` command::
11
12 bzr hooks
13+
14+
15+Example: a merge plugin
16+-----------------------
17+
18+Here's a complete plugin that demonstrates the ``Merger.merge_file_content``
19+hook. It installs a hook that forces any merge of a file named ``*.xml``
20+to be a conflict, even if Bazaar thinks it can merge it cleanly.
21+
22+``merge_xml.py``::
23+
24+ """Custom 'merge' logic for *.xml files.
25+
26+ Always conflicts if both branches have changed the file.
27+ """
28+
29+ from bzrlib.merge import AbstractPerFileMerger, Merger
30+
31+ def merge_xml_files_hook(merger):
32+ """Hook to merge *.xml files"""
33+ return MergeXMLFiles(merger)
34+
35+ class MergeXMLFiles(AbstractPerFileMerger):
36+
37+ def filename_matches(self, params):
38+ inventory = self.merger.this_tree.inventory
39+ filename = inventory[params.file_id].name
40+ if filename.endswith('.xml'):
41+ return filename
42+
43+ def merge_contents(self, params):
44+ """Merge the contents of a single file."""
45+ # First, check whether this custom merge logic should be used. We
46+ # expect most files should not be merged by this handler.
47+ if (
48+ # OTHER is a straight winner, rely on default merge.
49+ params.winner == 'other' or
50+ # THIS and OTHER aren't both files.
51+ not params.is_file_merge() or
52+ # The filename doesn't match *.xml
53+ not self.filename_matches(params)):
54+ return 'not_applicable', None
55+ return 'conflicted', params.this_lines
56+
57+ Merger.hooks.install_named_hook(
58+ 'merge_file_content', merge_xml_files_hook, '*.xml file merge')
59+
60+``merge_file_content`` hooks are executed for each file to be merged. For
61+a more a complex example look at the ``news_merge`` plugin that's bundled with
62+Bazaar in the ``bzrlib/plugins`` directory.
63+
64
65=== modified file 'doc/en/user-guide/writing_a_plugin.txt'
66--- doc/en/user-guide/writing_a_plugin.txt 2009-08-20 13:26:36 +0000
67+++ doc/en/user-guide/writing_a_plugin.txt 2010-04-21 07:59:15 +0000
68@@ -22,6 +22,14 @@
69 ``bzrlib.commands.register_command(cmd_foo)``. You must register the
70 command when your file is imported, otherwise bzr will not see it.
71
72+Installing a hook
73+-----------------
74+
75+See `Using hooks`_.
76+
77+ .. _Using hooks: hooks.txt
78+
79+
80 Specifying a plugin version number
81 ----------------------------------
82 Simply define ``version_info`` to be a tuple defining the current version