##// END OF EJS Templates
revlog: stop exporting node.short
Matt Mackall -
r14393:bdf44e63 default
parent child Browse files
Show More
@@ -1,631 +1,632 b''
1 1 # Patch transplanting extension for Mercurial
2 2 #
3 3 # Copyright 2006, 2007 Brendan Cully <brendan@kublai.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 '''command to transplant changesets from another branch
9 9
10 10 This extension allows you to transplant patches from another branch.
11 11
12 12 Transplanted patches are recorded in .hg/transplant/transplants, as a
13 13 map from a changeset hash to its hash in the source repository.
14 14 '''
15 15
16 16 from mercurial.i18n import _
17 17 import os, tempfile
18 from mercurial.node import short
18 19 from mercurial import bundlerepo, hg, merge, match
19 20 from mercurial import patch, revlog, scmutil, util, error, cmdutil
20 21 from mercurial import revset, templatekw
21 22
22 23 cmdtable = {}
23 24 command = cmdutil.command(cmdtable)
24 25
25 26 class transplantentry(object):
26 27 def __init__(self, lnode, rnode):
27 28 self.lnode = lnode
28 29 self.rnode = rnode
29 30
30 31 class transplants(object):
31 32 def __init__(self, path=None, transplantfile=None, opener=None):
32 33 self.path = path
33 34 self.transplantfile = transplantfile
34 35 self.opener = opener
35 36
36 37 if not opener:
37 38 self.opener = scmutil.opener(self.path)
38 39 self.transplants = {}
39 40 self.dirty = False
40 41 self.read()
41 42
42 43 def read(self):
43 44 abspath = os.path.join(self.path, self.transplantfile)
44 45 if self.transplantfile and os.path.exists(abspath):
45 46 for line in self.opener.read(self.transplantfile).splitlines():
46 47 lnode, rnode = map(revlog.bin, line.split(':'))
47 48 list = self.transplants.setdefault(rnode, [])
48 49 list.append(transplantentry(lnode, rnode))
49 50
50 51 def write(self):
51 52 if self.dirty and self.transplantfile:
52 53 if not os.path.isdir(self.path):
53 54 os.mkdir(self.path)
54 55 fp = self.opener(self.transplantfile, 'w')
55 56 for list in self.transplants.itervalues():
56 57 for t in list:
57 58 l, r = map(revlog.hex, (t.lnode, t.rnode))
58 59 fp.write(l + ':' + r + '\n')
59 60 fp.close()
60 61 self.dirty = False
61 62
62 63 def get(self, rnode):
63 64 return self.transplants.get(rnode) or []
64 65
65 66 def set(self, lnode, rnode):
66 67 list = self.transplants.setdefault(rnode, [])
67 68 list.append(transplantentry(lnode, rnode))
68 69 self.dirty = True
69 70
70 71 def remove(self, transplant):
71 72 list = self.transplants.get(transplant.rnode)
72 73 if list:
73 74 del list[list.index(transplant)]
74 75 self.dirty = True
75 76
76 77 class transplanter(object):
77 78 def __init__(self, ui, repo):
78 79 self.ui = ui
79 80 self.path = repo.join('transplant')
80 81 self.opener = scmutil.opener(self.path)
81 82 self.transplants = transplants(self.path, 'transplants',
82 83 opener=self.opener)
83 84
84 85 def applied(self, repo, node, parent):
85 86 '''returns True if a node is already an ancestor of parent
86 87 or has already been transplanted'''
87 88 if hasnode(repo, node):
88 89 if node in repo.changelog.reachable(parent, stop=node):
89 90 return True
90 91 for t in self.transplants.get(node):
91 92 # it might have been stripped
92 93 if not hasnode(repo, t.lnode):
93 94 self.transplants.remove(t)
94 95 return False
95 96 if t.lnode in repo.changelog.reachable(parent, stop=t.lnode):
96 97 return True
97 98 return False
98 99
99 100 def apply(self, repo, source, revmap, merges, opts={}):
100 101 '''apply the revisions in revmap one by one in revision order'''
101 102 revs = sorted(revmap)
102 103 p1, p2 = repo.dirstate.parents()
103 104 pulls = []
104 105 diffopts = patch.diffopts(self.ui, opts)
105 106 diffopts.git = True
106 107
107 108 lock = wlock = None
108 109 try:
109 110 wlock = repo.wlock()
110 111 lock = repo.lock()
111 112 for rev in revs:
112 113 node = revmap[rev]
113 revstr = '%s:%s' % (rev, revlog.short(node))
114 revstr = '%s:%s' % (rev, short(node))
114 115
115 116 if self.applied(repo, node, p1):
116 117 self.ui.warn(_('skipping already applied revision %s\n') %
117 118 revstr)
118 119 continue
119 120
120 121 parents = source.changelog.parents(node)
121 122 if not opts.get('filter'):
122 123 # If the changeset parent is the same as the
123 124 # wdir's parent, just pull it.
124 125 if parents[0] == p1:
125 126 pulls.append(node)
126 127 p1 = node
127 128 continue
128 129 if pulls:
129 130 if source != repo:
130 131 repo.pull(source, heads=pulls)
131 132 merge.update(repo, pulls[-1], False, False, None)
132 133 p1, p2 = repo.dirstate.parents()
133 134 pulls = []
134 135
135 136 domerge = False
136 137 if node in merges:
137 138 # pulling all the merge revs at once would mean we
138 139 # couldn't transplant after the latest even if
139 140 # transplants before them fail.
140 141 domerge = True
141 142 if not hasnode(repo, node):
142 143 repo.pull(source, heads=[node])
143 144
144 145 if parents[1] != revlog.nullid:
145 146 self.ui.note(_('skipping merge changeset %s:%s\n')
146 % (rev, revlog.short(node)))
147 % (rev, short(node)))
147 148 patchfile = None
148 149 else:
149 150 fd, patchfile = tempfile.mkstemp(prefix='hg-transplant-')
150 151 fp = os.fdopen(fd, 'w')
151 152 gen = patch.diff(source, parents[0], node, opts=diffopts)
152 153 for chunk in gen:
153 154 fp.write(chunk)
154 155 fp.close()
155 156
156 157 del revmap[rev]
157 158 if patchfile or domerge:
158 159 try:
159 160 n = self.applyone(repo, node,
160 161 source.changelog.read(node),
161 162 patchfile, merge=domerge,
162 163 log=opts.get('log'),
163 164 filter=opts.get('filter'))
164 165 if n and domerge:
165 166 self.ui.status(_('%s merged at %s\n') % (revstr,
166 revlog.short(n)))
167 short(n)))
167 168 elif n:
168 169 self.ui.status(_('%s transplanted to %s\n')
169 % (revlog.short(node),
170 revlog.short(n)))
170 % (short(node),
171 short(n)))
171 172 finally:
172 173 if patchfile:
173 174 os.unlink(patchfile)
174 175 if pulls:
175 176 repo.pull(source, heads=pulls)
176 177 merge.update(repo, pulls[-1], False, False, None)
177 178 finally:
178 179 self.saveseries(revmap, merges)
179 180 self.transplants.write()
180 181 lock.release()
181 182 wlock.release()
182 183
183 184 def filter(self, filter, node, changelog, patchfile):
184 185 '''arbitrarily rewrite changeset before applying it'''
185 186
186 187 self.ui.status(_('filtering %s\n') % patchfile)
187 188 user, date, msg = (changelog[1], changelog[2], changelog[4])
188 189 fd, headerfile = tempfile.mkstemp(prefix='hg-transplant-')
189 190 fp = os.fdopen(fd, 'w')
190 191 fp.write("# HG changeset patch\n")
191 192 fp.write("# User %s\n" % user)
192 193 fp.write("# Date %d %d\n" % date)
193 194 fp.write(msg + '\n')
194 195 fp.close()
195 196
196 197 try:
197 198 util.system('%s %s %s' % (filter, util.shellquote(headerfile),
198 199 util.shellquote(patchfile)),
199 200 environ={'HGUSER': changelog[1],
200 201 'HGREVISION': revlog.hex(node),
201 202 },
202 203 onerr=util.Abort, errprefix=_('filter failed'))
203 204 user, date, msg = self.parselog(file(headerfile))[1:4]
204 205 finally:
205 206 os.unlink(headerfile)
206 207
207 208 return (user, date, msg)
208 209
209 210 def applyone(self, repo, node, cl, patchfile, merge=False, log=False,
210 211 filter=None):
211 212 '''apply the patch in patchfile to the repository as a transplant'''
212 213 (manifest, user, (time, timezone), files, message) = cl[:5]
213 214 date = "%d %d" % (time, timezone)
214 215 extra = {'transplant_source': node}
215 216 if filter:
216 217 (user, date, message) = self.filter(filter, node, cl, patchfile)
217 218
218 219 if log:
219 220 # we don't translate messages inserted into commits
220 221 message += '\n(transplanted from %s)' % revlog.hex(node)
221 222
222 self.ui.status(_('applying %s\n') % revlog.short(node))
223 self.ui.status(_('applying %s\n') % short(node))
223 224 self.ui.note('%s %s\n%s\n' % (user, date, message))
224 225
225 226 if not patchfile and not merge:
226 227 raise util.Abort(_('can only omit patchfile if merging'))
227 228 if patchfile:
228 229 try:
229 230 files = {}
230 231 patch.patch(self.ui, repo, patchfile, files=files, eolmode=None)
231 232 files = list(files)
232 233 if not files:
233 234 self.ui.warn(_('%s: empty changeset') % revlog.hex(node))
234 235 return None
235 236 except Exception, inst:
236 237 seriespath = os.path.join(self.path, 'series')
237 238 if os.path.exists(seriespath):
238 239 os.unlink(seriespath)
239 240 p1 = repo.dirstate.p1()
240 241 p2 = node
241 242 self.log(user, date, message, p1, p2, merge=merge)
242 243 self.ui.write(str(inst) + '\n')
243 244 raise util.Abort(_('fix up the merge and run '
244 245 'hg transplant --continue'))
245 246 else:
246 247 files = None
247 248 if merge:
248 249 p1, p2 = repo.dirstate.parents()
249 250 repo.dirstate.setparents(p1, node)
250 251 m = match.always(repo.root, '')
251 252 else:
252 253 m = match.exact(repo.root, '', files)
253 254
254 255 n = repo.commit(message, user, date, extra=extra, match=m)
255 256 if not n:
256 257 # Crash here to prevent an unclear crash later, in
257 258 # transplants.write(). This can happen if patch.patch()
258 259 # does nothing but claims success or if repo.status() fails
259 260 # to report changes done by patch.patch(). These both
260 261 # appear to be bugs in other parts of Mercurial, but dying
261 262 # here, as soon as we can detect the problem, is preferable
262 263 # to silently dropping changesets on the floor.
263 264 raise RuntimeError('nothing committed after transplant')
264 265 if not merge:
265 266 self.transplants.set(n, node)
266 267
267 268 return n
268 269
269 270 def resume(self, repo, source, opts=None):
270 271 '''recover last transaction and apply remaining changesets'''
271 272 if os.path.exists(os.path.join(self.path, 'journal')):
272 273 n, node = self.recover(repo)
273 self.ui.status(_('%s transplanted as %s\n') % (revlog.short(node),
274 revlog.short(n)))
274 self.ui.status(_('%s transplanted as %s\n') % (short(node),
275 short(n)))
275 276 seriespath = os.path.join(self.path, 'series')
276 277 if not os.path.exists(seriespath):
277 278 self.transplants.write()
278 279 return
279 280 nodes, merges = self.readseries()
280 281 revmap = {}
281 282 for n in nodes:
282 283 revmap[source.changelog.rev(n)] = n
283 284 os.unlink(seriespath)
284 285
285 286 self.apply(repo, source, revmap, merges, opts)
286 287
287 288 def recover(self, repo):
288 289 '''commit working directory using journal metadata'''
289 290 node, user, date, message, parents = self.readlog()
290 291 merge = len(parents) == 2
291 292
292 293 if not user or not date or not message or not parents[0]:
293 294 raise util.Abort(_('transplant log file is corrupt'))
294 295
295 296 extra = {'transplant_source': node}
296 297 wlock = repo.wlock()
297 298 try:
298 299 p1, p2 = repo.dirstate.parents()
299 300 if p1 != parents[0]:
300 301 raise util.Abort(
301 302 _('working dir not at transplant parent %s') %
302 303 revlog.hex(parents[0]))
303 304 if merge:
304 305 repo.dirstate.setparents(p1, parents[1])
305 306 n = repo.commit(message, user, date, extra=extra)
306 307 if not n:
307 308 raise util.Abort(_('commit failed'))
308 309 if not merge:
309 310 self.transplants.set(n, node)
310 311 self.unlog()
311 312
312 313 return n, node
313 314 finally:
314 315 wlock.release()
315 316
316 317 def readseries(self):
317 318 nodes = []
318 319 merges = []
319 320 cur = nodes
320 321 for line in self.opener.read('series').splitlines():
321 322 if line.startswith('# Merges'):
322 323 cur = merges
323 324 continue
324 325 cur.append(revlog.bin(line))
325 326
326 327 return (nodes, merges)
327 328
328 329 def saveseries(self, revmap, merges):
329 330 if not revmap:
330 331 return
331 332
332 333 if not os.path.isdir(self.path):
333 334 os.mkdir(self.path)
334 335 series = self.opener('series', 'w')
335 336 for rev in sorted(revmap):
336 337 series.write(revlog.hex(revmap[rev]) + '\n')
337 338 if merges:
338 339 series.write('# Merges\n')
339 340 for m in merges:
340 341 series.write(revlog.hex(m) + '\n')
341 342 series.close()
342 343
343 344 def parselog(self, fp):
344 345 parents = []
345 346 message = []
346 347 node = revlog.nullid
347 348 inmsg = False
348 349 user = None
349 350 date = None
350 351 for line in fp.read().splitlines():
351 352 if inmsg:
352 353 message.append(line)
353 354 elif line.startswith('# User '):
354 355 user = line[7:]
355 356 elif line.startswith('# Date '):
356 357 date = line[7:]
357 358 elif line.startswith('# Node ID '):
358 359 node = revlog.bin(line[10:])
359 360 elif line.startswith('# Parent '):
360 361 parents.append(revlog.bin(line[9:]))
361 362 elif not line.startswith('# '):
362 363 inmsg = True
363 364 message.append(line)
364 365 if None in (user, date):
365 366 raise util.Abort(_("filter corrupted changeset (no user or date)"))
366 367 return (node, user, date, '\n'.join(message), parents)
367 368
368 369 def log(self, user, date, message, p1, p2, merge=False):
369 370 '''journal changelog metadata for later recover'''
370 371
371 372 if not os.path.isdir(self.path):
372 373 os.mkdir(self.path)
373 374 fp = self.opener('journal', 'w')
374 375 fp.write('# User %s\n' % user)
375 376 fp.write('# Date %s\n' % date)
376 377 fp.write('# Node ID %s\n' % revlog.hex(p2))
377 378 fp.write('# Parent ' + revlog.hex(p1) + '\n')
378 379 if merge:
379 380 fp.write('# Parent ' + revlog.hex(p2) + '\n')
380 381 fp.write(message.rstrip() + '\n')
381 382 fp.close()
382 383
383 384 def readlog(self):
384 385 return self.parselog(self.opener('journal'))
385 386
386 387 def unlog(self):
387 388 '''remove changelog journal'''
388 389 absdst = os.path.join(self.path, 'journal')
389 390 if os.path.exists(absdst):
390 391 os.unlink(absdst)
391 392
392 393 def transplantfilter(self, repo, source, root):
393 394 def matchfn(node):
394 395 if self.applied(repo, node, root):
395 396 return False
396 397 if source.changelog.parents(node)[1] != revlog.nullid:
397 398 return False
398 399 extra = source.changelog.read(node)[5]
399 400 cnode = extra.get('transplant_source')
400 401 if cnode and self.applied(repo, cnode, root):
401 402 return False
402 403 return True
403 404
404 405 return matchfn
405 406
406 407 def hasnode(repo, node):
407 408 try:
408 409 return repo.changelog.rev(node) is not None
409 410 except error.RevlogError:
410 411 return False
411 412
412 413 def browserevs(ui, repo, nodes, opts):
413 414 '''interactively transplant changesets'''
414 415 def browsehelp(ui):
415 416 ui.write(_('y: transplant this changeset\n'
416 417 'n: skip this changeset\n'
417 418 'm: merge at this changeset\n'
418 419 'p: show patch\n'
419 420 'c: commit selected changesets\n'
420 421 'q: cancel transplant\n'
421 422 '?: show this help\n'))
422 423
423 424 displayer = cmdutil.show_changeset(ui, repo, opts)
424 425 transplants = []
425 426 merges = []
426 427 for node in nodes:
427 428 displayer.show(repo[node])
428 429 action = None
429 430 while not action:
430 431 action = ui.prompt(_('apply changeset? [ynmpcq?]:'))
431 432 if action == '?':
432 433 browsehelp(ui)
433 434 action = None
434 435 elif action == 'p':
435 436 parent = repo.changelog.parents(node)[0]
436 437 for chunk in patch.diff(repo, parent, node):
437 438 ui.write(chunk)
438 439 action = None
439 440 elif action not in ('y', 'n', 'm', 'c', 'q'):
440 441 ui.write(_('no such option\n'))
441 442 action = None
442 443 if action == 'y':
443 444 transplants.append(node)
444 445 elif action == 'm':
445 446 merges.append(node)
446 447 elif action == 'c':
447 448 break
448 449 elif action == 'q':
449 450 transplants = ()
450 451 merges = ()
451 452 break
452 453 displayer.close()
453 454 return (transplants, merges)
454 455
455 456 @command('transplant',
456 457 [('s', 'source', '', _('pull patches from REPO'), _('REPO')),
457 458 ('b', 'branch', [],
458 459 _('pull patches from branch BRANCH'), _('BRANCH')),
459 460 ('a', 'all', None, _('pull all changesets up to BRANCH')),
460 461 ('p', 'prune', [], _('skip over REV'), _('REV')),
461 462 ('m', 'merge', [], _('merge at REV'), _('REV')),
462 463 ('', 'log', None, _('append transplant info to log message')),
463 464 ('c', 'continue', None, _('continue last transplant session '
464 465 'after repair')),
465 466 ('', 'filter', '',
466 467 _('filter changesets through command'), _('CMD'))],
467 468 _('hg transplant [-s REPO] [-b BRANCH [-a]] [-p REV] '
468 469 '[-m REV] [REV]...'))
469 470 def transplant(ui, repo, *revs, **opts):
470 471 '''transplant changesets from another branch
471 472
472 473 Selected changesets will be applied on top of the current working
473 474 directory with the log of the original changeset. The changesets
474 475 are copied and will thus appear twice in the history. Use the
475 476 rebase extension instead if you want to move a whole branch of
476 477 unpublished changesets.
477 478
478 479 If --log is specified, log messages will have a comment appended
479 480 of the form::
480 481
481 482 (transplanted from CHANGESETHASH)
482 483
483 484 You can rewrite the changelog message with the --filter option.
484 485 Its argument will be invoked with the current changelog message as
485 486 $1 and the patch as $2.
486 487
487 488 If --source/-s is specified, selects changesets from the named
488 489 repository. If --branch/-b is specified, selects changesets from
489 490 the branch holding the named revision, up to that revision. If
490 491 --all/-a is specified, all changesets on the branch will be
491 492 transplanted, otherwise you will be prompted to select the
492 493 changesets you want.
493 494
494 495 :hg:`transplant --branch REVISION --all` will transplant the
495 496 selected branch (up to the named revision) onto your current
496 497 working directory.
497 498
498 499 You can optionally mark selected transplanted changesets as merge
499 500 changesets. You will not be prompted to transplant any ancestors
500 501 of a merged transplant, and you can merge descendants of them
501 502 normally instead of transplanting them.
502 503
503 504 If no merges or revisions are provided, :hg:`transplant` will
504 505 start an interactive changeset browser.
505 506
506 507 If a changeset application fails, you can fix the merge by hand
507 508 and then resume where you left off by calling :hg:`transplant
508 509 --continue/-c`.
509 510 '''
510 511 def incwalk(repo, csets, match=util.always):
511 512 for node in csets:
512 513 if match(node):
513 514 yield node
514 515
515 516 def transplantwalk(repo, root, branches, match=util.always):
516 517 if not branches:
517 518 branches = repo.heads()
518 519 ancestors = []
519 520 for branch in branches:
520 521 ancestors.append(repo.changelog.ancestor(root, branch))
521 522 for node in repo.changelog.nodesbetween(ancestors, branches)[0]:
522 523 if match(node):
523 524 yield node
524 525
525 526 def checkopts(opts, revs):
526 527 if opts.get('continue'):
527 528 if opts.get('branch') or opts.get('all') or opts.get('merge'):
528 529 raise util.Abort(_('--continue is incompatible with '
529 530 'branch, all or merge'))
530 531 return
531 532 if not (opts.get('source') or revs or
532 533 opts.get('merge') or opts.get('branch')):
533 534 raise util.Abort(_('no source URL, branch tag or revision '
534 535 'list provided'))
535 536 if opts.get('all'):
536 537 if not opts.get('branch'):
537 538 raise util.Abort(_('--all requires a branch revision'))
538 539 if revs:
539 540 raise util.Abort(_('--all is incompatible with a '
540 541 'revision list'))
541 542
542 543 checkopts(opts, revs)
543 544
544 545 if not opts.get('log'):
545 546 opts['log'] = ui.config('transplant', 'log')
546 547 if not opts.get('filter'):
547 548 opts['filter'] = ui.config('transplant', 'filter')
548 549
549 550 tp = transplanter(ui, repo)
550 551
551 552 p1, p2 = repo.dirstate.parents()
552 553 if len(repo) > 0 and p1 == revlog.nullid:
553 554 raise util.Abort(_('no revision checked out'))
554 555 if not opts.get('continue'):
555 556 if p2 != revlog.nullid:
556 557 raise util.Abort(_('outstanding uncommitted merges'))
557 558 m, a, r, d = repo.status()[:4]
558 559 if m or a or r or d:
559 560 raise util.Abort(_('outstanding local changes'))
560 561
561 562 sourcerepo = opts.get('source')
562 563 if sourcerepo:
563 564 source = hg.repository(ui, ui.expandpath(sourcerepo))
564 565 branches = map(source.lookup, opts.get('branch', ()))
565 566 source, csets, cleanupfn = bundlerepo.getremotechanges(ui, repo, source,
566 567 onlyheads=branches, force=True)
567 568 else:
568 569 source = repo
569 570 branches = map(source.lookup, opts.get('branch', ()))
570 571 cleanupfn = None
571 572
572 573 try:
573 574 if opts.get('continue'):
574 575 tp.resume(repo, source, opts)
575 576 return
576 577
577 578 tf = tp.transplantfilter(repo, source, p1)
578 579 if opts.get('prune'):
579 580 prune = [source.lookup(r)
580 581 for r in scmutil.revrange(source, opts.get('prune'))]
581 582 matchfn = lambda x: tf(x) and x not in prune
582 583 else:
583 584 matchfn = tf
584 585 merges = map(source.lookup, opts.get('merge', ()))
585 586 revmap = {}
586 587 if revs:
587 588 for r in scmutil.revrange(source, revs):
588 589 revmap[int(r)] = source.lookup(r)
589 590 elif opts.get('all') or not merges:
590 591 if source != repo:
591 592 alltransplants = incwalk(source, csets, match=matchfn)
592 593 else:
593 594 alltransplants = transplantwalk(source, p1, branches,
594 595 match=matchfn)
595 596 if opts.get('all'):
596 597 revs = alltransplants
597 598 else:
598 599 revs, newmerges = browserevs(ui, source, alltransplants, opts)
599 600 merges.extend(newmerges)
600 601 for r in revs:
601 602 revmap[source.changelog.rev(r)] = r
602 603 for r in merges:
603 604 revmap[source.changelog.rev(r)] = r
604 605
605 606 tp.apply(repo, source, revmap, merges, opts)
606 607 finally:
607 608 if cleanupfn:
608 609 cleanupfn()
609 610
610 611 def revsettransplanted(repo, subset, x):
611 612 """``transplanted([set])``
612 613 Transplanted changesets in set, or all transplanted changesets.
613 614 """
614 615 if x:
615 616 s = revset.getset(repo, subset, x)
616 617 else:
617 618 s = subset
618 619 return [r for r in s if repo[r].extra().get('transplant_source')]
619 620
620 621 def kwtransplanted(repo, ctx, **args):
621 622 """:transplanted: String. The node identifier of the transplanted
622 623 changeset if any."""
623 624 n = ctx.extra().get('transplant_source')
624 625 return n and revlog.hex(n) or ''
625 626
626 627 def extsetup(ui):
627 628 revset.symbols['transplanted'] = revsettransplanted
628 629 templatekw.keywords['transplanted'] = kwtransplanted
629 630
630 631 # tell hggettext to extract docstrings from these functions:
631 632 i18nfunctions = [revsettransplanted, kwtransplanted]
@@ -1,1278 +1,1278 b''
1 1 # revlog.py - storage back-end for mercurial
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 """Storage back-end for Mercurial.
9 9
10 10 This provides efficient delta storage with O(1) retrieve and append
11 11 and O(changes) merge between branches.
12 12 """
13 13
14 14 # import stuff from node for others to import from revlog
15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
15 from node import bin, hex, nullid, nullrev
16 16 from i18n import _
17 17 import ancestor, mdiff, parsers, error, util, dagutil
18 18 import struct, zlib, errno
19 19
20 20 _pack = struct.pack
21 21 _unpack = struct.unpack
22 22 _compress = zlib.compress
23 23 _decompress = zlib.decompress
24 24 _sha = util.sha1
25 25
26 26 # revlog header flags
27 27 REVLOGV0 = 0
28 28 REVLOGNG = 1
29 29 REVLOGNGINLINEDATA = (1 << 16)
30 30 REVLOGGENERALDELTA = (1 << 17)
31 31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGGENERALDELTA
35 35
36 36 # revlog index flags
37 37 REVIDX_KNOWN_FLAGS = 0
38 38
39 39 # max size of revlog with inline data
40 40 _maxinline = 131072
41 41 _chunksize = 1048576
42 42
43 43 RevlogError = error.RevlogError
44 44 LookupError = error.LookupError
45 45
46 46 def getoffset(q):
47 47 return int(q >> 16)
48 48
49 49 def gettype(q):
50 50 return int(q & 0xFFFF)
51 51
52 52 def offset_type(offset, type):
53 53 return long(long(offset) << 16 | type)
54 54
55 55 nullhash = _sha(nullid)
56 56
57 57 def hash(text, p1, p2):
58 58 """generate a hash from the given text and its parent hashes
59 59
60 60 This hash combines both the current file contents and its history
61 61 in a manner that makes it easy to distinguish nodes with the same
62 62 content in the revision graph.
63 63 """
64 64 # As of now, if one of the parent node is null, p2 is null
65 65 if p2 == nullid:
66 66 # deep copy of a hash is faster than creating one
67 67 s = nullhash.copy()
68 68 s.update(p1)
69 69 else:
70 70 # none of the parent nodes are nullid
71 71 l = [p1, p2]
72 72 l.sort()
73 73 s = _sha(l[0])
74 74 s.update(l[1])
75 75 s.update(text)
76 76 return s.digest()
77 77
78 78 def compress(text):
79 79 """ generate a possibly-compressed representation of text """
80 80 if not text:
81 81 return ("", text)
82 82 l = len(text)
83 83 bin = None
84 84 if l < 44:
85 85 pass
86 86 elif l > 1000000:
87 87 # zlib makes an internal copy, thus doubling memory usage for
88 88 # large files, so lets do this in pieces
89 89 z = zlib.compressobj()
90 90 p = []
91 91 pos = 0
92 92 while pos < l:
93 93 pos2 = pos + 2**20
94 94 p.append(z.compress(text[pos:pos2]))
95 95 pos = pos2
96 96 p.append(z.flush())
97 97 if sum(map(len, p)) < l:
98 98 bin = "".join(p)
99 99 else:
100 100 bin = _compress(text)
101 101 if bin is None or len(bin) > l:
102 102 if text[0] == '\0':
103 103 return ("", text)
104 104 return ('u', text)
105 105 return ("", bin)
106 106
107 107 def decompress(bin):
108 108 """ decompress the given input """
109 109 if not bin:
110 110 return bin
111 111 t = bin[0]
112 112 if t == '\0':
113 113 return bin
114 114 if t == 'x':
115 115 return _decompress(bin)
116 116 if t == 'u':
117 117 return bin[1:]
118 118 raise RevlogError(_("unknown compression type %r") % t)
119 119
120 120 indexformatv0 = ">4l20s20s20s"
121 121 v0shaoffset = 56
122 122
123 123 class revlogoldio(object):
124 124 def __init__(self):
125 125 self.size = struct.calcsize(indexformatv0)
126 126
127 127 def parseindex(self, data, inline):
128 128 s = self.size
129 129 index = []
130 130 nodemap = {nullid: nullrev}
131 131 n = off = 0
132 132 l = len(data)
133 133 while off + s <= l:
134 134 cur = data[off:off + s]
135 135 off += s
136 136 e = _unpack(indexformatv0, cur)
137 137 # transform to revlogv1 format
138 138 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
139 139 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
140 140 index.append(e2)
141 141 nodemap[e[6]] = n
142 142 n += 1
143 143
144 144 # add the magic null revision at -1
145 145 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
146 146
147 147 return index, nodemap, None
148 148
149 149 def packentry(self, entry, node, version, rev):
150 150 if gettype(entry[0]):
151 151 raise RevlogError(_("index entry flags need RevlogNG"))
152 152 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
153 153 node(entry[5]), node(entry[6]), entry[7])
154 154 return _pack(indexformatv0, *e2)
155 155
156 156 # index ng:
157 157 # 6 bytes: offset
158 158 # 2 bytes: flags
159 159 # 4 bytes: compressed length
160 160 # 4 bytes: uncompressed length
161 161 # 4 bytes: base rev
162 162 # 4 bytes: link rev
163 163 # 4 bytes: parent 1 rev
164 164 # 4 bytes: parent 2 rev
165 165 # 32 bytes: nodeid
166 166 indexformatng = ">Qiiiiii20s12x"
167 167 ngshaoffset = 32
168 168 versionformat = ">I"
169 169
170 170 class revlogio(object):
171 171 def __init__(self):
172 172 self.size = struct.calcsize(indexformatng)
173 173
174 174 def parseindex(self, data, inline):
175 175 # call the C implementation to parse the index data
176 176 index, cache = parsers.parse_index2(data, inline)
177 177 return index, None, cache
178 178
179 179 def packentry(self, entry, node, version, rev):
180 180 p = _pack(indexformatng, *entry)
181 181 if rev == 0:
182 182 p = _pack(versionformat, version) + p[4:]
183 183 return p
184 184
185 185 class revlog(object):
186 186 """
187 187 the underlying revision storage object
188 188
189 189 A revlog consists of two parts, an index and the revision data.
190 190
191 191 The index is a file with a fixed record size containing
192 192 information on each revision, including its nodeid (hash), the
193 193 nodeids of its parents, the position and offset of its data within
194 194 the data file, and the revision it's based on. Finally, each entry
195 195 contains a linkrev entry that can serve as a pointer to external
196 196 data.
197 197
198 198 The revision data itself is a linear collection of data chunks.
199 199 Each chunk represents a revision and is usually represented as a
200 200 delta against the previous chunk. To bound lookup time, runs of
201 201 deltas are limited to about 2 times the length of the original
202 202 version data. This makes retrieval of a version proportional to
203 203 its size, or O(1) relative to the number of revisions.
204 204
205 205 Both pieces of the revlog are written to in an append-only
206 206 fashion, which means we never need to rewrite a file to insert or
207 207 remove data, and can use some simple techniques to avoid the need
208 208 for locking while reading.
209 209 """
210 210 def __init__(self, opener, indexfile):
211 211 """
212 212 create a revlog object
213 213
214 214 opener is a function that abstracts the file opening operation
215 215 and can be used to implement COW semantics or the like.
216 216 """
217 217 self.indexfile = indexfile
218 218 self.datafile = indexfile[:-2] + ".d"
219 219 self.opener = opener
220 220 self._cache = None
221 221 self._basecache = (0, 0)
222 222 self._chunkcache = (0, '')
223 223 self.index = []
224 224 self._pcache = {}
225 225 self._nodecache = {nullid: nullrev}
226 226 self._nodepos = None
227 227
228 228 v = REVLOG_DEFAULT_VERSION
229 229 if hasattr(opener, 'options'):
230 230 if 'revlogv1' in opener.options:
231 231 if 'generaldelta' in opener.options:
232 232 v |= REVLOGGENERALDELTA
233 233 else:
234 234 v = 0
235 235
236 236 i = ''
237 237 self._initempty = True
238 238 try:
239 239 f = self.opener(self.indexfile)
240 240 i = f.read()
241 241 f.close()
242 242 if len(i) > 0:
243 243 v = struct.unpack(versionformat, i[:4])[0]
244 244 self._initempty = False
245 245 except IOError, inst:
246 246 if inst.errno != errno.ENOENT:
247 247 raise
248 248
249 249 self.version = v
250 250 self._inline = v & REVLOGNGINLINEDATA
251 251 self._generaldelta = v & REVLOGGENERALDELTA
252 252 flags = v & ~0xFFFF
253 253 fmt = v & 0xFFFF
254 254 if fmt == REVLOGV0 and flags:
255 255 raise RevlogError(_("index %s unknown flags %#04x for format v0")
256 256 % (self.indexfile, flags >> 16))
257 257 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
258 258 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
259 259 % (self.indexfile, flags >> 16))
260 260 elif fmt > REVLOGNG:
261 261 raise RevlogError(_("index %s unknown format %d")
262 262 % (self.indexfile, fmt))
263 263
264 264 self._io = revlogio()
265 265 if self.version == REVLOGV0:
266 266 self._io = revlogoldio()
267 267 try:
268 268 d = self._io.parseindex(i, self._inline)
269 269 except (ValueError, IndexError):
270 270 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
271 271 self.index, nodemap, self._chunkcache = d
272 272 if nodemap is not None:
273 273 self.nodemap = self._nodecache = nodemap
274 274 if not self._chunkcache:
275 275 self._chunkclear()
276 276
277 277 def tip(self):
278 278 return self.node(len(self.index) - 2)
279 279 def __len__(self):
280 280 return len(self.index) - 1
281 281 def __iter__(self):
282 282 for i in xrange(len(self)):
283 283 yield i
284 284
285 285 @util.propertycache
286 286 def nodemap(self):
287 287 self.rev(self.node(0))
288 288 return self._nodecache
289 289
290 290 def rev(self, node):
291 291 try:
292 292 return self._nodecache[node]
293 293 except KeyError:
294 294 n = self._nodecache
295 295 i = self.index
296 296 p = self._nodepos
297 297 if p is None:
298 298 p = len(i) - 2
299 299 for r in xrange(p, -1, -1):
300 300 v = i[r][7]
301 301 n[v] = r
302 302 if v == node:
303 303 self._nodepos = r - 1
304 304 return r
305 305 raise LookupError(node, self.indexfile, _('no node'))
306 306
307 307 def node(self, rev):
308 308 return self.index[rev][7]
309 309 def linkrev(self, rev):
310 310 return self.index[rev][4]
311 311 def parents(self, node):
312 312 i = self.index
313 313 d = i[self.rev(node)]
314 314 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
315 315 def parentrevs(self, rev):
316 316 return self.index[rev][5:7]
317 317 def start(self, rev):
318 318 return int(self.index[rev][0] >> 16)
319 319 def end(self, rev):
320 320 return self.start(rev) + self.length(rev)
321 321 def length(self, rev):
322 322 return self.index[rev][1]
323 323 def chainbase(self, rev):
324 324 index = self.index
325 325 base = index[rev][3]
326 326 while base != rev:
327 327 rev = base
328 328 base = index[rev][3]
329 329 return base
330 330 def flags(self, rev):
331 331 return self.index[rev][0] & 0xFFFF
332 332 def rawsize(self, rev):
333 333 """return the length of the uncompressed text for a given revision"""
334 334 l = self.index[rev][2]
335 335 if l >= 0:
336 336 return l
337 337
338 338 t = self.revision(self.node(rev))
339 339 return len(t)
340 340 size = rawsize
341 341
342 342 def reachable(self, node, stop=None):
343 343 """return the set of all nodes ancestral to a given node, including
344 344 the node itself, stopping when stop is matched"""
345 345 reachable = set((node,))
346 346 visit = [node]
347 347 if stop:
348 348 stopn = self.rev(stop)
349 349 else:
350 350 stopn = 0
351 351 while visit:
352 352 n = visit.pop(0)
353 353 if n == stop:
354 354 continue
355 355 if n == nullid:
356 356 continue
357 357 for p in self.parents(n):
358 358 if self.rev(p) < stopn:
359 359 continue
360 360 if p not in reachable:
361 361 reachable.add(p)
362 362 visit.append(p)
363 363 return reachable
364 364
365 365 def ancestors(self, *revs):
366 366 """Generate the ancestors of 'revs' in reverse topological order.
367 367
368 368 Yield a sequence of revision numbers starting with the parents
369 369 of each revision in revs, i.e., each revision is *not* considered
370 370 an ancestor of itself. Results are in breadth-first order:
371 371 parents of each rev in revs, then parents of those, etc. Result
372 372 does not include the null revision."""
373 373 visit = list(revs)
374 374 seen = set([nullrev])
375 375 while visit:
376 376 for parent in self.parentrevs(visit.pop(0)):
377 377 if parent not in seen:
378 378 visit.append(parent)
379 379 seen.add(parent)
380 380 yield parent
381 381
382 382 def descendants(self, *revs):
383 383 """Generate the descendants of 'revs' in revision order.
384 384
385 385 Yield a sequence of revision numbers starting with a child of
386 386 some rev in revs, i.e., each revision is *not* considered a
387 387 descendant of itself. Results are ordered by revision number (a
388 388 topological sort)."""
389 389 first = min(revs)
390 390 if first == nullrev:
391 391 for i in self:
392 392 yield i
393 393 return
394 394
395 395 seen = set(revs)
396 396 for i in xrange(first + 1, len(self)):
397 397 for x in self.parentrevs(i):
398 398 if x != nullrev and x in seen:
399 399 seen.add(i)
400 400 yield i
401 401 break
402 402
403 403 def findcommonmissing(self, common=None, heads=None):
404 404 """Return a tuple of the ancestors of common and the ancestors of heads
405 405 that are not ancestors of common.
406 406
407 407 More specifically, the second element is a list of nodes N such that
408 408 every N satisfies the following constraints:
409 409
410 410 1. N is an ancestor of some node in 'heads'
411 411 2. N is not an ancestor of any node in 'common'
412 412
413 413 The list is sorted by revision number, meaning it is
414 414 topologically sorted.
415 415
416 416 'heads' and 'common' are both lists of node IDs. If heads is
417 417 not supplied, uses all of the revlog's heads. If common is not
418 418 supplied, uses nullid."""
419 419 if common is None:
420 420 common = [nullid]
421 421 if heads is None:
422 422 heads = self.heads()
423 423
424 424 common = [self.rev(n) for n in common]
425 425 heads = [self.rev(n) for n in heads]
426 426
427 427 # we want the ancestors, but inclusive
428 428 has = set(self.ancestors(*common))
429 429 has.add(nullrev)
430 430 has.update(common)
431 431
432 432 # take all ancestors from heads that aren't in has
433 433 missing = set()
434 434 visit = [r for r in heads if r not in has]
435 435 while visit:
436 436 r = visit.pop(0)
437 437 if r in missing:
438 438 continue
439 439 else:
440 440 missing.add(r)
441 441 for p in self.parentrevs(r):
442 442 if p not in has:
443 443 visit.append(p)
444 444 missing = list(missing)
445 445 missing.sort()
446 446 return has, [self.node(r) for r in missing]
447 447
448 448 def findmissing(self, common=None, heads=None):
449 449 """Return the ancestors of heads that are not ancestors of common.
450 450
451 451 More specifically, return a list of nodes N such that every N
452 452 satisfies the following constraints:
453 453
454 454 1. N is an ancestor of some node in 'heads'
455 455 2. N is not an ancestor of any node in 'common'
456 456
457 457 The list is sorted by revision number, meaning it is
458 458 topologically sorted.
459 459
460 460 'heads' and 'common' are both lists of node IDs. If heads is
461 461 not supplied, uses all of the revlog's heads. If common is not
462 462 supplied, uses nullid."""
463 463 _common, missing = self.findcommonmissing(common, heads)
464 464 return missing
465 465
466 466 def nodesbetween(self, roots=None, heads=None):
467 467 """Return a topological path from 'roots' to 'heads'.
468 468
469 469 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
470 470 topologically sorted list of all nodes N that satisfy both of
471 471 these constraints:
472 472
473 473 1. N is a descendant of some node in 'roots'
474 474 2. N is an ancestor of some node in 'heads'
475 475
476 476 Every node is considered to be both a descendant and an ancestor
477 477 of itself, so every reachable node in 'roots' and 'heads' will be
478 478 included in 'nodes'.
479 479
480 480 'outroots' is the list of reachable nodes in 'roots', i.e., the
481 481 subset of 'roots' that is returned in 'nodes'. Likewise,
482 482 'outheads' is the subset of 'heads' that is also in 'nodes'.
483 483
484 484 'roots' and 'heads' are both lists of node IDs. If 'roots' is
485 485 unspecified, uses nullid as the only root. If 'heads' is
486 486 unspecified, uses list of all of the revlog's heads."""
487 487 nonodes = ([], [], [])
488 488 if roots is not None:
489 489 roots = list(roots)
490 490 if not roots:
491 491 return nonodes
492 492 lowestrev = min([self.rev(n) for n in roots])
493 493 else:
494 494 roots = [nullid] # Everybody's a descendent of nullid
495 495 lowestrev = nullrev
496 496 if (lowestrev == nullrev) and (heads is None):
497 497 # We want _all_ the nodes!
498 498 return ([self.node(r) for r in self], [nullid], list(self.heads()))
499 499 if heads is None:
500 500 # All nodes are ancestors, so the latest ancestor is the last
501 501 # node.
502 502 highestrev = len(self) - 1
503 503 # Set ancestors to None to signal that every node is an ancestor.
504 504 ancestors = None
505 505 # Set heads to an empty dictionary for later discovery of heads
506 506 heads = {}
507 507 else:
508 508 heads = list(heads)
509 509 if not heads:
510 510 return nonodes
511 511 ancestors = set()
512 512 # Turn heads into a dictionary so we can remove 'fake' heads.
513 513 # Also, later we will be using it to filter out the heads we can't
514 514 # find from roots.
515 515 heads = dict.fromkeys(heads, False)
516 516 # Start at the top and keep marking parents until we're done.
517 517 nodestotag = set(heads)
518 518 # Remember where the top was so we can use it as a limit later.
519 519 highestrev = max([self.rev(n) for n in nodestotag])
520 520 while nodestotag:
521 521 # grab a node to tag
522 522 n = nodestotag.pop()
523 523 # Never tag nullid
524 524 if n == nullid:
525 525 continue
526 526 # A node's revision number represents its place in a
527 527 # topologically sorted list of nodes.
528 528 r = self.rev(n)
529 529 if r >= lowestrev:
530 530 if n not in ancestors:
531 531 # If we are possibly a descendent of one of the roots
532 532 # and we haven't already been marked as an ancestor
533 533 ancestors.add(n) # Mark as ancestor
534 534 # Add non-nullid parents to list of nodes to tag.
535 535 nodestotag.update([p for p in self.parents(n) if
536 536 p != nullid])
537 537 elif n in heads: # We've seen it before, is it a fake head?
538 538 # So it is, real heads should not be the ancestors of
539 539 # any other heads.
540 540 heads.pop(n)
541 541 if not ancestors:
542 542 return nonodes
543 543 # Now that we have our set of ancestors, we want to remove any
544 544 # roots that are not ancestors.
545 545
546 546 # If one of the roots was nullid, everything is included anyway.
547 547 if lowestrev > nullrev:
548 548 # But, since we weren't, let's recompute the lowest rev to not
549 549 # include roots that aren't ancestors.
550 550
551 551 # Filter out roots that aren't ancestors of heads
552 552 roots = [n for n in roots if n in ancestors]
553 553 # Recompute the lowest revision
554 554 if roots:
555 555 lowestrev = min([self.rev(n) for n in roots])
556 556 else:
557 557 # No more roots? Return empty list
558 558 return nonodes
559 559 else:
560 560 # We are descending from nullid, and don't need to care about
561 561 # any other roots.
562 562 lowestrev = nullrev
563 563 roots = [nullid]
564 564 # Transform our roots list into a set.
565 565 descendents = set(roots)
566 566 # Also, keep the original roots so we can filter out roots that aren't
567 567 # 'real' roots (i.e. are descended from other roots).
568 568 roots = descendents.copy()
569 569 # Our topologically sorted list of output nodes.
570 570 orderedout = []
571 571 # Don't start at nullid since we don't want nullid in our output list,
572 572 # and if nullid shows up in descedents, empty parents will look like
573 573 # they're descendents.
574 574 for r in xrange(max(lowestrev, 0), highestrev + 1):
575 575 n = self.node(r)
576 576 isdescendent = False
577 577 if lowestrev == nullrev: # Everybody is a descendent of nullid
578 578 isdescendent = True
579 579 elif n in descendents:
580 580 # n is already a descendent
581 581 isdescendent = True
582 582 # This check only needs to be done here because all the roots
583 583 # will start being marked is descendents before the loop.
584 584 if n in roots:
585 585 # If n was a root, check if it's a 'real' root.
586 586 p = tuple(self.parents(n))
587 587 # If any of its parents are descendents, it's not a root.
588 588 if (p[0] in descendents) or (p[1] in descendents):
589 589 roots.remove(n)
590 590 else:
591 591 p = tuple(self.parents(n))
592 592 # A node is a descendent if either of its parents are
593 593 # descendents. (We seeded the dependents list with the roots
594 594 # up there, remember?)
595 595 if (p[0] in descendents) or (p[1] in descendents):
596 596 descendents.add(n)
597 597 isdescendent = True
598 598 if isdescendent and ((ancestors is None) or (n in ancestors)):
599 599 # Only include nodes that are both descendents and ancestors.
600 600 orderedout.append(n)
601 601 if (ancestors is not None) and (n in heads):
602 602 # We're trying to figure out which heads are reachable
603 603 # from roots.
604 604 # Mark this head as having been reached
605 605 heads[n] = True
606 606 elif ancestors is None:
607 607 # Otherwise, we're trying to discover the heads.
608 608 # Assume this is a head because if it isn't, the next step
609 609 # will eventually remove it.
610 610 heads[n] = True
611 611 # But, obviously its parents aren't.
612 612 for p in self.parents(n):
613 613 heads.pop(p, None)
614 614 heads = [n for n, flag in heads.iteritems() if flag]
615 615 roots = list(roots)
616 616 assert orderedout
617 617 assert roots
618 618 assert heads
619 619 return (orderedout, roots, heads)
620 620
621 621 def headrevs(self):
622 622 count = len(self)
623 623 if not count:
624 624 return [nullrev]
625 625 ishead = [1] * (count + 1)
626 626 index = self.index
627 627 for r in xrange(count):
628 628 e = index[r]
629 629 ishead[e[5]] = ishead[e[6]] = 0
630 630 return [r for r in xrange(count) if ishead[r]]
631 631
632 632 def heads(self, start=None, stop=None):
633 633 """return the list of all nodes that have no children
634 634
635 635 if start is specified, only heads that are descendants of
636 636 start will be returned
637 637 if stop is specified, it will consider all the revs from stop
638 638 as if they had no children
639 639 """
640 640 if start is None and stop is None:
641 641 if not len(self):
642 642 return [nullid]
643 643 return [self.node(r) for r in self.headrevs()]
644 644
645 645 if start is None:
646 646 start = nullid
647 647 if stop is None:
648 648 stop = []
649 649 stoprevs = set([self.rev(n) for n in stop])
650 650 startrev = self.rev(start)
651 651 reachable = set((startrev,))
652 652 heads = set((startrev,))
653 653
654 654 parentrevs = self.parentrevs
655 655 for r in xrange(startrev + 1, len(self)):
656 656 for p in parentrevs(r):
657 657 if p in reachable:
658 658 if r not in stoprevs:
659 659 reachable.add(r)
660 660 heads.add(r)
661 661 if p in heads and p not in stoprevs:
662 662 heads.remove(p)
663 663
664 664 return [self.node(r) for r in heads]
665 665
666 666 def children(self, node):
667 667 """find the children of a given node"""
668 668 c = []
669 669 p = self.rev(node)
670 670 for r in range(p + 1, len(self)):
671 671 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
672 672 if prevs:
673 673 for pr in prevs:
674 674 if pr == p:
675 675 c.append(self.node(r))
676 676 elif p == nullrev:
677 677 c.append(self.node(r))
678 678 return c
679 679
680 680 def descendant(self, start, end):
681 681 if start == nullrev:
682 682 return True
683 683 for i in self.descendants(start):
684 684 if i == end:
685 685 return True
686 686 elif i > end:
687 687 break
688 688 return False
689 689
690 690 def ancestor(self, a, b):
691 691 """calculate the least common ancestor of nodes a and b"""
692 692
693 693 # fast path, check if it is a descendant
694 694 a, b = self.rev(a), self.rev(b)
695 695 start, end = sorted((a, b))
696 696 if self.descendant(start, end):
697 697 return self.node(start)
698 698
699 699 def parents(rev):
700 700 return [p for p in self.parentrevs(rev) if p != nullrev]
701 701
702 702 c = ancestor.ancestor(a, b, parents)
703 703 if c is None:
704 704 return nullid
705 705
706 706 return self.node(c)
707 707
708 708 def _match(self, id):
709 709 if isinstance(id, (long, int)):
710 710 # rev
711 711 return self.node(id)
712 712 if len(id) == 20:
713 713 # possibly a binary node
714 714 # odds of a binary node being all hex in ASCII are 1 in 10**25
715 715 try:
716 716 node = id
717 717 self.rev(node) # quick search the index
718 718 return node
719 719 except LookupError:
720 720 pass # may be partial hex id
721 721 try:
722 722 # str(rev)
723 723 rev = int(id)
724 724 if str(rev) != id:
725 725 raise ValueError
726 726 if rev < 0:
727 727 rev = len(self) + rev
728 728 if rev < 0 or rev >= len(self):
729 729 raise ValueError
730 730 return self.node(rev)
731 731 except (ValueError, OverflowError):
732 732 pass
733 733 if len(id) == 40:
734 734 try:
735 735 # a full hex nodeid?
736 736 node = bin(id)
737 737 self.rev(node)
738 738 return node
739 739 except (TypeError, LookupError):
740 740 pass
741 741
742 742 def _partialmatch(self, id):
743 743 if id in self._pcache:
744 744 return self._pcache[id]
745 745
746 746 if len(id) < 40:
747 747 try:
748 748 # hex(node)[:...]
749 749 l = len(id) // 2 # grab an even number of digits
750 750 prefix = bin(id[:l * 2])
751 751 nl = [e[7] for e in self.index if e[7].startswith(prefix)]
752 752 nl = [n for n in nl if hex(n).startswith(id)]
753 753 if len(nl) > 0:
754 754 if len(nl) == 1:
755 755 self._pcache[id] = nl[0]
756 756 return nl[0]
757 757 raise LookupError(id, self.indexfile,
758 758 _('ambiguous identifier'))
759 759 return None
760 760 except TypeError:
761 761 pass
762 762
763 763 def lookup(self, id):
764 764 """locate a node based on:
765 765 - revision number or str(revision number)
766 766 - nodeid or subset of hex nodeid
767 767 """
768 768 n = self._match(id)
769 769 if n is not None:
770 770 return n
771 771 n = self._partialmatch(id)
772 772 if n:
773 773 return n
774 774
775 775 raise LookupError(id, self.indexfile, _('no match found'))
776 776
777 777 def cmp(self, node, text):
778 778 """compare text with a given file revision
779 779
780 780 returns True if text is different than what is stored.
781 781 """
782 782 p1, p2 = self.parents(node)
783 783 return hash(text, p1, p2) != node
784 784
785 785 def _addchunk(self, offset, data):
786 786 o, d = self._chunkcache
787 787 # try to add to existing cache
788 788 if o + len(d) == offset and len(d) + len(data) < _chunksize:
789 789 self._chunkcache = o, d + data
790 790 else:
791 791 self._chunkcache = offset, data
792 792
793 793 def _loadchunk(self, offset, length):
794 794 if self._inline:
795 795 df = self.opener(self.indexfile)
796 796 else:
797 797 df = self.opener(self.datafile)
798 798
799 799 readahead = max(65536, length)
800 800 df.seek(offset)
801 801 d = df.read(readahead)
802 802 self._addchunk(offset, d)
803 803 if readahead > length:
804 804 return d[:length]
805 805 return d
806 806
807 807 def _getchunk(self, offset, length):
808 808 o, d = self._chunkcache
809 809 l = len(d)
810 810
811 811 # is it in the cache?
812 812 cachestart = offset - o
813 813 cacheend = cachestart + length
814 814 if cachestart >= 0 and cacheend <= l:
815 815 if cachestart == 0 and cacheend == l:
816 816 return d # avoid a copy
817 817 return d[cachestart:cacheend]
818 818
819 819 return self._loadchunk(offset, length)
820 820
821 821 def _chunkraw(self, startrev, endrev):
822 822 start = self.start(startrev)
823 823 length = self.end(endrev) - start
824 824 if self._inline:
825 825 start += (startrev + 1) * self._io.size
826 826 return self._getchunk(start, length)
827 827
828 828 def _chunk(self, rev):
829 829 return decompress(self._chunkraw(rev, rev))
830 830
831 831 def _chunkbase(self, rev):
832 832 return self._chunk(rev)
833 833
834 834 def _chunkclear(self):
835 835 self._chunkcache = (0, '')
836 836
837 837 def deltaparent(self, rev):
838 838 """return deltaparent of the given revision"""
839 839 base = self.index[rev][3]
840 840 if base == rev:
841 841 return nullrev
842 842 elif self._generaldelta:
843 843 return base
844 844 else:
845 845 return rev - 1
846 846
847 847 def revdiff(self, rev1, rev2):
848 848 """return or calculate a delta between two revisions"""
849 849 if rev1 != nullrev and self.deltaparent(rev2) == rev1:
850 850 return self._chunk(rev2)
851 851
852 852 return mdiff.textdiff(self.revision(self.node(rev1)),
853 853 self.revision(self.node(rev2)))
854 854
855 855 def revision(self, node):
856 856 """return an uncompressed revision of a given node"""
857 857 cachedrev = None
858 858 if node == nullid:
859 859 return ""
860 860 if self._cache:
861 861 if self._cache[0] == node:
862 862 return self._cache[2]
863 863 cachedrev = self._cache[1]
864 864
865 865 # look up what we need to read
866 866 text = None
867 867 rev = self.rev(node)
868 868
869 869 # check rev flags
870 870 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
871 871 raise RevlogError(_('incompatible revision flag %x') %
872 872 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
873 873
874 874 # build delta chain
875 875 chain = []
876 876 index = self.index # for performance
877 877 generaldelta = self._generaldelta
878 878 iterrev = rev
879 879 e = index[iterrev]
880 880 while iterrev != e[3] and iterrev != cachedrev:
881 881 chain.append(iterrev)
882 882 if generaldelta:
883 883 iterrev = e[3]
884 884 else:
885 885 iterrev -= 1
886 886 e = index[iterrev]
887 887 chain.reverse()
888 888 base = iterrev
889 889
890 890 if iterrev == cachedrev:
891 891 # cache hit
892 892 text = self._cache[2]
893 893
894 894 # drop cache to save memory
895 895 self._cache = None
896 896
897 897 self._chunkraw(base, rev)
898 898 if text is None:
899 899 text = self._chunkbase(base)
900 900
901 901 bins = [self._chunk(r) for r in chain]
902 902 text = mdiff.patches(text, bins)
903 903
904 904 text = self._checkhash(text, node, rev)
905 905
906 906 self._cache = (node, rev, text)
907 907 return text
908 908
909 909 def _checkhash(self, text, node, rev):
910 910 p1, p2 = self.parents(node)
911 911 if node != hash(text, p1, p2):
912 912 raise RevlogError(_("integrity check failed on %s:%d")
913 913 % (self.indexfile, rev))
914 914 return text
915 915
916 916 def checkinlinesize(self, tr, fp=None):
917 917 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
918 918 return
919 919
920 920 trinfo = tr.find(self.indexfile)
921 921 if trinfo is None:
922 922 raise RevlogError(_("%s not found in the transaction")
923 923 % self.indexfile)
924 924
925 925 trindex = trinfo[2]
926 926 dataoff = self.start(trindex)
927 927
928 928 tr.add(self.datafile, dataoff)
929 929
930 930 if fp:
931 931 fp.flush()
932 932 fp.close()
933 933
934 934 df = self.opener(self.datafile, 'w')
935 935 try:
936 936 for r in self:
937 937 df.write(self._chunkraw(r, r))
938 938 finally:
939 939 df.close()
940 940
941 941 fp = self.opener(self.indexfile, 'w', atomictemp=True)
942 942 self.version &= ~(REVLOGNGINLINEDATA)
943 943 self._inline = False
944 944 for i in self:
945 945 e = self._io.packentry(self.index[i], self.node, self.version, i)
946 946 fp.write(e)
947 947
948 948 # if we don't call rename, the temp file will never replace the
949 949 # real index
950 950 fp.rename()
951 951
952 952 tr.replace(self.indexfile, trindex * self._io.size)
953 953 self._chunkclear()
954 954
955 955 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
956 956 """add a revision to the log
957 957
958 958 text - the revision data to add
959 959 transaction - the transaction object used for rollback
960 960 link - the linkrev data to add
961 961 p1, p2 - the parent nodeids of the revision
962 962 cachedelta - an optional precomputed delta
963 963 """
964 964 node = hash(text, p1, p2)
965 965 if node in self.nodemap:
966 966 return node
967 967
968 968 dfh = None
969 969 if not self._inline:
970 970 dfh = self.opener(self.datafile, "a")
971 971 ifh = self.opener(self.indexfile, "a+")
972 972 try:
973 973 return self._addrevision(node, text, transaction, link, p1, p2,
974 974 cachedelta, ifh, dfh)
975 975 finally:
976 976 if dfh:
977 977 dfh.close()
978 978 ifh.close()
979 979
980 980 def _addrevision(self, node, text, transaction, link, p1, p2,
981 981 cachedelta, ifh, dfh):
982 982 """internal function to add revisions to the log
983 983
984 984 see addrevision for argument descriptions.
985 985 invariants:
986 986 - text is optional (can be None); if not set, cachedelta must be set.
987 987 if both are set, they must correspond to eachother.
988 988 """
989 989 btext = [text]
990 990 def buildtext():
991 991 if btext[0] is not None:
992 992 return btext[0]
993 993 # flush any pending writes here so we can read it in revision
994 994 if dfh:
995 995 dfh.flush()
996 996 ifh.flush()
997 997 basetext = self.revision(self.node(cachedelta[0]))
998 998 btext[0] = mdiff.patch(basetext, cachedelta[1])
999 999 chk = hash(btext[0], p1, p2)
1000 1000 if chk != node:
1001 1001 raise RevlogError(_("consistency error in delta"))
1002 1002 return btext[0]
1003 1003
1004 1004 def builddelta(rev):
1005 1005 # can we use the cached delta?
1006 1006 if cachedelta and cachedelta[0] == rev:
1007 1007 delta = cachedelta[1]
1008 1008 else:
1009 1009 t = buildtext()
1010 1010 ptext = self.revision(self.node(rev))
1011 1011 delta = mdiff.textdiff(ptext, t)
1012 1012 data = compress(delta)
1013 1013 l = len(data[1]) + len(data[0])
1014 1014 if basecache[0] == rev:
1015 1015 chainbase = basecache[1]
1016 1016 else:
1017 1017 chainbase = self.chainbase(rev)
1018 1018 dist = l + offset - self.start(chainbase)
1019 1019 if self._generaldelta:
1020 1020 base = rev
1021 1021 else:
1022 1022 base = chainbase
1023 1023 return dist, l, data, base, chainbase
1024 1024
1025 1025 curr = len(self)
1026 1026 prev = curr - 1
1027 1027 base = chainbase = curr
1028 1028 offset = self.end(prev)
1029 1029 flags = 0
1030 1030 d = None
1031 1031 basecache = self._basecache
1032 1032 p1r, p2r = self.rev(p1), self.rev(p2)
1033 1033
1034 1034 # should we try to build a delta?
1035 1035 if prev != nullrev:
1036 1036 if self._generaldelta:
1037 1037 if p1r >= basecache[1]:
1038 1038 d = builddelta(p1r)
1039 1039 elif p2r >= basecache[1]:
1040 1040 d = builddelta(p2r)
1041 1041 else:
1042 1042 d = builddelta(prev)
1043 1043 else:
1044 1044 d = builddelta(prev)
1045 1045 dist, l, data, base, chainbase = d
1046 1046
1047 1047 # full versions are inserted when the needed deltas
1048 1048 # become comparable to the uncompressed text
1049 1049 if text is None:
1050 1050 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1051 1051 cachedelta[1])
1052 1052 else:
1053 1053 textlen = len(text)
1054 1054 if d is None or dist > textlen * 2:
1055 1055 text = buildtext()
1056 1056 data = compress(text)
1057 1057 l = len(data[1]) + len(data[0])
1058 1058 base = chainbase = curr
1059 1059
1060 1060 e = (offset_type(offset, flags), l, textlen,
1061 1061 base, link, p1r, p2r, node)
1062 1062 self.index.insert(-1, e)
1063 1063 self.nodemap[node] = curr
1064 1064
1065 1065 entry = self._io.packentry(e, self.node, self.version, curr)
1066 1066 if not self._inline:
1067 1067 transaction.add(self.datafile, offset)
1068 1068 transaction.add(self.indexfile, curr * len(entry))
1069 1069 if data[0]:
1070 1070 dfh.write(data[0])
1071 1071 dfh.write(data[1])
1072 1072 dfh.flush()
1073 1073 ifh.write(entry)
1074 1074 else:
1075 1075 offset += curr * self._io.size
1076 1076 transaction.add(self.indexfile, offset, curr)
1077 1077 ifh.write(entry)
1078 1078 ifh.write(data[0])
1079 1079 ifh.write(data[1])
1080 1080 self.checkinlinesize(transaction, ifh)
1081 1081
1082 1082 if type(text) == str: # only accept immutable objects
1083 1083 self._cache = (node, curr, text)
1084 1084 self._basecache = (curr, chainbase)
1085 1085 return node
1086 1086
1087 1087 def group(self, nodelist, bundler, reorder=None):
1088 1088 """Calculate a delta group, yielding a sequence of changegroup chunks
1089 1089 (strings).
1090 1090
1091 1091 Given a list of changeset revs, return a set of deltas and
1092 1092 metadata corresponding to nodes. The first delta is
1093 1093 first parent(nodelist[0]) -> nodelist[0], the receiver is
1094 1094 guaranteed to have this parent as it has all history before
1095 1095 these changesets. In the case firstparent is nullrev the
1096 1096 changegroup starts with a full revision.
1097 1097 """
1098 1098
1099 1099 # for generaldelta revlogs, we linearize the revs; this will both be
1100 1100 # much quicker and generate a much smaller bundle
1101 1101 if (self._generaldelta and reorder is not False) or reorder:
1102 1102 dag = dagutil.revlogdag(self)
1103 1103 revs = set(self.rev(n) for n in nodelist)
1104 1104 revs = dag.linearize(revs)
1105 1105 else:
1106 1106 revs = sorted([self.rev(n) for n in nodelist])
1107 1107
1108 1108 # if we don't have any revisions touched by these changesets, bail
1109 1109 if not revs:
1110 1110 yield bundler.close()
1111 1111 return
1112 1112
1113 1113 # add the parent of the first rev
1114 1114 p = self.parentrevs(revs[0])[0]
1115 1115 revs.insert(0, p)
1116 1116
1117 1117 # build deltas
1118 1118 for r in xrange(len(revs) - 1):
1119 1119 prev, curr = revs[r], revs[r + 1]
1120 1120 for c in bundler.revchunk(self, curr, prev):
1121 1121 yield c
1122 1122
1123 1123 yield bundler.close()
1124 1124
1125 1125 def addgroup(self, bundle, linkmapper, transaction):
1126 1126 """
1127 1127 add a delta group
1128 1128
1129 1129 given a set of deltas, add them to the revision log. the
1130 1130 first delta is against its parent, which should be in our
1131 1131 log, the rest are against the previous delta.
1132 1132 """
1133 1133
1134 1134 # track the base of the current delta log
1135 1135 node = None
1136 1136
1137 1137 r = len(self)
1138 1138 end = 0
1139 1139 if r:
1140 1140 end = self.end(r - 1)
1141 1141 ifh = self.opener(self.indexfile, "a+")
1142 1142 isize = r * self._io.size
1143 1143 if self._inline:
1144 1144 transaction.add(self.indexfile, end + isize, r)
1145 1145 dfh = None
1146 1146 else:
1147 1147 transaction.add(self.indexfile, isize, r)
1148 1148 transaction.add(self.datafile, end)
1149 1149 dfh = self.opener(self.datafile, "a")
1150 1150
1151 1151 try:
1152 1152 # loop through our set of deltas
1153 1153 chain = None
1154 1154 while 1:
1155 1155 chunkdata = bundle.deltachunk(chain)
1156 1156 if not chunkdata:
1157 1157 break
1158 1158 node = chunkdata['node']
1159 1159 p1 = chunkdata['p1']
1160 1160 p2 = chunkdata['p2']
1161 1161 cs = chunkdata['cs']
1162 1162 deltabase = chunkdata['deltabase']
1163 1163 delta = chunkdata['delta']
1164 1164
1165 1165 link = linkmapper(cs)
1166 1166 if node in self.nodemap:
1167 1167 # this can happen if two branches make the same change
1168 1168 chain = node
1169 1169 continue
1170 1170
1171 1171 for p in (p1, p2):
1172 1172 if not p in self.nodemap:
1173 1173 raise LookupError(p, self.indexfile,
1174 1174 _('unknown parent'))
1175 1175
1176 1176 if deltabase not in self.nodemap:
1177 1177 raise LookupError(deltabase, self.indexfile,
1178 1178 _('unknown delta base'))
1179 1179
1180 1180 baserev = self.rev(deltabase)
1181 1181 chain = self._addrevision(node, None, transaction, link,
1182 1182 p1, p2, (baserev, delta), ifh, dfh)
1183 1183 if not dfh and not self._inline:
1184 1184 # addrevision switched from inline to conventional
1185 1185 # reopen the index
1186 1186 ifh.close()
1187 1187 dfh = self.opener(self.datafile, "a")
1188 1188 ifh = self.opener(self.indexfile, "a")
1189 1189 finally:
1190 1190 if dfh:
1191 1191 dfh.close()
1192 1192 ifh.close()
1193 1193
1194 1194 return node
1195 1195
1196 1196 def strip(self, minlink, transaction):
1197 1197 """truncate the revlog on the first revision with a linkrev >= minlink
1198 1198
1199 1199 This function is called when we're stripping revision minlink and
1200 1200 its descendants from the repository.
1201 1201
1202 1202 We have to remove all revisions with linkrev >= minlink, because
1203 1203 the equivalent changelog revisions will be renumbered after the
1204 1204 strip.
1205 1205
1206 1206 So we truncate the revlog on the first of these revisions, and
1207 1207 trust that the caller has saved the revisions that shouldn't be
1208 1208 removed and that it'll readd them after this truncation.
1209 1209 """
1210 1210 if len(self) == 0:
1211 1211 return
1212 1212
1213 1213 for rev in self:
1214 1214 if self.index[rev][4] >= minlink:
1215 1215 break
1216 1216 else:
1217 1217 return
1218 1218
1219 1219 # first truncate the files on disk
1220 1220 end = self.start(rev)
1221 1221 if not self._inline:
1222 1222 transaction.add(self.datafile, end)
1223 1223 end = rev * self._io.size
1224 1224 else:
1225 1225 end += rev * self._io.size
1226 1226
1227 1227 transaction.add(self.indexfile, end)
1228 1228
1229 1229 # then reset internal state in memory to forget those revisions
1230 1230 self._cache = None
1231 1231 self._chunkclear()
1232 1232 for x in xrange(rev, len(self)):
1233 1233 del self.nodemap[self.node(x)]
1234 1234
1235 1235 del self.index[rev:-1]
1236 1236
1237 1237 def checksize(self):
1238 1238 expected = 0
1239 1239 if len(self):
1240 1240 expected = max(0, self.end(len(self) - 1))
1241 1241
1242 1242 try:
1243 1243 f = self.opener(self.datafile)
1244 1244 f.seek(0, 2)
1245 1245 actual = f.tell()
1246 1246 f.close()
1247 1247 dd = actual - expected
1248 1248 except IOError, inst:
1249 1249 if inst.errno != errno.ENOENT:
1250 1250 raise
1251 1251 dd = 0
1252 1252
1253 1253 try:
1254 1254 f = self.opener(self.indexfile)
1255 1255 f.seek(0, 2)
1256 1256 actual = f.tell()
1257 1257 f.close()
1258 1258 s = self._io.size
1259 1259 i = max(0, actual // s)
1260 1260 di = actual - (i * s)
1261 1261 if self._inline:
1262 1262 databytes = 0
1263 1263 for r in self:
1264 1264 databytes += max(0, self.length(r))
1265 1265 dd = 0
1266 1266 di = actual - len(self) * s - databytes
1267 1267 except IOError, inst:
1268 1268 if inst.errno != errno.ENOENT:
1269 1269 raise
1270 1270 di = 0
1271 1271
1272 1272 return (dd, di)
1273 1273
1274 1274 def files(self):
1275 1275 res = [self.indexfile]
1276 1276 if not self._inline:
1277 1277 res.append(self.datafile)
1278 1278 return res
@@ -1,15 +1,14 b''
1 1 $ "$TESTDIR/hghave" pyflakes || exit 80
2 2 $ cd $(dirname $TESTDIR)
3 3 $ pyflakes mercurial hgext 2>&1 | $TESTDIR/filterpyflakes.py
4 4 mercurial/hgweb/server.py:*: 'activeCount' imported but unused (glob)
5 5 mercurial/commands.py:*: 'base85' imported but unused (glob)
6 6 mercurial/commands.py:*: 'bdiff' imported but unused (glob)
7 7 mercurial/commands.py:*: 'mpatch' imported but unused (glob)
8 8 mercurial/commands.py:*: 'osutil' imported but unused (glob)
9 mercurial/revlog.py:*: 'short' imported but unused (glob)
10 9 hgext/inotify/linux/__init__.py:*: 'from _inotify import *' used; unable to detect undefined names (glob)
11 10 mercurial/util.py:*: 'from posix import *' used; unable to detect undefined names (glob)
12 11 mercurial/windows.py:*: 'from win32 import *' used; unable to detect undefined names (glob)
13 12 mercurial/util.py:*: 'from windows import *' used; unable to detect undefined names (glob)
14 13
15 14
General Comments 0
You need to be logged in to leave comments. Login now