# HG changeset patch # User Pulkit Goyal # Date 2018-11-05 19:52:10 # Node ID 6f0b6905ef6fdfac7774191d7869131a31f99020 # Parent 525dcf5c1d41431f5476c6d09a9f3c430a131d9d 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 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 @@ testedwith = 'ships-with-hg-core' 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 @@ def uisetup(ui): # 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 @@ def cloneshallow(orig, ui, repo, *args, 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 @@ def reposetup(ui, repo): 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 @@ def onetimeclientsetup(ui): # 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 @@ def onetimeclientsetup(ui): # 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 @@ def onetimeclientsetup(ui): # 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 @@ def onetimeclientsetup(ui): # 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 @@ def onetimeclientsetup(ui): # 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 onetimeclientsetup(ui): 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 @@ def onetimeclientsetup(ui): # 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 @@ def onetimeclientsetup(ui): # 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 onetimeclientsetup(ui): 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 @@ def onetimeclientsetup(ui): # 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 @@ def getrenamedfn(repo, endrev=None): 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 @@ def filelogrevset(orig, repo, subset, x) 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 @@ def gcclient(ui, cachepath): # 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 @@ def gcclient(ui, cachepath): 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 @@ def wcpprefetch(ui, repo, **kwargs): """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 wcpprefetch(ui, repo, **kwargs): 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 @@ def exchangepull(orig, repo, remote, *ar 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 @@ def prefetch(ui, repo, *pats, **opts): 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) 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 @@ def debugindex(orig, ui, repo, file_=Non 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 debugindex(orig, ui, repo, file_=Non 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/remotefilelogserver.py b/hgext/remotefilelog/remotefilelogserver.py --- a/hgext/remotefilelog/remotefilelogserver.py +++ b/hgext/remotefilelog/remotefilelogserver.py @@ -132,7 +132,7 @@ def onetimesetup(ui): 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 @@ def onetimesetup(ui): 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 @@ def onetimesetup(ui): # 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 @@ def getfile(repo, proto, file, node): 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 getfile(repo, proto, file, node): 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/shallowbundle.py b/hgext/remotefilelog/shallowbundle.py --- a/hgext/remotefilelog/shallowbundle.py +++ b/hgext/remotefilelog/shallowbundle.py @@ -54,7 +54,7 @@ def shallowgroup(cls, self, nodelist, rl 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 @@ class shallowcg1packer(changegroup.cgpac 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 @@ class shallowcg1packer(changegroup.cgpac 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 @@ class shallowcg1packer(changegroup.cgpac 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 @@ def makechangegroup(orig, repo, outgoing 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/shallowutil.py b/hgext/remotefilelog/shallowutil.py --- a/hgext/remotefilelog/shallowutil.py +++ b/hgext/remotefilelog/shallowutil.py @@ -30,6 +30,10 @@ from . import constants 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)