diff mercurial/simplemerge.py @ 48578:77e24ee8994b

simplemerge: take arguments as annotated context objects The labels we put in conflict markers are formatted so the part before the ':' (typically says things like "local") is padded so the ':' is aligned among the labels. That means that if you specify a long label for "base" but the conflict marker style is "merge" (i.e. 2-way), the other two will have unwanted padding. We often don't specify a label for the base, so we don't notice the problem (and it may very well be that it didn't exist before my D11972). I think the best fix is to pass the labels along with the context objects, so the low-level code that switches on the marker style to use (i.e. `simplemerge`) can do the formatting. This patch starts doing that by passing a fully-formatted label to `simplemerge`. A coming patch will move the formatting to `simplemerge`. Differential Revision: https://phab.mercurial-scm.org/D12013
author Martin von Zweigbergk <martinvonz@google.com>
date Thu, 20 Jan 2022 11:00:30 -0800
parents 50de08904c63
children 3c8cc987672e
line wrap: on
line diff
--- a/mercurial/simplemerge.py	Thu Jan 20 09:04:39 2022 -0800
+++ b/mercurial/simplemerge.py	Thu Jan 20 11:00:30 2022 -0800
@@ -19,6 +19,7 @@
 from __future__ import absolute_import
 
 from .i18n import _
+from .thirdparty import attr
 from . import (
     error,
     mdiff,
@@ -284,13 +285,14 @@
     return text
 
 
-def _picklabels(overrides):
-    if len(overrides) > 3:
-        raise error.Abort(_(b"can only specify three labels."))
-    result = [None, None, None]
-    for i, override in enumerate(overrides):
-        result[i] = override
-    return result
+def _format_labels(*inputs):
+    labels = []
+    for input in inputs:
+        if input.label:
+            labels.append(input.label)
+        else:
+            labels.append(None)
+    return labels
 
 
 def _detect_newline(m3):
@@ -462,7 +464,13 @@
     return lines
 
 
-def simplemerge(ui, localctx, basectx, otherctx, **opts):
+@attr.s
+class MergeInput(object):
+    fctx = attr.ib()
+    label = attr.ib(default=None)
+
+
+def simplemerge(ui, local, base, other, **opts):
     """Performs the simplemerge algorithm.
 
     The merged result is written into `localctx`.
@@ -479,9 +487,9 @@
         return _verifytext(ctx.decodeddata(), ctx.path(), ui, opts)
 
     try:
-        localtext = readctx(localctx)
-        basetext = readctx(basectx)
-        othertext = readctx(otherctx)
+        localtext = readctx(local.fctx)
+        basetext = readctx(base.fctx)
+        othertext = readctx(other.fctx)
     except error.Abort:
         return True
 
@@ -495,20 +503,22 @@
     elif mode == b'other':
         lines = _resolve(m3, (2,))
     else:
-        name_a, name_b, name_base = _picklabels(opts.get('label', []))
         if mode == b'mergediff':
-            lines, conflicts = render_mergediff(m3, name_a, name_b, name_base)
+            labels = _format_labels(local, other, base)
+            lines, conflicts = render_mergediff(m3, *labels)
         elif mode == b'merge3':
-            lines, conflicts = render_merge3(m3, name_a, name_b, name_base)
+            labels = _format_labels(local, other, base)
+            lines, conflicts = render_merge3(m3, *labels)
         else:
-            lines, conflicts = render_minimized(m3, name_a, name_b)
+            labels = _format_labels(local, other)
+            lines, conflicts = render_minimized(m3, *labels)
 
     mergedtext = b''.join(lines)
     if opts.get('print'):
         ui.fout.write(mergedtext)
     else:
-        # localctx.flags() already has the merged flags (done in
+        # local.fctx.flags() already has the merged flags (done in
         # mergestate.resolve())
-        localctx.write(mergedtext, localctx.flags())
+        local.fctx.write(mergedtext, local.fctx.flags())
 
     return conflicts