evolvestate: rename the file to state.py and class name to cmdstate
authorPulkit Goyal <7895pulkit@gmail.com>
Wed, 17 Jan 2018 19:12:32 +0530
changeset 3457 82e9f9603b1b
parent 3456 b03d955e391e
child 3458 210132852a3d
evolvestate: rename the file to state.py and class name to cmdstate The evolvestate.py and the evolvestate class in it were introduced to help `hg evolve` grow continue and abort options. But the class and it's API can be easily used to store state information for other state files like we stored the information for grab state in the previous commit. Let's rename the the file and class.
hgext3rd/evolve/__init__.py
hgext3rd/evolve/cmdrewrite.py
hgext3rd/evolve/evolvestate.py
hgext3rd/evolve/state.py
--- a/hgext3rd/evolve/__init__.py	Fri Jan 26 17:55:16 2018 +0100
+++ b/hgext3rd/evolve/__init__.py	Wed Jan 17 19:12:32 2018 +0530
@@ -310,7 +310,7 @@
     compat,
     debugcmd,
     cmdrewrite,
-    evolvestate,
+    state,
     exthelper,
     metadata,
     obscache,
@@ -840,8 +840,8 @@
         raise
 
 def summaryhook(ui, repo):
-    state = evolvestate.evolvestate(repo)
-    if state:
+    evolvestate = state.cmdstate(repo)
+    if evolvestate:
         # i18n: column positioning for "hg summary"
         ui.status(_('evolve: (evolve --continue)\n'))
 
@@ -1571,11 +1571,11 @@
 
     # Continuation handling
     if contopt:
-        state = evolvestate.evolvestate(repo)
-        if not state:
+        evolvestate = state.cmdstate(repo)
+        if not evolvestate:
             raise error.Abort('no evolve to continue')
-        state.load()
-        orig = repo[state['current']]
+        evolvestate.load()
+        orig = repo[evolvestate['current']]
         with repo.wlock(), repo.lock():
             ctx = orig
             source = ctx.extra().get('source')
@@ -1598,7 +1598,7 @@
                                    date=date, extra=extra)
 
             obsolete.createmarkers(repo, [(ctx, (repo[node],))])
-            state.delete()
+            evolvestate.delete()
             return
 
     cmdutil.bailifchanged(repo)
@@ -1784,8 +1784,8 @@
             relocate(repo, orig, target, pctx, keepbranch)
         except MergeFailure:
             ops = {'current': orig.node()}
