[PATCH 6 of 7 V2] tags: extract tags computation from fnodes into its own function

Pierre-Yves David pierre-yves.david at ens-lyon.org
Tue Mar 28 08:03:49 EDT 2017


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at ens-lyon.org>
# Date 1490674092 -7200
#      Tue Mar 28 06:08:12 2017 +0200
# Node ID c2abcd1197667b2e603169352ad9f620bdbe7333
# Parent  60c027be19b2c5ba812dd3aa4f8627c9c5a705da
# EXP-Topic tags
# Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
#              hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r c2abcd119766
tags: extract tags computation from fnodes into its own function

I'm about to introduce code that needs to perform such computation on
"arbitrary" nodes. The logic is extracted into its own function for reuse.

diff --git a/mercurial/tags.py b/mercurial/tags.py
--- a/mercurial/tags.py
+++ b/mercurial/tags.py
@@ -85,19 +85,18 @@ def findglobaltags(ui, repo):
 
     The tags cache is read and updated as a side-effect of calling.
     '''
-    alltags = {}
-
     (heads, tagfnode, valid, cachetags, shouldwrite) = _readtagcache(ui, repo)
     if cachetags is not None:
         assert not shouldwrite
         # XXX is this really 100% correct?  are there oddball special
         # cases where a global tag should outrank a local tag but won't,
         # because cachetags does not contain rank info?
+        alltags = {}
         _updatetags(cachetags, alltags)
         return alltags
 
     seen = set()  # set of fnode
-    fctx = None
+    fnodes = []
     for head in reversed(heads):  # oldest to newest
         assert head in repo.changelog.nodemap, \
                "tag cache returned bogus head %s" % short(head)
@@ -105,19 +104,32 @@ def findglobaltags(ui, repo):
         fnode = tagfnode.get(head)
         if fnode and fnode not in seen:
             seen.add(fnode)
-            if not fctx:
-                fctx = repo.filectx('.hgtags', fileid=fnode)
-            else:
-                fctx = fctx.filectx(fnode)
+            fnodes.append(fnode)
 
-            filetags = _readtags(ui, repo, fctx.data().splitlines(), fctx)
-            _updatetags(filetags, alltags)
+    alltags = _tagsfromfnodes(ui, repo, fnodes)
 
     # and update the cache (if necessary)
     if shouldwrite:
         _writetagcache(ui, repo, valid, alltags)
     return alltags
 
+def _tagsfromfnodes(ui, repo, fnodes):
+    """return a tagsmap from a list of file-node
+
+    tagsmap: tag name to (node, hist) 2-tuples.
+
+    The order of the list matters."""
+    alltags = {}
+    fctx = None
+    for fnode in fnodes:
+        if fctx is None:
+            fctx = repo.filectx('.hgtags', fileid=fnode)
+        else:
+            fctx = fctx.filectx(fnode)
+        filetags = _readtags(ui, repo, fctx.data().splitlines(), fctx)
+        _updatetags(filetags, alltags)
+    return alltags
+
 def readlocaltags(ui, repo, alltags, tagtypes):
     '''Read local tags in repo. Update alltags and tagtypes.'''
     try:


More information about the Mercurial-devel mailing list