[patch] remove default diff prefix; support -p0 and -p1

Martin Pool mbp at sourcefrog.net
Thu May 4 07:22:39 BST 2006


* change diffs back to not having path prefixes by default
* you can specify them with 'diff --prefix OLD:NEW'
  (note that this has changed from 'diff --diff-prefix', which seemed a bit
   redundant)
* you can also just say 'diff -p0' (gives the default) or 'diff -p1'

I plan to put this in 0.8

-- 
Martin
-------------- next part --------------
=== modified file 'NEWS'
--- NEWS	
+++ NEWS	
@@ -98,8 +98,9 @@
     * Hitting CTRL-C while doing an SFTP push will no longer cause stale locks
       to be left in the SFTP repository. (Robert Collins, Martin Pool).
 
-    * New option 'diff --diff-prefix' to control how files are named in diff
-      output.  (Alexander Belchenko, Goffredo Baroncelli)
+    * New option 'diff --prefix' to control how files are named in diff
+      output, with shortcuts '-p0' and '-p1' corresponding to the options for 
+      GNU patch.  (Alexander Belchenko, Goffredo Baroncelli, Martin Pool)
 
     * If bzr shows an unexpected revision-history after pulling (perhaps due
       to a reweave) it can now be corrected by 'bzr reconcile'.

=== modified file 'bzrlib/builtins.py'
--- bzrlib/builtins.py	
+++ bzrlib/builtins.py	
@@ -976,41 +976,48 @@
     If files are listed, only the changes in those files are listed.
     Otherwise, all changes for the tree are listed.
 
+    "bzr diff -p1" is equivalent to "bzr diff --prefix old/:new/", and
+    produces patches suitable for "patch -p1".
+
     examples:
         bzr diff
         bzr diff -r1
         bzr diff -r1..2
-    """
-    # TODO: Allow diff across branches.
+        bzr diff --diff-prefix old/:new/
+        bzr diff bzr.mine bzr.dev
+        bzr diff foo.c
+    """
     # TODO: Option to use external diff command; could be GNU diff, wdiff,
     #       or a graphical diff.
 
     # TODO: Python difflib is not exactly the same as unidiff; should
     #       either fix it up or prefer to use an external diff.
 
-    # TODO: If a directory is given, diff everything under that.
-
     # TODO: Selected-file diff is inefficient and doesn't show you
     #       deleted files.
 
     # TODO: This probably handles non-Unix newlines poorly.
     
     takes_args = ['file*']
-    takes_options = ['revision', 'diff-options', 'diff-prefix']
+    takes_options = ['revision', 'diff-options', 'prefix']
     aliases = ['di', 'dif']
 
     @display_command
     def run(self, revision=None, file_list=None, diff_options=None,
-       diff_prefix=None):
+            prefix=None):
         from bzrlib.diff import diff_cmd_helper, show_diff_trees
 
-        if diff_prefix:
-            if not ':' in diff_prefix:
-                 raise BzrError("--diff-prefix expects two values separated by a colon")            
-            old_label,new_label=diff_prefix.split(":")
-        else:
-            old_label='a/'
-            new_label='b/'
+        if (prefix is None) or (prefix == '0'):
+            # diff -p0 format
+            old_label = ''
+            new_label = ''
+        elif prefix == '1':
+            old_label = 'old/'
+            new_label = 'new/'
+        else:
+            if not ':' in prefix:
+                 raise BzrError("--diff-prefix expects two values separated by a colon")
+            old_label, new_label = prefix.split(":")
         
         try:
             tree1, file_list = internal_tree_files(file_list)

=== modified file 'bzrlib/diff.py'
--- bzrlib/diff.py	
+++ bzrlib/diff.py	
@@ -286,12 +286,12 @@
     has_changes = 0
     for path, file_id, kind in delta.removed:
         has_changes = 1
-        print >>to_file, '=== removed %s %r' % (kind, old_label + path)
+        print >>to_file, '=== removed %s %r' % (kind, path)
         old_tree.inventory[file_id].diff(diff_file, old_label + path, old_tree,
                                          DEVNULL, None, None, to_file)
     for path, file_id, kind in delta.added:
         has_changes = 1
