Mercurial > hg
comparison mercurial/wireproto.py @ 37546:3a2367e6c6f2
wireproto: move version 2 command handlers to wireprotov2server
This is relatively straightforward.
As part of this, we introduced a local @wireprotocommand that
wraps the main one and defines a v2 only policy by default.
Because the hacky HTTPv2 peer isn't using capabilities response
yet, we had to move some code around to force import of
wireprotov2server so commands are registered. This is super
hacky. But this code will go away once the HTTPv2 peer is using
the capabilities response to derive permissions.
Differential Revision: https://phab.mercurial-scm.org/D3231
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Mon, 09 Apr 2018 19:35:39 -0700 |
parents | 3e5e37204b32 |
children | 5e71dea79aae |
comparison
equal
deleted
inserted
replaced
37545:93397c4633f6 | 37546:3a2367e6c6f2 |
---|---|
1303 manargs, advargs)) | 1303 manargs, advargs)) |
1304 except error.PushRaced as exc: | 1304 except error.PushRaced as exc: |
1305 bundler.newpart('error:pushraced', | 1305 bundler.newpart('error:pushraced', |
1306 [('message', stringutil.forcebytestr(exc))]) | 1306 [('message', stringutil.forcebytestr(exc))]) |
1307 return wireprototypes.streamreslegacy(gen=bundler.getchunks()) | 1307 return wireprototypes.streamreslegacy(gen=bundler.getchunks()) |
1308 | |
1309 # Wire protocol version 2 commands only past this point. | |
1310 | |
1311 def _capabilitiesv2(repo, proto): | |
1312 """Obtain the set of capabilities for version 2 transports. | |
1313 | |
1314 These capabilities are distinct from the capabilities for version 1 | |
1315 transports. | |
1316 """ | |
1317 compression = [] | |
1318 for engine in supportedcompengines(repo.ui, util.SERVERROLE): | |
1319 compression.append({ | |
1320 b'name': engine.wireprotosupport().name, | |
1321 }) | |
1322 | |
1323 caps = { | |
1324 'commands': {}, | |
1325 'compression': compression, | |
1326 } | |
1327 | |
1328 for command, entry in commandsv2.items(): | |
1329 caps['commands'][command] = { | |
1330 'args': entry.args, | |
1331 'permissions': [entry.permission], | |
1332 } | |
1333 | |
1334 return proto.addcapabilities(repo, caps) | |
1335 | |
1336 @wireprotocommand('branchmap', permission='pull', | |
1337 transportpolicy=POLICY_V2_ONLY) | |
1338 def branchmapv2(repo, proto): | |
1339 branchmap = {encoding.fromlocal(k): v | |
1340 for k, v in repo.branchmap().iteritems()} | |
1341 | |
1342 return wireprototypes.cborresponse(branchmap) | |
1343 | |
1344 @wireprotocommand('capabilities', permission='pull', | |
1345 transportpolicy=POLICY_V2_ONLY) | |
1346 def capabilitiesv2(repo, proto): | |
1347 caps = _capabilitiesv2(repo, proto) | |
1348 | |
1349 return wireprototypes.cborresponse(caps) | |
1350 | |
1351 @wireprotocommand('heads', | |
1352 args={ | |
1353 'publiconly': False, | |
1354 }, | |
1355 permission='pull', | |
1356 transportpolicy=POLICY_V2_ONLY) | |
1357 def headsv2(repo, proto, publiconly=False): | |
1358 if publiconly: | |
1359 repo = repo.filtered('immutable') | |
1360 | |
1361 return wireprototypes.cborresponse(repo.heads()) | |
1362 | |
1363 @wireprotocommand('known', | |
1364 args={ | |
1365 'nodes': [b'deadbeef'], | |
1366 }, | |
1367 permission='pull', | |
1368 transportpolicy=POLICY_V2_ONLY) | |
1369 def knownv2(repo, proto, nodes=None): | |
1370 nodes = nodes or [] | |
1371 result = b''.join(b'1' if n else b'0' for n in repo.known(nodes)) | |
1372 return wireprototypes.cborresponse(result) | |
1373 | |
1374 @wireprotocommand('listkeys', | |
1375 args={ | |
1376 'namespace': b'ns', | |
1377 }, | |
1378 permission='pull', | |
1379 transportpolicy=POLICY_V2_ONLY) | |
1380 def listkeysv2(repo, proto, namespace=None): | |
1381 keys = repo.listkeys(encoding.tolocal(namespace)) | |
1382 keys = {encoding.fromlocal(k): encoding.fromlocal(v) | |
1383 for k, v in keys.iteritems()} | |
1384 | |
1385 return wireprototypes.cborresponse(keys) | |
1386 | |
1387 @wireprotocommand('lookup', | |
1388 args={ | |
1389 'key': b'foo', | |
1390 }, | |
1391 permission='pull', | |
1392 transportpolicy=POLICY_V2_ONLY) | |
1393 def lookupv2(repo, proto, key): | |
1394 key = encoding.tolocal(key) | |
1395 | |
1396 # TODO handle exception. | |
1397 node = repo.lookup(key) | |
1398 | |
1399 return wireprototypes.cborresponse(node) | |
1400 | |
1401 @wireprotocommand('pushkey', | |
1402 args={ | |
1403 'namespace': b'ns', | |
1404 'key': b'key', | |
1405 'old': b'old', | |
1406 'new': b'new', | |
1407 }, | |
1408 permission='push', | |
1409 transportpolicy=POLICY_V2_ONLY) | |
1410 def pushkeyv2(repo, proto, namespace, key, old, new): | |
1411 # TODO handle ui output redirection | |
1412 r = repo.pushkey(encoding.tolocal(namespace), | |
1413 encoding.tolocal(key), | |
1414 encoding.tolocal(old), | |
1415 encoding.tolocal(new)) | |
1416 | |
1417 return wireprototypes.cborresponse(r) |