# HG changeset patch # User Pierre-Yves David # Date 1677014720 -3600 # Node ID 65943224c184c008b8457686165e438cfa1a52d0 # Parent 3433723d1b9ba029d6421ef3d19234bb44b9b62d dirstate: introduce a check_invalidated decorator This is a common need, so let us consolidate it to simplify the code. diff -r 3433723d1b9b -r 65943224c184 mercurial/dirstate.py --- 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"