Patchwork checkcopies: extract the '_related' closure

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 11, 2016, 12:20 a.m.
Message ID <335c2c5695971db93149.1476145246@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/17027/
State Accepted
Headers show

Comments

Pierre-Yves David - Oct. 11, 2016, 12:20 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1476142148 -7200
#      Tue Oct 11 01:29:08 2016 +0200
# Node ID 335c2c5695971db93149eb02d03835c0fca6faf8
# Parent  8d079c0594b35dfbf57baf8d83fde686a946920a
# EXP-Topic checkcopies
checkcopies: extract the '_related' closure

There is not need for it to be a closure.
Yuya Nishihara - Oct. 12, 2016, 1:44 p.m.
On Tue, 11 Oct 2016 02:20:46 +0200, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
> # Date 1476142148 -7200
> #      Tue Oct 11 01:29:08 2016 +0200
> # Node ID 335c2c5695971db93149eb02d03835c0fca6faf8
> # Parent  8d079c0594b35dfbf57baf8d83fde686a946920a
> # EXP-Topic checkcopies
> checkcopies: extract the '_related' closure

Nice. Rebased onto the previous checkcopies series and pushed, thanks.

Patch

diff --git a/mercurial/copies.py b/mercurial/copies.py
--- a/mercurial/copies.py
+++ b/mercurial/copies.py
@@ -454,6 +454,40 @@  def mergecopies(repo, c1, c2, ca):
 
     return copy, movewithdir, diverge, renamedelete
 
+def _related(f1, f2, limit):
+    """return True if f1 and f2 filectx have a common ancestor
+
+    Walk back to common ancestor to see if the two files originate
+    from the same file. Since workingfilectx's rev() is None it messes
+    up the integer comparison logic, hence the pre-step check for
+    None (f1 and f2 can only be workingfilectx's initially).
+    """
+
+    if f1 == f2:
+        return f1 # a match
+
+    g1, g2 = f1.ancestors(), f2.ancestors()
+    try:
+        f1r, f2r = f1.linkrev(), f2.linkrev()
+
+        if f1r is None:
+            f1 = next(g1)
+        if f2r is None:
+            f2 = next(g2)
+
+        while True:
+            f1r, f2r = f1.linkrev(), f2.linkrev()
+            if f1r > f2r:
+                f1 = next(g1)
+            elif f2r > f1r:
+                f2 = next(g2)
+            elif f1 == f2:
+                return f1 # a match
+            elif f1r == f2r or f1r < limit or f2r < limit:
+                return False # copy no longer relevant
+    except StopIteration:
+        return False
+
 def _checkcopies(ctx, f, m1, m2, ca, limit, diverge, copy, fullcopy):
     """
     check possible copies of f from m1 to m2
@@ -477,37 +511,6 @@  def _checkcopies(ctx, f, m1, m2, ca, lim
     ma = ca.manifest()
     getfctx = _makegetfctx(ctx)
 
-    def _related(f1, f2, limit):
-        # Walk back to common ancestor to see if the two files originate
-        # from the same file. Since workingfilectx's rev() is None it messes
-        # up the integer comparison logic, hence the pre-step check for
-        # None (f1 and f2 can only be workingfilectx's initially).
-
-        if f1 == f2:
-            return f1 # a match
-
-        g1, g2 = f1.ancestors(), f2.ancestors()
-        try:
-            f1r, f2r = f1.linkrev(), f2.linkrev()
-
-            if f1r is None:
-                f1 = next(g1)
-            if f2r is None:
-                f2 = next(g2)
-
-            while True:
-                f1r, f2r = f1.linkrev(), f2.linkrev()
-                if f1r > f2r:
-                    f1 = next(g1)
-                elif f2r > f1r:
-                    f2 = next(g2)
-                elif f1 == f2:
-                    return f1 # a match
-                elif f1r == f2r or f1r < limit or f2r < limit:
-                    return False # copy no longer relevant
-        except StopIteration:
-            return False
-
     of = None
     seen = set([f])
     for oc in getfctx(f, m1[f]).ancestors():