[PATCH 2 of 3] mq: switch to replaceclass for monkeypatching

Bryan O'Sullivan bos at serpentine.com
Tue Nov 20 16:55:44 CST 2012


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1353452137 28800
# Node ID 5e900b16c15eae6113a9e3de6684f6da42784d52
# Parent  cbf274dc847b5c28a282ae98a7d8b9bd48b6137d
mq: switch to replaceclass for monkeypatching

This change is 99.5% un-indenting code without changes.

diff --git a/hgext/mq.py b/hgext/mq.py
--- a/hgext/mq.py
+++ b/hgext/mq.py
@@ -62,7 +62,7 @@ such as -f/--force or --exact are passed
 from mercurial.i18n import _
 from mercurial.node import bin, hex, short, nullid, nullrev
 from mercurial.lock import release
-from mercurial import commands, cmdutil, hg, scmutil, util, revset
+from mercurial import commands, cmdutil, hg, localrepo, scmutil, util, revset
 from mercurial import repair, extensions, error, phases
 from mercurial import patch as patchmod
 import os, re, errno, shutil
@@ -3399,116 +3399,115 @@ def mqphasedefaults(repo, roots):
         roots[mqphase].add(qbase.node())
     return roots
 
-def reposetup(ui, repo):
-    class mqrepo(repo.__class__):
-        @util.propertycache
-        def mq(self):
-            return queue(self.ui, self.path)
-
-        def abortifwdirpatched(self, errmsg, force=False):
-            if self.mq.applied and not force:
-                parents = self.dirstate.parents()
-                patches = [s.node for s in self.mq.applied]
-                if parents[0] in patches or parents[1] in patches:
-                    raise util.Abort(errmsg)
-
-        def commit(self, text="", user=None, date=None, match=None,
-                   force=False, editor=False, extra={}):
-            self.abortifwdirpatched(
-                _('cannot commit over an applied mq patch'),
-                force)
-
-            return super(mqrepo, self).commit(text, user, date, match, force,
-                                              editor, extra)
-
-        def checkpush(self, force, revs):
-            if self.mq.applied and not force:
-                outapplied = [e.node for e in self.mq.applied]
-                if revs:
-                    # Assume applied patches have no non-patch descendants and
-                    # are not on remote already. Filtering any changeset not
-                    # pushed.
-                    heads = set(revs)
-                    for node in reversed(outapplied):
-                        if node in heads:
-                            break
-                        else:
-                            outapplied.pop()
-                # looking for pushed and shared changeset
-                for node in outapplied:
-                    if self[node].phase() < phases.secret:
-                        raise util.Abort(_('source has mq patches applied'))
-                # no non-secret patches pushed
-            super(mqrepo, self).checkpush(force, revs)
-
-        def _findtags(self):
-            '''augment tags from base class with patch tags'''
-            result = super(mqrepo, self)._findtags()
-
-            q = self.mq
-            if not q.applied:
-                return result
-
-            mqtags = [(patch.node, patch.name) for patch in q.applied]
-
+ at extensions.replaceclass(localrepo, 'localrepository')
+class mqrepo(localrepo.localrepository):
+    @util.propertycache
+    def mq(self):
+        return queue(self.ui, self.path)
+
+    def abortifwdirpatched(self, errmsg, force=False):
+        if self.mq.applied and not force:
+            parents = self.dirstate.parents()
+            patches = [s.node for s in self.mq.applied]
+            if parents[0] in patches or parents[1] in patches:
+                raise util.Abort(errmsg)
+
+    def commit(self, text="", user=None, date=None, match=None,
+               force=False, editor=False, extra={}):
+        self.abortifwdirpatched(
+            _('cannot commit over an applied mq patch'),
+            force)
+
+        return super(mqrepo, self).commit(text, user, date, match, force,
+                                          editor, extra)
+
+    def checkpush(self, force, revs):
+        if self.mq.applied and not force:
+            outapplied = [e.node for e in self.mq.applied]
+            if revs:
+                # Assume applied patches have no non-patch descendants and
+                # are not on remote already. Filtering any changeset not
+                # pushed.
+                heads = set(revs)
+                for node in reversed(outapplied):
+                    if node in heads:
+                        break
+                    else:
+                        outapplied.pop()
+            # looking for pushed and shared changeset
+            for node in outapplied:
+                if self[node].phase() < phases.secret:
+                    raise util.Abort(_('source has mq patches applied'))
+            # no non-secret patches pushed
+        super(mqrepo, self).checkpush(force, revs)
+
+    def _findtags(self):
+        '''augment tags from base class with patch tags'''
+        result = super(mqrepo, self)._findtags()
+
+        q = self.mq
+        if not q.applied:
+            return result
+
+        mqtags = [(patch.node, patch.name) for patch in q.applied]
+
+        try:
+            self.changelog.rev(mqtags[-1][0])
+        except error.LookupError:
+            self.ui.warn(_('mq status file refers to unknown node %s\n')
+                         % short(mqtags[-1][0]))
+            return result
+
+        mqtags.append((mqtags[-1][0], 'qtip'))
+        mqtags.append((mqtags[0][0], 'qbase'))
+        mqtags.append((self.changelog.parents(mqtags[0][0])[0], 'qparent'))
+        tags = result[0]
+        for patch in mqtags:
+            if patch[1] in tags:
+                self.ui.warn(_('tag %s overrides mq patch of the same '
+                               'name\n') % patch[1])
+            else:
+                tags[patch[1]] = patch[0]
+
+        return result
+
+    def _branchtags(self, partial, lrev):
+        q = self.mq
+        cl = self.changelog
+        qbase = None
+        if not q.applied:
+            if getattr(self, '_committingpatch', False):
+                # Committing a new patch, must be tip
+                qbase = len(cl) - 1
+        else:
+            qbasenode = q.applied[0].node
             try:
