localrepo: move check for existing repo into createrepository()
authorMartin von Zweigbergk <martinvonz@google.com>
Wed, 12 Sep 2018 08:41:00 -0700
changeset 39590 e471cb2852ea
parent 39589 4eb0f2452ad7
child 39591 76b58f240821
localrepo: move check for existing repo into createrepository() For symmetry with the check for existence of a repo in localrepository.__init__, we should check for the non-existence in createrepository(). We could alternatively move both checks into instance(). Differential Revision: https://phab.mercurial-scm.org/D4549
mercurial/localrepo.py
--- a/mercurial/localrepo.py	Wed Sep 12 21:32:08 2018 -0400
+++ b/mercurial/localrepo.py	Wed Sep 12 08:41:00 2018 -0700
@@ -2378,12 +2378,7 @@
 
 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, createopts=createopts)
+        createrepository(ui, path, createopts=createopts)
 
     return localrepository(ui, util.urllocalpath(path), intents=intents)
 
@@ -2459,10 +2454,10 @@
 
     return {k: v for k, v in createopts.items() if k not in known}
 
-def createrepository(ui, wdirvfs, createopts=None):
+def createrepository(ui, path, createopts=None):
     """Create a new repository in a vfs.
 
-    ``wdirvfs`` is a vfs instance pointing at the working directory.
+    ``path`` path to the new repo's working directory.
     ``createopts`` options for the new repository.
     """
     createopts = createopts or {}
@@ -2481,10 +2476,14 @@
 
     requirements = newreporequirements(ui, createopts=createopts)
 
+    wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
     if not wdirvfs.exists():
         wdirvfs.makedirs()
 
     hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
+    if hgvfs.exists():
+        raise error.RepoError(_('repository %s already exists') % path)
+
     hgvfs.makedir(notindexed=True)
 
     if b'store' in requirements: