diff mercurial/registrar.py @ 37716:dfc51a482031

registrar: replace "cmdtype" with an intent-based mechanism (API) Commands perform varied actions and repositories vary in their capabilities. Historically, the .hg/requires file has been used to lock out clients lacking a requirement. But this is a very heavy-handed approach and is typically reserved for cases where the on-disk storage format changes and we want to prevent incompatible clients from operating on a repo. Outside of the .hg/requires file, we tend to deal with things like optional, extension-provided features via checking at call sites. We'll either have checks in core or extensions will monkeypatch functions in core disabling incompatible features, enabling new features, etc. Things are somewhat tolerable today. But once we introduce alternate storage backends with varying support for repository features and vastly different modes of behavior, the current model will quickly grow unwieldy. For example, the implementation of the "simple store" required a lot of hacks to deal with stripping and verify because various parts of core assume things are implemented a certain way. Partial clone will require new ways of modeling file data retrieval, because we can no longer assume that all file data is already local. In this new world, some commands might not make any sense for certain types of repositories. What we need is a mechanism to affect the construction of repository (and eventually peer) instances so the requirements/capabilities needed for the current operation can be taken into account. "Current operation" can almost certainly be defined by a command. So it makes sense for commands to declare their intended actions. This commit introduces the "intents" concept on the command registrar. "intents" captures a set of strings that declare actions that are anticipated to be taken, requirements the repository must possess, etc. These intents will be passed into hg.repo(), which will pass them into localrepository, where they can be used to influence the object being created. Some use cases for this include: * For read-only intents, constructing a repository object that doesn't expose methods that can mutate the repository. Its VFS instances don't even allow opening a file with write access. * For read-only intents, constructing a repository object without cache invalidation logic. If the repo never changes during its lifetime, nothing ever needs to be invalidated and we don't need to do expensive things like verify the changelog's hidden revisions state is accurate every time we access repo.changelog. * We can automatically hide commands from `hg help` when the current repository doesn't provide that command. For example, an alternate storage backend may not support `hg commit`, so we can hide that command or anything else that would perform local commits. We already kind of had an "intents" mechanism on the registrar in the form of "cmdtype." However, it was never used. And it was limited to a single value. We really need something that supports multiple intents. And because intents may be defined by extensions and at this point are advisory, I think it is best to define them in a set rather than as separate arguments/attributes on the command. Differential Revision: https://phab.mercurial-scm.org/D3376
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 14 Apr 2018 09:23:48 -0700
parents 9bcf096a2da2
children aa98392eb5b0
line wrap: on
line diff
--- a/mercurial/registrar.py	Sat Apr 14 11:20:38 2018 -0400
+++ b/mercurial/registrar.py	Sat Apr 14 09:23:48 2018 -0700
@@ -138,15 +138,18 @@
     potential repository locations. See ``findrepo()``. If a repository is
     found, it will be used and passed to the decorated function.
 
-    There are three constants in the class which tells what type of the command
-    that is. That information will be helpful at various places. It will be also
-    be used to decide what level of access the command has on hidden commits.
-    The constants are:
+    The `intents` argument defines a set of intended actions or capabilities
+    the command is taking. These intents can be used to affect the construction
+    of the repository object passed to the command. For example, commands
+    declaring that they are read-only could receive a repository that doesn't
+    have any methods allowing repository mutation. Other intents could be used
+    to prevent the command from running if the requested intent could not be
+    fulfilled.
 
-    `unrecoverablewrite` is for those write commands which can't be recovered
-    like push.
-    `recoverablewrite` is for write commands which can be recovered like commit.
-    `readonly` is for commands which are read only.
+    The following intents are defined:
+
+    readonly
+       The command is read-only
 
     The signature of the decorated function looks like this:
         def cmd(ui[, repo] [, <args>] [, <options>])
@@ -161,29 +164,22 @@
     descriptions and examples.
     """
 
-    unrecoverablewrite = "unrecoverable"
-    recoverablewrite = "recoverable"
-    readonly = "readonly"
-
-    possiblecmdtypes = {unrecoverablewrite, recoverablewrite, readonly}
-
     def _doregister(self, func, name, options=(), synopsis=None,
                     norepo=False, optionalrepo=False, inferrepo=False,
-                    cmdtype=unrecoverablewrite):
+                    intents=None):
 
-        if cmdtype not in self.possiblecmdtypes:
-            raise error.ProgrammingError("unknown cmdtype value '%s' for "
-                                         "'%s' command" % (cmdtype, name))
         func.norepo = norepo
         func.optionalrepo = optionalrepo
         func.inferrepo = inferrepo
-        func.cmdtype = cmdtype
+        func.intents = intents or set()
         if synopsis:
             self._table[name] = func, list(options), synopsis
         else:
             self._table[name] = func, list(options)
         return func
 
+INTENT_READONLY = b'readonly'
+
 class revsetpredicate(_funcregistrarbase):
     """Decorator to register revset predicate