-        print >>to_file, '=== added %s %r' % (kind, new_label + path)
+        print >>to_file, '=== added %s %r' % (kind, path)
         new_tree.inventory[file_id].diff(diff_file, new_label + path, new_tree,
                                          DEVNULL, None, None, to_file, 
                                          reverse=True)
@@ -300,15 +300,14 @@
         has_changes = 1
         prop_str = get_prop_change(meta_modified)
         print >>to_file, '=== renamed %s %r => %r%s' % (
-                    kind, old_label + old_path, new_label + new_path, prop_str)
+                    kind, old_path, new_path, prop_str)
         _maybe_diff_file_or_symlink(old_label, old_path, old_tree, file_id,
                                     new_label, new_path, new_tree,
                                     text_modified, kind, to_file, diff_file)
     for path, file_id, kind, text_modified, meta_modified in delta.modified:
         has_changes = 1
         prop_str = get_prop_change(meta_modified)
-        print >>to_file, '=== modified %s %r%s' % (kind, old_label + path,
-                    prop_str)
+        print >>to_file, '=== modified %s %r%s' % (kind, path, prop_str)
         if text_modified:
             _maybe_diff_file_or_symlink(old_label, path, old_tree, file_id,
                                         new_label, path, new_tree,

=== modified file 'bzrlib/option.py'
--- bzrlib/option.py	
+++ bzrlib/option.py	
@@ -165,8 +165,6 @@
 _global_option('basis', type=str)
 _global_option('bound')
 _global_option('diff-options', type=str)
-_global_option('diff-prefix', type=str, 
-               help='Set prefixes to added to old and new filenames, as two values separated by a colon')
 _global_option('help',
                help='show help message')
 _global_option('file', type=unicode)
@@ -175,6 +173,9 @@
 _global_option('forward')
 _global_option('message', type=unicode)
 _global_option('no-recurse')
+_global_option('prefix', type=str, 
+               help='Set prefixes to added to old and new filenames, as '
+                    'two values separated by a colon.')
 _global_option('profile',
                help='show performance profiling information')
 _global_option('revision', type=_parse_revision_str)
@@ -218,3 +219,4 @@
 Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
 Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']
 Option.SHORT_OPTIONS['q'] = Option.OPTIONS['quiet']
+Option.SHORT_OPTIONS['p'] = Option.OPTIONS['prefix']

=== modified file 'bzrlib/tests/blackbox/test_diff.py'
--- bzrlib/tests/blackbox/test_diff.py	
+++ bzrlib/tests/blackbox/test_diff.py	
@@ -30,10 +30,10 @@
     def make_example_branch(test):
         # FIXME: copied from test_too_much -- share elsewhere?
         test.runbzr('init')
-        file('hello', 'wt').write('foo')
+        file('hello', 'wt').write('foo\n')
         test.runbzr('add hello')
         test.runbzr('commit -m setup hello')
-        file('goodbye', 'wt').write('baz')
+        file('goodbye', 'wt').write('baz\n')
         test.runbzr('add goodbye')
         test.runbzr('commit -m setup goodbye')
 
@@ -50,6 +50,54 @@
         os.unlink('moo')
         self.runbzr('diff')
 
+    def test_diff_prefix(self):
+        """diff --prefix appends to filenames in output"""
+        self.make_example_branch()
+        file('hello', 'wt').write('hello world!\n')
+        out, err = self.runbzr('diff --prefix old/:new/', retcode=1)
+        self.assertEquals(err, '')
+        self.assertEqualDiff(out, '''\
+=== modified file 'hello'
+--- old/hello\t
++++ new/hello\t
+@@ -1,1 +1,1 @@
+-foo
++hello world!
+
+''')
+
+    def test_diff_p1(self):
+        """diff -p1 produces lkml-style diffs"""
+        self.make_example_branch()
+        file('hello', 'wt').write('hello world!\n')
+        out, err = self.runbzr('diff -p1', retcode=1)
+        self.assertEquals(err, '')
+        self.assertEqualDiff(out, '''\
+=== modified file 'hello'
+--- old/hello\t
++++ new/hello\t
+@@ -1,1 +1,1 @@
+-foo
++hello world!
+
+''')
+
+    def test_diff_p0(self):
+        """diff -p0 produces diffs with no prefix"""
+        self.make_example_branch()
+        file('hello', 'wt').write('hello world!\n')
+        out, err = self.runbzr('diff -p0', retcode=1)
+        self.assertEquals(err, '')
+        self.assertEqualDiff(out, '''\
+=== modified file 'hello'
+--- hello\t
++++ hello\t
+@@ -1,1 +1,1 @@
+-foo
++hello world!
+
+''')
+
     def test_diff_nonexistent(self):
         # Get an error from a file that does not exist at all
         # (Malone #3619)
@@ -82,18 +130,18 @@
         output = self.run_bzr_captured(['diff', '-r', 'branch:branch2', 
                                         'branch1'],
                                        retcode=1)
-        self.assertEquals(("=== modified file 'a/file'\n"
-                           "--- a/file\t\n"
-                           "+++ b/file\t\n"
+        self.assertEquals(("=== modified file 'file'\n"
+                           "--- file\t\n"
+                           "+++ file\t\n"
                            "@@ -1,1 +1,1 @@\n"
                            "-new content\n"
                            "+contents of branch1/file\n"
                            "\n", ''), output)
         output = self.run_bzr_captured(['diff', 'branch2', 'branch1'],
                                        retcode=1)
-        self.assertEqualDiff(("=== modified file 'a/file'\n"
-                              "--- a/file\t\n"
-                              "+++ b/file\t\n"
+        self.assertEqualDiff(("=== modified file 'file'\n"
+                              "--- file\t\n"
+                              "+++ file\t\n"
                               "@@ -1,1 +1,1 @@\n"
                               "-new content\n"
                               "+contents of branch1/file\n"
@@ -144,23 +192,23 @@
         super(TestDiffLabels, self).make_example_branch()
         self.runbzr('remove hello')
         diff = self.run_bzr_captured(['diff'], retcode=1)
-        self.assertTrue("=== removed file 'a/hello'" in diff[0])
+        self.assertTrue("=== removed file 'hello'" in diff[0])
 
     def test_diff_label_added(self):
         super(TestDiffLabels, self).make_example_branch()
         file('barbar', 'wt').write('barbar')
         self.runbzr('add barbar')
         diff = self.run_bzr_captured(['diff'], retcode=1)
-        self.assertTrue("=== added file 'b/barbar'" in diff[0])
+        self.assertTrue("=== added file 'barbar'" in diff[0])
 
     def test_diff_label_modified(self):
         super(TestDiffLabels, self).make_example_branch()
         file('hello', 'wt').write('barbar')
         diff = self.run_bzr_captured(['diff'], retcode=1)
-        self.assertTrue("=== modified file 'a/hello'" in diff[0])
+        self.assertTrue("=== modified file 'hello'" in diff[0])
 
     def test_diff_label_renamed(self):
         super(TestDiffLabels, self).make_example_branch()
         self.runbzr('rename hello gruezi')
         diff = self.run_bzr_captured(['diff'], retcode=1)
-        self.assertTrue("=== renamed file 'a/hello' => 'b/gruezi'" in diff[0])
+        self.assertTrue("=== renamed file 'hello' => 'gruezi'" in diff[0])

=== modified file 'tutorial.txt'
--- tutorial.txt	
+++ tutorial.txt	
@@ -246,6 +246,11 @@
 
     % bzr diff --diff-options --side-by-side foo
 
+Some projects prefer patches to show a prefix at the start of the path for
+old and new files.  The --prefix option can be used to provide such a prefix. 
+As a shortcut, ``bzr diff -p1`` produces a form that works with the 
+command ``patch -p1``.
+
 Committing changes
 ==================
 



More information about the bazaar mailing list