Patchwork [5,of,6,ctx-minor-fixes] context: use object-oriented programming

login
register
mail settings
Submitter Sean Farley
Date Sept. 19, 2018, 5:35 a.m.
Message ID <56222df6e5c8738b9b04.1537335347@1.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.ip6.arpa>
Download mbox | patch
Permalink /patch/34820/
State Accepted
Headers show

Comments

Sean Farley - Sept. 19, 2018, 5:35 a.m.
# HG changeset patch
# User Sean Farley <sean@farley.io>
# Date 1528495331 25200
#      Fri Jun 08 15:02:11 2018 -0700
# Branch ctx-minor-fixes
# Node ID 56222df6e5c8738b9b04881477c166540c50b17a
# Parent  0173c0dca9d389d55a3f0e8ff3bcf43dc7ef659d
context: use object-oriented programming

We have abstracted methods, let's use them. Future patches will iron out
the different types and enforce ctx._parents to be revs.

Patch

diff --git a/mercurial/context.py b/mercurial/context.py
index 3bcfe6c..1871edb 100644
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -234,14 +234,14 @@  class basectx(object):
     def parents(self):
         """return contexts for each parent changeset"""
         return self._parents
 
     def p1(self):
-        return self._parents[0]
+        return self.parents()[0]
 
     def p2(self):
-        parents = self._parents
+        parents = self.parents()
         if len(parents) == 2:
             return parents[1]
         return changectx(self._repo, nullrev)
 
     def _fileinfo(self, path):
@@ -564,11 +564,11 @@  class changectx(basectx):
         merge.preferancestor configuration before falling back to the
         revlog ancestor."""
         # deal with workingctxs
         n2 = c2._node
         if n2 is None:
-            n2 = c2._parents[0]._node
+            n2 = c2.p1().node()
         cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
         if not cahs:
             anc = nullid
         elif len(cahs) == 1:
             anc = cahs[0]
@@ -1098,11 +1098,11 @@  class committablectx(basectx):
             self._extra['branch'] = branch
         if self._extra['branch'] == '':
             self._extra['branch'] = 'default'
 
     def __bytes__(self):
-        return bytes(self._parents[0]) + "+"
+        return bytes(self.p1()) + "+"
 
     __str__ = encoding.strmethod(__bytes__)
 
     def __nonzero__(self):
         return True
@@ -1227,11 +1227,11 @@  class committablectx(basectx):
         except OSError:
             return ''
 
     def ancestor(self, c2):
         """return the "best" ancestor context of self and c2"""
-        return self._parents[0].ancestor(c2) # punt on two parents for now
+        return self.p1().ancestor(c2) # punt on two parents for now
 
     def walk(self, match):
         '''Generates matching file names.'''
         return sorted(self._repo.dirstate.walk(match,
                                                subrepos=sorted(self.substate),
@@ -1240,14 +1240,14 @@  class committablectx(basectx):
     def matches(self, match):
         ds = self._repo.dirstate
         return sorted(f for f in ds.matches(match) if ds[f] != 'r')
 
     def ancestors(self):
-        for p in self._parents:
+        for p in self.parents():
             yield p
         for a in self._repo.changelog.ancestors(
-            [p.rev() for p in self._parents]):
+                [p.rev() for p in self.parents()]):
             yield changectx(self._repo, a)
 
     def markcommitted(self, node):
         """Perform post-commit cleanup necessary after committing this ctx
 
@@ -1450,11 +1450,11 @@  class workingctx(committablectx):
             return [], [], []
 
         modified = []
         deleted = []
         fixup = []
-        pctx = self._parents[0]
+        pctx = self.p1()
         # do a full compare of any files that might have changed
         for f in sorted(files):
             try:
                 # This will return True for a file that got replaced by a
                 # directory in the interim, but fixing that is pretty hard.
@@ -1651,11 +1651,11 @@  class committablefilectx(basefilectx):
         def filenode(ctx, path):
             return ctx._manifest.get(path, nullid)
 
         path = self._path
         fl = self._filelog
-        pcl = self._changectx._parents
+        pcl = self._changectx.parents()
         renamed = self.renamed()
 
         if renamed:
             pl = [renamed + (None,)]
         else:
@@ -1684,11 +1684,11 @@  class workingfilectx(committablefilectx)
         return self._repo.wread(self._path)
     def renamed(self):
         rp = self._repo.dirstate.copied(self._path)
         if not rp:
             return None
-        return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
+        return rp, self._changectx.p1()._manifest.get(rp, nullid)
 
     def size(self):
         return self._repo.wvfs.lstat(self._path).st_size
     def date(self):
         t, tz = self._changectx.date()
@@ -2093,11 +2093,11 @@  class overlayworkingfilectx(committablef
 
     def renamed(self):
         path = self._parent.copydata(self._path)
         if not path:
             return None
-        return path, self._changectx._parents[0]._manifest.get(path, nullid)
+        return path, self._changectx.p1()._manifest.get(path, nullid)
 
     def size(self):
         return self._parent.size(self._path)
 
     def markcopied(self, origin):
@@ -2280,11 +2280,11 @@  class memctx(committablectx):
     @propertycache
     def _manifest(self):
         """generate a manifest based on the return values of filectxfn"""
 
         # keep this simple for now; just worry about p1
-        pctx = self._parents[0]
+        pctx = self.p1()
         man = pctx.manifest().copy()
 
         for f in self._status.modified:
             man[f] = modifiednodeid
 
@@ -2300,11 +2300,11 @@  class memctx(committablectx):
     @propertycache
     def _status(self):
         """Calculate exact status from ``files`` specified at construction
         """
         man1 = self.p1().manifest()
-        p2 = self._parents[1]
+        p2 = self.p2()
         # "1 < len(self._parents)" can't be used for checking
         # existence of the 2nd parent, because "memctx._parents" is
         # explicitly initialized by the list, of which length is 2.
         if p2.node() != nullid:
             man2 = p2.manifest()
@@ -2434,11 +2434,11 @@  class metadataonlyctx(committablectx):
     def _status(self):
         """Calculate exact status from ``files`` specified in the ``origctx``
         and parents manifests.
         """
         man1 = self.p1().manifest()
-        p2 = self._parents[1]
+        p2 = self.p2()
         # "1 < len(self._parents)" can't be used for checking
         # existence of the 2nd parent, because "metadataonlyctx._parents" is
         # explicitly initialized by the list, of which length is 2.
         if p2.node() != nullid:
             man2 = p2.manifest()