| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import threading |
| 4 |
|
| 5 |
from mercurial.node import ( |
| 6 |
hex, |
| 7 |
sha1nodeconstants, |
| 8 |
) |
| 9 |
from mercurial import ( |
| 10 |
mdiff, |
| 11 |
revlog, |
| 12 |
) |
| 13 |
from mercurial.revlogutils import ( |
| 14 |
constants as revlog_constants, |
| 15 |
) |
| 16 |
from . import ( |
| 17 |
basestore, |
| 18 |
constants, |
| 19 |
shallowutil, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
class ChainIndicies: |
| 24 |
"""A static class for easy reference to the delta chain indicies.""" |
| 25 |
|
| 26 |
# The filename of this revision delta |
| 27 |
NAME = 0 |
| 28 |
# The mercurial file node for this revision delta |
| 29 |
NODE = 1 |
| 30 |
# The filename of the delta base's revision. This is useful when delta |
| 31 |
# between different files (like in the case of a move or copy, we can delta |
| 32 |
# against the original file content). |
| 33 |
BASENAME = 2 |
| 34 |
# The mercurial file node for the delta base revision. This is the nullid if |
| 35 |
# this delta is a full text. |
| 36 |
BASENODE = 3 |
| 37 |
# The actual delta or full text data. |
| 38 |
DATA = 4 |
| 39 |
|
| 40 |
|
| 41 |
class unioncontentstore(basestore.baseunionstore): |
| 42 |
def __init__(self, *stores, **kwargs): |
| 43 |
super().__init__(*stores, **kwargs) |
| 44 |
|
| 45 |
self.writestore = kwargs.get('writestore') |
| 46 |
|
| 47 |
# If allowincomplete==True then the union store can return partial |
| 48 |
# delta chains, otherwise it will throw a KeyError if a full |
| 49 |
# deltachain can't be found. |
| 50 |
self.allowincomplete = kwargs.get('allowincomplete', False) |
| 51 |
|
| 52 |
def get(self, name, node): |
| 53 |
"""Fetches the full text revision contents of the given name+node pair. |
| 54 |
If the full text doesn't exist, throws a KeyError. |
| 55 |
|
| 56 |
Under the hood, this uses getdeltachain() across all the stores to build |
| 57 |
up a full chain to produce the full text. |
| 58 |
""" |
| 59 |
chain = self.getdeltachain(name, node) |
| 60 |
|
| 61 |
if chain[-1][ChainIndicies.BASENODE] != sha1nodeconstants.nullid: |
| 62 |
# If we didn't receive a full chain, throw |
| 63 |
raise KeyError((name, hex(node))) |
| 64 |
|
| 65 |
# The last entry in the chain is a full text, so we start our delta |
| 66 |
# applies with that. |
| 67 |
fulltext = chain.pop()[ChainIndicies.DATA] |
| 68 |
|
| 69 |
text = fulltext |
| 70 |
while chain: |
| 71 |
delta = chain.pop()[ChainIndicies.DATA] |
| 72 |
text = mdiff.patches(text, [delta]) |
| 73 |
|
| 74 |
return text |
| 75 |
|
| 76 |
@basestore.baseunionstore.retriable |
| 77 |
def getdelta(self, name, node): |
| 78 |
"""Return the single delta entry for the given name/node pair.""" |
| 79 |
for store in self.stores: |
| 80 |
try: |
| 81 |
return store.getdelta(name, node) |
| 82 |
except KeyError: |
| 83 |
pass |
| 84 |
|
| 85 |
raise KeyError((name, hex(node))) |
| 86 |
|
| 87 |
def getdeltachain(self, name, node): |
| 88 |
"""Returns the deltachain for the given name/node pair. |
| 89 |
|
| 90 |
Returns an ordered list of: |
| 91 |
|
| 92 |
[(name, node, deltabasename, deltabasenode, deltacontent),...] |
| 93 |
|
| 94 |
where the chain is terminated by a full text entry with a nullid |
| 95 |
deltabasenode. |
| 96 |
""" |
| 97 |
chain = self._getpartialchain(name, node) |
| 98 |
while chain[-1][ChainIndicies.BASENODE] != sha1nodeconstants.nullid: |
| 99 |
x, x, deltabasename, deltabasenode, x = chain[-1] |
| 100 |
try: |
| 101 |
morechain = self._getpartialchain(deltabasename, deltabasenode) |
| 102 |
chain.extend(morechain) |
| 103 |
except KeyError: |
| 104 |
# If we allow incomplete chains, don't throw. |
| 105 |
if not self.allowincomplete: |
| 106 |
raise |
| 107 |
break |
| 108 |
|
| 109 |
return chain |
| 110 |
|
| 111 |
@basestore.baseunionstore.retriable |
| 112 |
def getmeta(self, name, node): |
| 113 |
"""Returns the metadata dict for given node.""" |
| 114 |
for store in self.stores: |
| 115 |
try: |
| 116 |
return store.getmeta(name, node) |
| 117 |
except KeyError: |
| 118 |
pass |
| 119 |
raise KeyError((name, hex(node))) |
| 120 |
|
| 121 |
def getmetrics(self): |
| 122 |
metrics = [s.getmetrics() for s in self.stores] |
| 123 |
return shallowutil.sumdicts(*metrics) |
| 124 |
|
| 125 |
@basestore.baseunionstore.retriable |
| 126 |
def _getpartialchain(self, name, node): |
| 127 |
"""Returns a partial delta chain for the given name/node pair. |
| 128 |
|
| 129 |
A partial chain is a chain that may not be terminated in a full-text. |
| 130 |
""" |
| 131 |
for store in self.stores: |
| 132 |
try: |
| 133 |
return store.getdeltachain(name, node) |
| 134 |
except KeyError: |
| 135 |
pass |
| 136 |
|
| 137 |
raise KeyError((name, hex(node))) |
| 138 |
|
| 139 |
def add(self, name, node, data): |
| 140 |
raise RuntimeError( |
| 141 |
b"cannot add content only to remotefilelog contentstore" |
| 142 |
) |
| 143 |
|
| 144 |
def getmissing(self, keys): |
| 145 |
missing = keys |
| 146 |
for store in self.stores: |
| 147 |
if missing: |
| 148 |
missing = store.getmissing(missing) |
| 149 |
return missing |
| 150 |
|
| 151 |
def addremotefilelognode(self, name, node, data): |
| 152 |
if self.writestore: |
| 153 |
self.writestore.addremotefilelognode(name, node, data) |
| 154 |
else: |
| 155 |
raise RuntimeError(b"no writable store configured") |
| 156 |
|
| 157 |
def markledger(self, ledger, options=None): |
| 158 |
for store in self.stores: |
| 159 |
store.markledger(ledger, options) |
| 160 |
|
| 161 |
|
| 162 |
class remotefilelogcontentstore(basestore.basestore): |
| 163 |
def __init__(self, *args, **kwargs): |
| 164 |
super().__init__(*args, **kwargs) |
| 165 |
self._threaddata = threading.local() |
| 166 |
|
| 167 |
def get(self, name, node): |
| 168 |
# return raw revision text |
| 169 |
data = self._getdata(name, node) |
| 170 |
|
| 171 |
offset, size, flags = shallowutil.parsesizeflags(data) |
| 172 |
content = data[offset : offset + size] |
| 173 |
|
| 174 |
ancestormap = shallowutil.ancestormap(data) |
| 175 |
p1, p2, linknode, copyfrom = ancestormap[node] |
| 176 |
copyrev = None |
| 177 |
if copyfrom: |
| 178 |
copyrev = hex(p1) |
| 179 |
|
| 180 |
self._updatemetacache(node, size, flags) |
| 181 |
|
| 182 |
# lfs tracks renames in its own metadata, remove hg copy metadata, |
| 183 |
# because copy metadata will be re-added by lfs flag processor. |
| 184 |
if flags & revlog.REVIDX_EXTSTORED: |
| 185 |
copyrev = copyfrom = None |
| 186 |
revision = shallowutil.createrevlogtext(content, copyfrom, copyrev) |
| 187 |
return revision |
| 188 |
|
| 189 |
def getdelta(self, name, node): |
| 190 |
# Since remotefilelog content stores only contain full texts, just |
| 191 |
# return that. |
| 192 |
revision = self.get(name, node) |
| 193 |
return ( |
| 194 |
revision, |
| 195 |
name, |
| 196 |
sha1nodeconstants.nullid, |
| 197 |
self.getmeta(name, node), |
| 198 |
) |
| 199 |
|
| 200 |
def getdeltachain(self, name, node): |
| 201 |
# Since remotefilelog content stores just contain full texts, we return |
| 202 |
# a fake delta chain that just consists of a single full text revision. |
| 203 |
# The nullid in the deltabasenode slot indicates that the revision is a |
| 204 |
# fulltext. |
| 205 |
revision = self.get(name, node) |
| 206 |
return [(name, node, None, sha1nodeconstants.nullid, revision)] |
| 207 |
|
| 208 |
def getmeta(self, name, node): |
| 209 |
self._sanitizemetacache() |
| 210 |
if node != self._threaddata.metacache[0]: |
| 211 |
data = self._getdata(name, node) |
| 212 |
offset, size, flags = shallowutil.parsesizeflags(data) |
| 213 |
self._updatemetacache(node, size, flags) |
| 214 |
return self._threaddata.metacache[1] |
| 215 |
|
| 216 |
def add(self, name, node, data): |
| 217 |
raise RuntimeError( |
| 218 |
b"cannot add content only to remotefilelog contentstore" |
| 219 |
) |
| 220 |
|
| 221 |
def _sanitizemetacache(self): |
| 222 |
metacache = getattr(self._threaddata, 'metacache', None) |
| 223 |
if metacache is None: |
| 224 |
self._threaddata.metacache = (None, None) # (node, meta) |
| 225 |
|
| 226 |
def _updatemetacache(self, node, size, flags): |
| 227 |
self._sanitizemetacache() |
| 228 |
if node == self._threaddata.metacache[0]: |
| 229 |
return |
| 230 |
meta = {constants.METAKEYFLAG: flags, constants.METAKEYSIZE: size} |
| 231 |
self._threaddata.metacache = (node, meta) |
| 232 |
|
| 233 |
|
| 234 |
class remotecontentstore: |
| 235 |
def __init__(self, ui, fileservice, shared): |
| 236 |
self._fileservice = fileservice |
| 237 |
# type(shared) is usually remotefilelogcontentstore |
| 238 |
self._shared = shared |
| 239 |
|
| 240 |
def get(self, name, node): |
| 241 |
self._fileservice.prefetch( |
| 242 |
[(name, hex(node))], force=True, fetchdata=True |
| 243 |
) |
| 244 |
return self._shared.get(name, node) |
| 245 |
|
| 246 |
def getdelta(self, name, node): |
| 247 |
revision = self.get(name, node) |
| 248 |
return ( |
| 249 |
revision, |
| 250 |
name, |
| 251 |
sha1nodeconstants.nullid, |
| 252 |
self._shared.getmeta(name, node), |
| 253 |
) |
| 254 |
|
| 255 |
def getdeltachain(self, name, node): |
| 256 |
# Since our remote content stores just contain full texts, we return a |
| 257 |
# fake delta chain that just consists of a single full text revision. |
| 258 |
# The nullid in the deltabasenode slot indicates that the revision is a |
| 259 |
# fulltext. |
| 260 |
revision = self.get(name, node) |
| 261 |
return [(name, node, None, sha1nodeconstants.nullid, revision)] |
| 262 |
|
| 263 |
def getmeta(self, name, node): |
| 264 |
self._fileservice.prefetch( |
| 265 |
[(name, hex(node))], force=True, fetchdata=True |
| 266 |
) |
| 267 |
return self._shared.getmeta(name, node) |
| 268 |
|
| 269 |
def add(self, name, node, data): |
| 270 |
raise RuntimeError(b"cannot add to a remote store") |
| 271 |
|
| 272 |
def getmissing(self, keys): |
| 273 |
return keys |
| 274 |
|
| 275 |
def markledger(self, ledger, options=None): |
| 276 |
pass |
| 277 |
|
| 278 |
|
| 279 |
class manifestrevlogstore: |
| 280 |
_repackendlinkrev: int |
| 281 |
|
| 282 |
def __init__(self, repo): |
| 283 |
self._store = repo.store |
| 284 |
self._svfs = repo.svfs |
| 285 |
self._revlogs = dict() |
| 286 |
self._cl = repo.changelog |
| 287 |
self._repackstartlinkrev = 0 |
| 288 |
|
| 289 |
def get(self, name, node): |
| 290 |
return self._revlog(name).rawdata(node) |
| 291 |
|
| 292 |
def getdelta(self, name, node): |
| 293 |
revision = self.get(name, node) |
| 294 |
return revision, name, self._cl.nullid, self.getmeta(name, node) |
| 295 |
|
| 296 |
def getdeltachain(self, name, node): |
| 297 |
revision = self.get(name, node) |
| 298 |
return [(name, node, None, self._cl.nullid, revision)] |
| 299 |
|
| 300 |
def getmeta(self, name, node): |
| 301 |
rl = self._revlog(name) |
| 302 |
rev = rl.rev(node) |
| 303 |
return { |
| 304 |
constants.METAKEYFLAG: rl.flags(rev), |
| 305 |
constants.METAKEYSIZE: rl.rawsize(rev), |
| 306 |
} |
| 307 |
|
| 308 |
def getancestors(self, name, node, known=None): |
| 309 |
if known is None: |
| 310 |
known = set() |
| 311 |
if node in known: |
| 312 |
return [] |
| 313 |
|
| 314 |
rl = self._revlog(name) |
| 315 |
ancestors = {} |
| 316 |
missing = {node} |
| 317 |
for ancrev in rl.ancestors([rl.rev(node)], inclusive=True): |
| 318 |
ancnode = rl.node(ancrev) |
| 319 |
missing.discard(ancnode) |
| 320 |
|
| 321 |
p1, p2 = rl.parents(ancnode) |
| 322 |
if p1 != self._cl.nullid and p1 not in known: |
| 323 |
missing.add(p1) |
| 324 |
if p2 != self._cl.nullid and p2 not in known: |
| 325 |
missing.add(p2) |
| 326 |
|
| 327 |
linknode = self._cl.node(rl.linkrev(ancrev)) |
| 328 |
ancestors[rl.node(ancrev)] = (p1, p2, linknode, b'') |
| 329 |
if not missing: |
| 330 |
break |
| 331 |
return ancestors |
| 332 |
|
| 333 |
def getnodeinfo(self, name, node): |
| 334 |
cl = self._cl |
| 335 |
rl = self._revlog(name) |
| 336 |
parents = rl.parents(node) |
| 337 |
linkrev = rl.linkrev(rl.rev(node)) |
| 338 |
return (parents[0], parents[1], cl.node(linkrev), None) |
| 339 |
|
| 340 |
def add(self, *args): |
| 341 |
raise RuntimeError(b"cannot add to a revlog store") |
| 342 |
|
| 343 |
def _revlog(self, name) -> revlog.revlog: |
| 344 |
rl = self._revlogs.get(name) |
| 345 |
if rl is None: |
| 346 |
revlogname = b'00manifesttree' |
| 347 |
if name != b'': |
| 348 |
revlogname = b'meta/%s/00manifest' % name |
| 349 |
# XXX building your own revlog is bound to troubles |
| 350 |
rl = revlog.revlog( |
| 351 |
self._svfs, |
| 352 |
target=(revlog_constants.KIND_MANIFESTLOG, name), |
| 353 |
radix=revlogname, |
| 354 |
) |
| 355 |
self._revlogs[name] = rl |
| 356 |
return rl |
| 357 |
|
| 358 |
def getmissing(self, keys): |
| 359 |
missing = [] |
| 360 |
for name, node in keys: |
| 361 |
mfrevlog = self._revlog(name) |
| 362 |
if not mfrevlog.index.has_node(node): |
| 363 |
missing.append((name, node)) |
| 364 |
|
| 365 |
return missing |
| 366 |
|
| 367 |
def setrepacklinkrevrange(self, startrev, endrev): |
| 368 |
self._repackstartlinkrev = startrev |
| 369 |
self._repackendlinkrev = endrev |
| 370 |
|
| 371 |
def markledger(self, ledger, options=None): |
| 372 |
if options and options.get(constants.OPTION_PACKSONLY): |
| 373 |
return |
| 374 |
treename = b'' |
| 375 |
rl = revlog.revlog( |
| 376 |
self._svfs, |
| 377 |
target=(revlog_constants.KIND_MANIFESTLOG, treename), |
| 378 |
radix=b'00manifesttree', |
| 379 |
) |
| 380 |
startlinkrev = self._repackstartlinkrev |
| 381 |
endlinkrev = self._repackendlinkrev |
| 382 |
for rev in range(len(rl) - 1, -1, -1): |
| 383 |
linkrev = rl.linkrev(rev) |
| 384 |
if linkrev < startlinkrev: |
| 385 |
break |
| 386 |
if linkrev > endlinkrev: |
| 387 |
continue |
| 388 |
node = rl.node(rev) |
| 389 |
ledger.markdataentry(self, treename, node) |
| 390 |
ledger.markhistoryentry(self, treename, node) |
| 391 |
|
| 392 |
for t, path, size in self._store.data_entries(): |
| 393 |
if path[:5] != b'meta/' or path[-2:] != b'.i': |
| 394 |
continue |
| 395 |
|
| 396 |
treename = path[5 : -len(b'/00manifest')] |
| 397 |
|
| 398 |
rl = revlog.revlog( |
| 399 |
self._svfs, |
| 400 |
target=(revlog_constants.KIND_MANIFESTLOG, treename), |
| 401 |
radix=path[:-2], |
| 402 |
) |
| 403 |
for rev in range(len(rl) - 1, -1, -1): |
| 404 |
linkrev = rl.linkrev(rev) |
| 405 |
if linkrev < startlinkrev: |
| 406 |
break |
| 407 |
if linkrev > endlinkrev: |
| 408 |
continue |
| 409 |
node = rl.node(rev) |
| 410 |
ledger.markdataentry(self, treename, node) |
| 411 |
ledger.markhistoryentry(self, treename, node) |
| 412 |
|
| 413 |
def cleanup(self, ledger): |
| 414 |
pass |