diff mercurial/localrepo.py @ 4892:d69b1fb111b9

tag: handle .hgtags and .hg/localtags with missing final newline (issue 601) This also fixes an asymmetry bug in which we called the pretag hook if we were going to create a local tag, but didn't call the tag hook afterwards.
author Bryan O'Sullivan <bos@serpentine.com>
date Mon, 16 Jul 2007 20:15:03 -0700
parents 192cd95c2ba8
children 6fd953d5faea 8535c1770dd3
line wrap: on
line diff
--- a/mercurial/localrepo.py	Mon Jul 16 18:01:20 2007 -0700
+++ b/mercurial/localrepo.py	Mon Jul 16 20:15:03 2007 -0700
@@ -119,22 +119,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'])