[MERGE] Bzr diff produces -p1 diffs

Matthew D. Fuller fullermd at over-yonder.net
Thu Apr 20 11:04:59 BST 2006


On Thu, Mar 02, 2006 at 05:43:41PM +1100 I heard the voice of
Michael Ellerman, and lo! it spake thus:
> 
> What about this?
> 
> bzr diff --no-prefix

Updated patch against current bzr.dev (made with --no-prefix, of
course ;) attached.


-- 
Matthew Fuller     (MF4839)   |  fullermd at over-yonder.net
Systems/Network Administrator |  http://www.over-yonder.net/~fullermd/
           On the Internet, nobody can hear you scream.
-------------- next part --------------
=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py	
+++ bzrlib/builtins.py	
@@ -997,11 +997,13 @@
     # TODO: This probably handles non-Unix newlines poorly.
     
     takes_args = ['file*']
-    takes_options = ['revision', 'diff-options']
+    takes_options = ['revision', 'diff-options',
+        Option('no-prefix', "Don't prefix filenames, ie. produce a -p0 diff.")]
     aliases = ['di', 'dif']
 
     @display_command
-    def run(self, revision=None, file_list=None, diff_options=None):
+    def run(self, revision=None, file_list=None, diff_options=None,
+            no_prefix=False):
         from bzrlib.diff import diff_cmd_helper, show_diff_trees
         try:
             tree1, file_list = internal_tree_files(file_list)
@@ -1023,19 +1025,22 @@
                 raise BzrCommandError("Can't specify -r with two branches")
             if (len(revision) == 1) or (revision[1].spec is None):
                 return diff_cmd_helper(tree1, file_list, diff_options,
-                                       revision[0])
+                                       revision[0], no_prefix=no_prefix)
             elif len(revision) == 2:
                 return diff_cmd_helper(tree1, file_list, diff_options,
-                                       revision[0], revision[1])
+                                       revision[0], revision[1],
+                                       no_prefix=no_prefix)
             else:
                 raise BzrCommandError('bzr diff --revision takes exactly one or two revision identifiers')
         else:
             if tree2 is not None:
                 return show_diff_trees(tree1, tree2, sys.stdout, 
                                        specific_files=file_list,
-                                       external_diff_options=diff_options)
-            else:
-                return diff_cmd_helper(tree1, file_list, diff_options)
+                                       external_diff_options=diff_options,
+                                       no_prefix=no_prefix)
+            else:
+                return diff_cmd_helper(tree1, file_list, diff_options,
+                        no_prefix=no_prefix)
 
 
 class cmd_deleted(Command):

=== modified file 'bzrlib/diff.py'
--- bzrlib/diff.py	
+++ bzrlib/diff.py	
@@ -190,7 +190,8 @@
 
 
 def diff_cmd_helper(tree, specific_files, external_diff_options, 
-                    old_revision_spec=None, new_revision_spec=None):
+                    old_revision_spec=None, new_revision_spec=None,
+                    no_prefix=False):
     """Helper for cmd_diff.
 
    tree 
@@ -209,7 +210,10 @@
     new_revision_spec
         If None, use working tree as new revision, otherwise use the tree for
         the specified revision.
-    
+
+    no_prefix
+        If True, don't display a prefix on filenames, ie. produce a -p0 diff.
+
     The more general form is show_diff_trees(), where the caller
     supplies any two trees.
     """
@@ -229,11 +233,11 @@
         new_tree = spec_tree(new_revision_spec)
 
     return show_diff_trees(old_tree, new_tree, sys.stdout, specific_files,
-                           external_diff_options)
+                           external_diff_options, no_prefix)
 
 
 def show_diff_trees(old_tree, new_tree, to_file, specific_files=None,
-                    external_diff_options=None):
+                    external_diff_options=None, no_prefix=False):
     """Show in text form the changes from one tree to another.
 
     to_files
@@ -241,13 +245,17 @@
 
     external_diff_options
         If set, use an external GNU diff and pass these options.
+
+    no_prefix
+        If True, don't display a prefix on filenames, ie. produce a -p0 diff.
     """
     old_tree.lock_read()
     try:
         new_tree.lock_read()
         try:
             return _show_diff_trees(old_tree, new_tree, to_file,
-                                    specific_files, external_diff_options)
+                                    specific_files, external_diff_options,
+                                    no_prefix)
         finally:
             new_tree.unlock()
     finally:
