diff hgext/histedit.py @ 27086:5f5c7d9f4a08

histedit: constant-ify the constraints list Used a class as a namespace, and then wired up a classmethod to return all known constraints. I'm mostly happy with this, even though it's kind of weird for hg.
author Augie Fackler <augie@google.com>
date Mon, 23 Nov 2015 10:13:05 -0500
parents d50ff8f4891f
children 3a2fd83182fb
line wrap: on
line diff
--- a/hgext/histedit.py	Tue Nov 17 15:04:31 2015 -0800
+++ b/hgext/histedit.py	Mon Nov 23 10:13:05 2015 -0500
@@ -181,6 +181,18 @@
 cmdtable = {}
 command = cmdutil.command(cmdtable)
 
+class _constraints(object):
+    # aborts if there are multiple rules for one node
+    noduplicates = 'noduplicates'
+    # abort if the node does belong to edited stack
+    forceother = 'forceother'
+    # abort if the node doesn't belong to edited stack
+    noother = 'noother'
+
+    @classmethod
+    def known(cls):
+        return set([v for k, v in cls.__dict__.items() if k[0] != '_'])
+
 # Note for extension authors: ONLY specify testedwith = 'internal' for
 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
 # be specifying the version(s) of Mercurial they are tested with, or
@@ -336,14 +348,8 @@
 
     def constraints(self):
         """Return a set of constrains that this action should be verified for
-
-           Available constraints:
-               noduplicates - aborts if there are multiple rules for one node
-               noother - abort if the node doesn't belong to edited stack
-               forceother - abort if the node does belong to edited stack
         """
-
-        return set(['noduplicates', 'noother'])
+        return set([_constraints.noduplicates, _constraints.noother])
 
     def nodetoverify(self):
         """Returns a node associated with the action that will be used for
@@ -648,7 +654,7 @@
 
 class base(histeditaction):
     def constraints(self):
-        return set(['forceother'])
+        return set([_constraints.forceother])
 
     def run(self):
         if self.repo['.'].node() != self.node:
@@ -1087,7 +1093,6 @@
     Will abort if there are to many or too few rules, a malformed rule,
     or a rule on a changeset outside of the user-given range.
     """
-    known_constraints = ['noother', 'noduplicates']
     parsed = []
     expected = set(c.hex() for c in ctxs)
     seen = set()
@@ -1101,21 +1106,21 @@
         action = actiontable[verb].fromrule(state, rest)
         constraints = action.constraints()
         for constraint in constraints:
-            if constraint not in known_constraints:
+            if constraint not in _constraints.known():
                 error.Abort(_('unknown constraint "%s"') % constraint)
 
         nodetoverify = action.nodetoverify()
         if nodetoverify is not None:
             ha = node.hex(nodetoverify)
-            if 'noother' in constraints and ha not in expected:
+            if _constraints.noother in constraints and ha not in expected:
                 raise error.Abort(
                     _('may not use "%s" with changesets '
                       'other than the ones listed') % verb)
-            if 'forceother' in constraints and ha in expected:
+            if _constraints.forceother in constraints and ha in expected:
                 raise error.Abort(
                     _('may not use "%s" with changesets '
                       'within the edited list') % verb)
-            if 'noduplicates' in constraints and ha in seen:
+            if _constraints.noduplicates in constraints and ha in seen:
                 raise error.Abort(_('duplicated command for changeset %s') %
                         ha[:12])
             seen.add(ha)