exchange.py
1242 lines
| 47.1 KiB
| text/x-python
|
PythonLexer
/ mercurial / exchange.py
|
r21024 | # exchange.py - utility to exchange data between repos. | ||
|
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. | ||||
from i18n import _ | ||||
|
r20469 | from node import hex, nullid | ||
|
r21141 | import errno, urllib | ||
|
r21184 | import util, scmutil, changegroup, base85, error | ||
|
r22622 | import discovery, phases, obsolete, bookmarks as bookmod, bundle2, pushkey | ||
|
r20345 | |||
|
r21064 | def readbundle(ui, fh, fname, vfs=None): | ||
|
r21065 | header = changegroup.readexactly(fh, 4) | ||
|
r21063 | |||
|
r21065 | alg = None | ||
|
r21063 | if not fname: | ||
fname = "stream" | ||||
if not header.startswith('HG') and header.startswith('\0'): | ||||
fh = changegroup.headerlessfixup(fh, header) | ||||
|
r21065 | header = "HG10" | ||
alg = 'UN' | ||||
|
r21063 | elif vfs: | ||
fname = vfs.join(fname) | ||||
|
r21065 | magic, version = header[0:2], header[2:4] | ||
|
r21063 | |||
if magic != 'HG': | ||||
raise util.Abort(_('%s: not a Mercurial bundle') % fname) | ||||
|
r21065 | if version == '10': | ||
if alg is None: | ||||
alg = changegroup.readexactly(fh, 2) | ||||
|
r22390 | return changegroup.cg1unpacker(fh, alg) | ||
|
r21144 | elif version == '2X': | ||
|
r21067 | return bundle2.unbundle20(ui, fh, header=magic + version) | ||
|
r21065 | else: | ||
|
r21063 | raise util.Abort(_('%s: unknown bundle version %s') % (fname, version)) | ||
|
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) | ||||
return bundler.newpart('B2X:OBSMARKERS', data=stream) | ||||
return None | ||||
|
r20346 | |||
class pushoperation(object): | ||||
"""A object that represent a single push operation | ||||
It purpose is to carry push related state and very common operation. | ||||
|
r21024 | A new should be created at the beginning of each push and discarded | ||
|
r20346 | afterward. | ||
""" | ||||
|
r22623 | def __init__(self, repo, remote, force=False, revs=None, newbranch=False, | ||
bookmarks=()): | ||||
|
r20346 | # repo we push from | ||
self.repo = repo | ||||
|
r20347 | self.ui = repo.ui | ||
|
r20348 | # repo we push to | ||
self.remote = remote | ||||
|
r20349 | # force option provided | ||
self.force = force | ||||
|
r20350 | # revs to be pushed (None is "all") | ||
self.revs = revs | ||||
|
r22623 | # bookmark explicitly pushed | ||
self.bookmarks = bookmarks | ||||
|
r20351 | # allow push of new branch | ||
self.newbranch = newbranch | ||||
|
r20436 | # did a local lock get acquired? | ||
self.locallocked = None | ||||
|
r21901 | # step already performed | ||
# (used to check what steps have been already performed through bundle2) | ||||
self.stepsdone = set() | ||||
|
r22615 | # Integer version of the changegroup push result | ||
|
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() | ||||
|
r22615 | self.cgresult = None | ||
|
r22624 | # Boolean value for the bookmark push | ||
self.bkresult = None | ||||
|
r21024 | # discover.outgoing object (contains common and outgoing data) | ||
|
r20440 | self.outgoing = None | ||
|
r20462 | # all remote heads before the push | ||
self.remoteheads = None | ||||
|
r20464 | # testable as a boolean indicating if any nodes are missing locally. | ||
self.incoming = None | ||||
|
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 | ||||
|
r22034 | # outgoing obsmarkers | ||
|
r22035 | self.outobsmarkers = set() | ||
|
r22239 | # outgoing bookmarks | ||
self.outbookmarks = [] | ||||
|
r20346 | |||
|
r22014 | @util.propertycache | ||
def futureheads(self): | ||||
"""future remote heads if the changeset push succeeds""" | ||||
return self.outgoing.missingheads | ||||
|
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) | ||||
common = set(self.outgoing.common) | ||||
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 | ||||
|
r22016 | @property | ||
def commonheads(self): | ||||
"""set of all common heads after changeset bundle push""" | ||||
|
r22615 | if self.cgresult: | ||
|
r22016 | return self.futureheads | ||
else: | ||||
return self.fallbackheads | ||||
|
r22015 | |||
|
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')), | ||||
} | ||||
|
r22623 | def push(repo, remote, force=False, revs=None, newbranch=False, bookmarks=()): | ||
|
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() | ||||
''' | ||||
|
r22623 | pushop = pushoperation(repo, remote, force, revs, newbranch, bookmarks) | ||
|
r20348 | if pushop.remote.local(): | ||
missing = (set(pushop.repo.requirements) | ||||
- pushop.remote.local().supported) | ||||
|
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). | ||||
|
r20348 | if not pushop.remote.canpush(): | ||
|
r20345 | raise util.Abort(_("destination does not support push")) | ||
# get local lock as we might write phase data | ||||
locallock = None | ||||
try: | ||||
|
r20346 | locallock = pushop.repo.lock() | ||
|
r20436 | pushop.locallocked = True | ||
|
r20345 | except IOError, err: | ||
|
r20436 | pushop.locallocked = False | ||
|
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 | ||||
|
r20347 | pushop.ui.debug(msg) | ||
|
r20345 | try: | ||
|
r20924 | pushop.repo.checkpush(pushop) | ||
|
r20345 | lock = None | ||
|
r20348 | unbundle = pushop.remote.capable('unbundle') | ||
|
r20345 | if not unbundle: | ||
|
r20348 | lock = pushop.remote.lock() | ||
|
r20345 | try: | ||
|
r20466 | _pushdiscovery(pushop) | ||
|
r21903 | if (pushop.repo.ui.configbool('experimental', 'bundle2-exp', | ||
False) | ||||
and pushop.remote.capable('bundle2-exp')): | ||||
_pushbundle2(pushop) | ||||
_pushchangeset(pushop) | ||||
|
r20441 | _pushsyncphase(pushop) | ||
|
r20433 | _pushobsolete(pushop) | ||
|
r22224 | _pushbookmark(pushop) | ||
|
r20345 | finally: | ||
if lock is not None: | ||||
lock.release() | ||||
finally: | ||||
if locallock is not None: | ||||
locallock.release() | ||||
|
r22616 | return pushop | ||
|
r20352 | |||
|
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 | ||||
|
r20466 | def _pushdiscovery(pushop): | ||
|
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""" | ||||
|
r20466 | unfi = pushop.repo.unfiltered() | ||
fci = discovery.findcommonincoming | ||||
commoninc = fci(unfi, pushop.remote, force=pushop.force) | ||||
common, inc, remoteheads = commoninc | ||||
fco = discovery.findcommonoutgoing | ||||
outgoing = fco(unfi, pushop.remote, onlyheads=pushop.revs, | ||||
commoninc=commoninc, force=pushop.force) | ||||
pushop.outgoing = outgoing | ||||
pushop.remoteheads = remoteheads | ||||
pushop.incoming = inc | ||||
|
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) | ||||
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 | ||||
# | ||||
# should not be necessary for pushblishing server, but because of an | ||||
# 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 | ||||
|
r22035 | @pushdiscovery('obsmarker') | ||
def _pushdiscoveryobsmarkers(pushop): | ||||
|
r22269 | if (obsolete._enabled | ||
and pushop.repo.obsstore | ||||
and 'obsolete' in pushop.remote.listkeys('namespaces')): | ||||
|
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) | ||||
|
r22035 | |||
|
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') | ||||
|
r22651 | explicit = set(pushop.bookmarks) | ||
|
r22622 | comp = bookmod.compare(repo, repo._bookmarks, remotebookmark, srchex=hex) | ||
|
r22243 | addsrc, adddst, advsrc, advdst, diverge, differ, invalid = comp | ||
|
r22239 | for b, scid, dcid in advsrc: | ||
|
r22651 | if b in explicit: | ||
explicit.remove(b) | ||||
|
r22239 | if not ancestors or repo[scid].rev() in ancestors: | ||
pushop.outbookmarks.append((b, dcid, scid)) | ||||
|
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, '')) | ||||
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() | ||||
|
r22239 | |||
|
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!") | ||||
|
r22628 | mst = {"unstable": _("push includes unstable changeset: %s!"), | ||
"bumped": _("push includes bumped changeset: %s!"), | ||||
"divergent": _("push includes divergent changeset: %s!")} | ||||
|
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(): | ||||
|
r22628 | raise util.Abort(mst[ctx.troubles()[0]] % ctx) | ||
|
r20465 | newbm = pushop.ui.configlist('bookmarks', 'pushing') | ||
discovery.checkheads(unfi, pushop.remote, outgoing, | ||||
pushop.remoteheads, | ||||
pushop.newbranch, | ||||
bool(pushop.incoming), | ||||
newbm) | ||||
return True | ||||
|
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 = {} | ||||
def b2partsgenerator(stepname): | ||||
"""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 | ||||
b2partsgenorder.append(stepname) | ||||
return func | ||||
return dec | ||||
@b2partsgenerator('changeset') | ||||
|
r21899 | def _pushb2ctx(pushop, bundler): | ||
"""handle changegroup push through bundle2 | ||||
|
r22615 | addchangegroup result is stored in the ``pushop.cgresult`` attribute. | ||
|
r21899 | """ | ||
|
r21902 | if 'changesets' in pushop.stepsdone: | ||
return | ||||
pushop.stepsdone.add('changesets') | ||||
|
r21899 | # Send known heads to the server for race detection. | ||
|
r21903 | if not _pushcheckoutgoing(pushop): | ||
return | ||||
pushop.repo.prepushoutgoinghooks(pushop.repo, | ||||
pushop.remote, | ||||
pushop.outgoing) | ||||
|
r21899 | if not pushop.force: | ||
bundler.newpart('B2X:CHECK:HEADS', data=iter(pushop.remoteheads)) | ||||
|
r22390 | cg = changegroup.getlocalchangegroup(pushop.repo, 'push', pushop.outgoing) | ||
|
r21899 | cgpart = bundler.newpart('B2X:CHANGEGROUP', data=cg.getchunks()) | ||
def handlereply(op): | ||||
"""extract addchangroup returns from server reply""" | ||||
cgreplies = op.records.getreplies(cgpart.id) | ||||
assert len(cgreplies['changegroup']) == 1 | ||||
|
r22615 | pushop.cgresult = cgreplies['changegroup'][0]['return'] | ||
|
r21899 | return handlereply | ||
|
r22020 | @b2partsgenerator('phase') | ||
def _pushb2phases(pushop, bundler): | ||||
"""handle phase push through bundle2""" | ||||
if 'phases' in pushop.stepsdone: | ||||
return | ||||
b2caps = bundle2.bundle2caps(pushop.remote) | ||||
if not 'b2x:pushkey' in b2caps: | ||||
return | ||||
pushop.stepsdone.add('phases') | ||||
part2node = [] | ||||
enc = pushkey.encode | ||||
for newremotehead in pushop.outdatedphases: | ||||
part = bundler.newpart('b2x:pushkey') | ||||
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)) | ||||
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 | ||||
|
r21904 | |||
|
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: | ||||
buildobsmarkerspart(bundler, pushop.outobsmarkers) | ||||
|
r22242 | @b2partsgenerator('bookmarks') | ||
def _pushb2bookmarks(pushop, bundler): | ||||
"""handle phase push through bundle2""" | ||||
if 'bookmarks' in pushop.stepsdone: | ||||
return | ||||
b2caps = bundle2.bundle2caps(pushop.remote) | ||||
if 'b2x:pushkey' not in b2caps: | ||||
return | ||||
pushop.stepsdone.add('bookmarks') | ||||
part2book = [] | ||||
enc = pushkey.encode | ||||
for book, old, new in pushop.outbookmarks: | ||||
part = bundler.newpart('b2x:pushkey') | ||||
part.addparam('namespace', enc('bookmarks')) | ||||
part.addparam('key', enc(book)) | ||||
part.addparam('old', enc(old)) | ||||
part.addparam('new', enc(new)) | ||||
|
r22650 | action = 'update' | ||
if not old: | ||||
action = 'export' | ||||
elif not new: | ||||
action = 'delete' | ||||
part2book.append((part.id, book, action)) | ||||
|
r22242 | def handlereply(op): | ||
|
r22650 | ui = pushop.ui | ||
for partid, book, action in part2book: | ||||
|
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: | ||||
|
r22650 | ui.status(bookmsgmap[action][0] % book) | ||
|
r22242 | else: | ||
|
r22650 | ui.warn(bookmsgmap[action][1] % book) | ||
|
r22649 | if pushop.bkresult is not None: | ||
pushop.bkresult = 1 | ||||
|
r22242 | return handlereply | ||
|
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.""" | ||||
|
r21644 | bundler = bundle2.bundle20(pushop.ui, bundle2.bundle2caps(pushop.remote)) | ||
|
r21142 | # create reply capability | ||
|
r22342 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(pushop.repo)) | ||
|
r21600 | bundler.newpart('b2x:replycaps', data=capsblob) | ||
|
r21904 | replyhandlers = [] | ||
|
r22017 | for partgenname in b2partsgenorder: | ||
partgen = b2partsgenmapping[partgenname] | ||||
|
r21904 | ret = partgen(pushop, bundler) | ||
|
r21941 | if callable(ret): | ||
replyhandlers.append(ret) | ||||
|
r21904 | # do not push if nothing to push | ||
|
r21903 | if bundler.nbparts <= 1: | ||
return | ||||
|
r21061 | stream = util.chunkbuffer(bundler.getchunks()) | ||
|
r21182 | try: | ||
reply = pushop.remote.unbundle(stream, ['force'], 'push') | ||||
|
r21618 | except error.BundleValueError, exc: | ||
|
r21182 | raise util.Abort('missing support for %s' % exc) | ||
|
r21061 | try: | ||
op = bundle2.processbundle(pushop.repo, reply) | ||||
|
r21618 | except error.BundleValueError, exc: | ||
|
r21061 | raise util.Abort('missing support for %s' % exc) | ||
|
r21904 | for rephand in replyhandlers: | ||
rephand(op) | ||||
|
r21061 | |||
|
r20463 | def _pushchangeset(pushop): | ||
"""Make the actual push of changeset bundle to remote repo""" | ||||
|
r21902 | if 'changesets' in pushop.stepsdone: | ||
return | ||||
pushop.stepsdone.add('changesets') | ||||
|
r21903 | if not _pushcheckoutgoing(pushop): | ||
return | ||||
pushop.repo.prepushoutgoinghooks(pushop.repo, | ||||
pushop.remote, | ||||
pushop.outgoing) | ||||
|
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 | ||||
|
r22390 | bundler = changegroup.cg1packer(pushop.repo, bundlecaps) | ||
|
r20925 | cg = changegroup.getsubset(pushop.repo, | ||
outgoing, | ||||
bundler, | ||||
'push', | ||||
fastpath=True) | ||||
|
r20463 | else: | ||
|
r22390 | cg = changegroup.getlocalchangegroup(pushop.repo, 'push', outgoing, | ||
|
r20928 | bundlecaps) | ||
|
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 | ||||
|
r22615 | pushop.cgresult = pushop.remote.unbundle(cg, remoteheads, | ||
|
r21761 | pushop.repo.url()) | ||
|
r20463 | else: | ||
# we return an integer indicating remote head count | ||||
# change | ||||
|
r22615 | pushop.cgresult = pushop.remote.addchangegroup(cg, 'push', | ||
pushop.repo.url()) | ||||
|
r20463 | |||
|
r20441 | def _pushsyncphase(pushop): | ||
|
r21024 | """synchronise phase information locally and remotely""" | ||
|
r20468 | cheads = pushop.commonheads | ||
|
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 | ||||
|
r22615 | and pushop.cgresult is None # nothing was pushed | ||
|
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'} | ||||
|
r21012 | if not remotephases: # old server or public only reply from non-publishing | ||
|
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 | ||||
|
r22615 | if pushop.cgresult: | ||
|
r22020 | if 'phases' in pushop.stepsdone: | ||
# phases already pushed though bundle2 | ||||
return | ||||
|
r22019 | outdated = pushop.outdatedphases | ||
else: | ||||
outdated = pushop.fallbackoutdatedphases | ||||
|
r22020 | pushop.stepsdone.add('phases') | ||
|
r22019 | # filter heads already turned public by the push | ||
outdated = [c for c in outdated if c.node() not in pheads] | ||||
|
r21662 | b2caps = bundle2.bundle2caps(pushop.remote) | ||
if 'b2x:pushkey' in b2caps: | ||||
# server supports bundle2, let's do a batched push through it | ||||
# | ||||
# This will eventually be unified with the changesets bundle2 push | ||||
bundler = bundle2.bundle20(pushop.ui, b2caps) | ||||
|
r22342 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(pushop.repo)) | ||
|
r21662 | bundler.newpart('b2x:replycaps', data=capsblob) | ||
part2node = [] | ||||
enc = pushkey.encode | ||||
for newremotehead in outdated: | ||||
part = bundler.newpart('b2x:pushkey') | ||||
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)) | ||||
stream = util.chunkbuffer(bundler.getchunks()) | ||||
try: | ||||
reply = pushop.remote.unbundle(stream, ['force'], 'push') | ||||
op = bundle2.processbundle(pushop.repo, reply) | ||||
except error.BundleValueError, exc: | ||||
raise util.Abort('missing support for %s' % exc) | ||||
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) | ||||
else: | ||||
# fallback to independant 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) | ||||
|
r20441 | |||
|
r20438 | def _localphasemove(pushop, nodes, phase=phases.public): | ||
"""move <nodes> to <phase> in the local source repo""" | ||||
if pushop.locallocked: | ||||
|
r22051 | tr = pushop.repo.transaction('push-phase-sync') | ||
try: | ||||
|
r22069 | phases.advanceboundary(pushop.repo, tr, phase, nodes) | ||
|
r22051 | tr.close() | ||
finally: | ||||
tr.release() | ||||
|
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) | ||||
|
r20433 | def _pushobsolete(pushop): | ||
|
r20434 | """utility function to push obsolete markers to a remote""" | ||
|
r22036 | if 'obsmarkers' in pushop.stepsdone: | ||
return | ||||
|
r20435 | pushop.ui.debug('try to push obsolete markers to remote\n') | ||
|
r20433 | repo = pushop.repo | ||
remote = pushop.remote | ||||
|
r22036 | pushop.stepsdone.add('obsmarkers') | ||
|
r22350 | if pushop.outobsmarkers: | ||
|
r20432 | rslts = [] | ||
|
r22034 | remotedata = obsolete._pushkeyescape(pushop.outobsmarkers) | ||
|
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) | ||||
|
r20431 | def _pushbookmark(pushop): | ||
|
r20352 | """Update bookmark position on remote""" | ||
|
r22615 | if pushop.cgresult == 0 or 'bookmarks' in pushop.stepsdone: | ||
|
r22228 | return | ||
|
r22240 | pushop.stepsdone.add('bookmarks') | ||
|
r20431 | ui = pushop.ui | ||
remote = pushop.remote | ||||
|
r22650 | |||
|
r22239 | for b, old, new in pushop.outbookmarks: | ||
|
r22650 | action = 'update' | ||
if not old: | ||||
action = 'export' | ||||
elif not new: | ||||
action = 'delete' | ||||
|
r22239 | if remote.pushkey('bookmarks', b, old, new): | ||
|
r22650 | ui.status(bookmsgmap[action][0] % b) | ||
|
r20352 | else: | ||
|
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 | ||||
|
r20469 | |||
|
r20472 | class pulloperation(object): | ||
"""A object that represent a single pull operation | ||||
It purpose is to carry push related state and very common operation. | ||||
|
r21024 | A new should be created at the beginning of each pull and discarded | ||
|
r20472 | afterward. | ||
""" | ||||
|
r22654 | def __init__(self, repo, remote, heads=None, force=False, bookmarks=()): | ||
|
r20596 | # repo we pull into | ||
|
r20472 | self.repo = repo | ||
|
r20596 | # repo we pull from | ||
|
r20473 | self.remote = remote | ||
|
r20474 | # revision we try to pull (None is "all") | ||
self.heads = heads | ||||
|
r22654 | # bookmark pulled explicitly | ||
self.explicitbookmarks = bookmarks | ||||
|
r20475 | # do we force pull? | ||
self.force = force | ||||
|
r20477 | # the name the pull transaction | ||
self._trname = 'pull\n' + util.hidepassword(remote.url()) | ||||
# hold the transaction once created | ||||
self._tr = None | ||||
|
r20487 | # set of common changeset between local and remote before pull | ||
self.common = None | ||||
# set of pulled head | ||||
self.rheads = None | ||||
|
r21024 | # list of missing changeset to fetch remotely | ||
|
r20488 | self.fetch = None | ||
|
r22654 | # remote bookmarks data | ||
self.remotebookmarks = None | ||||
|
r21024 | # result of changegroup pulling (used as return code by pull) | ||
|
r20898 | self.cgresult = None | ||
|
r22937 | # list of step already done | ||
self.stepsdone = set() | ||||
|
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 | ||||
|
r20878 | c = set(self.common) | ||
ret = list(self.common) | ||||
for n in self.rheads: | ||||
if n not in c: | ||||
ret.append(n) | ||||
return ret | ||||
|
r20487 | else: | ||
# We pulled a specific subset | ||||
# sync on this subset | ||||
return self.heads | ||||
|
r20477 | |||
def gettransaction(self): | ||||
"""get appropriate pull transaction, creating it if needed""" | ||||
if self._tr is None: | ||||
self._tr = self.repo.transaction(self._trname) | ||||
return self._tr | ||||
def closetransaction(self): | ||||
"""close transaction if created""" | ||||
if self._tr is not None: | ||||
self._tr.close() | ||||
def releasetransaction(self): | ||||
"""release transaction if created""" | ||||
if self._tr is not None: | ||||
self._tr.release() | ||||
|
r20469 | |||
|
r22645 | def pull(repo, remote, heads=None, force=False, bookmarks=()): | ||
|
r22654 | pullop = pulloperation(repo, remote, heads, force, bookmarks=bookmarks) | ||
|
r20473 | if pullop.remote.local(): | ||
missing = set(pullop.remote.requirements) - pullop.repo.supported | ||||
|
r20469 | if missing: | ||
msg = _("required features are not" | ||||
" supported in the destination:" | ||||
" %s") % (', '.join(sorted(missing))) | ||||
raise util.Abort(msg) | ||||
|
r22654 | pullop.remotebookmarks = remote.listkeys('bookmarks') | ||
|
r20472 | lock = pullop.repo.lock() | ||
|
r20469 | try: | ||
|
r20900 | _pulldiscovery(pullop) | ||
|
r21256 | if (pullop.repo.ui.configbool('experimental', 'bundle2-exp', False) | ||
|
r21148 | and pullop.remote.capable('bundle2-exp')): | ||
|
r20955 | _pullbundle2(pullop) | ||
|
r22653 | _pullchangeset(pullop) | ||
_pullphase(pullop) | ||||
|
r22655 | _pullbookmarks(pullop) | ||
|
r22653 | _pullobsolete(pullop) | ||
|
r20477 | pullop.closetransaction() | ||
|
r20469 | finally: | ||
|
r20477 | pullop.releasetransaction() | ||
|
r20469 | lock.release() | ||
|
r22693 | return pullop | ||
|
r20476 | |||
|
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 | ||||
|
r20900 | def _pulldiscovery(pullop): | ||
|
r22936 | """Run all discovery steps""" | ||
for stepname in pulldiscoveryorder: | ||||
step = pulldiscoverymapping[stepname] | ||||
step(pullop) | ||||
@pulldiscovery('changegroup') | ||||
def _pulldiscoverychangegroup(pullop): | ||||
|
r20900 | """discovery phase for the pull | ||
Current handle changeset discovery only, will change handle all discovery | ||||
at some point.""" | ||||
tmp = discovery.findcommonincoming(pullop.repo.unfiltered(), | ||||
pullop.remote, | ||||
heads=pullop.heads, | ||||
force=pullop.force) | ||||
pullop.common, pullop.fetch, pullop.rheads = tmp | ||||
|
r20955 | def _pullbundle2(pullop): | ||
"""pull data using bundle2 | ||||
For now, the only supported data are changegroup.""" | ||||
|
r21658 | remotecaps = bundle2.bundle2caps(pullop.remote) | ||
|
r21645 | kwargs = {'bundlecaps': caps20to10(pullop.repo)} | ||
|
r20955 | # pulling changegroup | ||
|
r22937 | pullop.stepsdone.add('changegroup') | ||
|
r21259 | |||
kwargs['common'] = pullop.common | ||||
kwargs['heads'] = pullop.heads or pullop.rheads | ||||
|
r22352 | kwargs['cg'] = pullop.fetch | ||
|
r21658 | if 'b2x:listkeys' in remotecaps: | ||
|
r22656 | kwargs['listkeys'] = ['phase', 'bookmarks'] | ||
|
r20955 | if not pullop.fetch: | ||
|
r21258 | pullop.repo.ui.status(_("no changes found\n")) | ||
pullop.cgresult = 0 | ||||
|
r20955 | else: | ||
if pullop.heads is None and list(pullop.common) == [nullid]: | ||||
pullop.repo.ui.status(_("requesting all changes\n")) | ||||
|
r22354 | if obsolete._enabled: | ||
remoteversions = bundle2.obsmarkersversion(remotecaps) | ||||
if obsolete.commonversion(remoteversions) is not None: | ||||
kwargs['obsmarkers'] = True | ||||
|
r22937 | pullop.stepsdone.add('obsmarkers') | ||
|
r21159 | _pullbundle2extraprepare(pullop, kwargs) | ||
|
r20955 | if kwargs.keys() == ['format']: | ||
return # nothing to pull | ||||
bundle = pullop.remote.getbundle('pull', **kwargs) | ||||
try: | ||||
op = bundle2.processbundle(pullop.repo, bundle, pullop.gettransaction) | ||||
|
r21618 | except error.BundleValueError, exc: | ||
|
r20955 | raise util.Abort('missing support for %s' % exc) | ||
|
r21259 | |||
if pullop.fetch: | ||||
assert len(op.records['changegroup']) == 1 | ||||
pullop.cgresult = op.records['changegroup'][0]['return'] | ||||
|
r20955 | |||
|
r21658 | # processing phases change | ||
for namespace, value in op.records['listkeys']: | ||||
if namespace == 'phases': | ||||
_pullapplyphases(pullop, value) | ||||
|
r22656 | # processing bookmark update | ||
for namespace, value in op.records['listkeys']: | ||||
if namespace == 'bookmarks': | ||||
pullop.remotebookmarks = value | ||||
_pullbookmarks(pullop) | ||||
|
r21159 | def _pullbundle2extraprepare(pullop, kwargs): | ||
"""hook function so that extensions can extend the getbundle call""" | ||||
pass | ||||
|
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 | ||||
|
r22937 | if 'changegroup' in pullop.stepsdone: | ||
|
r22653 | return | ||
|
r22937 | pullop.stepsdone.add('changegroup') | ||
|
r20899 | if not pullop.fetch: | ||
pullop.repo.ui.status(_("no changes found\n")) | ||||
pullop.cgresult = 0 | ||||
return | ||||
|
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 " | ||||
|
r21554 | "other repository doesn't support " | ||
"changegroupsubset.")) | ||||
|
r20489 | else: | ||
cg = pullop.remote.changegroupsubset(pullop.fetch, pullop.heads, 'pull') | ||||
|
r20933 | pullop.cgresult = changegroup.addchangegroup(pullop.repo, cg, 'pull', | ||
|
r20899 | pullop.remote.url()) | ||
|
r20489 | |||
|
r20486 | def _pullphase(pullop): | ||
# Get remote phases data from remote | ||||
|
r22937 | if 'phases' in pullop.stepsdone: | ||
|
r22653 | return | ||
|
r21654 | remotephases = pullop.remote.listkeys('phases') | ||
_pullapplyphases(pullop, remotephases) | ||||
def _pullapplyphases(pullop, remotephases): | ||||
"""apply phase movement from observed remote state""" | ||||
|
r22937 | if 'phases' in pullop.stepsdone: | ||
return | ||||
pullop.stepsdone.add('phases') | ||||
|
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) | ||||
|
r22068 | dheads = pullop.pulledsubset | ||
|
r20486 | else: | ||
# Remote is old or publishing all common changesets | ||||
# should be seen as public | ||||
|
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: | ||||
|
r22069 | tr = pullop.gettransaction() | ||
phases.advanceboundary(pullop.repo, tr, public, pheads) | ||||
|
r22068 | |||
# exclude changesets already draft locally and update the others | ||||
dheads = [pn for pn in dheads if phase(unfi, rev(pn)) > draft] | ||||
if dheads: | ||||
|
r22069 | tr = pullop.gettransaction() | ||
phases.advanceboundary(pullop.repo, tr, draft, dheads) | ||||
|
r20486 | |||
|
r22654 | def _pullbookmarks(pullop): | ||
"""process the remote bookmark information to update the local one""" | ||||
|
r22937 | if 'bookmarks' in pullop.stepsdone: | ||
|
r22654 | return | ||
|
r22937 | pullop.stepsdone.add('bookmarks') | ||
|
r22654 | repo = pullop.repo | ||
remotebookmarks = pullop.remotebookmarks | ||||
bookmod.updatefromremote(repo.ui, repo, remotebookmarks, | ||||
|
r22658 | pullop.remote.url(), | ||
|
r22666 | pullop.gettransaction, | ||
|
r22658 | explicit=pullop.explicitbookmarks) | ||
|
r22654 | |||
|
r20478 | def _pullobsolete(pullop): | ||
|
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""" | ||||
|
r22937 | if 'obsmarkers' in pullop.stepsdone: | ||
|
r22653 | return | ||
|
r22937 | pullop.stepsdone.add('obsmarkers') | ||
|
r20476 | tr = None | ||
if obsolete._enabled: | ||||
|
r20478 | pullop.repo.ui.debug('fetching remote obsolete markers\n') | ||
remoteobs = pullop.remote.listkeys('obsolete') | ||||
|
r20476 | if 'dump0' in remoteobs: | ||
|
r20478 | tr = pullop.gettransaction() | ||
|
r20476 | for key in sorted(remoteobs, reverse=True): | ||
if key.startswith('dump'): | ||||
data = base85.b85decode(remoteobs[key]) | ||||
|
r20478 | pullop.repo.obsstore.mergemarkers(tr, data) | ||
pullop.repo.invalidatevolatilesets() | ||||
|
r20476 | return tr | ||
|
r21645 | def caps20to10(repo): | ||
"""return a set with appropriate options to use bundle20 during getbundle""" | ||||
caps = set(['HG2X']) | ||||
|
r22342 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo)) | ||
|
r21645 | caps.add('bundle2=' + urllib.quote(capsblob)) | ||
return caps | ||||
|
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 = {} | ||||
def getbundle2partsgenerator(stepname): | ||||
"""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 | ||||
getbundle2partsorder.append(stepname) | ||||
return func | ||||
return dec | ||||
|
r21157 | def getbundle(repo, source, heads=None, common=None, bundlecaps=None, | ||
**kwargs): | ||||
|
r20954 | """return a full bundle (with potentially multiple kind of parts) | ||
|
r21144 | Could be a bundle HG10 or a bundle HG2X depending on bundlecaps | ||
|
r20954 | passed. For now, the bundle can contain only changegroup, but this will | ||
changes when more part type will be available for bundle2. | ||||
|
r22390 | This is different from changegroup.getchangegroup that only returns an HG10 | ||
|
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. | ||||
""" | ||||
|
r22542 | # bundle10 case | ||
|
r21144 | if bundlecaps is None or 'HG2X' not in bundlecaps: | ||
|
r22542 | if bundlecaps and not kwargs.get('cg', True): | ||
raise ValueError(_('request for bundle10 must include changegroup')) | ||||
|
r21656 | if kwargs: | ||
raise ValueError(_('unsupported getbundle arguments: %s') | ||||
% ', '.join(sorted(kwargs.keys()))) | ||||
|
r22542 | return changegroup.getchangegroup(repo, source, heads=heads, | ||
common=common, bundlecaps=bundlecaps) | ||||
# bundle20 case | ||||
|
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) | ||||
|
r22542 | |||
for name in getbundle2partsorder: | ||||
func = getbundle2partsmapping[name] | ||||
|
r22543 | kwargs['heads'] = heads | ||
kwargs['common'] = common | ||||
func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps, | ||||
**kwargs) | ||||
|
r22542 | |||
return util.chunkbuffer(bundler.getchunks()) | ||||
@getbundle2partsgenerator('changegroup') | ||||
|
r22543 | def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, heads=None, common=None, **kwargs): | ||||
|
r22542 | """add a changegroup part to the requested bundle""" | ||
cg = None | ||||
if kwargs.get('cg', True): | ||||
# build changegroup bundle here. | ||||
cg = changegroup.getchangegroup(repo, source, heads=heads, | ||||
common=common, bundlecaps=bundlecaps) | ||||
|
r21259 | if cg: | ||
|
r21600 | bundler.newpart('b2x:changegroup', data=cg.getchunks()) | ||
|
r22542 | |||
@getbundle2partsgenerator('listkeys') | ||||
|
r22543 | def _getbundlelistkeysparts(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, **kwargs): | ||||
|
r22542 | """add parts containing listkeys namespaces to the requested bundle""" | ||
|
r21657 | listkeys = kwargs.get('listkeys', ()) | ||
for namespace in listkeys: | ||||
part = bundler.newpart('b2x:listkeys') | ||||
part.addparam('namespace', namespace) | ||||
keys = repo.listkeys(namespace).items() | ||||
part.data = pushkey.encodekeys(keys) | ||||
|
r20967 | |||
|
r22542 | @getbundle2partsgenerator('obsmarkers') | ||
|
r22543 | def _getbundleobsmarkerpart(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, heads=None, **kwargs): | ||||
|
r22541 | """add an obsolescence markers part to the requested bundle""" | ||
|
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) | ||||
buildobsmarkerspart(bundler, markers) | ||||
|
r22542 | @getbundle2partsgenerator('extra') | ||
|
r22543 | def _getbundleextrapart(bundler, repo, source, bundlecaps=None, | ||
b2caps=None, **kwargs): | ||||
|
r21158 | """hook function to let extensions add parts to the requested bundle""" | ||
pass | ||||
|
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 | ||||
|
r21184 | raise error.PushRaced('repository changed while %s - ' | ||
'please try again' % context) | ||||
|
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 | ||||
|
r21024 | mechanism to check that no push race occurred between the creation of the | ||
|
r20968 | bundle and its application. | ||
If the push was raced as PushRaced exception is raised.""" | ||||
r = 0 | ||||
|
r21061 | # need a transaction when processing a bundle2 stream | ||
tr = None | ||||
|
r20968 | lock = repo.lock() | ||
try: | ||||
check_heads(repo, heads, 'uploading changes') | ||||
# push can proceed | ||||
|
r21061 | if util.safehasattr(cg, 'params'): | ||
|
r21187 | try: | ||
tr = repo.transaction('unbundle') | ||||
tr.hookargs['bundle2-exp'] = '1' | ||||
r = bundle2.processbundle(repo, cg, lambda: tr).reply | ||||
cl = repo.unfiltered().changelog | ||||
p = cl.writepending() and repo.root or "" | ||||
repo.hook('b2x-pretransactionclose', throw=True, source=source, | ||||
url=url, pending=p, **tr.hookargs) | ||||
tr.close() | ||||
repo.hook('b2x-transactionclose', source=source, url=url, | ||||
**tr.hookargs) | ||||
except Exception, exc: | ||||
exc.duringunbundle2 = True | ||||
raise | ||||
|
r21061 | else: | ||
r = changegroup.addchangegroup(repo, cg, source, url) | ||||
|
r20968 | finally: | ||
|
r21061 | if tr is not None: | ||
tr.release() | ||||
|
r20968 | lock.release() | ||
return r | ||||