@@ -255,12 +263,15 @@
 
 
 def _show_diff_trees(old_tree, new_tree, to_file,
-                     specific_files, external_diff_options):
+                     specific_files, external_diff_options, no_prefix):
 
     # TODO: Options to control putting on a prefix or suffix, perhaps
     # as a format string?
-    old_label = 'a/'
-    new_label = 'b/'
+    if no_prefix:
+        old_label = new_label = ''
+    else:
+        old_label = 'a/'
+        new_label = 'b/'
 
     DEVNULL = '/dev/null'
     # Windows users, don't panic about this filename -- it is a

=== modified file 'bzrlib/tests/blackbox/test_diff.py'
--- bzrlib/tests/blackbox/test_diff.py	
+++ bzrlib/tests/blackbox/test_diff.py	
@@ -140,6 +140,14 @@
 
 class TestDiffLabels(TestDiff):
 
+    def test_diff_header(self):
+        super(TestDiffLabels, self).example_branch()
+        file('hello', 'wt').write('barbar')
+        diff = self.run_bzr_captured(['diff'], retcode=1)
+        diff_lines = diff[0].split('\n')
+        self.assertEquals("--- a/hello\t", diff_lines[1])
+        self.assertEquals("+++ b/hello\t", diff_lines[2])
+
     def test_diff_label_removed(self):
         super(TestDiffLabels, self).make_example_branch()
         self.runbzr('remove hello')
@@ -164,3 +172,38 @@
         self.runbzr('rename hello gruezi')
         diff = self.run_bzr_captured(['diff'], retcode=1)
         self.assertTrue("=== renamed file 'a/hello' => 'b/gruezi'" in diff[0])
+
+class TestDiffLabelsNoPrefix(TestDiff):
+
+    def test_diff_header(self):
+        super(TestDiffLabelsNoPrefix, self).example_branch()
+        file('hello', 'wt').write('barbar')
+        diff = self.run_bzr_captured(['diff', '--no-prefix'], retcode=1)
+        diff_lines = diff[0].split('\n')
+        self.assertEquals("--- hello\t", diff_lines[1])
+        self.assertEquals("+++ hello\t", diff_lines[2])
+
+    def test_diff_label_removed(self):
+        super(TestDiffLabelsNoPrefix, self).example_branch()
+        self.runbzr('remove hello')
+        diff = self.run_bzr_captured(['diff', '--no-prefix'], retcode=1)
+        self.assertTrue("=== removed file 'hello'" in diff[0])
+
+    def test_diff_label_added(self):
+        super(TestDiffLabelsNoPrefix, self).example_branch()
+        file('barbar', 'wt').write('barbar')
+        self.runbzr('add barbar')
+        diff = self.run_bzr_captured(['diff', '--no-prefix'], retcode=1)
+        self.assertTrue("=== added file 'barbar'" in diff[0])
+
+    def test_diff_label_modified(self):
+        super(TestDiffLabelsNoPrefix, self).example_branch()
+        file('hello', 'wt').write('barbar')
+        diff = self.run_bzr_captured(['diff', '--no-prefix'], retcode=1)
+        self.assertTrue("=== modified file 'hello'" in diff[0])
+
+    def test_diff_label_renamed(self):
+        super(TestDiffLabelsNoPrefix, self).example_branch()
+        self.runbzr('rename hello gruezi')
+        diff = self.run_bzr_captured(['diff', '--no-prefix'], retcode=1)
+        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])



More information about the bazaar mailing list