shallowutil: introduce a helper function isenabled()
authorPulkit Goyal <pulkit@yandex-team.ru>
Mon, 05 Nov 2018 11:52:10 -0800
changeset 40514 6f0b6905ef6f
parent 40513 525dcf5c1d41
child 40515 1c34b31a6c9c
shallowutil: introduce a helper function isenabled() This patch introduces a inenabled() function which will check whether remotefilelog is enabled or not. The function is then also used at all the places where check whether remotefilelog is enabled or not. The new function makes code easy to read without need to understand what is the constant involved and why we are checking repo.requirements. Differential Revision: https://phab.mercurial-scm.org/D5190
hgext/remotefilelog/__init__.py
hgext/remotefilelog/debugcommands.py
hgext/remotefilelog/remotefilelogserver.py
hgext/remotefilelog/shallowbundle.py
hgext/remotefilelog/shallowutil.py
--- a/hgext/remotefilelog/__init__.py	Wed Oct 24 16:59:05 2018 +0300
+++ b/hgext/remotefilelog/__init__.py	Mon Nov 05 11:52:10 2018 -0800
@@ -216,6 +216,8 @@
 repoclass = localrepo.localrepository
 repoclass._basesupported.add(constants.SHALLOWREPO_REQUIREMENT)
 
+isenabled = shallowutil.isenabled
+
 def uisetup(ui):
     """Wraps user facing Mercurial commands to swap them out with shallow
     versions.
@@ -236,8 +238,7 @@
 
     # Prevent 'hg manifest --all'
     def _manifest(orig, ui, repo, *args, **opts):
-        if (constants.SHALLOWREPO_REQUIREMENT in repo.requirements
-            and opts.get('all')):
+        if (isenabled(repo) and opts.get('all')):
             raise error.Abort(_("--all is not supported in a shallow repo"))
 
         return orig(ui, repo, *args, **opts)
@@ -262,7 +263,7 @@
     if opts.get('shallow'):
         repos = []
         def pull_shallow(orig, self, *args, **kwargs):
-            if constants.SHALLOWREPO_REQUIREMENT not in self.requirements:
+            if not isenabled(self):
                 repos.append(self.unfiltered())
                 # set up the client hooks so the post-clone update works
                 setupclient(self.ui, self.unfiltered())
@@ -347,7 +348,7 @@
     ui.setconfig('hooks', 'commit.prefetch', wcpprefetch)
 
     isserverenabled = ui.configbool('remotefilelog', 'server')
-    isshallowclient = constants.SHALLOWREPO_REQUIREMENT in repo.requirements
+    isshallowclient = isenabled(repo)
 
     if isserverenabled and isshallowclient:
         raise RuntimeError("Cannot be both a server and shallow client.")
@@ -396,7 +397,7 @@
 
     # prefetch files before update
     def applyupdates(orig, repo, actions, wctx, mctx, overwrite, labels=None):
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             manifest = mctx.manifest()
             files = []
             for f, args, msg in actions['g']:
@@ -409,7 +410,7 @@
     # Prefetch merge checkunknownfiles
     def checkunknownfiles(orig, repo, wctx, mctx, force, actions,
                           *args, **kwargs):
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             files = []
             sparsematch = repo.maybesparsematch(mctx.rev())
             for f, (m, actionargs, msg) in actions.iteritems():
@@ -428,7 +429,7 @@
     # Prefetch files before status attempts to look at their size and contents
     def checklookup(orig, self, files):
         repo = self._repo
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             prefetchfiles = []
             for parent in self._parents:
                 for f in files:
@@ -441,7 +442,7 @@
 
     # Prefetch the logic that compares added and removed files for renames
     def findrenames(orig, repo, matcher, added, removed, *args, **kwargs):
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             files = []
             parentctx = repo['.']
             for f in removed:
@@ -454,7 +455,7 @@
     # prefetch files before mergecopies check
     def computenonoverlap(orig, repo, c1, c2, *args, **kwargs):
         u1, u2 = orig(repo, c1, c2, *args, **kwargs)
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             m1 = c1.manifest()
             m2 = c2.manifest()
             files = []
@@ -486,7 +487,7 @@
     def computeforwardmissing(orig, a, b, match=None):
         missing = list(orig(a, b, match=match))
         repo = a._repo
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             mb = b.manifest()
 
             files = []
@@ -513,7 +514,7 @@
             # repo can be None when running in chg:
             # - at startup, reposetup was called because serve is not norepo
             # - a norepo command like "help" is called
-            if repo and constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+            if repo and isenabled(repo):
                 repo.fileservice.close()
     extensions.wrapfunction(dispatch, 'runcommand', runcommand)
 
@@ -525,7 +526,7 @@
 
     # prevent strip from stripping remotefilelogs
     def _collectbrokencsets(orig, repo, files, striprev):
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             files = list([f for f in files if not repo.shallowmatch(f)])
         return orig(repo, files, striprev)
     extensions.wrapfunction(repair, '_collectbrokencsets', _collectbrokencsets)
@@ -576,16 +577,14 @@
     def filectx(orig, self, path, fileid=None, filelog=None):
         if fileid is None:
             fileid = self.filenode(path)
-        if (constants.SHALLOWREPO_REQUIREMENT in self._repo.requirements and
-            self._repo.shallowmatch(path)):
+        if (isenabled(self._repo) and self._repo.shallowmatch(path)):
             return remotefilectx.remotefilectx(self._repo, path,
                 fileid=fileid, changectx=self, filelog=filelog)
         return orig(self, path, fileid=fileid, filelog=filelog)
     extensions.wrapfunction(context.changectx, 'filectx', filectx)
 
     def workingfilectx(orig, self, path, filelog=None):
-        if (constants.SHALLOWREPO_REQUIREMENT in self._repo.requirements and
-            self._repo.shallowmatch(path)):
+        if (isenabled(self._repo) and self._repo.shallowmatch(path)):
             return remotefilectx.remoteworkingfilectx(self._repo,
                 path, workingctx=self, filelog=filelog)
         return orig(self, path, filelog=filelog)
@@ -594,7 +593,7 @@
     # prefetch required revisions before a diff
     def trydiff(orig, repo, revs, ctx1, ctx2, modified, added, removed,
                 copy, getfilectx, *args, **kwargs):
-        if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if isenabled(repo):
             prefetch = []
             mf1 = ctx1.manifest()
             for fname in modified + added + removed:
@@ -652,7 +651,7 @@
     return getrenamed
 
 def walkfilerevs(orig, repo, match, follow, revs, fncache):
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not isenabled(repo):
         return orig(repo, match, follow, revs, fncache)
 
     # remotefilelog's can't be walked in rev order, so throw.
@@ -692,7 +691,7 @@
     a slower, more accurate result, use ``file()``.
     """
 
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not isenabled(repo):
         return orig(repo, subset, x)
 
     # i18n: "filelog" is a keyword
