diff mercurial/repair.py @ 28868:445a25bb70be

obsstore: move delete function from obsstore class to repair module Since one of the original patches was accepted already and people on the mailing list still have suggestions as to how this should be improved, I'm implementing those suggestions in the following patches (this and the ones that might follow).
author Kostia Balytskyi <ikostia@fb.com>
date Tue, 12 Apr 2016 04:06:50 -0700
parents ae53ecc47414
children bf7b8157c483
line wrap: on
line diff
--- a/mercurial/repair.py	Tue Apr 12 03:40:53 2016 -0700
+++ b/mercurial/repair.py	Tue Apr 12 04:06:50 2016 -0700
@@ -17,6 +17,7 @@
     changegroup,
     error,
     exchange,
+    obsolete,
     util,
 )
 
@@ -313,3 +314,32 @@
                      "ancestors(head() and not bookmark(%s)) - "
                      "ancestors(bookmark() and not bookmark(%s))",
                      mark, mark, mark)
+
+def deleteobsmarkers(obsstore, indices):
+    """Delete some obsmarkers from obsstore and return how many were deleted
+
+    'indices' is a list of ints which are the indices
+    of the markers to be deleted.
+
+    Every invocation of this function completely rewrites the obsstore file,
+    skipping the markers we want to be removed. The new temporary file is
+    created, remaining markers are written there and on .close() this file
+    gets atomically renamed to obsstore, thus guaranteeing consistency."""
+    if not indices:
+        # we don't want to rewrite the obsstore with the same content
+        return
+
+    left = []
+    current = obsstore._all
+    n = 0
+    for i, m in enumerate(current):
+        if i in indices:
+            n += 1
+            continue
+        left.append(m)
+
+    newobsstorefile = obsstore.svfs('obsstore', 'w', atomictemp=True)
+    for bytes in obsolete.encodemarkers(left, True, obsstore._version):
+        newobsstorefile.write(bytes)
+    newobsstorefile.close()
+    return n