diff mercurial/repair.py @ 5909:f45f7390c1c5

strip: calculate list of extra nodes to save and pass it to changegroupsubset When we remove revision N from the repository, all revisions >= N are affected: either it's a descendant from N and will also be removed, or it's not a descendant of N and will be renumbered. As a consequence, we have to (at least temporarily) remove all filelog and manifest revisions that have a linkrev >= N, readding some of them later. Unfortunately, it's possible to have a revlog with two revisions r1 and r2 such that r1 < r2, but linkrev(r1) > linkrev(r2). If we try to strip revision linkrev(r1) from the repository, we'll also lose revision r2 when we truncate this revlog. We already use changegroupsubset to create a temporary changegroup containing the revisions that have to be restored, but that function is unable to detect that we also wanted to save the r2 in the case above. So we manually calculate these extra nodes and pass it to changegroupsubset. This should fix issue764.
author Alexis S. L. Carvalho <alexis@cecm.usp.br>
date Sat, 19 Jan 2008 18:01:16 -0200
parents 3afbd82a6c82
children b9a830fa10f6
line wrap: on
line diff
--- a/mercurial/repair.py	Sat Jan 19 18:01:16 2008 -0200
+++ b/mercurial/repair.py	Sat Jan 19 18:01:16 2008 -0200
@@ -21,9 +21,9 @@
             seen[p] = 1
     return heads
 
-def _bundle(repo, bases, heads, node, suffix):
+def _bundle(repo, bases, heads, node, suffix, extranodes=None):
     """create a bundle with the specified revisions as a backup"""
-    cg = repo.changegroupsubset(bases, heads, 'strip')
+    cg = repo.changegroupsubset(bases, heads, 'strip', extranodes)
     backupdir = repo.join("strip-backup")
     if not os.path.isdir(backupdir):
         os.mkdir(backupdir)
@@ -44,6 +44,42 @@
 
     return filenodes
 
+def _collectextranodes(repo, files, link):
+    """return the nodes that have to be saved before the strip"""
+    def collectone(revlog):
+        extra = []
+        startrev = count = revlog.count()
+        # find the truncation point of the revlog
+        for i in xrange(0, count):
+            node = revlog.node(i)
+            lrev = revlog.linkrev(node)
+            if lrev >= link:
+                startrev = i + 1
+                break
+
+        # see if any revision after that point has a linkrev less than link
+        # (we have to manually save these guys)
+        for i in xrange(startrev, count):
+            node = revlog.node(i)
+            lrev = revlog.linkrev(node)
+            if lrev < link:
+                extra.append((node, cl.node(lrev)))
+
+        return extra
+
+    extranodes = {}
+    cl = repo.changelog
+    extra = collectone(repo.manifest)
+    if extra:
+        extranodes[1] = extra
+    for fname in files:
+        f = repo.file(fname)
+        extra = collectone(f)
+        if extra:
+            extranodes[fname] = extra
+
+    return extranodes
+
 def _stripall(repo, striprev, filenodes):
     """strip the requested nodes from the filelogs"""
     # we go in two steps here so the strip loop happens in a
@@ -102,23 +138,27 @@
                 if cl.rev(x) > striprev:
                     savebases[x] = 1
 
+    filenodes = _collectfilenodes(repo, striprev)
+
+    extranodes = _collectextranodes(repo, filenodes, striprev)
+
     # create a changegroup for all the branches we need to keep
     if backup == "all":
         _bundle(repo, [node], cl.heads(), node, 'backup')
-    if saveheads:
-        chgrpfile = _bundle(repo, savebases.keys(), saveheads, node, 'temp')
+    if saveheads or extranodes:
+        chgrpfile = _bundle(repo, savebases.keys(), saveheads, node, 'temp',
+                            extranodes)
 
-    filenodes = _collectfilenodes(repo, striprev)
     _stripall(repo, striprev, filenodes)
 
     change = cl.read(node)
     cl.strip(striprev, striprev)
     repo.manifest.strip(repo.manifest.rev(change[0]), striprev)
-    if saveheads:
+    if saveheads or extranodes:
         ui.status("adding branch\n")
         f = open(chgrpfile, "rb")
         gen = changegroup.readbundle(f, chgrpfile)
-        repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile)
+        repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
         f.close()
         if backup != "strip":
             os.unlink(chgrpfile)