Patchwork [2,of,3,STABLE] transactions: add version number to journal.backupfiles

login
register
mail settings
Submitter Durham Goode
Date Oct. 21, 2014, 7:58 p.m.
Message ID <b4a651b82658895889a0.1413921515@dev2000.prn2.facebook.com>
Download mbox | patch
Permalink /patch/6435/
State Accepted
Headers show

Comments

Durham Goode - Oct. 21, 2014, 7:58 p.m.
# HG changeset patch
# User Durham Goode <durham@fb.com>
# Date 1413916649 25200
#      Tue Oct 21 11:37:29 2014 -0700
# Branch stable
# Node ID b4a651b82658895889a0f53f93fd75f59590c18d
# Parent  e3cf0a29709409a7de70205d416325e0240d283c
transactions: add version number to journal.backupfiles

The transaction format will be changing a bit over the next releases, so let's
go ahead and add a version number to make backwards compatibility easier. This
whole file format was broken prior to 3.2 (see previous patch), so changing it
now is pretty low risk.

Patch

diff --git a/mercurial/transaction.py b/mercurial/transaction.py
--- a/mercurial/transaction.py
+++ b/mercurial/transaction.py
@@ -15,6 +15,8 @@  from i18n import _
 import errno
 import error, util
 
+version = 1
+
 def active(func):
     def _active(self, *args, **kwds):
         if self.count == 0:
@@ -92,6 +94,7 @@  class transaction(object):
         self.backupjournal = "%s.backupfiles" % journal
         self.file = opener.open(self.journal, "w")
         self.backupsfile = opener.open(self.backupjournal, 'w')
+        self.backupsfile.write('%d\n' % version)
         if createmode is not None:
             opener.chmod(self.journal, createmode & 0666)
             opener.chmod(self.backupjournal, createmode & 0666)
@@ -348,10 +351,20 @@  def rollback(opener, file, report):
         fp = opener.open(backupjournal)
         data = fp.read()
         if len(data) > 0:
-            parts = data.split('\0')
-            # Skip the final part, since it's just a trailing empty space
-            for i in xrange(0, len(parts) - 1, 2):
-                f, b = parts[i:i + 2]
-                backupentries.append((f, b, None))
+            ver = version
+            versionend = data.find('\n')
+            if versionend != -1:
+                ver = data[:versionend]
+                data = data[versionend + 1:]
+
+            if ver == str(version):
+                parts = data.split('\0')
+                # Skip the final part, since it's just a trailing empty space
+                for i in xrange(0, len(parts) - 1, 2):
+                    f, b = parts[i:i + 2]
+                    backupentries.append((f, b, None))
+            else:
+                report(_("journal was created by a newer version of "
+                         "Mercurial"))
 
     _playback(file, report, opener, entries, backupentries)