Patchwork D376: filemerge: extract `_picklabels` as a helper function

login
register
mail settings
Submitter phabricator
Date Aug. 14, 2017, 6:15 a.m.
Message ID <differential-rev-PHID-DREV-vh644r3eh4qlpi7emj4j-req@phab.mercurial-scm.org>
Download mbox | patch
Permalink /patch/22958/
State Superseded
Headers show

Comments

phabricator - Aug. 14, 2017, 6:15 a.m.
phillco created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This shortens `simplemerge()` and is a bit cleaner, IMO.

REPOSITORY
  rHG Mercurial

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

AFFECTED FILES
  mercurial/simplemerge.py

CHANGE DETAILS




To: phillco, #hg-reviewers
Cc: mercurial-devel
phabricator - Aug. 19, 2017, 5 a.m.
martinvonz added inline comments.

INLINE COMMENTS

> simplemerge.py:411-423
> +def _picklabels(defaults, overrides):
> +    name_a, name_b, name_base = defaults
> +
> +    if len(overrides) > 0:
> +        name_a = overrides[0]
> +    if len(overrides) > 1:
> +        name_b = overrides[1]

def _picklabels(defaults, overrides):
    if len(overrides) > 3:
          raise error.Abort(_("can only specify three labels."))
    result = defaults[:]
    for i, override in enumerate(overrides):
      result[i] = override
    return result

no?

REPOSITORY
  rHG Mercurial

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

To: phillco, #hg-reviewers
Cc: martinvonz, mercurial-devel
phabricator - Aug. 19, 2017, 5:43 p.m.
phillco added inline comments.

INLINE COMMENTS

> martinvonz wrote in simplemerge.py:411-423
>   def _picklabels(defaults, overrides):
>     if len(overrides) > 3:
>           raise error.Abort(_("can only specify three labels."))
>     result = defaults[:]
>     for i, override in enumerate(overrides):
>       result[i] = override
>     return result
> 
> no?

I think so, not sure why the original was so weird. I'll replace it.

REPOSITORY
  rHG Mercurial

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

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

Patch

diff --git a/mercurial/simplemerge.py b/mercurial/simplemerge.py
--- a/mercurial/simplemerge.py
+++ b/mercurial/simplemerge.py
@@ -408,6 +408,20 @@ 
             raise error.Abort(msg)
     return text
 
+def _picklabels(defaults, overrides):
+    name_a, name_b, name_base = defaults
+
+    if len(overrides) > 0:
+        name_a = overrides[0]
+    if len(overrides) > 1:
+        name_b = overrides[1]
+    if len(overrides) > 2:
+        name_base = overrides[2]
+    if len(overrides) > 3:
+        raise error.Abort(_("can only specify three labels."))
+
+    return [name_a, name_b, name_base]
+
 def simplemerge(ui, localfile, basefile, otherfile,
                 localctx=None, basectx=None, otherctx=None, repo=None, **opts):
     """Performs the simplemerge algorithm.
@@ -446,23 +460,11 @@ 
             self.ctx.write(self.text, self.ctx.flags())
 
     mode = opts.get('mode','merge')
-    if mode == 'union':
-        name_a = None
-        name_b = None
-        name_base = None
-    else:
-        name_a = localfile
-        name_b = otherfile
-        name_base = None
-        labels = opts.get('label', [])
-        if len(labels) > 0:
-            name_a = labels[0]
-        if len(labels) > 1:
-            name_b = labels[1]
-        if len(labels) > 2:
-            name_base = labels[2]
-        if len(labels) > 3:
-            raise error.Abort(_("can only specify three labels."))
+    name_a, name_b, name_base = None, None, None
+    if mode != 'union':
+        name_a, name_b, name_base = _picklabels([localfile,
+                                                 otherfile, None],
+                                                opts.get('label', []))
 
     try:
         localtext = readctx(localctx) if localctx else readfile(localfile)