diff hgsubversion/maps.py @ 430:2851b81c65ce

maps: make sure AuthorMaps don't overwrite themselves, fix overriding Author maps for the Python repo got truncated because of the author map stupidly writing upon itself. This patch implements a better and faster scenario, where entries will only be written to the saved author map if they're not coming from that file. They're also now streamed into the file directly, instead of having to re-open the file on every entry, and formatting is preserved.
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Mon, 15 Jun 2009 16:09:27 +0200
parents b17b2969861c
children fbc7cf4fd701
line wrap: on
line diff
--- a/hgsubversion/maps.py
+++ b/hgsubversion/maps.py
@@ -34,10 +34,18 @@ class AuthorMap(dict):
         ''' Load mappings from a file at the specified path. '''
         if not os.path.exists(path):
             return
+
+        writing = False
+        if path != self.path:
+            writing = open(self.path, 'a')
+
         self.ui.note('reading authormap from %s\n' % path)
         f = open(path, 'r')
         for number, line in enumerate(f):
 
+            if writing:
+                writing.write(line)
+
             line = line.split('#')[0]
             if not line.strip():
                 continue
@@ -51,23 +59,16 @@ class AuthorMap(dict):
 
             src = src.strip()
             dst = dst.strip()
+            self.ui.debug('adding author %s to author map\n' % src)
             if src in self and dst != self[src]:
                 msg = 'overriding author: "%s" to "%s" (%s)\n'
                 self.ui.warn(msg % (self[src], dst, src))
-            else:
-                self[src] = dst
-
-        f.close()
+            self[src] = dst
 
-    def __setitem__(self, key, value):
-        ''' Similar to dict.__setitem__, but also updates the new mapping in the
-        backing store. '''
-        self.super.__setitem__(key, value)
-        self.ui.debug('adding author %s to author map\n' % self.path)
-        f = open(self.path, 'w+')
-        for k, v in self.iteritems():
-            f.write("%s=%s\n" % (k, v))
         f.close()
+        if writing:
+            writing.flush()
+            writing.close()
 
     def __getitem__(self, author):
         ''' Similar to dict.__getitem__, except in case of an unknown author.