Patchwork D8655: localrepo: unify share checking and related code

login
register
mail settings
Submitter phabricator
Date June 24, 2020, 12:56 p.m.
Message ID <differential-rev-PHID-DREV-vd75xxazyrli67rrdmtt-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/46555/
State New
Headers show

Comments

phabricator - June 24, 2020, 12:56 p.m.
pulkit created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This makes code cleaner. I might have done it in previous patch too but now the
  cleanup is much easier to understand.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D8655

AFFECTED FILES
  mercurial/localrepo.py

CHANGE DETAILS




To: pulkit, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -534,17 +534,28 @@ 
             raise
         requirements = set()
 
+    shared = False
+    sharedpath = None
+    sharedvfs = None
+    if b'shared' in requirements or b'relshared' in requirements:
+        shared = True
+        sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
+        if b'relshared' in requirements:
+            sharedpath = hgvfs.join(sharedpath)
+
+        sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
+
+        if not sharedvfs.exists():
+            raise error.RepoError(
+                _(b'.hg/sharedpath points to nonexistent directory %s')
+                % sharedvfs.base
+            )
+
     # if .hg/requires contains the exp-sharesafe requirement, it means
     # there exists a `.hg/store/requires` too and we should read it
-    # TODO: make this code more stricter by checking whether store exists
-    # and other checks
     if SHARESAFE_REQUIREMENT in requirements:
-        if hgvfs.exists(b'sharedpath'):
-            # This is a shared repo
-            sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
-            if b'relshared' in requirements:
-                sharedpath = hgvfs.join(sharedpath)
-            storevfs = vfsmod.vfs(vfsmod.vfs(sharedpath).join(b'store'))
+        if shared:
+            storevfs = vfsmod.vfs(sharedvfs.join(b'store'))
         else:
             storevfs = vfsmod.vfs(hgvfs.join(b'store'), cacheaudited=True)
         try:
@@ -596,21 +607,8 @@ 
     # ``relshared`` requirements indicate the store lives in the path contained
     # in the ``.hg/sharedpath`` file. This is an absolute path for
     # ``shared`` and relative to ``.hg/`` for ``relshared``.
-    if b'shared' in requirements or b'relshared' in requirements:
-        sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
-        if b'relshared' in requirements:
-            sharedpath = hgvfs.join(sharedpath)
-
-        sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
-
-        if not sharedvfs.exists():
-            raise error.RepoError(
-                _(b'.hg/sharedpath points to nonexistent directory %s')
-                % sharedvfs.base
-            )
-
+    if shared:
         features.add(repository.REPO_FEATURE_SHARED_STORAGE)
-
         storebasepath = sharedvfs.base
         cachepath = sharedvfs.join(b'cache')
     else: