[MERGE] auto-detect line endings for merge

Aaron Bentley aaron.bentley at utoronto.ca
Mon Mar 12 20:13:26 GMT 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aaron Bentley wrote:
> Hi all,
> 
> When Merge3 is writing out conflict markers, it always uses UNIX (LF)
> newlines.  This isn't really appropriate in text files that use CRLF or
> CR newlines.  Vim, for example, opens mixed files in UNIX mode, so every
>  source line ends with ^M.
> 
> This patch just uses whatever newlines were used on the first line of
> the THIS text, if any.  Simple, hacky, but pretty effective.

Oops, forgot to use [MERGE] in the subject.

Aaron

- ------------------------------------------------------------------------

# Bazaar merge directive format 1
# revision_id: abentley at panoramicfeedback.com-20070312195641-\
#   ezjnseqwgjtkh0iu
# target_branch: http://bazaar-vcs.org/bzr/bzr.dev
# testament_sha1: 11cbf0b2b1cf00b3f8379b07496f682b76c9012a
# timestamp: 2007-03-12 15:57:02 -0400
# source_branch: http://panoramicfeedback.com/opensource/bzr/repo\
#   /bzr.ab
#
# Bazaar revision bundle v0.9
#
# message:
#   merge3 auto-detects line endings for conflict markers
# committer: Aaron Bentley <abentley at panoramicfeedback.com>
# date: Mon 2007-03-12 15:56:41.592000008 -0400

=== modified file bzrlib/merge3.py
- --- bzrlib/merge3.py
+++ bzrlib/merge3.py
@@ -88,6 +88,12 @@
                     reprocess=False):
         """Return merge in cvs-like form.
         """
+        newline = '\n'
+        if len(self.a) > 0:
+            if self.a[0].endswith('\r\n'):
+                newline = '\r\n'
+            elif self.a[0].endswith('\r'):
+                newline = '\r'
         if base_marker and reprocess:
             raise CantReprocessAndShowBase()
         if name_a:
@@ -111,17 +117,17 @@
                 for i in range(t[1], t[2]):
                     yield self.b[i]
             elif what == 'conflict':
- -                yield start_marker + '\n'
+                yield start_marker + newline
                 for i in range(t[3], t[4]):
                     yield self.a[i]
                 if base_marker is not None:
- -                    yield base_marker + '\n'
+                    yield base_marker + newline
                     for i in range(t[1], t[2]):
                         yield self.base[i]
- -                yield mid_marker + '\n'
+                yield mid_marker + newline
                 for i in range(t[5], t[6]):
                     yield self.b[i]
- -                yield end_marker + '\n'
+                yield end_marker + newline
             else:
                 raise ValueError(what)


=== modified file bzrlib/tests/test_merge3.py
- --- bzrlib/tests/test_merge3.py
+++ bzrlib/tests/test_merge3.py
@@ -363,3 +363,23 @@

     def test_binary(self):
         self.assertRaises(BinaryFile, Merge3, ['\x00'], ['a'], ['b'])
+
+    def test_dos_text(self):
+        base_text = 'a\r\n'
+        this_text = 'b\r\n'
+        other_text = 'c\r\n'
+        m3 = Merge3(base_text.splitlines(True),
other_text.splitlines(True),
+                    this_text.splitlines(True))
+        m_lines = m3.merge_lines('OTHER', 'THIS')
+        self.assertEqual('<<<<<<< OTHER\r\nc\r\n=======\r\nb\r\n'
+            '>>>>>>> THIS\r\n'.splitlines(True), list(m_lines))
+
+    def test_mac_text(self):
+        base_text = 'a\r'
+        this_text = 'b\r'
+        other_text = 'c\r'
+        m3 = Merge3(base_text.splitlines(True),
other_text.splitlines(True),
+                    this_text.splitlines(True))
+        m_lines = m3.merge_lines('OTHER', 'THIS')
+        self.assertEqual('<<<<<<< OTHER\rc\r=======\rb\r'
+            '>>>>>>> THIS\r'.splitlines(True), list(m_lines))

=== modified directory  //
last-changed:abentley at panoramicfeedback.com-20070312
... 195641-ezjnseqwgjtkh0iu
# revision id:
abentley at panoramicfeedback.com-20070312195641-ezjnseqwgjtkh0iu
# sha1: 11cbf0b2b1cf00b3f8379b07496f682b76c9012a
# inventory sha1: 1f14731347bdc4eb2aba0c9f17ed9f867fc49c35
# parent ids:
#   aaron.bentley at utoronto.ca-20070311203448-a86mnhetckle18co
# base id: aaron.bentley at utoronto.ca-20070311203448-a86mnhetckle18co
# properties:
#   branch-nick: Aaron's mergeable stuff


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFF9bRm0F+nu1YWqI0RAp+DAJ4k4MnhuTQvffFlq5TFvY+Vnt9lVwCfXdVZ
x0mlPj8ei1e+GpzfY2gFkWY=
=fRBu
-----END PGP SIGNATURE-----



More information about the bazaar mailing list