diff mercurial/hg.py @ 343:d7df759d0e97

rework all code using tags -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 rework all code using tags Add three utility functions: tags(): get (and possibly load) the tags mapping tagslist(): sort tag,node by revision (aka topologically) nodetags(): return a list of tags associated with a node (also cached) Update all the code using tags to use these. Simplify identify code make unknown always visible if printed don't ignore tip pseudo-tag manifest hash: e6deb4d545ad465be7735f9ec43227bcb5e238c7 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCr+HjywK+sNU5EO8RAh4/AJ90cI0WxmvQAj6Lq2ZiG8LmqZan/QCfR8B5 ltu8tOIEHDa8LhfS9wtBu0k= =pv3t -----END PGP SIGNATURE-----
author mpm@selenic.com
date Wed, 15 Jun 2005 00:08:03 -0800
parents c3d873ef4b31
children db419f14df4b
line wrap: on
line diff
--- a/mercurial/hg.py	Wed Jun 15 00:03:25 2005 -0800
+++ b/mercurial/hg.py	Wed Jun 15 00:08:03 2005 -0800
@@ -334,7 +334,8 @@
         self.manifest = manifest(self.opener)
         self.changelog = changelog(self.opener)
         self.ignorelist = None
-        self.tags = None
+        self.tagscache = None
+        self.nodetagscache = None
 
         if not self.remote:
             self.dirstate = dirstate(self.opener, ui, self.root)
@@ -355,9 +356,10 @@
             if pat.search(f): return True
         return False
 
-    def lookup(self, key):
-        if self.tags is None:
-            self.tags = {}
+    def tags(self):
+        '''return a mapping of tag to node'''
+        if not self.tagscache: 
+            self.tagscache = {}
             try:
                 # read each head of the tags file, ending with the tip
                 # and add each tag found to the map, with "newer" ones
@@ -369,11 +371,35 @@
                     for l in fl.revision(r).splitlines():
                         if l:
                             n, k = l.split(" ")
-                            self.tags[k] = bin(n)
+                            self.tagscache[k] = bin(n)
             except KeyError: pass
-            self.tags['tip'] = self.changelog.tip()
+            self.tagscache['tip'] = self.changelog.tip()
+
+        return self.tagscache
+
+    def tagslist(self):
+        '''return a list of tags ordered by revision'''
+        l = []
+        for t,n in self.tags().items():
+            try:
+                r = self.changelog.rev(n)
+            except:
+                r = -2 # sort to the beginning of the list if unknown
+            l.append((r,t,n))
+        l.sort()
+        return [(t,n) for r,t,n in l]
+
+    def nodetags(self, node):
+        '''return the tags associated with a node'''
+        if not self.nodetagscache:
+            self.nodetagscache = {}
+            for t,n in self.tags().items():
+                self.nodetagscache.setdefault(n,[]).append(t)
+        return self.nodetagscache.get(node, [])
+
+    def lookup(self, key):
         try:
-            return self.tags[key]
+            return self.tags()[key]
         except KeyError:
             return self.changelog.lookup(key)