##// END OF EJS Templates
changegroup: flush the ui stdio buffers after adding a changegroup...
Matt Harbison -
r24717:90f2b9de default
parent child Browse files
Show More
@@ -1,898 +1,899 b''
1 1 # changegroup.py - Mercurial changegroup manipulation functions
2 2 #
3 3 # Copyright 2006 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 import weakref
9 9 from i18n import _
10 10 from node import nullrev, nullid, hex, short
11 11 import mdiff, util, dagutil
12 12 import struct, os, bz2, zlib, tempfile
13 13 import discovery, error, phases, branchmap
14 14
15 15 _CHANGEGROUPV1_DELTA_HEADER = "20s20s20s20s"
16 16 _CHANGEGROUPV2_DELTA_HEADER = "20s20s20s20s20s"
17 17
18 18 def readexactly(stream, n):
19 19 '''read n bytes from stream.read and abort if less was available'''
20 20 s = stream.read(n)
21 21 if len(s) < n:
22 22 raise util.Abort(_("stream ended unexpectedly"
23 23 " (got %d bytes, expected %d)")
24 24 % (len(s), n))
25 25 return s
26 26
27 27 def getchunk(stream):
28 28 """return the next chunk from stream as a string"""
29 29 d = readexactly(stream, 4)
30 30 l = struct.unpack(">l", d)[0]
31 31 if l <= 4:
32 32 if l:
33 33 raise util.Abort(_("invalid chunk length %d") % l)
34 34 return ""
35 35 return readexactly(stream, l - 4)
36 36
37 37 def chunkheader(length):
38 38 """return a changegroup chunk header (string)"""
39 39 return struct.pack(">l", length + 4)
40 40
41 41 def closechunk():
42 42 """return a changegroup chunk header (string) for a zero-length chunk"""
43 43 return struct.pack(">l", 0)
44 44
45 45 def combineresults(results):
46 46 """logic to combine 0 or more addchangegroup results into one"""
47 47 changedheads = 0
48 48 result = 1
49 49 for ret in results:
50 50 # If any changegroup result is 0, return 0
51 51 if ret == 0:
52 52 result = 0
53 53 break
54 54 if ret < -1:
55 55 changedheads += ret + 1
56 56 elif ret > 1:
57 57 changedheads += ret - 1
58 58 if changedheads > 0:
59 59 result = 1 + changedheads
60 60 elif changedheads < 0:
61 61 result = -1 + changedheads
62 62 return result
63 63
64 64 class nocompress(object):
65 65 def compress(self, x):
66 66 return x
67 67 def flush(self):
68 68 return ""
69 69
70 70 bundletypes = {
71 71 "": ("", nocompress), # only when using unbundle on ssh and old http servers
72 72 # since the unification ssh accepts a header but there
73 73 # is no capability signaling it.
74 74 "HG20": (), # special-cased below
75 75 "HG10UN": ("HG10UN", nocompress),
76 76 "HG10BZ": ("HG10", lambda: bz2.BZ2Compressor()),
77 77 "HG10GZ": ("HG10GZ", lambda: zlib.compressobj()),
78 78 }
79 79
80 80 # hgweb uses this list to communicate its preferred type
81 81 bundlepriority = ['HG10GZ', 'HG10BZ', 'HG10UN']
82 82
83 83 def writebundle(ui, cg, filename, bundletype, vfs=None):
84 84 """Write a bundle file and return its filename.
85 85
86 86 Existing files will not be overwritten.
87 87 If no filename is specified, a temporary file is created.
88 88 bz2 compression can be turned off.
89 89 The bundle file will be deleted in case of errors.
90 90 """
91 91
92 92 fh = None
93 93 cleanup = None
94 94 try:
95 95 if filename:
96 96 if vfs:
97 97 fh = vfs.open(filename, "wb")
98 98 else:
99 99 fh = open(filename, "wb")
100 100 else:
101 101 fd, filename = tempfile.mkstemp(prefix="hg-bundle-", suffix=".hg")
102 102 fh = os.fdopen(fd, "wb")
103 103 cleanup = filename
104 104
105 105 if bundletype == "HG20":
106 106 import bundle2
107 107 bundle = bundle2.bundle20(ui)
108 108 part = bundle.newpart('changegroup', data=cg.getchunks())
109 109 part.addparam('version', cg.version)
110 110 z = nocompress()
111 111 chunkiter = bundle.getchunks()
112 112 else:
113 113 if cg.version != '01':
114 114 raise util.Abort(_('old bundle types only supports v1 '
115 115 'changegroups'))
116 116 header, compressor = bundletypes[bundletype]
117 117 fh.write(header)
118 118 z = compressor()
119 119 chunkiter = cg.getchunks()
120 120
121 121 # parse the changegroup data, otherwise we will block
122 122 # in case of sshrepo because we don't know the end of the stream
123 123
124 124 # an empty chunkgroup is the end of the changegroup
125 125 # a changegroup has at least 2 chunkgroups (changelog and manifest).
126 126 # after that, an empty chunkgroup is the end of the changegroup
127 127 for chunk in chunkiter:
128 128 fh.write(z.compress(chunk))
129 129 fh.write(z.flush())
130 130 cleanup = None
131 131 return filename
132 132 finally:
133 133 if fh is not None:
134 134 fh.close()
135 135 if cleanup is not None:
136 136 if filename and vfs:
137 137 vfs.unlink(cleanup)
138 138 else:
139 139 os.unlink(cleanup)
140 140
141 141 def decompressor(fh, alg):
142 142 if alg == 'UN':
143 143 return fh
144 144 elif alg == 'GZ':
145 145 def generator(f):
146 146 zd = zlib.decompressobj()
147 147 for chunk in util.filechunkiter(f):
148 148 yield zd.decompress(chunk)
149 149 elif alg == 'BZ':
150 150 def generator(f):
151 151 zd = bz2.BZ2Decompressor()
152 152 zd.decompress("BZ")
153 153 for chunk in util.filechunkiter(f, 4096):
154 154 yield zd.decompress(chunk)
155 155 else:
156 156 raise util.Abort("unknown bundle compression '%s'" % alg)
157 157 return util.chunkbuffer(generator(fh))
158 158
159 159 class cg1unpacker(object):
160 160 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
161 161 deltaheadersize = struct.calcsize(deltaheader)
162 162 version = '01'
163 163 def __init__(self, fh, alg):
164 164 self._stream = decompressor(fh, alg)
165 165 self._type = alg
166 166 self.callback = None
167 167 def compressed(self):
168 168 return self._type != 'UN'
169 169 def read(self, l):
170 170 return self._stream.read(l)
171 171 def seek(self, pos):
172 172 return self._stream.seek(pos)
173 173 def tell(self):
174 174 return self._stream.tell()
175 175 def close(self):
176 176 return self._stream.close()
177 177
178 178 def chunklength(self):
179 179 d = readexactly(self._stream, 4)
180 180 l = struct.unpack(">l", d)[0]
181 181 if l <= 4:
182 182 if l:
183 183 raise util.Abort(_("invalid chunk length %d") % l)
184 184 return 0
185 185 if self.callback:
186 186 self.callback()
187 187 return l - 4
188 188
189 189 def changelogheader(self):
190 190 """v10 does not have a changelog header chunk"""
191 191 return {}
192 192
193 193 def manifestheader(self):
194 194 """v10 does not have a manifest header chunk"""
195 195 return {}
196 196
197 197 def filelogheader(self):
198 198 """return the header of the filelogs chunk, v10 only has the filename"""
199 199 l = self.chunklength()
200 200 if not l:
201 201 return {}
202 202 fname = readexactly(self._stream, l)
203 203 return {'filename': fname}
204 204
205 205 def _deltaheader(self, headertuple, prevnode):
206 206 node, p1, p2, cs = headertuple
207 207 if prevnode is None:
208 208 deltabase = p1
209 209 else:
210 210 deltabase = prevnode
211 211 return node, p1, p2, deltabase, cs
212 212
213 213 def deltachunk(self, prevnode):
214 214 l = self.chunklength()
215 215 if not l:
216 216 return {}
217 217 headerdata = readexactly(self._stream, self.deltaheadersize)
218 218 header = struct.unpack(self.deltaheader, headerdata)
219 219 delta = readexactly(self._stream, l - self.deltaheadersize)
220 220 node, p1, p2, deltabase, cs = self._deltaheader(header, prevnode)
221 221 return {'node': node, 'p1': p1, 'p2': p2, 'cs': cs,
222 222 'deltabase': deltabase, 'delta': delta}
223 223
224 224 def getchunks(self):
225 225 """returns all the chunks contains in the bundle
226 226
227 227 Used when you need to forward the binary stream to a file or another
228 228 network API. To do so, it parse the changegroup data, otherwise it will
229 229 block in case of sshrepo because it don't know the end of the stream.
230 230 """
231 231 # an empty chunkgroup is the end of the changegroup
232 232 # a changegroup has at least 2 chunkgroups (changelog and manifest).
233 233 # after that, an empty chunkgroup is the end of the changegroup
234 234 empty = False
235 235 count = 0
236 236 while not empty or count <= 2:
237 237 empty = True
238 238 count += 1
239 239 while True:
240 240 chunk = getchunk(self)
241 241 if not chunk:
242 242 break
243 243 empty = False
244 244 yield chunkheader(len(chunk))
245 245 pos = 0
246 246 while pos < len(chunk):
247 247 next = pos + 2**20
248 248 yield chunk[pos:next]
249 249 pos = next
250 250 yield closechunk()
251 251
252 252 class cg2unpacker(cg1unpacker):
253 253 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
254 254 deltaheadersize = struct.calcsize(deltaheader)
255 255 version = '02'
256 256
257 257 def _deltaheader(self, headertuple, prevnode):
258 258 node, p1, p2, deltabase, cs = headertuple
259 259 return node, p1, p2, deltabase, cs
260 260
261 261 class headerlessfixup(object):
262 262 def __init__(self, fh, h):
263 263 self._h = h
264 264 self._fh = fh
265 265 def read(self, n):
266 266 if self._h:
267 267 d, self._h = self._h[:n], self._h[n:]
268 268 if len(d) < n:
269 269 d += readexactly(self._fh, n - len(d))
270 270 return d
271 271 return readexactly(self._fh, n)
272 272
273 273 class cg1packer(object):
274 274 deltaheader = _CHANGEGROUPV1_DELTA_HEADER
275 275 version = '01'
276 276 def __init__(self, repo, bundlecaps=None):
277 277 """Given a source repo, construct a bundler.
278 278
279 279 bundlecaps is optional and can be used to specify the set of
280 280 capabilities which can be used to build the bundle.
281 281 """
282 282 # Set of capabilities we can use to build the bundle.
283 283 if bundlecaps is None:
284 284 bundlecaps = set()
285 285 self._bundlecaps = bundlecaps
286 286 self._changelog = repo.changelog
287 287 self._manifest = repo.manifest
288 288 reorder = repo.ui.config('bundle', 'reorder', 'auto')
289 289 if reorder == 'auto':
290 290 reorder = None
291 291 else:
292 292 reorder = util.parsebool(reorder)
293 293 self._repo = repo
294 294 self._reorder = reorder
295 295 self._progress = repo.ui.progress
296 296 if self._repo.ui.verbose and not self._repo.ui.debugflag:
297 297 self._verbosenote = self._repo.ui.note
298 298 else:
299 299 self._verbosenote = lambda s: None
300 300
301 301 def close(self):
302 302 return closechunk()
303 303
304 304 def fileheader(self, fname):
305 305 return chunkheader(len(fname)) + fname
306 306
307 307 def group(self, nodelist, revlog, lookup, units=None, reorder=None):
308 308 """Calculate a delta group, yielding a sequence of changegroup chunks
309 309 (strings).
310 310
311 311 Given a list of changeset revs, return a set of deltas and
312 312 metadata corresponding to nodes. The first delta is
313 313 first parent(nodelist[0]) -> nodelist[0], the receiver is
314 314 guaranteed to have this parent as it has all history before
315 315 these changesets. In the case firstparent is nullrev the
316 316 changegroup starts with a full revision.
317 317
318 318 If units is not None, progress detail will be generated, units specifies
319 319 the type of revlog that is touched (changelog, manifest, etc.).
320 320 """
321 321 # if we don't have any revisions touched by these changesets, bail
322 322 if len(nodelist) == 0:
323 323 yield self.close()
324 324 return
325 325
326 326 # for generaldelta revlogs, we linearize the revs; this will both be
327 327 # much quicker and generate a much smaller bundle
328 328 if (revlog._generaldelta and reorder is not False) or reorder:
329 329 dag = dagutil.revlogdag(revlog)
330 330 revs = set(revlog.rev(n) for n in nodelist)
331 331 revs = dag.linearize(revs)
332 332 else:
333 333 revs = sorted([revlog.rev(n) for n in nodelist])
334 334
335 335 # add the parent of the first rev
336 336 p = revlog.parentrevs(revs[0])[0]
337 337 revs.insert(0, p)
338 338
339 339 # build deltas
340 340 total = len(revs) - 1
341 341 msgbundling = _('bundling')
342 342 for r in xrange(len(revs) - 1):
343 343 if units is not None:
344 344 self._progress(msgbundling, r + 1, unit=units, total=total)
345 345 prev, curr = revs[r], revs[r + 1]
346 346 linknode = lookup(revlog.node(curr))
347 347 for c in self.revchunk(revlog, curr, prev, linknode):
348 348 yield c
349 349
350 350 yield self.close()
351 351
352 352 # filter any nodes that claim to be part of the known set
353 353 def prune(self, revlog, missing, commonrevs, source):
354 354 rr, rl = revlog.rev, revlog.linkrev
355 355 return [n for n in missing if rl(rr(n)) not in commonrevs]
356 356
357 357 def generate(self, commonrevs, clnodes, fastpathlinkrev, source):
358 358 '''yield a sequence of changegroup chunks (strings)'''
359 359 repo = self._repo
360 360 cl = self._changelog
361 361 mf = self._manifest
362 362 reorder = self._reorder
363 363 progress = self._progress
364 364
365 365 # for progress output
366 366 msgbundling = _('bundling')
367 367
368 368 clrevorder = {}
369 369 mfs = {} # needed manifests
370 370 fnodes = {} # needed file nodes
371 371 changedfiles = set()
372 372
373 373 # Callback for the changelog, used to collect changed files and manifest
374 374 # nodes.
375 375 # Returns the linkrev node (identity in the changelog case).
376 376 def lookupcl(x):
377 377 c = cl.read(x)
378 378 clrevorder[x] = len(clrevorder)
379 379 changedfiles.update(c[3])
380 380 # record the first changeset introducing this manifest version
381 381 mfs.setdefault(c[0], x)
382 382 return x
383 383
384 384 self._verbosenote(_('uncompressed size of bundle content:\n'))
385 385 size = 0
386 386 for chunk in self.group(clnodes, cl, lookupcl, units=_('changesets'),
387 387 reorder=reorder):
388 388 size += len(chunk)
389 389 yield chunk
390 390 self._verbosenote(_('%8.i (changelog)\n') % size)
391 391 progress(msgbundling, None)
392 392
393 393 # Callback for the manifest, used to collect linkrevs for filelog
394 394 # revisions.
395 395 # Returns the linkrev node (collected in lookupcl).
396 396 def lookupmf(x):
397 397 clnode = mfs[x]
398 398 if not fastpathlinkrev or reorder:
399 399 mdata = mf.readfast(x)
400 400 for f, n in mdata.iteritems():
401 401 if f in changedfiles:
402 402 # record the first changeset introducing this filelog
403 403 # version
404 404 fclnodes = fnodes.setdefault(f, {})
405 405 fclnode = fclnodes.setdefault(n, clnode)
406 406 if clrevorder[clnode] < clrevorder[fclnode]:
407 407 fclnodes[n] = clnode
408 408 return clnode
409 409
410 410 mfnodes = self.prune(mf, mfs, commonrevs, source)
411 411 size = 0
412 412 for chunk in self.group(mfnodes, mf, lookupmf, units=_('manifests'),
413 413 reorder=reorder):
414 414 size += len(chunk)
415 415 yield chunk
416 416 self._verbosenote(_('%8.i (manifests)\n') % size)
417 417 progress(msgbundling, None)
418 418
419 419 mfs.clear()
420 420 needed = set(cl.rev(x) for x in clnodes)
421 421
422 422 def linknodes(filerevlog, fname):
423 423 if fastpathlinkrev and not reorder:
424 424 llr = filerevlog.linkrev
425 425 def genfilenodes():
426 426 for r in filerevlog:
427 427 linkrev = llr(r)
428 428 if linkrev in needed:
429 429 yield filerevlog.node(r), cl.node(linkrev)
430 430 return dict(genfilenodes())
431 431 return fnodes.get(fname, {})
432 432
433 433 for chunk in self.generatefiles(changedfiles, linknodes, commonrevs,
434 434 source):
435 435 yield chunk
436 436
437 437 yield self.close()
438 438 progress(msgbundling, None)
439 439
440 440 if clnodes:
441 441 repo.hook('outgoing', node=hex(clnodes[0]), source=source)
442 442
443 443 def generatefiles(self, changedfiles, linknodes, commonrevs, source):
444 444 repo = self._repo
445 445 progress = self._progress
446 446 reorder = self._reorder
447 447 msgbundling = _('bundling')
448 448
449 449 total = len(changedfiles)
450 450 # for progress output
451 451 msgfiles = _('files')
452 452 for i, fname in enumerate(sorted(changedfiles)):
453 453 filerevlog = repo.file(fname)
454 454 if not filerevlog:
455 455 raise util.Abort(_("empty or missing revlog for %s") % fname)
456 456
457 457 linkrevnodes = linknodes(filerevlog, fname)
458 458 # Lookup for filenodes, we collected the linkrev nodes above in the
459 459 # fastpath case and with lookupmf in the slowpath case.
460 460 def lookupfilelog(x):
461 461 return linkrevnodes[x]
462 462
463 463 filenodes = self.prune(filerevlog, linkrevnodes, commonrevs, source)
464 464 if filenodes:
465 465 progress(msgbundling, i + 1, item=fname, unit=msgfiles,
466 466 total=total)
467 467 h = self.fileheader(fname)
468 468 size = len(h)
469 469 yield h
470 470 for chunk in self.group(filenodes, filerevlog, lookupfilelog,
471 471 reorder=reorder):
472 472 size += len(chunk)
473 473 yield chunk
474 474 self._verbosenote(_('%8.i %s\n') % (size, fname))
475 475
476 476 def deltaparent(self, revlog, rev, p1, p2, prev):
477 477 return prev
478 478
479 479 def revchunk(self, revlog, rev, prev, linknode):
480 480 node = revlog.node(rev)
481 481 p1, p2 = revlog.parentrevs(rev)
482 482 base = self.deltaparent(revlog, rev, p1, p2, prev)
483 483
484 484 prefix = ''
485 485 if revlog.iscensored(base) or revlog.iscensored(rev):
486 486 try:
487 487 delta = revlog.revision(node)
488 488 except error.CensoredNodeError, e:
489 489 delta = e.tombstone
490 490 if base == nullrev:
491 491 prefix = mdiff.trivialdiffheader(len(delta))
492 492 else:
493 493 baselen = revlog.rawsize(base)
494 494 prefix = mdiff.replacediffheader(baselen, len(delta))
495 495 elif base == nullrev:
496 496 delta = revlog.revision(node)
497 497 prefix = mdiff.trivialdiffheader(len(delta))
498 498 else:
499 499 delta = revlog.revdiff(base, rev)
500 500 p1n, p2n = revlog.parents(node)
501 501 basenode = revlog.node(base)
502 502 meta = self.builddeltaheader(node, p1n, p2n, basenode, linknode)
503 503 meta += prefix
504 504 l = len(meta) + len(delta)
505 505 yield chunkheader(l)
506 506 yield meta
507 507 yield delta
508 508 def builddeltaheader(self, node, p1n, p2n, basenode, linknode):
509 509 # do nothing with basenode, it is implicitly the previous one in HG10
510 510 return struct.pack(self.deltaheader, node, p1n, p2n, linknode)
511 511
512 512 class cg2packer(cg1packer):
513 513 version = '02'
514 514 deltaheader = _CHANGEGROUPV2_DELTA_HEADER
515 515
516 516 def group(self, nodelist, revlog, lookup, units=None, reorder=None):
517 517 if (revlog._generaldelta and reorder is not True):
518 518 reorder = False
519 519 return super(cg2packer, self).group(nodelist, revlog, lookup,
520 520 units=units, reorder=reorder)
521 521
522 522 def deltaparent(self, revlog, rev, p1, p2, prev):
523 523 dp = revlog.deltaparent(rev)
524 524 # avoid storing full revisions; pick prev in those cases
525 525 # also pick prev when we can't be sure remote has dp
526 526 if dp == nullrev or (dp != p1 and dp != p2 and dp != prev):
527 527 return prev
528 528 return dp
529 529
530 530 def builddeltaheader(self, node, p1n, p2n, basenode, linknode):
531 531 return struct.pack(self.deltaheader, node, p1n, p2n, basenode, linknode)
532 532
533 533 packermap = {'01': (cg1packer, cg1unpacker),
534 534 '02': (cg2packer, cg2unpacker)}
535 535
536 536 def _changegroupinfo(repo, nodes, source):
537 537 if repo.ui.verbose or source == 'bundle':
538 538 repo.ui.status(_("%d changesets found\n") % len(nodes))
539 539 if repo.ui.debugflag:
540 540 repo.ui.debug("list of changesets:\n")
541 541 for node in nodes:
542 542 repo.ui.debug("%s\n" % hex(node))
543 543
544 544 def getsubsetraw(repo, outgoing, bundler, source, fastpath=False):
545 545 repo = repo.unfiltered()
546 546 commonrevs = outgoing.common
547 547 csets = outgoing.missing
548 548 heads = outgoing.missingheads
549 549 # We go through the fast path if we get told to, or if all (unfiltered
550 550 # heads have been requested (since we then know there all linkrevs will
551 551 # be pulled by the client).
552 552 heads.sort()
553 553 fastpathlinkrev = fastpath or (
554 554 repo.filtername is None and heads == sorted(repo.heads()))
555 555
556 556 repo.hook('preoutgoing', throw=True, source=source)
557 557 _changegroupinfo(repo, csets, source)
558 558 return bundler.generate(commonrevs, csets, fastpathlinkrev, source)
559 559
560 560 def getsubset(repo, outgoing, bundler, source, fastpath=False, version='01'):
561 561 gengroup = getsubsetraw(repo, outgoing, bundler, source, fastpath)
562 562 return packermap[version][1](util.chunkbuffer(gengroup), 'UN')
563 563
564 564 def changegroupsubset(repo, roots, heads, source, version='01'):
565 565 """Compute a changegroup consisting of all the nodes that are
566 566 descendants of any of the roots and ancestors of any of the heads.
567 567 Return a chunkbuffer object whose read() method will return
568 568 successive changegroup chunks.
569 569
570 570 It is fairly complex as determining which filenodes and which
571 571 manifest nodes need to be included for the changeset to be complete
572 572 is non-trivial.
573 573
574 574 Another wrinkle is doing the reverse, figuring out which changeset in
575 575 the changegroup a particular filenode or manifestnode belongs to.
576 576 """
577 577 cl = repo.changelog
578 578 if not roots:
579 579 roots = [nullid]
580 580 # TODO: remove call to nodesbetween.
581 581 csets, roots, heads = cl.nodesbetween(roots, heads)
582 582 discbases = []
583 583 for n in roots:
584 584 discbases.extend([p for p in cl.parents(n) if p != nullid])
585 585 outgoing = discovery.outgoing(cl, discbases, heads)
586 586 bundler = packermap[version][0](repo)
587 587 return getsubset(repo, outgoing, bundler, source, version=version)
588 588
589 589 def getlocalchangegroupraw(repo, source, outgoing, bundlecaps=None,
590 590 version='01'):
591 591 """Like getbundle, but taking a discovery.outgoing as an argument.
592 592
593 593 This is only implemented for local repos and reuses potentially
594 594 precomputed sets in outgoing. Returns a raw changegroup generator."""
595 595 if not outgoing.missing:
596 596 return None
597 597 bundler = packermap[version][0](repo, bundlecaps)
598 598 return getsubsetraw(repo, outgoing, bundler, source)
599 599
600 600 def getlocalchangegroup(repo, source, outgoing, bundlecaps=None):
601 601 """Like getbundle, but taking a discovery.outgoing as an argument.
602 602
603 603 This is only implemented for local repos and reuses potentially
604 604 precomputed sets in outgoing."""
605 605 if not outgoing.missing:
606 606 return None
607 607 bundler = cg1packer(repo, bundlecaps)
608 608 return getsubset(repo, outgoing, bundler, source)
609 609
610 610 def _computeoutgoing(repo, heads, common):
611 611 """Computes which revs are outgoing given a set of common
612 612 and a set of heads.
613 613
614 614 This is a separate function so extensions can have access to
615 615 the logic.
616 616
617 617 Returns a discovery.outgoing object.
618 618 """
619 619 cl = repo.changelog
620 620 if common:
621 621 hasnode = cl.hasnode
622 622 common = [n for n in common if hasnode(n)]
623 623 else:
624 624 common = [nullid]
625 625 if not heads:
626 626 heads = cl.heads()
627 627 return discovery.outgoing(cl, common, heads)
628 628
629 629 def getchangegroupraw(repo, source, heads=None, common=None, bundlecaps=None,
630 630 version='01'):
631 631 """Like changegroupsubset, but returns the set difference between the
632 632 ancestors of heads and the ancestors common.
633 633
634 634 If heads is None, use the local heads. If common is None, use [nullid].
635 635
636 636 If version is None, use a version '1' changegroup.
637 637
638 638 The nodes in common might not all be known locally due to the way the
639 639 current discovery protocol works. Returns a raw changegroup generator.
640 640 """
641 641 outgoing = _computeoutgoing(repo, heads, common)
642 642 return getlocalchangegroupraw(repo, source, outgoing, bundlecaps=bundlecaps,
643 643 version=version)
644 644
645 645 def getchangegroup(repo, source, heads=None, common=None, bundlecaps=None):
646 646 """Like changegroupsubset, but returns the set difference between the
647 647 ancestors of heads and the ancestors common.
648 648
649 649 If heads is None, use the local heads. If common is None, use [nullid].
650 650
651 651 The nodes in common might not all be known locally due to the way the
652 652 current discovery protocol works.
653 653 """
654 654 outgoing = _computeoutgoing(repo, heads, common)
655 655 return getlocalchangegroup(repo, source, outgoing, bundlecaps=bundlecaps)
656 656
657 657 def changegroup(repo, basenodes, source):
658 658 # to avoid a race we use changegroupsubset() (issue1320)
659 659 return changegroupsubset(repo, basenodes, repo.heads(), source)
660 660
661 661 def addchangegroupfiles(repo, source, revmap, trp, pr, needfiles):
662 662 revisions = 0
663 663 files = 0
664 664 while True:
665 665 chunkdata = source.filelogheader()
666 666 if not chunkdata:
667 667 break
668 668 f = chunkdata["filename"]
669 669 repo.ui.debug("adding %s revisions\n" % f)
670 670 pr()
671 671 fl = repo.file(f)
672 672 o = len(fl)
673 673 try:
674 674 if not fl.addgroup(source, revmap, trp):
675 675 raise util.Abort(_("received file revlog group is empty"))
676 676 except error.CensoredBaseError, e:
677 677 raise util.Abort(_("received delta base is censored: %s") % e)
678 678 revisions += len(fl) - o
679 679 files += 1
680 680 if f in needfiles:
681 681 needs = needfiles[f]
682 682 for new in xrange(o, len(fl)):
683 683 n = fl.node(new)
684 684 if n in needs:
685 685 needs.remove(n)
686 686 else:
687 687 raise util.Abort(
688 688 _("received spurious file revlog entry"))
689 689 if not needs:
690 690 del needfiles[f]
691 691 repo.ui.progress(_('files'), None)
692 692
693 693 for f, needs in needfiles.iteritems():
694 694 fl = repo.file(f)
695 695 for n in needs:
696 696 try:
697 697 fl.rev(n)
698 698 except error.LookupError:
699 699 raise util.Abort(
700 700 _('missing file data for %s:%s - run hg verify') %
701 701 (f, hex(n)))
702 702
703 703 return revisions, files
704 704
705 705 def addchangegroup(repo, source, srctype, url, emptyok=False,
706 706 targetphase=phases.draft):
707 707 """Add the changegroup returned by source.read() to this repo.
708 708 srctype is a string like 'push', 'pull', or 'unbundle'. url is
709 709 the URL of the repo where this changegroup is coming from.
710 710
711 711 Return an integer summarizing the change to this repo:
712 712 - nothing changed or no source: 0
713 713 - more heads than before: 1+added heads (2..n)
714 714 - fewer heads than before: -1-removed heads (-2..-n)
715 715 - number of heads stays the same: 1
716 716 """
717 717 repo = repo.unfiltered()
718 718 def csmap(x):
719 719 repo.ui.debug("add changeset %s\n" % short(x))
720 720 return len(cl)
721 721
722 722 def revmap(x):
723 723 return cl.rev(x)
724 724
725 725 if not source:
726 726 return 0
727 727
728 728 changesets = files = revisions = 0
729 729 efiles = set()
730 730
731 731 tr = repo.transaction("\n".join([srctype, util.hidepassword(url)]))
732 732 # The transaction could have been created before and already carries source
733 733 # information. In this case we use the top level data. We overwrite the
734 734 # argument because we need to use the top level value (if they exist) in
735 735 # this function.
736 736 srctype = tr.hookargs.setdefault('source', srctype)
737 737 url = tr.hookargs.setdefault('url', url)
738 738
739 739 # write changelog data to temp files so concurrent readers will not see
740 740 # inconsistent view
741 741 cl = repo.changelog
742 742 cl.delayupdate(tr)
743 743 oldheads = cl.heads()
744 744 try:
745 745 repo.hook('prechangegroup', throw=True, **tr.hookargs)
746 746
747 747 trp = weakref.proxy(tr)
748 748 # pull off the changeset group
749 749 repo.ui.status(_("adding changesets\n"))
750 750 clstart = len(cl)
751 751 class prog(object):
752 752 step = _('changesets')
753 753 count = 1
754 754 ui = repo.ui
755 755 total = None
756 756 def __call__(repo):
757 757 repo.ui.progress(repo.step, repo.count, unit=_('chunks'),
758 758 total=repo.total)
759 759 repo.count += 1
760 760 pr = prog()
761 761 source.callback = pr
762 762
763 763 source.changelogheader()
764 764 srccontent = cl.addgroup(source, csmap, trp)
765 765 if not (srccontent or emptyok):
766 766 raise util.Abort(_("received changelog group is empty"))
767 767 clend = len(cl)
768 768 changesets = clend - clstart
769 769 for c in xrange(clstart, clend):
770 770 efiles.update(repo[c].files())
771 771 efiles = len(efiles)
772 772 repo.ui.progress(_('changesets'), None)
773 773
774 774 # pull off the manifest group
775 775 repo.ui.status(_("adding manifests\n"))
776 776 pr.step = _('manifests')
777 777 pr.count = 1
778 778 pr.total = changesets # manifests <= changesets
779 779 # no need to check for empty manifest group here:
780 780 # if the result of the merge of 1 and 2 is the same in 3 and 4,
781 781 # no new manifest will be created and the manifest group will
782 782 # be empty during the pull
783 783 source.manifestheader()
784 784 repo.manifest.addgroup(source, revmap, trp)
785 785 repo.ui.progress(_('manifests'), None)
786 786
787 787 needfiles = {}
788 788 if repo.ui.configbool('server', 'validate', default=False):
789 789 # validate incoming csets have their manifests
790 790 for cset in xrange(clstart, clend):
791 791 mfest = repo.changelog.read(repo.changelog.node(cset))[0]
792 792 mfest = repo.manifest.readdelta(mfest)
793 793 # store file nodes we must see
794 794 for f, n in mfest.iteritems():
795 795 needfiles.setdefault(f, set()).add(n)
796 796
797 797 # process the files
798 798 repo.ui.status(_("adding file changes\n"))
799 799 pr.step = _('files')
800 800 pr.count = 1
801 801 pr.total = efiles
802 802 source.callback = None
803 803
804 804 newrevs, newfiles = addchangegroupfiles(repo, source, revmap, trp, pr,
805 805 needfiles)
806 806 revisions += newrevs
807 807 files += newfiles
808 808
809 809 dh = 0
810 810 if oldheads:
811 811 heads = cl.heads()
812 812 dh = len(heads) - len(oldheads)
813 813 for h in heads:
814 814 if h not in oldheads and repo[h].closesbranch():
815 815 dh -= 1
816 816 htext = ""
817 817 if dh:
818 818 htext = _(" (%+d heads)") % dh
819 819
820 820 repo.ui.status(_("added %d changesets"
821 821 " with %d changes to %d files%s\n")
822 822 % (changesets, revisions, files, htext))
823 823 repo.invalidatevolatilesets()
824 824
825 825 if changesets > 0:
826 826 p = lambda: tr.writepending() and repo.root or ""
827 827 if 'node' not in tr.hookargs:
828 828 tr.hookargs['node'] = hex(cl.node(clstart))
829 829 hookargs = dict(tr.hookargs)
830 830 else:
831 831 hookargs = dict(tr.hookargs)
832 832 hookargs['node'] = hex(cl.node(clstart))
833 833 repo.hook('pretxnchangegroup', throw=True, pending=p, **hookargs)
834 834
835 835 added = [cl.node(r) for r in xrange(clstart, clend)]
836 836 publishing = repo.ui.configbool('phases', 'publish', True)
837 837 if srctype in ('push', 'serve'):
838 838 # Old servers can not push the boundary themselves.
839 839 # New servers won't push the boundary if changeset already
840 840 # exists locally as secret
841 841 #
842 842 # We should not use added here but the list of all change in
843 843 # the bundle
844 844 if publishing:
845 845 phases.advanceboundary(repo, tr, phases.public, srccontent)
846 846 else:
847 847 # Those changesets have been pushed from the outside, their
848 848 # phases are going to be pushed alongside. Therefor
849 849 # `targetphase` is ignored.
850 850 phases.advanceboundary(repo, tr, phases.draft, srccontent)
851 851 phases.retractboundary(repo, tr, phases.draft, added)
852 852 elif srctype != 'strip':
853 853 # publishing only alter behavior during push
854 854 #
855 855 # strip should not touch boundary at all
856 856 phases.retractboundary(repo, tr, targetphase, added)
857 857
858 858 if changesets > 0:
859 859 if srctype != 'strip':
860 860 # During strip, branchcache is invalid but coming call to
861 861 # `destroyed` will repair it.
862 862 # In other case we can safely update cache on disk.
863 863 branchmap.updatecache(repo.filtered('served'))
864 864
865 865 def runhooks():
866 866 # These hooks run when the lock releases, not when the
867 867 # transaction closes. So it's possible for the changelog
868 868 # to have changed since we last saw it.
869 869 if clstart >= len(repo):
870 870 return
871 871
872 872 # forcefully update the on-disk branch cache
873 873 repo.ui.debug("updating the branch cache\n")
874 874 repo.hook("changegroup", **hookargs)
875 875
876 876 for n in added:
877 877 args = hookargs.copy()
878 878 args['node'] = hex(n)
879 879 repo.hook("incoming", **args)
880 880
881 881 newheads = [h for h in repo.heads() if h not in oldheads]
882 882 repo.ui.log("incoming",
883 883 "%s incoming changes - new heads: %s\n",
884 884 len(added),
885 885 ', '.join([hex(c[:6]) for c in newheads]))
886 886
887 887 tr.addpostclose('changegroup-runhooks-%020i' % clstart,
888 888 lambda tr: repo._afterlock(runhooks))
889 889
890 890 tr.close()
891 891
892 892 finally:
893 893 tr.release()
894 repo.ui.flush()
894 895 # never return 0 here:
895 896 if dh < 0:
896 897 return dh - 1
897 898 else:
898 899 return dh + 1
General Comments 0
You need to be logged in to leave comments. Login now