localrepo: factor _findtags() out of tags() (issue548).
authorGreg Ward <greg-hg@gerg.ca>
Thu, 16 Jul 2009 10:39:41 -0400
changeset 9145 6b03f93b8ff3
parent 9144 ad72e3b08bc0
child 9146 5614a628d173
localrepo: factor _findtags() out of tags() (issue548). This makes in-memory caching the sole responsibility of localrepo, eliminating some localrepo code that was duplicated in mq and bookmarks.
hgext/bookmarks.py
hgext/mq.py
mercurial/localrepo.py
--- a/hgext/bookmarks.py	Thu Jul 16 10:39:41 2009 -0400
+++ b/hgext/bookmarks.py	Thu Jul 16 10:39:41 2009 -0400
@@ -293,14 +293,11 @@
                 write(self, marks)
             return result
 
-        def tags(self):
+        def _findtags(self):
             """Merge bookmarks with normal tags"""
-            if self.tagscache:
-                return self.tagscache
-
-            tagscache = super(bookmark_repo, self).tags()
-            tagscache.update(parse(self))
-            return tagscache
+            (tags, tagtypes) = super(bookmark_repo, self)._findtags()
+            tags.update(parse(self))
+            return (tags, tagtypes)
 
     repo.__class__ = bookmark_repo
 
--- a/hgext/mq.py	Thu Jul 16 10:39:41 2009 -0400
+++ b/hgext/mq.py	Thu Jul 16 10:39:41 2009 -0400
@@ -2415,34 +2415,33 @@
                 raise util.Abort(_('source has mq patches applied'))
             return super(mqrepo, self).push(remote, force, revs)
 
-        def tags(self):
-            if self.tagscache:
-                return self.tagscache
-
-            tagscache = super(mqrepo, self).tags()
+        def _findtags(self):
+            '''augment tags from base class with patch tags'''
+            result = super(mqrepo, self)._findtags()
 
             q = self.mq
             if not q.applied:
-                return tagscache
+                return result
 
             mqtags = [(bin(patch.rev), patch.name) for patch in q.applied]
 
             if mqtags[-1][0] not in self.changelog.nodemap:
                 self.ui.warn(_('mq status file refers to unknown node %s\n')
                              % short(mqtags[-1][0]))
-                return tagscache
+                return result
 
             mqtags.append((mqtags[-1][0], 'qtip'))
             mqtags.append((mqtags[0][0], 'qbase'))
             mqtags.append((self.changelog.parents(mqtags[0][0])[0], 'qparent'))
+            tags = result[0]
             for patch in mqtags:
-                if patch[1] in tagscache:
+                if patch[1] in tags:
                     self.ui.warn(_('Tag %s overrides mq patch of the same name\n')
                                  % patch[1])
                 else:
-                    tagscache[patch[1]] = patch[0]
+                    tags[patch[1]] = patch[0]
 
-            return tagscache
+            return result
 
         def _branchtags(self, partial, lrev):
             q = self.mq
--- a/mercurial/localrepo.py	Thu Jul 16 10:39:41 2009 -0400
+++ b/mercurial/localrepo.py	Thu Jul 16 10:39:41 2009 -0400
@@ -233,8 +233,24 @@
 
     def tags(self):
         '''return a mapping of tag to node'''
-        if self.tagscache:
-            return self.tagscache
+        if self.tagscache is None:
+            (self.tagscache, self._tagstypecache) = self._findtags()
+
+        return self.tagscache
+
+    def _findtags(self):
+        '''Do the hard work of finding tags.  Return a pair of dicts
+        (tags, tagtypes) where tags maps tag name to node, and tagtypes
+        maps tag name to a string like \'global\' or \'local\'.
+        Subclasses or extensions are free to add their own tags, but
+        should be aware that the returned dicts will be retained for the
+        duration of the localrepo object.'''
+
+        # XXX what tagtype should subclasses/extensions use?  Currently
+        # mq and bookmarks add tags, but do not set the tagtype at all.
+        # Should each extension invent its own tag type?  Should there
+        # be one tagtype for all such "virtual" tags?  Or is the status
+        # quo fine?
 
         globaltags = {}
         tagtypes = {}
@@ -318,15 +334,13 @@
         except IOError:
             pass
 
-        self.tagscache = {}
-        self._tagstypecache = {}
+        tags = {}
         for k, nh in globaltags.iteritems():
             n = nh[0]
             if n != nullid:
-                self.tagscache[k] = n
-            self._tagstypecache[k] = tagtypes[k]
-        self.tagscache['tip'] = self.changelog.tip()
-        return self.tagscache
+                tags[k] = n
+        tags['tip'] = self.changelog.tip()
+        return (tags, tagtypes)
 
     def tagtype(self, tagname):
         '''