upgrade: move `printrequirements()` to UpgradeOperation class
authorPulkit Goyal <7895pulkit@gmail.com>
Sat, 12 Dec 2020 18:09:16 +0530
changeset 46191 aba979b1b90b
parent 46190 9ab2ab5bf9af
child 46192 25d11b24dedf
upgrade: move `printrequirements()` to UpgradeOperation class Part of refactor where we make things more arranged and integrated into single `UpgradeOperation` class. Differential Revision: https://phab.mercurial-scm.org/D9577
mercurial/upgrade.py
mercurial/upgrade_utils/actions.py
mercurial/upgrade_utils/engine.py
--- a/mercurial/upgrade.py	Sat Dec 12 18:00:18 2020 +0530
+++ b/mercurial/upgrade.py	Sat Dec 12 18:09:16 2020 +0530
@@ -123,36 +123,10 @@
             ui.warn(msg % b', '.join(sorted(incompatible)))
             revlogs = upgrade_engine.UPGRADE_ALL_REVLOGS
 
-    def write_labeled(l, label):
-        first = True
-        for r in sorted(l):
-            if not first:
-                ui.write(b', ')
-            ui.write(r, label=label)
-            first = False
-
-    def printrequirements():
-        ui.write(_(b'requirements\n'))
-        ui.write(_(b'   preserved: '))
-        write_labeled(
-            newreqs & repo.requirements, "upgrade-repo.requirement.preserved"
-        )
-        ui.write((b'\n'))
-        removed = repo.requirements - newreqs
-        if repo.requirements - newreqs:
-            ui.write(_(b'   removed: '))
-            write_labeled(removed, "upgrade-repo.requirement.removed")
-            ui.write((b'\n'))
-        added = newreqs - repo.requirements
-        if added:
-            ui.write(_(b'   added: '))
-            write_labeled(added, "upgrade-repo.requirement.added")
-            ui.write((b'\n'))
-        ui.write(b'\n')
-
     upgrade_op = upgrade_actions.UpgradeOperation(
         ui,
         newreqs,
+        repo.requirements,
         actions,
         revlogs,
     )
@@ -205,7 +179,7 @@
             )
         )
 
-        printrequirements()
+        upgrade_op.print_requirements()
         upgrade_op.print_optimisations()
         upgrade_op.print_upgrade_actions()
         upgrade_op.print_affected_revlogs()
@@ -225,7 +199,7 @@
 
     # Else we're in the run=true case.
     ui.write(_(b'upgrade will perform the following actions:\n\n'))
-    printrequirements()
+    upgrade_op.print_requirements()
     upgrade_op.print_optimisations()
     upgrade_op.print_upgrade_actions()
     upgrade_op.print_affected_revlogs()
--- a/mercurial/upgrade_utils/actions.py	Sat Dec 12 18:00:18 2020 +0530
+++ b/mercurial/upgrade_utils/actions.py	Sat Dec 12 18:09:16 2020 +0530
@@ -557,12 +557,32 @@
 class UpgradeOperation(object):
     """represent the work to be done during an upgrade"""
 
-    def __init__(self, ui, requirements, actions, revlogs_to_process):
+    def __init__(
+        self,
+        ui,
+        new_requirements,
+        current_requirements,
+        actions,
+        revlogs_to_process,
+    ):
         self.ui = ui
-        self.requirements = requirements
+        self.new_requirements = new_requirements
+        self.current_requirements = current_requirements
         self.actions = actions
         self._actions_names = set([a.name for a in actions])
         self.revlogs_to_process = revlogs_to_process
+        # requirements which will be added by the operation
+        self._added_requirements = (
+            self.new_requirements - self.current_requirements
+        )
+        # requirements which will be removed by the operation
+        self._removed_requirements = (
+            self.current_requirements - self.new_requirements
+        )
+        # requirements which will be preserved by the operation
+        self._preserved_requirements = (
+            self.current_requirements & self.new_requirements
+        )
 
     def _write_labeled(self, l, label):
         """
@@ -575,6 +595,27 @@
             self.ui.write(r, label=label)
             first = False
 
+    def print_requirements(self):
+        self.ui.write(_(b'requirements\n'))
+        self.ui.write(_(b'   preserved: '))
+        self._write_labeled(
+            self._preserved_requirements, "upgrade-repo.requirement.preserved"
+        )
+        self.ui.write((b'\n'))
+        if self._removed_requirements:
+            self.ui.write(_(b'   removed: '))
+            self._write_labeled(
+                self._removed_requirements, "upgrade-repo.requirement.removed"
+            )
+            self.ui.write((b'\n'))
+        if self._added_requirements:
+            self.ui.write(_(b'   added: '))
+            self._write_labeled(
+                self._added_requirements, "upgrade-repo.requirement.added"
+            )
+            self.ui.write((b'\n'))
+        self.ui.write(b'\n')
+
     def print_optimisations(self):
         optimisations = [a for a in self.actions if a.type == OPTIMISATION]
         optimisations.sort(key=lambda a: a.name)
--- a/mercurial/upgrade_utils/engine.py	Sat Dec 12 18:00:18 2020 +0530
+++ b/mercurial/upgrade_utils/engine.py	Sat Dec 12 18:09:16 2020 +0530
@@ -429,7 +429,7 @@
     # The sorted() makes execution deterministic.
     for p, kind, st in sorted(srcrepo.store.vfs.readdir(b'', stat=True)):
         if not _filterstorefile(
-            srcrepo, dstrepo, upgrade_op.requirements, p, kind, st
+            srcrepo, dstrepo, upgrade_op.new_requirements, p, kind, st
         ):
             continue
 
@@ -489,7 +489,7 @@
             b'again\n'
         )
     )
-    scmutil.writereporequirements(srcrepo, upgrade_op.requirements)
+    scmutil.writereporequirements(srcrepo, upgrade_op.new_requirements)
 
     # The lock file from the old store won't be removed because nothing has a
     # reference to its new location. So clean it up manually. Alternatively, we