make revlog.addgroup pass its file handles to addrevision
authorAlexis S. L. Carvalho <alexis@cecm.usp.br>
Sat, 14 Oct 2006 18:47:48 -0300
changeset 3390 a74addddd092
parent 3389 efbe24c7d8d9
child 3391 defadc26e674
make revlog.addgroup pass its file handles to addrevision This should fix issue255. It looks like the problem there happens when addgroup calls addrevision to add a full revision, and addrevision decides to split the index file into a .i/.d pair. Since addgroup has an open file handle for the index file, the renaming of the new .i file to its final name fails on windows.
mercurial/revlog.py
--- a/mercurial/revlog.py	Sat Oct 14 10:53:29 2006 +0200
+++ b/mercurial/revlog.py	Sat Oct 14 18:47:48 2006 -0300
@@ -955,6 +955,14 @@
         p1, p2 - the parent nodeids of the revision
         d - an optional precomputed delta
         """
+        if not self.inlinedata():
+            dfh = self.opener(self.datafile, "a")
+        else:
+            dfh = None
+        ifh = self.opener(self.indexfile, "a+")
+        return self._addrevision(text, transaction, link, p1, p2, d, ifh, dfh)
+
+    def _addrevision(self, text, transaction, link, p1, p2, d, ifh, dfh):
         if text is None: text = ""
         if p1 is None: p1 = self.tip()
         if p2 is None: p2 = nullid
@@ -1004,28 +1012,25 @@
         if not self.inlinedata():
             transaction.add(self.datafile, offset)
             transaction.add(self.indexfile, n * len(entry))
-            f = self.opener(self.datafile, "a")
             if data[0]:
-                f.write(data[0])
-            f.write(data[1])
-            f.close()
-            f = self.opener(self.indexfile, "a")
+                dfh.write(data[0])
+            dfh.write(data[1])
+            dfh.flush()
         else:
-            f = self.opener(self.indexfile, "a+")
-            f.seek(0, 2)
-            transaction.add(self.indexfile, f.tell(), self.count() - 1)
+            ifh.seek(0, 2)
+            transaction.add(self.indexfile, ifh.tell(), self.count() - 1)
 
         if len(self.index) == 1 and self.version != REVLOGV0:
             l = struct.pack(versionformat, self.version)
-            f.write(l)
+            ifh.write(l)
             entry = entry[4:]
 
-        f.write(entry)
+        ifh.write(entry)
 
         if self.inlinedata():
-            f.write(data[0])
-            f.write(data[1])
-            self.checkinlinesize(transaction, f)
+            ifh.write(data[0])
+            ifh.write(data[1])
+            self.checkinlinesize(transaction, ifh)
 
         self.cache = (node, n, text)
         return node
@@ -1146,7 +1151,13 @@
                 ifh.flush()
                 text = self.revision(chain)
                 text = self.patches(text, [delta])
-                chk = self.addrevision(text, transaction, link, p1, p2)
+                chk = self._addrevision(text, transaction, link, p1, p2, None,
+                                        ifh, dfh)
+                if not dfh and not self.inlinedata():
+                    # addrevision switched from inline to conventional
+                    # reopen the index
+                    dfh = self.opener(self.datafile, "a")
+                    ifh = self.opener(self.indexfile, "a")
                 if chk != node:
                     raise RevlogError(_("consistency error adding group"))
                 textlen = len(text)
@@ -1166,11 +1177,6 @@
                         dfh = self.opener(self.datafile, "a")
                         ifh = self.opener(self.indexfile, "a")
                 else:
-                    if not dfh:
-                        # addrevision switched from inline to conventional
-                        # reopen the index
-                        dfh = self.opener(self.datafile, "a")
-                        ifh = self.opener(self.indexfile, "a")
                     dfh.write(cdelta)
                     ifh.write(struct.pack(self.indexformat, *e))