Patchwork D10360: sidedata: move sidedata-related utils to the dedicated module

login
register
mail settings
Submitter phabricator
Date April 10, 2021, 7:17 p.m.
Message ID <differential-rev-PHID-DREV-v5vvautvlx2maiftjoc3-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48678/
State Superseded
Headers show

Comments

phabricator - April 10, 2021, 7:17 p.m.
Alphare created this revision.
Herald added a reviewer: indygreg.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  mercurial/changegroup.py
  mercurial/localrepo.py
  mercurial/metadata.py
  mercurial/revlog.py
  mercurial/revlogutils/sidedata.py
  mercurial/upgrade_utils/engine.py
  mercurial/utils/storageutil.py
  tests/testlib/ext-sidedata.py

CHANGE DETAILS




To: Alphare, indygreg, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/tests/testlib/ext-sidedata.py b/tests/testlib/ext-sidedata.py
--- a/tests/testlib/ext-sidedata.py
+++ b/tests/testlib/ext-sidedata.py
@@ -15,7 +15,6 @@ 
     nullrev,
 )
 from mercurial import (
-    changegroup,
     extensions,
     requirements,
     revlog,
@@ -90,7 +89,7 @@ 
         )
         dstrepo.register_wanted_sidedata(b"whatever")
 
-    return changegroup.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata)
+    return sidedata.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata)
 
 
 def extsetup(ui):
diff --git a/mercurial/utils/storageutil.py b/mercurial/utils/storageutil.py
--- a/mercurial/utils/storageutil.py
+++ b/mercurial/utils/storageutil.py
@@ -499,7 +499,7 @@ 
         sidedata_flags = (0, 0)
         if sidedata_helpers:
             old_sidedata = store.sidedata(rev)
