Rev 5132: (Jelmer) Support --format option to 'bzr diff'. in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Tue Apr 6 02:43:41 BST 2010


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

------------------------------------------------------------
revno: 5132 [merge]
revision-id: pqm at pqm.ubuntu.com-20100406014339-1hnb17m0cytvah0w
parent: pqm at pqm.ubuntu.com-20100402163146-gbp0cmngkeqe30y0
parent: jelmer at samba.org-20100405215639-blaww00wgf2jgl82
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Tue 2010-04-06 02:43:39 +0100
message:
  (Jelmer) Support --format option to 'bzr diff'.
modified:
  NEWS                           NEWS-20050323055033-4e00b5db738777ff
  bzrlib/builtins.py             builtins.py-20050830033751-fc01482b9ca23183
  bzrlib/diff.py                 diff.py-20050309040759-26944fbbf2ebbf36
  bzrlib/tests/blackbox/test_diff.py test_diff.py-20060110203741-aa99ac93e633d971
=== modified file 'NEWS'
--- a/NEWS	2010-04-02 15:10:54 +0000
+++ b/NEWS	2010-04-05 21:47:13 +0000
@@ -16,6 +16,9 @@
 New Features
 ************
 
+* ``bzr diff`` now supports a --format option, which can be used to 
+  select alternative diff formats. (Jelmer Vernooij, #555994)
+
 Bug Fixes
 *********
 

=== modified file 'bzrlib/builtins.py'
--- a/bzrlib/builtins.py	2010-04-01 04:38:26 +0000
+++ b/bzrlib/builtins.py	2010-04-05 21:50:59 +0000
@@ -1954,14 +1954,19 @@
             help='Use this command to compare files.',
             type=unicode,
             ),
+        RegistryOption('format',
+            help='Diff format to use.',
+            lazy_registry=('bzrlib.diff', 'format_registry'),
+            value_switches=False, title='Diff format'),
         ]
     aliases = ['di', 'dif']
     encoding_type = 'exact'
 
     @display_command
     def run(self, revision=None, file_list=None, diff_options=None,
-            prefix=None, old=None, new=None, using=None):
-        from bzrlib.diff import get_trees_and_branches_to_diff, show_diff_trees
+            prefix=None, old=None, new=None, using=None, format=None):
+        from bzrlib.diff import (get_trees_and_branches_to_diff,
+            show_diff_trees)
 
         if (prefix is None) or (prefix == '0'):
             # diff -p0 format
@@ -1981,6 +1986,10 @@
             raise errors.BzrCommandError('bzr diff --revision takes exactly'
                                          ' one or two revision specifiers')
 
+        if using is not None and format is not None:
+            raise errors.BzrCommandError('--using and --format are mutually '
+                'exclusive.')
+
         (old_tree, new_tree,
          old_branch, new_branch,
          specific_files, extra_trees) = get_trees_and_branches_to_diff(
@@ -1989,7 +1998,8 @@
                                specific_files=specific_files,
                                external_diff_options=diff_options,
                                old_label=old_label, new_label=new_label,
-                               extra_trees=extra_trees, using=using)
+                               extra_trees=extra_trees, using=using,
+                               format_cls=format)
 
 
 class cmd_deleted(Command):

=== modified file 'bzrlib/diff.py'
--- a/bzrlib/diff.py	2010-02-23 07:43:11 +0000
+++ b/bzrlib/diff.py	2010-04-05 21:56:39 +0000
@@ -43,6 +43,9 @@
 from bzrlib.workingtree import WorkingTree
 """)
 
+from bzrlib.registry import (
+    Registry,
+    )
 from bzrlib.symbol_versioning import (
     deprecated_function,
     )
@@ -411,25 +414,22 @@
                     old_label='a/', new_label='b/',
                     extra_trees=None,
                     path_encoding='utf8',
-                    using=None):
+                    using=None,
+                    format_cls=None):
     """Show in text form the changes from one tree to another.
 
-    to_file
-        The output stream.
-
-    specific_files
-        Include only changes to these files - None for all changes.
-
-    external_diff_options
-        If set, use an external GNU diff and pass these options.
-
-    extra_trees
-        If set, more Trees to use for looking up file ids
-
-    path_encoding
-        If set, the path will be encoded as specified, otherwise is supposed
-        to be utf8
+    :param to_file: The output stream.
+    :param specific_files:Include only changes to these files - None for all
+        changes.
+    :param external_diff_options: If set, use an external GNU diff and pass 
+        these options.
+    :param extra_trees: If set, more Trees to use for looking up file ids
+    :param path_encoding: If set, the path will be encoded as specified, 
+        otherwise is supposed to be utf8
+    :param format_cls: Formatter class (DiffTree subclass)
     """
+    if format_cls is None:
+        format_cls = DiffTree
     old_tree.lock_read()
     try:
         if extra_trees is not None:
@@ -437,10 +437,10 @@
                 tree.lock_read()
         new_tree.lock_read()
         try:
-            differ = DiffTree.from_trees_options(old_tree, new_tree, to_file,
-                                                 path_encoding,
-                                                 external_diff_options,
-                                                 old_label, new_label, using)
+            differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
+                                                   path_encoding,
+                                                   external_diff_options,
+                                                   old_label, new_label, using)
             return differ.show_diff(specific_files, extra_trees)
         finally:
             new_tree.unlock()
@@ -882,7 +882,7 @@
     def show_diff(self, specific_files, extra_trees=None):
         """Write tree diff to self.to_file
 
-        :param sepecific_files: the specific files to compare (recursive)
+        :param specific_files: the specific files to compare (recursive)
         :param extra_trees: extra trees to use for mapping paths to file_ids
         """
         try:
@@ -978,3 +978,7 @@
             if error_path is None:
                 error_path = old_path
             raise errors.NoDiffFound(error_path)
+
+
+format_registry = Registry()
+format_registry.register('default', DiffTree)

=== modified file 'bzrlib/tests/blackbox/test_diff.py'
--- a/bzrlib/tests/blackbox/test_diff.py	2010-03-24 14:15:01 +0000
+++ b/bzrlib/tests/blackbox/test_diff.py	2010-04-05 21:56:39 +0000
@@ -25,6 +25,10 @@
     tests,
     workingtree,
     )
+from bzrlib.diff import (
+    DiffTree,
+    format_registry as diff_format_registry,
+    )
 
 
 def subst_dates(string):
@@ -132,6 +136,10 @@
         out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
             error_regexes=('one or two revision specifiers',))
 
+    def test_diff_using_and_format(self):
+        out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
+            error_regexes=('are mutually exclusive',))
+
     def test_diff_nonexistent_revision(self):
         out, err = self.run_bzr('diff -r 123', retcode=3,
             error_regexes=("Requested revision: '123' does not "
@@ -297,6 +305,22 @@
         output = self.run_bzr('diff -r 1.. branch1', retcode=1)
         self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')
 
+    def test_custom_format(self):
+        class BooDiffTree(DiffTree):
+
+            def show_diff(self, specific_files, extra_trees=None):
+                self.to_file.write("BOO!\n")
+                return super(BooDiffTree, self).show_diff(specific_files,
+                    extra_trees)
+
+        diff_format_registry.register("boo", BooDiffTree, 
+            "Scary diff format")
+        self.addCleanup(diff_format_registry.remove, "boo")
+        self.make_example_branch()
+        self.build_tree_contents([('hello', 'hello world!\n')])
+        output = self.run_bzr('diff --format=boo', retcode=1)
+        self.assertTrue("BOO!" in output[0])
+
 
 class TestCheckoutDiff(TestDiff):
 




More information about the bazaar-commits mailing list