[Request] [+- ] D11976: simplemerge: use same newline detection for :mergediff as for :merge[3]

martinvonz (Martin von Zweigbergk) phabricator at mercurial-scm.org
Wed Jan 12 06:56:26 UTC 2022


martinvonz created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  The `:merge` and `:merge3` markers used the newline style detected
  from the base input. The `:mergediff` style always used LF as newline
  character. This patch teaches `:mergediff` to use the same detection
  as the others did.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D11976

AFFECTED FILES
  mercurial/simplemerge.py

CHANGE DETAILS

diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py
--- a/mercurial/simplemerge.py
+++ b/mercurial/simplemerge.py
@@ -341,6 +341,15 @@
     return result
 
 
+def _detect_newline(m3):
+    if len(m3.a) > 0:
+        if m3.a[0].endswith(b'\r\n'):
+            return b'\r\n'
+        elif m3.a[0].endswith(b'\r'):
+            return b'\r'
+    return b'\n'
+
+
 def render_markers(
     m3,
     name_a=None,
@@ -353,13 +362,8 @@
     minimize=False,
 ):
     """Return merge in cvs-like form."""
+    newline = _detect_newline(m3)
     conflicts = False
-    newline = b'\n'
-    if len(m3.a) > 0:
-        if m3.a[0].endswith(b'\r\n'):
-            newline = b'\r\n'
-        elif m3.a[0].endswith(b'\r'):
-            newline = b'\r'
     if name_a and start_marker:
         start_marker = start_marker + b' ' + name_a
     if name_b and end_marker:
@@ -391,6 +395,7 @@
 
 
 def render_mergediff(m3, name_a, name_b, name_base):
+    newline = _detect_newline(m3)
     lines = []
     conflicts = False
     for what, group_lines in m3.merge_groups():
@@ -432,20 +437,20 @@
                         for line in lines2[block[2] : block[3]]:
                             yield b'+' + line
 
-            lines.append(b"<<<<<<<\n")
+            lines.append(b"<<<<<<<" + newline)
             if matching_lines(a_blocks) < matching_lines(b_blocks):
-                lines.append(b"======= %s\n" % name_a)
+                lines.append(b"======= " + name_a + newline)
                 lines.extend(a_lines)
-                lines.append(b"------- %s\n" % name_base)
-                lines.append(b"+++++++ %s\n" % name_b)
+                lines.append(b"------- " + name_base + newline)
+                lines.append(b"+++++++ " + name_b + newline)
                 lines.extend(diff_lines(b_blocks, base_lines, b_lines))
             else:
-                lines.append(b"------- %s\n" % name_base)
-                lines.append(b"+++++++ %s\n" % name_a)
+                lines.append(b"------- " + name_base + newline)
+                lines.append(b"+++++++ " + name_a + newline)
                 lines.extend(diff_lines(a_blocks, base_lines, a_lines))
-                lines.append(b"======= %s\n" % name_b)
+                lines.append(b"======= " + name_b + newline)
                 lines.extend(b_lines)
-            lines.append(b">>>>>>>\n")
+            lines.append(b">>>>>>>" + newline)
             conflicts = True
         else:
             lines.extend(group_lines)



To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20220112/11e3c0a8/attachment-0001.html>


More information about the Mercurial-patches mailing list