-            sidedata, sidedata_flags = run_sidedata_helpers(
+            sidedata, sidedata_flags = sidedatamod.run_sidedata_helpers(
                 store=store,
                 sidedata_helpers=sidedata_helpers,
                 sidedata=old_sidedata,
@@ -532,30 +532,6 @@ 
         prevrev = rev
 
 
-def run_sidedata_helpers(store, sidedata_helpers, sidedata, rev):
-    """Returns the sidedata for the given revision after running through
-    the given helpers.
-    - `store`: the revlog this applies to (changelog, manifest, or filelog
-      instance)
-    - `sidedata_helpers`: see `storageutil.emitrevisions`
-    - `sidedata`: previous sidedata at the given rev, if any
-    - `rev`: affected rev of `store`
-    """
-    repo, sd_computers, sd_removers = sidedata_helpers
-    kind = store.revlog_kind
-    flags_to_add = 0
-    flags_to_remove = 0
-    for _keys, sd_computer, _flags in sd_computers.get(kind, []):
-        sidedata, flags = sd_computer(repo, store, rev, sidedata)
-        flags_to_add |= flags[0]
-        flags_to_remove |= flags[1]
-    for keys, _computer, flags in sd_removers.get(kind, []):
-        for key in keys:
-            sidedata.pop(key, None)
-        flags_to_remove |= flags
-    return sidedata, (flags_to_add, flags_to_remove)
-
-
 def deltaiscensored(delta, baserev, baselenfn):
     """Determine if a delta represents censored revision data.
 
diff --git a/mercurial/upgrade_utils/engine.py b/mercurial/upgrade_utils/engine.py
--- a/mercurial/upgrade_utils/engine.py
+++ b/mercurial/upgrade_utils/engine.py
@@ -12,7 +12,6 @@ 
 from ..i18n import _
 from ..pycompat import getattr
 from .. import (
-    changegroup,
     changelog,
     error,
     filelog,
@@ -43,7 +42,7 @@ 
             flagutil.REVIDX_HASCOPIESINFO,
             replace=True,
         )
-    return changegroup.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata)
+    return sidedatamod.get_sidedata_helpers(srcrepo, dstrepo._wanted_sidedata)
 
 
 def _revlogfrompath(repo, path):
diff --git a/mercurial/revlogutils/sidedata.py b/mercurial/revlogutils/sidedata.py
--- a/mercurial/revlogutils/sidedata.py
+++ b/mercurial/revlogutils/sidedata.py
@@ -32,9 +32,11 @@ 
 
 from __future__ import absolute_import
 
+import collections
 import struct
 
-from .. import error
+from .. import error, requirements as requirementsmod
+from ..revlogutils import flagutil
 from ..utils import hashutil
 
 ## sidedata type constant
@@ -91,3 +93,63 @@ 
         sidedata[key] = entrytext
         dataoffset = nextdataoffset
     return sidedata
+
+
+def get_sidedata_helpers(repo, remote_sd_categories, pull=False):
+    # Computers for computing sidedata on-the-fly
+    sd_computers = collections.defaultdict(list)
+    # Computers for categories to remove from sidedata
+    sd_removers = collections.defaultdict(list)
+    to_generate = remote_sd_categories - repo._wanted_sidedata
+    to_remove = repo._wanted_sidedata - remote_sd_categories
+    if pull:
+        to_generate, to_remove = to_remove, to_generate
+
+    for revlog_kind, computers in repo._sidedata_computers.items():
+        for category, computer in computers.items():
+            if category in to_generate:
+                sd_computers[revlog_kind].append(computer)
+            if category in to_remove:
+                sd_removers[revlog_kind].append(computer)
+
+    sidedata_helpers = (repo, sd_computers, sd_removers)
+    return sidedata_helpers
+
+
+def run_sidedata_helpers(store, sidedata_helpers, sidedata, rev):
+    """Returns the sidedata for the given revision after running through
+    the given helpers.
+    - `store`: the revlog this applies to (changelog, manifest, or filelog
+      instance)
+    - `sidedata_helpers`: see `storageutil.emitrevisions`
+    - `sidedata`: previous sidedata at the given rev, if any
+    - `rev`: affected rev of `store`
+    """
+    repo, sd_computers, sd_removers = sidedata_helpers
+    kind = store.revlog_kind
+    flags_to_add = 0
+    flags_to_remove = 0
+    for _keys, sd_computer, _flags in sd_computers.get(kind, []):
+        sidedata, flags = sd_computer(repo, store, rev, sidedata)
+        flags_to_add |= flags[0]
+        flags_to_remove |= flags[1]
+    for keys, _computer, flags in sd_removers.get(kind, []):
+        for key in keys:
+            sidedata.pop(key, None)
+        flags_to_remove |= flags
+    return sidedata, (flags_to_add, flags_to_remove)
+
+
+def set_sidedata_spec_for_repo(repo):
+    # prevent cycle metadata -> revlogutils.sidedata -> metadata
+    from .. import metadata
+
+    if requirementsmod.COPIESSDC_REQUIREMENT in repo.requirements:
+        repo.register_wanted_sidedata(SD_FILES)
+    repo.register_sidedata_computer(
+        b"changelog",
+        SD_FILES,
+        (SD_FILES,),
+        metadata.copies_sidedata_computer,
+        flagutil.REVIDX_HASCOPIESINFO,
+    )
diff --git a/mercurial/revlog.py b/mercurial/revlog.py
--- a/mercurial/revlog.py
+++ b/mercurial/revlog.py
@@ -2897,7 +2897,7 @@ 
                 text, sidedata = self._revisiondata(rev)
 
                 if sidedata_helpers is not None:
-                    (sidedata, new_flags) = storageutil.run_sidedata_helpers(
+                    (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
                         self, sidedata_helpers, sidedata, rev
                     )
                     flags = flags | new_flags[0] & ~new_flags[1]
@@ -2927,7 +2927,7 @@ 
                     sidedata = self.sidedata(rev)
 
                 if sidedata_helpers is not None:
-                    (sidedata, new_flags) = storageutil.run_sidedata_helpers(
+                    (sidedata, new_flags) = sidedatautil.run_sidedata_helpers(
                         self, sidedata_helpers, sidedata, rev
                     )
                     flags = flags | new_flags[0] & ~new_flags[1]
@@ -3199,7 +3199,7 @@ 
             current_offset = fp.tell()
             for rev in range(startrev, endrev + 1):
                 entry = self.index[rev]
-                new_sidedata, flags = storageutil.run_sidedata_helpers(
+                new_sidedata, flags = sidedatautil.run_sidedata_helpers(
                     store=self,
                     sidedata_helpers=helpers,
                     sidedata={},
diff --git a/mercurial/metadata.py b/mercurial/metadata.py
--- a/mercurial/metadata.py
+++ b/mercurial/metadata.py
@@ -17,7 +17,6 @@ 
 )
 from . import (
     error,
-    requirements as requirementsmod,
     util,
 )
 
@@ -826,18 +825,6 @@ 
     return sidedata, (flags_to_add, 0)
 
 
-def set_sidedata_spec_for_repo(repo):
-    if requirementsmod.COPIESSDC_REQUIREMENT in repo.requirements:
-        repo.register_wanted_sidedata(sidedatamod.SD_FILES)
-    repo.register_sidedata_computer(
-        b"changelog",
-        sidedatamod.SD_FILES,
-        (sidedatamod.SD_FILES,),
-        copies_sidedata_computer,
-        sidedataflag.REVIDX_HASCOPIESINFO,
-    )
-
-
 def _sidedata_worker(srcrepo, revs_queue, sidedata_queue, tokens):
     """The function used by worker precomputing sidedata
 
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -50,7 +50,6 @@ 
     match as matchmod,
     mergestate as mergestatemod,
     mergeutil,
-    metadata as metadatamod,
     namespaces,
     narrowspec,
     obsolete,
@@ -89,6 +88,7 @@ 
 from .revlogutils import (
     concurrency_checker as revlogchecker,
     constants as revlogconst,
+    sidedata as sidedatamod,
 )
 
 release = lockmod.release
@@ -1406,7 +1406,7 @@ 
 
         self._wanted_sidedata = set()
         self._sidedata_computers = {}
-        metadatamod.set_sidedata_spec_for_repo(self)
+        sidedatamod.set_sidedata_spec_for_repo(self)
 
     def _getvfsward(self, origfunc):
         """build a ward for self.vfs"""
diff --git a/mercurial/changegroup.py b/mercurial/changegroup.py
--- a/mercurial/changegroup.py
+++ b/mercurial/changegroup.py
@@ -7,7 +7,6 @@ 
 
 from __future__ import absolute_import
 
-import collections
 import os
 import struct
 import weakref
@@ -301,7 +300,7 @@ 
             and srctype == b'pull'
         )
         if adding_sidedata:
-            sidedata_helpers = get_sidedata_helpers(
+            sidedata_helpers = sidedatamod.get_sidedata_helpers(
                 repo,
                 sidedata_categories or set(),
                 pull=True,
@@ -1071,7 +1070,9 @@ 
                 # TODO a better approach would be for the strip bundle to
                 # correctly advertise its sidedata categories directly.
                 remote_sidedata = repo._wanted_sidedata
-            sidedata_helpers = get_sidedata_helpers(repo, remote_sidedata)
+            sidedata_helpers = sidedatamod.get_sidedata_helpers(
+                repo, remote_sidedata
+            )
 
         clstate, deltas = self._generatechangelog(
             cl,
@@ -1938,24 +1939,3 @@ 
                 )
 
     return revisions, files
-
-
-def get_sidedata_helpers(repo, remote_sd_categories, pull=False):
-    # Computers for computing sidedata on-the-fly
-    sd_computers = collections.defaultdict(list)
-    # Computers for categories to remove from sidedata
-    sd_removers = collections.defaultdict(list)
-    to_generate = remote_sd_categories - repo._wanted_sidedata
-    to_remove = repo._wanted_sidedata - remote_sd_categories
-    if pull:
-        to_generate, to_remove = to_remove, to_generate
-
-    for revlog_kind, computers in repo._sidedata_computers.items():
-        for category, computer in computers.items():
-            if category in to_generate:
-                sd_computers[revlog_kind].append(computer)
-            if category in to_remove:
-                sd_removers[revlog_kind].append(computer)
-
-    sidedata_helpers = (repo, sd_computers, sd_removers)
-    return sidedata_helpers