changeset 408:f137231f9d30

extract the revmap support into a separate dict-like class
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Wed, 10 Jun 2009 15:45:20 +0200
parents cbb5644895e8
children d4615986e1db
files hgsubversion/hg_delta_editor.py hgsubversion/maps.py hgsubversion/stupid.py hgsubversion/util.py
diffstat 4 files changed, 49 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/hgsubversion/hg_delta_editor.py
+++ b/hgsubversion/hg_delta_editor.py
@@ -66,12 +66,6 @@ def ieditor(fn):
 
 
 class HgChangeReceiver(delta.Editor):
-    def add_to_revmap(self, revnum, branch, node_hash):
-        f = open(self.revmap_file, 'a')
-        f.write(str(revnum) + ' ' + node.hex(node_hash) + ' ' + (branch or '') + '\n')
-        f.flush()
-        f.close()
-        self.revmap[revnum, branch] = node_hash
 
     def last_known_revision(self):
         """Obtain the highest numbered -- i.e. latest -- revision known.
@@ -99,15 +93,7 @@ class HgChangeReceiver(delta.Editor):
             os.makedirs(self.meta_data_dir)
         self._set_uuid(uuid)
         # TODO: validate subdir too
-
-        if os.path.isfile(self.revmap_file):
-            self.revmap = util.parse_revmap(self.revmap_file)
-        else:
-            self.revmap = {}
-            f = open(self.revmap_file, 'w')
-            f.write('%s\n' % util.REVMAP_FILE_VERSION)
-            f.flush()
-            f.close()
+        self.revmap = maps.RevMap(repo)
 
         author_host = self.ui.config('hgsubversion', 'defaulthost', uuid)
         authors = self.ui.config('hgsubversion', 'authormap')
@@ -572,7 +558,7 @@ class HgChangeReceiver(delta.Editor):
                                  extra)
             new = self.repo.commitctx(ctx)
             if (rev.revnum, b) not in self.revmap:
-                self.add_to_revmap(rev.revnum, b, new)
+                self.revmap[rev.revnum, b] = new
             if b in endbranches:
                 endbranches.pop(b)
                 bname = b or 'default'
@@ -672,7 +658,7 @@ class HgChangeReceiver(delta.Editor):
             new_hash = self.repo.commitctx(current_ctx)
             util.describe_commit(self.ui, new_hash, branch)
             if (rev.revnum, branch) not in self.revmap:
-                self.add_to_revmap(rev.revnum, branch, new_hash)
+                self.revmap[rev.revnum, branch] = new_hash
 
         # 2. handle branches that need to be committed without any files
         for branch in self.commit_branches_empty:
@@ -700,7 +686,7 @@ class HgChangeReceiver(delta.Editor):
             new_hash = self.repo.commitctx(current_ctx)
             util.describe_commit(self.ui, new_hash, branch)
             if (rev.revnum, branch) not in self.revmap:
-                self.add_to_revmap(rev.revnum, branch, new_hash)
+                self.revmap[rev.revnum, branch] = new_hash
 
         # 3. handle tags
         if tbdelta['tags'][0] or tbdelta['tags'][1]:
@@ -790,10 +776,6 @@ class HgChangeReceiver(delta.Editor):
     uuid = property(_get_uuid, _set_uuid, None,
                     'Error-checked UUID of source Subversion repository.')
 
-    @property
-    def revmap_file(self):
-        return os.path.join(self.meta_data_dir, 'rev_map')
-
     @property
     def meta_data_dir(self):
         return os.path.join(self.path, '.hg', 'svn')
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -2,6 +2,7 @@
 
 import os
 from mercurial import util as hgutil
+from mercurial import node
 
 class AuthorMap(dict):
     '''A mapping from Subversion-style authors to Mercurial-style
@@ -91,3 +92,46 @@ class AuthorMap(dict):
         else:
             # Mercurial incorrectly splits at e.g. '.', so we roll our own.
             return author.rsplit('@', 1)[0]
+
+
+class RevMap(dict):
+
+    VERSION = 1
+
+    def __init__(self, repo):
+        dict.__init__(self)
+        self.path = os.path.join(repo.path, 'svn', 'rev_map')
+        if os.path.isfile(self.path):
+            self._load()
+        else:
+            self._write()
+
+    def _load(self):
+        f = open(self.path)
+        ver = int(f.readline())
+        if ver != self.VERSION:
+            print 'revmap too new -- please upgrade'
+            raise NotImplementedError
+        for l in f:
+            revnum, hash, branch = l.split(' ', 2)
+            if branch == '\n':
+                branch = None
+            else:
+                branch = branch[:-1]
+            dict.__setitem__(self, (int(revnum), branch), node.bin(hash))
+        f.close()
+
+    def _write(self):
+        f = open(self.path, 'w')
+        f.write('%s\n' % self.VERSION)
+        f.flush()
+        f.close()
+
+    def __setitem__(self, key, hash):
+        revnum, branch = key
+        f = open(self.path, 'a')
+        b = branch or ''
+        f.write(str(revnum) + ' ' + node.hex(hash) + ' ' + b + '\n')
+        f.flush()
+        f.close()
+        dict.__setitem__(self, (revnum, branch), hash)
--- a/hgsubversion/stupid.py
+++ b/hgsubversion/stupid.py
@@ -616,7 +616,7 @@ def convert_rev(ui, hg_editor, svn, r, t
         branch = extra.get('branch', None)
         if not branch in hg_editor.branches:
             hg_editor.branches[branch] = None, 0, r.revnum
-        hg_editor.add_to_revmap(r.revnum, b, ha)
+        hg_editor.revmap[r.revnum, b] = ha
         util.describe_commit(ui, ha, b)
 
     # These are branches with an 'R' status in svn log. This means they were
--- a/hgsubversion/util.py
+++ b/hgsubversion/util.py
@@ -32,27 +32,6 @@ def normalize_url(svnurl):
     return url
 
 
-REVMAP_FILE_VERSION = 1
-def parse_revmap(revmap_filename):
-    revmap = {}
-    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: #pragma: no cover
-        print ('Your revmap was made by a newer version of hgsubversion.'
-               ' Please upgrade.')
-        raise NotImplementedError
-    return revmap
-
-
 class PrefixMatch(object):
     def __init__(self, prefix):
         self.p = prefix