[PATCH 01 of 36] context: add an empty class that will be used as a parent of all contexts

Sean Farley sean.michael.farley at gmail.com
Wed Aug 7 13:13:01 CDT 2013


# HG changeset patch
# User Sean Farley <sean.michael.farley at gmail.com>
# Date 1373763561 18000
#      Sat Jul 13 19:59:21 2013 -0500
# Node ID 006e3b2905974dff47890fb2a9f1e1e88bf83228
# Parent  a58251c0568fc5e86089a803ca75f75cc8c01678
context: add an empty class that will be used as a parent of all contexts

At the moment, there is no simple way to check if an object is a context
because there is no common parent class. If there were, we could use
'isinstance' everywhere. Simply having memctx inherit from workingctx or
changectx would allow the use of 'isinstance' but that could lead to some
confusing situations of reading the code since we have three distinct concepts
of a context:

- changectx represents a changeset *already* in the repo, and is therefore immutable
- workingctx represents changes on disk in the working directory
- memctx represents changes solely in memory which may or may not be on disk

Therefore, I propose refactoring context.py to have all three contexts inherit
from a parent class 'context'.

diff --git a/mercurial/context.py b/mercurial/context.py
--- a/mercurial/context.py
+++ b/mercurial/context.py
@@ -14,13 +14,24 @@
 import obsolete as obsmod
 import repoview
 
 propertycache = util.propertycache
 
-class changectx(object):
+class context(object):
+    """A context 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,
+    memctx: a context that represents changes in-memory and can also
+            be committed."""
+    def __new__(cls, repo, changeid='', *args, **kwargs):
+        return super(context, cls).__new__(cls)
+
+class changectx(context):
     """A changecontext object makes access to data related to a particular
-    changeset convenient."""
+    changeset convenient. It represents a read-only context already presnt in
+    the repo."""
     def __init__(self, repo, changeid=''):
         """changeid is a revision number, node, or tag"""
         if changeid == '':
             changeid = '.'
         self._repo = repo


More information about the Mercurial-devel mailing list