Merge lp:~jelmer/bzr/diff-format into lp:bzr

Proposed by Jelmer Vernooij
Status: Merged
Approved by: Ian Clatworthy
Approved revision: no longer in the source branch.
Merged at revision: not available
Proposed branch: lp:~jelmer/bzr/diff-format
Merge into: lp:bzr
Diff against target: 193 lines (+65/-24)
4 files modified
NEWS (+3/-0)
bzrlib/builtins.py (+13/-3)
bzrlib/diff.py (+25/-21)
bzrlib/tests/blackbox/test_diff.py (+24/-0)
To merge this branch: bzr merge lp:~jelmer/bzr/diff-format
Reviewer Review Type Date Requested Status
Ian Clatworthy Approve
Review via email: mp+22837@code.launchpad.net

Description of the change

This adds support for a --format option to 'bzr diff', defaulting to the current DiffTree implementation in Bazaar.

bzr-git and bzr-svn already ship with custom DiffTree implementations to output diffs in the formats supported by Subversion and Git.

To post a comment you must log in.
Revision history for this message
Ian Clatworthy (ian-clatworthy) wrote :

I'd prefer to see the default diff format looked up using the registry API for getting the default. Otherwise, this looks fine by me.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'NEWS'
--- NEWS 2010-04-02 15:10:54 +0000
+++ NEWS 2010-04-05 22:03:20 +0000
@@ -16,6 +16,9 @@
16New Features16New Features
17************17************
1818
19* ``bzr diff`` now supports a --format option, which can be used to
20 select alternative diff formats. (Jelmer Vernooij, #555994)
21
19Bug Fixes22Bug Fixes
20*********23*********
2124
2225
=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py 2010-04-01 04:38:26 +0000
+++ bzrlib/builtins.py 2010-04-05 22:03:20 +0000
@@ -1954,14 +1954,19 @@
1954 help='Use this command to compare files.',1954 help='Use this command to compare files.',
1955 type=unicode,1955 type=unicode,
1956 ),1956 ),
1957 RegistryOption('format',
1958 help='Diff format to use.',
1959 lazy_registry=('bzrlib.diff', 'format_registry'),
1960 value_switches=False, title='Diff format'),
1957 ]1961 ]
1958 aliases = ['di', 'dif']1962 aliases = ['di', 'dif']
1959 encoding_type = 'exact'1963 encoding_type = 'exact'
19601964
1961 @display_command1965 @display_command
1962 def run(self, revision=None, file_list=None, diff_options=None,1966 def run(self, revision=None, file_list=None, diff_options=None,
1963 prefix=None, old=None, new=None, using=None):1967 prefix=None, old=None, new=None, using=None, format=None):
1964 from bzrlib.diff import get_trees_and_branches_to_diff, show_diff_trees1968 from bzrlib.diff import (get_trees_and_branches_to_diff,
1969 show_diff_trees)
19651970
1966 if (prefix is None) or (prefix == '0'):1971 if (prefix is None) or (prefix == '0'):
1967 # diff -p0 format1972 # diff -p0 format
@@ -1981,6 +1986,10 @@
1981 raise errors.BzrCommandError('bzr diff --revision takes exactly'1986 raise errors.BzrCommandError('bzr diff --revision takes exactly'
1982 ' one or two revision specifiers')1987 ' one or two revision specifiers')
19831988
1989 if using is not None and format is not None:
1990 raise errors.BzrCommandError('--using and --format are mutually '
1991 'exclusive.')
1992
1984 (old_tree, new_tree,1993 (old_tree, new_tree,
1985 old_branch, new_branch,1994 old_branch, new_branch,
1986 specific_files, extra_trees) = get_trees_and_branches_to_diff(1995 specific_files, extra_trees) = get_trees_and_branches_to_diff(
@@ -1989,7 +1998,8 @@
1989 specific_files=specific_files,1998 specific_files=specific_files,
1990 external_diff_options=diff_options,1999 external_diff_options=diff_options,
1991 old_label=old_label, new_label=new_label,2000 old_label=old_label, new_label=new_label,
1992 extra_trees=extra_trees, using=using)2001 extra_trees=extra_trees, using=using,
2002 format_cls=format)
19932003
19942004
1995class cmd_deleted(Command):2005class cmd_deleted(Command):
19962006
=== modified file 'bzrlib/diff.py'
--- bzrlib/diff.py 2010-02-23 07:43:11 +0000
+++ bzrlib/diff.py 2010-04-05 22:03:20 +0000
@@ -43,6 +43,9 @@
43from bzrlib.workingtree import WorkingTree43from bzrlib.workingtree import WorkingTree
44""")44""")
4545
46from bzrlib.registry import (
47 Registry,
48 )
46from bzrlib.symbol_versioning import (49from bzrlib.symbol_versioning import (
47 deprecated_function,50 deprecated_function,
48 )51 )
@@ -411,25 +414,22 @@
411 old_label='a/', new_label='b/',414 old_label='a/', new_label='b/',
412 extra_trees=None,415 extra_trees=None,
413 path_encoding='utf8',416 path_encoding='utf8',
414 using=None):417 using=None,
418 format_cls=None):
415 """Show in text form the changes from one tree to another.419 """Show in text form the changes from one tree to another.
416420
417 to_file421 :param to_file: The output stream.
418 The output stream.422 :param specific_files:Include only changes to these files - None for all
419423 changes.
420 specific_files424 :param external_diff_options: If set, use an external GNU diff and pass
421 Include only changes to these files - None for all changes.425 these options.
422426 :param extra_trees: If set, more Trees to use for looking up file ids
423 external_diff_options427 :param path_encoding: If set, the path will be encoded as specified,
424 If set, use an external GNU diff and pass these options.428 otherwise is supposed to be utf8
425429 :param format_cls: Formatter class (DiffTree subclass)
426 extra_trees
427 If set, more Trees to use for looking up file ids
428
429 path_encoding
430 If set, the path will be encoded as specified, otherwise is supposed
431 to be utf8
432 """430 """
431 if format_cls is None:
432 format_cls = DiffTree
433 old_tree.lock_read()433 old_tree.lock_read()
434 try:434 try:
435 if extra_trees is not None:435 if extra_trees is not None:
@@ -437,10 +437,10 @@
437 tree.lock_read()437 tree.lock_read()
438 new_tree.lock_read()438 new_tree.lock_read()
439 try:439 try:
440 differ = DiffTree.from_trees_options(old_tree, new_tree, to_file,440 differ = format_cls.from_trees_options(old_tree, new_tree, to_file,
441 path_encoding,441 path_encoding,
442 external_diff_options,442 external_diff_options,
443 old_label, new_label, using)443 old_label, new_label, using)
444 return differ.show_diff(specific_files, extra_trees)444 return differ.show_diff(specific_files, extra_trees)
445 finally:445 finally:
446 new_tree.unlock()446 new_tree.unlock()
@@ -882,7 +882,7 @@
882 def show_diff(self, specific_files, extra_trees=None):882 def show_diff(self, specific_files, extra_trees=None):
883 """Write tree diff to self.to_file883 """Write tree diff to self.to_file
884884
885 :param sepecific_files: the specific files to compare (recursive)885 :param specific_files: the specific files to compare (recursive)
886 :param extra_trees: extra trees to use for mapping paths to file_ids886 :param extra_trees: extra trees to use for mapping paths to file_ids
887 """887 """
888 try:888 try:
@@ -978,3 +978,7 @@
978 if error_path is None:978 if error_path is None:
979 error_path = old_path979 error_path = old_path
980 raise errors.NoDiffFound(error_path)980 raise errors.NoDiffFound(error_path)
981
982
983format_registry = Registry()
984format_registry.register('default', DiffTree)
981985
=== modified file 'bzrlib/tests/blackbox/test_diff.py'
--- bzrlib/tests/blackbox/test_diff.py 2010-03-24 14:15:01 +0000
+++ bzrlib/tests/blackbox/test_diff.py 2010-04-05 22:03:20 +0000
@@ -25,6 +25,10 @@
25 tests,25 tests,
26 workingtree,26 workingtree,
27 )27 )
28from bzrlib.diff import (
29 DiffTree,
30 format_registry as diff_format_registry,
31 )
2832
2933
30def subst_dates(string):34def subst_dates(string):
@@ -132,6 +136,10 @@
132 out, err = self.run_bzr('diff -r 1..23..123', retcode=3,136 out, err = self.run_bzr('diff -r 1..23..123', retcode=3,
133 error_regexes=('one or two revision specifiers',))137 error_regexes=('one or two revision specifiers',))
134138
139 def test_diff_using_and_format(self):
140 out, err = self.run_bzr('diff --format=default --using=mydi', retcode=3,
141 error_regexes=('are mutually exclusive',))
142
135 def test_diff_nonexistent_revision(self):143 def test_diff_nonexistent_revision(self):
136 out, err = self.run_bzr('diff -r 123', retcode=3,144 out, err = self.run_bzr('diff -r 123', retcode=3,
137 error_regexes=("Requested revision: '123' does not "145 error_regexes=("Requested revision: '123' does not "
@@ -297,6 +305,22 @@
297 output = self.run_bzr('diff -r 1.. branch1', retcode=1)305 output = self.run_bzr('diff -r 1.. branch1', retcode=1)
298 self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')306 self.assertContainsRe(output[0], '\n\\-original line\n\\+repo line\n')
299307
308 def test_custom_format(self):
309 class BooDiffTree(DiffTree):
310
311 def show_diff(self, specific_files, extra_trees=None):
312 self.to_file.write("BOO!\n")
313 return super(BooDiffTree, self).show_diff(specific_files,
314 extra_trees)
315
316 diff_format_registry.register("boo", BooDiffTree,
317 "Scary diff format")
318 self.addCleanup(diff_format_registry.remove, "boo")
319 self.make_example_branch()
320 self.build_tree_contents([('hello', 'hello world!\n')])
321 output = self.run_bzr('diff --format=boo', retcode=1)
322 self.assertTrue("BOO!" in output[0])
323
300324
301class TestCheckoutDiff(TestDiff):325class TestCheckoutDiff(TestDiff):
302326