| 1 |
# narrowbundle2.py - bundle2 extensions for narrow repository support |
| 2 |
# |
| 3 |
# Copyright 2017 Google, 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 struct |
| 11 |
|
| 12 |
from mercurial.i18n import _ |
| 13 |
from mercurial import ( |
| 14 |
bundle2, |
| 15 |
bundle2_part_handlers, |
| 16 |
changegroup, |
| 17 |
error, |
| 18 |
exchange, |
| 19 |
localrepo, |
| 20 |
narrowspec, |
| 21 |
repair, |
| 22 |
requirements, |
| 23 |
scmutil, |
| 24 |
transaction, |
| 25 |
util, |
| 26 |
wireprototypes, |
| 27 |
) |
| 28 |
|
| 29 |
_NARROWACL_SECTION = b'narrowacl' |
| 30 |
_CHANGESPECPART = b'narrow:changespec' |
| 31 |
_RESSPECS = b'narrow:responsespec' |
| 32 |
_SPECPART = b'narrow:spec' |
| 33 |
_SPECPART_INCLUDE = b'include' |
| 34 |
_SPECPART_EXCLUDE = b'exclude' |
| 35 |
_KILLNODESIGNAL = b'KILL' |
| 36 |
_DONESIGNAL = b'DONE' |
| 37 |
_ELIDEDCSHEADER = b'>20s20s20sl' # cset id, p1, p2, len(text) |
| 38 |
_ELIDEDMFHEADER = b'>20s20s20s20sl' # manifest id, p1, p2, link id, len(text) |
| 39 |
_CSHEADERSIZE = struct.calcsize(_ELIDEDCSHEADER) |
| 40 |
_MFHEADERSIZE = struct.calcsize(_ELIDEDMFHEADER) |
| 41 |
|
| 42 |
|
| 43 |
# Serve a changegroup for a client with a narrow clone. |
| 44 |
def getbundlechangegrouppart_narrow( |
| 45 |
bundler, |
| 46 |
repo, |
| 47 |
source, |
| 48 |
bundlecaps=None, |
| 49 |
b2caps=None, |
| 50 |
heads=None, |
| 51 |
common=None, |
| 52 |
**kwargs, |
| 53 |
): |
| 54 |
assert repo.ui.configbool(b'experimental', b'narrowservebrokenellipses') |
| 55 |
|
| 56 |
cgversions = b2caps.get(b'changegroup') |
| 57 |
cgversions = [ |
| 58 |
v |
| 59 |
for v in cgversions |
| 60 |
if v in changegroup.supportedoutgoingversions(repo) |
| 61 |
] |
| 62 |
if not cgversions: |
| 63 |
raise ValueError(_(b'no common changegroup version')) |
| 64 |
version = max(cgversions) |
| 65 |
|
| 66 |
include = sorted(filter(bool, kwargs.get('includepats', []))) |
| 67 |
exclude = sorted(filter(bool, kwargs.get('excludepats', []))) |
| 68 |
generateellipsesbundle2( |
| 69 |
bundler, |
| 70 |
repo, |
| 71 |
include, |
| 72 |
exclude, |
| 73 |
version, |
| 74 |
common, |
| 75 |
heads, |
| 76 |
kwargs.get('depth', None), |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
def generateellipsesbundle2( |
| 81 |
bundler, |
| 82 |
repo, |
| 83 |
include, |
| 84 |
exclude, |
| 85 |
version, |
| 86 |
common, |
| 87 |
heads, |
| 88 |
depth, |
| 89 |
): |
| 90 |
match = narrowspec.match(repo.root, include=include, exclude=exclude) |
| 91 |
if depth is not None: |
| 92 |
depth = int(depth) |
| 93 |
if depth < 1: |
| 94 |
raise error.Abort(_(b'depth must be positive, got %d') % depth) |
| 95 |
|
| 96 |
heads = set(heads or repo.heads()) |
| 97 |
common = set(common or [repo.nullid]) |
| 98 |
|
| 99 |
visitnodes, relevant_nodes, ellipsisroots = exchange._computeellipsis( |
| 100 |
repo, common, heads, set(), match, depth=depth |
| 101 |
) |
| 102 |
|
| 103 |
repo.ui.debug(b'Found %d relevant revs\n' % len(relevant_nodes)) |
| 104 |
if visitnodes: |
| 105 |
packer = changegroup.getbundler( |
| 106 |
version, |
| 107 |
repo, |
| 108 |
matcher=match, |
| 109 |
ellipses=True, |
| 110 |
shallow=depth is not None, |
| 111 |
ellipsisroots=ellipsisroots, |
| 112 |
fullnodes=relevant_nodes, |
| 113 |
) |
| 114 |
cgdata = packer.generate(common, visitnodes, False, b'narrow_widen') |
| 115 |
|
| 116 |
part = bundler.newpart(b'changegroup', data=cgdata) |
| 117 |
part.addparam(b'version', version) |
| 118 |
if scmutil.istreemanifest(repo): |
| 119 |
part.addparam(b'treemanifest', b'1') |
| 120 |
|
| 121 |
|
| 122 |
def generate_ellipses_bundle2_for_widening( |
| 123 |
bundler, |
| 124 |
repo, |
| 125 |
oldmatch, |
| 126 |
newmatch, |
| 127 |
version, |
| 128 |
common, |
| 129 |
known, |
| 130 |
): |
| 131 |
common = set(common or [repo.nullid]) |
| 132 |
# Steps: |
| 133 |
# 1. Send kill for "$known & ::common" |
| 134 |
# |
| 135 |
# 2. Send changegroup for ::common |
| 136 |
# |
| 137 |
# 3. Proceed. |
| 138 |
# |
| 139 |
# In the future, we can send kills for only the specific |
| 140 |
# nodes we know should go away or change shape, and then |
| 141 |
# send a data stream that tells the client something like this: |
| 142 |
# |
| 143 |
# a) apply this changegroup |
| 144 |
# b) apply nodes XXX, YYY, ZZZ that you already have |
| 145 |
# c) goto a |
| 146 |
# |
| 147 |
# until they've built up the full new state. |
| 148 |
knownrevs = {repo.changelog.rev(n) for n in known} |
| 149 |
# TODO: we could send only roots() of this set, and the |
| 150 |
# list of nodes in common, and the client could work out |
| 151 |
# what to strip, instead of us explicitly sending every |
| 152 |
# single node. |
| 153 |
deadrevs = knownrevs |
| 154 |
|
| 155 |
def genkills(): |
| 156 |
for r in deadrevs: |
| 157 |
yield _KILLNODESIGNAL |
| 158 |
yield repo.changelog.node(r) |
| 159 |
yield _DONESIGNAL |
| 160 |
|
| 161 |
bundler.newpart(_CHANGESPECPART, data=genkills()) |
| 162 |
newvisit, newfull, newellipsis = exchange._computeellipsis( |
| 163 |
repo, set(), common, knownrevs, newmatch |
| 164 |
) |
| 165 |
if newvisit: |
| 166 |
packer = changegroup.getbundler( |
| 167 |
version, |
| 168 |
repo, |
| 169 |
matcher=newmatch, |
| 170 |
ellipses=True, |
| 171 |
shallow=False, |
| 172 |
ellipsisroots=newellipsis, |
| 173 |
fullnodes=newfull, |
| 174 |
) |
| 175 |
cgdata = packer.generate(common, newvisit, False, b'narrow_widen') |
| 176 |
|
| 177 |
part = bundler.newpart(b'changegroup', data=cgdata) |
| 178 |
part.addparam(b'version', version) |
| 179 |
if scmutil.istreemanifest(repo): |
| 180 |
part.addparam(b'treemanifest', b'1') |
| 181 |
|
| 182 |
|
| 183 |
@bundle2_part_handlers.parthandler(_RESSPECS) |
| 184 |
def _handlenarrowspecs(op, inpart): |
| 185 |
data = inpart.read() |
| 186 |
inc, exc = data.split(b'\0') |
| 187 |
includepats = set(inc.splitlines()) |
| 188 |
excludepats = set(exc.splitlines()) |
| 189 |
narrowspec.validatepatterns(includepats) |
| 190 |
narrowspec.validatepatterns(excludepats) |
| 191 |
|
| 192 |
if not op.repo.is_narrow: |
| 193 |
op.repo.requirements.add(requirements.NARROW_REQUIREMENT) |
| 194 |
del op.repo.is_narrow # refresh the property cache |
| 195 |
scmutil.writereporequirements(op.repo) |
| 196 |
op.repo.setnarrowpats(includepats, excludepats) |
| 197 |
narrowspec.copytoworkingcopy(op.repo) |
| 198 |
|
| 199 |
|
| 200 |
@bundle2_part_handlers.parthandler(_CHANGESPECPART) |
| 201 |
def _handlechangespec(op, inpart): |
| 202 |
repo = op.repo |
| 203 |
cl = repo.changelog |
| 204 |
|
| 205 |
# changesets which need to be stripped entirely. either they're no longer |
| 206 |
# needed in the new narrow spec, or the server is sending a replacement |
| 207 |
# in the changegroup part. |
| 208 |
clkills = set() |
| 209 |
|
| 210 |
# A changespec part contains all the updates to ellipsis nodes |
| 211 |
# that will happen as a result of widening or narrowing a |
| 212 |
# repo. All the changes that this block encounters are ellipsis |
| 213 |
# nodes or flags to kill an existing ellipsis. |
| 214 |
chunksignal = changegroup.readexactly(inpart, 4) |
| 215 |
while chunksignal != _DONESIGNAL: |
| 216 |
if chunksignal == _KILLNODESIGNAL: |
| 217 |
# a node used to be an ellipsis but isn't anymore |
| 218 |
ck = changegroup.readexactly(inpart, 20) |
| 219 |
if cl.hasnode(ck): |
| 220 |
clkills.add(ck) |
| 221 |
else: |
| 222 |
raise error.Abort( |
| 223 |
_(b'unexpected changespec node chunk type: %s') % chunksignal |
| 224 |
) |
| 225 |
chunksignal = changegroup.readexactly(inpart, 4) |
| 226 |
|
| 227 |
if clkills: |
| 228 |
# preserve bookmarks that repair.strip() would otherwise strip |
| 229 |
op._bookmarksbackup = repo._bookmarks |
| 230 |
|
| 231 |
class dummybmstore(dict): |
| 232 |
def applychanges(self, repo, tr, changes): |
| 233 |
pass |
| 234 |
|
| 235 |
localrepo.localrepository._bookmarks.set(repo, dummybmstore()) |
| 236 |
chgrpfile = repair.strip( |
| 237 |
op.ui, repo, list(clkills), backup=True, topic=b'widen' |
| 238 |
) |
| 239 |
if chgrpfile: |
| 240 |
op._widen_uninterr = repo.ui.uninterruptible() |
| 241 |
op._widen_uninterr.__enter__() |
| 242 |
# presence of _widen_bundle attribute activates widen handler later |
| 243 |
op._widen_bundle = chgrpfile |
| 244 |
# Set the new narrowspec if we're widening. The setnewnarrowpats() method |
| 245 |
# will currently always be there when using the core+narrowhg server, but |
| 246 |
# other servers may include a changespec part even when not widening (e.g. |
| 247 |
# because we're deepening a shallow repo). |
| 248 |
if hasattr(repo, 'setnewnarrowpats'): |
| 249 |
op.gettransaction() |
| 250 |
repo.setnewnarrowpats() |
| 251 |
|
| 252 |
|
| 253 |
def handlechangegroup_widen(op, inpart): |
| 254 |
"""Changegroup exchange handler which restores temporarily-stripped nodes""" |
| 255 |
# We saved a bundle with stripped node data we must now restore. |
| 256 |
# This approach is based on mercurial/repair.py@6ee26a53c111. |
| 257 |
repo = op.repo |
| 258 |
ui = op.ui |
| 259 |
|
| 260 |
chgrpfile = op._widen_bundle |
| 261 |
del op._widen_bundle |
| 262 |
vfs = repo.vfs |
| 263 |
|
| 264 |
ui.note(_(b"adding branch\n")) |
| 265 |
f = vfs.open(chgrpfile, b"rb") |
| 266 |
try: |
| 267 |
gen = exchange.readbundle(ui, f, chgrpfile, vfs) |
| 268 |
# silence internal shuffling chatter |
| 269 |
maybe_silent = ( |
| 270 |
ui.silent() if not ui.verbose else util.nullcontextmanager() |
| 271 |
) |
| 272 |
with maybe_silent: |
| 273 |
if isinstance(gen, bundle2.unbundle20): |
| 274 |
with repo.transaction(b'strip') as tr: |
| 275 |
bundle2.processbundle(repo, gen, lambda: tr) |
| 276 |
else: |
| 277 |
gen.apply( |
| 278 |
repo, b'strip', b'bundle:' + vfs.join(chgrpfile), True |
| 279 |
) |
| 280 |
finally: |
| 281 |
f.close() |
| 282 |
|
| 283 |
transaction.cleanup_undo_files(repo.ui.warn, repo.vfs_map) |
| 284 |
|
| 285 |
# Remove partial backup only if there were no exceptions |
| 286 |
op._widen_uninterr.__exit__(None, None, None) |
| 287 |
vfs.unlink(chgrpfile) |
| 288 |
|
| 289 |
|
| 290 |
def setup(): |
| 291 |
"""Enable narrow repo support in bundle2-related extension points.""" |
| 292 |
getbundleargs = wireprototypes.GETBUNDLE_ARGUMENTS |
| 293 |
|
| 294 |
getbundleargs[b'narrow'] = b'boolean' |
| 295 |
getbundleargs[b'depth'] = b'plain' |
| 296 |
getbundleargs[b'oldincludepats'] = b'csv' |
| 297 |
getbundleargs[b'oldexcludepats'] = b'csv' |
| 298 |
getbundleargs[b'known'] = b'csv' |
| 299 |
|
| 300 |
# Extend changegroup serving to handle requests from narrow clients. |
| 301 |
origcgfn = exchange.getbundle2partsmapping[b'changegroup'] |
| 302 |
|
| 303 |
def wrappedcgfn(*args, **kwargs): |
| 304 |
repo = args[1] |
| 305 |
if repo.ui.has_section(_NARROWACL_SECTION): |
| 306 |
kwargs = exchange.applynarrowacl(repo, kwargs) |
| 307 |
|
| 308 |
if kwargs.get('narrow', False) and repo.ui.configbool( |
| 309 |
b'experimental', b'narrowservebrokenellipses' |
| 310 |
): |
| 311 |
getbundlechangegrouppart_narrow(*args, **kwargs) |
| 312 |
else: |
| 313 |
origcgfn(*args, **kwargs) |
| 314 |
|
| 315 |
exchange.getbundle2partsmapping[b'changegroup'] = wrappedcgfn |
| 316 |
|
| 317 |
# Extend changegroup receiver so client can fixup after widen requests. |
| 318 |
origcghandler = bundle2.parthandlermapping[b'changegroup'] |
| 319 |
|
| 320 |
def wrappedcghandler(op, inpart): |
| 321 |
origcghandler(op, inpart) |
| 322 |
if hasattr(op, '_widen_bundle'): |
| 323 |
handlechangegroup_widen(op, inpart) |
| 324 |
if hasattr(op, '_bookmarksbackup'): |
| 325 |
localrepo.localrepository._bookmarks.set( |
| 326 |
op.repo, op._bookmarksbackup |
| 327 |
) |
| 328 |
del op._bookmarksbackup |
| 329 |
|
| 330 |
wrappedcghandler.params = origcghandler.params |
| 331 |
bundle2.parthandlermapping[b'changegroup'] = wrappedcghandler |