exchange.py
1583 lines
| 59.7 KiB
| text/x-python
|
PythonLexer
/ mercurial / exchange.py
Mads Kiilerich
|
r21024 | # exchange.py - utility to exchange data between repos. | ||
Pierre-Yves David
|
r20345 | # | ||
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
Gregory Szorc
|
r25236 | import time | ||
Pierre-Yves David
|
r20345 | from i18n import _ | ||
Pierre-Yves David
|
r20469 | from node import hex, nullid | ||
Pierre-Yves David
|
r21141 | import errno, urllib | ||
Gregory Szorc
|
r25235 | import util, scmutil, changegroup, base85, error, store | ||
Pierre-Yves David
|
r22622 | import discovery, phases, obsolete, bookmarks as bookmod, bundle2, pushkey | ||
Pierre-Yves David
|
r24752 | import lock as lockmod | ||
Gregory Szorc
|
r25402 | import tags | ||
Pierre-Yves David
|
r20345 | |||
Pierre-Yves David
|
r21064 | def readbundle(ui, fh, fname, vfs=None): | ||
Pierre-Yves David
|
r21065 | header = changegroup.readexactly(fh, 4) | ||
Pierre-Yves David
|
r21063 | |||
Pierre-Yves David
|
r21065 | alg = None | ||
Pierre-Yves David
|
r21063 | if not fname: | ||
fname = "stream" | ||||
if not header.startswith('HG') and header.startswith('\0'): | ||||
fh = changegroup.headerlessfixup(fh, header) | ||||
Pierre-Yves David
|
r21065 | header = "HG10" | ||
alg = 'UN' | ||||
Pierre-Yves David
|
r21063 | elif vfs: | ||
fname = vfs.join(fname) | ||||
Pierre-Yves David
|
r21065 | magic, version = header[0:2], header[2:4] | ||
Pierre-Yves David
|
r21063 | |||
if magic != 'HG': | ||||
raise util.Abort(_('%s: not a Mercurial bundle') % fname) | ||||
Pierre-Yves David
|
r21065 | if version == '10': | ||
if alg is None: | ||||
alg = changegroup.readexactly(fh, 2) | ||||
Sune Foldager
|
r22390 | return changegroup.cg1unpacker(fh, alg) | ||
Pierre-Yves David
|
r24649 | elif version.startswith('2'): | ||
Pierre-Yves David
|
r25640 | return bundle2.getunbundler(ui, fh, magicstring=magic + version) | ||
Pierre-Yves David
|
r21065 | else: | ||
Pierre-Yves David
|
r21063 | raise util.Abort(_('%s: unknown bundle version %s') % (fname, version)) | ||
Pierre-Yves David
|
r22346 | def buildobsmarkerspart(bundler, markers): | ||
"""add an obsmarker part to the bundler with <markers> | ||||
No part is created if markers is empty. | ||||
Raises ValueError if the bundler doesn't support any known obsmarker format. | ||||
""" | ||||
if markers: | ||||
remoteversions = bundle2.obsmarkersversion(bundler.capabilities) | ||||
version = obsolete.commonversion(remoteversions) | ||||
if version is None: | ||||
raise ValueError('bundler do not support common obsmarker format') | ||||
stream = obsolete.encodemarkers(markers, True, version=version) | ||||
Pierre-Yves David
|
r24686 | return bundler.newpart('obsmarkers', data=stream) | ||
Pierre-Yves David
|
r22346 | return None | ||
Pierre-Yves David
|
r20346 | |||
Pierre-Yves David
|
r24650 | def _canusebundle2(op): | ||
"""return true if a pull/push can use bundle2 | ||||
Feel free to nuke this function when we drop the experimental option""" | ||||
Pierre-Yves David
|
r25404 | return (op.repo.ui.configbool('experimental', 'bundle2-exp', True) | ||
Pierre-Yves David
|
r24686 | and op.remote.capable('bundle2')) | ||
Pierre-Yves David
|
r24650 | |||
Pierre-Yves David
|
r20346 | class pushoperation(object): | ||
"""A object that represent a single push operation | ||||
It purpose is to carry push related state and very common operation. | ||||
Mads Kiilerich
|
r21024 | A new should be created at the beginning of each push and discarded | ||
Pierre-Yves David
|
r20346 | afterward. | ||
""" | ||||
Pierre-Yves David
|
r22623 | def __init__(self, repo, remote, force=False, revs=None, newbranch=False, | ||
bookmarks=()): | ||||
Pierre-Yves David
|
r20346 | # repo we push from | ||
self.repo = repo | ||||
Pierre-Yves David
|
r20347 | self.ui = repo.ui | ||
Pierre-Yves David
|
r20348 | # repo we push to | ||
self.remote = remote | ||||
Pierre-Yves David
|
r20349 | # force option provided | ||
self.force = force | ||||
Pierre-Yves David
|
r20350 | # revs to be pushed (None is "all") | ||
self.revs = revs | ||||
Pierre-Yves David
|
r22623 | # bookmark explicitly pushed | ||
self.bookmarks = bookmarks | ||||
Pierre-Yves David
|
r20351 | # allow push of new branch | ||
self.newbranch = newbranch | ||||
Pierre-Yves David
|
r20436 | # did a local lock get acquired? | ||
self.locallocked = None | ||||
Pierre-Yves David
|
r21901 | # step already performed | ||
# (used to check what steps have been already performed through bundle2) | ||||
self.stepsdone = set() | ||||
Pierre-Yves David
|
r22615 | # Integer version of the changegroup push result | ||
Pierre-Yves David
|
r20439 | # - None means nothing to push | ||
# - 0 means HTTP error | ||||
# - 1 means we pushed and remote head count is unchanged *or* | ||||
# we have outgoing changesets but refused to push | ||||
# - other values as described by addchangegroup() | ||||
Pierre-Yves David
|
r22615 | self.cgresult = None | ||
Pierre-Yves David
|
r22624 | # Boolean value for the bookmark push | ||
self.bkresult = None | ||||
Mads Kiilerich
|
r21024 | # discover.outgoing object (contains common and outgoing data) | ||
Pierre-Yves David
|
r20440 | self.outgoing = None | ||
Pierre-Yves David
|
r20462 | # all remote heads before the push | ||
self.remoteheads = None | ||||
Pierre-Yves David
|
r20464 | # testable as a boolean indicating if any nodes are missing locally. | ||
self.incoming = None | ||||
Pierre-Yves David
|
r22019 | # phases changes that must be pushed along side the changesets | ||
self.outdatedphases = None | ||||
# phases changes that must be pushed if changeset push fails | ||||
self.fallbackoutdatedphases = None | ||||
Pierre-Yves David
|
r22034 | # outgoing obsmarkers | ||
Pierre-Yves David
|
r22035 | self.outobsmarkers = set() | ||
Pierre-Yves David
|
r22239 | # outgoing bookmarks | ||
self.outbookmarks = [] | ||||
Eric Sumner
|
r23437 | # transaction manager | ||
self.trmanager = None | ||||
Pierre-Yves David
|
r25485 | # map { pushkey partid -> callback handling failure} | ||
# used to handle exception from mandatory pushkey part failure | ||||
self.pkfailcb = {} | ||||
Pierre-Yves David
|
r20346 | |||
Pierre-Yves David
|
r22014 | @util.propertycache | ||
def futureheads(self): | ||||
"""future remote heads if the changeset push succeeds""" | ||||
return self.outgoing.missingheads | ||||
Pierre-Yves David
|
r22015 | @util.propertycache | ||
def fallbackheads(self): | ||||
"""future remote heads if the changeset push fails""" | ||||
if self.revs is None: | ||||
# not target to push, all common are relevant | ||||
return self.outgoing.commonheads | ||||
unfi = self.repo.unfiltered() | ||||
# I want cheads = heads(::missingheads and ::commonheads) | ||||
# (missingheads is revs with secret changeset filtered out) | ||||
# | ||||
# This can be expressed as: | ||||
# cheads = ( (missingheads and ::commonheads) | ||||
# + (commonheads and ::missingheads))" | ||||
# ) | ||||
# | ||||
# while trying to push we already computed the following: | ||||
# common = (::commonheads) | ||||
# missing = ((commonheads::missingheads) - commonheads) | ||||
# | ||||
# We can pick: | ||||
# * missingheads part of common (::commonheads) | ||||
Durham Goode
|
r26184 | common = self.outgoing.common | ||
Pierre-Yves David
|
r22015 | nm = self.repo.changelog.nodemap | ||
cheads = [node for node in self.revs if nm[node] in common] | ||||
# and | ||||
# * commonheads parents on missing | ||||
revset = unfi.set('%ln and parents(roots(%ln))', | ||||
self.outgoing.commonheads, | ||||
self.outgoing.missing) | ||||
cheads.extend(c.node() for c in revset) | ||||
return cheads | ||||
Pierre-Yves David
|
r22016 | @property | ||
def commonheads(self): | ||||
"""set of all common heads after changeset bundle push""" | ||||
Pierre-Yves David
|
r22615 | if self.cgresult: | ||
Pierre-Yves David
|
r22016 | return self.futureheads | ||
else: | ||||
return self.fallbackheads | ||||
Pierre-Yves David
|
r22015 | |||
Pierre-Yves David
|
r22650 | # mapping of message used when pushing bookmark | ||
bookmsgmap = {'update': (_("updating bookmark %s\n"), | ||||
_('updating bookmark %s failed!\n')), | ||||
'export': (_("exporting bookmark %s\n"), | ||||
_('exporting bookmark %s failed!\n')), | ||||
'delete': (_("deleting remote bookmark %s\n"), | ||||
_('deleting remote bookmark %s failed!\n')), | ||||
} | ||||
Pierre-Yves David
|
r22623 | def push(repo, remote, force=False, revs=None, newbranch=False, bookmarks=()): | ||
Pierre-Yves David
|
r20345 | '''Push outgoing changesets (limited by revs) from a local | ||
repository to remote. Return an integer: | ||||
- None means nothing to push | ||||
- 0 means HTTP error | ||||
- 1 means we pushed and remote head count is unchanged *or* | ||||
we have outgoing changesets but refused to push | ||||
- other values as described by addchangegroup() | ||||
''' | ||||
Pierre-Yves David
|
r22623 | pushop = pushoperation(repo, remote, force, revs, newbranch, bookmarks) | ||
Pierre-Yves David
|
r20348 | if pushop.remote.local(): | ||
missing = (set(pushop.repo.requirements) | ||||
- pushop.remote.local().supported) | ||||
Pierre-Yves David
|
r20345 | if missing: | ||
msg = _("required features are not" | ||||
" supported in the destination:" | ||||
" %s") % (', '.join(sorted(missing))) | ||||
raise util.Abort(msg) | ||||
# there are two ways to push to remote repo: | ||||
# | ||||
# addchangegroup assumes local user can lock remote | ||||
# repo (local filesystem, old ssh servers). | ||||
# | ||||
# unbundle assumes local user cannot lock remote repo (new ssh | ||||
# servers, http servers). | ||||
Pierre-Yves David
|
r20348 | if not pushop.remote.canpush(): | ||
Pierre-Yves David
|
r20345 | raise util.Abort(_("destination does not support push")) | ||
# get local lock as we might write phase data | ||||
Pierre-Yves David
|
r24754 | localwlock = locallock = None | ||
Pierre-Yves David
|
r20345 | try: | ||
Pierre-Yves David
|
r24754 | # bundle2 push may receive a reply bundle touching bookmarks or other | ||
# things requiring the wlock. Take it now to ensure proper ordering. | ||||
maypushback = pushop.ui.configbool('experimental', 'bundle2.pushback') | ||||
if _canusebundle2(pushop) and maypushback: | ||||
localwlock = pushop.repo.wlock() | ||||
Pierre-Yves David
|
r20346 | locallock = pushop.repo.lock() | ||
Pierre-Yves David
|
r20436 | pushop.locallocked = True | ||
Gregory Szorc
|
r25660 | except IOError as err: | ||
Pierre-Yves David
|
r20436 | pushop.locallocked = False | ||
Pierre-Yves David
|
r20345 | if err.errno != errno.EACCES: | ||
raise | ||||
# source repo cannot be locked. | ||||
# We do not abort the push, but just disable the local phase | ||||
# synchronisation. | ||||
msg = 'cannot lock source repository: %s\n' % err | ||||
Pierre-Yves David
|
r20347 | pushop.ui.debug(msg) | ||
Pierre-Yves David
|
r20345 | try: | ||
Eric Sumner
|
r23437 | if pushop.locallocked: | ||
pushop.trmanager = transactionmanager(repo, | ||||
'push-response', | ||||
pushop.remote.url()) | ||||
Pierre-Yves David
|
r20924 | pushop.repo.checkpush(pushop) | ||
Pierre-Yves David
|
r20345 | lock = None | ||
Pierre-Yves David
|
r20348 | unbundle = pushop.remote.capable('unbundle') | ||
Pierre-Yves David
|
r20345 | if not unbundle: | ||
Pierre-Yves David
|
r20348 | lock = pushop.remote.lock() | ||
Pierre-Yves David
|
r20345 | try: | ||
Pierre-Yves David
|
r20466 | _pushdiscovery(pushop) | ||
Pierre-Yves David
|
r24650 | if _canusebundle2(pushop): | ||
Pierre-Yves David
|
r21903 | _pushbundle2(pushop) | ||
_pushchangeset(pushop) | ||||
Pierre-Yves David
|
r20441 | _pushsyncphase(pushop) | ||
Pierre-Yves David
|
r20433 | _pushobsolete(pushop) | ||
Pierre-Yves David
|
r22224 | _pushbookmark(pushop) | ||
Pierre-Yves David
|
r20345 | finally: | ||
if lock is not None: | ||||
lock.release() | ||||
Eric Sumner
|
r23437 | if pushop.trmanager: | ||
pushop.trmanager.close() | ||||
Pierre-Yves David
|
r20345 | finally: | ||
Eric Sumner
|
r23437 | if pushop.trmanager: | ||
pushop.trmanager.release() | ||||
Pierre-Yves David
|
r20345 | if locallock is not None: | ||
locallock.release() | ||||
Pierre-Yves David
|
r24754 | if localwlock is not None: | ||
localwlock.release() | ||||
Pierre-Yves David
|
r20345 | |||
Pierre-Yves David
|
r22616 | return pushop | ||
Pierre-Yves David
|
r20352 | |||
Pierre-Yves David
|
r22018 | # list of steps to perform discovery before push | ||
pushdiscoveryorder = [] | ||||
# Mapping between step name and function | ||||
# | ||||
# This exists to help extensions wrap steps if necessary | ||||
pushdiscoverymapping = {} | ||||
def pushdiscovery(stepname): | ||||
"""decorator for function performing discovery before push | ||||
The function is added to the step -> function mapping and appended to the | ||||
list of steps. Beware that decorated function will be added in order (this | ||||
may matter). | ||||
You can only use this decorator for a new step, if you want to wrap a step | ||||
from an extension, change the pushdiscovery dictionary directly.""" | ||||
def dec(func): | ||||
assert stepname not in pushdiscoverymapping | ||||
pushdiscoverymapping[stepname] = func | ||||
pushdiscoveryorder.append(stepname) | ||||
return func | ||||
return dec | ||||
Pierre-Yves David
|
r20466 | def _pushdiscovery(pushop): | ||
Pierre-Yves David
|
r22018 | """Run all discovery steps""" | ||
for stepname in pushdiscoveryorder: | ||||
step = pushdiscoverymapping[stepname] | ||||
step(pushop) | ||||
@pushdiscovery('changeset') | ||||
def _pushdiscoverychangeset(pushop): | ||||
"""discover the changeset that need to be pushed""" | ||||
Pierre-Yves David
|
r20466 | fci = discovery.findcommonincoming | ||
Pierre-Yves David
|
r23848 | commoninc = fci(pushop.repo, pushop.remote, force=pushop.force) | ||
Pierre-Yves David
|
r20466 | common, inc, remoteheads = commoninc | ||
fco = discovery.findcommonoutgoing | ||||
Pierre-Yves David
|
r23848 | outgoing = fco(pushop.repo, pushop.remote, onlyheads=pushop.revs, | ||
Pierre-Yves David
|
r20466 | commoninc=commoninc, force=pushop.force) | ||
pushop.outgoing = outgoing | ||||
pushop.remoteheads = remoteheads | ||||
pushop.incoming = inc | ||||
Pierre-Yves David
|
r22019 | @pushdiscovery('phase') | ||
def _pushdiscoveryphase(pushop): | ||||
"""discover the phase that needs to be pushed | ||||
(computed for both success and failure case for changesets push)""" | ||||
outgoing = pushop.outgoing | ||||
unfi = pushop.repo.unfiltered() | ||||
remotephases = pushop.remote.listkeys('phases') | ||||
publishing = remotephases.get('publishing', False) | ||||
Pierre-Yves David
|
r25337 | if (pushop.ui.configbool('ui', '_usedassubrepo', False) | ||
and remotephases # server supports phases | ||||
and not pushop.outgoing.missing # no changesets to be pushed | ||||
and publishing): | ||||
# When: | ||||
# - this is a subrepo push | ||||
# - and remote support phase | ||||
# - and no changeset are to be pushed | ||||
# - and remote is publishing | ||||
# We may be in issue 3871 case! | ||||
# We drop the possible phase synchronisation done by | ||||
# courtesy to publish changesets possibly locally draft | ||||
# on the remote. | ||||
remotephases = {'publishing': 'True'} | ||||
Pierre-Yves David
|
r22019 | ana = phases.analyzeremotephases(pushop.repo, | ||
pushop.fallbackheads, | ||||
remotephases) | ||||
pheads, droots = ana | ||||
extracond = '' | ||||
if not publishing: | ||||
extracond = ' and public()' | ||||
revset = 'heads((%%ln::%%ln) %s)' % extracond | ||||
# Get the list of all revs draft on remote by public here. | ||||
# XXX Beware that revset break if droots is not strictly | ||||
# XXX root we may want to ensure it is but it is costly | ||||
fallback = list(unfi.set(revset, droots, pushop.fallbackheads)) | ||||
if not outgoing.missing: | ||||
future = fallback | ||||
else: | ||||
# adds changeset we are going to push as draft | ||||
# | ||||
Mads Kiilerich
|
r23139 | # should not be necessary for publishing server, but because of an | ||
Pierre-Yves David
|
r22019 | # issue fixed in xxxxx we have to do it anyway. | ||
fdroots = list(unfi.set('roots(%ln + %ln::)', | ||||
outgoing.missing, droots)) | ||||
fdroots = [f.node() for f in fdroots] | ||||
future = list(unfi.set(revset, fdroots, pushop.futureheads)) | ||||
pushop.outdatedphases = future | ||||
pushop.fallbackoutdatedphases = fallback | ||||
Pierre-Yves David
|
r22035 | @pushdiscovery('obsmarker') | ||
def _pushdiscoveryobsmarkers(pushop): | ||||
Durham Goode
|
r22953 | if (obsolete.isenabled(pushop.repo, obsolete.exchangeopt) | ||
Pierre-Yves David
|
r22269 | and pushop.repo.obsstore | ||
and 'obsolete' in pushop.remote.listkeys('namespaces')): | ||||
Pierre-Yves David
|
r22350 | repo = pushop.repo | ||
# very naive computation, that can be quite expensive on big repo. | ||||
# However: evolution is currently slow on them anyway. | ||||
nodes = (c.node() for c in repo.set('::%ln', pushop.futureheads)) | ||||
pushop.outobsmarkers = pushop.repo.obsstore.relevantmarkers(nodes) | ||||
Pierre-Yves David
|
r22035 | |||
Pierre-Yves David
|
r22239 | @pushdiscovery('bookmarks') | ||
def _pushdiscoverybookmarks(pushop): | ||||
ui = pushop.ui | ||||
repo = pushop.repo.unfiltered() | ||||
remote = pushop.remote | ||||
ui.debug("checking for updated bookmarks\n") | ||||
ancestors = () | ||||
if pushop.revs: | ||||
revnums = map(repo.changelog.rev, pushop.revs) | ||||
ancestors = repo.changelog.ancestors(revnums, inclusive=True) | ||||
remotebookmark = remote.listkeys('bookmarks') | ||||
Pierre-Yves David
|
r22651 | explicit = set(pushop.bookmarks) | ||
Pierre-Yves David
|
r22622 | comp = bookmod.compare(repo, repo._bookmarks, remotebookmark, srchex=hex) | ||
Gregory Szorc
|
r23081 | addsrc, adddst, advsrc, advdst, diverge, differ, invalid, same = comp | ||
Pierre-Yves David
|
r22239 | for b, scid, dcid in advsrc: | ||
Pierre-Yves David
|
r22651 | if b in explicit: | ||
explicit.remove(b) | ||||
Pierre-Yves David
|
r22239 | if not ancestors or repo[scid].rev() in ancestors: | ||
pushop.outbookmarks.append((b, dcid, scid)) | ||||
Pierre-Yves David
|
r22651 | # search added bookmark | ||
for b, scid, dcid in addsrc: | ||||
if b in explicit: | ||||
explicit.remove(b) | ||||
pushop.outbookmarks.append((b, '', scid)) | ||||
# search for overwritten bookmark | ||||
for b, scid, dcid in advdst + diverge + differ: | ||||
if b in explicit: | ||||
explicit.remove(b) | ||||
pushop.outbookmarks.append((b, dcid, scid)) | ||||
# search for bookmark to delete | ||||
for b, scid, dcid in adddst: | ||||
if b in explicit: | ||||
explicit.remove(b) | ||||
# treat as "deleted locally" | ||||
pushop.outbookmarks.append((b, dcid, '')) | ||||
Gregory Szorc
|
r23082 | # identical bookmarks shouldn't get reported | ||
for b, scid, dcid in same: | ||||
if b in explicit: | ||||
explicit.remove(b) | ||||
Pierre-Yves David
|
r22651 | |||
if explicit: | ||||
explicit = sorted(explicit) | ||||
# we should probably list all of them | ||||
ui.warn(_('bookmark %s does not exist on the local ' | ||||
'or remote repository!\n') % explicit[0]) | ||||
pushop.bkresult = 2 | ||||
pushop.outbookmarks.sort() | ||||
Pierre-Yves David
|
r22239 | |||
Pierre-Yves David
|
r20465 | def _pushcheckoutgoing(pushop): | ||
outgoing = pushop.outgoing | ||||
unfi = pushop.repo.unfiltered() | ||||
if not outgoing.missing: | ||||
# nothing to push | ||||
scmutil.nochangesfound(unfi.ui, unfi, outgoing.excluded) | ||||
return False | ||||
# something to push | ||||
if not pushop.force: | ||||
# if repo.obsstore == False --> no obsolete | ||||
# then, save the iteration | ||||
if unfi.obsstore: | ||||
# this message are here for 80 char limit reason | ||||
mso = _("push includes obsolete changeset: %s!") | ||||
Matt Mackall
|
r22628 | mst = {"unstable": _("push includes unstable changeset: %s!"), | ||
"bumped": _("push includes bumped changeset: %s!"), | ||||
"divergent": _("push includes divergent changeset: %s!")} | ||||
Pierre-Yves David
|
r20465 | # If we are to push if there is at least one | ||
# obsolete or unstable changeset in missing, at | ||||
# least one of the missinghead will be obsolete or | ||||
# unstable. So checking heads only is ok | ||||
for node in outgoing.missingheads: | ||||
ctx = unfi[node] | ||||
if ctx.obsolete(): | ||||
raise util.Abort(mso % ctx) | ||||
elif ctx.troubled(): | ||||
Matt Mackall
|
r22628 | raise util.Abort(mst[ctx.troubles()[0]] % ctx) | ||
Matt Mackall
|
r25836 | |||
# internal config: bookmarks.pushing | ||||
Pierre-Yves David
|
r20465 | newbm = pushop.ui.configlist('bookmarks', 'pushing') | ||
discovery.checkheads(unfi, pushop.remote, outgoing, | ||||
pushop.remoteheads, | ||||
pushop.newbranch, | ||||
bool(pushop.incoming), | ||||
newbm) | ||||
return True | ||||
Pierre-Yves David
|
r22017 | # List of names of steps to perform for an outgoing bundle2, order matters. | ||
b2partsgenorder = [] | ||||
# Mapping between step name and function | ||||
# | ||||
# This exists to help extensions wrap steps if necessary | ||||
b2partsgenmapping = {} | ||||
Pierre-Yves David
|
r24731 | def b2partsgenerator(stepname, idx=None): | ||
Pierre-Yves David
|
r22017 | """decorator for function generating bundle2 part | ||
The function is added to the step -> function mapping and appended to the | ||||
list of steps. Beware that decorated functions will be added in order | ||||
(this may matter). | ||||
You can only use this decorator for new steps, if you want to wrap a step | ||||
from an extension, attack the b2partsgenmapping dictionary directly.""" | ||||
def dec(func): | ||||
assert stepname not in b2partsgenmapping | ||||
b2partsgenmapping[stepname] = func | ||||
Pierre-Yves David
|
r24731 | if idx is None: | ||
b2partsgenorder.append(stepname) | ||||
else: | ||||
b2partsgenorder.insert(idx, stepname) | ||||
Pierre-Yves David
|
r22017 | return func | ||
return dec | ||||
Ryan McElroy
|
r26428 | def _pushb2ctxcheckheads(pushop, bundler): | ||
"""Generate race condition checking parts | ||||
Exists as an indepedent function to aid extensions | ||||
""" | ||||
if not pushop.force: | ||||
bundler.newpart('check:heads', data=iter(pushop.remoteheads)) | ||||
Pierre-Yves David
|
r22017 | @b2partsgenerator('changeset') | ||
Pierre-Yves David
|
r21899 | def _pushb2ctx(pushop, bundler): | ||
"""handle changegroup push through bundle2 | ||||
Pierre-Yves David
|
r22615 | addchangegroup result is stored in the ``pushop.cgresult`` attribute. | ||
Pierre-Yves David
|
r21899 | """ | ||
Pierre-Yves David
|
r21902 | if 'changesets' in pushop.stepsdone: | ||
return | ||||
pushop.stepsdone.add('changesets') | ||||
Pierre-Yves David
|
r21899 | # Send known heads to the server for race detection. | ||
Pierre-Yves David
|
r21903 | if not _pushcheckoutgoing(pushop): | ||
return | ||||
pushop.repo.prepushoutgoinghooks(pushop.repo, | ||||
pushop.remote, | ||||
pushop.outgoing) | ||||
Ryan McElroy
|
r26428 | |||
_pushb2ctxcheckheads(pushop, bundler) | ||||
Pierre-Yves David
|
r23180 | b2caps = bundle2.bundle2caps(pushop.remote) | ||
version = None | ||||
Pierre-Yves David
|
r24686 | cgversions = b2caps.get('changegroup') | ||
Pierre-Yves David
|
r23208 | if not cgversions: # 3.1 and 3.2 ship with an empty value | ||
Pierre-Yves David
|
r23180 | cg = changegroup.getlocalchangegroupraw(pushop.repo, 'push', | ||
pushop.outgoing) | ||||
else: | ||||
cgversions = [v for v in cgversions if v in changegroup.packermap] | ||||
if not cgversions: | ||||
raise ValueError(_('no common changegroup version')) | ||||
version = max(cgversions) | ||||
cg = changegroup.getlocalchangegroupraw(pushop.repo, 'push', | ||||
pushop.outgoing, | ||||
version=version) | ||||
Pierre-Yves David
|
r24686 | cgpart = bundler.newpart('changegroup', data=cg) | ||
Pierre-Yves David
|
r23180 | if version is not None: | ||
cgpart.addparam('version', version) | ||||
Pierre-Yves David
|
r21899 | def handlereply(op): | ||
Mads Kiilerich
|
r23139 | """extract addchangegroup returns from server reply""" | ||
Pierre-Yves David
|
r21899 | cgreplies = op.records.getreplies(cgpart.id) | ||
assert len(cgreplies['changegroup']) == 1 | ||||
Pierre-Yves David
|
r22615 | pushop.cgresult = cgreplies['changegroup'][0]['return'] | ||
Pierre-Yves David
|
r21899 | return handlereply | ||
Pierre-Yves David
|
r22020 | @b2partsgenerator('phase') | ||
def _pushb2phases(pushop, bundler): | ||||
"""handle phase push through bundle2""" | ||||
if 'phases' in pushop.stepsdone: | ||||
return | ||||
b2caps = bundle2.bundle2caps(pushop.remote) | ||||
Pierre-Yves David
|
r24686 | if not 'pushkey' in b2caps: | ||
Pierre-Yves David
|
r22020 | return | ||
pushop.stepsdone.add('phases') | ||||
part2node = [] | ||||
Pierre-Yves David
|
r25502 | |||
def handlefailure(pushop, exc): | ||||
targetid = int(exc.partid) | ||||
for partid, node in part2node: | ||||
if partid == targetid: | ||||
raise error.Abort(_('updating %s to public failed') % node) | ||||
Pierre-Yves David
|
r22020 | enc = pushkey.encode | ||
for newremotehead in pushop.outdatedphases: | ||||
Pierre-Yves David
|
r25502 | part = bundler.newpart('pushkey') | ||
Pierre-Yves David
|
r22020 | part.addparam('namespace', enc('phases')) | ||
part.addparam('key', enc(newremotehead.hex())) | ||||
part.addparam('old', enc(str(phases.draft))) | ||||
part.addparam('new', enc(str(phases.public))) | ||||
part2node.append((part.id, newremotehead)) | ||||
Pierre-Yves David
|
r25502 | pushop.pkfailcb[part.id] = handlefailure | ||
Pierre-Yves David
|
r22020 | def handlereply(op): | ||
for partid, node in part2node: | ||||
partrep = op.records.getreplies(partid) | ||||
results = partrep['pushkey'] | ||||
assert len(results) <= 1 | ||||
msg = None | ||||
if not results: | ||||
msg = _('server ignored update of %s to public!\n') % node | ||||
elif not int(results[0]['return']): | ||||
msg = _('updating %s to public failed!\n') % node | ||||
if msg is not None: | ||||
pushop.ui.warn(msg) | ||||
return handlereply | ||||
Pierre-Yves David
|
r21904 | |||
Pierre-Yves David
|
r22347 | @b2partsgenerator('obsmarkers') | ||
def _pushb2obsmarkers(pushop, bundler): | ||||
if 'obsmarkers' in pushop.stepsdone: | ||||
return | ||||
remoteversions = bundle2.obsmarkersversion(bundler.capabilities) | ||||
if obsolete.commonversion(remoteversions) is None: | ||||
return | ||||
pushop.stepsdone.add('obsmarkers') | ||||
if pushop.outobsmarkers: | ||||
Pierre-Yves David
|
r25118 | markers = sorted(pushop.outobsmarkers) | ||
buildobsmarkerspart(bundler, markers) | ||||
Pierre-Yves David
|
r22347 | |||
Pierre-Yves David
|
r22242 | @b2partsgenerator('bookmarks') | ||
def _pushb2bookmarks(pushop, bundler): | ||||
Martin von Zweigbergk
|
r25895 | """handle bookmark push through bundle2""" | ||
Pierre-Yves David
|
r22242 | if 'bookmarks' in pushop.stepsdone: | ||
return | ||||
b2caps = bundle2.bundle2caps(pushop.remote) | ||||
Pierre-Yves David
|
r24686 | if 'pushkey' not in b2caps: | ||
Pierre-Yves David
|
r22242 | return | ||
pushop.stepsdone.add('bookmarks') | ||||
part2book = [] | ||||
enc = pushkey.encode | ||||
Pierre-Yves David
|
r25501 | |||
def handlefailure(pushop, exc): | ||||
targetid = int(exc.partid) | ||||
for partid, book, action in part2book: | ||||
if partid == targetid: | ||||
raise error.Abort(bookmsgmap[action][1].rstrip() % book) | ||||
# we should not be called for part we did not generated | ||||
assert False | ||||
Pierre-Yves David
|
r22242 | for book, old, new in pushop.outbookmarks: | ||
Pierre-Yves David
|
r25501 | part = bundler.newpart('pushkey') | ||
Pierre-Yves David
|
r22242 | part.addparam('namespace', enc('bookmarks')) | ||
part.addparam('key', enc(book)) | ||||
part.addparam('old', enc(old)) | ||||
part.addparam('new', enc(new)) | ||||
Pierre-Yves David
|
r22650 | action = 'update' | ||
if not old: | ||||
action = 'export' | ||||
elif not new: | ||||
action = 'delete' | ||||
part2book.append((part.id, book, action)) | ||||
Pierre-Yves David
|
r25501 | pushop.pkfailcb[part.id] = handlefailure | ||
Pierre-Yves David
|
r22650 | |||
Pierre-Yves David
|
r22242 | def handlereply(op): | ||
Pierre-Yves David
|
r22650 | ui = pushop.ui | ||
for partid, book, action in part2book: | ||||
Pierre-Yves David
|
r22242 | partrep = op.records.getreplies(partid) | ||
results = partrep['pushkey'] | ||||
assert len(results) <= 1 | ||||
if not results: | ||||
pushop.ui.warn(_('server ignored bookmark %s update\n') % book) | ||||
else: | ||||
ret = int(results[0]['return']) | ||||
if ret: | ||||
Pierre-Yves David
|
r22650 | ui.status(bookmsgmap[action][0] % book) | ||
Pierre-Yves David
|
r22242 | else: | ||
Pierre-Yves David
|
r22650 | ui.warn(bookmsgmap[action][1] % book) | ||
Pierre-Yves David
|
r22649 | if pushop.bkresult is not None: | ||
pushop.bkresult = 1 | ||||
Pierre-Yves David
|
r22242 | return handlereply | ||
Pierre-Yves David
|
r21061 | def _pushbundle2(pushop): | ||
"""push data to the remote using bundle2 | ||||
The only currently supported type of data is changegroup but this will | ||||
evolve in the future.""" | ||||
Pierre-Yves David
|
r21644 | bundler = bundle2.bundle20(pushop.ui, bundle2.bundle2caps(pushop.remote)) | ||
Eric Sumner
|
r23439 | pushback = (pushop.trmanager | ||
and pushop.ui.configbool('experimental', 'bundle2.pushback')) | ||||
Pierre-Yves David
|
r21142 | # create reply capability | ||
Eric Sumner
|
r23439 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(pushop.repo, | ||
allowpushback=pushback)) | ||||
Pierre-Yves David
|
r24686 | bundler.newpart('replycaps', data=capsblob) | ||
Pierre-Yves David
|
r21904 | replyhandlers = [] | ||
Pierre-Yves David
|
r22017 | for partgenname in b2partsgenorder: | ||
partgen = b2partsgenmapping[partgenname] | ||||
Pierre-Yves David
|
r21904 | ret = partgen(pushop, bundler) | ||
Pierre-Yves David
|
r21941 | if callable(ret): | ||
replyhandlers.append(ret) | ||||
Pierre-Yves David
|
r21904 | # do not push if nothing to push | ||
Pierre-Yves David
|
r21903 | if bundler.nbparts <= 1: | ||
return | ||||
Pierre-Yves David
|
r21061 | stream = util.chunkbuffer(bundler.getchunks()) | ||
Pierre-Yves David
|
r21182 | try: | ||
Pierre-Yves David
|
r25485 | try: | ||
reply = pushop.remote.unbundle(stream, ['force'], 'push') | ||||
Gregory Szorc
|
r25660 | except error.BundleValueError as exc: | ||
Pierre-Yves David
|
r25485 | raise util.Abort('missing support for %s' % exc) | ||
try: | ||||
trgetter = None | ||||
if pushback: | ||||
trgetter = pushop.trmanager.transaction | ||||
op = bundle2.processbundle(pushop.repo, reply, trgetter) | ||||
Gregory Szorc
|
r25660 | except error.BundleValueError as exc: | ||
Pierre-Yves David
|
r25485 | raise util.Abort('missing support for %s' % exc) | ||
Gregory Szorc
|
r25660 | except error.PushkeyFailed as exc: | ||
Pierre-Yves David
|
r25485 | partid = int(exc.partid) | ||
if partid not in pushop.pkfailcb: | ||||
raise | ||||
pushop.pkfailcb[partid](pushop, exc) | ||||
Pierre-Yves David
|
r21904 | for rephand in replyhandlers: | ||
rephand(op) | ||||
Pierre-Yves David
|
r21061 | |||
Pierre-Yves David
|
r20463 | def _pushchangeset(pushop): | ||
"""Make the actual push of changeset bundle to remote repo""" | ||||
Pierre-Yves David
|
r21902 | if 'changesets' in pushop.stepsdone: | ||
return | ||||
pushop.stepsdone.add('changesets') | ||||
Pierre-Yves David
|
r21903 | if not _pushcheckoutgoing(pushop): | ||
return | ||||
pushop.repo.prepushoutgoinghooks(pushop.repo, | ||||
pushop.remote, | ||||
pushop.outgoing) | ||||
Pierre-Yves David
|
r20463 | outgoing = pushop.outgoing | ||
unbundle = pushop.remote.capable('unbundle') | ||||
# TODO: get bundlecaps from remote | ||||
bundlecaps = None | ||||
# create a changegroup from local | ||||
if pushop.revs is None and not (outgoing.excluded | ||||
or pushop.repo.changelog.filteredrevs): | ||||
# push everything, | ||||
# use the fast path, no race possible on push | ||||
Sune Foldager
|
r22390 | bundler = changegroup.cg1packer(pushop.repo, bundlecaps) | ||
Pierre-Yves David
|
r20925 | cg = changegroup.getsubset(pushop.repo, | ||
outgoing, | ||||
bundler, | ||||
'push', | ||||
fastpath=True) | ||||
Pierre-Yves David
|
r20463 | else: | ||
Sune Foldager
|
r22390 | cg = changegroup.getlocalchangegroup(pushop.repo, 'push', outgoing, | ||
Pierre-Yves David
|
r20928 | bundlecaps) | ||
Pierre-Yves David
|
r20463 | |||
# apply changegroup to remote | ||||
if unbundle: | ||||
# local repo finds heads on server, finds out what | ||||
# revs it must push. once revs transferred, if server | ||||
# finds it has different heads (someone else won | ||||
# commit/push race), server aborts. | ||||
if pushop.force: | ||||
remoteheads = ['force'] | ||||
else: | ||||
remoteheads = pushop.remoteheads | ||||
# ssh: return remote's addchangegroup() | ||||
# http: return remote's addchangegroup() or 0 for error | ||||
Pierre-Yves David
|
r22615 | pushop.cgresult = pushop.remote.unbundle(cg, remoteheads, | ||
Matt Mackall
|
r21761 | pushop.repo.url()) | ||
Pierre-Yves David
|
r20463 | else: | ||
# we return an integer indicating remote head count | ||||
# change | ||||
Pierre-Yves David
|
r22615 | pushop.cgresult = pushop.remote.addchangegroup(cg, 'push', | ||
pushop.repo.url()) | ||||
Pierre-Yves David
|
r20463 | |||
Pierre-Yves David
|
r20441 | def _pushsyncphase(pushop): | ||
Mads Kiilerich
|
r21024 | """synchronise phase information locally and remotely""" | ||
Pierre-Yves David
|
r20468 | cheads = pushop.commonheads | ||
Pierre-Yves David
|
r20441 | # even when we don't push, exchanging phase data is useful | ||
remotephases = pushop.remote.listkeys('phases') | ||||
if (pushop.ui.configbool('ui', '_usedassubrepo', False) | ||||
and remotephases # server supports phases | ||||
Pierre-Yves David
|
r22615 | and pushop.cgresult is None # nothing was pushed | ||
Pierre-Yves David
|
r20441 | and remotephases.get('publishing', False)): | ||
# When: | ||||
# - this is a subrepo push | ||||
# - and remote support phase | ||||
# - and no changeset was pushed | ||||
# - and remote is publishing | ||||
# We may be in issue 3871 case! | ||||
# We drop the possible phase synchronisation done by | ||||
# courtesy to publish changesets possibly locally draft | ||||
# on the remote. | ||||
remotephases = {'publishing': 'True'} | ||||
Pierre-Yves David
|
r21012 | if not remotephases: # old server or public only reply from non-publishing | ||
Pierre-Yves David
|
r20441 | _localphasemove(pushop, cheads) | ||
# don't push any phase data as there is nothing to push | ||||
else: | ||||
ana = phases.analyzeremotephases(pushop.repo, cheads, | ||||
remotephases) | ||||
pheads, droots = ana | ||||
### Apply remote phase on local | ||||
if remotephases.get('publishing', False): | ||||
_localphasemove(pushop, cheads) | ||||
else: # publish = False | ||||
_localphasemove(pushop, pheads) | ||||
_localphasemove(pushop, cheads, phases.draft) | ||||
### Apply local phase on remote | ||||
Pierre-Yves David
|
r22615 | if pushop.cgresult: | ||
Pierre-Yves David
|
r22020 | if 'phases' in pushop.stepsdone: | ||
# phases already pushed though bundle2 | ||||
return | ||||
Pierre-Yves David
|
r22019 | outdated = pushop.outdatedphases | ||
else: | ||||
outdated = pushop.fallbackoutdatedphases | ||||
Pierre-Yves David
|
r22020 | pushop.stepsdone.add('phases') | ||
Pierre-Yves David
|
r22019 | # filter heads already turned public by the push | ||
outdated = [c for c in outdated if c.node() not in pheads] | ||||
Pierre-Yves David
|
r23376 | # fallback to independent pushkey command | ||
for newremotehead in outdated: | ||||
r = pushop.remote.pushkey('phases', | ||||
newremotehead.hex(), | ||||
str(phases.draft), | ||||
str(phases.public)) | ||||
if not r: | ||||
pushop.ui.warn(_('updating %s to public failed!\n') | ||||
% newremotehead) | ||||
Pierre-Yves David
|
r20441 | |||
Pierre-Yves David
|
r20438 | def _localphasemove(pushop, nodes, phase=phases.public): | ||
"""move <nodes> to <phase> in the local source repo""" | ||||
Eric Sumner
|
r23437 | if pushop.trmanager: | ||
phases.advanceboundary(pushop.repo, | ||||
pushop.trmanager.transaction(), | ||||
phase, | ||||
nodes) | ||||
Pierre-Yves David
|
r20438 | else: | ||
# repo is not locked, do not change any phases! | ||||
# Informs the user that phases should have been moved when | ||||
# applicable. | ||||
actualmoves = [n for n in nodes if phase < pushop.repo[n].phase()] | ||||
phasestr = phases.phasenames[phase] | ||||
if actualmoves: | ||||
pushop.ui.status(_('cannot lock source repo, skipping ' | ||||
'local %s phase update\n') % phasestr) | ||||
Pierre-Yves David
|
r20433 | def _pushobsolete(pushop): | ||
Pierre-Yves David
|
r20434 | """utility function to push obsolete markers to a remote""" | ||
Pierre-Yves David
|
r22036 | if 'obsmarkers' in pushop.stepsdone: | ||
return | ||||
Pierre-Yves David
|
r20433 | repo = pushop.repo | ||
remote = pushop.remote | ||||
Pierre-Yves David
|
r22036 | pushop.stepsdone.add('obsmarkers') | ||
Pierre-Yves David
|
r22350 | if pushop.outobsmarkers: | ||
Pierre-Yves David
|
r25559 | pushop.ui.debug('try to push obsolete markers to remote\n') | ||
Pierre-Yves David
|
r20432 | rslts = [] | ||
Pierre-Yves David
|
r25118 | remotedata = obsolete._pushkeyescape(sorted(pushop.outobsmarkers)) | ||
Pierre-Yves David
|
r20432 | for key in sorted(remotedata, reverse=True): | ||
# reverse sort to ensure we end with dump0 | ||||
data = remotedata[key] | ||||
rslts.append(remote.pushkey('obsolete', key, '', data)) | ||||
if [r for r in rslts if not r]: | ||||
msg = _('failed to push some obsolete markers!\n') | ||||
repo.ui.warn(msg) | ||||
Pierre-Yves David
|
r20431 | def _pushbookmark(pushop): | ||
Pierre-Yves David
|
r20352 | """Update bookmark position on remote""" | ||
Pierre-Yves David
|
r22615 | if pushop.cgresult == 0 or 'bookmarks' in pushop.stepsdone: | ||
Pierre-Yves David
|
r22228 | return | ||
Pierre-Yves David
|
r22240 | pushop.stepsdone.add('bookmarks') | ||
Pierre-Yves David
|
r20431 | ui = pushop.ui | ||
remote = pushop.remote | ||||
Pierre-Yves David
|
r22650 | |||
Pierre-Yves David
|
r22239 | for b, old, new in pushop.outbookmarks: | ||
Pierre-Yves David
|
r22650 | action = 'update' | ||
if not old: | ||||
action = 'export' | ||||
elif not new: | ||||
action = 'delete' | ||||
Pierre-Yves David
|
r22239 | if remote.pushkey('bookmarks', b, old, new): | ||
Pierre-Yves David
|
r22650 | ui.status(bookmsgmap[action][0] % b) | ||
Pierre-Yves David
|
r20352 | else: | ||
Pierre-Yves David
|
r22650 | ui.warn(bookmsgmap[action][1] % b) | ||
# discovery can have set the value form invalid entry | ||||
if pushop.bkresult is not None: | ||||
pushop.bkresult = 1 | ||||
Pierre-Yves David
|
r20469 | |||
Pierre-Yves David
|
r20472 | class pulloperation(object): | ||
"""A object that represent a single pull operation | ||||
Mike Edgar
|
r23219 | It purpose is to carry pull related state and very common operation. | ||
Pierre-Yves David
|
r20472 | |||
Mads Kiilerich
|
r21024 | A new should be created at the beginning of each pull and discarded | ||
Pierre-Yves David
|
r20472 | afterward. | ||
""" | ||||
Pierre-Yves David
|
r25446 | def __init__(self, repo, remote, heads=None, force=False, bookmarks=(), | ||
remotebookmarks=None): | ||||
Siddharth Agarwal
|
r20596 | # repo we pull into | ||
Pierre-Yves David
|
r20472 | self.repo = repo | ||
Siddharth Agarwal
|
r20596 | # repo we pull from | ||
Pierre-Yves David
|
r20473 | self.remote = remote | ||
Pierre-Yves David
|
r20474 | # revision we try to pull (None is "all") | ||
self.heads = heads | ||||
Pierre-Yves David
|
r22654 | # bookmark pulled explicitly | ||
self.explicitbookmarks = bookmarks | ||||
Pierre-Yves David
|
r20475 | # do we force pull? | ||
self.force = force | ||||
Eric Sumner
|
r23436 | # transaction manager | ||
self.trmanager = None | ||||
Pierre-Yves David
|
r20487 | # set of common changeset between local and remote before pull | ||
self.common = None | ||||
# set of pulled head | ||||
self.rheads = None | ||||
Mads Kiilerich
|
r21024 | # list of missing changeset to fetch remotely | ||
Pierre-Yves David
|
r20488 | self.fetch = None | ||
Pierre-Yves David
|
r22654 | # remote bookmarks data | ||
Pierre-Yves David
|
r25446 | self.remotebookmarks = remotebookmarks | ||
Mads Kiilerich
|
r21024 | # result of changegroup pulling (used as return code by pull) | ||
Pierre-Yves David
|
r20898 | self.cgresult = None | ||
Pierre-Yves David
|
r22937 | # list of step already done | ||
self.stepsdone = set() | ||||
Pierre-Yves David
|
r20487 | |||
@util.propertycache | ||||
def pulledsubset(self): | ||||
"""heads of the set of changeset target by the pull""" | ||||
# compute target subset | ||||
if self.heads is None: | ||||
# We pulled every thing possible | ||||
# sync on everything common | ||||
Pierre-Yves David
|
r20878 | c = set(self.common) | ||
ret = list(self.common) | ||||
for n in self.rheads: | ||||
if n not in c: | ||||
ret.append(n) | ||||
return ret | ||||
Pierre-Yves David
|
r20487 | else: | ||
# We pulled a specific subset | ||||
# sync on this subset | ||||
return self.heads | ||||
Pierre-Yves David
|
r20477 | |||
def gettransaction(self): | ||||
Eric Sumner
|
r23436 | # deprecated; talk to trmanager directly | ||
return self.trmanager.transaction() | ||||
class transactionmanager(object): | ||||
Mads Kiilerich
|
r23543 | """An object to manage the life cycle of a transaction | ||
Eric Sumner
|
r23436 | |||
It creates the transaction on demand and calls the appropriate hooks when | ||||
closing the transaction.""" | ||||
def __init__(self, repo, source, url): | ||||
self.repo = repo | ||||
self.source = source | ||||
self.url = url | ||||
self._tr = None | ||||
def transaction(self): | ||||
"""Return an open transaction object, constructing if necessary""" | ||||
if not self._tr: | ||||
trname = '%s\n%s' % (self.source, util.hidepassword(self.url)) | ||||
self._tr = self.repo.transaction(trname) | ||||
self._tr.hookargs['source'] = self.source | ||||
self._tr.hookargs['url'] = self.url | ||||
Pierre-Yves David
|
r20477 | return self._tr | ||
Eric Sumner
|
r23436 | def close(self): | ||
Pierre-Yves David
|
r20477 | """close transaction if created""" | ||
if self._tr is not None: | ||||
Pierre-Yves David
|
r23222 | self._tr.close() | ||
Pierre-Yves David
|
r20477 | |||
Eric Sumner
|
r23436 | def release(self): | ||
Pierre-Yves David
|
r20477 | """release transaction if created""" | ||
if self._tr is not None: | ||||
self._tr.release() | ||||
Pierre-Yves David
|
r20469 | |||
Pierre-Yves David
|
r25445 | def pull(repo, remote, heads=None, force=False, bookmarks=(), opargs=None): | ||
if opargs is None: | ||||
opargs = {} | ||||
pullop = pulloperation(repo, remote, heads, force, bookmarks=bookmarks, | ||||
**opargs) | ||||
Pierre-Yves David
|
r20473 | if pullop.remote.local(): | ||
missing = set(pullop.remote.requirements) - pullop.repo.supported | ||||
Pierre-Yves David
|
r20469 | if missing: | ||
msg = _("required features are not" | ||||
" supported in the destination:" | ||||
" %s") % (', '.join(sorted(missing))) | ||||
raise util.Abort(msg) | ||||
Pierre-Yves David
|
r20472 | lock = pullop.repo.lock() | ||
Pierre-Yves David
|
r20469 | try: | ||
Eric Sumner
|
r23436 | pullop.trmanager = transactionmanager(repo, 'pull', remote.url()) | ||
Pierre-Yves David
|
r20900 | _pulldiscovery(pullop) | ||
Pierre-Yves David
|
r24650 | if _canusebundle2(pullop): | ||
Pierre-Yves David
|
r20955 | _pullbundle2(pullop) | ||
Pierre-Yves David
|
r22653 | _pullchangeset(pullop) | ||
_pullphase(pullop) | ||||
Pierre-Yves David
|
r22655 | _pullbookmarks(pullop) | ||
Pierre-Yves David
|
r22653 | _pullobsolete(pullop) | ||
Eric Sumner
|
r23436 | pullop.trmanager.close() | ||
Pierre-Yves David
|
r20469 | finally: | ||
Eric Sumner
|
r23436 | pullop.trmanager.release() | ||
Pierre-Yves David
|
r20469 | lock.release() | ||
Pierre-Yves David
|
r22693 | return pullop | ||
Pierre-Yves David
|
r20476 | |||
Pierre-Yves David
|
r22936 | # list of steps to perform discovery before pull | ||
pulldiscoveryorder = [] | ||||
# Mapping between step name and function | ||||
# | ||||
# This exists to help extensions wrap steps if necessary | ||||
pulldiscoverymapping = {} | ||||
def pulldiscovery(stepname): | ||||
"""decorator for function performing discovery before pull | ||||
The function is added to the step -> function mapping and appended to the | ||||
list of steps. Beware that decorated function will be added in order (this | ||||
may matter). | ||||
You can only use this decorator for a new step, if you want to wrap a step | ||||
from an extension, change the pulldiscovery dictionary directly.""" | ||||
def dec(func): | ||||
assert stepname not in pulldiscoverymapping | ||||
pulldiscoverymapping[stepname] = func | ||||
pulldiscoveryorder.append(stepname) | ||||
return func | ||||
return dec | ||||
Pierre-Yves David
|
r20900 | def _pulldiscovery(pullop): | ||
Pierre-Yves David
|
r22936 | """Run all discovery steps""" | ||
for stepname in pulldiscoveryorder: | ||||
step = pulldiscoverymapping[stepname] | ||||
step(pullop) | ||||
Pierre-Yves David
|
r25369 | @pulldiscovery('b1:bookmarks') | ||
def _pullbookmarkbundle1(pullop): | ||||
"""fetch bookmark data in bundle1 case | ||||
If not using bundle2, we have to fetch bookmarks before changeset | ||||
discovery to reduce the chance and impact of race conditions.""" | ||||
Pierre-Yves David
|
r25443 | if pullop.remotebookmarks is not None: | ||
return | ||||
Pierre-Yves David
|
r25479 | if (_canusebundle2(pullop) | ||
and 'listkeys' in bundle2.bundle2caps(pullop.remote)): | ||||
# all known bundle2 servers now support listkeys, but lets be nice with | ||||
# new implementation. | ||||
return | ||||
pullop.remotebookmarks = pullop.remote.listkeys('bookmarks') | ||||
Pierre-Yves David
|
r25369 | |||
Pierre-Yves David
|
r22936 | @pulldiscovery('changegroup') | ||
def _pulldiscoverychangegroup(pullop): | ||||
Pierre-Yves David
|
r20900 | """discovery phase for the pull | ||
Current handle changeset discovery only, will change handle all discovery | ||||
at some point.""" | ||||
Pierre-Yves David
|
r23848 | tmp = discovery.findcommonincoming(pullop.repo, | ||
Pierre-Yves David
|
r20900 | pullop.remote, | ||
heads=pullop.heads, | ||||
force=pullop.force) | ||||
Pierre-Yves David
|
r23848 | common, fetch, rheads = tmp | ||
nm = pullop.repo.unfiltered().changelog.nodemap | ||||
if fetch and rheads: | ||||
# If a remote heads in filtered locally, lets drop it from the unknown | ||||
# remote heads and put in back in common. | ||||
# | ||||
# This is a hackish solution to catch most of "common but locally | ||||
# hidden situation". We do not performs discovery on unfiltered | ||||
# repository because it end up doing a pathological amount of round | ||||
# trip for w huge amount of changeset we do not care about. | ||||
# | ||||
# If a set of such "common but filtered" changeset exist on the server | ||||
# but are not including a remote heads, we'll not be able to detect it, | ||||
scommon = set(common) | ||||
filteredrheads = [] | ||||
for n in rheads: | ||||
Pierre-Yves David
|
r23975 | if n in nm: | ||
if n not in scommon: | ||||
common.append(n) | ||||
Pierre-Yves David
|
r23848 | else: | ||
filteredrheads.append(n) | ||||
if not filteredrheads: | ||||
fetch = [] | ||||
rheads = filteredrheads | ||||
pullop.common = common | ||||
pullop.fetch = fetch | ||||
pullop.rheads = rheads | ||||
Pierre-Yves David
|
r20900 | |||
Pierre-Yves David
|
r20955 | def _pullbundle2(pullop): | ||
"""pull data using bundle2 | ||||
For now, the only supported data are changegroup.""" | ||||
Pierre-Yves David
|
r21658 | remotecaps = bundle2.bundle2caps(pullop.remote) | ||
Pierre-Yves David
|
r21645 | kwargs = {'bundlecaps': caps20to10(pullop.repo)} | ||
Pierre-Yves David
|
r20955 | # pulling changegroup | ||
Pierre-Yves David
|
r22937 | pullop.stepsdone.add('changegroup') | ||
Durham Goode
|
r21259 | |||
kwargs['common'] = pullop.common | ||||
kwargs['heads'] = pullop.heads or pullop.rheads | ||||
Pierre-Yves David
|
r22352 | kwargs['cg'] = pullop.fetch | ||
Pierre-Yves David
|
r24686 | if 'listkeys' in remotecaps: | ||
Pierre-Yves David
|
r25444 | kwargs['listkeys'] = ['phase'] | ||
if pullop.remotebookmarks is None: | ||||
# make sure to always includes bookmark data when migrating | ||||
# `hg incoming --bundle` to using this function. | ||||
kwargs['listkeys'].append('bookmarks') | ||||
Pierre-Yves David
|
r20955 | if not pullop.fetch: | ||
Pierre-Yves David
|
r21258 | pullop.repo.ui.status(_("no changes found\n")) | ||
pullop.cgresult = 0 | ||||
Pierre-Yves David
|
r20955 | else: | ||
if pullop.heads is None and list(pullop.common) == [nullid]: | ||||
pullop.repo.ui.status(_("requesting all changes\n")) | ||||
Durham Goode
|
r22953 | if obsolete.isenabled(pullop.repo, obsolete.exchangeopt): | ||
Pierre-Yves David
|
r22354 | remoteversions = bundle2.obsmarkersversion(remotecaps) | ||
if obsolete.commonversion(remoteversions) is not None: | ||||
kwargs['obsmarkers'] = True | ||||
Pierre-Yves David
|
r22937 | pullop.stepsdone.add('obsmarkers') | ||
Pierre-Yves David
|
r21159 | _pullbundle2extraprepare(pullop, kwargs) | ||
Pierre-Yves David
|
r20955 | bundle = pullop.remote.getbundle('pull', **kwargs) | ||
try: | ||||
op = bundle2.processbundle(pullop.repo, bundle, pullop.gettransaction) | ||||
Gregory Szorc
|
r25660 | except error.BundleValueError as exc: | ||
Pierre-Yves David
|
r20955 | raise util.Abort('missing support for %s' % exc) | ||
Durham Goode
|
r21259 | |||
if pullop.fetch: | ||||
Eric Sumner
|
r23890 | results = [cg['return'] for cg in op.records['changegroup']] | ||
pullop.cgresult = changegroup.combineresults(results) | ||||
Pierre-Yves David
|
r20955 | |||
Pierre-Yves David
|
r21658 | # processing phases change | ||
for namespace, value in op.records['listkeys']: | ||||
if namespace == 'phases': | ||||
_pullapplyphases(pullop, value) | ||||
Pierre-Yves David
|
r22656 | # processing bookmark update | ||
for namespace, value in op.records['listkeys']: | ||||
if namespace == 'bookmarks': | ||||
pullop.remotebookmarks = value | ||||
Pierre-Yves David
|
r25444 | |||
# bookmark data were either already there or pulled in the bundle | ||||
if pullop.remotebookmarks is not None: | ||||
_pullbookmarks(pullop) | ||||
Pierre-Yves David
|
r22656 | |||
Pierre-Yves David
|
r21159 | def _pullbundle2extraprepare(pullop, kwargs): | ||
"""hook function so that extensions can extend the getbundle call""" | ||||
pass | ||||
Pierre-Yves David
|
r20489 | def _pullchangeset(pullop): | ||
"""pull changeset from unbundle into the local repo""" | ||||
# We delay the open of the transaction as late as possible so we | ||||
# don't open transaction for nothing or you break future useful | ||||
# rollback call | ||||
Pierre-Yves David
|
r22937 | if 'changegroup' in pullop.stepsdone: | ||
Pierre-Yves David
|
r22653 | return | ||
Pierre-Yves David
|
r22937 | pullop.stepsdone.add('changegroup') | ||
Pierre-Yves David
|
r20899 | if not pullop.fetch: | ||
Mike Edgar
|
r23217 | pullop.repo.ui.status(_("no changes found\n")) | ||
pullop.cgresult = 0 | ||||
return | ||||
Pierre-Yves David
|
r20489 | pullop.gettransaction() | ||
if pullop.heads is None and list(pullop.common) == [nullid]: | ||||
pullop.repo.ui.status(_("requesting all changes\n")) | ||||
elif pullop.heads is None and pullop.remote.capable('changegroupsubset'): | ||||
# issue1320, avoid a race if remote changed after discovery | ||||
pullop.heads = pullop.rheads | ||||
if pullop.remote.capable('getbundle'): | ||||
# TODO: get bundlecaps from remote | ||||
cg = pullop.remote.getbundle('pull', common=pullop.common, | ||||
heads=pullop.heads or pullop.rheads) | ||||
elif pullop.heads is None: | ||||
cg = pullop.remote.changegroup(pullop.fetch, 'pull') | ||||
elif not pullop.remote.capable('changegroupsubset'): | ||||
raise util.Abort(_("partial pull cannot be done because " | ||||
Pierre-Yves David
|
r21554 | "other repository doesn't support " | ||
"changegroupsubset.")) | ||||
Pierre-Yves David
|
r20489 | else: | ||
cg = pullop.remote.changegroupsubset(pullop.fetch, pullop.heads, 'pull') | ||||
Pierre-Yves David
|
r20933 | pullop.cgresult = changegroup.addchangegroup(pullop.repo, cg, 'pull', | ||
Pierre-Yves David
|
r20899 | pullop.remote.url()) | ||
Pierre-Yves David
|
r20489 | |||
Pierre-Yves David
|
r20486 | def _pullphase(pullop): | ||
# Get remote phases data from remote | ||||
Pierre-Yves David
|
r22937 | if 'phases' in pullop.stepsdone: | ||
Pierre-Yves David
|
r22653 | return | ||
Pierre-Yves David
|
r21654 | remotephases = pullop.remote.listkeys('phases') | ||
_pullapplyphases(pullop, remotephases) | ||||
def _pullapplyphases(pullop, remotephases): | ||||
"""apply phase movement from observed remote state""" | ||||
Pierre-Yves David
|
r22937 | if 'phases' in pullop.stepsdone: | ||
return | ||||
pullop.stepsdone.add('phases') | ||||
Pierre-Yves David
|
r20486 | publishing = bool(remotephases.get('publishing', False)) | ||
if remotephases and not publishing: | ||||
# remote is new and unpublishing | ||||
pheads, _dr = phases.analyzeremotephases(pullop.repo, | ||||
pullop.pulledsubset, | ||||
remotephases) | ||||
Pierre-Yves David
|
r22068 | dheads = pullop.pulledsubset | ||
Pierre-Yves David
|
r20486 | else: | ||
# Remote is old or publishing all common changesets | ||||
# should be seen as public | ||||
Pierre-Yves David
|
r22068 | pheads = pullop.pulledsubset | ||
dheads = [] | ||||
unfi = pullop.repo.unfiltered() | ||||
phase = unfi._phasecache.phase | ||||
rev = unfi.changelog.nodemap.get | ||||
public = phases.public | ||||
draft = phases.draft | ||||
# exclude changesets already public locally and update the others | ||||
pheads = [pn for pn in pheads if phase(unfi, rev(pn)) > public] | ||||
if pheads: | ||||
Pierre-Yves David
|
r22069 | tr = pullop.gettransaction() | ||
phases.advanceboundary(pullop.repo, tr, public, pheads) | ||||
Pierre-Yves David
|
r22068 | |||
# exclude changesets already draft locally and update the others | ||||
dheads = [pn for pn in dheads if phase(unfi, rev(pn)) > draft] | ||||
if dheads: | ||||
Pierre-Yves David
|
r22069 | tr = pullop.gettransaction() | ||
phases.advanceboundary(pullop.repo, tr, draft, dheads) | ||||
Pierre-Yves David
|
r20486 | |||
Pierre-Yves David
|
r22654 | def _pullbookmarks(pullop): | ||
"""process the remote bookmark information to update the local one""" | ||||
Pierre-Yves David
|
r22937 | if 'bookmarks' in pullop.stepsdone: | ||
Pierre-Yves David
|
r22654 | return | ||
Pierre-Yves David
|
r22937 | pullop.stepsdone.add('bookmarks') | ||
Pierre-Yves David
|
r22654 | repo = pullop.repo | ||
remotebookmarks = pullop.remotebookmarks | ||||
bookmod.updatefromremote(repo.ui, repo, remotebookmarks, | ||||
Pierre-Yves David
|
r22658 | pullop.remote.url(), | ||
Pierre-Yves David
|
r22666 | pullop.gettransaction, | ||
Pierre-Yves David
|
r22658 | explicit=pullop.explicitbookmarks) | ||
Pierre-Yves David
|
r22654 | |||
Pierre-Yves David
|
r20478 | def _pullobsolete(pullop): | ||
Pierre-Yves David
|
r20476 | """utility function to pull obsolete markers from a remote | ||
The `gettransaction` is function that return the pull transaction, creating | ||||
one if necessary. We return the transaction to inform the calling code that | ||||
a new transaction have been created (when applicable). | ||||
Exists mostly to allow overriding for experimentation purpose""" | ||||
Pierre-Yves David
|
r22937 | if 'obsmarkers' in pullop.stepsdone: | ||
Pierre-Yves David
|
r22653 | return | ||
Pierre-Yves David
|
r22937 | pullop.stepsdone.add('obsmarkers') | ||
Pierre-Yves David
|
r20476 | tr = None | ||
Durham Goode
|
r22953 | if obsolete.isenabled(pullop.repo, obsolete.exchangeopt): | ||
Pierre-Yves David
|
r20478 | pullop.repo.ui.debug('fetching remote obsolete markers\n') | ||
remoteobs = pullop.remote.listkeys('obsolete') | ||||
Pierre-Yves David
|
r20476 | if 'dump0' in remoteobs: | ||
Pierre-Yves David
|
r20478 | tr = pullop.gettransaction() | ||
Pierre-Yves David
|
r20476 | for key in sorted(remoteobs, reverse=True): | ||
if key.startswith('dump'): | ||||
data = base85.b85decode(remoteobs[key]) | ||||
Pierre-Yves David
|
r20478 | pullop.repo.obsstore.mergemarkers(tr, data) | ||
pullop.repo.invalidatevolatilesets() | ||||
Pierre-Yves David
|
r20476 | return tr | ||
Pierre-Yves David
|
r21645 | def caps20to10(repo): | ||
"""return a set with appropriate options to use bundle20 during getbundle""" | ||||
Pierre-Yves David
|
r24686 | caps = set(['HG20']) | ||
Pierre-Yves David
|
r22342 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo)) | ||
Pierre-Yves David
|
r21645 | caps.add('bundle2=' + urllib.quote(capsblob)) | ||
return caps | ||||
Mike Hommey
|
r22542 | # List of names of steps to perform for a bundle2 for getbundle, order matters. | ||
getbundle2partsorder = [] | ||||
# Mapping between step name and function | ||||
# | ||||
# This exists to help extensions wrap steps if necessary | ||||
getbundle2partsmapping = {} | ||||
Pierre-Yves David
|
r24732 | def getbundle2partsgenerator(stepname, idx=None): | ||
Mike Hommey
|
r22542 | """decorator for function generating bundle2 part for getbundle | ||
The function is added to the step -> function mapping and appended to the | ||||
list of steps. Beware that decorated functions will be added in order | ||||
(this may matter). | ||||
You can only use this decorator for new steps, if you want to wrap a step | ||||
from an extension, attack the getbundle2partsmapping dictionary directly.""" | ||||
def dec(func): | ||||
assert stepname not in getbundle2partsmapping | ||||
getbundle2partsmapping[stepname] = func | ||||
Pierre-Yves David
|
r24732 | if idx is None: | ||
getbundle2partsorder.append(stepname) | ||||
else: | ||||
getbundle2partsorder.insert(idx, stepname) | ||||
Mike Hommey
|
r22542 | return func | ||
return dec | ||||
Pierre-Yves David
|
r21157 | def getbundle(repo, source, heads=None, common=None, bundlecaps=None, | ||
**kwargs): | ||||
Pierre-Yves David
|
r20954 | """return a full bundle (with potentially multiple kind of parts) | ||
Pierre-Yves David
|
r24686 | Could be a bundle HG10 or a bundle HG20 depending on bundlecaps | ||
Pierre-Yves David
|
r20954 | passed. For now, the bundle can contain only changegroup, but this will | ||
changes when more part type will be available for bundle2. | ||||
Sune Foldager
|
r22390 | This is different from changegroup.getchangegroup that only returns an HG10 | ||
Pierre-Yves David
|
r20954 | changegroup bundle. They may eventually get reunited in the future when we | ||
have a clearer idea of the API we what to query different data. | ||||
The implementation is at a very early stage and will get massive rework | ||||
when the API of bundle is refined. | ||||
""" | ||||
Mike Hommey
|
r22542 | # bundle10 case | ||
Pierre-Yves David
|
r24649 | usebundle2 = False | ||
if bundlecaps is not None: | ||||
Augie Fackler
|
r25149 | usebundle2 = any((cap.startswith('HG2') for cap in bundlecaps)) | ||
Pierre-Yves David
|
r24649 | if not usebundle2: | ||
Mike Hommey
|
r22542 | if bundlecaps and not kwargs.get('cg', True): | ||
raise ValueError(_('request for bundle10 must include changegroup')) | ||||
Pierre-Yves David
|
r21656 | if kwargs: | ||
raise ValueError(_('unsupported getbundle arguments: %s') | ||||
% ', '.join(sorted(kwargs.keys()))) | ||||
Mike Hommey
|
r22542 | return changegroup.getchangegroup(repo, source, heads=heads, | ||
common=common, bundlecaps=bundlecaps) | ||||
# bundle20 case | ||||
Pierre-Yves David
|
r21143 | b2caps = {} | ||
for bcaps in bundlecaps: | ||||
if bcaps.startswith('bundle2='): | ||||
blob = urllib.unquote(bcaps[len('bundle2='):]) | ||||
b2caps.update(bundle2.decodecaps(blob)) | ||||
bundler = bundle2.bundle20(repo.ui, b2caps) | ||||
Mike Hommey
|
r22542 | |||
Mike Edgar
|
r23218 | kwargs['heads'] = heads | ||
kwargs['common'] = common | ||||
Mike Hommey
|
r22542 | for name in getbundle2partsorder: | ||
func = getbundle2partsmapping[name] | ||||
Mike Hommey
|
r22543 | func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps, | ||
**kwargs) | ||||
Mike Hommey
|
r22542 | |||
return util.chunkbuffer(bundler.getchunks()) | ||||
@getbundle2partsgenerator('changegroup') | ||||
Mike Hommey
|
r22543 | def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, heads=None, common=None, **kwargs): | ||||
Mike Hommey
|
r22542 | """add a changegroup part to the requested bundle""" | ||
cg = None | ||||
if kwargs.get('cg', True): | ||||
# build changegroup bundle here. | ||||
Pierre-Yves David
|
r23179 | version = None | ||
Pierre-Yves David
|
r24686 | cgversions = b2caps.get('changegroup') | ||
Pierre-Yves David
|
r25503 | getcgkwargs = {} | ||
if cgversions: # 3.1 and 3.2 ship with an empty value | ||||
Pierre-Yves David
|
r23179 | cgversions = [v for v in cgversions if v in changegroup.packermap] | ||
if not cgversions: | ||||
raise ValueError(_('no common changegroup version')) | ||||
Pierre-Yves David
|
r25503 | version = getcgkwargs['version'] = max(cgversions) | ||
Pierre-Yves David
|
r25504 | outgoing = changegroup.computeoutgoing(repo, heads, common) | ||
cg = changegroup.getlocalchangegroupraw(repo, source, outgoing, | ||||
bundlecaps=bundlecaps, | ||||
**getcgkwargs) | ||||
Mike Hommey
|
r22542 | |||
Durham Goode
|
r21259 | if cg: | ||
Pierre-Yves David
|
r24686 | part = bundler.newpart('changegroup', data=cg) | ||
Pierre-Yves David
|
r23179 | if version is not None: | ||
part.addparam('version', version) | ||||
Pierre-Yves David
|
r25516 | part.addparam('nbchanges', str(len(outgoing.missing)), mandatory=False) | ||
Mike Hommey
|
r22542 | |||
@getbundle2partsgenerator('listkeys') | ||||
Mike Hommey
|
r22543 | def _getbundlelistkeysparts(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, **kwargs): | ||||
Mike Hommey
|
r22542 | """add parts containing listkeys namespaces to the requested bundle""" | ||
Pierre-Yves David
|
r21657 | listkeys = kwargs.get('listkeys', ()) | ||
for namespace in listkeys: | ||||
Pierre-Yves David
|
r24686 | part = bundler.newpart('listkeys') | ||
Pierre-Yves David
|
r21657 | part.addparam('namespace', namespace) | ||
keys = repo.listkeys(namespace).items() | ||||
part.data = pushkey.encodekeys(keys) | ||||
Pierre-Yves David
|
r20967 | |||
Mike Hommey
|
r22542 | @getbundle2partsgenerator('obsmarkers') | ||
Mike Hommey
|
r22543 | def _getbundleobsmarkerpart(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, heads=None, **kwargs): | ||||
Mike Hommey
|
r22541 | """add an obsolescence markers part to the requested bundle""" | ||
Pierre-Yves David
|
r22353 | if kwargs.get('obsmarkers', False): | ||
if heads is None: | ||||
heads = repo.heads() | ||||
subset = [c.node() for c in repo.set('::%ln', heads)] | ||||
markers = repo.obsstore.relevantmarkers(subset) | ||||
Pierre-Yves David
|
r25118 | markers = sorted(markers) | ||
Pierre-Yves David
|
r22353 | buildobsmarkerspart(bundler, markers) | ||
Gregory Szorc
|
r25402 | @getbundle2partsgenerator('hgtagsfnodes') | ||
def _getbundletagsfnodes(bundler, repo, source, bundlecaps=None, | ||||
b2caps=None, heads=None, common=None, | ||||
**kwargs): | ||||
"""Transfer the .hgtags filenodes mapping. | ||||
Only values for heads in this bundle will be transferred. | ||||
The part data consists of pairs of 20 byte changeset node and .hgtags | ||||
filenodes raw values. | ||||
""" | ||||
# Don't send unless: | ||||
# - changeset are being exchanged, | ||||
# - the client supports it. | ||||
if not (kwargs.get('cg', True) and 'hgtagsfnodes' in b2caps): | ||||
return | ||||
outgoing = changegroup.computeoutgoing(repo, heads, common) | ||||
if not outgoing.missingheads: | ||||
return | ||||
cache = tags.hgtagsfnodescache(repo.unfiltered()) | ||||
chunks = [] | ||||
# .hgtags fnodes are only relevant for head changesets. While we could | ||||
# transfer values for all known nodes, there will likely be little to | ||||
# no benefit. | ||||
# | ||||
# We don't bother using a generator to produce output data because | ||||
# a) we only have 40 bytes per head and even esoteric numbers of heads | ||||
# consume little memory (1M heads is 40MB) b) we don't want to send the | ||||
# part if we don't have entries and knowing if we have entries requires | ||||
# cache lookups. | ||||
for node in outgoing.missingheads: | ||||
# Don't compute missing, as this may slow down serving. | ||||
fnode = cache.getfnode(node, computemissing=False) | ||||
if fnode is not None: | ||||
chunks.extend([node, fnode]) | ||||
if chunks: | ||||
bundler.newpart('hgtagsfnodes', data=''.join(chunks)) | ||||
Pierre-Yves David
|
r20967 | def check_heads(repo, their_heads, context): | ||
"""check if the heads of a repo have been modified | ||||
Used by peer for unbundling. | ||||
""" | ||||
heads = repo.heads() | ||||
heads_hash = util.sha1(''.join(sorted(heads))).digest() | ||||
if not (their_heads == ['force'] or their_heads == heads or | ||||
their_heads == ['hashed', heads_hash]): | ||||
# someone else committed/pushed/unbundled while we | ||||
# were transferring data | ||||
Pierre-Yves David
|
r21184 | raise error.PushRaced('repository changed while %s - ' | ||
'please try again' % context) | ||||
Pierre-Yves David
|
r20968 | |||
def unbundle(repo, cg, heads, source, url): | ||||
"""Apply a bundle to a repo. | ||||
this function makes sure the repo is locked during the application and have | ||||
Mads Kiilerich
|
r21024 | mechanism to check that no push race occurred between the creation of the | ||
Pierre-Yves David
|
r20968 | bundle and its application. | ||
If the push was raced as PushRaced exception is raised.""" | ||||
r = 0 | ||||
Pierre-Yves David
|
r21061 | # need a transaction when processing a bundle2 stream | ||
Pierre-Yves David
|
r24752 | wlock = lock = tr = None | ||
Pierre-Yves David
|
r24847 | recordout = None | ||
Pierre-Yves David
|
r24878 | # quick fix for output mismatch with bundle2 in 3.4 | ||
captureoutput = repo.ui.configbool('experimental', 'bundle2-output-capture', | ||||
False) | ||||
Pierre-Yves David
|
r25423 | if url.startswith('remote:http:') or url.startswith('remote:https:'): | ||
Pierre-Yves David
|
r24878 | captureoutput = True | ||
Pierre-Yves David
|
r20968 | try: | ||
check_heads(repo, heads, 'uploading changes') | ||||
# push can proceed | ||||
Pierre-Yves David
|
r21061 | if util.safehasattr(cg, 'params'): | ||
Pierre-Yves David
|
r24795 | r = None | ||
Pierre-Yves David
|
r21187 | try: | ||
Pierre-Yves David
|
r24752 | wlock = repo.wlock() | ||
lock = repo.lock() | ||||
Pierre-Yves David
|
r24738 | tr = repo.transaction(source) | ||
Pierre-Yves David
|
r22971 | tr.hookargs['source'] = source | ||
tr.hookargs['url'] = url | ||||
Pierre-Yves David
|
r24686 | tr.hookargs['bundle2'] = '1' | ||
Pierre-Yves David
|
r24878 | op = bundle2.bundleoperation(repo, lambda: tr, | ||
captureoutput=captureoutput) | ||||
Pierre-Yves David
|
r24851 | try: | ||
Martin von Zweigbergk
|
r25896 | op = bundle2.processbundle(repo, cg, op=op) | ||
Pierre-Yves David
|
r24851 | finally: | ||
r = op.reply | ||||
Pierre-Yves David
|
r24878 | if captureoutput and r is not None: | ||
Pierre-Yves David
|
r24851 | repo.ui.pushbuffer(error=True, subproc=True) | ||
def recordout(output): | ||||
r.newpart('output', data=output, mandatory=False) | ||||
Pierre-Yves David
|
r23222 | tr.close() | ||
Gregory Szorc
|
r25660 | except BaseException as exc: | ||
Pierre-Yves David
|
r21187 | exc.duringunbundle2 = True | ||
Pierre-Yves David
|
r24878 | if captureoutput and r is not None: | ||
Pierre-Yves David
|
r24847 | parts = exc._bundle2salvagedoutput = r.salvageoutput() | ||
def recordout(output): | ||||
part = bundle2.bundlepart('output', data=output, | ||||
mandatory=False) | ||||
parts.append(part) | ||||
Pierre-Yves David
|
r21187 | raise | ||
Pierre-Yves David
|
r21061 | else: | ||
Pierre-Yves David
|
r24752 | lock = repo.lock() | ||
Pierre-Yves David
|
r21061 | r = changegroup.addchangegroup(repo, cg, source, url) | ||
Pierre-Yves David
|
r20968 | finally: | ||
Pierre-Yves David
|
r24752 | lockmod.release(tr, lock, wlock) | ||
Pierre-Yves David
|
r24847 | if recordout is not None: | ||
recordout(repo.ui.popbuffer()) | ||||
Pierre-Yves David
|
r20968 | return r | ||
Gregory Szorc
|
r25235 | |||
# This is it's own function so extensions can override it. | ||||
def _walkstreamfiles(repo): | ||||
return repo.store.walk() | ||||
def generatestreamclone(repo): | ||||
"""Emit content for a streaming clone. | ||||
This is a generator of raw chunks that constitute a streaming clone. | ||||
The stream begins with a line of 2 space-delimited integers containing the | ||||
number of entries and total bytes size. | ||||
Next, are N entries for each file being transferred. Each file entry starts | ||||
as a line with the file name and integer size delimited by a null byte. | ||||
The raw file data follows. Following the raw file data is the next file | ||||
entry, or EOF. | ||||
When used on the wire protocol, an additional line indicating protocol | ||||
success will be prepended to the stream. This function is not responsible | ||||
for adding it. | ||||
This function will obtain a repository lock to ensure a consistent view of | ||||
the store is captured. It therefore may raise LockError. | ||||
""" | ||||
entries = [] | ||||
total_bytes = 0 | ||||
# Get consistent snapshot of repo, lock during scan. | ||||
lock = repo.lock() | ||||
try: | ||||
repo.ui.debug('scanning\n') | ||||
for name, ename, size in _walkstreamfiles(repo): | ||||
if size: | ||||
entries.append((name, size)) | ||||
total_bytes += size | ||||
finally: | ||||
lock.release() | ||||
repo.ui.debug('%d files, %d bytes to transfer\n' % | ||||
(len(entries), total_bytes)) | ||||
yield '%d %d\n' % (len(entries), total_bytes) | ||||
Siddharth Agarwal
|
r25668 | svfs = repo.svfs | ||
oldaudit = svfs.mustaudit | ||||
Gregory Szorc
|
r25235 | debugflag = repo.ui.debugflag | ||
Siddharth Agarwal
|
r25668 | svfs.mustaudit = False | ||
Gregory Szorc
|
r25235 | |||
try: | ||||
for name, size in entries: | ||||
if debugflag: | ||||
repo.ui.debug('sending %s (%d bytes)\n' % (name, size)) | ||||
# partially encode name over the wire for backwards compat | ||||
yield '%s\0%d\n' % (store.encodedir(name), size) | ||||
if size <= 65536: | ||||
Siddharth Agarwal
|
r25668 | fp = svfs(name) | ||
Gregory Szorc
|
r25235 | try: | ||
data = fp.read(size) | ||||
finally: | ||||
fp.close() | ||||
yield data | ||||
else: | ||||
Siddharth Agarwal
|
r25668 | for chunk in util.filechunkiter(svfs(name), limit=size): | ||
Gregory Szorc
|
r25235 | yield chunk | ||
finally: | ||||
Siddharth Agarwal
|
r25668 | svfs.mustaudit = oldaudit | ||
Gregory Szorc
|
r25236 | |||
def consumestreamclone(repo, fp): | ||||
"""Apply the contents from a streaming clone file. | ||||
This takes the output from "streamout" and applies it to the specified | ||||
repository. | ||||
Like "streamout," the status line added by the wire protocol is not handled | ||||
by this function. | ||||
""" | ||||
lock = repo.lock() | ||||
try: | ||||
repo.ui.status(_('streaming all changes\n')) | ||||
l = fp.readline() | ||||
try: | ||||
total_files, total_bytes = map(int, l.split(' ', 1)) | ||||
except (ValueError, TypeError): | ||||
raise error.ResponseError( | ||||
_('unexpected response from remote server:'), l) | ||||
repo.ui.status(_('%d files to transfer, %s of data\n') % | ||||
(total_files, util.bytecount(total_bytes))) | ||||
handled_bytes = 0 | ||||
repo.ui.progress(_('clone'), 0, total=total_bytes) | ||||
start = time.time() | ||||
tr = repo.transaction(_('clone')) | ||||
try: | ||||
for i in xrange(total_files): | ||||
# XXX doesn't support '\n' or '\r' in filenames | ||||
l = fp.readline() | ||||
try: | ||||
name, size = l.split('\0', 1) | ||||
size = int(size) | ||||
except (ValueError, TypeError): | ||||
raise error.ResponseError( | ||||
_('unexpected response from remote server:'), l) | ||||
if repo.ui.debugflag: | ||||
repo.ui.debug('adding %s (%s)\n' % | ||||
(name, util.bytecount(size))) | ||||
# for backwards compat, name was partially encoded | ||||
ofp = repo.svfs(store.decodedir(name), 'w') | ||||
for chunk in util.filechunkiter(fp, limit=size): | ||||
handled_bytes += len(chunk) | ||||
repo.ui.progress(_('clone'), handled_bytes, | ||||
total=total_bytes) | ||||
ofp.write(chunk) | ||||
ofp.close() | ||||
tr.close() | ||||
finally: | ||||
tr.release() | ||||
# Writing straight to files circumvented the inmemory caches | ||||
repo.invalidate() | ||||
elapsed = time.time() - start | ||||
if elapsed <= 0: | ||||
elapsed = 0.001 | ||||
repo.ui.progress(_('clone'), None) | ||||
repo.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') % | ||||
(util.bytecount(total_bytes), elapsed, | ||||
util.bytecount(total_bytes / elapsed))) | ||||
finally: | ||||
lock.release() | ||||