Show More
@@ -1,2222 +1,2228 | |||
|
1 | 1 | # exchange.py - utility to exchange data between repos. |
|
2 | 2 | # |
|
3 | 3 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms of the |
|
6 | 6 | # GNU General Public License version 2 or any later version. |
|
7 | 7 | |
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import collections |
|
11 | 11 | import errno |
|
12 | 12 | import hashlib |
|
13 | 13 | |
|
14 | 14 | from .i18n import _ |
|
15 | 15 | from .node import ( |
|
16 | 16 | bin, |
|
17 | 17 | hex, |
|
18 | 18 | nullid, |
|
19 | 19 | ) |
|
20 | 20 | from . import ( |
|
21 | 21 | bookmarks as bookmod, |
|
22 | 22 | bundle2, |
|
23 | 23 | changegroup, |
|
24 | 24 | discovery, |
|
25 | 25 | error, |
|
26 | 26 | lock as lockmod, |
|
27 | 27 | logexchange, |
|
28 | 28 | obsolete, |
|
29 | 29 | phases, |
|
30 | 30 | pushkey, |
|
31 | 31 | pycompat, |
|
32 | 32 | scmutil, |
|
33 | 33 | sslutil, |
|
34 | 34 | streamclone, |
|
35 | 35 | url as urlmod, |
|
36 | 36 | util, |
|
37 | 37 | ) |
|
38 | 38 | |
|
39 | 39 | urlerr = util.urlerr |
|
40 | 40 | urlreq = util.urlreq |
|
41 | 41 | |
|
42 | 42 | # Maps bundle version human names to changegroup versions. |
|
43 | 43 | _bundlespeccgversions = {'v1': '01', |
|
44 | 44 | 'v2': '02', |
|
45 | 45 | 'packed1': 's1', |
|
46 | 46 | 'bundle2': '02', #legacy |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | # Compression engines allowed in version 1. THIS SHOULD NEVER CHANGE. |
|
50 | 50 | _bundlespecv1compengines = {'gzip', 'bzip2', 'none'} |
|
51 | 51 | |
|
52 | 52 | def parsebundlespec(repo, spec, strict=True, externalnames=False): |
|
53 | 53 | """Parse a bundle string specification into parts. |
|
54 | 54 | |
|
55 | 55 | Bundle specifications denote a well-defined bundle/exchange format. |
|
56 | 56 | The content of a given specification should not change over time in |
|
57 | 57 | order to ensure that bundles produced by a newer version of Mercurial are |
|
58 | 58 | readable from an older version. |
|
59 | 59 | |
|
60 | 60 | The string currently has the form: |
|
61 | 61 | |
|
62 | 62 | <compression>-<type>[;<parameter0>[;<parameter1>]] |
|
63 | 63 | |
|
64 | 64 | Where <compression> is one of the supported compression formats |
|
65 | 65 | and <type> is (currently) a version string. A ";" can follow the type and |
|
66 | 66 | all text afterwards is interpreted as URI encoded, ";" delimited key=value |
|
67 | 67 | pairs. |
|
68 | 68 | |
|
69 | 69 | If ``strict`` is True (the default) <compression> is required. Otherwise, |
|
70 | 70 | it is optional. |
|
71 | 71 | |
|
72 | 72 | If ``externalnames`` is False (the default), the human-centric names will |
|
73 | 73 | be converted to their internal representation. |
|
74 | 74 | |
|
75 | 75 | Returns a 3-tuple of (compression, version, parameters). Compression will |
|
76 | 76 | be ``None`` if not in strict mode and a compression isn't defined. |
|
77 | 77 | |
|
78 | 78 | An ``InvalidBundleSpecification`` is raised when the specification is |
|
79 | 79 | not syntactically well formed. |
|
80 | 80 | |
|
81 | 81 | An ``UnsupportedBundleSpecification`` is raised when the compression or |
|
82 | 82 | bundle type/version is not recognized. |
|
83 | 83 | |
|
84 | 84 | Note: this function will likely eventually return a more complex data |
|
85 | 85 | structure, including bundle2 part information. |
|
86 | 86 | """ |
|
87 | 87 | def parseparams(s): |
|
88 | 88 | if ';' not in s: |
|
89 | 89 | return s, {} |
|
90 | 90 | |
|
91 | 91 | params = {} |
|
92 | 92 | version, paramstr = s.split(';', 1) |
|
93 | 93 | |
|
94 | 94 | for p in paramstr.split(';'): |
|
95 | 95 | if '=' not in p: |
|
96 | 96 | raise error.InvalidBundleSpecification( |
|
97 | 97 | _('invalid bundle specification: ' |
|
98 | 98 | 'missing "=" in parameter: %s') % p) |
|
99 | 99 | |
|
100 | 100 | key, value = p.split('=', 1) |
|
101 | 101 | key = urlreq.unquote(key) |
|
102 | 102 | value = urlreq.unquote(value) |
|
103 | 103 | params[key] = value |
|
104 | 104 | |
|
105 | 105 | return version, params |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | if strict and '-' not in spec: |
|
109 | 109 | raise error.InvalidBundleSpecification( |
|
110 | 110 | _('invalid bundle specification; ' |
|
111 | 111 | 'must be prefixed with compression: %s') % spec) |
|
112 | 112 | |
|
113 | 113 | if '-' in spec: |
|
114 | 114 | compression, version = spec.split('-', 1) |
|
115 | 115 | |
|
116 | 116 | if compression not in util.compengines.supportedbundlenames: |
|
117 | 117 | raise error.UnsupportedBundleSpecification( |
|
118 | 118 | _('%s compression is not supported') % compression) |
|
119 | 119 | |
|
120 | 120 | version, params = parseparams(version) |
|
121 | 121 | |
|
122 | 122 | if version not in _bundlespeccgversions: |
|
123 | 123 | raise error.UnsupportedBundleSpecification( |
|
124 | 124 | _('%s is not a recognized bundle version') % version) |
|
125 | 125 | else: |
|
126 | 126 | # Value could be just the compression or just the version, in which |
|
127 | 127 | # case some defaults are assumed (but only when not in strict mode). |
|
128 | 128 | assert not strict |
|
129 | 129 | |
|
130 | 130 | spec, params = parseparams(spec) |
|
131 | 131 | |
|
132 | 132 | if spec in util.compengines.supportedbundlenames: |
|
133 | 133 | compression = spec |
|
134 | 134 | version = 'v1' |
|
135 | 135 | # Generaldelta repos require v2. |
|
136 | 136 | if 'generaldelta' in repo.requirements: |
|
137 | 137 | version = 'v2' |
|
138 | 138 | # Modern compression engines require v2. |
|
139 | 139 | if compression not in _bundlespecv1compengines: |
|
140 | 140 | version = 'v2' |
|
141 | 141 | elif spec in _bundlespeccgversions: |
|
142 | 142 | if spec == 'packed1': |
|
143 | 143 | compression = 'none' |
|
144 | 144 | else: |
|
145 | 145 | compression = 'bzip2' |
|
146 | 146 | version = spec |
|
147 | 147 | else: |
|
148 | 148 | raise error.UnsupportedBundleSpecification( |
|
149 | 149 | _('%s is not a recognized bundle specification') % spec) |
|
150 | 150 | |
|
151 | 151 | # Bundle version 1 only supports a known set of compression engines. |
|
152 | 152 | if version == 'v1' and compression not in _bundlespecv1compengines: |
|
153 | 153 | raise error.UnsupportedBundleSpecification( |
|
154 | 154 | _('compression engine %s is not supported on v1 bundles') % |
|
155 | 155 | compression) |
|
156 | 156 | |
|
157 | 157 | # The specification for packed1 can optionally declare the data formats |
|
158 | 158 | # required to apply it. If we see this metadata, compare against what the |
|
159 | 159 | # repo supports and error if the bundle isn't compatible. |
|
160 | 160 | if version == 'packed1' and 'requirements' in params: |
|
161 | 161 | requirements = set(params['requirements'].split(',')) |
|
162 | 162 | missingreqs = requirements - repo.supportedformats |
|
163 | 163 | if missingreqs: |
|
164 | 164 | raise error.UnsupportedBundleSpecification( |
|
165 | 165 | _('missing support for repository features: %s') % |
|
166 | 166 | ', '.join(sorted(missingreqs))) |
|
167 | 167 | |
|
168 | 168 | if not externalnames: |
|
169 | 169 | engine = util.compengines.forbundlename(compression) |
|
170 | 170 | compression = engine.bundletype()[1] |
|
171 | 171 | version = _bundlespeccgversions[version] |
|
172 | 172 | return compression, version, params |
|
173 | 173 | |
|
174 | 174 | def readbundle(ui, fh, fname, vfs=None): |
|
175 | 175 | header = changegroup.readexactly(fh, 4) |
|
176 | 176 | |
|
177 | 177 | alg = None |
|
178 | 178 | if not fname: |
|
179 | 179 | fname = "stream" |
|
180 | 180 | if not header.startswith('HG') and header.startswith('\0'): |
|
181 | 181 | fh = changegroup.headerlessfixup(fh, header) |
|
182 | 182 | header = "HG10" |
|
183 | 183 | alg = 'UN' |
|
184 | 184 | elif vfs: |
|
185 | 185 | fname = vfs.join(fname) |
|
186 | 186 | |
|
187 | 187 | magic, version = header[0:2], header[2:4] |
|
188 | 188 | |
|
189 | 189 | if magic != 'HG': |
|
190 | 190 | raise error.Abort(_('%s: not a Mercurial bundle') % fname) |
|
191 | 191 | if version == '10': |
|
192 | 192 | if alg is None: |
|
193 | 193 | alg = changegroup.readexactly(fh, 2) |
|
194 | 194 | return changegroup.cg1unpacker(fh, alg) |
|
195 | 195 | elif version.startswith('2'): |
|
196 | 196 | return bundle2.getunbundler(ui, fh, magicstring=magic + version) |
|
197 | 197 | elif version == 'S1': |
|
198 | 198 | return streamclone.streamcloneapplier(fh) |
|
199 | 199 | else: |
|
200 | 200 | raise error.Abort(_('%s: unknown bundle version %s') % (fname, version)) |
|
201 | 201 | |
|
202 | 202 | def getbundlespec(ui, fh): |
|
203 | 203 | """Infer the bundlespec from a bundle file handle. |
|
204 | 204 | |
|
205 | 205 | The input file handle is seeked and the original seek position is not |
|
206 | 206 | restored. |
|
207 | 207 | """ |
|
208 | 208 | def speccompression(alg): |
|
209 | 209 | try: |
|
210 | 210 | return util.compengines.forbundletype(alg).bundletype()[0] |
|
211 | 211 | except KeyError: |
|
212 | 212 | return None |
|
213 | 213 | |
|
214 | 214 | b = readbundle(ui, fh, None) |
|
215 | 215 | if isinstance(b, changegroup.cg1unpacker): |
|
216 | 216 | alg = b._type |
|
217 | 217 | if alg == '_truncatedBZ': |
|
218 | 218 | alg = 'BZ' |
|
219 | 219 | comp = speccompression(alg) |
|
220 | 220 | if not comp: |
|
221 | 221 | raise error.Abort(_('unknown compression algorithm: %s') % alg) |
|
222 | 222 | return '%s-v1' % comp |
|
223 | 223 | elif isinstance(b, bundle2.unbundle20): |
|
224 | 224 | if 'Compression' in b.params: |
|
225 | 225 | comp = speccompression(b.params['Compression']) |
|
226 | 226 | if not comp: |
|
227 | 227 | raise error.Abort(_('unknown compression algorithm: %s') % comp) |
|
228 | 228 | else: |
|
229 | 229 | comp = 'none' |
|
230 | 230 | |
|
231 | 231 | version = None |
|
232 | 232 | for part in b.iterparts(): |
|
233 | 233 | if part.type == 'changegroup': |
|
234 | 234 | version = part.params['version'] |
|
235 | 235 | if version in ('01', '02'): |
|
236 | 236 | version = 'v2' |
|
237 | 237 | else: |
|
238 | 238 | raise error.Abort(_('changegroup version %s does not have ' |
|
239 | 239 | 'a known bundlespec') % version, |
|
240 | 240 | hint=_('try upgrading your Mercurial ' |
|
241 | 241 | 'client')) |
|
242 | 242 | |
|
243 | 243 | if not version: |
|
244 | 244 | raise error.Abort(_('could not identify changegroup version in ' |
|
245 | 245 | 'bundle')) |
|
246 | 246 | |
|
247 | 247 | return '%s-%s' % (comp, version) |
|
248 | 248 | elif isinstance(b, streamclone.streamcloneapplier): |
|
249 | 249 | requirements = streamclone.readbundle1header(fh)[2] |
|
250 | 250 | params = 'requirements=%s' % ','.join(sorted(requirements)) |
|
251 | 251 | return 'none-packed1;%s' % urlreq.quote(params) |
|
252 | 252 | else: |
|
253 | 253 | raise error.Abort(_('unknown bundle type: %s') % b) |
|
254 | 254 | |
|
255 | 255 | def _computeoutgoing(repo, heads, common): |
|
256 | 256 | """Computes which revs are outgoing given a set of common |
|
257 | 257 | and a set of heads. |
|
258 | 258 | |
|
259 | 259 | This is a separate function so extensions can have access to |
|
260 | 260 | the logic. |
|
261 | 261 | |
|
262 | 262 | Returns a discovery.outgoing object. |
|
263 | 263 | """ |
|
264 | 264 | cl = repo.changelog |
|
265 | 265 | if common: |
|
266 | 266 | hasnode = cl.hasnode |
|
267 | 267 | common = [n for n in common if hasnode(n)] |
|
268 | 268 | else: |
|
269 | 269 | common = [nullid] |
|
270 | 270 | if not heads: |
|
271 | 271 | heads = cl.heads() |
|
272 | 272 | return discovery.outgoing(repo, common, heads) |
|
273 | 273 | |
|
274 | 274 | def _forcebundle1(op): |
|
275 | 275 | """return true if a pull/push must use bundle1 |
|
276 | 276 | |
|
277 | 277 | This function is used to allow testing of the older bundle version""" |
|
278 | 278 | ui = op.repo.ui |
|
279 | 279 | forcebundle1 = False |
|
280 | 280 | # The goal is this config is to allow developer to choose the bundle |
|
281 | 281 | # version used during exchanged. This is especially handy during test. |
|
282 | 282 | # Value is a list of bundle version to be picked from, highest version |
|
283 | 283 | # should be used. |
|
284 | 284 | # |
|
285 | 285 | # developer config: devel.legacy.exchange |
|
286 | 286 | exchange = ui.configlist('devel', 'legacy.exchange') |
|
287 | 287 | forcebundle1 = 'bundle2' not in exchange and 'bundle1' in exchange |
|
288 | 288 | return forcebundle1 or not op.remote.capable('bundle2') |
|
289 | 289 | |
|
290 | 290 | class pushoperation(object): |
|
291 | 291 | """A object that represent a single push operation |
|
292 | 292 | |
|
293 | 293 | Its purpose is to carry push related state and very common operations. |
|
294 | 294 | |
|
295 | 295 | A new pushoperation should be created at the beginning of each push and |
|
296 | 296 | discarded afterward. |
|
297 | 297 | """ |
|
298 | 298 | |
|
299 | 299 | def __init__(self, repo, remote, force=False, revs=None, newbranch=False, |
|
300 | 300 | bookmarks=(), pushvars=None): |
|
301 | 301 | # repo we push from |
|
302 | 302 | self.repo = repo |
|
303 | 303 | self.ui = repo.ui |
|
304 | 304 | # repo we push to |
|
305 | 305 | self.remote = remote |
|
306 | 306 | # force option provided |
|
307 | 307 | self.force = force |
|
308 | 308 | # revs to be pushed (None is "all") |
|
309 | 309 | self.revs = revs |
|
310 | 310 | # bookmark explicitly pushed |
|
311 | 311 | self.bookmarks = bookmarks |
|
312 | 312 | # allow push of new branch |
|
313 | 313 | self.newbranch = newbranch |
|
314 | 314 | # step already performed |
|
315 | 315 | # (used to check what steps have been already performed through bundle2) |
|
316 | 316 | self.stepsdone = set() |
|
317 | 317 | # Integer version of the changegroup push result |
|
318 | 318 | # - None means nothing to push |
|
319 | 319 | # - 0 means HTTP error |
|
320 | 320 | # - 1 means we pushed and remote head count is unchanged *or* |
|
321 | 321 | # we have outgoing changesets but refused to push |
|
322 | 322 | # - other values as described by addchangegroup() |
|
323 | 323 | self.cgresult = None |
|
324 | 324 | # Boolean value for the bookmark push |
|
325 | 325 | self.bkresult = None |
|
326 | 326 | # discover.outgoing object (contains common and outgoing data) |
|
327 | 327 | self.outgoing = None |
|
328 | 328 | # all remote topological heads before the push |
|
329 | 329 | self.remoteheads = None |
|
330 | 330 | # Details of the remote branch pre and post push |
|
331 | 331 | # |
|
332 | 332 | # mapping: {'branch': ([remoteheads], |
|
333 | 333 | # [newheads], |
|
334 | 334 | # [unsyncedheads], |
|
335 | 335 | # [discardedheads])} |
|
336 | 336 | # - branch: the branch name |
|
337 | 337 | # - remoteheads: the list of remote heads known locally |
|
338 | 338 | # None if the branch is new |
|
339 | 339 | # - newheads: the new remote heads (known locally) with outgoing pushed |
|
340 | 340 | # - unsyncedheads: the list of remote heads unknown locally. |
|
341 | 341 | # - discardedheads: the list of remote heads made obsolete by the push |
|
342 | 342 | self.pushbranchmap = None |
|
343 | 343 | # testable as a boolean indicating if any nodes are missing locally. |
|
344 | 344 | self.incoming = None |
|
345 | 345 | # summary of the remote phase situation |
|
346 | 346 | self.remotephases = None |
|
347 | 347 | # phases changes that must be pushed along side the changesets |
|
348 | 348 | self.outdatedphases = None |
|
349 | 349 | # phases changes that must be pushed if changeset push fails |
|
350 | 350 | self.fallbackoutdatedphases = None |
|
351 | 351 | # outgoing obsmarkers |
|
352 | 352 | self.outobsmarkers = set() |
|
353 | 353 | # outgoing bookmarks |
|
354 | 354 | self.outbookmarks = [] |
|
355 | 355 | # transaction manager |
|
356 | 356 | self.trmanager = None |
|
357 | 357 | # map { pushkey partid -> callback handling failure} |
|
358 | 358 | # used to handle exception from mandatory pushkey part failure |
|
359 | 359 | self.pkfailcb = {} |
|
360 | 360 | # an iterable of pushvars or None |
|
361 | 361 | self.pushvars = pushvars |
|
362 | 362 | |
|
363 | 363 | @util.propertycache |
|
364 | 364 | def futureheads(self): |
|
365 | 365 | """future remote heads if the changeset push succeeds""" |
|
366 | 366 | return self.outgoing.missingheads |
|
367 | 367 | |
|
368 | 368 | @util.propertycache |
|
369 | 369 | def fallbackheads(self): |
|
370 | 370 | """future remote heads if the changeset push fails""" |
|
371 | 371 | if self.revs is None: |
|
372 | 372 | # not target to push, all common are relevant |
|
373 | 373 | return self.outgoing.commonheads |
|
374 | 374 | unfi = self.repo.unfiltered() |
|
375 | 375 | # I want cheads = heads(::missingheads and ::commonheads) |
|
376 | 376 | # (missingheads is revs with secret changeset filtered out) |
|
377 | 377 | # |
|
378 | 378 | # This can be expressed as: |
|
379 | 379 | # cheads = ( (missingheads and ::commonheads) |
|
380 | 380 | # + (commonheads and ::missingheads))" |
|
381 | 381 | # ) |
|
382 | 382 | # |
|
383 | 383 | # while trying to push we already computed the following: |
|
384 | 384 | # common = (::commonheads) |
|
385 | 385 | # missing = ((commonheads::missingheads) - commonheads) |
|
386 | 386 | # |
|
387 | 387 | # We can pick: |
|
388 | 388 | # * missingheads part of common (::commonheads) |
|
389 | 389 | common = self.outgoing.common |
|
390 | 390 | nm = self.repo.changelog.nodemap |
|
391 | 391 | cheads = [node for node in self.revs if nm[node] in common] |
|
392 | 392 | # and |
|
393 | 393 | # * commonheads parents on missing |
|
394 | 394 | revset = unfi.set('%ln and parents(roots(%ln))', |
|
395 | 395 | self.outgoing.commonheads, |
|
396 | 396 | self.outgoing.missing) |
|
397 | 397 | cheads.extend(c.node() for c in revset) |
|
398 | 398 | return cheads |
|
399 | 399 | |
|
400 | 400 | @property |
|
401 | 401 | def commonheads(self): |
|
402 | 402 | """set of all common heads after changeset bundle push""" |
|
403 | 403 | if self.cgresult: |
|
404 | 404 | return self.futureheads |
|
405 | 405 | else: |
|
406 | 406 | return self.fallbackheads |
|
407 | 407 | |
|
408 | 408 | # mapping of message used when pushing bookmark |
|
409 | 409 | bookmsgmap = {'update': (_("updating bookmark %s\n"), |
|
410 | 410 | _('updating bookmark %s failed!\n')), |
|
411 | 411 | 'export': (_("exporting bookmark %s\n"), |
|
412 | 412 | _('exporting bookmark %s failed!\n')), |
|
413 | 413 | 'delete': (_("deleting remote bookmark %s\n"), |
|
414 | 414 | _('deleting remote bookmark %s failed!\n')), |
|
415 | 415 | } |
|
416 | 416 | |
|
417 | 417 | |
|
418 | 418 | def push(repo, remote, force=False, revs=None, newbranch=False, bookmarks=(), |
|
419 | 419 | opargs=None): |
|
420 | 420 | '''Push outgoing changesets (limited by revs) from a local |
|
421 | 421 | repository to remote. Return an integer: |
|
422 | 422 | - None means nothing to push |
|
423 | 423 | - 0 means HTTP error |
|
424 | 424 | - 1 means we pushed and remote head count is unchanged *or* |
|
425 | 425 | we have outgoing changesets but refused to push |
|
426 | 426 | - other values as described by addchangegroup() |
|
427 | 427 | ''' |
|
428 | 428 | if opargs is None: |
|
429 | 429 | opargs = {} |
|
430 | 430 | pushop = pushoperation(repo, remote, force, revs, newbranch, bookmarks, |
|
431 | 431 | **pycompat.strkwargs(opargs)) |
|
432 | 432 | if pushop.remote.local(): |
|
433 | 433 | missing = (set(pushop.repo.requirements) |
|
434 | 434 | - pushop.remote.local().supported) |
|
435 | 435 | if missing: |
|
436 | 436 | msg = _("required features are not" |
|
437 | 437 | " supported in the destination:" |
|
438 | 438 | " %s") % (', '.join(sorted(missing))) |
|
439 | 439 | raise error.Abort(msg) |
|
440 | 440 | |
|
441 | 441 | if not pushop.remote.canpush(): |
|
442 | 442 | raise error.Abort(_("destination does not support push")) |
|
443 | 443 | |
|
444 | 444 | if not pushop.remote.capable('unbundle'): |
|
445 | 445 | raise error.Abort(_('cannot push: destination does not support the ' |
|
446 | 446 | 'unbundle wire protocol command')) |
|
447 | 447 | |
|
448 | 448 | # get lock as we might write phase data |
|
449 | 449 | wlock = lock = None |
|
450 | 450 | try: |
|
451 | 451 | # bundle2 push may receive a reply bundle touching bookmarks or other |
|
452 | 452 | # things requiring the wlock. Take it now to ensure proper ordering. |
|
453 | 453 | maypushback = pushop.ui.configbool('experimental', 'bundle2.pushback') |
|
454 | 454 | if (not _forcebundle1(pushop)) and maypushback: |
|
455 | 455 | wlock = pushop.repo.wlock() |
|
456 | 456 | lock = pushop.repo.lock() |
|
457 | 457 | pushop.trmanager = transactionmanager(pushop.repo, |
|
458 | 458 | 'push-response', |
|
459 | 459 | pushop.remote.url()) |
|
460 | 460 | except IOError as err: |
|
461 | 461 | if err.errno != errno.EACCES: |
|
462 | 462 | raise |
|
463 | 463 | # source repo cannot be locked. |
|
464 | 464 | # We do not abort the push, but just disable the local phase |
|
465 | 465 | # synchronisation. |
|
466 | 466 | msg = 'cannot lock source repository: %s\n' % err |
|
467 | 467 | pushop.ui.debug(msg) |
|
468 | 468 | |
|
469 | 469 | with wlock or util.nullcontextmanager(), \ |
|
470 | 470 | lock or util.nullcontextmanager(), \ |
|
471 | 471 | pushop.trmanager or util.nullcontextmanager(): |
|
472 | 472 | pushop.repo.checkpush(pushop) |
|
473 | 473 | _pushdiscovery(pushop) |
|
474 | 474 | if not _forcebundle1(pushop): |
|
475 | 475 | _pushbundle2(pushop) |
|
476 | 476 | _pushchangeset(pushop) |
|
477 | 477 | _pushsyncphase(pushop) |
|
478 | 478 | _pushobsolete(pushop) |
|
479 | 479 | _pushbookmark(pushop) |
|
480 | 480 | |
|
481 | 481 | return pushop |
|
482 | 482 | |
|
483 | 483 | # list of steps to perform discovery before push |
|
484 | 484 | pushdiscoveryorder = [] |
|
485 | 485 | |
|
486 | 486 | # Mapping between step name and function |
|
487 | 487 | # |
|
488 | 488 | # This exists to help extensions wrap steps if necessary |
|
489 | 489 | pushdiscoverymapping = {} |
|
490 | 490 | |
|
491 | 491 | def pushdiscovery(stepname): |
|
492 | 492 | """decorator for function performing discovery before push |
|
493 | 493 | |
|
494 | 494 | The function is added to the step -> function mapping and appended to the |
|
495 | 495 | list of steps. Beware that decorated function will be added in order (this |
|
496 | 496 | may matter). |
|
497 | 497 | |
|
498 | 498 | You can only use this decorator for a new step, if you want to wrap a step |
|
499 | 499 | from an extension, change the pushdiscovery dictionary directly.""" |
|
500 | 500 | def dec(func): |
|
501 | 501 | assert stepname not in pushdiscoverymapping |
|
502 | 502 | pushdiscoverymapping[stepname] = func |
|
503 | 503 | pushdiscoveryorder.append(stepname) |
|
504 | 504 | return func |
|
505 | 505 | return dec |
|
506 | 506 | |
|
507 | 507 | def _pushdiscovery(pushop): |
|
508 | 508 | """Run all discovery steps""" |
|
509 | 509 | for stepname in pushdiscoveryorder: |
|
510 | 510 | step = pushdiscoverymapping[stepname] |
|
511 | 511 | step(pushop) |
|
512 | 512 | |
|
513 | 513 | @pushdiscovery('changeset') |
|
514 | 514 | def _pushdiscoverychangeset(pushop): |
|
515 | 515 | """discover the changeset that need to be pushed""" |
|
516 | 516 | fci = discovery.findcommonincoming |
|
517 | 517 | if pushop.revs: |
|
518 | 518 | commoninc = fci(pushop.repo, pushop.remote, force=pushop.force, |
|
519 | 519 | ancestorsof=pushop.revs) |
|
520 | 520 | else: |
|
521 | 521 | commoninc = fci(pushop.repo, pushop.remote, force=pushop.force) |
|
522 | 522 | common, inc, remoteheads = commoninc |
|
523 | 523 | fco = discovery.findcommonoutgoing |
|
524 | 524 | outgoing = fco(pushop.repo, pushop.remote, onlyheads=pushop.revs, |
|
525 | 525 | commoninc=commoninc, force=pushop.force) |
|
526 | 526 | pushop.outgoing = outgoing |
|
527 | 527 | pushop.remoteheads = remoteheads |
|
528 | 528 | pushop.incoming = inc |
|
529 | 529 | |
|
530 | 530 | @pushdiscovery('phase') |
|
531 | 531 | def _pushdiscoveryphase(pushop): |
|
532 | 532 | """discover the phase that needs to be pushed |
|
533 | 533 | |
|
534 | 534 | (computed for both success and failure case for changesets push)""" |
|
535 | 535 | outgoing = pushop.outgoing |
|
536 | 536 | unfi = pushop.repo.unfiltered() |
|
537 | 537 | remotephases = pushop.remote.listkeys('phases') |
|
538 | 538 | if (pushop.ui.configbool('ui', '_usedassubrepo') |
|
539 | 539 | and remotephases # server supports phases |
|
540 | 540 | and not pushop.outgoing.missing # no changesets to be pushed |
|
541 | 541 | and remotephases.get('publishing', False)): |
|
542 | 542 | # When: |
|
543 | 543 | # - this is a subrepo push |
|
544 | 544 | # - and remote support phase |
|
545 | 545 | # - and no changeset are to be pushed |
|
546 | 546 | # - and remote is publishing |
|
547 | 547 | # We may be in issue 3781 case! |
|
548 | 548 | # We drop the possible phase synchronisation done by |
|
549 | 549 | # courtesy to publish changesets possibly locally draft |
|
550 | 550 | # on the remote. |
|
551 | 551 | pushop.outdatedphases = [] |
|
552 | 552 | pushop.fallbackoutdatedphases = [] |
|
553 | 553 | return |
|
554 | 554 | |
|
555 | 555 | pushop.remotephases = phases.remotephasessummary(pushop.repo, |
|
556 | 556 | pushop.fallbackheads, |
|
557 | 557 | remotephases) |
|
558 | 558 | droots = pushop.remotephases.draftroots |
|
559 | 559 | |
|
560 | 560 | extracond = '' |
|
561 | 561 | if not pushop.remotephases.publishing: |
|
562 | 562 | extracond = ' and public()' |
|
563 | 563 | revset = 'heads((%%ln::%%ln) %s)' % extracond |
|
564 | 564 | # Get the list of all revs draft on remote by public here. |
|
565 | 565 | # XXX Beware that revset break if droots is not strictly |
|
566 | 566 | # XXX root we may want to ensure it is but it is costly |
|
567 | 567 | fallback = list(unfi.set(revset, droots, pushop.fallbackheads)) |
|
568 | 568 | if not outgoing.missing: |
|
569 | 569 | future = fallback |
|
570 | 570 | else: |
|
571 | 571 | # adds changeset we are going to push as draft |
|
572 | 572 | # |
|
573 | 573 | # should not be necessary for publishing server, but because of an |
|
574 | 574 | # issue fixed in xxxxx we have to do it anyway. |
|
575 | 575 | fdroots = list(unfi.set('roots(%ln + %ln::)', |
|
576 | 576 | outgoing.missing, droots)) |
|
577 | 577 | fdroots = [f.node() for f in fdroots] |
|
578 | 578 | future = list(unfi.set(revset, fdroots, pushop.futureheads)) |
|
579 | 579 | pushop.outdatedphases = future |
|
580 | 580 | pushop.fallbackoutdatedphases = fallback |
|
581 | 581 | |
|
582 | 582 | @pushdiscovery('obsmarker') |
|
583 | 583 | def _pushdiscoveryobsmarkers(pushop): |
|
584 | 584 | if (obsolete.isenabled(pushop.repo, obsolete.exchangeopt) |
|
585 | 585 | and pushop.repo.obsstore |
|
586 | 586 | and 'obsolete' in pushop.remote.listkeys('namespaces')): |
|
587 | 587 | repo = pushop.repo |
|
588 | 588 | # very naive computation, that can be quite expensive on big repo. |
|
589 | 589 | # However: evolution is currently slow on them anyway. |
|
590 | 590 | nodes = (c.node() for c in repo.set('::%ln', pushop.futureheads)) |
|
591 | 591 | pushop.outobsmarkers = pushop.repo.obsstore.relevantmarkers(nodes) |
|
592 | 592 | |
|
593 | 593 | @pushdiscovery('bookmarks') |
|
594 | 594 | def _pushdiscoverybookmarks(pushop): |
|
595 | 595 | ui = pushop.ui |
|
596 | 596 | repo = pushop.repo.unfiltered() |
|
597 | 597 | remote = pushop.remote |
|
598 | 598 | ui.debug("checking for updated bookmarks\n") |
|
599 | 599 | ancestors = () |
|
600 | 600 | if pushop.revs: |
|
601 | 601 | revnums = map(repo.changelog.rev, pushop.revs) |
|
602 | 602 | ancestors = repo.changelog.ancestors(revnums, inclusive=True) |
|
603 | 603 | remotebookmark = remote.listkeys('bookmarks') |
|
604 | 604 | |
|
605 | 605 | explicit = set([repo._bookmarks.expandname(bookmark) |
|
606 | 606 | for bookmark in pushop.bookmarks]) |
|
607 | 607 | |
|
608 | 608 | remotebookmark = bookmod.unhexlifybookmarks(remotebookmark) |
|
609 | 609 | comp = bookmod.comparebookmarks(repo, repo._bookmarks, remotebookmark) |
|
610 | 610 | |
|
611 | 611 | def safehex(x): |
|
612 | 612 | if x is None: |
|
613 | 613 | return x |
|
614 | 614 | return hex(x) |
|
615 | 615 | |
|
616 | 616 | def hexifycompbookmarks(bookmarks): |
|
617 | 617 | for b, scid, dcid in bookmarks: |
|
618 | 618 | yield b, safehex(scid), safehex(dcid) |
|
619 | 619 | |
|
620 | 620 | comp = [hexifycompbookmarks(marks) for marks in comp] |
|
621 | 621 | addsrc, adddst, advsrc, advdst, diverge, differ, invalid, same = comp |
|
622 | 622 | |
|
623 | 623 | for b, scid, dcid in advsrc: |
|
624 | 624 | if b in explicit: |
|
625 | 625 | explicit.remove(b) |
|
626 | 626 | if not ancestors or repo[scid].rev() in ancestors: |
|
627 | 627 | pushop.outbookmarks.append((b, dcid, scid)) |
|
628 | 628 | # search added bookmark |
|
629 | 629 | for b, scid, dcid in addsrc: |
|
630 | 630 | if b in explicit: |
|
631 | 631 | explicit.remove(b) |
|
632 | 632 | pushop.outbookmarks.append((b, '', scid)) |
|
633 | 633 | # search for overwritten bookmark |
|
634 | 634 | for b, scid, dcid in list(advdst) + list(diverge) + list(differ): |
|
635 | 635 | if b in explicit: |
|
636 | 636 | explicit.remove(b) |
|
637 | 637 | pushop.outbookmarks.append((b, dcid, scid)) |
|
638 | 638 | # search for bookmark to delete |
|
639 | 639 | for b, scid, dcid in adddst: |
|
640 | 640 | if b in explicit: |
|
641 | 641 | explicit.remove(b) |
|
642 | 642 | # treat as "deleted locally" |
|
643 | 643 | pushop.outbookmarks.append((b, dcid, '')) |
|
644 | 644 | # identical bookmarks shouldn't get reported |
|
645 | 645 | for b, scid, dcid in same: |
|
646 | 646 | if b in explicit: |
|
647 | 647 | explicit.remove(b) |
|
648 | 648 | |
|
649 | 649 | if explicit: |
|
650 | 650 | explicit = sorted(explicit) |
|
651 | 651 | # we should probably list all of them |
|
652 | 652 | ui.warn(_('bookmark %s does not exist on the local ' |
|
653 | 653 | 'or remote repository!\n') % explicit[0]) |
|
654 | 654 | pushop.bkresult = 2 |
|
655 | 655 | |
|
656 | 656 | pushop.outbookmarks.sort() |
|
657 | 657 | |
|
658 | 658 | def _pushcheckoutgoing(pushop): |
|
659 | 659 | outgoing = pushop.outgoing |
|
660 | 660 | unfi = pushop.repo.unfiltered() |
|
661 | 661 | if not outgoing.missing: |
|
662 | 662 | # nothing to push |
|
663 | 663 | scmutil.nochangesfound(unfi.ui, unfi, outgoing.excluded) |
|
664 | 664 | return False |
|
665 | 665 | # something to push |
|
666 | 666 | if not pushop.force: |
|
667 | 667 | # if repo.obsstore == False --> no obsolete |
|
668 | 668 | # then, save the iteration |
|
669 | 669 | if unfi.obsstore: |
|
670 | 670 | # this message are here for 80 char limit reason |
|
671 | 671 | mso = _("push includes obsolete changeset: %s!") |
|
672 | 672 | mspd = _("push includes phase-divergent changeset: %s!") |
|
673 | 673 | mscd = _("push includes content-divergent changeset: %s!") |
|
674 | 674 | mst = {"orphan": _("push includes orphan changeset: %s!"), |
|
675 | 675 | "phase-divergent": mspd, |
|
676 | 676 | "content-divergent": mscd} |
|
677 | 677 | # If we are to push if there is at least one |
|
678 | 678 | # obsolete or unstable changeset in missing, at |
|
679 | 679 | # least one of the missinghead will be obsolete or |
|
680 | 680 | # unstable. So checking heads only is ok |
|
681 | 681 | for node in outgoing.missingheads: |
|
682 | 682 | ctx = unfi[node] |
|
683 | 683 | if ctx.obsolete(): |
|
684 | 684 | raise error.Abort(mso % ctx) |
|
685 | 685 | elif ctx.isunstable(): |
|
686 | 686 | # TODO print more than one instability in the abort |
|
687 | 687 | # message |
|
688 | 688 | raise error.Abort(mst[ctx.instabilities()[0]] % ctx) |
|
689 | 689 | |
|
690 | 690 | discovery.checkheads(pushop) |
|
691 | 691 | return True |
|
692 | 692 | |
|
693 | 693 | # List of names of steps to perform for an outgoing bundle2, order matters. |
|
694 | 694 | b2partsgenorder = [] |
|
695 | 695 | |
|
696 | 696 | # Mapping between step name and function |
|
697 | 697 | # |
|
698 | 698 | # This exists to help extensions wrap steps if necessary |
|
699 | 699 | b2partsgenmapping = {} |
|
700 | 700 | |
|
701 | 701 | def b2partsgenerator(stepname, idx=None): |
|
702 | 702 | """decorator for function generating bundle2 part |
|
703 | 703 | |
|
704 | 704 | The function is added to the step -> function mapping and appended to the |
|
705 | 705 | list of steps. Beware that decorated functions will be added in order |
|
706 | 706 | (this may matter). |
|
707 | 707 | |
|
708 | 708 | You can only use this decorator for new steps, if you want to wrap a step |
|
709 | 709 | from an extension, attack the b2partsgenmapping dictionary directly.""" |
|
710 | 710 | def dec(func): |
|
711 | 711 | assert stepname not in b2partsgenmapping |
|
712 | 712 | b2partsgenmapping[stepname] = func |
|
713 | 713 | if idx is None: |
|
714 | 714 | b2partsgenorder.append(stepname) |
|
715 | 715 | else: |
|
716 | 716 | b2partsgenorder.insert(idx, stepname) |
|
717 | 717 | return func |
|
718 | 718 | return dec |
|
719 | 719 | |
|
720 | 720 | def _pushb2ctxcheckheads(pushop, bundler): |
|
721 | 721 | """Generate race condition checking parts |
|
722 | 722 | |
|
723 | 723 | Exists as an independent function to aid extensions |
|
724 | 724 | """ |
|
725 | 725 | # * 'force' do not check for push race, |
|
726 | 726 | # * if we don't push anything, there are nothing to check. |
|
727 | 727 | if not pushop.force and pushop.outgoing.missingheads: |
|
728 | 728 | allowunrelated = 'related' in bundler.capabilities.get('checkheads', ()) |
|
729 | 729 | emptyremote = pushop.pushbranchmap is None |
|
730 | 730 | if not allowunrelated or emptyremote: |
|
731 | 731 | bundler.newpart('check:heads', data=iter(pushop.remoteheads)) |
|
732 | 732 | else: |
|
733 | 733 | affected = set() |
|
734 | 734 | for branch, heads in pushop.pushbranchmap.iteritems(): |
|
735 | 735 | remoteheads, newheads, unsyncedheads, discardedheads = heads |
|
736 | 736 | if remoteheads is not None: |
|
737 | 737 | remote = set(remoteheads) |
|
738 | 738 | affected |= set(discardedheads) & remote |
|
739 | 739 | affected |= remote - set(newheads) |
|
740 | 740 | if affected: |
|
741 | 741 | data = iter(sorted(affected)) |
|
742 | 742 | bundler.newpart('check:updated-heads', data=data) |
|
743 | 743 | |
|
744 | 744 | def _pushing(pushop): |
|
745 | 745 | """return True if we are pushing anything""" |
|
746 | 746 | return bool(pushop.outgoing.missing |
|
747 | 747 | or pushop.outdatedphases |
|
748 | 748 | or pushop.outobsmarkers |
|
749 | 749 | or pushop.outbookmarks) |
|
750 | 750 | |
|
751 | 751 | @b2partsgenerator('check-bookmarks') |
|
752 | 752 | def _pushb2checkbookmarks(pushop, bundler): |
|
753 | 753 | """insert bookmark move checking""" |
|
754 | 754 | if not _pushing(pushop) or pushop.force: |
|
755 | 755 | return |
|
756 | 756 | b2caps = bundle2.bundle2caps(pushop.remote) |
|
757 | 757 | hasbookmarkcheck = 'bookmarks' in b2caps |
|
758 | 758 | if not (pushop.outbookmarks and hasbookmarkcheck): |
|
759 | 759 | return |
|
760 | 760 | data = [] |
|
761 | 761 | for book, old, new in pushop.outbookmarks: |
|
762 | 762 | old = bin(old) |
|
763 | 763 | data.append((book, old)) |
|
764 | 764 | checkdata = bookmod.binaryencode(data) |
|
765 | 765 | bundler.newpart('check:bookmarks', data=checkdata) |
|
766 | 766 | |
|
767 | 767 | @b2partsgenerator('check-phases') |
|
768 | 768 | def _pushb2checkphases(pushop, bundler): |
|
769 | 769 | """insert phase move checking""" |
|
770 | 770 | if not _pushing(pushop) or pushop.force: |
|
771 | 771 | return |
|
772 | 772 | b2caps = bundle2.bundle2caps(pushop.remote) |
|
773 | 773 | hasphaseheads = 'heads' in b2caps.get('phases', ()) |
|
774 | 774 | if pushop.remotephases is not None and hasphaseheads: |
|
775 | 775 | # check that the remote phase has not changed |
|
776 | 776 | checks = [[] for p in phases.allphases] |
|
777 | 777 | checks[phases.public].extend(pushop.remotephases.publicheads) |
|
778 | 778 | checks[phases.draft].extend(pushop.remotephases.draftroots) |
|
779 | 779 | if any(checks): |
|
780 | 780 | for nodes in checks: |
|
781 | 781 | nodes.sort() |
|
782 | 782 | checkdata = phases.binaryencode(checks) |
|
783 | 783 | bundler.newpart('check:phases', data=checkdata) |
|
784 | 784 | |
|
785 | 785 | @b2partsgenerator('changeset') |
|
786 | 786 | def _pushb2ctx(pushop, bundler): |
|
787 | 787 | """handle changegroup push through bundle2 |
|
788 | 788 | |
|
789 | 789 | addchangegroup result is stored in the ``pushop.cgresult`` attribute. |
|
790 | 790 | """ |
|
791 | 791 | if 'changesets' in pushop.stepsdone: |
|
792 | 792 | return |
|
793 | 793 | pushop.stepsdone.add('changesets') |
|
794 | 794 | # Send known heads to the server for race detection. |
|
795 | 795 | if not _pushcheckoutgoing(pushop): |
|
796 | 796 | return |
|
797 | 797 | pushop.repo.prepushoutgoinghooks(pushop) |
|
798 | 798 | |
|
799 | 799 | _pushb2ctxcheckheads(pushop, bundler) |
|
800 | 800 | |
|
801 | 801 | b2caps = bundle2.bundle2caps(pushop.remote) |
|
802 | 802 | version = '01' |
|
803 | 803 | cgversions = b2caps.get('changegroup') |
|
804 | 804 | if cgversions: # 3.1 and 3.2 ship with an empty value |
|
805 | 805 | cgversions = [v for v in cgversions |
|
806 | 806 | if v in changegroup.supportedoutgoingversions( |
|
807 | 807 | pushop.repo)] |
|
808 | 808 | if not cgversions: |
|
809 | 809 | raise ValueError(_('no common changegroup version')) |
|
810 | 810 | version = max(cgversions) |
|
811 | 811 | cgstream = changegroup.makestream(pushop.repo, pushop.outgoing, version, |
|
812 | 812 | 'push') |
|
813 | 813 | cgpart = bundler.newpart('changegroup', data=cgstream) |
|
814 | 814 | if cgversions: |
|
815 | 815 | cgpart.addparam('version', version) |
|
816 | 816 | if 'treemanifest' in pushop.repo.requirements: |
|
817 | 817 | cgpart.addparam('treemanifest', '1') |
|
818 | 818 | def handlereply(op): |
|
819 | 819 | """extract addchangegroup returns from server reply""" |
|
820 | 820 | cgreplies = op.records.getreplies(cgpart.id) |
|
821 | 821 | assert len(cgreplies['changegroup']) == 1 |
|
822 | 822 | pushop.cgresult = cgreplies['changegroup'][0]['return'] |
|
823 | 823 | return handlereply |
|
824 | 824 | |
|
825 | 825 | @b2partsgenerator('phase') |
|
826 | 826 | def _pushb2phases(pushop, bundler): |
|
827 | 827 | """handle phase push through bundle2""" |
|
828 | 828 | if 'phases' in pushop.stepsdone: |
|
829 | 829 | return |
|
830 | 830 | b2caps = bundle2.bundle2caps(pushop.remote) |
|
831 | 831 | ui = pushop.repo.ui |
|
832 | 832 | |
|
833 | 833 | legacyphase = 'phases' in ui.configlist('devel', 'legacy.exchange') |
|
834 | 834 | haspushkey = 'pushkey' in b2caps |
|
835 | 835 | hasphaseheads = 'heads' in b2caps.get('phases', ()) |
|
836 | 836 | |
|
837 | 837 | if hasphaseheads and not legacyphase: |
|
838 | 838 | return _pushb2phaseheads(pushop, bundler) |
|
839 | 839 | elif haspushkey: |
|
840 | 840 | return _pushb2phasespushkey(pushop, bundler) |
|
841 | 841 | |
|
842 | 842 | def _pushb2phaseheads(pushop, bundler): |
|
843 | 843 | """push phase information through a bundle2 - binary part""" |
|
844 | 844 | pushop.stepsdone.add('phases') |
|
845 | 845 | if pushop.outdatedphases: |
|
846 | 846 | updates = [[] for p in phases.allphases] |
|
847 | 847 | updates[0].extend(h.node() for h in pushop.outdatedphases) |
|
848 | 848 | phasedata = phases.binaryencode(updates) |
|
849 | 849 | bundler.newpart('phase-heads', data=phasedata) |
|
850 | 850 | |
|
851 | 851 | def _pushb2phasespushkey(pushop, bundler): |
|
852 | 852 | """push phase information through a bundle2 - pushkey part""" |
|
853 | 853 | pushop.stepsdone.add('phases') |
|
854 | 854 | part2node = [] |
|
855 | 855 | |
|
856 | 856 | def handlefailure(pushop, exc): |
|
857 | 857 | targetid = int(exc.partid) |
|
858 | 858 | for partid, node in part2node: |
|
859 | 859 | if partid == targetid: |
|
860 | 860 | raise error.Abort(_('updating %s to public failed') % node) |
|
861 | 861 | |
|
862 | 862 | enc = pushkey.encode |
|
863 | 863 | for newremotehead in pushop.outdatedphases: |
|
864 | 864 | part = bundler.newpart('pushkey') |
|
865 | 865 | part.addparam('namespace', enc('phases')) |
|
866 | 866 | part.addparam('key', enc(newremotehead.hex())) |
|
867 | 867 | part.addparam('old', enc('%d' % phases.draft)) |
|
868 | 868 | part.addparam('new', enc('%d' % phases.public)) |
|
869 | 869 | part2node.append((part.id, newremotehead)) |
|
870 | 870 | pushop.pkfailcb[part.id] = handlefailure |
|
871 | 871 | |
|
872 | 872 | def handlereply(op): |
|
873 | 873 | for partid, node in part2node: |
|
874 | 874 | partrep = op.records.getreplies(partid) |
|
875 | 875 | results = partrep['pushkey'] |
|
876 | 876 | assert len(results) <= 1 |
|
877 | 877 | msg = None |
|
878 | 878 | if not results: |
|
879 | 879 | msg = _('server ignored update of %s to public!\n') % node |
|
880 | 880 | elif not int(results[0]['return']): |
|
881 | 881 | msg = _('updating %s to public failed!\n') % node |
|
882 | 882 | if msg is not None: |
|
883 | 883 | pushop.ui.warn(msg) |
|
884 | 884 | return handlereply |
|
885 | 885 | |
|
886 | 886 | @b2partsgenerator('obsmarkers') |
|
887 | 887 | def _pushb2obsmarkers(pushop, bundler): |
|
888 | 888 | if 'obsmarkers' in pushop.stepsdone: |
|
889 | 889 | return |
|
890 | 890 | remoteversions = bundle2.obsmarkersversion(bundler.capabilities) |
|
891 | 891 | if obsolete.commonversion(remoteversions) is None: |
|
892 | 892 | return |
|
893 | 893 | pushop.stepsdone.add('obsmarkers') |
|
894 | 894 | if pushop.outobsmarkers: |
|
895 | 895 | markers = sorted(pushop.outobsmarkers) |
|
896 | 896 | bundle2.buildobsmarkerspart(bundler, markers) |
|
897 | 897 | |
|
898 | 898 | @b2partsgenerator('bookmarks') |
|
899 | 899 | def _pushb2bookmarks(pushop, bundler): |
|
900 | 900 | """handle bookmark push through bundle2""" |
|
901 | 901 | if 'bookmarks' in pushop.stepsdone: |
|
902 | 902 | return |
|
903 | 903 | b2caps = bundle2.bundle2caps(pushop.remote) |
|
904 | 904 | |
|
905 | 905 | legacy = pushop.repo.ui.configlist('devel', 'legacy.exchange') |
|
906 | 906 | legacybooks = 'bookmarks' in legacy |
|
907 | 907 | |
|
908 | 908 | if not legacybooks and 'bookmarks' in b2caps: |
|
909 | 909 | return _pushb2bookmarkspart(pushop, bundler) |
|
910 | 910 | elif 'pushkey' in b2caps: |
|
911 | 911 | return _pushb2bookmarkspushkey(pushop, bundler) |
|
912 | 912 | |
|
913 | 913 | def _bmaction(old, new): |
|
914 | 914 | """small utility for bookmark pushing""" |
|
915 | 915 | if not old: |
|
916 | 916 | return 'export' |
|
917 | 917 | elif not new: |
|
918 | 918 | return 'delete' |
|
919 | 919 | return 'update' |
|
920 | 920 | |
|
921 | 921 | def _pushb2bookmarkspart(pushop, bundler): |
|
922 | 922 | pushop.stepsdone.add('bookmarks') |
|
923 | 923 | if not pushop.outbookmarks: |
|
924 | 924 | return |
|
925 | 925 | |
|
926 | 926 | allactions = [] |
|
927 | 927 | data = [] |
|
928 | 928 | for book, old, new in pushop.outbookmarks: |
|
929 | 929 | new = bin(new) |
|
930 | 930 | data.append((book, new)) |
|
931 | 931 | allactions.append((book, _bmaction(old, new))) |
|
932 | 932 | checkdata = bookmod.binaryencode(data) |
|
933 | 933 | bundler.newpart('bookmarks', data=checkdata) |
|
934 | 934 | |
|
935 | 935 | def handlereply(op): |
|
936 | 936 | ui = pushop.ui |
|
937 | 937 | # if success |
|
938 | 938 | for book, action in allactions: |
|
939 | 939 | ui.status(bookmsgmap[action][0] % book) |
|
940 | 940 | |
|
941 | 941 | return handlereply |
|
942 | 942 | |
|
943 | 943 | def _pushb2bookmarkspushkey(pushop, bundler): |
|
944 | 944 | pushop.stepsdone.add('bookmarks') |
|
945 | 945 | part2book = [] |
|
946 | 946 | enc = pushkey.encode |
|
947 | 947 | |
|
948 | 948 | def handlefailure(pushop, exc): |
|
949 | 949 | targetid = int(exc.partid) |
|
950 | 950 | for partid, book, action in part2book: |
|
951 | 951 | if partid == targetid: |
|
952 | 952 | raise error.Abort(bookmsgmap[action][1].rstrip() % book) |
|
953 | 953 | # we should not be called for part we did not generated |
|
954 | 954 | assert False |
|
955 | 955 | |
|
956 | 956 | for book, old, new in pushop.outbookmarks: |
|
957 | 957 | part = bundler.newpart('pushkey') |
|
958 | 958 | part.addparam('namespace', enc('bookmarks')) |
|
959 | 959 | part.addparam('key', enc(book)) |
|
960 | 960 | part.addparam('old', enc(old)) |
|
961 | 961 | part.addparam('new', enc(new)) |
|
962 | 962 | action = 'update' |
|
963 | 963 | if not old: |
|
964 | 964 | action = 'export' |
|
965 | 965 | elif not new: |
|
966 | 966 | action = 'delete' |
|
967 | 967 | part2book.append((part.id, book, action)) |
|
968 | 968 | pushop.pkfailcb[part.id] = handlefailure |
|
969 | 969 | |
|
970 | 970 | def handlereply(op): |
|
971 | 971 | ui = pushop.ui |
|
972 | 972 | for partid, book, action in part2book: |
|
973 | 973 | partrep = op.records.getreplies(partid) |
|
974 | 974 | results = partrep['pushkey'] |
|
975 | 975 | assert len(results) <= 1 |
|
976 | 976 | if not results: |
|
977 | 977 | pushop.ui.warn(_('server ignored bookmark %s update\n') % book) |
|
978 | 978 | else: |
|
979 | 979 | ret = int(results[0]['return']) |
|
980 | 980 | if ret: |
|
981 | 981 | ui.status(bookmsgmap[action][0] % book) |
|
982 | 982 | else: |
|
983 | 983 | ui.warn(bookmsgmap[action][1] % book) |
|
984 | 984 | if pushop.bkresult is not None: |
|
985 | 985 | pushop.bkresult = 1 |
|
986 | 986 | return handlereply |
|
987 | 987 | |
|
988 | 988 | @b2partsgenerator('pushvars', idx=0) |
|
989 | 989 | def _getbundlesendvars(pushop, bundler): |
|
990 | 990 | '''send shellvars via bundle2''' |
|
991 | 991 | pushvars = pushop.pushvars |
|
992 | 992 | if pushvars: |
|
993 | 993 | shellvars = {} |
|
994 | 994 | for raw in pushvars: |
|
995 | 995 | if '=' not in raw: |
|
996 | 996 | msg = ("unable to parse variable '%s', should follow " |
|
997 | 997 | "'KEY=VALUE' or 'KEY=' format") |
|
998 | 998 | raise error.Abort(msg % raw) |
|
999 | 999 | k, v = raw.split('=', 1) |
|
1000 | 1000 | shellvars[k] = v |
|
1001 | 1001 | |
|
1002 | 1002 | part = bundler.newpart('pushvars') |
|
1003 | 1003 | |
|
1004 | 1004 | for key, value in shellvars.iteritems(): |
|
1005 | 1005 | part.addparam(key, value, mandatory=False) |
|
1006 | 1006 | |
|
1007 | 1007 | def _pushbundle2(pushop): |
|
1008 | 1008 | """push data to the remote using bundle2 |
|
1009 | 1009 | |
|
1010 | 1010 | The only currently supported type of data is changegroup but this will |
|
1011 | 1011 | evolve in the future.""" |
|
1012 | 1012 | bundler = bundle2.bundle20(pushop.ui, bundle2.bundle2caps(pushop.remote)) |
|
1013 | 1013 | pushback = (pushop.trmanager |
|
1014 | 1014 | and pushop.ui.configbool('experimental', 'bundle2.pushback')) |
|
1015 | 1015 | |
|
1016 | 1016 | # create reply capability |
|
1017 | 1017 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(pushop.repo, |
|
1018 | 1018 | allowpushback=pushback)) |
|
1019 | 1019 | bundler.newpart('replycaps', data=capsblob) |
|
1020 | 1020 | replyhandlers = [] |
|
1021 | 1021 | for partgenname in b2partsgenorder: |
|
1022 | 1022 | partgen = b2partsgenmapping[partgenname] |
|
1023 | 1023 | ret = partgen(pushop, bundler) |
|
1024 | 1024 | if callable(ret): |
|
1025 | 1025 | replyhandlers.append(ret) |
|
1026 | 1026 | # do not push if nothing to push |
|
1027 | 1027 | if bundler.nbparts <= 1: |
|
1028 | 1028 | return |
|
1029 | 1029 | stream = util.chunkbuffer(bundler.getchunks()) |
|
1030 | 1030 | try: |
|
1031 | 1031 | try: |
|
1032 | 1032 | reply = pushop.remote.unbundle( |
|
1033 | 1033 | stream, ['force'], pushop.remote.url()) |
|
1034 | 1034 | except error.BundleValueError as exc: |
|
1035 | 1035 | raise error.Abort(_('missing support for %s') % exc) |
|
1036 | 1036 | try: |
|
1037 | 1037 | trgetter = None |
|
1038 | 1038 | if pushback: |
|
1039 | 1039 | trgetter = pushop.trmanager.transaction |
|
1040 | 1040 | op = bundle2.processbundle(pushop.repo, reply, trgetter) |
|
1041 | 1041 | except error.BundleValueError as exc: |
|
1042 | 1042 | raise error.Abort(_('missing support for %s') % exc) |
|
1043 | 1043 | except bundle2.AbortFromPart as exc: |
|
1044 | 1044 | pushop.ui.status(_('remote: %s\n') % exc) |
|
1045 | 1045 | if exc.hint is not None: |
|
1046 | 1046 | pushop.ui.status(_('remote: %s\n') % ('(%s)' % exc.hint)) |
|
1047 | 1047 | raise error.Abort(_('push failed on remote')) |
|
1048 | 1048 | except error.PushkeyFailed as exc: |
|
1049 | 1049 | partid = int(exc.partid) |
|
1050 | 1050 | if partid not in pushop.pkfailcb: |
|
1051 | 1051 | raise |
|
1052 | 1052 | pushop.pkfailcb[partid](pushop, exc) |
|
1053 | 1053 | for rephand in replyhandlers: |
|
1054 | 1054 | rephand(op) |
|
1055 | 1055 | |
|
1056 | 1056 | def _pushchangeset(pushop): |
|
1057 | 1057 | """Make the actual push of changeset bundle to remote repo""" |
|
1058 | 1058 | if 'changesets' in pushop.stepsdone: |
|
1059 | 1059 | return |
|
1060 | 1060 | pushop.stepsdone.add('changesets') |
|
1061 | 1061 | if not _pushcheckoutgoing(pushop): |
|
1062 | 1062 | return |
|
1063 | 1063 | |
|
1064 | 1064 | # Should have verified this in push(). |
|
1065 | 1065 | assert pushop.remote.capable('unbundle') |
|
1066 | 1066 | |
|
1067 | 1067 | pushop.repo.prepushoutgoinghooks(pushop) |
|
1068 | 1068 | outgoing = pushop.outgoing |
|
1069 | 1069 | # TODO: get bundlecaps from remote |
|
1070 | 1070 | bundlecaps = None |
|
1071 | 1071 | # create a changegroup from local |
|
1072 | 1072 | if pushop.revs is None and not (outgoing.excluded |
|
1073 | 1073 | or pushop.repo.changelog.filteredrevs): |
|
1074 | 1074 | # push everything, |
|
1075 | 1075 | # use the fast path, no race possible on push |
|
1076 | 1076 | cg = changegroup.makechangegroup(pushop.repo, outgoing, '01', 'push', |
|
1077 | 1077 | fastpath=True, bundlecaps=bundlecaps) |
|
1078 | 1078 | else: |
|
1079 | 1079 | cg = changegroup.makechangegroup(pushop.repo, outgoing, '01', |
|
1080 | 1080 | 'push', bundlecaps=bundlecaps) |
|
1081 | 1081 | |
|
1082 | 1082 | # apply changegroup to remote |
|
1083 | 1083 | # local repo finds heads on server, finds out what |
|
1084 | 1084 | # revs it must push. once revs transferred, if server |
|
1085 | 1085 | # finds it has different heads (someone else won |
|
1086 | 1086 | # commit/push race), server aborts. |
|
1087 | 1087 | if pushop.force: |
|
1088 | 1088 | remoteheads = ['force'] |
|
1089 | 1089 | else: |
|
1090 | 1090 | remoteheads = pushop.remoteheads |
|
1091 | 1091 | # ssh: return remote's addchangegroup() |
|
1092 | 1092 | # http: return remote's addchangegroup() or 0 for error |
|
1093 | 1093 | pushop.cgresult = pushop.remote.unbundle(cg, remoteheads, |
|
1094 | 1094 | pushop.repo.url()) |
|
1095 | 1095 | |
|
1096 | 1096 | def _pushsyncphase(pushop): |
|
1097 | 1097 | """synchronise phase information locally and remotely""" |
|
1098 | 1098 | cheads = pushop.commonheads |
|
1099 | 1099 | # even when we don't push, exchanging phase data is useful |
|
1100 | 1100 | remotephases = pushop.remote.listkeys('phases') |
|
1101 | 1101 | if (pushop.ui.configbool('ui', '_usedassubrepo') |
|
1102 | 1102 | and remotephases # server supports phases |
|
1103 | 1103 | and pushop.cgresult is None # nothing was pushed |
|
1104 | 1104 | and remotephases.get('publishing', False)): |
|
1105 | 1105 | # When: |
|
1106 | 1106 | # - this is a subrepo push |
|
1107 | 1107 | # - and remote support phase |
|
1108 | 1108 | # - and no changeset was pushed |
|
1109 | 1109 | # - and remote is publishing |
|
1110 | 1110 | # We may be in issue 3871 case! |
|
1111 | 1111 | # We drop the possible phase synchronisation done by |
|
1112 | 1112 | # courtesy to publish changesets possibly locally draft |
|
1113 | 1113 | # on the remote. |
|
1114 | 1114 | remotephases = {'publishing': 'True'} |
|
1115 | 1115 | if not remotephases: # old server or public only reply from non-publishing |
|
1116 | 1116 | _localphasemove(pushop, cheads) |
|
1117 | 1117 | # don't push any phase data as there is nothing to push |
|
1118 | 1118 | else: |
|
1119 | 1119 | ana = phases.analyzeremotephases(pushop.repo, cheads, |
|
1120 | 1120 | remotephases) |
|
1121 | 1121 | pheads, droots = ana |
|
1122 | 1122 | ### Apply remote phase on local |
|
1123 | 1123 | if remotephases.get('publishing', False): |
|
1124 | 1124 | _localphasemove(pushop, cheads) |
|
1125 | 1125 | else: # publish = False |
|
1126 | 1126 | _localphasemove(pushop, pheads) |
|
1127 | 1127 | _localphasemove(pushop, cheads, phases.draft) |
|
1128 | 1128 | ### Apply local phase on remote |
|
1129 | 1129 | |
|
1130 | 1130 | if pushop.cgresult: |
|
1131 | 1131 | if 'phases' in pushop.stepsdone: |
|
1132 | 1132 | # phases already pushed though bundle2 |
|
1133 | 1133 | return |
|
1134 | 1134 | outdated = pushop.outdatedphases |
|
1135 | 1135 | else: |
|
1136 | 1136 | outdated = pushop.fallbackoutdatedphases |
|
1137 | 1137 | |
|
1138 | 1138 | pushop.stepsdone.add('phases') |
|
1139 | 1139 | |
|
1140 | 1140 | # filter heads already turned public by the push |
|
1141 | 1141 | outdated = [c for c in outdated if c.node() not in pheads] |
|
1142 | 1142 | # fallback to independent pushkey command |
|
1143 | 1143 | for newremotehead in outdated: |
|
1144 | 1144 | r = pushop.remote.pushkey('phases', |
|
1145 | 1145 | newremotehead.hex(), |
|
1146 | 1146 | str(phases.draft), |
|
1147 | 1147 | str(phases.public)) |
|
1148 | 1148 | if not r: |
|
1149 | 1149 | pushop.ui.warn(_('updating %s to public failed!\n') |
|
1150 | 1150 | % newremotehead) |
|
1151 | 1151 | |
|
1152 | 1152 | def _localphasemove(pushop, nodes, phase=phases.public): |
|
1153 | 1153 | """move <nodes> to <phase> in the local source repo""" |
|
1154 | 1154 | if pushop.trmanager: |
|
1155 | 1155 | phases.advanceboundary(pushop.repo, |
|
1156 | 1156 | pushop.trmanager.transaction(), |
|
1157 | 1157 | phase, |
|
1158 | 1158 | nodes) |
|
1159 | 1159 | else: |
|
1160 | 1160 | # repo is not locked, do not change any phases! |
|
1161 | 1161 | # Informs the user that phases should have been moved when |
|
1162 | 1162 | # applicable. |
|
1163 | 1163 | actualmoves = [n for n in nodes if phase < pushop.repo[n].phase()] |
|
1164 | 1164 | phasestr = phases.phasenames[phase] |
|
1165 | 1165 | if actualmoves: |
|
1166 | 1166 | pushop.ui.status(_('cannot lock source repo, skipping ' |
|
1167 | 1167 | 'local %s phase update\n') % phasestr) |
|
1168 | 1168 | |
|
1169 | 1169 | def _pushobsolete(pushop): |
|
1170 | 1170 | """utility function to push obsolete markers to a remote""" |
|
1171 | 1171 | if 'obsmarkers' in pushop.stepsdone: |
|
1172 | 1172 | return |
|
1173 | 1173 | repo = pushop.repo |
|
1174 | 1174 | remote = pushop.remote |
|
1175 | 1175 | pushop.stepsdone.add('obsmarkers') |
|
1176 | 1176 | if pushop.outobsmarkers: |
|
1177 | 1177 | pushop.ui.debug('try to push obsolete markers to remote\n') |
|
1178 | 1178 | rslts = [] |
|
1179 | 1179 | remotedata = obsolete._pushkeyescape(sorted(pushop.outobsmarkers)) |
|
1180 | 1180 | for key in sorted(remotedata, reverse=True): |
|
1181 | 1181 | # reverse sort to ensure we end with dump0 |
|
1182 | 1182 | data = remotedata[key] |
|
1183 | 1183 | rslts.append(remote.pushkey('obsolete', key, '', data)) |
|
1184 | 1184 | if [r for r in rslts if not r]: |
|
1185 | 1185 | msg = _('failed to push some obsolete markers!\n') |
|
1186 | 1186 | repo.ui.warn(msg) |
|
1187 | 1187 | |
|
1188 | 1188 | def _pushbookmark(pushop): |
|
1189 | 1189 | """Update bookmark position on remote""" |
|
1190 | 1190 | if pushop.cgresult == 0 or 'bookmarks' in pushop.stepsdone: |
|
1191 | 1191 | return |
|
1192 | 1192 | pushop.stepsdone.add('bookmarks') |
|
1193 | 1193 | ui = pushop.ui |
|
1194 | 1194 | remote = pushop.remote |
|
1195 | 1195 | |
|
1196 | 1196 | for b, old, new in pushop.outbookmarks: |
|
1197 | 1197 | action = 'update' |
|
1198 | 1198 | if not old: |
|
1199 | 1199 | action = 'export' |
|
1200 | 1200 | elif not new: |
|
1201 | 1201 | action = 'delete' |
|
1202 | 1202 | if remote.pushkey('bookmarks', b, old, new): |
|
1203 | 1203 | ui.status(bookmsgmap[action][0] % b) |
|
1204 | 1204 | else: |
|
1205 | 1205 | ui.warn(bookmsgmap[action][1] % b) |
|
1206 | 1206 | # discovery can have set the value form invalid entry |
|
1207 | 1207 | if pushop.bkresult is not None: |
|
1208 | 1208 | pushop.bkresult = 1 |
|
1209 | 1209 | |
|
1210 | 1210 | class pulloperation(object): |
|
1211 | 1211 | """A object that represent a single pull operation |
|
1212 | 1212 | |
|
1213 | 1213 | It purpose is to carry pull related state and very common operation. |
|
1214 | 1214 | |
|
1215 | 1215 | A new should be created at the beginning of each pull and discarded |
|
1216 | 1216 | afterward. |
|
1217 | 1217 | """ |
|
1218 | 1218 | |
|
1219 | 1219 | def __init__(self, repo, remote, heads=None, force=False, bookmarks=(), |
|
1220 | 1220 | remotebookmarks=None, streamclonerequested=None): |
|
1221 | 1221 | # repo we pull into |
|
1222 | 1222 | self.repo = repo |
|
1223 | 1223 | # repo we pull from |
|
1224 | 1224 | self.remote = remote |
|
1225 | 1225 | # revision we try to pull (None is "all") |
|
1226 | 1226 | self.heads = heads |
|
1227 | 1227 | # bookmark pulled explicitly |
|
1228 | 1228 | self.explicitbookmarks = [repo._bookmarks.expandname(bookmark) |
|
1229 | 1229 | for bookmark in bookmarks] |
|
1230 | 1230 | # do we force pull? |
|
1231 | 1231 | self.force = force |
|
1232 | 1232 | # whether a streaming clone was requested |
|
1233 | 1233 | self.streamclonerequested = streamclonerequested |
|
1234 | 1234 | # transaction manager |
|
1235 | 1235 | self.trmanager = None |
|
1236 | 1236 | # set of common changeset between local and remote before pull |
|
1237 | 1237 | self.common = None |
|
1238 | 1238 | # set of pulled head |
|
1239 | 1239 | self.rheads = None |
|
1240 | 1240 | # list of missing changeset to fetch remotely |
|
1241 | 1241 | self.fetch = None |
|
1242 | 1242 | # remote bookmarks data |
|
1243 | 1243 | self.remotebookmarks = remotebookmarks |
|
1244 | 1244 | # result of changegroup pulling (used as return code by pull) |
|
1245 | 1245 | self.cgresult = None |
|
1246 | 1246 | # list of step already done |
|
1247 | 1247 | self.stepsdone = set() |
|
1248 | 1248 | # Whether we attempted a clone from pre-generated bundles. |
|
1249 | 1249 | self.clonebundleattempted = False |
|
1250 | 1250 | |
|
1251 | 1251 | @util.propertycache |
|
1252 | 1252 | def pulledsubset(self): |
|
1253 | 1253 | """heads of the set of changeset target by the pull""" |
|
1254 | 1254 | # compute target subset |
|
1255 | 1255 | if self.heads is None: |
|
1256 | 1256 | # We pulled every thing possible |
|
1257 | 1257 | # sync on everything common |
|
1258 | 1258 | c = set(self.common) |
|
1259 | 1259 | ret = list(self.common) |
|
1260 | 1260 | for n in self.rheads: |
|
1261 | 1261 | if n not in c: |
|
1262 | 1262 | ret.append(n) |
|
1263 | 1263 | return ret |
|
1264 | 1264 | else: |
|
1265 | 1265 | # We pulled a specific subset |
|
1266 | 1266 | # sync on this subset |
|
1267 | 1267 | return self.heads |
|
1268 | 1268 | |
|
1269 | 1269 | @util.propertycache |
|
1270 | 1270 | def canusebundle2(self): |
|
1271 | 1271 | return not _forcebundle1(self) |
|
1272 | 1272 | |
|
1273 | 1273 | @util.propertycache |
|
1274 | 1274 | def remotebundle2caps(self): |
|
1275 | 1275 | return bundle2.bundle2caps(self.remote) |
|
1276 | 1276 | |
|
1277 | 1277 | def gettransaction(self): |
|
1278 | 1278 | # deprecated; talk to trmanager directly |
|
1279 | 1279 | return self.trmanager.transaction() |
|
1280 | 1280 | |
|
1281 | 1281 | class transactionmanager(util.transactional): |
|
1282 | 1282 | """An object to manage the life cycle of a transaction |
|
1283 | 1283 | |
|
1284 | 1284 | It creates the transaction on demand and calls the appropriate hooks when |
|
1285 | 1285 | closing the transaction.""" |
|
1286 | 1286 | def __init__(self, repo, source, url): |
|
1287 | 1287 | self.repo = repo |
|
1288 | 1288 | self.source = source |
|
1289 | 1289 | self.url = url |
|
1290 | 1290 | self._tr = None |
|
1291 | 1291 | |
|
1292 | 1292 | def transaction(self): |
|
1293 | 1293 | """Return an open transaction object, constructing if necessary""" |
|
1294 | 1294 | if not self._tr: |
|
1295 | 1295 | trname = '%s\n%s' % (self.source, util.hidepassword(self.url)) |
|
1296 | 1296 | self._tr = self.repo.transaction(trname) |
|
1297 | 1297 | self._tr.hookargs['source'] = self.source |
|
1298 | 1298 | self._tr.hookargs['url'] = self.url |
|
1299 | 1299 | return self._tr |
|
1300 | 1300 | |
|
1301 | 1301 | def close(self): |
|
1302 | 1302 | """close transaction if created""" |
|
1303 | 1303 | if self._tr is not None: |
|
1304 | 1304 | self._tr.close() |
|
1305 | 1305 | |
|
1306 | 1306 | def release(self): |
|
1307 | 1307 | """release transaction if created""" |
|
1308 | 1308 | if self._tr is not None: |
|
1309 | 1309 | self._tr.release() |
|
1310 | 1310 | |
|
1311 | 1311 | def pull(repo, remote, heads=None, force=False, bookmarks=(), opargs=None, |
|
1312 | 1312 | streamclonerequested=None): |
|
1313 | 1313 | """Fetch repository data from a remote. |
|
1314 | 1314 | |
|
1315 | 1315 | This is the main function used to retrieve data from a remote repository. |
|
1316 | 1316 | |
|
1317 | 1317 | ``repo`` is the local repository to clone into. |
|
1318 | 1318 | ``remote`` is a peer instance. |
|
1319 | 1319 | ``heads`` is an iterable of revisions we want to pull. ``None`` (the |
|
1320 | 1320 | default) means to pull everything from the remote. |
|
1321 | 1321 | ``bookmarks`` is an iterable of bookmarks requesting to be pulled. By |
|
1322 | 1322 | default, all remote bookmarks are pulled. |
|
1323 | 1323 | ``opargs`` are additional keyword arguments to pass to ``pulloperation`` |
|
1324 | 1324 | initialization. |
|
1325 | 1325 | ``streamclonerequested`` is a boolean indicating whether a "streaming |
|
1326 | 1326 | clone" is requested. A "streaming clone" is essentially a raw file copy |
|
1327 | 1327 | of revlogs from the server. This only works when the local repository is |
|
1328 | 1328 | empty. The default value of ``None`` means to respect the server |
|
1329 | 1329 | configuration for preferring stream clones. |
|
1330 | 1330 | |
|
1331 | 1331 | Returns the ``pulloperation`` created for this pull. |
|
1332 | 1332 | """ |
|
1333 | 1333 | if opargs is None: |
|
1334 | 1334 | opargs = {} |
|
1335 | 1335 | pullop = pulloperation(repo, remote, heads, force, bookmarks=bookmarks, |
|
1336 | 1336 | streamclonerequested=streamclonerequested, |
|
1337 | 1337 | **pycompat.strkwargs(opargs)) |
|
1338 | 1338 | |
|
1339 | 1339 | peerlocal = pullop.remote.local() |
|
1340 | 1340 | if peerlocal: |
|
1341 | 1341 | missing = set(peerlocal.requirements) - pullop.repo.supported |
|
1342 | 1342 | if missing: |
|
1343 | 1343 | msg = _("required features are not" |
|
1344 | 1344 | " supported in the destination:" |
|
1345 | 1345 | " %s") % (', '.join(sorted(missing))) |
|
1346 | 1346 | raise error.Abort(msg) |
|
1347 | 1347 | |
|
1348 | 1348 | pullop.trmanager = transactionmanager(repo, 'pull', remote.url()) |
|
1349 | 1349 | with repo.wlock(), repo.lock(), pullop.trmanager: |
|
1350 | 1350 | # This should ideally be in _pullbundle2(). However, it needs to run |
|
1351 | 1351 | # before discovery to avoid extra work. |
|
1352 | 1352 | _maybeapplyclonebundle(pullop) |
|
1353 | 1353 | streamclone.maybeperformlegacystreamclone(pullop) |
|
1354 | 1354 | _pulldiscovery(pullop) |
|
1355 | 1355 | if pullop.canusebundle2: |
|
1356 | 1356 | _pullbundle2(pullop) |
|
1357 | 1357 | _pullchangeset(pullop) |
|
1358 | 1358 | _pullphase(pullop) |
|
1359 | 1359 | _pullbookmarks(pullop) |
|
1360 | 1360 | _pullobsolete(pullop) |
|
1361 | 1361 | |
|
1362 | 1362 | # storing remotenames |
|
1363 | 1363 | if repo.ui.configbool('experimental', 'remotenames'): |
|
1364 | 1364 | logexchange.pullremotenames(repo, remote) |
|
1365 | 1365 | |
|
1366 | 1366 | return pullop |
|
1367 | 1367 | |
|
1368 | 1368 | # list of steps to perform discovery before pull |
|
1369 | 1369 | pulldiscoveryorder = [] |
|
1370 | 1370 | |
|
1371 | 1371 | # Mapping between step name and function |
|
1372 | 1372 | # |
|
1373 | 1373 | # This exists to help extensions wrap steps if necessary |
|
1374 | 1374 | pulldiscoverymapping = {} |
|
1375 | 1375 | |
|
1376 | 1376 | def pulldiscovery(stepname): |
|
1377 | 1377 | """decorator for function performing discovery before pull |
|
1378 | 1378 | |
|
1379 | 1379 | The function is added to the step -> function mapping and appended to the |
|
1380 | 1380 | list of steps. Beware that decorated function will be added in order (this |
|
1381 | 1381 | may matter). |
|
1382 | 1382 | |
|
1383 | 1383 | You can only use this decorator for a new step, if you want to wrap a step |
|
1384 | 1384 | from an extension, change the pulldiscovery dictionary directly.""" |
|
1385 | 1385 | def dec(func): |
|
1386 | 1386 | assert stepname not in pulldiscoverymapping |
|
1387 | 1387 | pulldiscoverymapping[stepname] = func |
|
1388 | 1388 | pulldiscoveryorder.append(stepname) |
|
1389 | 1389 | return func |
|
1390 | 1390 | return dec |
|
1391 | 1391 | |
|
1392 | 1392 | def _pulldiscovery(pullop): |
|
1393 | 1393 | """Run all discovery steps""" |
|
1394 | 1394 | for stepname in pulldiscoveryorder: |
|
1395 | 1395 | step = pulldiscoverymapping[stepname] |
|
1396 | 1396 | step(pullop) |
|
1397 | 1397 | |
|
1398 | 1398 | @pulldiscovery('b1:bookmarks') |
|
1399 | 1399 | def _pullbookmarkbundle1(pullop): |
|
1400 | 1400 | """fetch bookmark data in bundle1 case |
|
1401 | 1401 | |
|
1402 | 1402 | If not using bundle2, we have to fetch bookmarks before changeset |
|
1403 | 1403 | discovery to reduce the chance and impact of race conditions.""" |
|
1404 | 1404 | if pullop.remotebookmarks is not None: |
|
1405 | 1405 | return |
|
1406 | 1406 | if pullop.canusebundle2 and 'listkeys' in pullop.remotebundle2caps: |
|
1407 | 1407 | # all known bundle2 servers now support listkeys, but lets be nice with |
|
1408 | 1408 | # new implementation. |
|
1409 | 1409 | return |
|
1410 | 1410 | books = pullop.remote.listkeys('bookmarks') |
|
1411 | 1411 | pullop.remotebookmarks = bookmod.unhexlifybookmarks(books) |
|
1412 | 1412 | |
|
1413 | 1413 | |
|
1414 | 1414 | @pulldiscovery('changegroup') |
|
1415 | 1415 | def _pulldiscoverychangegroup(pullop): |
|
1416 | 1416 | """discovery phase for the pull |
|
1417 | 1417 | |
|
1418 | 1418 | Current handle changeset discovery only, will change handle all discovery |
|
1419 | 1419 | at some point.""" |
|
1420 | 1420 | tmp = discovery.findcommonincoming(pullop.repo, |
|
1421 | 1421 | pullop.remote, |
|
1422 | 1422 | heads=pullop.heads, |
|
1423 | 1423 | force=pullop.force) |
|
1424 | 1424 | common, fetch, rheads = tmp |
|
1425 | 1425 | nm = pullop.repo.unfiltered().changelog.nodemap |
|
1426 | 1426 | if fetch and rheads: |
|
1427 | 1427 | # If a remote heads is filtered locally, put in back in common. |
|
1428 | 1428 | # |
|
1429 | 1429 | # This is a hackish solution to catch most of "common but locally |
|
1430 | 1430 | # hidden situation". We do not performs discovery on unfiltered |
|
1431 | 1431 | # repository because it end up doing a pathological amount of round |
|
1432 | 1432 | # trip for w huge amount of changeset we do not care about. |
|
1433 | 1433 | # |
|
1434 | 1434 | # If a set of such "common but filtered" changeset exist on the server |
|
1435 | 1435 | # but are not including a remote heads, we'll not be able to detect it, |
|
1436 | 1436 | scommon = set(common) |
|
1437 | 1437 | for n in rheads: |
|
1438 | 1438 | if n in nm: |
|
1439 | 1439 | if n not in scommon: |
|
1440 | 1440 | common.append(n) |
|
1441 | 1441 | if set(rheads).issubset(set(common)): |
|
1442 | 1442 | fetch = [] |
|
1443 | 1443 | pullop.common = common |
|
1444 | 1444 | pullop.fetch = fetch |
|
1445 | 1445 | pullop.rheads = rheads |
|
1446 | 1446 | |
|
1447 | 1447 | def _pullbundle2(pullop): |
|
1448 | 1448 | """pull data using bundle2 |
|
1449 | 1449 | |
|
1450 | 1450 | For now, the only supported data are changegroup.""" |
|
1451 | 1451 | kwargs = {'bundlecaps': caps20to10(pullop.repo)} |
|
1452 | 1452 | |
|
1453 | # make ui easier to access | |
|
1454 | ui = pullop.repo.ui | |
|
1455 | ||
|
1453 | 1456 | # At the moment we don't do stream clones over bundle2. If that is |
|
1454 | 1457 | # implemented then here's where the check for that will go. |
|
1455 | 1458 | streaming = False |
|
1456 | 1459 | |
|
1460 | # declare pull perimeters | |
|
1461 | kwargs['common'] = pullop.common | |
|
1462 | kwargs['heads'] = pullop.heads or pullop.rheads | |
|
1463 | ||
|
1457 | 1464 | # pulling changegroup |
|
1458 | 1465 | pullop.stepsdone.add('changegroup') |
|
1459 | 1466 | |
|
1460 | kwargs['common'] = pullop.common | |
|
1461 | kwargs['heads'] = pullop.heads or pullop.rheads | |
|
1462 | 1467 | kwargs['cg'] = pullop.fetch |
|
1463 | 1468 | |
|
1464 | ui = pullop.repo.ui | |
|
1465 | 1469 | legacyphase = 'phases' in ui.configlist('devel', 'legacy.exchange') |
|
1466 | 1470 | hasbinaryphase = 'heads' in pullop.remotebundle2caps.get('phases', ()) |
|
1467 | 1471 | if (not legacyphase and hasbinaryphase): |
|
1468 | 1472 | kwargs['phases'] = True |
|
1469 | 1473 | pullop.stepsdone.add('phases') |
|
1470 | 1474 | |
|
1475 | if 'listkeys' in pullop.remotebundle2caps: | |
|
1476 | if 'phases' not in pullop.stepsdone: | |
|
1477 | kwargs['listkeys'] = ['phases'] | |
|
1478 | ||
|
1471 | 1479 | bookmarksrequested = False |
|
1472 | 1480 | legacybookmark = 'bookmarks' in ui.configlist('devel', 'legacy.exchange') |
|
1473 | 1481 | hasbinarybook = 'bookmarks' in pullop.remotebundle2caps |
|
1474 | 1482 | |
|
1475 | 1483 | if pullop.remotebookmarks is not None: |
|
1476 | 1484 | pullop.stepsdone.add('request-bookmarks') |
|
1477 | 1485 | |
|
1478 | 1486 | if ('request-bookmarks' not in pullop.stepsdone |
|
1479 | 1487 | and pullop.remotebookmarks is None |
|
1480 | 1488 | and not legacybookmark and hasbinarybook): |
|
1481 | 1489 | kwargs['bookmarks'] = True |
|
1482 | 1490 | bookmarksrequested = True |
|
1483 | 1491 | |
|
1484 | 1492 | if 'listkeys' in pullop.remotebundle2caps: |
|
1485 | if 'phases' not in pullop.stepsdone: | |
|
1486 | kwargs['listkeys'] = ['phases'] | |
|
1487 | 1493 | if 'request-bookmarks' not in pullop.stepsdone: |
|
1488 | 1494 | # make sure to always includes bookmark data when migrating |
|
1489 | 1495 | # `hg incoming --bundle` to using this function. |
|
1490 | 1496 | pullop.stepsdone.add('request-bookmarks') |
|
1491 | 1497 | kwargs.setdefault('listkeys', []).append('bookmarks') |
|
1492 | 1498 | |
|
1493 | 1499 | # If this is a full pull / clone and the server supports the clone bundles |
|
1494 | 1500 | # feature, tell the server whether we attempted a clone bundle. The |
|
1495 | 1501 | # presence of this flag indicates the client supports clone bundles. This |
|
1496 | 1502 | # will enable the server to treat clients that support clone bundles |
|
1497 | 1503 | # differently from those that don't. |
|
1498 | 1504 | if (pullop.remote.capable('clonebundles') |
|
1499 | 1505 | and pullop.heads is None and list(pullop.common) == [nullid]): |
|
1500 | 1506 | kwargs['cbattempted'] = pullop.clonebundleattempted |
|
1501 | 1507 | |
|
1502 | 1508 | if streaming: |
|
1503 | 1509 | pullop.repo.ui.status(_('streaming all changes\n')) |
|
1504 | 1510 | elif not pullop.fetch: |
|
1505 | 1511 | pullop.repo.ui.status(_("no changes found\n")) |
|
1506 | 1512 | pullop.cgresult = 0 |
|
1507 | 1513 | else: |
|
1508 | 1514 | if pullop.heads is None and list(pullop.common) == [nullid]: |
|
1509 | 1515 | pullop.repo.ui.status(_("requesting all changes\n")) |
|
1510 | 1516 | if obsolete.isenabled(pullop.repo, obsolete.exchangeopt): |
|
1511 | 1517 | remoteversions = bundle2.obsmarkersversion(pullop.remotebundle2caps) |
|
1512 | 1518 | if obsolete.commonversion(remoteversions) is not None: |
|
1513 | 1519 | kwargs['obsmarkers'] = True |
|
1514 | 1520 | pullop.stepsdone.add('obsmarkers') |
|
1515 | 1521 | _pullbundle2extraprepare(pullop, kwargs) |
|
1516 | 1522 | bundle = pullop.remote.getbundle('pull', **pycompat.strkwargs(kwargs)) |
|
1517 | 1523 | try: |
|
1518 | 1524 | op = bundle2.bundleoperation(pullop.repo, pullop.gettransaction) |
|
1519 | 1525 | op.modes['bookmarks'] = 'records' |
|
1520 | 1526 | bundle2.processbundle(pullop.repo, bundle, op=op) |
|
1521 | 1527 | except bundle2.AbortFromPart as exc: |
|
1522 | 1528 | pullop.repo.ui.status(_('remote: abort: %s\n') % exc) |
|
1523 | 1529 | raise error.Abort(_('pull failed on remote'), hint=exc.hint) |
|
1524 | 1530 | except error.BundleValueError as exc: |
|
1525 | 1531 | raise error.Abort(_('missing support for %s') % exc) |
|
1526 | 1532 | |
|
1527 | 1533 | if pullop.fetch: |
|
1528 | 1534 | pullop.cgresult = bundle2.combinechangegroupresults(op) |
|
1529 | 1535 | |
|
1530 | 1536 | # processing phases change |
|
1531 | 1537 | for namespace, value in op.records['listkeys']: |
|
1532 | 1538 | if namespace == 'phases': |
|
1533 | 1539 | _pullapplyphases(pullop, value) |
|
1534 | 1540 | |
|
1535 | 1541 | # processing bookmark update |
|
1536 | 1542 | if bookmarksrequested: |
|
1537 | 1543 | books = {} |
|
1538 | 1544 | for record in op.records['bookmarks']: |
|
1539 | 1545 | books[record['bookmark']] = record["node"] |
|
1540 | 1546 | pullop.remotebookmarks = books |
|
1541 | 1547 | else: |
|
1542 | 1548 | for namespace, value in op.records['listkeys']: |
|
1543 | 1549 | if namespace == 'bookmarks': |
|
1544 | 1550 | pullop.remotebookmarks = bookmod.unhexlifybookmarks(value) |
|
1545 | 1551 | |
|
1546 | 1552 | # bookmark data were either already there or pulled in the bundle |
|
1547 | 1553 | if pullop.remotebookmarks is not None: |
|
1548 | 1554 | _pullbookmarks(pullop) |
|
1549 | 1555 | |
|
1550 | 1556 | def _pullbundle2extraprepare(pullop, kwargs): |
|
1551 | 1557 | """hook function so that extensions can extend the getbundle call""" |
|
1552 | 1558 | |
|
1553 | 1559 | def _pullchangeset(pullop): |
|
1554 | 1560 | """pull changeset from unbundle into the local repo""" |
|
1555 | 1561 | # We delay the open of the transaction as late as possible so we |
|
1556 | 1562 | # don't open transaction for nothing or you break future useful |
|
1557 | 1563 | # rollback call |
|
1558 | 1564 | if 'changegroup' in pullop.stepsdone: |
|
1559 | 1565 | return |
|
1560 | 1566 | pullop.stepsdone.add('changegroup') |
|
1561 | 1567 | if not pullop.fetch: |
|
1562 | 1568 | pullop.repo.ui.status(_("no changes found\n")) |
|
1563 | 1569 | pullop.cgresult = 0 |
|
1564 | 1570 | return |
|
1565 | 1571 | tr = pullop.gettransaction() |
|
1566 | 1572 | if pullop.heads is None and list(pullop.common) == [nullid]: |
|
1567 | 1573 | pullop.repo.ui.status(_("requesting all changes\n")) |
|
1568 | 1574 | elif pullop.heads is None and pullop.remote.capable('changegroupsubset'): |
|
1569 | 1575 | # issue1320, avoid a race if remote changed after discovery |
|
1570 | 1576 | pullop.heads = pullop.rheads |
|
1571 | 1577 | |
|
1572 | 1578 | if pullop.remote.capable('getbundle'): |
|
1573 | 1579 | # TODO: get bundlecaps from remote |
|
1574 | 1580 | cg = pullop.remote.getbundle('pull', common=pullop.common, |
|
1575 | 1581 | heads=pullop.heads or pullop.rheads) |
|
1576 | 1582 | elif pullop.heads is None: |
|
1577 | 1583 | cg = pullop.remote.changegroup(pullop.fetch, 'pull') |
|
1578 | 1584 | elif not pullop.remote.capable('changegroupsubset'): |
|
1579 | 1585 | raise error.Abort(_("partial pull cannot be done because " |
|
1580 | 1586 | "other repository doesn't support " |
|
1581 | 1587 | "changegroupsubset.")) |
|
1582 | 1588 | else: |
|
1583 | 1589 | cg = pullop.remote.changegroupsubset(pullop.fetch, pullop.heads, 'pull') |
|
1584 | 1590 | bundleop = bundle2.applybundle(pullop.repo, cg, tr, 'pull', |
|
1585 | 1591 | pullop.remote.url()) |
|
1586 | 1592 | pullop.cgresult = bundle2.combinechangegroupresults(bundleop) |
|
1587 | 1593 | |
|
1588 | 1594 | def _pullphase(pullop): |
|
1589 | 1595 | # Get remote phases data from remote |
|
1590 | 1596 | if 'phases' in pullop.stepsdone: |
|
1591 | 1597 | return |
|
1592 | 1598 | remotephases = pullop.remote.listkeys('phases') |
|
1593 | 1599 | _pullapplyphases(pullop, remotephases) |
|
1594 | 1600 | |
|
1595 | 1601 | def _pullapplyphases(pullop, remotephases): |
|
1596 | 1602 | """apply phase movement from observed remote state""" |
|
1597 | 1603 | if 'phases' in pullop.stepsdone: |
|
1598 | 1604 | return |
|
1599 | 1605 | pullop.stepsdone.add('phases') |
|
1600 | 1606 | publishing = bool(remotephases.get('publishing', False)) |
|
1601 | 1607 | if remotephases and not publishing: |
|
1602 | 1608 | # remote is new and non-publishing |
|
1603 | 1609 | pheads, _dr = phases.analyzeremotephases(pullop.repo, |
|
1604 | 1610 | pullop.pulledsubset, |
|
1605 | 1611 | remotephases) |
|
1606 | 1612 | dheads = pullop.pulledsubset |
|
1607 | 1613 | else: |
|
1608 | 1614 | # Remote is old or publishing all common changesets |
|
1609 | 1615 | # should be seen as public |
|
1610 | 1616 | pheads = pullop.pulledsubset |
|
1611 | 1617 | dheads = [] |
|
1612 | 1618 | unfi = pullop.repo.unfiltered() |
|
1613 | 1619 | phase = unfi._phasecache.phase |
|
1614 | 1620 | rev = unfi.changelog.nodemap.get |
|
1615 | 1621 | public = phases.public |
|
1616 | 1622 | draft = phases.draft |
|
1617 | 1623 | |
|
1618 | 1624 | # exclude changesets already public locally and update the others |
|
1619 | 1625 | pheads = [pn for pn in pheads if phase(unfi, rev(pn)) > public] |
|
1620 | 1626 | if pheads: |
|
1621 | 1627 | tr = pullop.gettransaction() |
|
1622 | 1628 | phases.advanceboundary(pullop.repo, tr, public, pheads) |
|
1623 | 1629 | |
|
1624 | 1630 | # exclude changesets already draft locally and update the others |
|
1625 | 1631 | dheads = [pn for pn in dheads if phase(unfi, rev(pn)) > draft] |
|
1626 | 1632 | if dheads: |
|
1627 | 1633 | tr = pullop.gettransaction() |
|
1628 | 1634 | phases.advanceboundary(pullop.repo, tr, draft, dheads) |
|
1629 | 1635 | |
|
1630 | 1636 | def _pullbookmarks(pullop): |
|
1631 | 1637 | """process the remote bookmark information to update the local one""" |
|
1632 | 1638 | if 'bookmarks' in pullop.stepsdone: |
|
1633 | 1639 | return |
|
1634 | 1640 | pullop.stepsdone.add('bookmarks') |
|
1635 | 1641 | repo = pullop.repo |
|
1636 | 1642 | remotebookmarks = pullop.remotebookmarks |
|
1637 | 1643 | bookmod.updatefromremote(repo.ui, repo, remotebookmarks, |
|
1638 | 1644 | pullop.remote.url(), |
|
1639 | 1645 | pullop.gettransaction, |
|
1640 | 1646 | explicit=pullop.explicitbookmarks) |
|
1641 | 1647 | |
|
1642 | 1648 | def _pullobsolete(pullop): |
|
1643 | 1649 | """utility function to pull obsolete markers from a remote |
|
1644 | 1650 | |
|
1645 | 1651 | The `gettransaction` is function that return the pull transaction, creating |
|
1646 | 1652 | one if necessary. We return the transaction to inform the calling code that |
|
1647 | 1653 | a new transaction have been created (when applicable). |
|
1648 | 1654 | |
|
1649 | 1655 | Exists mostly to allow overriding for experimentation purpose""" |
|
1650 | 1656 | if 'obsmarkers' in pullop.stepsdone: |
|
1651 | 1657 | return |
|
1652 | 1658 | pullop.stepsdone.add('obsmarkers') |
|
1653 | 1659 | tr = None |
|
1654 | 1660 | if obsolete.isenabled(pullop.repo, obsolete.exchangeopt): |
|
1655 | 1661 | pullop.repo.ui.debug('fetching remote obsolete markers\n') |
|
1656 | 1662 | remoteobs = pullop.remote.listkeys('obsolete') |
|
1657 | 1663 | if 'dump0' in remoteobs: |
|
1658 | 1664 | tr = pullop.gettransaction() |
|
1659 | 1665 | markers = [] |
|
1660 | 1666 | for key in sorted(remoteobs, reverse=True): |
|
1661 | 1667 | if key.startswith('dump'): |
|
1662 | 1668 | data = util.b85decode(remoteobs[key]) |
|
1663 | 1669 | version, newmarks = obsolete._readmarkers(data) |
|
1664 | 1670 | markers += newmarks |
|
1665 | 1671 | if markers: |
|
1666 | 1672 | pullop.repo.obsstore.add(tr, markers) |
|
1667 | 1673 | pullop.repo.invalidatevolatilesets() |
|
1668 | 1674 | return tr |
|
1669 | 1675 | |
|
1670 | 1676 | def caps20to10(repo): |
|
1671 | 1677 | """return a set with appropriate options to use bundle20 during getbundle""" |
|
1672 | 1678 | caps = {'HG20'} |
|
1673 | 1679 | capsblob = bundle2.encodecaps(bundle2.getrepocaps(repo)) |
|
1674 | 1680 | caps.add('bundle2=' + urlreq.quote(capsblob)) |
|
1675 | 1681 | return caps |
|
1676 | 1682 | |
|
1677 | 1683 | # List of names of steps to perform for a bundle2 for getbundle, order matters. |
|
1678 | 1684 | getbundle2partsorder = [] |
|
1679 | 1685 | |
|
1680 | 1686 | # Mapping between step name and function |
|
1681 | 1687 | # |
|
1682 | 1688 | # This exists to help extensions wrap steps if necessary |
|
1683 | 1689 | getbundle2partsmapping = {} |
|
1684 | 1690 | |
|
1685 | 1691 | def getbundle2partsgenerator(stepname, idx=None): |
|
1686 | 1692 | """decorator for function generating bundle2 part for getbundle |
|
1687 | 1693 | |
|
1688 | 1694 | The function is added to the step -> function mapping and appended to the |
|
1689 | 1695 | list of steps. Beware that decorated functions will be added in order |
|
1690 | 1696 | (this may matter). |
|
1691 | 1697 | |
|
1692 | 1698 | You can only use this decorator for new steps, if you want to wrap a step |
|
1693 | 1699 | from an extension, attack the getbundle2partsmapping dictionary directly.""" |
|
1694 | 1700 | def dec(func): |
|
1695 | 1701 | assert stepname not in getbundle2partsmapping |
|
1696 | 1702 | getbundle2partsmapping[stepname] = func |
|
1697 | 1703 | if idx is None: |
|
1698 | 1704 | getbundle2partsorder.append(stepname) |
|
1699 | 1705 | else: |
|
1700 | 1706 | getbundle2partsorder.insert(idx, stepname) |
|
1701 | 1707 | return func |
|
1702 | 1708 | return dec |
|
1703 | 1709 | |
|
1704 | 1710 | def bundle2requested(bundlecaps): |
|
1705 | 1711 | if bundlecaps is not None: |
|
1706 | 1712 | return any(cap.startswith('HG2') for cap in bundlecaps) |
|
1707 | 1713 | return False |
|
1708 | 1714 | |
|
1709 | 1715 | def getbundlechunks(repo, source, heads=None, common=None, bundlecaps=None, |
|
1710 | 1716 | **kwargs): |
|
1711 | 1717 | """Return chunks constituting a bundle's raw data. |
|
1712 | 1718 | |
|
1713 | 1719 | Could be a bundle HG10 or a bundle HG20 depending on bundlecaps |
|
1714 | 1720 | passed. |
|
1715 | 1721 | |
|
1716 | 1722 | Returns an iterator over raw chunks (of varying sizes). |
|
1717 | 1723 | """ |
|
1718 | 1724 | kwargs = pycompat.byteskwargs(kwargs) |
|
1719 | 1725 | usebundle2 = bundle2requested(bundlecaps) |
|
1720 | 1726 | # bundle10 case |
|
1721 | 1727 | if not usebundle2: |
|
1722 | 1728 | if bundlecaps and not kwargs.get('cg', True): |
|
1723 | 1729 | raise ValueError(_('request for bundle10 must include changegroup')) |
|
1724 | 1730 | |
|
1725 | 1731 | if kwargs: |
|
1726 | 1732 | raise ValueError(_('unsupported getbundle arguments: %s') |
|
1727 | 1733 | % ', '.join(sorted(kwargs.keys()))) |
|
1728 | 1734 | outgoing = _computeoutgoing(repo, heads, common) |
|
1729 | 1735 | return changegroup.makestream(repo, outgoing, '01', source, |
|
1730 | 1736 | bundlecaps=bundlecaps) |
|
1731 | 1737 | |
|
1732 | 1738 | # bundle20 case |
|
1733 | 1739 | b2caps = {} |
|
1734 | 1740 | for bcaps in bundlecaps: |
|
1735 | 1741 | if bcaps.startswith('bundle2='): |
|
1736 | 1742 | blob = urlreq.unquote(bcaps[len('bundle2='):]) |
|
1737 | 1743 | b2caps.update(bundle2.decodecaps(blob)) |
|
1738 | 1744 | bundler = bundle2.bundle20(repo.ui, b2caps) |
|
1739 | 1745 | |
|
1740 | 1746 | kwargs['heads'] = heads |
|
1741 | 1747 | kwargs['common'] = common |
|
1742 | 1748 | |
|
1743 | 1749 | for name in getbundle2partsorder: |
|
1744 | 1750 | func = getbundle2partsmapping[name] |
|
1745 | 1751 | func(bundler, repo, source, bundlecaps=bundlecaps, b2caps=b2caps, |
|
1746 | 1752 | **pycompat.strkwargs(kwargs)) |
|
1747 | 1753 | |
|
1748 | 1754 | return bundler.getchunks() |
|
1749 | 1755 | |
|
1750 | 1756 | @getbundle2partsgenerator('stream') |
|
1751 | 1757 | def _getbundlestream(bundler, repo, source, bundlecaps=None, |
|
1752 | 1758 | b2caps=None, heads=None, common=None, **kwargs): |
|
1753 | 1759 | if not kwargs.get('stream', False): |
|
1754 | 1760 | return |
|
1755 | 1761 | filecount, bytecount, it = streamclone.generatev2(repo) |
|
1756 | 1762 | requirements = ' '.join(repo.requirements) |
|
1757 | 1763 | part = bundler.newpart('stream', data=it) |
|
1758 | 1764 | part.addparam('bytecount', '%d' % bytecount, mandatory=True) |
|
1759 | 1765 | part.addparam('filecount', '%d' % filecount, mandatory=True) |
|
1760 | 1766 | part.addparam('requirements', requirements, mandatory=True) |
|
1761 | 1767 | part.addparam('version', 'v2', mandatory=True) |
|
1762 | 1768 | |
|
1763 | 1769 | @getbundle2partsgenerator('changegroup') |
|
1764 | 1770 | def _getbundlechangegrouppart(bundler, repo, source, bundlecaps=None, |
|
1765 | 1771 | b2caps=None, heads=None, common=None, **kwargs): |
|
1766 | 1772 | """add a changegroup part to the requested bundle""" |
|
1767 | 1773 | cgstream = None |
|
1768 | 1774 | if kwargs.get(r'cg', True): |
|
1769 | 1775 | # build changegroup bundle here. |
|
1770 | 1776 | version = '01' |
|
1771 | 1777 | cgversions = b2caps.get('changegroup') |
|
1772 | 1778 | if cgversions: # 3.1 and 3.2 ship with an empty value |
|
1773 | 1779 | cgversions = [v for v in cgversions |
|
1774 | 1780 | if v in changegroup.supportedoutgoingversions(repo)] |
|
1775 | 1781 | if not cgversions: |
|
1776 | 1782 | raise ValueError(_('no common changegroup version')) |
|
1777 | 1783 | version = max(cgversions) |
|
1778 | 1784 | outgoing = _computeoutgoing(repo, heads, common) |
|
1779 | 1785 | if outgoing.missing: |
|
1780 | 1786 | cgstream = changegroup.makestream(repo, outgoing, version, source, |
|
1781 | 1787 | bundlecaps=bundlecaps) |
|
1782 | 1788 | |
|
1783 | 1789 | if cgstream: |
|
1784 | 1790 | part = bundler.newpart('changegroup', data=cgstream) |
|
1785 | 1791 | if cgversions: |
|
1786 | 1792 | part.addparam('version', version) |
|
1787 | 1793 | part.addparam('nbchanges', '%d' % len(outgoing.missing), |
|
1788 | 1794 | mandatory=False) |
|
1789 | 1795 | if 'treemanifest' in repo.requirements: |
|
1790 | 1796 | part.addparam('treemanifest', '1') |
|
1791 | 1797 | |
|
1792 | 1798 | @getbundle2partsgenerator('bookmarks') |
|
1793 | 1799 | def _getbundlebookmarkpart(bundler, repo, source, bundlecaps=None, |
|
1794 | 1800 | b2caps=None, **kwargs): |
|
1795 | 1801 | """add a bookmark part to the requested bundle""" |
|
1796 | 1802 | if not kwargs.get(r'bookmarks', False): |
|
1797 | 1803 | return |
|
1798 | 1804 | if 'bookmarks' not in b2caps: |
|
1799 | 1805 | raise ValueError(_('no common bookmarks exchange method')) |
|
1800 | 1806 | books = bookmod.listbinbookmarks(repo) |
|
1801 | 1807 | data = bookmod.binaryencode(books) |
|
1802 | 1808 | if data: |
|
1803 | 1809 | bundler.newpart('bookmarks', data=data) |
|
1804 | 1810 | |
|
1805 | 1811 | @getbundle2partsgenerator('listkeys') |
|
1806 | 1812 | def _getbundlelistkeysparts(bundler, repo, source, bundlecaps=None, |
|
1807 | 1813 | b2caps=None, **kwargs): |
|
1808 | 1814 | """add parts containing listkeys namespaces to the requested bundle""" |
|
1809 | 1815 | listkeys = kwargs.get(r'listkeys', ()) |
|
1810 | 1816 | for namespace in listkeys: |
|
1811 | 1817 | part = bundler.newpart('listkeys') |
|
1812 | 1818 | part.addparam('namespace', namespace) |
|
1813 | 1819 | keys = repo.listkeys(namespace).items() |
|
1814 | 1820 | part.data = pushkey.encodekeys(keys) |
|
1815 | 1821 | |
|
1816 | 1822 | @getbundle2partsgenerator('obsmarkers') |
|
1817 | 1823 | def _getbundleobsmarkerpart(bundler, repo, source, bundlecaps=None, |
|
1818 | 1824 | b2caps=None, heads=None, **kwargs): |
|
1819 | 1825 | """add an obsolescence markers part to the requested bundle""" |
|
1820 | 1826 | if kwargs.get(r'obsmarkers', False): |
|
1821 | 1827 | if heads is None: |
|
1822 | 1828 | heads = repo.heads() |
|
1823 | 1829 | subset = [c.node() for c in repo.set('::%ln', heads)] |
|
1824 | 1830 | markers = repo.obsstore.relevantmarkers(subset) |
|
1825 | 1831 | markers = sorted(markers) |
|
1826 | 1832 | bundle2.buildobsmarkerspart(bundler, markers) |
|
1827 | 1833 | |
|
1828 | 1834 | @getbundle2partsgenerator('phases') |
|
1829 | 1835 | def _getbundlephasespart(bundler, repo, source, bundlecaps=None, |
|
1830 | 1836 | b2caps=None, heads=None, **kwargs): |
|
1831 | 1837 | """add phase heads part to the requested bundle""" |
|
1832 | 1838 | if kwargs.get(r'phases', False): |
|
1833 | 1839 | if not 'heads' in b2caps.get('phases'): |
|
1834 | 1840 | raise ValueError(_('no common phases exchange method')) |
|
1835 | 1841 | if heads is None: |
|
1836 | 1842 | heads = repo.heads() |
|
1837 | 1843 | |
|
1838 | 1844 | headsbyphase = collections.defaultdict(set) |
|
1839 | 1845 | if repo.publishing(): |
|
1840 | 1846 | headsbyphase[phases.public] = heads |
|
1841 | 1847 | else: |
|
1842 | 1848 | # find the appropriate heads to move |
|
1843 | 1849 | |
|
1844 | 1850 | phase = repo._phasecache.phase |
|
1845 | 1851 | node = repo.changelog.node |
|
1846 | 1852 | rev = repo.changelog.rev |
|
1847 | 1853 | for h in heads: |
|
1848 | 1854 | headsbyphase[phase(repo, rev(h))].add(h) |
|
1849 | 1855 | seenphases = list(headsbyphase.keys()) |
|
1850 | 1856 | |
|
1851 | 1857 | # We do not handle anything but public and draft phase for now) |
|
1852 | 1858 | if seenphases: |
|
1853 | 1859 | assert max(seenphases) <= phases.draft |
|
1854 | 1860 | |
|
1855 | 1861 | # if client is pulling non-public changesets, we need to find |
|
1856 | 1862 | # intermediate public heads. |
|
1857 | 1863 | draftheads = headsbyphase.get(phases.draft, set()) |
|
1858 | 1864 | if draftheads: |
|
1859 | 1865 | publicheads = headsbyphase.get(phases.public, set()) |
|
1860 | 1866 | |
|
1861 | 1867 | revset = 'heads(only(%ln, %ln) and public())' |
|
1862 | 1868 | extraheads = repo.revs(revset, draftheads, publicheads) |
|
1863 | 1869 | for r in extraheads: |
|
1864 | 1870 | headsbyphase[phases.public].add(node(r)) |
|
1865 | 1871 | |
|
1866 | 1872 | # transform data in a format used by the encoding function |
|
1867 | 1873 | phasemapping = [] |
|
1868 | 1874 | for phase in phases.allphases: |
|
1869 | 1875 | phasemapping.append(sorted(headsbyphase[phase])) |
|
1870 | 1876 | |
|
1871 | 1877 | # generate the actual part |
|
1872 | 1878 | phasedata = phases.binaryencode(phasemapping) |
|
1873 | 1879 | bundler.newpart('phase-heads', data=phasedata) |
|
1874 | 1880 | |
|
1875 | 1881 | @getbundle2partsgenerator('hgtagsfnodes') |
|
1876 | 1882 | def _getbundletagsfnodes(bundler, repo, source, bundlecaps=None, |
|
1877 | 1883 | b2caps=None, heads=None, common=None, |
|
1878 | 1884 | **kwargs): |
|
1879 | 1885 | """Transfer the .hgtags filenodes mapping. |
|
1880 | 1886 | |
|
1881 | 1887 | Only values for heads in this bundle will be transferred. |
|
1882 | 1888 | |
|
1883 | 1889 | The part data consists of pairs of 20 byte changeset node and .hgtags |
|
1884 | 1890 | filenodes raw values. |
|
1885 | 1891 | """ |
|
1886 | 1892 | # Don't send unless: |
|
1887 | 1893 | # - changeset are being exchanged, |
|
1888 | 1894 | # - the client supports it. |
|
1889 | 1895 | if not (kwargs.get(r'cg', True) and 'hgtagsfnodes' in b2caps): |
|
1890 | 1896 | return |
|
1891 | 1897 | |
|
1892 | 1898 | outgoing = _computeoutgoing(repo, heads, common) |
|
1893 | 1899 | bundle2.addparttagsfnodescache(repo, bundler, outgoing) |
|
1894 | 1900 | |
|
1895 | 1901 | def check_heads(repo, their_heads, context): |
|
1896 | 1902 | """check if the heads of a repo have been modified |
|
1897 | 1903 | |
|
1898 | 1904 | Used by peer for unbundling. |
|
1899 | 1905 | """ |
|
1900 | 1906 | heads = repo.heads() |
|
1901 | 1907 | heads_hash = hashlib.sha1(''.join(sorted(heads))).digest() |
|
1902 | 1908 | if not (their_heads == ['force'] or their_heads == heads or |
|
1903 | 1909 | their_heads == ['hashed', heads_hash]): |
|
1904 | 1910 | # someone else committed/pushed/unbundled while we |
|
1905 | 1911 | # were transferring data |
|
1906 | 1912 | raise error.PushRaced('repository changed while %s - ' |
|
1907 | 1913 | 'please try again' % context) |
|
1908 | 1914 | |
|
1909 | 1915 | def unbundle(repo, cg, heads, source, url): |
|
1910 | 1916 | """Apply a bundle to a repo. |
|
1911 | 1917 | |
|
1912 | 1918 | this function makes sure the repo is locked during the application and have |
|
1913 | 1919 | mechanism to check that no push race occurred between the creation of the |
|
1914 | 1920 | bundle and its application. |
|
1915 | 1921 | |
|
1916 | 1922 | If the push was raced as PushRaced exception is raised.""" |
|
1917 | 1923 | r = 0 |
|
1918 | 1924 | # need a transaction when processing a bundle2 stream |
|
1919 | 1925 | # [wlock, lock, tr] - needs to be an array so nested functions can modify it |
|
1920 | 1926 | lockandtr = [None, None, None] |
|
1921 | 1927 | recordout = None |
|
1922 | 1928 | # quick fix for output mismatch with bundle2 in 3.4 |
|
1923 | 1929 | captureoutput = repo.ui.configbool('experimental', 'bundle2-output-capture') |
|
1924 | 1930 | if url.startswith('remote:http:') or url.startswith('remote:https:'): |
|
1925 | 1931 | captureoutput = True |
|
1926 | 1932 | try: |
|
1927 | 1933 | # note: outside bundle1, 'heads' is expected to be empty and this |
|
1928 | 1934 | # 'check_heads' call wil be a no-op |
|
1929 | 1935 | check_heads(repo, heads, 'uploading changes') |
|
1930 | 1936 | # push can proceed |
|
1931 | 1937 | if not isinstance(cg, bundle2.unbundle20): |
|
1932 | 1938 | # legacy case: bundle1 (changegroup 01) |
|
1933 | 1939 | txnname = "\n".join([source, util.hidepassword(url)]) |
|
1934 | 1940 | with repo.lock(), repo.transaction(txnname) as tr: |
|
1935 | 1941 | op = bundle2.applybundle(repo, cg, tr, source, url) |
|
1936 | 1942 | r = bundle2.combinechangegroupresults(op) |
|
1937 | 1943 | else: |
|
1938 | 1944 | r = None |
|
1939 | 1945 | try: |
|
1940 | 1946 | def gettransaction(): |
|
1941 | 1947 | if not lockandtr[2]: |
|
1942 | 1948 | lockandtr[0] = repo.wlock() |
|
1943 | 1949 | lockandtr[1] = repo.lock() |
|
1944 | 1950 | lockandtr[2] = repo.transaction(source) |
|
1945 | 1951 | lockandtr[2].hookargs['source'] = source |
|
1946 | 1952 | lockandtr[2].hookargs['url'] = url |
|
1947 | 1953 | lockandtr[2].hookargs['bundle2'] = '1' |
|
1948 | 1954 | return lockandtr[2] |
|
1949 | 1955 | |
|
1950 | 1956 | # Do greedy locking by default until we're satisfied with lazy |
|
1951 | 1957 | # locking. |
|
1952 | 1958 | if not repo.ui.configbool('experimental', 'bundle2lazylocking'): |
|
1953 | 1959 | gettransaction() |
|
1954 | 1960 | |
|
1955 | 1961 | op = bundle2.bundleoperation(repo, gettransaction, |
|
1956 | 1962 | captureoutput=captureoutput) |
|
1957 | 1963 | try: |
|
1958 | 1964 | op = bundle2.processbundle(repo, cg, op=op) |
|
1959 | 1965 | finally: |
|
1960 | 1966 | r = op.reply |
|
1961 | 1967 | if captureoutput and r is not None: |
|
1962 | 1968 | repo.ui.pushbuffer(error=True, subproc=True) |
|
1963 | 1969 | def recordout(output): |
|
1964 | 1970 | r.newpart('output', data=output, mandatory=False) |
|
1965 | 1971 | if lockandtr[2] is not None: |
|
1966 | 1972 | lockandtr[2].close() |
|
1967 | 1973 | except BaseException as exc: |
|
1968 | 1974 | exc.duringunbundle2 = True |
|
1969 | 1975 | if captureoutput and r is not None: |
|
1970 | 1976 | parts = exc._bundle2salvagedoutput = r.salvageoutput() |
|
1971 | 1977 | def recordout(output): |
|
1972 | 1978 | part = bundle2.bundlepart('output', data=output, |
|
1973 | 1979 | mandatory=False) |
|
1974 | 1980 | parts.append(part) |
|
1975 | 1981 | raise |
|
1976 | 1982 | finally: |
|
1977 | 1983 | lockmod.release(lockandtr[2], lockandtr[1], lockandtr[0]) |
|
1978 | 1984 | if recordout is not None: |
|
1979 | 1985 | recordout(repo.ui.popbuffer()) |
|
1980 | 1986 | return r |
|
1981 | 1987 | |
|
1982 | 1988 | def _maybeapplyclonebundle(pullop): |
|
1983 | 1989 | """Apply a clone bundle from a remote, if possible.""" |
|
1984 | 1990 | |
|
1985 | 1991 | repo = pullop.repo |
|
1986 | 1992 | remote = pullop.remote |
|
1987 | 1993 | |
|
1988 | 1994 | if not repo.ui.configbool('ui', 'clonebundles'): |
|
1989 | 1995 | return |
|
1990 | 1996 | |
|
1991 | 1997 | # Only run if local repo is empty. |
|
1992 | 1998 | if len(repo): |
|
1993 | 1999 | return |
|
1994 | 2000 | |
|
1995 | 2001 | if pullop.heads: |
|
1996 | 2002 | return |
|
1997 | 2003 | |
|
1998 | 2004 | if not remote.capable('clonebundles'): |
|
1999 | 2005 | return |
|
2000 | 2006 | |
|
2001 | 2007 | res = remote._call('clonebundles') |
|
2002 | 2008 | |
|
2003 | 2009 | # If we call the wire protocol command, that's good enough to record the |
|
2004 | 2010 | # attempt. |
|
2005 | 2011 | pullop.clonebundleattempted = True |
|
2006 | 2012 | |
|
2007 | 2013 | entries = parseclonebundlesmanifest(repo, res) |
|
2008 | 2014 | if not entries: |
|
2009 | 2015 | repo.ui.note(_('no clone bundles available on remote; ' |
|
2010 | 2016 | 'falling back to regular clone\n')) |
|
2011 | 2017 | return |
|
2012 | 2018 | |
|
2013 | 2019 | entries = filterclonebundleentries( |
|
2014 | 2020 | repo, entries, streamclonerequested=pullop.streamclonerequested) |
|
2015 | 2021 | |
|
2016 | 2022 | if not entries: |
|
2017 | 2023 | # There is a thundering herd concern here. However, if a server |
|
2018 | 2024 | # operator doesn't advertise bundles appropriate for its clients, |
|
2019 | 2025 | # they deserve what's coming. Furthermore, from a client's |
|
2020 | 2026 | # perspective, no automatic fallback would mean not being able to |
|
2021 | 2027 | # clone! |
|
2022 | 2028 | repo.ui.warn(_('no compatible clone bundles available on server; ' |
|
2023 | 2029 | 'falling back to regular clone\n')) |
|
2024 | 2030 | repo.ui.warn(_('(you may want to report this to the server ' |
|
2025 | 2031 | 'operator)\n')) |
|
2026 | 2032 | return |
|
2027 | 2033 | |
|
2028 | 2034 | entries = sortclonebundleentries(repo.ui, entries) |
|
2029 | 2035 | |
|
2030 | 2036 | url = entries[0]['URL'] |
|
2031 | 2037 | repo.ui.status(_('applying clone bundle from %s\n') % url) |
|
2032 | 2038 | if trypullbundlefromurl(repo.ui, repo, url): |
|
2033 | 2039 | repo.ui.status(_('finished applying clone bundle\n')) |
|
2034 | 2040 | # Bundle failed. |
|
2035 | 2041 | # |
|
2036 | 2042 | # We abort by default to avoid the thundering herd of |
|
2037 | 2043 | # clients flooding a server that was expecting expensive |
|
2038 | 2044 | # clone load to be offloaded. |
|
2039 | 2045 | elif repo.ui.configbool('ui', 'clonebundlefallback'): |
|
2040 | 2046 | repo.ui.warn(_('falling back to normal clone\n')) |
|
2041 | 2047 | else: |
|
2042 | 2048 | raise error.Abort(_('error applying bundle'), |
|
2043 | 2049 | hint=_('if this error persists, consider contacting ' |
|
2044 | 2050 | 'the server operator or disable clone ' |
|
2045 | 2051 | 'bundles via ' |
|
2046 | 2052 | '"--config ui.clonebundles=false"')) |
|
2047 | 2053 | |
|
2048 | 2054 | def parseclonebundlesmanifest(repo, s): |
|
2049 | 2055 | """Parses the raw text of a clone bundles manifest. |
|
2050 | 2056 | |
|
2051 | 2057 | Returns a list of dicts. The dicts have a ``URL`` key corresponding |
|
2052 | 2058 | to the URL and other keys are the attributes for the entry. |
|
2053 | 2059 | """ |
|
2054 | 2060 | m = [] |
|
2055 | 2061 | for line in s.splitlines(): |
|
2056 | 2062 | fields = line.split() |
|
2057 | 2063 | if not fields: |
|
2058 | 2064 | continue |
|
2059 | 2065 | attrs = {'URL': fields[0]} |
|
2060 | 2066 | for rawattr in fields[1:]: |
|
2061 | 2067 | key, value = rawattr.split('=', 1) |
|
2062 | 2068 | key = urlreq.unquote(key) |
|
2063 | 2069 | value = urlreq.unquote(value) |
|
2064 | 2070 | attrs[key] = value |
|
2065 | 2071 | |
|
2066 | 2072 | # Parse BUNDLESPEC into components. This makes client-side |
|
2067 | 2073 | # preferences easier to specify since you can prefer a single |
|
2068 | 2074 | # component of the BUNDLESPEC. |
|
2069 | 2075 | if key == 'BUNDLESPEC': |
|
2070 | 2076 | try: |
|
2071 | 2077 | comp, version, params = parsebundlespec(repo, value, |
|
2072 | 2078 | externalnames=True) |
|
2073 | 2079 | attrs['COMPRESSION'] = comp |
|
2074 | 2080 | attrs['VERSION'] = version |
|
2075 | 2081 | except error.InvalidBundleSpecification: |
|
2076 | 2082 | pass |
|
2077 | 2083 | except error.UnsupportedBundleSpecification: |
|
2078 | 2084 | pass |
|
2079 | 2085 | |
|
2080 | 2086 | m.append(attrs) |
|
2081 | 2087 | |
|
2082 | 2088 | return m |
|
2083 | 2089 | |
|
2084 | 2090 | def filterclonebundleentries(repo, entries, streamclonerequested=False): |
|
2085 | 2091 | """Remove incompatible clone bundle manifest entries. |
|
2086 | 2092 | |
|
2087 | 2093 | Accepts a list of entries parsed with ``parseclonebundlesmanifest`` |
|
2088 | 2094 | and returns a new list consisting of only the entries that this client |
|
2089 | 2095 | should be able to apply. |
|
2090 | 2096 | |
|
2091 | 2097 | There is no guarantee we'll be able to apply all returned entries because |
|
2092 | 2098 | the metadata we use to filter on may be missing or wrong. |
|
2093 | 2099 | """ |
|
2094 | 2100 | newentries = [] |
|
2095 | 2101 | for entry in entries: |
|
2096 | 2102 | spec = entry.get('BUNDLESPEC') |
|
2097 | 2103 | if spec: |
|
2098 | 2104 | try: |
|
2099 | 2105 | comp, version, params = parsebundlespec(repo, spec, strict=True) |
|
2100 | 2106 | |
|
2101 | 2107 | # If a stream clone was requested, filter out non-streamclone |
|
2102 | 2108 | # entries. |
|
2103 | 2109 | if streamclonerequested and (comp != 'UN' or version != 's1'): |
|
2104 | 2110 | repo.ui.debug('filtering %s because not a stream clone\n' % |
|
2105 | 2111 | entry['URL']) |
|
2106 | 2112 | continue |
|
2107 | 2113 | |
|
2108 | 2114 | except error.InvalidBundleSpecification as e: |
|
2109 | 2115 | repo.ui.debug(str(e) + '\n') |
|
2110 | 2116 | continue |
|
2111 | 2117 | except error.UnsupportedBundleSpecification as e: |
|
2112 | 2118 | repo.ui.debug('filtering %s because unsupported bundle ' |
|
2113 | 2119 | 'spec: %s\n' % (entry['URL'], str(e))) |
|
2114 | 2120 | continue |
|
2115 | 2121 | # If we don't have a spec and requested a stream clone, we don't know |
|
2116 | 2122 | # what the entry is so don't attempt to apply it. |
|
2117 | 2123 | elif streamclonerequested: |
|
2118 | 2124 | repo.ui.debug('filtering %s because cannot determine if a stream ' |
|
2119 | 2125 | 'clone bundle\n' % entry['URL']) |
|
2120 | 2126 | continue |
|
2121 | 2127 | |
|
2122 | 2128 | if 'REQUIRESNI' in entry and not sslutil.hassni: |
|
2123 | 2129 | repo.ui.debug('filtering %s because SNI not supported\n' % |
|
2124 | 2130 | entry['URL']) |
|
2125 | 2131 | continue |
|
2126 | 2132 | |
|
2127 | 2133 | newentries.append(entry) |
|
2128 | 2134 | |
|
2129 | 2135 | return newentries |
|
2130 | 2136 | |
|
2131 | 2137 | class clonebundleentry(object): |
|
2132 | 2138 | """Represents an item in a clone bundles manifest. |
|
2133 | 2139 | |
|
2134 | 2140 | This rich class is needed to support sorting since sorted() in Python 3 |
|
2135 | 2141 | doesn't support ``cmp`` and our comparison is complex enough that ``key=`` |
|
2136 | 2142 | won't work. |
|
2137 | 2143 | """ |
|
2138 | 2144 | |
|
2139 | 2145 | def __init__(self, value, prefers): |
|
2140 | 2146 | self.value = value |
|
2141 | 2147 | self.prefers = prefers |
|
2142 | 2148 | |
|
2143 | 2149 | def _cmp(self, other): |
|
2144 | 2150 | for prefkey, prefvalue in self.prefers: |
|
2145 | 2151 | avalue = self.value.get(prefkey) |
|
2146 | 2152 | bvalue = other.value.get(prefkey) |
|
2147 | 2153 | |
|
2148 | 2154 | # Special case for b missing attribute and a matches exactly. |
|
2149 | 2155 | if avalue is not None and bvalue is None and avalue == prefvalue: |
|
2150 | 2156 | return -1 |
|
2151 | 2157 | |
|
2152 | 2158 | # Special case for a missing attribute and b matches exactly. |
|
2153 | 2159 | if bvalue is not None and avalue is None and bvalue == prefvalue: |
|
2154 | 2160 | return 1 |
|
2155 | 2161 | |
|
2156 | 2162 | # We can't compare unless attribute present on both. |
|
2157 | 2163 | if avalue is None or bvalue is None: |
|
2158 | 2164 | continue |
|
2159 | 2165 | |
|
2160 | 2166 | # Same values should fall back to next attribute. |
|
2161 | 2167 | if avalue == bvalue: |
|
2162 | 2168 | continue |
|
2163 | 2169 | |
|
2164 | 2170 | # Exact matches come first. |
|
2165 | 2171 | if avalue == prefvalue: |
|
2166 | 2172 | return -1 |
|
2167 | 2173 | if bvalue == prefvalue: |
|
2168 | 2174 | return 1 |
|
2169 | 2175 | |
|
2170 | 2176 | # Fall back to next attribute. |
|
2171 | 2177 | continue |
|
2172 | 2178 | |
|
2173 | 2179 | # If we got here we couldn't sort by attributes and prefers. Fall |
|
2174 | 2180 | # back to index order. |
|
2175 | 2181 | return 0 |
|
2176 | 2182 | |
|
2177 | 2183 | def __lt__(self, other): |
|
2178 | 2184 | return self._cmp(other) < 0 |
|
2179 | 2185 | |
|
2180 | 2186 | def __gt__(self, other): |
|
2181 | 2187 | return self._cmp(other) > 0 |
|
2182 | 2188 | |
|
2183 | 2189 | def __eq__(self, other): |
|
2184 | 2190 | return self._cmp(other) == 0 |
|
2185 | 2191 | |
|
2186 | 2192 | def __le__(self, other): |
|
2187 | 2193 | return self._cmp(other) <= 0 |
|
2188 | 2194 | |
|
2189 | 2195 | def __ge__(self, other): |
|
2190 | 2196 | return self._cmp(other) >= 0 |
|
2191 | 2197 | |
|
2192 | 2198 | def __ne__(self, other): |
|
2193 | 2199 | return self._cmp(other) != 0 |
|
2194 | 2200 | |
|
2195 | 2201 | def sortclonebundleentries(ui, entries): |
|
2196 | 2202 | prefers = ui.configlist('ui', 'clonebundleprefers') |
|
2197 | 2203 | if not prefers: |
|
2198 | 2204 | return list(entries) |
|
2199 | 2205 | |
|
2200 | 2206 | prefers = [p.split('=', 1) for p in prefers] |
|
2201 | 2207 | |
|
2202 | 2208 | items = sorted(clonebundleentry(v, prefers) for v in entries) |
|
2203 | 2209 | return [i.value for i in items] |
|
2204 | 2210 | |
|
2205 | 2211 | def trypullbundlefromurl(ui, repo, url): |
|
2206 | 2212 | """Attempt to apply a bundle from a URL.""" |
|
2207 | 2213 | with repo.lock(), repo.transaction('bundleurl') as tr: |
|
2208 | 2214 | try: |
|
2209 | 2215 | fh = urlmod.open(ui, url) |
|
2210 | 2216 | cg = readbundle(ui, fh, 'stream') |
|
2211 | 2217 | |
|
2212 | 2218 | if isinstance(cg, streamclone.streamcloneapplier): |
|
2213 | 2219 | cg.apply(repo) |
|
2214 | 2220 | else: |
|
2215 | 2221 | bundle2.applybundle(repo, cg, tr, 'clonebundles', url) |
|
2216 | 2222 | return True |
|
2217 | 2223 | except urlerr.httperror as e: |
|
2218 | 2224 | ui.warn(_('HTTP error fetching bundle: %s\n') % str(e)) |
|
2219 | 2225 | except urlerr.urlerror as e: |
|
2220 | 2226 | ui.warn(_('error fetching bundle: %s\n') % e.reason) |
|
2221 | 2227 | |
|
2222 | 2228 | return False |
General Comments 0
You need to be logged in to leave comments.
Login now