| 1 |
# remotefilectx.py - filectx/workingfilectx implementations for remotefilelog |
| 2 |
# |
| 3 |
# Copyright 2013 Facebook, Inc. |
| 4 |
# |
| 5 |
# This software may be used and distributed according to the terms of the |
| 6 |
# GNU General Public License version 2 or any later version. |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import collections |
| 11 |
import time |
| 12 |
|
| 13 |
from typing import ( |
| 14 |
Iterator, |
| 15 |
Protocol, |
| 16 |
) |
| 17 |
|
| 18 |
from mercurial.node import bin, hex, nullrev |
| 19 |
from mercurial import ( |
| 20 |
context, |
| 21 |
error, |
| 22 |
phases, |
| 23 |
util, |
| 24 |
) |
| 25 |
from mercurial.interfaces import ( |
| 26 |
repository, |
| 27 |
) |
| 28 |
from . import ( |
| 29 |
fileserverclient, |
| 30 |
shallowutil, |
| 31 |
) |
| 32 |
|
| 33 |
propertycache = util.propertycache |
| 34 |
FASTLOG_TIMEOUT_IN_SECS = 0.5 |
| 35 |
|
| 36 |
|
| 37 |
class RFLRepo(repository.IRepo, Protocol): |
| 38 |
fileservice: fileserverclient.fileserverclient |
| 39 |
|
| 40 |
|
| 41 |
class remotefilectx(context.filectx): |
| 42 |
_repo: RFLRepo |
| 43 |
|
| 44 |
def __init__( |
| 45 |
self, |
| 46 |
repo, |
| 47 |
path, |
| 48 |
changeid=None, |
| 49 |
fileid=None, |
| 50 |
filelog=None, |
| 51 |
changectx=None, |
| 52 |
ancestormap=None, |
| 53 |
): |
| 54 |
if fileid == nullrev: |
| 55 |
fileid = repo.nullid |
| 56 |
if fileid and len(fileid) == 40: |
| 57 |
fileid = bin(fileid) |
| 58 |
super().__init__(repo, path, changeid, fileid, filelog, changectx) |
| 59 |
self._ancestormap = ancestormap |
| 60 |
|
| 61 |
def size(self): |
| 62 |
return self._filelog.size(self._filenode) |
| 63 |
|
| 64 |
@propertycache |
| 65 |
def _changeid(self): |
| 66 |
if '_changeid' in self.__dict__: |
| 67 |
return self._changeid |
| 68 |
elif '_changectx' in self.__dict__: |
| 69 |
return self._changectx.rev() |
| 70 |
elif '_descendantrev' in self.__dict__: |
| 71 |
# this file context was created from a revision with a known |
| 72 |
# descendant, we can (lazily) correct for linkrev aliases |
| 73 |
linknode = self._adjustlinknode( |
| 74 |
self._path, self._filelog, self._filenode, self._descendantrev |
| 75 |
) |
| 76 |
return self._repo.unfiltered().changelog.rev(linknode) |
| 77 |
else: |
| 78 |
return self.linkrev() |
| 79 |
|
| 80 |
def filectx(self, fileid, changeid=None): |
| 81 |
"""opens an arbitrary revision of the file without |
| 82 |
opening a new filelog""" |
| 83 |
return remotefilectx( |
| 84 |
self._repo, |
| 85 |
self._path, |
| 86 |
fileid=fileid, |
| 87 |
filelog=self._filelog, |
| 88 |
changeid=changeid, |
| 89 |
) |
| 90 |
|
| 91 |
def linkrev(self): |
| 92 |
return self._linkrev |
| 93 |
|
| 94 |
@propertycache |
| 95 |
def _linkrev(self): |
| 96 |
if self._filenode == self._repo.nullid: |
| 97 |
return nullrev |
| 98 |
|
| 99 |
ancestormap = self.ancestormap() |
| 100 |
p1, p2, linknode, copyfrom = ancestormap[self._filenode] |
| 101 |
rev = self._repo.changelog.index.get_rev(linknode) |
| 102 |
if rev is not None: |
| 103 |
return rev |
| 104 |
|
| 105 |
# Search all commits for the appropriate linkrev (slow, but uncommon) |
| 106 |
path = self._path |
| 107 |
fileid = self._filenode |
| 108 |
cl = self._repo.unfiltered().changelog |
| 109 |
mfl = self._repo.manifestlog |
| 110 |
|
| 111 |
for rev in range(len(cl) - 1, 0, -1): |
| 112 |
node = cl.node(rev) |
| 113 |
data = cl.read( |
| 114 |
node |
| 115 |
) # get changeset data (we avoid object creation) |
| 116 |
if path in data[3]: # checking the 'files' field. |
| 117 |
# The file has been touched, check if the hash is what we're |
| 118 |
# looking for. |
| 119 |
# |
| 120 |
# The change has to be against a parent, otherwise we might be |
| 121 |
# missing linkrev worthy changes. |
| 122 |
m = mfl[data[0]].read_delta_parents(exact=False) |
| 123 |
if fileid == m.get(path): |
| 124 |
return rev |
| 125 |
|
| 126 |
# Couldn't find the linkrev. This should generally not happen, and will |
| 127 |
# likely cause a crash. |
| 128 |
return None |
| 129 |
|
| 130 |
def introrev(self): |
| 131 |
"""return the rev of the changeset which introduced this file revision |
| 132 |
|
| 133 |
This method is different from linkrev because it take into account the |
| 134 |
changeset the filectx was created from. It ensures the returned |
| 135 |
revision is one of its ancestors. This prevents bugs from |
| 136 |
'linkrev-shadowing' when a file revision is used by multiple |
| 137 |
changesets. |
| 138 |
""" |
| 139 |
lkr = self.linkrev() |
| 140 |
attrs = vars(self) |
| 141 |
noctx = not ('_changeid' in attrs or r'_changectx' in attrs) |
| 142 |
if noctx or self.rev() == lkr: |
| 143 |
return lkr |
| 144 |
linknode = self._adjustlinknode( |
| 145 |
self._path, |
| 146 |
self._filelog, |
| 147 |
self._filenode, |
| 148 |
self.rev(), |
| 149 |
inclusive=True, |
| 150 |
) |
| 151 |
return self._repo.changelog.rev(linknode) |
| 152 |
|
| 153 |
def renamed(self): |
| 154 |
"""check if file was actually renamed in this changeset revision |
| 155 |
|
| 156 |
If rename logged in file revision, we report copy for changeset only |
| 157 |
if file revisions linkrev points back to the changeset in question |
| 158 |
or both changeset parents contain different file revisions. |
| 159 |
""" |
| 160 |
ancestormap = self.ancestormap() |
| 161 |
|
| 162 |
p1, p2, linknode, copyfrom = ancestormap[self._filenode] |
| 163 |
if not copyfrom: |
| 164 |
return None |
| 165 |
|
| 166 |
renamed = (copyfrom, p1) |
| 167 |
if self.rev() == self.linkrev(): |
| 168 |
return renamed |
| 169 |
|
| 170 |
name = self.path() |
| 171 |
fnode = self._filenode |
| 172 |
for p in self._changectx.parents(): |
| 173 |
try: |
| 174 |
if fnode == p.filenode(name): |
| 175 |
return None |
| 176 |
except error.LookupError: |
| 177 |
pass |
| 178 |
return renamed |
| 179 |
|
| 180 |
def copysource(self): |
| 181 |
copy = self.renamed() |
| 182 |
return copy and copy[0] |
| 183 |
|
| 184 |
def ancestormap(self): |
| 185 |
if not self._ancestormap: |
| 186 |
self._ancestormap = self.filelog().ancestormap(self._filenode) |
| 187 |
|
| 188 |
return self._ancestormap |
| 189 |
|
| 190 |
def parents(self): |
| 191 |
repo = self._repo |
| 192 |
ancestormap = self.ancestormap() |
| 193 |
|
| 194 |
p1, p2, linknode, copyfrom = ancestormap[self._filenode] |
| 195 |
results = [] |
| 196 |
if p1 != repo.nullid: |
| 197 |
path = copyfrom or self._path |
| 198 |
flog = repo.file(path) |
| 199 |
p1ctx = remotefilectx( |
| 200 |
repo, path, fileid=p1, filelog=flog, ancestormap=ancestormap |
| 201 |
) |
| 202 |
p1ctx._descendantrev = self.rev() |
| 203 |
results.append(p1ctx) |
| 204 |
|
| 205 |
if p2 != repo.nullid: |
| 206 |
path = self._path |
| 207 |
flog = repo.file(path) |
| 208 |
p2ctx = remotefilectx( |
| 209 |
repo, path, fileid=p2, filelog=flog, ancestormap=ancestormap |
| 210 |
) |
| 211 |
p2ctx._descendantrev = self.rev() |
| 212 |
results.append(p2ctx) |
| 213 |
|
| 214 |
return results |
| 215 |
|
| 216 |
def _nodefromancrev(self, ancrev, cl, mfl, path, fnode): |
| 217 |
"""returns the node for <path> in <ancrev> if content matches <fnode>""" |
| 218 |
ancctx = cl.read(ancrev) # This avoids object creation. |
| 219 |
manifestnode, files = ancctx[0], ancctx[3] |
| 220 |
# If the file was touched in this ancestor, and the content is similar |
| 221 |
# to the one we are searching for. |
| 222 |
if path in files: |
| 223 |
m = mfl[manifestnode].read_delta_parents(exact=False) |
| 224 |
if fnode == m.get(path): |
| 225 |
return cl.node(ancrev) |
| 226 |
return None |
| 227 |
|
| 228 |
def _adjustlinknode(self, path, filelog, fnode, srcrev, inclusive=False): |
| 229 |
"""return the first ancestor of <srcrev> introducing <fnode> |
| 230 |
|
| 231 |
If the linkrev of the file revision does not point to an ancestor of |
| 232 |
srcrev, we'll walk down the ancestors until we find one introducing |
| 233 |
this file revision. |
| 234 |
|
| 235 |
:repo: a localrepository object (used to access changelog and manifest) |
| 236 |
:path: the file path |
| 237 |
:fnode: the nodeid of the file revision |
| 238 |
:filelog: the filelog of this path |
| 239 |
:srcrev: the changeset revision we search ancestors from |
| 240 |
:inclusive: if true, the src revision will also be checked |
| 241 |
|
| 242 |
Note: This is based on adjustlinkrev in core, but it's quite different. |
| 243 |
|
| 244 |
adjustlinkrev depends on the fact that the linkrev is the bottom most |
| 245 |
node, and uses that as a stopping point for the ancestor traversal. We |
| 246 |
can't do that here because the linknode is not guaranteed to be the |
| 247 |
bottom most one. |
| 248 |
|
| 249 |
In our code here, we actually know what a bunch of potential ancestor |
| 250 |
linknodes are, so instead of stopping the cheap-ancestor-traversal when |
| 251 |
we get to a linkrev, we stop when we see any of the known linknodes. |
| 252 |
""" |
| 253 |
repo = self._repo |
| 254 |
cl = repo.unfiltered().changelog |
| 255 |
mfl = repo.manifestlog |
| 256 |
ancestormap = self.ancestormap() |
| 257 |
linknode = ancestormap[fnode][2] |
| 258 |
|
| 259 |
if srcrev is None: |
| 260 |
# wctx case, used by workingfilectx during mergecopy |
| 261 |
revs = [p.rev() for p in self._repo[None].parents()] |
| 262 |
inclusive = True # we skipped the real (revless) source |
| 263 |
else: |
| 264 |
revs = [srcrev] |
| 265 |
|
| 266 |
if self._verifylinknode(revs, linknode): |
| 267 |
return linknode |
| 268 |
|
| 269 |
commonlogkwargs = { |
| 270 |
'revs': b' '.join([hex(cl.node(rev)) for rev in revs]), |
| 271 |
'fnode': hex(fnode), |
| 272 |
'filepath': path, |
| 273 |
'user': shallowutil.getusername(repo.ui), |
| 274 |
'reponame': shallowutil.getreponame(repo.ui), |
| 275 |
} |
| 276 |
|
| 277 |
repo.ui.log(b'linkrevfixup', b'adjusting linknode\n', **commonlogkwargs) |
| 278 |
|
| 279 |
pc = repo._phasecache |
| 280 |
seenpublic = False |
| 281 |
iteranc = cl.ancestors(revs, inclusive=inclusive) |
| 282 |
for ancrev in iteranc: |
| 283 |
# First, check locally-available history. |
| 284 |
lnode = self._nodefromancrev(ancrev, cl, mfl, path, fnode) |
| 285 |
if lnode is not None: |
| 286 |
return lnode |
| 287 |
|
| 288 |
# adjusting linknode can be super-slow. To mitigate the issue |
| 289 |
# we use two heuristics: calling fastlog and forcing remotefilelog |
| 290 |
# prefetch |
| 291 |
if not seenpublic and pc.phase(repo, ancrev) == phases.public: |
| 292 |
# TODO: there used to be a codepath to fetch linknodes |
| 293 |
# from a server as a fast path, but it appeared to |
| 294 |
# depend on an API FB added to their phabricator. |
| 295 |
lnode = self._forceprefetch( |
| 296 |
repo, path, fnode, revs, commonlogkwargs |
| 297 |
) |
| 298 |
if lnode: |
| 299 |
return lnode |
| 300 |
seenpublic = True |
| 301 |
|
| 302 |
return linknode |
| 303 |
|
| 304 |
def _forceprefetch(self, repo, path, fnode, revs, commonlogkwargs): |
| 305 |
# This next part is super non-obvious, so big comment block time! |
| 306 |
# |
| 307 |
# It is possible to get extremely bad performance here when a fairly |
| 308 |
# common set of circumstances occur when this extension is combined |
| 309 |
# with a server-side commit rewriting extension like pushrebase. |
| 310 |
# |
| 311 |
# First, an engineer creates Commit A and pushes it to the server. |
| 312 |
# While the server's data structure will have the correct linkrev |
| 313 |
# for the files touched in Commit A, the client will have the |
| 314 |
# linkrev of the local commit, which is "invalid" because it's not |
| 315 |
# an ancestor of the main line of development. |
| 316 |
# |
| 317 |
# The client will never download the remotefilelog with the correct |
| 318 |
# linkrev as long as nobody else touches that file, since the file |
| 319 |
# data and history hasn't changed since Commit A. |
| 320 |
# |
| 321 |
# After a long time (or a short time in a heavily used repo), if the |
| 322 |
# same engineer returns to change the same file, some commands -- |
| 323 |
# such as amends of commits with file moves, logs, diffs, etc -- |
| 324 |
# can trigger this _adjustlinknode code. In those cases, finding |
| 325 |
# the correct rev can become quite expensive, as the correct |
| 326 |
# revision is far back in history and we need to walk back through |
| 327 |
# history to find it. |
| 328 |
# |
| 329 |
# In order to improve this situation, we force a prefetch of the |
| 330 |
# remotefilelog data blob for the file we were called on. We do this |
| 331 |
# at most once, when we first see a public commit in the history we |
| 332 |
# are traversing. |
| 333 |
# |
| 334 |
# Forcing the prefetch means we will download the remote blob even |
| 335 |
# if we have the "correct" blob in the local store. Since the union |
| 336 |
# store checks the remote store first, this means we are much more |
| 337 |
# likely to get the correct linkrev at this point. |
| 338 |
# |
| 339 |
# In rare circumstances (such as the server having a suboptimal |
| 340 |
# linkrev for our use case), we will fall back to the old slow path. |
| 341 |
# |
| 342 |
# We may want to add additional heuristics here in the future if |
| 343 |
# the slow path is used too much. One promising possibility is using |
| 344 |
# obsolescence markers to find a more-likely-correct linkrev. |
| 345 |
|
| 346 |
logmsg = b'' |
| 347 |
start = time.time() |
| 348 |
try: |
| 349 |
repo.fileservice.prefetch([(path, hex(fnode))], force=True) |
| 350 |
|
| 351 |
# Now that we've downloaded a new blob from the server, |
| 352 |
# we need to rebuild the ancestor map to recompute the |
| 353 |
# linknodes. |
| 354 |
self._ancestormap = None |
| 355 |
linknode = self.ancestormap()[fnode][2] # 2 is linknode |
| 356 |
if self._verifylinknode(revs, linknode): |
| 357 |
logmsg = b'remotefilelog prefetching succeeded' |
| 358 |
return linknode |
| 359 |
logmsg = b'remotefilelog prefetching not found' |
| 360 |
return None |
| 361 |
except Exception as e: |
| 362 |
logmsg = b'remotefilelog prefetching failed (%s)' % e |
| 363 |
return None |
| 364 |
finally: |
| 365 |
elapsed = time.time() - start |
| 366 |
repo.ui.log( |
| 367 |
b'linkrevfixup', |
| 368 |
logmsg + b'\n', |
| 369 |
elapsed=elapsed * 1000, |
| 370 |
**commonlogkwargs, |
| 371 |
) |
| 372 |
|
| 373 |
def _verifylinknode(self, revs, linknode): |
| 374 |
""" |
| 375 |
Check if a linknode is correct one for the current history. |
| 376 |
|
| 377 |
That is, return True if the linkrev is the ancestor of any of the |
| 378 |
passed in revs, otherwise return False. |
| 379 |
|
| 380 |
`revs` is a list that usually has one element -- usually the wdir parent |
| 381 |
or the user-passed rev we're looking back from. It may contain two revs |
| 382 |
when there is a merge going on, or zero revs when a root node with no |
| 383 |
parents is being created. |
| 384 |
""" |
| 385 |
if not revs: |
| 386 |
return False |
| 387 |
try: |
| 388 |
# Use the C fastpath to check if the given linknode is correct. |
| 389 |
cl = self._repo.unfiltered().changelog |
| 390 |
return any(cl.isancestor(linknode, cl.node(r)) for r in revs) |
| 391 |
except error.LookupError: |
| 392 |
# The linknode read from the blob may have been stripped or |
| 393 |
# otherwise not present in the repository anymore. Do not fail hard |
| 394 |
# in this case. Instead, return false and continue the search for |
| 395 |
# the correct linknode. |
| 396 |
return False |
| 397 |
|
| 398 |
def ancestors(self, followfirst=False) -> Iterator[remotefilectx]: |
| 399 |
ancestors = [] |
| 400 |
queue = collections.deque((self,)) |
| 401 |
seen = set() |
| 402 |
while queue: |
| 403 |
current = queue.pop() |
| 404 |
if current.filenode() in seen: |
| 405 |
continue |
| 406 |
seen.add(current.filenode()) |
| 407 |
|
| 408 |
ancestors.append(current) |
| 409 |
|
| 410 |
parents = current.parents() |
| 411 |
first = True |
| 412 |
for p in parents: |
| 413 |
if first or not followfirst: |
| 414 |
queue.append(p) |
| 415 |
first = False |
| 416 |
|
| 417 |
# Remove self |
| 418 |
ancestors.pop(0) |
| 419 |
|
| 420 |
# Sort by linkrev |
| 421 |
# The copy tracing algorithm depends on these coming out in order |
| 422 |
ancestors = sorted(ancestors, reverse=True, key=lambda x: x.linkrev()) |
| 423 |
|
| 424 |
yield from ancestors |
| 425 |
|
| 426 |
def ancestor(self, fc2, actx): |
| 427 |
# the easy case: no (relevant) renames |
| 428 |
if fc2.path() == self.path() and self.path() in actx: |
| 429 |
return actx[self.path()] |
| 430 |
|
| 431 |
# the next easiest cases: unambiguous predecessor (name trumps |
| 432 |
# history) |
| 433 |
if self.path() in actx and fc2.path() not in actx: |
| 434 |
return actx[self.path()] |
| 435 |
if fc2.path() in actx and self.path() not in actx: |
| 436 |
return actx[fc2.path()] |
| 437 |
|
| 438 |
# the code handling this was "dead" since by over 10 years and was |
| 439 |
# deleted. Check history if you need this to work again. |
| 440 |
raise NotImplementedError |
| 441 |
|
| 442 |
def annotate(self, *args, **kwargs): |
| 443 |
introctx = self |
| 444 |
prefetchskip = kwargs.pop('prefetchskip', None) |
| 445 |
if prefetchskip: |
| 446 |
# use introrev so prefetchskip can be accurately tested |
| 447 |
introrev = self.introrev() |
| 448 |
if self.rev() != introrev: |
| 449 |
introctx = remotefilectx( |
| 450 |
self._repo, |
| 451 |
self._path, |
| 452 |
changeid=introrev, |
| 453 |
fileid=self._filenode, |
| 454 |
filelog=self._filelog, |
| 455 |
ancestormap=self._ancestormap, |
| 456 |
) |
| 457 |
|
| 458 |
# like self.ancestors, but append to "fetch" and skip visiting parents |
| 459 |
# of nodes in "prefetchskip". |
| 460 |
fetch = [] |
| 461 |
seen = set() |
| 462 |
queue = collections.deque((introctx,)) |
| 463 |
seen.add(introctx.node()) |
| 464 |
while queue: |
| 465 |
current = queue.pop() |
| 466 |
if current.filenode() != self.filenode(): |
| 467 |
# this is a "joint point". fastannotate needs contents of |
| 468 |
# "joint point"s to calculate diffs for side branches. |
| 469 |
fetch.append((current.path(), hex(current.filenode()))) |
| 470 |
if prefetchskip and current in prefetchskip: |
| 471 |
continue |
| 472 |
for parent in current.parents(): |
| 473 |
if parent.node() not in seen: |
| 474 |
seen.add(parent.node()) |
| 475 |
queue.append(parent) |
| 476 |
|
| 477 |
self._repo.ui.debug( |
| 478 |
b'remotefilelog: prefetching %d files ' |
| 479 |
b'for annotate\n' % len(fetch) |
| 480 |
) |
| 481 |
if fetch: |
| 482 |
self._repo.fileservice.prefetch(fetch) |
| 483 |
return super().annotate(*args, **kwargs) |
| 484 |
|
| 485 |
# Return empty set so that the hg serve and thg don't stack trace |
| 486 |
def children(self): |
| 487 |
return [] |
| 488 |
|
| 489 |
|
| 490 |
class remoteworkingfilectx(context.workingfilectx, remotefilectx): |
| 491 |
def __init__(self, repo, path, filelog=None, workingctx=None): |
| 492 |
self._ancestormap = None |
| 493 |
super().__init__(repo, path, filelog, workingctx) |
| 494 |
|
| 495 |
def parents(self): |
| 496 |
return remotefilectx.parents(self) |
| 497 |
|
| 498 |
def ancestormap(self): |
| 499 |
if not self._ancestormap: |
| 500 |
path = self._path |
| 501 |
pcl = self._changectx._parents |
| 502 |
renamed = self.renamed() |
| 503 |
|
| 504 |
if renamed: |
| 505 |
p1 = renamed |
| 506 |
else: |
| 507 |
p1 = (path, pcl[0]._manifest.get(path, self._repo.nullid)) |
| 508 |
|
| 509 |
p2 = (path, self._repo.nullid) |
| 510 |
if len(pcl) > 1: |
| 511 |
p2 = (path, pcl[1]._manifest.get(path, self._repo.nullid)) |
| 512 |
|
| 513 |
m = {} |
| 514 |
if p1[1] != self._repo.nullid: |
| 515 |
p1ctx = self._repo.filectx(p1[0], fileid=p1[1]) |
| 516 |
m.update(p1ctx.filelog().ancestormap(p1[1])) |
| 517 |
|
| 518 |
if p2[1] != self._repo.nullid: |
| 519 |
p2ctx = self._repo.filectx(p2[0], fileid=p2[1]) |
| 520 |
m.update(p2ctx.filelog().ancestormap(p2[1])) |
| 521 |
|
| 522 |
copyfrom = b'' |
| 523 |
if renamed: |
| 524 |
copyfrom = renamed[0] |
| 525 |
m[None] = (p1[1], p2[1], self._repo.nullid, copyfrom) |
| 526 |
self._ancestormap = m |
| 527 |
|
| 528 |
return self._ancestormap |