1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
# shallowrepo.py - shallow repository that uses remote filelogs
#
# Copyright 2013 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import annotations
import os
from mercurial.i18n import _
from mercurial.node import hex, nullrev
from mercurial import (
encoding,
error,
localrepo,
match,
scmutil,
sparse,
util,
)
from mercurial.utils import procutil
from . import (
connectionpool,
constants,
contentstore,
datapack,
fileserverclient,
historypack,
metadatastore,
remotefilectx,
remotefilelog,
shallowutil,
)
# These make*stores functions are global so that other extensions can replace
# them.
def makelocalstores(repo):
"""In-repo stores, like .hg/store/data; can not be discarded."""
localpath = os.path.join(repo.svfs.vfs.base, b'data')
if not os.path.exists(localpath):
os.makedirs(localpath)
# Instantiate local data stores
localcontent = contentstore.remotefilelogcontentstore(
repo, localpath, repo.name, shared=False
)
localmetadata = metadatastore.remotefilelogmetadatastore(
repo, localpath, repo.name, shared=False
)
return localcontent, localmetadata
def makecachestores(repo):
"""Typically machine-wide, cache of remote data; can be discarded."""
# Instantiate shared cache stores
cachepath = shallowutil.getcachepath(repo.ui)
cachecontent = contentstore.remotefilelogcontentstore(
repo, cachepath, repo.name, shared=True
)
cachemetadata = metadatastore.remotefilelogmetadatastore(
repo, cachepath, repo.name, shared=True
)
repo.sharedstore = cachecontent
repo.shareddatastores.append(cachecontent)
repo.sharedhistorystores.append(cachemetadata)
return cachecontent, cachemetadata
def makeremotestores(repo, cachecontent, cachemetadata):
"""These stores fetch data from a remote server."""
# Instantiate remote stores
repo.fileservice = fileserverclient.fileserverclient(repo)
remotecontent = contentstore.remotecontentstore(
repo.ui, repo.fileservice, cachecontent
)
remotemetadata = metadatastore.remotemetadatastore(
repo.ui, repo.fileservice, cachemetadata
)
return remotecontent, remotemetadata
def makepackstores(repo):
"""Packs are more efficient (to read from) cache stores."""
# Instantiate pack stores
packpath = shallowutil.getcachepackpath(repo, constants.FILEPACK_CATEGORY)
packcontentstore = datapack.datapackstore(repo.ui, packpath)
packmetadatastore = historypack.historypackstore(repo.ui, packpath)
repo.shareddatastores.append(packcontentstore)
repo.sharedhistorystores.append(packmetadatastore)
shallowutil.reportpackmetrics(
repo.ui, b'filestore', packcontentstore, packmetadatastore
)
return packcontentstore, packmetadatastore
def makeunionstores(repo):
"""Union stores iterate the other stores and return the first result."""
repo.shareddatastores = []
repo.sharedhistorystores = []
packcontentstore, packmetadatastore = makepackstores(repo)
cachecontent, cachemetadata = makecachestores(repo)
localcontent, localmetadata = makelocalstores(repo)
remotecontent, remotemetadata = makeremotestores(
repo, cachecontent, cachemetadata
)
# Instantiate union stores
repo.contentstore = contentstore.unioncontentstore(
packcontentstore,
cachecontent,
localcontent,
remotecontent,
writestore=localcontent,
)
repo.metadatastore = metadatastore.unionmetadatastore(
packmetadatastore,
cachemetadata,
localmetadata,
remotemetadata,
writestore=localmetadata,
)
fileservicedatawrite = cachecontent
fileservicehistorywrite = cachemetadata
repo.fileservice.setstore(
repo.contentstore,
repo.metadatastore,
fileservicedatawrite,
fileservicehistorywrite,
)
shallowutil.reportpackmetrics(
repo.ui, b'filestore', packcontentstore, packmetadatastore
)
def wraprepo(repo):
class shallowrepository(repo.__class__):
@util.propertycache
def name(self):
return self.ui.config(b'remotefilelog', b'reponame')
@util.propertycache
def fallbackpath(self):
path = repo.ui.config(
b"remotefilelog",
b"fallbackpath",
repo.ui.config(b'paths', b'default'),
)
if not path:
raise error.Abort(
b"no remotefilelog server "
b"configured - is your .hg/hgrc trusted?"
)
return path
def maybesparsematch(self, *revs, **kwargs):
"""
A wrapper that allows the remotefilelog to invoke sparsematch() if
this is a sparse repository, or returns None if this is not a
sparse repository.
"""
if revs:
ret = sparse.matcher(repo, revs=revs)
else:
ret = sparse.matcher(repo)
if ret.always():
return None
return ret
def file(self, f, writable=False):
if f[0] == b'/':
f = f[1:]
if self.shallowmatch(f):
return remotefilelog.remotefilelog(self.svfs, f, self)
else:
return super().file(f, writable=writable)
def filectx(self, path, *args, **kwargs):
if self.shallowmatch(path):
return remotefilectx.remotefilectx(self, path, *args, **kwargs)
else:
return super().filectx(path, *args, **kwargs)
@localrepo.unfilteredmethod
def commitctx(self, ctx, error=False, origctx=None):
"""Add a new revision to current repository.
Revision information is passed via the context argument.
"""
# some contexts already have manifest nodes, they don't need any
# prefetching (for example if we're just editing a commit message
# we can reuse manifest
if not ctx.manifestnode():
# prefetch files that will likely be compared
m1 = ctx.p1().manifest()
files = []
for f in ctx.modified() + ctx.added():
fparent1 = m1.get(f, self.nullid)
if fparent1 != self.nullid:
files.append((f, hex(fparent1)))
self.fileservice.prefetch(files)
return super().commitctx(ctx, error=error, origctx=origctx)
def backgroundprefetch(
self, revs, base=None, repack=False, pats=None, opts=None
):
"""Runs prefetch in background with optional repack"""
cmd = [procutil.hgexecutable(), b'-R', repo.origroot, b'prefetch']
if repack:
cmd.append(b'--repack')
if revs:
cmd += [b'-r', revs]
# We know this command will find a binary, so don't block
# on it starting.
kwargs = {}
if repo.ui.configbool(b'devel', b'remotefilelog.bg-wait'):
kwargs['record_wait'] = repo.ui.atexit
procutil.runbgcommand(
cmd, encoding.environ, ensurestart=False, **kwargs
)
def prefetch(self, revs, base=None, pats=None, opts=None):
"""Prefetches all the necessary file revisions for the given revs
Optionally runs repack in background
"""
with repo._lock(
repo.svfs,
b'prefetchlock',
True,
None,
None,
_(b'prefetching in %s') % repo.origroot,
):
self._prefetch(revs, base, pats, opts)
def _prefetch(self, revs, base=None, pats=None, opts=None):
fallbackpath = self.fallbackpath
if fallbackpath:
# If we know a rev is on the server, we should fetch the server
# version of those files, since our local file versions might
# become obsolete if the local commits are stripped.
localrevs = repo.revs(b'outgoing(%s)', fallbackpath)
if base is not None and base != nullrev:
serverbase = list(
repo.revs(
b'first(reverse(::%s) - %ld)', base, localrevs
)
)
if serverbase:
base = serverbase[0]
else:
localrevs = repo
mfl = repo.manifestlog
mfrevlog = mfl.getstorage(b'')
if base is not None:
mfdict = mfl[repo[base].manifestnode()].read()
skip = set(mfdict.items())
else:
skip = set()
# Copy the skip set to start large and avoid constant resizing,
# and since it's likely to be very similar to the prefetch set.
files = skip.copy()
serverfiles = skip.copy()
visited = set()
visited.add(nullrev)
revcount = len(revs)
progress = self.ui.makeprogress(_(b'prefetching'), total=revcount)
progress.update(0)
for rev in sorted(revs):
ctx = repo[rev]
if pats:
m = scmutil.match(ctx, pats, opts)
sparsematch = repo.maybesparsematch(rev)
mfnode = ctx.manifestnode()
mfrev = mfrevlog.rev(mfnode)
# Decompressing manifests is expensive.
# When possible, only read the deltas.
mfdict = mfl[mfnode].read_any_fast_delta(visited)[1]
diff = mfdict.items()
if pats:
diff = (pf for pf in diff if m(pf[0]))
if sparsematch:
diff = (pf for pf in diff if sparsematch(pf[0]))
if rev not in localrevs:
serverfiles.update(diff)
else:
files.update(diff)
visited.add(mfrev)
progress.increment()
files.difference_update(skip)
serverfiles.difference_update(skip)
progress.complete()
# Fetch files known to be on the server
if serverfiles:
results = [(path, hex(fnode)) for (path, fnode) in serverfiles]
repo.fileservice.prefetch(results, force=True)
# Fetch files that may or may not be on the server
if files:
results = [(path, hex(fnode)) for (path, fnode) in files]
repo.fileservice.prefetch(results)
def close(self):
super().close()
self.connectionpool.close()
repo.__class__ = shallowrepository
repo.shallowmatch = match.always()
makeunionstores(repo)
repo.includepattern = repo.ui.configlist(
b"remotefilelog", b"includepattern", None
)
repo.excludepattern = repo.ui.configlist(
b"remotefilelog", b"excludepattern", None
)
if not hasattr(repo, 'connectionpool'):
repo.connectionpool = connectionpool.connectionpool(repo)
if repo.includepattern or repo.excludepattern:
repo.shallowmatch = match.match(
repo.root, b'', None, repo.includepattern, repo.excludepattern
)
|