mercurial/localrepo.py
changeset 4932 f0c25ed40ec6
parent 4929 22fcd6444ad9
child 4934 8535c1770dd3
child 5081 ea7b982b6c08
--- a/mercurial/localrepo.py	Mon Jul 16 18:01:20 2007 -0700
+++ b/mercurial/localrepo.py	Mon Jul 16 20:15:03 2007 -0700
@@ -118,22 +118,43 @@
 
         self.hook('pretag', throw=True, node=hex(node), tag=name, local=local)
 
+        def writetag(fp, name, munge, prevtags):
+            if prevtags and prevtags[-1] != '\n':
+                fp.write('\n')
+            fp.write('%s %s\n' % (hex(node), munge and munge(name) or name))
+            fp.close()
+            self.hook('tag', node=hex(node), tag=name, local=local)
+            
+        prevtags = ''
         if local:
+            try:
+                fp = self.opener('localtags', 'r+')
+            except IOError, err:
+                fp = self.opener('localtags', 'a')
+            else:
+                prevtags = fp.read()
+
             # local tags are stored in the current charset
-            self.opener('localtags', 'a').write('%s %s\n' % (hex(node), name))
-            self.hook('tag', node=hex(node), tag=name, local=local)
+            writetag(fp, name, None, prevtags)
             return
 
+        if use_dirstate:
+            try:
+                fp = self.wfile('.hgtags', 'rb+')
+            except IOError, err:
+                fp = self.wfile('.hgtags', 'ab')
+            else:
+                prevtags = fp.read()
+        else:
+            try:
+                prevtags = self.filectx('.hgtags', parent).data()
+            except revlog.LookupError:
+                pass
+            fp = self.wfile('.hgtags', 'wb')
+
         # committed tags are stored in UTF-8
-        line = '%s %s\n' % (hex(node), util.fromlocal(name))
-        if use_dirstate:
-            self.wfile('.hgtags', 'ab').write(line)
-        else:
-            try:
-                ntags = self.filectx('.hgtags', parent).data()
-            except revlog.LookupError:
-                ntags = ''
-            self.wfile('.hgtags', 'wb').write(ntags + line)
+        writetag(fp, name, util.fromlocal, prevtags)
+
         if use_dirstate and self.dirstate.state('.hgtags') == '?':
             self.add(['.hgtags'])