diff mercurial/localrepo.py @ 39549:089fc0db0954

hg: allow extra arguments to be passed to repo creation (API) Currently, repository creation is influenced by consulting the ui instance and turning config options into requirements. This means that in order to influence repository creation, you need to define and set a config option and that the option must translate to a requirement stored in the .hg/requires file. This commit introduces a new mechanism to influence repository creation. hg.repository() and hg.peer() have been taught to receive a new optional argument defining extra options to apply to repository creation. This value is passed along to the various instance() functions and can be used to influence repository creation. This will allow us to pass rich data directly to repository creation without having to go through the config layer. It also allows us to be more explicit about the features requested during repository creation and provides a natural point to detect unhandled options influencing repository creation. The new code detects when unknown creation options are present and aborts in that case. .. api:: options can now be passed to influence repository creation The various instance() functions to spawn new peers or repository instances now receive a ``createopts`` argument that can be a dict defining additional options to influence repository creation. localrepo.newreporequirements() also receives this argument. Differential Revision: https://phab.mercurial-scm.org/D4535
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 11 Sep 2018 17:11:32 -0700
parents 7ce9dea3a14a
children 261f1e8dc300
line wrap: on
line diff
--- a/mercurial/localrepo.py	Tue Sep 11 13:46:59 2018 -0700
+++ b/mercurial/localrepo.py	Tue Sep 11 17:11:32 2018 -0700
@@ -2380,26 +2380,28 @@
     assert name.startswith('journal')
     return os.path.join(base, name.replace('journal', 'undo', 1))
 
-def instance(ui, path, create, intents=None):
+def instance(ui, path, create, intents=None, createopts=None):
     if create:
         vfs = vfsmod.vfs(path, expandpath=True, realpath=True)
 
         if vfs.exists('.hg'):
             raise error.RepoError(_('repository %s already exists') % path)
 
-        createrepository(ui, vfs)
+        createrepository(ui, vfs, createopts=createopts)
 
     return localrepository(ui, util.urllocalpath(path), intents=intents)
 
 def islocal(path):
     return True
 
-def newreporequirements(ui):
+def newreporequirements(ui, createopts=None):
     """Determine the set of requirements for a new local repository.
 
     Extensions can wrap this function to specify custom requirements for
     new repositories.
     """
+    createopts = createopts or {}
+
     requirements = {'revlogv1'}
     if ui.configbool('format', 'usestore'):
         requirements.add('store')
@@ -2440,13 +2442,43 @@
 
     return requirements
 
-def createrepository(ui, wdirvfs):
+def filterknowncreateopts(ui, createopts):
+    """Filters a dict of repo creation options against options that are known.
+
+    Receives a dict of repo creation options and returns a dict of those
+    options that we don't know how to handle.
+
+    This function is called as part of repository creation. If the
+    returned dict contains any items, repository creation will not
+    be allowed, as it means there was a request to create a repository
+    with options not recognized by loaded code.
+
+    Extensions can wrap this function to filter out creation options
+    they know how to handle.
+    """
+    return dict(createopts)
+
+def createrepository(ui, wdirvfs, createopts=None):
     """Create a new repository in a vfs.
 
     ``wdirvfs`` is a vfs instance pointing at the working directory.
     ``requirements`` is a set of requirements for the new repository.
     """
-    requirements = newreporequirements(ui)
+    createopts = createopts or {}
+
+    unknownopts = filterknowncreateopts(ui, createopts)
+
+    if not isinstance(unknownopts, dict):
+        raise error.ProgrammingError('filterknowncreateopts() did not return '
+                                     'a dict')
+
+    if unknownopts:
+        raise error.Abort(_('unable to create repository because of unknown '
+                            'creation option: %s') %
+                          ', '.sorted(unknownopts),
+                          hint=_('is a required extension not loaded?'))
+
+    requirements = newreporequirements(ui, createopts=createopts)
 
     if not wdirvfs.exists():
         wdirvfs.makedirs()