@@ -800,7 +799,7 @@
         # Protect against any repo or config changes that have happened since
         # this repo was added to the repos file. We'd rather this loop succeed
         # and too much be deleted, than the loop fail and nothing gets deleted.
-        if constants.SHALLOWREPO_REQUIREMENT not in repo.requirements:
+        if not isenabled(repo):
             continue
 
         if not util.safehasattr(repo, 'name'):
@@ -849,7 +848,7 @@
         ui.warn(_("warning: no valid repos in repofile\n"))
 
 def log(orig, ui, repo, *pats, **opts):
-    if constants.SHALLOWREPO_REQUIREMENT not in repo.requirements:
+    if not isenabled(repo):
         return orig(ui, repo, *pats, **opts)
 
     follow = opts.get('follow')
@@ -910,7 +909,7 @@
     """Prefetches in background revisions specified by bgprefetchrevs revset.
     Does background repack if backgroundrepack flag is set in config.
     """
-    shallow = constants.SHALLOWREPO_REQUIREMENT in repo.requirements
+    shallow = isenabled(repo)
     bgprefetchrevs = ui.config('remotefilelog', 'bgprefetchrevs')
     isready = readytofetch(repo)
 
@@ -932,7 +931,7 @@
 def pull(orig, ui, repo, *pats, **opts):
     result = orig(ui, repo, *pats, **opts)
 
-    if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if isenabled(repo):
         # prefetch if it's configured
         prefetchrevset = ui.config('remotefilelog', 'pullprefetch')
         bgrepack = repo.ui.configbool('remotefilelog', 'backgroundrepack')
@@ -972,7 +971,7 @@
     return orig(repo, remote, *args, **kwargs)
 
 def _fileprefetchhook(repo, revs, match):
-    if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if isenabled(repo):
         allfiles = []
         for rev in revs:
             if rev == nodemod.wdirrev or rev is None:
