D5190: shallowutil: introduce a helper function isenabled()

pulkit (Pulkit Goyal) phabricator at mercurial-scm.org
Mon Nov 5 19:57:43 UTC 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rHG6f0b6905ef6f: shallowutil: introduce a helper function isenabled() (authored by pulkit, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D5190?vs=12337&id=12426#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D5190?vs=12337&id=12426

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

AFFECTED FILES
  hgext/remotefilelog/__init__.py
  hgext/remotefilelog/debugcommands.py
  hgext/remotefilelog/remotefilelogserver.py
  hgext/remotefilelog/shallowbundle.py
  hgext/remotefilelog/shallowutil.py

CHANGE DETAILS

diff --git a/hgext/remotefilelog/shallowutil.py b/hgext/remotefilelog/shallowutil.py
--- a/hgext/remotefilelog/shallowutil.py
+++ b/hgext/remotefilelog/shallowutil.py
@@ -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)
diff --git a/hgext/remotefilelog/shallowbundle.py b/hgext/remotefilelog/shallowbundle.py
--- a/hgext/remotefilelog/shallowbundle.py
+++ b/hgext/remotefilelog/shallowbundle.py
@@ -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
diff --git a/hgext/remotefilelog/remotefilelogserver.py b/hgext/remotefilelog/remotefilelogserver.py
--- a/hgext/remotefilelog/remotefilelogserver.py
+++ b/hgext/remotefilelog/remotefilelogserver.py
@@ -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'))
diff --git a/hgext/remotefilelog/debugcommands.py b/hgext/remotefilelog/debugcommands.py
--- a/hgext/remotefilelog/debugcommands.py
+++ b/hgext/remotefilelog/debugcommands.py
@@ -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])
diff --git a/hgext/remotefilelog/__init__.py b/hgext/remotefilelog/__init__.py
--- a/hgext/remotefilelog/__init__.py
+++ b/hgext/remotefilelog/__init__.py
@@ -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,25 +577,23 @@
     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)
     extensions.wrapfunction(context.workingctx, 'filectx', workingfilectx)
 
     # 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)



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


More information about the Mercurial-devel mailing list