changeset 34:50d55c3e0d85

Some refactors of the previous change, including transparent upgrade of old-style pickled dictionaries.
author Augie Fackler <durin42@gmail.com>
date Fri, 17 Oct 2008 11:03:52 -0500
parents a9c15cae50e5
children ef5d7a7aabb0
files hg_delta_editor.py svncommand.py util.py
diffstat 3 files changed, 46 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/hg_delta_editor.py
+++ b/hg_delta_editor.py
@@ -14,6 +14,8 @@ from mercurial import node
 from svn import delta
 from svn import core
 
+import util as our_util
+
 def pickle_atomic(data, file_path, dir=None):
     """pickle some data to a path atomically.
 
@@ -73,15 +75,7 @@ class HgChangeReceiver(delta.Editor):
             self.subdir = self.subdir[1:]
         self.revmap = {}
         if os.path.exists(self.revmap_file):
-            f = open(self.revmap_file)
-            for l in f:
-                revnum, node_hash, branch = l.split(' ', 2)
-                if branch == '\n':
-                    branch = None
-                else:
-                    branch = branch[:-1]
-                self.revmap[int(revnum), branch] = node.bin(node_hash)
-            f.close()
+            self.revmap = our_util.parse_revmap(self.revmap_file)
         self.branches = {}
         if os.path.exists(self.branch_info_file):
             f = open(self.branch_info_file)
@@ -119,7 +113,10 @@ class HgChangeReceiver(delta.Editor):
         else:
             self.repo = hg.repository(self.ui, repo_path, create=True)
             os.makedirs(os.path.dirname(self.uuid_file))
-            open(self.revmap_file, 'w') # make empty file
+            f = open(self.revmap_file, 'w')
+            f.write('%s\n' % our_util.REVMAP_FILE_VERSION)
+            f.flush()
+            f.close()
 
     def clear_current_info(self):
         '''Clear the info relevant to a replayed revision so that the next
--- a/svncommand.py
+++ b/svncommand.py
@@ -64,19 +64,6 @@ def generate_hg_tags(ui, hg_repo_path, *
         source_ha = hg_editor.get_parent_revision(source[1]+1, source[0])
         f.write('%s tag/%s\n' % (node.hex(source_ha), tag))
 
-def parse_revmap(revmap_filename):
-    revmap = {}
-    f = open(revmap_filename)
-    for l in f:
-        revnum, node_hash, branch = l.split(' ', 2)
-        if branch == '\n':
-            branch = None
-        else:
-            branch = branch[:-1]
-        revmap[int(revnum), branch] = node.bin(node_hash)
-    f.close()
-    return revmap
-
 @register_subcommand('up')
 def update(ui, args, repo, clean=False, **opts):
     """Update to a specified Subversion revision number.
@@ -85,7 +72,7 @@ def update(ui, args, repo, clean=False, 
     rev = int(args[0])
     path = os.path.join(repo.path, 'svn', 'rev_map')
     answers = []
-    for k,v in parse_revmap(path).iteritems():
+    for k,v in util.parse_revmap(path).iteritems():
         if k[0] == rev:
             answers.append((v, k[1]))
     if len(answers) == 1:
@@ -152,7 +139,7 @@ def verify_all_revisions(ui, args, repo,
     args = list(args)
     if args:
         start_rev = int(args.pop(0))
-    revmap = parse_revmap(os.path.join(repo.path, 'svn', 'rev_map'))
+    revmap = util.parse_revmap(os.path.join(repo.path, 'svn', 'rev_map'))
     revs = sorted(revmap.keys())
     for revnum, br in revs:
         if revnum < start_rev:
--- a/util.py
+++ b/util.py
@@ -1,6 +1,9 @@
 import os
+import pickle
 import shutil
 
+from mercurial import node
+
 svn_subcommands = { }
 
 def register_subcommand(name):
@@ -18,3 +21,37 @@ def wipe_all_files(hg_wc_path):
             shutil.rmtree(f)
         else:
             os.remove(f)
+
+REVMAP_FILE_VERSION = 1
+def parse_revmap(revmap_filename):
+    revmap = {}
+    f = open(revmap_filename)
+    try:
+        # Remove compat code after March of 2009. That should be more than long
+        # enough.
+        revmap = pickle.load(f)
+        f.close()
+        f = open(revmap_filename, 'w')
+        f.write('1\n')
+        for key, value in sorted(revmap.items()):
+            f.write('%s %s %s\n' % (str(key[0]), node.hex(value), key[1] or ''))
+        f.close()
+    except:
+        f.close()
+        f = open(revmap_filename)
+        ver = int(f.readline())
+        if ver == 1:
+            for l in f:
+                revnum, node_hash, branch = l.split(' ', 2)
+                if branch == '\n':
+                    branch = None
+                else:
+                    branch = branch[:-1]
+                revmap[int(revnum), branch] = node.bin(node_hash)
+            f.close()
+        else:
+            print ('Your revmap was made by a newer version of hgsubversion.'
+                   ' Please upgrade.')
+            raise NotImplementedError
+    return revmap
+