Patchwork D11976: simplemerge: use same newline detection for :mergediff as for :merge[3]

login
register
mail settings
Submitter phabricator
Date Jan. 12, 2022, 6:56 a.m.
Message ID <differential-rev-PHID-DREV-wgx2ooqyu2vvk2hcjzge-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/50299/
State New
Headers show

Comments

phabricator - Jan. 12, 2022, 6:56 a.m.
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




To: martinvonz, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

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)