##// END OF EJS Templates
obsolete: rename precursor into predecessor in obsolete docstrings...
Boris Feld -
r33700:0c3112f1 default
parent child Browse files
Show More
@@ -1,1049 +1,1050 b''
1 1 # obsolete.py - obsolete markers handling
2 2 #
3 3 # Copyright 2012 Pierre-Yves David <pierre-yves.david@ens-lyon.org>
4 4 # Logilab SA <contact@logilab.fr>
5 5 #
6 6 # This software may be used and distributed according to the terms of the
7 7 # GNU General Public License version 2 or any later version.
8 8
9 9 """Obsolete marker handling
10 10
11 11 An obsolete marker maps an old changeset to a list of new
12 12 changesets. If the list of new changesets is empty, the old changeset
13 13 is said to be "killed". Otherwise, the old changeset is being
14 14 "replaced" by the new changesets.
15 15
16 16 Obsolete markers can be used to record and distribute changeset graph
17 17 transformations performed by history rewrite operations, and help
18 18 building new tools to reconcile conflicting rewrite actions. To
19 19 facilitate conflict resolution, markers include various annotations
20 20 besides old and news changeset identifiers, such as creation date or
21 21 author name.
22 22
23 The old obsoleted changeset is called a "precursor" and possible
23 The old obsoleted changeset is called a "predecessor" and possible
24 24 replacements are called "successors". Markers that used changeset X as
25 a precursor are called "successor markers of X" because they hold
25 a predecessor are called "successor markers of X" because they hold
26 26 information about the successors of X. Markers that use changeset Y as
27 a successors are call "precursor markers of Y" because they hold
28 information about the precursors of Y.
27 a successors are call "predecessor markers of Y" because they hold
28 information about the predecessors of Y.
29 29
30 30 Examples:
31 31
32 32 - When changeset A is replaced by changeset A', one marker is stored:
33 33
34 34 (A, (A',))
35 35
36 36 - When changesets A and B are folded into a new changeset C, two markers are
37 37 stored:
38 38
39 39 (A, (C,)) and (B, (C,))
40 40
41 41 - When changeset A is simply "pruned" from the graph, a marker is created:
42 42
43 43 (A, ())
44 44
45 45 - When changeset A is split into B and C, a single marker is used:
46 46
47 47 (A, (B, C))
48 48
49 49 We use a single marker to distinguish the "split" case from the "divergence"
50 50 case. If two independent operations rewrite the same changeset A in to A' and
51 51 A'', we have an error case: divergent rewriting. We can detect it because
52 52 two markers will be created independently:
53 53
54 54 (A, (B,)) and (A, (C,))
55 55
56 56 Format
57 57 ------
58 58
59 59 Markers are stored in an append-only file stored in
60 60 '.hg/store/obsstore'.
61 61
62 62 The file starts with a version header:
63 63
64 64 - 1 unsigned byte: version number, starting at zero.
65 65
66 66 The header is followed by the markers. Marker format depend of the version. See
67 67 comment associated with each format for details.
68 68
69 69 """
70 70 from __future__ import absolute_import
71 71
72 72 import errno
73 73 import struct
74 74
75 75 from .i18n import _
76 76 from . import (
77 77 error,
78 78 node,
79 79 obsutil,
80 80 phases,
81 81 policy,
82 82 util,
83 83 )
84 84
85 85 parsers = policy.importmod(r'parsers')
86 86
87 87 _pack = struct.pack
88 88 _unpack = struct.unpack
89 89 _calcsize = struct.calcsize
90 90 propertycache = util.propertycache
91 91
92 92 # the obsolete feature is not mature enough to be enabled by default.
93 93 # you have to rely on third party extension extension to enable this.
94 94 _enabled = False
95 95
96 96 # Options for obsolescence
97 97 createmarkersopt = 'createmarkers'
98 98 allowunstableopt = 'allowunstable'
99 99 exchangeopt = 'exchange'
100 100
101 101 def isenabled(repo, option):
102 102 """Returns True if the given repository has the given obsolete option
103 103 enabled.
104 104 """
105 105 result = set(repo.ui.configlist('experimental', 'evolution'))
106 106 if 'all' in result:
107 107 return True
108 108
109 109 # For migration purposes, temporarily return true if the config hasn't been
110 110 # set but _enabled is true.
111 111 if len(result) == 0 and _enabled:
112 112 return True
113 113
114 114 # createmarkers must be enabled if other options are enabled
115 115 if ((allowunstableopt in result or exchangeopt in result) and
116 116 not createmarkersopt in result):
117 117 raise error.Abort(_("'createmarkers' obsolete option must be enabled "
118 118 "if other obsolete options are enabled"))
119 119
120 120 return option in result
121 121
122 122 ### obsolescence marker flag
123 123
124 124 ## bumpedfix flag
125 125 #
126 126 # When a changeset A' succeed to a changeset A which became public, we call A'
127 127 # "bumped" because it's a successors of a public changesets
128 128 #
129 129 # o A' (bumped)
130 130 # |`:
131 131 # | o A
132 132 # |/
133 133 # o Z
134 134 #
135 135 # The way to solve this situation is to create a new changeset Ad as children
136 136 # of A. This changeset have the same content than A'. So the diff from A to A'
137 137 # is the same than the diff from A to Ad. Ad is marked as a successors of A'
138 138 #
139 139 # o Ad
140 140 # |`:
141 141 # | x A'
142 142 # |'|
143 143 # o | A
144 144 # |/
145 145 # o Z
146 146 #
147 147 # But by transitivity Ad is also a successors of A. To avoid having Ad marked
148 148 # as bumped too, we add the `bumpedfix` flag to the marker. <A', (Ad,)>.
149 149 # This flag mean that the successors express the changes between the public and
150 150 # bumped version and fix the situation, breaking the transitivity of
151 151 # "bumped" here.
152 152 bumpedfix = 1
153 153 usingsha256 = 2
154 154
155 155 ## Parsing and writing of version "0"
156 156 #
157 157 # The header is followed by the markers. Each marker is made of:
158 158 #
159 159 # - 1 uint8 : number of new changesets "N", can be zero.
160 160 #
161 161 # - 1 uint32: metadata size "M" in bytes.
162 162 #
163 163 # - 1 byte: a bit field. It is reserved for flags used in common
164 164 # obsolete marker operations, to avoid repeated decoding of metadata
165 165 # entries.
166 166 #
167 167 # - 20 bytes: obsoleted changeset identifier.
168 168 #
169 169 # - N*20 bytes: new changesets identifiers.
170 170 #
171 171 # - M bytes: metadata as a sequence of nul-terminated strings. Each
172 172 # string contains a key and a value, separated by a colon ':', without
173 173 # additional encoding. Keys cannot contain '\0' or ':' and values
174 174 # cannot contain '\0'.
175 175 _fm0version = 0
176 176 _fm0fixed = '>BIB20s'
177 177 _fm0node = '20s'
178 178 _fm0fsize = _calcsize(_fm0fixed)
179 179 _fm0fnodesize = _calcsize(_fm0node)
180 180
181 181 def _fm0readmarkers(data, off, stop):
182 182 # Loop on markers
183 183 while off < stop:
184 184 # read fixed part
185 185 cur = data[off:off + _fm0fsize]
186 186 off += _fm0fsize
187 187 numsuc, mdsize, flags, pre = _unpack(_fm0fixed, cur)
188 188 # read replacement
189 189 sucs = ()
190 190 if numsuc:
191 191 s = (_fm0fnodesize * numsuc)
192 192 cur = data[off:off + s]
193 193 sucs = _unpack(_fm0node * numsuc, cur)
194 194 off += s
195 195 # read metadata
196 196 # (metadata will be decoded on demand)
197 197 metadata = data[off:off + mdsize]
198 198 if len(metadata) != mdsize:
199 199 raise error.Abort(_('parsing obsolete marker: metadata is too '
200 200 'short, %d bytes expected, got %d')
201 201 % (mdsize, len(metadata)))
202 202 off += mdsize
203 203 metadata = _fm0decodemeta(metadata)
204 204 try:
205 205 when, offset = metadata.pop('date', '0 0').split(' ')
206 206 date = float(when), int(offset)
207 207 except ValueError:
208 208 date = (0., 0)
209 209 parents = None
210 210 if 'p2' in metadata:
211 211 parents = (metadata.pop('p1', None), metadata.pop('p2', None))
212 212 elif 'p1' in metadata:
213 213 parents = (metadata.pop('p1', None),)
214 214 elif 'p0' in metadata:
215 215 parents = ()
216 216 if parents is not None:
217 217 try:
218 218 parents = tuple(node.bin(p) for p in parents)
219 219 # if parent content is not a nodeid, drop the data
220 220 for p in parents:
221 221 if len(p) != 20:
222 222 parents = None
223 223 break
224 224 except TypeError:
225 225 # if content cannot be translated to nodeid drop the data.
226 226 parents = None
227 227
228 228 metadata = tuple(sorted(metadata.iteritems()))
229 229
230 230 yield (pre, sucs, flags, metadata, date, parents)
231 231
232 232 def _fm0encodeonemarker(marker):
233 233 pre, sucs, flags, metadata, date, parents = marker
234 234 if flags & usingsha256:
235 235 raise error.Abort(_('cannot handle sha256 with old obsstore format'))
236 236 metadata = dict(metadata)
237 237 time, tz = date
238 238 metadata['date'] = '%r %i' % (time, tz)
239 239 if parents is not None:
240 240 if not parents:
241 241 # mark that we explicitly recorded no parents
242 242 metadata['p0'] = ''
243 243 for i, p in enumerate(parents, 1):
244 244 metadata['p%i' % i] = node.hex(p)
245 245 metadata = _fm0encodemeta(metadata)
246 246 numsuc = len(sucs)
247 247 format = _fm0fixed + (_fm0node * numsuc)
248 248 data = [numsuc, len(metadata), flags, pre]
249 249 data.extend(sucs)
250 250 return _pack(format, *data) + metadata
251 251
252 252 def _fm0encodemeta(meta):
253 253 """Return encoded metadata string to string mapping.
254 254
255 255 Assume no ':' in key and no '\0' in both key and value."""
256 256 for key, value in meta.iteritems():
257 257 if ':' in key or '\0' in key:
258 258 raise ValueError("':' and '\0' are forbidden in metadata key'")
259 259 if '\0' in value:
260 260 raise ValueError("':' is forbidden in metadata value'")
261 261 return '\0'.join(['%s:%s' % (k, meta[k]) for k in sorted(meta)])
262 262
263 263 def _fm0decodemeta(data):
264 264 """Return string to string dictionary from encoded version."""
265 265 d = {}
266 266 for l in data.split('\0'):
267 267 if l:
268 268 key, value = l.split(':')
269 269 d[key] = value
270 270 return d
271 271
272 272 ## Parsing and writing of version "1"
273 273 #
274 274 # The header is followed by the markers. Each marker is made of:
275 275 #
276 276 # - uint32: total size of the marker (including this field)
277 277 #
278 278 # - float64: date in seconds since epoch
279 279 #
280 280 # - int16: timezone offset in minutes
281 281 #
282 282 # - uint16: a bit field. It is reserved for flags used in common
283 283 # obsolete marker operations, to avoid repeated decoding of metadata
284 284 # entries.
285 285 #
286 286 # - uint8: number of successors "N", can be zero.
287 287 #
288 288 # - uint8: number of parents "P", can be zero.
289 289 #
290 290 # 0: parents data stored but no parent,
291 291 # 1: one parent stored,
292 292 # 2: two parents stored,
293 293 # 3: no parent data stored
294 294 #
295 295 # - uint8: number of metadata entries M
296 296 #
297 # - 20 or 32 bytes: precursor changeset identifier.
297 # - 20 or 32 bytes: predecessor changeset identifier.
298 298 #
299 299 # - N*(20 or 32) bytes: successors changesets identifiers.
300 300 #
301 # - P*(20 or 32) bytes: parents of the precursors changesets.
301 # - P*(20 or 32) bytes: parents of the predecessors changesets.
302 302 #
303 303 # - M*(uint8, uint8): size of all metadata entries (key and value)
304 304 #
305 305 # - remaining bytes: the metadata, each (key, value) pair after the other.
306 306 _fm1version = 1
307 307 _fm1fixed = '>IdhHBBB20s'
308 308 _fm1nodesha1 = '20s'
309 309 _fm1nodesha256 = '32s'
310 310 _fm1nodesha1size = _calcsize(_fm1nodesha1)
311 311 _fm1nodesha256size = _calcsize(_fm1nodesha256)
312 312 _fm1fsize = _calcsize(_fm1fixed)
313 313 _fm1parentnone = 3
314 314 _fm1parentshift = 14
315 315 _fm1parentmask = (_fm1parentnone << _fm1parentshift)
316 316 _fm1metapair = 'BB'
317 317 _fm1metapairsize = _calcsize(_fm1metapair)
318 318
319 319 def _fm1purereadmarkers(data, off, stop):
320 320 # make some global constants local for performance
321 321 noneflag = _fm1parentnone
322 322 sha2flag = usingsha256
323 323 sha1size = _fm1nodesha1size
324 324 sha2size = _fm1nodesha256size
325 325 sha1fmt = _fm1nodesha1
326 326 sha2fmt = _fm1nodesha256
327 327 metasize = _fm1metapairsize
328 328 metafmt = _fm1metapair
329 329 fsize = _fm1fsize
330 330 unpack = _unpack
331 331
332 332 # Loop on markers
333 333 ufixed = struct.Struct(_fm1fixed).unpack
334 334
335 335 while off < stop:
336 336 # read fixed part
337 337 o1 = off + fsize
338 338 t, secs, tz, flags, numsuc, numpar, nummeta, prec = ufixed(data[off:o1])
339 339
340 340 if flags & sha2flag:
341 341 # FIXME: prec was read as a SHA1, needs to be amended
342 342
343 343 # read 0 or more successors
344 344 if numsuc == 1:
345 345 o2 = o1 + sha2size
346 346 sucs = (data[o1:o2],)
347 347 else:
348 348 o2 = o1 + sha2size * numsuc
349 349 sucs = unpack(sha2fmt * numsuc, data[o1:o2])
350 350
351 351 # read parents
352 352 if numpar == noneflag:
353 353 o3 = o2
354 354 parents = None
355 355 elif numpar == 1:
356 356 o3 = o2 + sha2size
357 357 parents = (data[o2:o3],)
358 358 else:
359 359 o3 = o2 + sha2size * numpar
360 360 parents = unpack(sha2fmt * numpar, data[o2:o3])
361 361 else:
362 362 # read 0 or more successors
363 363 if numsuc == 1:
364 364 o2 = o1 + sha1size
365 365 sucs = (data[o1:o2],)
366 366 else:
367 367 o2 = o1 + sha1size * numsuc
368 368 sucs = unpack(sha1fmt * numsuc, data[o1:o2])
369 369
370 370 # read parents
371 371 if numpar == noneflag:
372 372 o3 = o2
373 373 parents = None
374 374 elif numpar == 1:
375 375 o3 = o2 + sha1size
376 376 parents = (data[o2:o3],)
377 377 else:
378 378 o3 = o2 + sha1size * numpar
379 379 parents = unpack(sha1fmt * numpar, data[o2:o3])
380 380
381 381 # read metadata
382 382 off = o3 + metasize * nummeta
383 383 metapairsize = unpack('>' + (metafmt * nummeta), data[o3:off])
384 384 metadata = []
385 385 for idx in xrange(0, len(metapairsize), 2):
386 386 o1 = off + metapairsize[idx]
387 387 o2 = o1 + metapairsize[idx + 1]
388 388 metadata.append((data[off:o1], data[o1:o2]))
389 389 off = o2
390 390
391 391 yield (prec, sucs, flags, tuple(metadata), (secs, tz * 60), parents)
392 392
393 393 def _fm1encodeonemarker(marker):
394 394 pre, sucs, flags, metadata, date, parents = marker
395 395 # determine node size
396 396 _fm1node = _fm1nodesha1
397 397 if flags & usingsha256:
398 398 _fm1node = _fm1nodesha256
399 399 numsuc = len(sucs)
400 400 numextranodes = numsuc
401 401 if parents is None:
402 402 numpar = _fm1parentnone
403 403 else:
404 404 numpar = len(parents)
405 405 numextranodes += numpar
406 406 formatnodes = _fm1node * numextranodes
407 407 formatmeta = _fm1metapair * len(metadata)
408 408 format = _fm1fixed + formatnodes + formatmeta
409 409 # tz is stored in minutes so we divide by 60
410 410 tz = date[1]//60
411 411 data = [None, date[0], tz, flags, numsuc, numpar, len(metadata), pre]
412 412 data.extend(sucs)
413 413 if parents is not None:
414 414 data.extend(parents)
415 415 totalsize = _calcsize(format)
416 416 for key, value in metadata:
417 417 lk = len(key)
418 418 lv = len(value)
419 419 data.append(lk)
420 420 data.append(lv)
421 421 totalsize += lk + lv
422 422 data[0] = totalsize
423 423 data = [_pack(format, *data)]
424 424 for key, value in metadata:
425 425 data.append(key)
426 426 data.append(value)
427 427 return ''.join(data)
428 428
429 429 def _fm1readmarkers(data, off, stop):
430 430 native = getattr(parsers, 'fm1readmarkers', None)
431 431 if not native:
432 432 return _fm1purereadmarkers(data, off, stop)
433 433 return native(data, off, stop)
434 434
435 435 # mapping to read/write various marker formats
436 436 # <version> -> (decoder, encoder)
437 437 formats = {_fm0version: (_fm0readmarkers, _fm0encodeonemarker),
438 438 _fm1version: (_fm1readmarkers, _fm1encodeonemarker)}
439 439
440 440 def _readmarkerversion(data):
441 441 return _unpack('>B', data[0:1])[0]
442 442
443 443 @util.nogc
444 444 def _readmarkers(data, off=None, stop=None):
445 445 """Read and enumerate markers from raw data"""
446 446 diskversion = _readmarkerversion(data)
447 447 if not off:
448 448 off = 1 # skip 1 byte version number
449 449 if stop is None:
450 450 stop = len(data)
451 451 if diskversion not in formats:
452 452 msg = _('parsing obsolete marker: unknown version %r') % diskversion
453 453 raise error.UnknownVersion(msg, version=diskversion)
454 454 return diskversion, formats[diskversion][0](data, off, stop)
455 455
456 456 def encodeheader(version=_fm0version):
457 457 return _pack('>B', version)
458 458
459 459 def encodemarkers(markers, addheader=False, version=_fm0version):
460 460 # Kept separate from flushmarkers(), it will be reused for
461 461 # markers exchange.
462 462 encodeone = formats[version][1]
463 463 if addheader:
464 464 yield encodeheader(version)
465 465 for marker in markers:
466 466 yield encodeone(marker)
467 467
468 468 @util.nogc
469 469 def _addsuccessors(successors, markers):
470 470 for mark in markers:
471 471 successors.setdefault(mark[0], set()).add(mark)
472 472
473 473 def _addprecursors(*args, **kwargs):
474 474 msg = ("'obsolete._addprecursors' is deprecated, "
475 475 "use 'obsolete._addpredecessors'")
476 476 util.nouideprecwarn(msg, '4.4')
477 477
478 478 return _addpredecessors(*args, **kwargs)
479 479
480 480 @util.nogc
481 481 def _addpredecessors(predecessors, markers):
482 482 for mark in markers:
483 483 for suc in mark[1]:
484 484 predecessors.setdefault(suc, set()).add(mark)
485 485
486 486 @util.nogc
487 487 def _addchildren(children, markers):
488 488 for mark in markers:
489 489 parents = mark[5]
490 490 if parents is not None:
491 491 for p in parents:
492 492 children.setdefault(p, set()).add(mark)
493 493
494 494 def _checkinvalidmarkers(markers):
495 495 """search for marker with invalid data and raise error if needed
496 496
497 497 Exist as a separated function to allow the evolve extension for a more
498 498 subtle handling.
499 499 """
500 500 for mark in markers:
501 501 if node.nullid in mark[1]:
502 502 raise error.Abort(_('bad obsolescence marker detected: '
503 503 'invalid successors nullid'))
504 504
505 505 class obsstore(object):
506 506 """Store obsolete markers
507 507
508 508 Markers can be accessed with two mappings:
509 - precursors[x] -> set(markers on precursors edges of x)
509 - predecessors[x] -> set(markers on predecessors edges of x)
510 510 - successors[x] -> set(markers on successors edges of x)
511 - children[x] -> set(markers on precursors edges of children(x)
511 - children[x] -> set(markers on predecessors edges of children(x)
512 512 """
513 513
514 514 fields = ('prec', 'succs', 'flag', 'meta', 'date', 'parents')
515 # prec: nodeid, precursor changesets
515 # prec: nodeid, predecessors changesets
516 516 # succs: tuple of nodeid, successor changesets (0-N length)
517 517 # flag: integer, flag field carrying modifier for the markers (see doc)
518 518 # meta: binary blob, encoded metadata dictionary
519 519 # date: (float, int) tuple, date of marker creation
520 # parents: (tuple of nodeid) or None, parents of precursors
520 # parents: (tuple of nodeid) or None, parents of predecessors
521 521 # None is used when no data has been recorded
522 522
523 523 def __init__(self, svfs, defaultformat=_fm1version, readonly=False):
524 524 # caches for various obsolescence related cache
525 525 self.caches = {}
526 526 self.svfs = svfs
527 527 self._defaultformat = defaultformat
528 528 self._readonly = readonly
529 529
530 530 def __iter__(self):
531 531 return iter(self._all)
532 532
533 533 def __len__(self):
534 534 return len(self._all)
535 535
536 536 def __nonzero__(self):
537 537 if not self._cached('_all'):
538 538 try:
539 539 return self.svfs.stat('obsstore').st_size > 1
540 540 except OSError as inst:
541 541 if inst.errno != errno.ENOENT:
542 542 raise
543 543 # just build an empty _all list if no obsstore exists, which
544 544 # avoids further stat() syscalls
545 545 pass
546 546 return bool(self._all)
547 547
548 548 __bool__ = __nonzero__
549 549
550 550 @property
551 551 def readonly(self):
552 552 """True if marker creation is disabled
553 553
554 554 Remove me in the future when obsolete marker is always on."""
555 555 return self._readonly
556 556
557 557 def create(self, transaction, prec, succs=(), flag=0, parents=None,
558 558 date=None, metadata=None, ui=None):
559 559 """obsolete: add a new obsolete marker
560 560
561 561 * ensuring it is hashable
562 562 * check mandatory metadata
563 563 * encode metadata
564 564
565 565 If you are a human writing code creating marker you want to use the
566 566 `createmarkers` function in this module instead.
567 567
568 568 return True if a new marker have been added, False if the markers
569 569 already existed (no op).
570 570 """
571 571 if metadata is None:
572 572 metadata = {}
573 573 if date is None:
574 574 if 'date' in metadata:
575 575 # as a courtesy for out-of-tree extensions
576 576 date = util.parsedate(metadata.pop('date'))
577 577 elif ui is not None:
578 578 date = ui.configdate('devel', 'default-date')
579 579 if date is None:
580 580 date = util.makedate()
581 581 else:
582 582 date = util.makedate()
583 583 if len(prec) != 20:
584 584 raise ValueError(prec)
585 585 for succ in succs:
586 586 if len(succ) != 20:
587 587 raise ValueError(succ)
588 588 if prec in succs:
589 589 raise ValueError(_('in-marker cycle with %s') % node.hex(prec))
590 590
591 591 metadata = tuple(sorted(metadata.iteritems()))
592 592
593 593 marker = (bytes(prec), tuple(succs), int(flag), metadata, date, parents)
594 594 return bool(self.add(transaction, [marker]))
595 595
596 596 def add(self, transaction, markers):
597 597 """Add new markers to the store
598 598
599 599 Take care of filtering duplicate.
600 600 Return the number of new marker."""
601 601 if self._readonly:
602 602 raise error.Abort(_('creating obsolete markers is not enabled on '
603 603 'this repo'))
604 604 known = set()
605 605 getsuccessors = self.successors.get
606 606 new = []
607 607 for m in markers:
608 608 if m not in getsuccessors(m[0], ()) and m not in known:
609 609 known.add(m)
610 610 new.append(m)
611 611 if new:
612 612 f = self.svfs('obsstore', 'ab')
613 613 try:
614 614 offset = f.tell()
615 615 transaction.add('obsstore', offset)
616 616 # offset == 0: new file - add the version header
617 617 data = b''.join(encodemarkers(new, offset == 0, self._version))
618 618 f.write(data)
619 619 finally:
620 620 # XXX: f.close() == filecache invalidation == obsstore rebuilt.
621 621 # call 'filecacheentry.refresh()' here
622 622 f.close()
623 623 addedmarkers = transaction.changes.get('obsmarkers')
624 624 if addedmarkers is not None:
625 625 addedmarkers.update(new)
626 626 self._addmarkers(new, data)
627 627 # new marker *may* have changed several set. invalidate the cache.
628 628 self.caches.clear()
629 629 # records the number of new markers for the transaction hooks
630 630 previous = int(transaction.hookargs.get('new_obsmarkers', '0'))
631 631 transaction.hookargs['new_obsmarkers'] = str(previous + len(new))
632 632 return len(new)
633 633
634 634 def mergemarkers(self, transaction, data):
635 635 """merge a binary stream of markers inside the obsstore
636 636
637 637 Returns the number of new markers added."""
638 638 version, markers = _readmarkers(data)
639 639 return self.add(transaction, markers)
640 640
641 641 @propertycache
642 642 def _data(self):
643 643 return self.svfs.tryread('obsstore')
644 644
645 645 @propertycache
646 646 def _version(self):
647 647 if len(self._data) >= 1:
648 648 return _readmarkerversion(self._data)
649 649 else:
650 650 return self._defaultformat
651 651
652 652 @propertycache
653 653 def _all(self):
654 654 data = self._data
655 655 if not data:
656 656 return []
657 657 self._version, markers = _readmarkers(data)
658 658 markers = list(markers)
659 659 _checkinvalidmarkers(markers)
660 660 return markers
661 661
662 662 @propertycache
663 663 def successors(self):
664 664 successors = {}
665 665 _addsuccessors(successors, self._all)
666 666 return successors
667 667
668 668 @property
669 669 def precursors(self):
670 670 msg = ("'obsstore.precursors' is deprecated, "
671 671 "use 'obsstore.predecessors'")
672 672 util.nouideprecwarn(msg, '4.4')
673 673
674 674 return self.predecessors
675 675
676 676 @propertycache
677 677 def predecessors(self):
678 678 predecessors = {}
679 679 _addpredecessors(predecessors, self._all)
680 680 return predecessors
681 681
682 682 @propertycache
683 683 def children(self):
684 684 children = {}
685 685 _addchildren(children, self._all)
686 686 return children
687 687
688 688 def _cached(self, attr):
689 689 return attr in self.__dict__
690 690
691 691 def _addmarkers(self, markers, rawdata):
692 692 markers = list(markers) # to allow repeated iteration
693 693 self._data = self._data + rawdata
694 694 self._all.extend(markers)
695 695 if self._cached('successors'):
696 696 _addsuccessors(self.successors, markers)
697 697 if self._cached('predecessors'):
698 698 _addpredecessors(self.predecessors, markers)
699 699 if self._cached('children'):
700 700 _addchildren(self.children, markers)
701 701 _checkinvalidmarkers(markers)
702 702
703 703 def relevantmarkers(self, nodes):
704 704 """return a set of all obsolescence markers relevant to a set of nodes.
705 705
706 706 "relevant" to a set of nodes mean:
707 707
708 708 - marker that use this changeset as successor
709 709 - prune marker of direct children on this changeset
710 - recursive application of the two rules on precursors of these markers
710 - recursive application of the two rules on predecessors of these
711 markers
711 712
712 713 It is a set so you cannot rely on order."""
713 714
714 715 pendingnodes = set(nodes)
715 716 seenmarkers = set()
716 717 seennodes = set(pendingnodes)
717 718 precursorsmarkers = self.predecessors
718 719 succsmarkers = self.successors
719 720 children = self.children
720 721 while pendingnodes:
721 722 direct = set()
722 723 for current in pendingnodes:
723 724 direct.update(precursorsmarkers.get(current, ()))
724 725 pruned = [m for m in children.get(current, ()) if not m[1]]
725 726 direct.update(pruned)
726 727 pruned = [m for m in succsmarkers.get(current, ()) if not m[1]]
727 728 direct.update(pruned)
728 729 direct -= seenmarkers
729 730 pendingnodes = set([m[0] for m in direct])
730 731 seenmarkers |= direct
731 732 pendingnodes -= seennodes
732 733 seennodes |= pendingnodes
733 734 return seenmarkers
734 735
735 736 def makestore(ui, repo):
736 737 """Create an obsstore instance from a repo."""
737 738 # read default format for new obsstore.
738 739 # developer config: format.obsstore-version
739 740 defaultformat = ui.configint('format', 'obsstore-version')
740 741 # rely on obsstore class default when possible.
741 742 kwargs = {}
742 743 if defaultformat is not None:
743 744 kwargs['defaultformat'] = defaultformat
744 745 readonly = not isenabled(repo, createmarkersopt)
745 746 store = obsstore(repo.svfs, readonly=readonly, **kwargs)
746 747 if store and readonly:
747 748 ui.warn(_('obsolete feature not enabled but %i markers found!\n')
748 749 % len(list(store)))
749 750 return store
750 751
751 752 def commonversion(versions):
752 753 """Return the newest version listed in both versions and our local formats.
753 754
754 755 Returns None if no common version exists.
755 756 """
756 757 versions.sort(reverse=True)
757 758 # search for highest version known on both side
758 759 for v in versions:
759 760 if v in formats:
760 761 return v
761 762 return None
762 763
763 764 # arbitrary picked to fit into 8K limit from HTTP server
764 765 # you have to take in account:
765 766 # - the version header
766 767 # - the base85 encoding
767 768 _maxpayload = 5300
768 769
769 770 def _pushkeyescape(markers):
770 771 """encode markers into a dict suitable for pushkey exchange
771 772
772 773 - binary data is base85 encoded
773 774 - split in chunks smaller than 5300 bytes"""
774 775 keys = {}
775 776 parts = []
776 777 currentlen = _maxpayload * 2 # ensure we create a new part
777 778 for marker in markers:
778 779 nextdata = _fm0encodeonemarker(marker)
779 780 if (len(nextdata) + currentlen > _maxpayload):
780 781 currentpart = []
781 782 currentlen = 0
782 783 parts.append(currentpart)
783 784 currentpart.append(nextdata)
784 785 currentlen += len(nextdata)
785 786 for idx, part in enumerate(reversed(parts)):
786 787 data = ''.join([_pack('>B', _fm0version)] + part)
787 788 keys['dump%i' % idx] = util.b85encode(data)
788 789 return keys
789 790
790 791 def listmarkers(repo):
791 792 """List markers over pushkey"""
792 793 if not repo.obsstore:
793 794 return {}
794 795 return _pushkeyescape(sorted(repo.obsstore))
795 796
796 797 def pushmarker(repo, key, old, new):
797 798 """Push markers over pushkey"""
798 799 if not key.startswith('dump'):
799 800 repo.ui.warn(_('unknown key: %r') % key)
800 801 return False
801 802 if old:
802 803 repo.ui.warn(_('unexpected old value for %r') % key)
803 804 return False
804 805 data = util.b85decode(new)
805 806 lock = repo.lock()
806 807 try:
807 808 tr = repo.transaction('pushkey: obsolete markers')
808 809 try:
809 810 repo.obsstore.mergemarkers(tr, data)
810 811 repo.invalidatevolatilesets()
811 812 tr.close()
812 813 return True
813 814 finally:
814 815 tr.release()
815 816 finally:
816 817 lock.release()
817 818
818 819 # keep compatibility for the 4.3 cycle
819 820 def allprecursors(obsstore, nodes, ignoreflags=0):
820 821 movemsg = 'obsolete.allprecursors moved to obsutil.allprecursors'
821 822 util.nouideprecwarn(movemsg, '4.3')
822 823 return obsutil.allprecursors(obsstore, nodes, ignoreflags)
823 824
824 825 def allsuccessors(obsstore, nodes, ignoreflags=0):
825 826 movemsg = 'obsolete.allsuccessors moved to obsutil.allsuccessors'
826 827 util.nouideprecwarn(movemsg, '4.3')
827 828 return obsutil.allsuccessors(obsstore, nodes, ignoreflags)
828 829
829 830 def marker(repo, data):
830 831 movemsg = 'obsolete.marker moved to obsutil.marker'
831 832 repo.ui.deprecwarn(movemsg, '4.3')
832 833 return obsutil.marker(repo, data)
833 834
834 835 def getmarkers(repo, nodes=None, exclusive=False):
835 836 movemsg = 'obsolete.getmarkers moved to obsutil.getmarkers'
836 837 repo.ui.deprecwarn(movemsg, '4.3')
837 838 return obsutil.getmarkers(repo, nodes=nodes, exclusive=exclusive)
838 839
839 840 def exclusivemarkers(repo, nodes):
840 841 movemsg = 'obsolete.exclusivemarkers moved to obsutil.exclusivemarkers'
841 842 repo.ui.deprecwarn(movemsg, '4.3')
842 843 return obsutil.exclusivemarkers(repo, nodes)
843 844
844 845 def foreground(repo, nodes):
845 846 movemsg = 'obsolete.foreground moved to obsutil.foreground'
846 847 repo.ui.deprecwarn(movemsg, '4.3')
847 848 return obsutil.foreground(repo, nodes)
848 849
849 850 def successorssets(repo, initialnode, cache=None):
850 851 movemsg = 'obsolete.successorssets moved to obsutil.successorssets'
851 852 repo.ui.deprecwarn(movemsg, '4.3')
852 853 return obsutil.successorssets(repo, initialnode, cache=cache)
853 854
854 855 # mapping of 'set-name' -> <function to compute this set>
855 856 cachefuncs = {}
856 857 def cachefor(name):
857 858 """Decorator to register a function as computing the cache for a set"""
858 859 def decorator(func):
859 860 if name in cachefuncs:
860 861 msg = "duplicated registration for volatileset '%s' (existing: %r)"
861 862 raise error.ProgrammingError(msg % (name, cachefuncs[name]))
862 863 cachefuncs[name] = func
863 864 return func
864 865 return decorator
865 866
866 867 def getrevs(repo, name):
867 868 """Return the set of revision that belong to the <name> set
868 869
869 870 Such access may compute the set and cache it for future use"""
870 871 repo = repo.unfiltered()
871 872 if not repo.obsstore:
872 873 return frozenset()
873 874 if name not in repo.obsstore.caches:
874 875 repo.obsstore.caches[name] = cachefuncs[name](repo)
875 876 return repo.obsstore.caches[name]
876 877
877 878 # To be simple we need to invalidate obsolescence cache when:
878 879 #
879 880 # - new changeset is added:
880 881 # - public phase is changed
881 882 # - obsolescence marker are added
882 883 # - strip is used a repo
883 884 def clearobscaches(repo):
884 885 """Remove all obsolescence related cache from a repo
885 886
886 887 This remove all cache in obsstore is the obsstore already exist on the
887 888 repo.
888 889
889 890 (We could be smarter here given the exact event that trigger the cache
890 891 clearing)"""
891 892 # only clear cache is there is obsstore data in this repo
892 893 if 'obsstore' in repo._filecache:
893 894 repo.obsstore.caches.clear()
894 895
895 896 def _mutablerevs(repo):
896 897 """the set of mutable revision in the repository"""
897 898 return repo._phasecache.getrevset(repo, (phases.draft, phases.secret))
898 899
899 900 @cachefor('obsolete')
900 901 def _computeobsoleteset(repo):
901 902 """the set of obsolete revisions"""
902 903 getnode = repo.changelog.node
903 904 notpublic = _mutablerevs(repo)
904 905 isobs = repo.obsstore.successors.__contains__
905 906 obs = set(r for r in notpublic if isobs(getnode(r)))
906 907 return obs
907 908
908 909 @cachefor('unstable')
909 910 def _computeunstableset(repo):
910 911 """the set of non obsolete revisions with obsolete parents"""
911 912 pfunc = repo.changelog.parentrevs
912 913 mutable = _mutablerevs(repo)
913 914 obsolete = getrevs(repo, 'obsolete')
914 915 others = mutable - obsolete
915 916 unstable = set()
916 917 for r in sorted(others):
917 918 # A rev is unstable if one of its parent is obsolete or unstable
918 919 # this works since we traverse following growing rev order
919 920 for p in pfunc(r):
920 921 if p in obsolete or p in unstable:
921 922 unstable.add(r)
922 923 break
923 924 return unstable
924 925
925 926 @cachefor('suspended')
926 927 def _computesuspendedset(repo):
927 928 """the set of obsolete parents with non obsolete descendants"""
928 929 suspended = repo.changelog.ancestors(getrevs(repo, 'unstable'))
929 930 return set(r for r in getrevs(repo, 'obsolete') if r in suspended)
930 931
931 932 @cachefor('extinct')
932 933 def _computeextinctset(repo):
933 934 """the set of obsolete parents without non obsolete descendants"""
934 935 return getrevs(repo, 'obsolete') - getrevs(repo, 'suspended')
935 936
936 937
937 938 @cachefor('bumped')
938 939 def _computebumpedset(repo):
939 940 """the set of revs trying to obsolete public revisions"""
940 941 bumped = set()
941 942 # util function (avoid attribute lookup in the loop)
942 943 phase = repo._phasecache.phase # would be faster to grab the full list
943 944 public = phases.public
944 945 cl = repo.changelog
945 946 torev = cl.nodemap.get
946 947 for ctx in repo.set('(not public()) and (not obsolete())'):
947 948 rev = ctx.rev()
948 949 # We only evaluate mutable, non-obsolete revision
949 950 node = ctx.node()
950 # (future) A cache of precursors may worth if split is very common
951 # (future) A cache of predecessors may worth if split is very common
951 952 for pnode in obsutil.allprecursors(repo.obsstore, [node],
952 953 ignoreflags=bumpedfix):
953 954 prev = torev(pnode) # unfiltered! but so is phasecache
954 955 if (prev is not None) and (phase(repo, prev) <= public):
955 # we have a public precursor
956 # we have a public predecessor
956 957 bumped.add(rev)
957 958 break # Next draft!
958 959 return bumped
959 960
960 961 @cachefor('divergent')
961 962 def _computedivergentset(repo):
962 963 """the set of rev that compete to be the final successors of some revision.
963 964 """
964 965 divergent = set()
965 966 obsstore = repo.obsstore
966 967 newermap = {}
967 968 for ctx in repo.set('(not public()) - obsolete()'):
968 969 mark = obsstore.predecessors.get(ctx.node(), ())
969 970 toprocess = set(mark)
970 971 seen = set()
971 972 while toprocess:
972 973 prec = toprocess.pop()[0]
973 974 if prec in seen:
974 975 continue # emergency cycle hanging prevention
975 976 seen.add(prec)
976 977 if prec not in newermap:
977 978 obsutil.successorssets(repo, prec, cache=newermap)
978 979 newer = [n for n in newermap[prec] if n]
979 980 if len(newer) > 1:
980 981 divergent.add(ctx.rev())
981 982 break
982 983 toprocess.update(obsstore.predecessors.get(prec, ()))
983 984 return divergent
984 985
985 986
986 987 def createmarkers(repo, relations, flag=0, date=None, metadata=None,
987 988 operation=None):
988 989 """Add obsolete markers between changesets in a repo
989 990
990 991 <relations> must be an iterable of (<old>, (<new>, ...)[,{metadata}])
991 992 tuple. `old` and `news` are changectx. metadata is an optional dictionary
992 993 containing metadata for this marker only. It is merged with the global
993 994 metadata specified through the `metadata` argument of this function,
994 995
995 996 Trying to obsolete a public changeset will raise an exception.
996 997
997 998 Current user and date are used except if specified otherwise in the
998 999 metadata attribute.
999 1000
1000 1001 This function operates within a transaction of its own, but does
1001 1002 not take any lock on the repo.
1002 1003 """
1003 1004 # prepare metadata
1004 1005 if metadata is None:
1005 1006 metadata = {}
1006 1007 if 'user' not in metadata:
1007 1008 metadata['user'] = repo.ui.username()
1008 1009 useoperation = repo.ui.configbool('experimental',
1009 1010 'evolution.track-operation')
1010 1011 if useoperation and operation:
1011 1012 metadata['operation'] = operation
1012 1013 tr = repo.transaction('add-obsolescence-marker')
1013 1014 try:
1014 1015 markerargs = []
1015 1016 for rel in relations:
1016 1017 prec = rel[0]
1017 1018 sucs = rel[1]
1018 1019 localmetadata = metadata.copy()
1019 1020 if 2 < len(rel):
1020 1021 localmetadata.update(rel[2])
1021 1022
1022 1023 if not prec.mutable():
1023 1024 raise error.Abort(_("cannot obsolete public changeset: %s")
1024 1025 % prec,
1025 1026 hint="see 'hg help phases' for details")
1026 1027 nprec = prec.node()
1027 1028 nsucs = tuple(s.node() for s in sucs)
1028 1029 npare = None
1029 1030 if not nsucs:
1030 1031 npare = tuple(p.node() for p in prec.parents())
1031 1032 if nprec in nsucs:
1032 1033 raise error.Abort(_("changeset %s cannot obsolete itself")
1033 1034 % prec)
1034 1035
1035 1036 # Creating the marker causes the hidden cache to become invalid,
1036 1037 # which causes recomputation when we ask for prec.parents() above.
1037 1038 # Resulting in n^2 behavior. So let's prepare all of the args
1038 1039 # first, then create the markers.
1039 1040 markerargs.append((nprec, nsucs, npare, localmetadata))
1040 1041
1041 1042 for args in markerargs:
1042 1043 nprec, nsucs, npare, localmetadata = args
1043 1044 repo.obsstore.create(tr, nprec, nsucs, flag, parents=npare,
1044 1045 date=date, metadata=localmetadata,
1045 1046 ui=repo.ui)
1046 1047 repo.filteredrevcache.clear()
1047 1048 tr.close()
1048 1049 finally:
1049 1050 tr.release()
General Comments 0
You need to be logged in to leave comments. Login now