-                self.changelog.rev(mqtags[-1][0])
+                qbase = cl.rev(qbasenode)
             except error.LookupError:
                 self.ui.warn(_('mq status file refers to unknown node %s\n')
-                             % short(mqtags[-1][0]))
-                return result
-
-            mqtags.append((mqtags[-1][0], 'qtip'))
-            mqtags.append((mqtags[0][0], 'qbase'))
-            mqtags.append((self.changelog.parents(mqtags[0][0])[0], 'qparent'))
-            tags = result[0]
-            for patch in mqtags:
-                if patch[1] in tags:
-                    self.ui.warn(_('tag %s overrides mq patch of the same '
-                                   'name\n') % patch[1])
-                else:
-                    tags[patch[1]] = patch[0]
-
-            return result
-
-        def _branchtags(self, partial, lrev):
-            q = self.mq
-            cl = self.changelog
-            qbase = None
-            if not q.applied:
-                if getattr(self, '_committingpatch', False):
-                    # Committing a new patch, must be tip
-                    qbase = len(cl) - 1
-            else:
-                qbasenode = q.applied[0].node
-                try:
-                    qbase = cl.rev(qbasenode)
-                except error.LookupError:
-                    self.ui.warn(_('mq status file refers to unknown node %s\n')
-                                 % short(qbasenode))
-            if qbase is None:
-                return super(mqrepo, self)._branchtags(partial, lrev)
-
-            start = lrev + 1
-            if start < qbase:
-                # update the cache (excluding the patches) and save it
-                ctxgen = (self[r] for r in xrange(lrev + 1, qbase))
-                self._updatebranchcache(partial, ctxgen)
-                self._writebranchcache(partial, cl.node(qbase - 1), qbase - 1)
-                start = qbase
-            # if start = qbase, the cache is as updated as it should be.
-            # if start > qbase, the cache includes (part of) the patches.
-            # we might as well use it, but we won't save it.
-
-            # update the cache up to the tip
-            ctxgen = (self[r] for r in xrange(start, len(cl)))
+                             % short(qbasenode))
+        if qbase is None:
+            return super(mqrepo, self)._branchtags(partial, lrev)
+
+        start = lrev + 1
+        if start < qbase:
+            # update the cache (excluding the patches) and save it
+            ctxgen = (self[r] for r in xrange(lrev + 1, qbase))
             self._updatebranchcache(partial, ctxgen)
-
-            return partial
-
+            self._writebranchcache(partial, cl.node(qbase - 1), qbase - 1)
+            start = qbase
+        # if start = qbase, the cache is as updated as it should be.
+        # if start > qbase, the cache includes (part of) the patches.
+        # we might as well use it, but we won't save it.
+
+        # update the cache up to the tip
+        ctxgen = (self[r] for r in xrange(start, len(cl)))
+        self._updatebranchcache(partial, ctxgen)
+
+        return partial
+
+def reposetup(ui, repo):
     if repo.local():
-        repo.__class__ = mqrepo
-
         repo._phasedefaults.append(mqphasedefaults)
 
 def mqimport(orig, ui, repo, *args, **kwargs):


More information about the Mercurial-devel mailing list