delta-find: add a simple safeguard to prevent bad non-general-delta stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Tue, 18 Apr 2023 17:17:31 +0200
branchstable
changeset 50356 2a6949ab9d23
parent 50355 0232571255d3
child 50357 8038493eb31a
delta-find: add a simple safeguard to prevent bad non-general-delta This make it easier to catch request to build delta that does not make sense in the general delta world. It seems better to raise a programming error than to corrupt the repository.
mercurial/revlogutils/deltas.py
--- a/mercurial/revlogutils/deltas.py	Wed Apr 19 18:16:19 2023 +0200
+++ b/mercurial/revlogutils/deltas.py	Tue Apr 18 17:17:31 2023 +0200
@@ -1135,7 +1135,7 @@
 
         return delta
 
-    def _builddeltainfo(self, revinfo, base, fh):
+    def _builddeltainfo(self, revinfo, base, fh, target_rev=None):
         # can we use the cached delta?
         revlog = self.revlog
         debug_search = self._write_debug is not None and self._debug_search
@@ -1143,6 +1143,13 @@
         if revlog._generaldelta:
             deltabase = base
         else:
+            if target_rev is not None and base != target_rev - 1:
+                msg = (
+                    b'general delta cannot use delta for something else '
+                    b'than `prev`: %d<-%d'
+                )
+                msg %= (base, target_rev)
+                raise error.ProgrammingError(msg)
             deltabase = chainbase
         snapshotdepth = None
         if revlog._sparserevlog and deltabase == nullrev:
@@ -1373,7 +1380,12 @@
 
                 if debug_search:
                     delta_start = util.timer()
-                candidatedelta = self._builddeltainfo(revinfo, candidaterev, fh)
+                candidatedelta = self._builddeltainfo(
+                    revinfo,
+                    candidaterev,
+                    fh,
+                    target_rev=target_rev,
+                )
                 if debug_search:
                     delta_end = util.timer()
                     msg = b"DBG-DELTAS-SEARCH:     delta-search-time=%f\n"