filemerge: work with `simplemerge.MergeInput` in `filemerge()`
authorMartin von Zweigbergk <martinvonz@google.com>
Thu, 20 Jan 2022 14:42:50 -0800
changeset 48585 07069fcd9a6e
parent 48584 74973a6d4e67
child 48586 fd9fe2658cda
filemerge: work with `simplemerge.MergeInput` in `filemerge()` We currently pass around pairs of file context objects and labels between functions in the `filemerge` module. I plan to pass around `simplemerge.MergeInput` instead. This patch prepares for that by using the type internally in `filemerge.filemerge()`. Differential Revision: https://phab.mercurial-scm.org/D12017
mercurial/filemerge.py
--- a/mercurial/filemerge.py	Thu Jan 20 14:13:12 2022 -0800
+++ b/mercurial/filemerge.py	Thu Jan 20 14:42:50 2022 -0800
@@ -805,35 +805,32 @@
         return True, r, False
 
 
-def _formatlabel(ctx, template, label, pad):
+def _formatlabel(input, template, pad):
     """Applies the given template to the ctx, prefixed by the label.
 
     Pad is the minimum width of the label prefix, so that multiple markers
     can have aligned templated parts.
     """
+    ctx = input.fctx.changectx()
     if ctx.node() is None:
         ctx = ctx.p1()
 
     props = {b'ctx': ctx}
     templateresult = template.renderdefault(props)
 
-    label = (b'%s:' % label).ljust(pad + 1)
+    label = (b'%s:' % input.label).ljust(pad + 1)
     mark = b'%s %s' % (label, templateresult)
     mark = mark.splitlines()[0]  # split for safety
 
     # 8 for the prefix of conflict marker lines (e.g. '<<<<<<< ')
-    return stringutil.ellipsis(mark, 80 - 8)
+    input.label = stringutil.ellipsis(mark, 80 - 8)
 
 
-def _formatlabels(repo, fcd, fco, fca, labels, tool=None):
+def _formatlabels(repo, inputs, tool=None):
     """Formats the given labels using the conflict marker template.
 
     Returns a list of formatted labels.
     """
-    cd = fcd.changectx()
-    co = fco.changectx()
-    ca = fca.changectx()
-
     ui = repo.ui
     template = ui.config(b'command-templates', b'mergemarker')
     if tool is not None:
@@ -844,14 +841,9 @@
         ui, template, defaults=templatekw.keywords, resources=tres
     )
 
-    pad = max(len(l) for l in labels)
+    pad = max(len(input.label) for input in inputs)
 
-    newlabels = [
-        _formatlabel(cd, tmpl, labels[0], pad),
-        _formatlabel(co, tmpl, labels[1], pad),
-        _formatlabel(ca, tmpl, labels[2], pad),
-    ]
-    return newlabels
+    return [_formatlabel(input, tmpl, pad) for input in inputs]
 
 
 def partextras(labels):
@@ -1052,8 +1044,19 @@
         labels = [b'local', b'other']
     if len(labels) < 3:
         labels.append(b'base')
+    local = simplemerge.MergeInput(fcd, labels[0])
+    other = simplemerge.MergeInput(fco, labels[1])
+    base = simplemerge.MergeInput(fca, labels[2])
     if mergetype == nomerge:
-        return func(repo, mynode, fcd, fco, fca, toolconf, labels)
+        return func(
+            repo,
+            mynode,
+            fcd,
+            fco,
+            fca,
+            toolconf,
+            [local.label, other.label, base.label],
+        )
 
     if orig != fco.path():
         ui.status(
@@ -1083,36 +1086,41 @@
         else:
             markerstyle = internalmarkerstyle
 
-        formattedlabels = labels
-        if markerstyle != b'basic':
-            formattedlabels = _formatlabels(
-                repo, fcd, fco, fca, labels, tool=tool
-            )
-
         if mergetype == fullmerge:
             # conflict markers generated by premerge will use 'detailed'
             # settings if either ui.mergemarkers or the tool's mergemarkers
             # setting is 'detailed'. This way tools can have basic labels in
             # space-constrained areas of the UI, but still get full information
             # in conflict markers if premerge is 'keep' or 'keep-merge3'.
-            premergelabels = labels
             labeltool = None
             if markerstyle != b'basic':
                 # respect 'tool's mergemarkertemplate (which defaults to
                 # command-templates.mergemarker)
                 labeltool = tool
             if internalmarkerstyle != b'basic' or markerstyle != b'basic':
-                premergelabels = _formatlabels(
-                    repo, fcd, fco, fca, premergelabels, tool=labeltool
-                )
+                _formatlabels(repo, [local, other, base], tool=labeltool)
 
             r = _premerge(
-                repo, fcd, fco, fca, toolconf, backup, labels=premergelabels
+                repo,
+                fcd,
+                fco,
+                fca,
+                toolconf,
+                backup,
+                labels=[local.label, other.label, base.label],
             )
             # we're done if premerge was successful (r is 0)
             if not r:
                 return r, False
 
+            # Reset to basic labels
+            local.label = labels[0]
+            other.label = labels[1]
+            base.label = labels[2]
+
+        if markerstyle != b'basic':
+            _formatlabels(repo, [local, other, base], tool=tool)
+
         needcheck, r, deleted = func(
             repo,
             mynode,
@@ -1121,7 +1129,7 @@
             fca,
             toolconf,
             backup,
-            labels=formattedlabels,
+            labels=[local.label, other.label, base.label],
         )
 
         if needcheck: