[PATCH 1 of 2 STABLE] filectx: move _adjustlinkrev as a method

Pierre-Yves David pierre-yves.david at ens-lyon.org
Fri Jan 30 17:20:33 UTC 2015


# HG changeset patch
# User Pierre-Yves David <pierre-yves.david at fb.com>
# Date 1422628743 0
#      Fri Jan 30 14:39:03 2015 +0000
# Branch stable
# Node ID b9d18d103d7aa91ac999630ec582300a5cb9a596
# Parent  6becb9dbca25057c6186e255a48dd2c2ce5701a5
filectx: move _adjustlinkrev as a method

We are going to introduce some wider caching mechanism during linkrev
adjustment, As there is no specific reason to not be a method and some reason to
be a method, lets make it a method.

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -20,45 +20,10 @@ propertycache = util.propertycache
 # Phony node value to stand-in for new files in some uses of
 # manifests. Manifests support 21-byte hashes for nodes which are
 # dirty in the working copy.
 _newnode = '!' * 21
 
-def _adjustlinkrev(repo, path, filelog, fnode, srcrev, inclusive=False):
-    """return the first ancestor of <srcrev> introducting <fnode>
-
-    If the linkrev of the file revision does not point to an ancestor of
-    srcrev, we'll walk down the ancestors until we find one introducing this
-    file revision.
-
-    :repo: a localrepository object (used to access changelog and manifest)
-    :path: the file path
-    :fnode: the nodeid of the file revision
-    :filelog: the filelog of this path
-    :srcrev: the changeset revision we search ancestors from
-    :inclusive: if true, the src revision will also be checked
-    """
-    cl = repo.unfiltered().changelog
-    ma = repo.manifest
-    # fetch the linkrev
-    fr = filelog.rev(fnode)
-    lkr = filelog.linkrev(fr)
-    # check if this linkrev is an ancestor of srcrev
-    anc = cl.ancestors([srcrev], lkr, inclusive=inclusive)
-    if lkr not in anc:
-        for a in anc:
-            ac = cl.read(a) # get changeset data (we avoid object creation).
-            if path in ac[3]: # checking the 'files' field.
-                # The file has been touched, check if the content is similar
-                # to the one we search for.
-                if fnode == ma.readfast(ac[0]).get(path):
-                    return a
-        # In theory, we should never get out of that loop without a result. But
-        # if manifest uses a buggy file revision (not children of the one it
-        # replaces) we could. Such a buggy situation will likely result is crash
-        # somewhere else at to some point.
-    return lkr
-
 class basectx(object):
     """A basectx object represents the common logic for its children:
     changectx: read-only context that is already present in the repo,
     workingctx: a context that represents the working directory and can
                 be committed,
@@ -779,10 +744,46 @@ class basefilectx(object):
             or self.size() == fctx.size()):
             return self._filelog.cmp(self._filenode, fctx.data())
 
         return True
 
+    def _adjustlinkrev(self, path, filelog, fnode, srcrev, inclusive=False):
+        """return the first ancestor of <srcrev> introducting <fnode>
+
+        If the linkrev of the file revision does not point to an ancestor of
+        srcrev, we'll walk down the ancestors until we find one introducing
+        this file revision.
+
+        :repo: a localrepository object (used to access changelog and manifest)
+        :path: the file path
+        :fnode: the nodeid of the file revision
+        :filelog: the filelog of this path
+        :srcrev: the changeset revision we search ancestors from
+        :inclusive: if true, the src revision will also be checked
+        """
+        repo = self._repo
+        cl = repo.unfiltered().changelog
+        ma = repo.manifest
+        # fetch the linkrev
+        fr = filelog.rev(fnode)
+        lkr = filelog.linkrev(fr)
+        # check if this linkrev is an ancestor of srcrev
+        anc = cl.ancestors([srcrev], lkr, inclusive=inclusive)
+        if lkr not in anc:
+            for a in anc:
+                ac = cl.read(a) # get changeset data (we avoid object creation)
+                if path in ac[3]: # checking the 'files' field.
+                    # The file has been touched, check if the content is
+                    # similar to the one we search for.
+                    if fnode == ma.readfast(ac[0]).get(path):
+                        return a
+            # In theory, we should never get out of that loop without a result.
+            # But if manifest uses a buggy file revision (not children of the
+            # one it replaces) we could. Such a buggy situation will likely
+            # result is crash somewhere else at to some point.
+        return lkr
+
     def introrev(self):
         """return the rev of the changeset which introduced this file revision
 
         This method is different from linkrev because it take into account the
         changeset the filectx was created from. It ensures the returned
@@ -793,12 +794,12 @@ class basefilectx(object):
         lkr = self.linkrev()
         attrs = vars(self)
         noctx = not ('_changeid' in attrs or '_changectx' in attrs)
         if noctx or self.rev() == lkr:
             return self.linkrev()
-        return _adjustlinkrev(self._repo, self._path, self._filelog,
-                              self._filenode, self.rev(), inclusive=True)
+        return self._adjustlinkrev(self._path, self._filelog, self._filenode,
+                                   self.rev(), inclusive=True)
 
     def parents(self):
         _path = self._path
         fl = self._filelog
         parents = self._filelog.parents(self._filenode)
@@ -820,11 +821,11 @@ class basefilectx(object):
         for path, fnode, l in pl:
             if '_changeid' in vars(self) or '_changectx' in vars(self):
                 # If self is associated with a changeset (probably explicitly
                 # fed), ensure the created filectx is associated with a
                 # changeset that is an ancestor of self.changectx.
-                rev = _adjustlinkrev(self._repo, path, l, fnode, self.rev())
+                rev = self._adjustlinkrev(path, l, fnode, self.rev())
                 fctx = filectx(self._repo, path, fileid=fnode, filelog=l,
                                changeid=rev)
             else:
                 fctx = filectx(self._repo, path, fileid=fnode, filelog=l)
             ret.append(fctx)


More information about the Mercurial-devel mailing list