[PATCH 1 of 5] localrepo: factor _findtags() out of tags() (affects mq and bookmarks)

Greg Ward greg at gerg.ca
Fri Jul 3 20:26:15 CDT 2009


# HG changeset patch
# User Greg Ward <greg at gerg.ca>
# Date 1246670740 14400
# Node ID a68a06ecc7f5b7e2d14ca5bd4d484efd4bf7aed2
# Parent  4ff1782e38fdf2063218e612ab825f1d81444465
localrepo: factor _findtags() out of tags() (affects mq and bookmarks).

This makes in-memory caching the sole responsibility of localrepo,
eliminating some localrepo code that was duplicated in mq and
bookmarks.

diff --git a/hgext/bookmarks.py b/hgext/bookmarks.py
--- a/hgext/bookmarks.py
+++ b/hgext/bookmarks.py
@@ -296,14 +296,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
 
diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -2427,34 +2427,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
diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -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 their
+        # 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):
         '''


More information about the Mercurial-devel mailing list