D6954: sidedatacopies: move various copies related function to the copies modules

marmoute (Pierre-Yves David) phabricator at mercurial-scm.org
Wed Oct 9 17:32:19 EDT 2019


marmoute updated this revision to Diff 17009.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D6954?vs=16955&id=17009

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D6954/new/

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

AFFECTED FILES
  mercurial/changelog.py
  mercurial/context.py
  mercurial/copies.py
  mercurial/scmutil.py

CHANGE DETAILS

diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py
--- a/mercurial/scmutil.py
+++ b/mercurial/scmutil.py
@@ -2219,23 +2219,3 @@
         mark,
         mark,
     )
-
-
-def computechangesetfilesadded(ctx):
-    """return the list of files added in a changeset
-    """
-    added = []
-    for f in ctx.files():
-        if not any(f in p for p in ctx.parents()):
-            added.append(f)
-    return added
-
-
-def computechangesetfilesremoved(ctx):
-    """return the list of files removed in a changeset
-    """
-    removed = []
-    for f in ctx.files():
-        if f not in ctx:
-            removed.append(f)
-    return removed
diff --git a/mercurial/copies.py b/mercurial/copies.py
--- a/mercurial/copies.py
+++ b/mercurial/copies.py
@@ -14,6 +14,7 @@
 from .i18n import _
 
 from . import (
+    error,
     match as matchmod,
     node,
     pathutil,
@@ -855,6 +856,26 @@
             wctx[dst].markcopied(src)
 
 
+def computechangesetfilesadded(ctx):
+    """return the list of files added in a changeset
+    """
+    added = []
+    for f in ctx.files():
+        if not any(f in p for p in ctx.parents()):
+            added.append(f)
+    return added
+
+
+def computechangesetfilesremoved(ctx):
+    """return the list of files removed in a changeset
+    """
+    removed = []
+    for f in ctx.files():
+        if f not in ctx:
+            removed.append(f)
+    return removed
+
+
 def computechangesetcopies(ctx):
     """return the copies data for a changeset
 
@@ -879,3 +900,58 @@
         elif src in p2 and p2[src].filenode() == srcnode:
             p2copies[dst] = src
     return p1copies, p2copies
+
+
+def encodecopies(files, copies):
+    items = []
+    for i, dst in enumerate(files):
+        if dst in copies:
+            items.append(b'%d\0%s' % (i, copies[dst]))
+    if len(items) != len(copies):
+        raise error.ProgrammingError(
+            b'some copy targets missing from file list'
+        )
+    return b"\n".join(items)
+
+
+def decodecopies(files, data):
+    try:
+        copies = {}
+        if not data:
+            return copies
+        for l in data.split(b'\n'):
+            strindex, src = l.split(b'\0')
+            i = int(strindex)
+            dst = files[i]
+            copies[dst] = src
+        return copies
+    except (ValueError, IndexError):
+        # Perhaps someone had chosen the same key name (e.g. "p1copies") and
+        # used different syntax for the value.
+        return None
+
+
+def encodefileindices(files, subset):
+    subset = set(subset)
+    indices = []
+    for i, f in enumerate(files):
+        if f in subset:
+            indices.append(b'%d' % i)
+    return b'\n'.join(indices)
+
+
+def decodefileindices(files, data):
+    try:
+        subset = []
+        if not data:
+            return subset
+        for strindex in data.split(b'\n'):
+            i = int(strindex)
+            if i < 0 or i >= len(files):
+                return None
+            subset.append(files[i])
+        return subset
+    except (ValueError, IndexError):
+        # Perhaps someone had chosen the same key name (e.g. "added") and
+        # used different syntax for the value.
+        return None
diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -546,7 +546,7 @@
                 filesadded = None
         if filesadded is None:
             if compute_on_none:
-                filesadded = scmutil.computechangesetfilesadded(self)
+                filesadded = copies.computechangesetfilesadded(self)
             else:
                 filesadded = []
         return filesadded
@@ -565,7 +565,7 @@
                 filesremoved = None
         if filesremoved is None:
             if compute_on_none:
-                filesremoved = scmutil.computechangesetfilesremoved(self)
+                filesremoved = copies.computechangesetfilesremoved(self)
             else:
                 filesremoved = []
         return filesremoved
diff --git a/mercurial/changelog.py b/mercurial/changelog.py
--- a/mercurial/changelog.py
+++ b/mercurial/changelog.py
@@ -16,6 +16,7 @@
 from .thirdparty import attr
 
 from . import (
+    copies,
     encoding,
     error,
     pycompat,
@@ -89,61 +90,6 @@
     return b"\0".join(items)
 
 
-def encodecopies(files, copies):
-    items = []
-    for i, dst in enumerate(files):
-        if dst in copies:
-            items.append(b'%d\0%s' % (i, copies[dst]))
-    if len(items) != len(copies):
-        raise error.ProgrammingError(
-            b'some copy targets missing from file list'
-        )
-    return b"\n".join(items)
-
-
-def decodecopies(files, data):
-    try:
-        copies = {}
-        if not data:
-            return copies
-        for l in data.split(b'\n'):
-            strindex, src = l.split(b'\0')
-            i = int(strindex)
-            dst = files[i]
-            copies[dst] = src
-        return copies
-    except (ValueError, IndexError):
-        # Perhaps someone had chosen the same key name (e.g. "p1copies") and
-        # used different syntax for the value.
-        return None
-
-
-def encodefileindices(files, subset):
-    subset = set(subset)
-    indices = []
-    for i, f in enumerate(files):
-        if f in subset:
-            indices.append(b'%d' % i)
-    return b'\n'.join(indices)
-
-
-def decodefileindices(files, data):
-    try:
-        subset = []
-        if not data:
-            return subset
-        for strindex in data.split(b'\n'):
-            i = int(strindex)
-            if i < 0 or i >= len(files):
-                return None
-            subset.append(files[i])
-        return subset
-    except (ValueError, IndexError):
-        # Perhaps someone had chosen the same key name (e.g. "added") and
-        # used different syntax for the value.
-        return None
-
-
 def stripdesc(desc):
     """strip trailing whitespace and leading and trailing empty lines"""
     return b'\n'.join([l.rstrip() for l in desc.splitlines()]).strip(b'\n')
@@ -368,7 +314,7 @@
             rawindices = self.extra.get(b'filesadded')
         if rawindices is None:
             return None
-        return decodefileindices(self.files, rawindices)
+        return copies.decodefileindices(self.files, rawindices)
 
     @property
     def filesremoved(self):
@@ -378,7 +324,7 @@
             rawindices = self.extra.get(b'filesremoved')
         if rawindices is None:
             return None
-        return decodefileindices(self.files, rawindices)
+        return copies.decodefileindices(self.files, rawindices)
 
     @property
     def p1copies(self):
@@ -387,7 +333,7 @@
         else:
             rawcopies = self.extra.get(b'p1copies')
         if rawcopies is not None:
-            rawcopies = decodecopies(self.files, rawcopies)
+            rawcopies = copies.decodecopies(self.files, rawcopies)
         return rawcopies
 
     @property
@@ -398,7 +344,7 @@
             rawcopies = self.extra.get(b'p2copies')
         if rawcopies is None:
             return None
-        return decodecopies(self.files, rawcopies)
+        return copies.decodecopies(self.files, rawcopies)
 
     @property
     def description(self):
@@ -711,13 +657,13 @@
             ):
                 extra.pop(name, None)
         if p1copies is not None:
-            p1copies = encodecopies(sortedfiles, p1copies)
+            p1copies = copies.encodecopies(sortedfiles, p1copies)
         if p2copies is not None:
-            p2copies = encodecopies(sortedfiles, p2copies)
+            p2copies = copies.encodecopies(sortedfiles, p2copies)
         if filesadded is not None:
-            filesadded = encodefileindices(sortedfiles, filesadded)
+            filesadded = copies.encodefileindices(sortedfiles, filesadded)
         if filesremoved is not None:
-            filesremoved = encodefileindices(sortedfiles, filesremoved)
+            filesremoved = copies.encodefileindices(sortedfiles, filesremoved)
         if self._copiesstorage == b'extra':
             extrasentries = p1copies, p2copies, filesadded, filesremoved
             if extra is None and any(x is not None for x in extrasentries):



To: marmoute, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list