changeset 50118:65943224c184

dirstate: introduce a check_invalidated decorator This is a common need, so let us consolidate it to simplify the code.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 21 Feb 2023 22:25:20 +0100
parents 3433723d1b9b
children de42ba9dd852
files mercurial/dirstate.py
diffstat 1 files changed, 22 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/dirstate.py	Sun Feb 19 03:21:12 2023 +0100
+++ b/mercurial/dirstate.py	Tue Feb 21 22:25:20 2023 +0100
@@ -65,18 +65,33 @@
         return obj._join(fname)
 
 
+def check_invalidated(func):
+    """check we func is called a non-invalidated dirstate
+
+    The dirstate is in an "invalidated state" after an error occured during its
+    modification and remains so until we exited the top level scope that framed
+    such change.
+    """
+
+    def wrap(self, *args, **kwargs):
+        if self._invalidated_context:
+            msg = 'calling `%s` after the dirstate was invalidated'
+            msg %= func.__name__
+            raise error.ProgrammingError(msg)
+        return func(self, *args, **kwargs)
+
+    return wrap
+
+
 def requires_changing_parents(func):
     def wrap(self, *args, **kwargs):
         if not self.is_changing_parents:
             msg = 'calling `%s` outside of a changing_parents context'
             msg %= func.__name__
             raise error.ProgrammingError(msg)
-        if self._invalidated_context:
-            msg = 'calling `%s` after the dirstate was invalidated'
-            raise error.ProgrammingError(msg)
         return func(self, *args, **kwargs)
 
-    return wrap
+    return check_invalidated(wrap)
 
 
 def requires_changing_files(func):
@@ -87,7 +102,7 @@
             raise error.ProgrammingError(msg)
         return func(self, *args, **kwargs)
 
-    return wrap
+    return check_invalidated(wrap)
 
 
 def requires_changing_any(func):
@@ -96,12 +111,9 @@
             msg = 'calling `%s` outside of a changing context'
             msg %= func.__name__
             raise error.ProgrammingError(msg)
-        if self._invalidated_context:
-            msg = 'calling `%s` after the dirstate was invalidated'
-            raise error.ProgrammingError(msg)
         return func(self, *args, **kwargs)
 
-    return wrap
+    return check_invalidated(wrap)
 
 
 def requires_not_changing_parents(func):
@@ -112,7 +124,7 @@
             raise error.ProgrammingError(msg)
         return func(self, *args, **kwargs)
 
-    return wrap
+    return check_invalidated(wrap)
 
 
 CHANGE_TYPE_PARENTS = "parents"