@@ -1068,7 +1067,7 @@
 
     Return 0 on success.
     """
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not isenabled(repo):
         raise error.Abort(_("repo is not shallow"))
 
     opts = resolveprefetchopts(ui, opts)
--- a/hgext/remotefilelog/debugcommands.py	Wed Oct 24 16:59:05 2018 +0300
+++ b/hgext/remotefilelog/debugcommands.py	Mon Nov 05 11:52:10 2018 -0800
@@ -92,7 +92,7 @@
     if (opts.get('changelog') or
         opts.get('manifest') or
         opts.get('dir') or
-        not constants.SHALLOWREPO_REQUIREMENT in repo.requirements or
+        not shallowutil.isenabled(repo) or
         not repo.shallowmatch(file_)):
         return orig(ui, repo, file_, **opts)
 
@@ -139,7 +139,7 @@
 
 def debugindexdot(orig, ui, repo, file_):
     """dump an index DAG as a graphviz dot file"""
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not shallowutil.isenabled(repo):
         return orig(ui, repo, file_)
 
     r = buildtemprevlog(repo, os.path.basename(file_)[:-2])
--- a/hgext/remotefilelog/remotefilelogserver.py	Wed Oct 24 16:59:05 2018 +0300
+++ b/hgext/remotefilelog/remotefilelogserver.py	Mon Nov 05 11:52:10 2018 -0800
@@ -132,7 +132,7 @@
     def _walkstreamfiles(orig, repo):
         if state.shallowremote:
             # if we are shallow ourselves, stream our local commits
-            if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+            if shallowutil.isenabled(repo):
                 striplen = len(repo.store.path) + 1
                 readdir = repo.store.rawvfs.readdir
                 visit = [os.path.join(repo.store.path, 'data')]
@@ -166,7 +166,7 @@
                     continue
                 yield x
 
-        elif constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        elif shallowutil.isenabled(repo):
             # don't allow cloning from a shallow repo to a full repo
             # since it would require fetching every version of every
             # file in order to create the revlogs.
@@ -193,8 +193,8 @@
     # expose remotefilelog capabilities
     def _capabilities(orig, repo, proto):
         caps = orig(repo, proto)
-        if ((constants.SHALLOWREPO_REQUIREMENT in repo.requirements or
-            ui.configbool('remotefilelog', 'server'))):
+        if (shallowutil.isenabled(repo) or ui.configbool('remotefilelog',
+                                                         'server')):
             if isinstance(proto, _sshv1server):
                 # legacy getfiles method which only works over ssh
                 caps.append(constants.NETWORK_CAP_LEGACY_SSH_GETFILES)
@@ -278,7 +278,7 @@
     data is a compressed blob with revlog flag and ancestors information. See
     createfileblob for its content.
     """
-    if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if shallowutil.isenabled(repo):
         return '1\0' + _('cannot fetch remote files from shallow repo')
     cachepath = repo.ui.config("remotefilelog", "servercachepath")
     if not cachepath:
@@ -291,7 +291,7 @@
 def getfiles(repo, proto):
     """A server api for requesting particular versions of particular files.
     """
-    if constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if shallowutil.isenabled(repo):
         raise error.Abort(_('cannot fetch remote files from shallow repo'))
     if not isinstance(proto, _sshv1server):
         raise error.Abort(_('cannot fetch remote files over non-ssh protocol'))
--- a/hgext/remotefilelog/shallowbundle.py	Wed Oct 24 16:59:05 2018 +0300
+++ b/hgext/remotefilelog/shallowbundle.py	Mon Nov 05 11:52:10 2018 -0800
@@ -54,7 +54,7 @@
 
 class shallowcg1packer(changegroup.cgpacker):
     def generate(self, commonrevs, clnodes, fastpathlinkrev, source):
-        if constants.SHALLOWREPO_REQUIREMENT in self._repo.requirements:
+        if shallowutil.isenabled(self._repo):
             fastpathlinkrev = False
 
         return super(shallowcg1packer, self).generate(commonrevs, clnodes,
@@ -69,7 +69,7 @@
             linknodes, commonrevs, source = args
         except ValueError:
             commonrevs, source, mfdicts, fastpathlinkrev, fnodes, clrevs = args
-        if constants.SHALLOWREPO_REQUIREMENT in self._repo.requirements:
+        if shallowutil.isenabled(self._repo):
             repo = self._repo
             if isinstance(repo, bundlerepo.bundlerepository):
                 # If the bundle contains filelogs, we can't pull from it, since
@@ -91,7 +91,7 @@
 
     def shouldaddfilegroups(self, source):
         repo = self._repo
-        if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+        if not shallowutil.isenabled(repo):
             return AllFiles
 
         if source == "push" or source == "bundle":
@@ -139,7 +139,7 @@
         yield delta
 
 def makechangegroup(orig, repo, outgoing, version, source, *args, **kwargs):
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not shallowutil.isenabled(repo):
         return orig(repo, outgoing, version, source, *args, **kwargs)
 
     original = repo.shallowmatch
@@ -168,7 +168,7 @@
         repo.shallowmatch = original
 
 def addchangegroupfiles(orig, repo, source, revmap, trp, expectedfiles, *args):
-    if not constants.SHALLOWREPO_REQUIREMENT in repo.requirements:
+    if not shallowutil.isenabled(repo):
         return orig(repo, source, revmap, trp, expectedfiles, *args)
 
     files = 0
--- a/hgext/remotefilelog/shallowutil.py	Wed Oct 24 16:59:05 2018 +0300
+++ b/hgext/remotefilelog/shallowutil.py	Mon Nov 05 11:52:10 2018 -0800
@@ -30,6 +30,10 @@
 if not pycompat.iswindows:
     import grp
 
+def isenabled(repo):
+    """returns whether the repository is remotefilelog enabled or not"""
+    return constants.SHALLOWREPO_REQUIREMENT in repo.requirements
+
 def getcachekey(reponame, file, id):
     pathhash = hashlib.sha1(file).hexdigest()
     return os.path.join(reponame, pathhash[:2], pathhash[2:], id)