-            state = evolvestate.evolvestate(repo, opts=ops)
-            state.save()
+            evolvestate = state.cmdstate(repo, opts=ops)
+            evolvestate.save()
             repo.ui.write_err(_('evolve failed!\n'))
             repo.ui.write_err(
                 _("fix conflict and run 'hg evolve --continue'"
--- a/hgext3rd/evolve/cmdrewrite.py	Fri Jan 26 17:55:16 2018 +0100
+++ b/hgext3rd/evolve/cmdrewrite.py	Wed Jan 17 19:12:32 2018 +0530
@@ -35,7 +35,7 @@
 
 from . import (
     compat,
-    evolvestate,
+    state,
     exthelper,
     rewriteutil,
     utility,
@@ -1173,7 +1173,7 @@
         revs.append(opts['rev'])
 
     with repo.wlock(), repo.lock(), repo.transaction('grab'):
-        state = evolvestate.evolvestate(repo, path='grabstate')
+        grabstate = state.cmdstate(repo, path='grabstate')
 
         if not cont and not abort:
             cmdutil.bailifchanged(repo)
@@ -1197,17 +1197,17 @@
             stats = merge.graft(repo, origctx, origctx.p1(), ['local',
                                                               'destination'])
             if stats[3]:
-                state.addopts({'orignode': origctx.node(),
-                               'oldpctx': pctx.node()})
-                state.save()
+                grabstate.addopts({'orignode': origctx.node(),
+                                   'oldpctx': pctx.node()})
+                grabstate.save()
                 raise error.InterventionRequired(_("unresolved merge conflicts"
                                                    " (see hg help resolve)"))
 
         elif abort:
-            if not state:
+            if not grabstate:
                 raise error.Abort(_("no interrupted grab state exists"))
-            state.load()
-            pctxnode = state['oldpctx']
+            grabstate.load()
+            pctxnode = grabstate['oldpctx']
             ui.status(_("aborting grab, updating to %s\n") %
                       node.hex(pctxnode)[:12])
             hg.updaterepo(repo, pctxnode, True)
@@ -1217,18 +1217,18 @@
             if revs:
                 raise error.Abort(_("cannot specify both --continue and "
                                     "revision"))
-            if not state:
+            if not grabstate:
                 raise error.Abort(_("no interrupted grab state exists"))
 
-            state.load()
-            orignode = state['orignode']
+            grabstate.load()
+            orignode = grabstate['orignode']
             origctx = repo[orignode]
 
         newnode = repo.commit(text=origctx.description(), user=origctx.user(),
                               date=origctx.date(), extra=origctx.extra())
 
-        if state:
-            state.delete()
+        if grabstate:
+            grabstate.delete()
         if newnode:
             obsolete.createmarkers(repo, [(origctx, (repo[newnode],))])
         else:
--- a/hgext3rd/evolve/evolvestate.py	Fri Jan 26 17:55:16 2018 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-# This software may be used and distributed according to the terms of the
-# GNU General Public License version 2 or any later version.
-
-"""
-This file contains class to wrap the state for hg evolve command and other
-related logic.
-
-All the data related to the command state is stored as dictionary in the object.
-The class has methods using which the data can be stored to disk in
-.hg/evolvestate file.
-
-We store the data on disk in cbor, for which we use cbor library to serialize
-and deserialize data.
-"""
-
-from __future__ import absolute_import
-
-from .thirdparty import cbor
-
-from mercurial import (
-    util,
-)
-
-class evolvestate():
-    """a wrapper class to store the state of `hg evolve` command
-
-    All the data for the state is stored in the form of key-value pairs in a
-    dictionary.
-
-    The class object can write all the data to .hg/evolvestate file and also can
-    populate the object data reading that file
-    """
-
-    def __init__(self, repo, path='evolvestate', opts={}):
-        self._repo = repo
-        self.path = path
-        self.opts = opts
-
-    def __nonzero__(self):
-        return self.exists()
-
-    def __getitem__(self, key):
-        return self.opts[key]
-
-    def load(self):
-        """load the existing evolvestate file into the class object"""
-        op = self._read()
-        self.opts.update(op)
-
-    def addopts(self, opts):
-        """add more key-value pairs to the data stored by the object"""
-        self.opts.update(opts)
-
-    def save(self):
-        """write all the evolvestate data stored in .hg/evolvestate file
-
-        we use third-party library cbor to serialize data to write in the file.
-        """
-        with self._repo.vfs(self.path, 'wb', atomictemp=True) as fp:
-            cbor.dump(self.opts, fp)
-
-    def _read(self):
-        """reads the evolvestate file and returns a dictionary which contain
-        data in the same format as it was before storing"""
-        with self._repo.vfs(self.path, 'rb') as fp:
-            return cbor.load(fp)
-
-    def delete(self):
-        """drop the evolvestate file if exists"""
-        util.unlinkpath(self._repo.vfs.join(self.path), ignoremissing=True)
-
-    def exists(self):
-        """check whether the evolvestate file exists or not"""
-        return self._repo.vfs.exists(self.path)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hgext3rd/evolve/state.py	Wed Jan 17 19:12:32 2018 +0530
@@ -0,0 +1,74 @@
+# This software may be used and distributed according to the terms of the
+# GNU General Public License version 2 or any later version.
+
+"""
+This file contains class to wrap the state for commands and other
+related logic.
+
+All the data related to the command state is stored as dictionary in the object.
+The class has methods using which the data can be stored to disk in a file under
+.hg/ directory.
+
+We store the data on disk in cbor, for which we use cbor library to serialize
+and deserialize data.
+"""
+
+from __future__ import absolute_import
+
+from .thirdparty import cbor
+
+from mercurial import (
+    util,
+)
+
+class cmdstate():
+    """a wrapper class to store the state of commands like `evolve`, `grab`
+
+    All the data for the state is stored in the form of key-value pairs in a
+    dictionary.
+
+    The class object can write all the data to a file in .hg/ directory and also
+    can populate the object data reading that file
+    """
+
+    def __init__(self, repo, path='evolvestate', opts={}):
+        self._repo = repo
+        self.path = path
+        self.opts = opts
+
+    def __nonzero__(self):
+        return self.exists()
+
+    def __getitem__(self, key):
+        return self.opts[key]
+
+    def load(self):
+        """load the existing evolvestate file into the class object"""
+        op = self._read()
+        self.opts.update(op)
+
+    def addopts(self, opts):
+        """add more key-value pairs to the data stored by the object"""
+        self.opts.update(opts)
+
+    def save(self):
+        """write all the evolvestate data stored in .hg/evolvestate file
+
+        we use third-party library cbor to serialize data to write in the file.
+        """
+        with self._repo.vfs(self.path, 'wb', atomictemp=True) as fp:
+            cbor.dump(self.opts, fp)
+
+    def _read(self):
+        """reads the evolvestate file and returns a dictionary which contain
+        data in the same format as it was before storing"""
+        with self._repo.vfs(self.path, 'rb') as fp:
+            return cbor.load(fp)
+
+    def delete(self):
+        """drop the evolvestate file if exists"""
+        util.unlinkpath(self._repo.vfs.join(self.path), ignoremissing=True)
+
+    def exists(self):
+        """check whether the evolvestate file exists or not"""
+        return self._repo.vfs.exists(self.path)