##// END OF EJS Templates
copies: prepare changelog for more copies storage mode...
marmoute -
r43296:0b87eb2f default
parent child Browse files
Show More
@@ -1,672 +1,674 b''
1 1 # changelog.py - changelog class 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 from __future__ import absolute_import
9 9
10 10 from .i18n import _
11 11 from .node import (
12 12 bin,
13 13 hex,
14 14 nullid,
15 15 )
16 16 from .thirdparty import (
17 17 attr,
18 18 )
19 19
20 20 from . import (
21 21 encoding,
22 22 error,
23 23 pycompat,
24 24 revlog,
25 25 util,
26 26 )
27 27 from .utils import (
28 28 dateutil,
29 29 stringutil,
30 30 )
31 31
32 32 _defaultextra = {'branch': 'default'}
33 33
34 34 def _string_escape(text):
35 35 """
36 36 >>> from .pycompat import bytechr as chr
37 37 >>> d = {b'nl': chr(10), b'bs': chr(92), b'cr': chr(13), b'nul': chr(0)}
38 38 >>> s = b"ab%(nl)scd%(bs)s%(bs)sn%(nul)s12ab%(cr)scd%(bs)s%(nl)s" % d
39 39 >>> s
40 40 'ab\\ncd\\\\\\\\n\\x0012ab\\rcd\\\\\\n'
41 41 >>> res = _string_escape(s)
42 42 >>> s == _string_unescape(res)
43 43 True
44 44 """
45 45 # subset of the string_escape codec
46 46 text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r')
47 47 return text.replace('\0', '\\0')
48 48
49 49 def _string_unescape(text):
50 50 if '\\0' in text:
51 51 # fix up \0 without getting into trouble with \\0
52 52 text = text.replace('\\\\', '\\\\\n')
53 53 text = text.replace('\\0', '\0')
54 54 text = text.replace('\n', '')
55 55 return stringutil.unescapestr(text)
56 56
57 57 def decodeextra(text):
58 58 """
59 59 >>> from .pycompat import bytechr as chr
60 60 >>> sorted(decodeextra(encodeextra({b'foo': b'bar', b'baz': chr(0) + b'2'})
61 61 ... ).items())
62 62 [('baz', '\\x002'), ('branch', 'default'), ('foo', 'bar')]
63 63 >>> sorted(decodeextra(encodeextra({b'foo': b'bar',
64 64 ... b'baz': chr(92) + chr(0) + b'2'})
65 65 ... ).items())
66 66 [('baz', '\\\\\\x002'), ('branch', 'default'), ('foo', 'bar')]
67 67 """
68 68 extra = _defaultextra.copy()
69 69 for l in text.split('\0'):
70 70 if l:
71 71 k, v = _string_unescape(l).split(':', 1)
72 72 extra[k] = v
73 73 return extra
74 74
75 75 def encodeextra(d):
76 76 # keys must be sorted to produce a deterministic changelog entry
77 77 items = [
78 78 _string_escape('%s:%s' % (k, pycompat.bytestr(d[k])))
79 79 for k in sorted(d)
80 80 ]
81 81 return "\0".join(items)
82 82
83 83 def encodecopies(files, copies):
84 84 items = []
85 85 for i, dst in enumerate(files):
86 86 if dst in copies:
87 87 items.append('%d\0%s' % (i, copies[dst]))
88 88 if len(items) != len(copies):
89 89 raise error.ProgrammingError('some copy targets missing from file list')
90 90 return "\n".join(items)
91 91
92 92 def decodecopies(files, data):
93 93 try:
94 94 copies = {}
95 95 if not data:
96 96 return copies
97 97 for l in data.split('\n'):
98 98 strindex, src = l.split('\0')
99 99 i = int(strindex)
100 100 dst = files[i]
101 101 copies[dst] = src
102 102 return copies
103 103 except (ValueError, IndexError):
104 104 # Perhaps someone had chosen the same key name (e.g. "p1copies") and
105 105 # used different syntax for the value.
106 106 return None
107 107
108 108 def encodefileindices(files, subset):
109 109 subset = set(subset)
110 110 indices = []
111 111 for i, f in enumerate(files):
112 112 if f in subset:
113 113 indices.append('%d' % i)
114 114 return '\n'.join(indices)
115 115
116 116 def decodefileindices(files, data):
117 117 try:
118 118 subset = []
119 119 if not data:
120 120 return subset
121 121 for strindex in data.split('\n'):
122 122 i = int(strindex)
123 123 if i < 0 or i >= len(files):
124 124 return None
125 125 subset.append(files[i])
126 126 return subset
127 127 except (ValueError, IndexError):
128 128 # Perhaps someone had chosen the same key name (e.g. "added") and
129 129 # used different syntax for the value.
130 130 return None
131 131
132 132 def stripdesc(desc):
133 133 """strip trailing whitespace and leading and trailing empty lines"""
134 134 return '\n'.join([l.rstrip() for l in desc.splitlines()]).strip('\n')
135 135
136 136 class appender(object):
137 137 '''the changelog index must be updated last on disk, so we use this class
138 138 to delay writes to it'''
139 139 def __init__(self, vfs, name, mode, buf):
140 140 self.data = buf
141 141 fp = vfs(name, mode)
142 142 self.fp = fp
143 143 self.offset = fp.tell()
144 144 self.size = vfs.fstat(fp).st_size
145 145 self._end = self.size
146 146
147 147 def end(self):
148 148 return self._end
149 149 def tell(self):
150 150 return self.offset
151 151 def flush(self):
152 152 pass
153 153
154 154 @property
155 155 def closed(self):
156 156 return self.fp.closed
157 157
158 158 def close(self):
159 159 self.fp.close()
160 160
161 161 def seek(self, offset, whence=0):
162 162 '''virtual file offset spans real file and data'''
163 163 if whence == 0:
164 164 self.offset = offset
165 165 elif whence == 1:
166 166 self.offset += offset
167 167 elif whence == 2:
168 168 self.offset = self.end() + offset
169 169 if self.offset < self.size:
170 170 self.fp.seek(self.offset)
171 171
172 172 def read(self, count=-1):
173 173 '''only trick here is reads that span real file and data'''
174 174 ret = ""
175 175 if self.offset < self.size:
176 176 s = self.fp.read(count)
177 177 ret = s
178 178 self.offset += len(s)
179 179 if count > 0:
180 180 count -= len(s)
181 181 if count != 0:
182 182 doff = self.offset - self.size
183 183 self.data.insert(0, "".join(self.data))
184 184 del self.data[1:]
185 185 s = self.data[0][doff:doff + count]
186 186 self.offset += len(s)
187 187 ret += s
188 188 return ret
189 189
190 190 def write(self, s):
191 191 self.data.append(bytes(s))
192 192 self.offset += len(s)
193 193 self._end += len(s)
194 194
195 195 def __enter__(self):
196 196 self.fp.__enter__()
197 197 return self
198 198
199 199 def __exit__(self, *args):
200 200 return self.fp.__exit__(*args)
201 201
202 202 def _divertopener(opener, target):
203 203 """build an opener that writes in 'target.a' instead of 'target'"""
204 204 def _divert(name, mode='r', checkambig=False):
205 205 if name != target:
206 206 return opener(name, mode)
207 207 return opener(name + ".a", mode)
208 208 return _divert
209 209
210 210 def _delayopener(opener, target, buf):
211 211 """build an opener that stores chunks in 'buf' instead of 'target'"""
212 212 def _delay(name, mode='r', checkambig=False):
213 213 if name != target:
214 214 return opener(name, mode)
215 215 return appender(opener, name, mode, buf)
216 216 return _delay
217 217
218 218 @attr.s
219 219 class _changelogrevision(object):
220 220 # Extensions might modify _defaultextra, so let the constructor below pass
221 221 # it in
222 222 extra = attr.ib()
223 223 manifest = attr.ib(default=nullid)
224 224 user = attr.ib(default='')
225 225 date = attr.ib(default=(0, 0))
226 226 files = attr.ib(default=attr.Factory(list))
227 227 filesadded = attr.ib(default=None)
228 228 filesremoved = attr.ib(default=None)
229 229 p1copies = attr.ib(default=None)
230 230 p2copies = attr.ib(default=None)
231 231 description = attr.ib(default='')
232 232
233 233 class changelogrevision(object):
234 234 """Holds results of a parsed changelog revision.
235 235
236 236 Changelog revisions consist of multiple pieces of data, including
237 237 the manifest node, user, and date. This object exposes a view into
238 238 the parsed object.
239 239 """
240 240
241 241 __slots__ = (
242 242 r'_offsets',
243 243 r'_text',
244 244 )
245 245
246 246 def __new__(cls, text):
247 247 if not text:
248 248 return _changelogrevision(extra=_defaultextra)
249 249
250 250 self = super(changelogrevision, cls).__new__(cls)
251 251 # We could return here and implement the following as an __init__.
252 252 # But doing it here is equivalent and saves an extra function call.
253 253
254 254 # format used:
255 255 # nodeid\n : manifest node in ascii
256 256 # user\n : user, no \n or \r allowed
257 257 # time tz extra\n : date (time is int or float, timezone is int)
258 258 # : extra is metadata, encoded and separated by '\0'
259 259 # : older versions ignore it
260 260 # files\n\n : files modified by the cset, no \n or \r allowed
261 261 # (.*) : comment (free text, ideally utf-8)
262 262 #
263 263 # changelog v0 doesn't use extra
264 264
265 265 nl1 = text.index('\n')
266 266 nl2 = text.index('\n', nl1 + 1)
267 267 nl3 = text.index('\n', nl2 + 1)
268 268
269 269 # The list of files may be empty. Which means nl3 is the first of the
270 270 # double newline that precedes the description.
271 271 if text[nl3 + 1:nl3 + 2] == '\n':
272 272 doublenl = nl3
273 273 else:
274 274 doublenl = text.index('\n\n', nl3 + 1)
275 275
276 276 self._offsets = (nl1, nl2, nl3, doublenl)
277 277 self._text = text
278 278
279 279 return self
280 280
281 281 @property
282 282 def manifest(self):
283 283 return bin(self._text[0:self._offsets[0]])
284 284
285 285 @property
286 286 def user(self):
287 287 off = self._offsets
288 288 return encoding.tolocal(self._text[off[0] + 1:off[1]])
289 289
290 290 @property
291 291 def _rawdate(self):
292 292 off = self._offsets
293 293 dateextra = self._text[off[1] + 1:off[2]]
294 294 return dateextra.split(' ', 2)[0:2]
295 295
296 296 @property
297 297 def _rawextra(self):
298 298 off = self._offsets
299 299 dateextra = self._text[off[1] + 1:off[2]]
300 300 fields = dateextra.split(' ', 2)
301 301 if len(fields) != 3:
302 302 return None
303 303
304 304 return fields[2]
305 305
306 306 @property
307 307 def date(self):
308 308 raw = self._rawdate
309 309 time = float(raw[0])
310 310 # Various tools did silly things with the timezone.
311 311 try:
312 312 timezone = int(raw[1])
313 313 except ValueError:
314 314 timezone = 0
315 315
316 316 return time, timezone
317 317
318 318 @property
319 319 def extra(self):
320 320 raw = self._rawextra
321 321 if raw is None:
322 322 return _defaultextra
323 323
324 324 return decodeextra(raw)
325 325
326 326 @property
327 327 def files(self):
328 328 off = self._offsets
329 329 if off[2] == off[3]:
330 330 return []
331 331
332 332 return self._text[off[2] + 1:off[3]].split('\n')
333 333
334 334 @property
335 335 def filesadded(self):
336 336 rawindices = self.extra.get('filesadded')
337 337 return rawindices and decodefileindices(self.files, rawindices)
338 338
339 339 @property
340 340 def filesremoved(self):
341 341 rawindices = self.extra.get('filesremoved')
342 342 return rawindices and decodefileindices(self.files, rawindices)
343 343
344 344 @property
345 345 def p1copies(self):
346 346 rawcopies = self.extra.get('p1copies')
347 347 return rawcopies and decodecopies(self.files, rawcopies)
348 348
349 349 @property
350 350 def p2copies(self):
351 351 rawcopies = self.extra.get('p2copies')
352 352 return rawcopies and decodecopies(self.files, rawcopies)
353 353
354 354 @property
355 355 def description(self):
356 356 return encoding.tolocal(self._text[self._offsets[3] + 2:])
357 357
358 358 class changelog(revlog.revlog):
359 359 def __init__(self, opener, trypending=False):
360 360 """Load a changelog revlog using an opener.
361 361
362 362 If ``trypending`` is true, we attempt to load the index from a
363 363 ``00changelog.i.a`` file instead of the default ``00changelog.i``.
364 364 The ``00changelog.i.a`` file contains index (and possibly inline
365 365 revision) data for a transaction that hasn't been finalized yet.
366 366 It exists in a separate file to facilitate readers (such as
367 367 hooks processes) accessing data before a transaction is finalized.
368 368 """
369 369 if trypending and opener.exists('00changelog.i.a'):
370 370 indexfile = '00changelog.i.a'
371 371 else:
372 372 indexfile = '00changelog.i'
373 373
374 374 datafile = '00changelog.d'
375 375 revlog.revlog.__init__(self, opener, indexfile, datafile=datafile,
376 376 checkambig=True, mmaplargeindex=True)
377 377
378 378 if self._initempty and (self.version & 0xFFFF == revlog.REVLOGV1):
379 379 # changelogs don't benefit from generaldelta.
380 380
381 381 self.version &= ~revlog.FLAG_GENERALDELTA
382 382 self._generaldelta = False
383 383
384 384 # Delta chains for changelogs tend to be very small because entries
385 385 # tend to be small and don't delta well with each. So disable delta
386 386 # chains.
387 387 self._storedeltachains = False
388 388
389 389 self._realopener = opener
390 390 self._delayed = False
391 391 self._delaybuf = None
392 392 self._divert = False
393 393 self.filteredrevs = frozenset()
394 self._copiesstorage = opener.options.get('copies-storage')
394 395
395 396 def tiprev(self):
396 397 for i in pycompat.xrange(len(self) -1, -2, -1):
397 398 if i not in self.filteredrevs:
398 399 return i
399 400
400 401 def tip(self):
401 402 """filtered version of revlog.tip"""
402 403 return self.node(self.tiprev())
403 404
404 405 def __contains__(self, rev):
405 406 """filtered version of revlog.__contains__"""
406 407 return (0 <= rev < len(self)
407 408 and rev not in self.filteredrevs)
408 409
409 410 def __iter__(self):
410 411 """filtered version of revlog.__iter__"""
411 412 if len(self.filteredrevs) == 0:
412 413 return revlog.revlog.__iter__(self)
413 414
414 415 def filterediter():
415 416 for i in pycompat.xrange(len(self)):
416 417 if i not in self.filteredrevs:
417 418 yield i
418 419
419 420 return filterediter()
420 421
421 422 def revs(self, start=0, stop=None):
422 423 """filtered version of revlog.revs"""
423 424 for i in super(changelog, self).revs(start, stop):
424 425 if i not in self.filteredrevs:
425 426 yield i
426 427
427 428 def _checknofilteredinrevs(self, revs):
428 429 """raise the appropriate error if 'revs' contains a filtered revision
429 430
430 431 This returns a version of 'revs' to be used thereafter by the caller.
431 432 In particular, if revs is an iterator, it is converted into a set.
432 433 """
433 434 safehasattr = util.safehasattr
434 435 if safehasattr(revs, '__next__'):
435 436 # Note that inspect.isgenerator() is not true for iterators,
436 437 revs = set(revs)
437 438
438 439 filteredrevs = self.filteredrevs
439 440 if safehasattr(revs, 'first'): # smartset
440 441 offenders = revs & filteredrevs
441 442 else:
442 443 offenders = filteredrevs.intersection(revs)
443 444
444 445 for rev in offenders:
445 446 raise error.FilteredIndexError(rev)
446 447 return revs
447 448
448 449 def headrevs(self, revs=None):
449 450 if revs is None and self.filteredrevs:
450 451 try:
451 452 return self.index.headrevsfiltered(self.filteredrevs)
452 453 # AttributeError covers non-c-extension environments and
453 454 # old c extensions without filter handling.
454 455 except AttributeError:
455 456 return self._headrevs()
456 457
457 458 if self.filteredrevs:
458 459 revs = self._checknofilteredinrevs(revs)
459 460 return super(changelog, self).headrevs(revs)
460 461
461 462 def strip(self, *args, **kwargs):
462 463 # XXX make something better than assert
463 464 # We can't expect proper strip behavior if we are filtered.
464 465 assert not self.filteredrevs
465 466 super(changelog, self).strip(*args, **kwargs)
466 467
467 468 def rev(self, node):
468 469 """filtered version of revlog.rev"""
469 470 r = super(changelog, self).rev(node)
470 471 if r in self.filteredrevs:
471 472 raise error.FilteredLookupError(hex(node), self.indexfile,
472 473 _('filtered node'))
473 474 return r
474 475
475 476 def node(self, rev):
476 477 """filtered version of revlog.node"""
477 478 if rev in self.filteredrevs:
478 479 raise error.FilteredIndexError(rev)
479 480 return super(changelog, self).node(rev)
480 481
481 482 def linkrev(self, rev):
482 483 """filtered version of revlog.linkrev"""
483 484 if rev in self.filteredrevs:
484 485 raise error.FilteredIndexError(rev)
485 486 return super(changelog, self).linkrev(rev)
486 487
487 488 def parentrevs(self, rev):
488 489 """filtered version of revlog.parentrevs"""
489 490 if rev in self.filteredrevs:
490 491 raise error.FilteredIndexError(rev)
491 492 return super(changelog, self).parentrevs(rev)
492 493
493 494 def flags(self, rev):
494 495 """filtered version of revlog.flags"""
495 496 if rev in self.filteredrevs:
496 497 raise error.FilteredIndexError(rev)
497 498 return super(changelog, self).flags(rev)
498 499
499 500 def delayupdate(self, tr):
500 501 "delay visibility of index updates to other readers"
501 502
502 503 if not self._delayed:
503 504 if len(self) == 0:
504 505 self._divert = True
505 506 if self._realopener.exists(self.indexfile + '.a'):
506 507 self._realopener.unlink(self.indexfile + '.a')
507 508 self.opener = _divertopener(self._realopener, self.indexfile)
508 509 else:
509 510 self._delaybuf = []
510 511 self.opener = _delayopener(self._realopener, self.indexfile,
511 512 self._delaybuf)
512 513 self._delayed = True
513 514 tr.addpending('cl-%i' % id(self), self._writepending)
514 515 tr.addfinalize('cl-%i' % id(self), self._finalize)
515 516
516 517 def _finalize(self, tr):
517 518 "finalize index updates"
518 519 self._delayed = False
519 520 self.opener = self._realopener
520 521 # move redirected index data back into place
521 522 if self._divert:
522 523 assert not self._delaybuf
523 524 tmpname = self.indexfile + ".a"
524 525 nfile = self.opener.open(tmpname)
525 526 nfile.close()
526 527 self.opener.rename(tmpname, self.indexfile, checkambig=True)
527 528 elif self._delaybuf:
528 529 fp = self.opener(self.indexfile, 'a', checkambig=True)
529 530 fp.write("".join(self._delaybuf))
530 531 fp.close()
531 532 self._delaybuf = None
532 533 self._divert = False
533 534 # split when we're done
534 535 self._enforceinlinesize(tr)
535 536
536 537 def _writepending(self, tr):
537 538 "create a file containing the unfinalized state for pretxnchangegroup"
538 539 if self._delaybuf:
539 540 # make a temporary copy of the index
540 541 fp1 = self._realopener(self.indexfile)
541 542 pendingfilename = self.indexfile + ".a"
542 543 # register as a temp file to ensure cleanup on failure
543 544 tr.registertmp(pendingfilename)
544 545 # write existing data
545 546 fp2 = self._realopener(pendingfilename, "w")
546 547 fp2.write(fp1.read())
547 548 # add pending data
548 549 fp2.write("".join(self._delaybuf))
549 550 fp2.close()
550 551 # switch modes so finalize can simply rename
551 552 self._delaybuf = None
552 553 self._divert = True
553 554 self.opener = _divertopener(self._realopener, self.indexfile)
554 555
555 556 if self._divert:
556 557 return True
557 558
558 559 return False
559 560
560 561 def _enforceinlinesize(self, tr, fp=None):
561 562 if not self._delayed:
562 563 revlog.revlog._enforceinlinesize(self, tr, fp)
563 564
564 565 def read(self, node):
565 566 """Obtain data from a parsed changelog revision.
566 567
567 568 Returns a 6-tuple of:
568 569
569 570 - manifest node in binary
570 571 - author/user as a localstr
571 572 - date as a 2-tuple of (time, timezone)
572 573 - list of files
573 574 - commit message as a localstr
574 575 - dict of extra metadata
575 576
576 577 Unless you need to access all fields, consider calling
577 578 ``changelogrevision`` instead, as it is faster for partial object
578 579 access.
579 580 """
580 581 c = changelogrevision(self.revision(node))
581 582 return (
582 583 c.manifest,
583 584 c.user,
584 585 c.date,
585 586 c.files,
586 587 c.description,
587 588 c.extra
588 589 )
589 590
590 591 def changelogrevision(self, nodeorrev):
591 592 """Obtain a ``changelogrevision`` for a node or revision."""
592 593 return changelogrevision(self.revision(nodeorrev))
593 594
594 595 def readfiles(self, node):
595 596 """
596 597 short version of read that only returns the files modified by the cset
597 598 """
598 599 text = self.revision(node)
599 600 if not text:
600 601 return []
601 602 last = text.index("\n\n")
602 603 l = text[:last].split('\n')
603 604 return l[3:]
604 605
605 606 def add(self, manifest, files, desc, transaction, p1, p2,
606 607 user, date=None, extra=None, p1copies=None, p2copies=None,
607 608 filesadded=None, filesremoved=None):
608 609 # Convert to UTF-8 encoded bytestrings as the very first
609 610 # thing: calling any method on a localstr object will turn it
610 611 # into a str object and the cached UTF-8 string is thus lost.
611 612 user, desc = encoding.fromlocal(user), encoding.fromlocal(desc)
612 613
613 614 user = user.strip()
614 615 # An empty username or a username with a "\n" will make the
615 616 # revision text contain two "\n\n" sequences -> corrupt
616 617 # repository since read cannot unpack the revision.
617 618 if not user:
618 619 raise error.StorageError(_("empty username"))
619 620 if "\n" in user:
620 621 raise error.StorageError(_("username %r contains a newline")
621 622 % pycompat.bytestr(user))
622 623
623 624 desc = stripdesc(desc)
624 625
625 626 if date:
626 627 parseddate = "%d %d" % dateutil.parsedate(date)
627 628 else:
628 629 parseddate = "%d %d" % dateutil.makedate()
629 630 if extra:
630 631 branch = extra.get("branch")
631 632 if branch in ("default", ""):
632 633 del extra["branch"]
633 634 elif branch in (".", "null", "tip"):
634 635 raise error.StorageError(_('the name \'%s\' is reserved')
635 636 % branch)
636 extrasentries = p1copies, p2copies, filesadded, filesremoved
637 if extra is None and any(x is not None for x in extrasentries):
638 extra = {}
639 637 sortedfiles = sorted(files)
640 638 if extra is not None:
641 639 for name in ('p1copies', 'p2copies', 'filesadded', 'filesremoved'):
642 640 extra.pop(name, None)
641 if self._copiesstorage == 'extra':
642 extrasentries = p1copies, p2copies, filesadded, filesremoved
643 if extra is None and any(x is not None for x in extrasentries):
644 extra = {}
643 645 if p1copies is not None:
644 646 extra['p1copies'] = encodecopies(sortedfiles, p1copies)
645 647 if p2copies is not None:
646 648 extra['p2copies'] = encodecopies(sortedfiles, p2copies)
647 649 if filesadded is not None:
648 650 extra['filesadded'] = encodefileindices(sortedfiles, filesadded)
649 651 if filesremoved is not None:
650 652 extra['filesremoved'] = encodefileindices(sortedfiles, filesremoved)
651 653
652 654 if extra:
653 655 extra = encodeextra(extra)
654 656 parseddate = "%s %s" % (parseddate, extra)
655 657 l = [hex(manifest), user, parseddate] + sortedfiles + ["", desc]
656 658 text = "\n".join(l)
657 659 return self.addrevision(text, transaction, len(self), p1, p2)
658 660
659 661 def branchinfo(self, rev):
660 662 """return the branch name and open/close state of a revision
661 663
662 664 This function exists because creating a changectx object
663 665 just to access this is costly."""
664 666 extra = self.read(rev)[5]
665 667 return encoding.tolocal(extra.get("branch")), 'close' in extra
666 668
667 669 def _nodeduplicatecallback(self, transaction, node):
668 670 # keep track of revisions that got "re-added", eg: unbunde of know rev.
669 671 #
670 672 # We track them in a list to preserve their order from the source bundle
671 673 duplicates = transaction.changes.setdefault('revduplicates', [])
672 674 duplicates.append(self.rev(node))
@@ -1,3318 +1,3323 b''
1 1 # localrepo.py - read/write repository class 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 from __future__ import absolute_import
9 9
10 10 import errno
11 11 import hashlib
12 12 import os
13 13 import random
14 14 import sys
15 15 import time
16 16 import weakref
17 17
18 18 from .i18n import _
19 19 from .node import (
20 20 bin,
21 21 hex,
22 22 nullid,
23 23 nullrev,
24 24 short,
25 25 )
26 26 from . import (
27 27 bookmarks,
28 28 branchmap,
29 29 bundle2,
30 30 changegroup,
31 31 color,
32 32 context,
33 33 dirstate,
34 34 dirstateguard,
35 35 discovery,
36 36 encoding,
37 37 error,
38 38 exchange,
39 39 extensions,
40 40 filelog,
41 41 hook,
42 42 lock as lockmod,
43 43 match as matchmod,
44 44 merge as mergemod,
45 45 mergeutil,
46 46 namespaces,
47 47 narrowspec,
48 48 obsolete,
49 49 pathutil,
50 50 phases,
51 51 pushkey,
52 52 pycompat,
53 53 repoview,
54 54 revset,
55 55 revsetlang,
56 56 scmutil,
57 57 sparse,
58 58 store as storemod,
59 59 subrepoutil,
60 60 tags as tagsmod,
61 61 transaction,
62 62 txnutil,
63 63 util,
64 64 vfs as vfsmod,
65 65 )
66 66
67 67 from .interfaces import (
68 68 repository,
69 69 util as interfaceutil,
70 70 )
71 71
72 72 from .utils import (
73 73 procutil,
74 74 stringutil,
75 75 )
76 76
77 77 from .revlogutils import (
78 78 constants as revlogconst,
79 79 )
80 80
81 81 release = lockmod.release
82 82 urlerr = util.urlerr
83 83 urlreq = util.urlreq
84 84
85 85 # set of (path, vfs-location) tuples. vfs-location is:
86 86 # - 'plain for vfs relative paths
87 87 # - '' for svfs relative paths
88 88 _cachedfiles = set()
89 89
90 90 class _basefilecache(scmutil.filecache):
91 91 """All filecache usage on repo are done for logic that should be unfiltered
92 92 """
93 93 def __get__(self, repo, type=None):
94 94 if repo is None:
95 95 return self
96 96 # proxy to unfiltered __dict__ since filtered repo has no entry
97 97 unfi = repo.unfiltered()
98 98 try:
99 99 return unfi.__dict__[self.sname]
100 100 except KeyError:
101 101 pass
102 102 return super(_basefilecache, self).__get__(unfi, type)
103 103
104 104 def set(self, repo, value):
105 105 return super(_basefilecache, self).set(repo.unfiltered(), value)
106 106
107 107 class repofilecache(_basefilecache):
108 108 """filecache for files in .hg but outside of .hg/store"""
109 109 def __init__(self, *paths):
110 110 super(repofilecache, self).__init__(*paths)
111 111 for path in paths:
112 112 _cachedfiles.add((path, 'plain'))
113 113
114 114 def join(self, obj, fname):
115 115 return obj.vfs.join(fname)
116 116
117 117 class storecache(_basefilecache):
118 118 """filecache for files in the store"""
119 119 def __init__(self, *paths):
120 120 super(storecache, self).__init__(*paths)
121 121 for path in paths:
122 122 _cachedfiles.add((path, ''))
123 123
124 124 def join(self, obj, fname):
125 125 return obj.sjoin(fname)
126 126
127 127 class mixedrepostorecache(_basefilecache):
128 128 """filecache for a mix files in .hg/store and outside"""
129 129 def __init__(self, *pathsandlocations):
130 130 # scmutil.filecache only uses the path for passing back into our
131 131 # join(), so we can safely pass a list of paths and locations
132 132 super(mixedrepostorecache, self).__init__(*pathsandlocations)
133 133 _cachedfiles.update(pathsandlocations)
134 134
135 135 def join(self, obj, fnameandlocation):
136 136 fname, location = fnameandlocation
137 137 if location == 'plain':
138 138 return obj.vfs.join(fname)
139 139 else:
140 140 if location != '':
141 141 raise error.ProgrammingError('unexpected location: %s' %
142 142 location)
143 143 return obj.sjoin(fname)
144 144
145 145 def isfilecached(repo, name):
146 146 """check if a repo has already cached "name" filecache-ed property
147 147
148 148 This returns (cachedobj-or-None, iscached) tuple.
149 149 """
150 150 cacheentry = repo.unfiltered()._filecache.get(name, None)
151 151 if not cacheentry:
152 152 return None, False
153 153 return cacheentry.obj, True
154 154
155 155 class unfilteredpropertycache(util.propertycache):
156 156 """propertycache that apply to unfiltered repo only"""
157 157
158 158 def __get__(self, repo, type=None):
159 159 unfi = repo.unfiltered()
160 160 if unfi is repo:
161 161 return super(unfilteredpropertycache, self).__get__(unfi)
162 162 return getattr(unfi, self.name)
163 163
164 164 class filteredpropertycache(util.propertycache):
165 165 """propertycache that must take filtering in account"""
166 166
167 167 def cachevalue(self, obj, value):
168 168 object.__setattr__(obj, self.name, value)
169 169
170 170
171 171 def hasunfilteredcache(repo, name):
172 172 """check if a repo has an unfilteredpropertycache value for <name>"""
173 173 return name in vars(repo.unfiltered())
174 174
175 175 def unfilteredmethod(orig):
176 176 """decorate method that always need to be run on unfiltered version"""
177 177 def wrapper(repo, *args, **kwargs):
178 178 return orig(repo.unfiltered(), *args, **kwargs)
179 179 return wrapper
180 180
181 181 moderncaps = {'lookup', 'branchmap', 'pushkey', 'known', 'getbundle',
182 182 'unbundle'}
183 183 legacycaps = moderncaps.union({'changegroupsubset'})
184 184
185 185 @interfaceutil.implementer(repository.ipeercommandexecutor)
186 186 class localcommandexecutor(object):
187 187 def __init__(self, peer):
188 188 self._peer = peer
189 189 self._sent = False
190 190 self._closed = False
191 191
192 192 def __enter__(self):
193 193 return self
194 194
195 195 def __exit__(self, exctype, excvalue, exctb):
196 196 self.close()
197 197
198 198 def callcommand(self, command, args):
199 199 if self._sent:
200 200 raise error.ProgrammingError('callcommand() cannot be used after '
201 201 'sendcommands()')
202 202
203 203 if self._closed:
204 204 raise error.ProgrammingError('callcommand() cannot be used after '
205 205 'close()')
206 206
207 207 # We don't need to support anything fancy. Just call the named
208 208 # method on the peer and return a resolved future.
209 209 fn = getattr(self._peer, pycompat.sysstr(command))
210 210
211 211 f = pycompat.futures.Future()
212 212
213 213 try:
214 214 result = fn(**pycompat.strkwargs(args))
215 215 except Exception:
216 216 pycompat.future_set_exception_info(f, sys.exc_info()[1:])
217 217 else:
218 218 f.set_result(result)
219 219
220 220 return f
221 221
222 222 def sendcommands(self):
223 223 self._sent = True
224 224
225 225 def close(self):
226 226 self._closed = True
227 227
228 228 @interfaceutil.implementer(repository.ipeercommands)
229 229 class localpeer(repository.peer):
230 230 '''peer for a local repo; reflects only the most recent API'''
231 231
232 232 def __init__(self, repo, caps=None):
233 233 super(localpeer, self).__init__()
234 234
235 235 if caps is None:
236 236 caps = moderncaps.copy()
237 237 self._repo = repo.filtered('served')
238 238 self.ui = repo.ui
239 239 self._caps = repo._restrictcapabilities(caps)
240 240
241 241 # Begin of _basepeer interface.
242 242
243 243 def url(self):
244 244 return self._repo.url()
245 245
246 246 def local(self):
247 247 return self._repo
248 248
249 249 def peer(self):
250 250 return self
251 251
252 252 def canpush(self):
253 253 return True
254 254
255 255 def close(self):
256 256 self._repo.close()
257 257
258 258 # End of _basepeer interface.
259 259
260 260 # Begin of _basewirecommands interface.
261 261
262 262 def branchmap(self):
263 263 return self._repo.branchmap()
264 264
265 265 def capabilities(self):
266 266 return self._caps
267 267
268 268 def clonebundles(self):
269 269 return self._repo.tryread('clonebundles.manifest')
270 270
271 271 def debugwireargs(self, one, two, three=None, four=None, five=None):
272 272 """Used to test argument passing over the wire"""
273 273 return "%s %s %s %s %s" % (one, two, pycompat.bytestr(three),
274 274 pycompat.bytestr(four),
275 275 pycompat.bytestr(five))
276 276
277 277 def getbundle(self, source, heads=None, common=None, bundlecaps=None,
278 278 **kwargs):
279 279 chunks = exchange.getbundlechunks(self._repo, source, heads=heads,
280 280 common=common, bundlecaps=bundlecaps,
281 281 **kwargs)[1]
282 282 cb = util.chunkbuffer(chunks)
283 283
284 284 if exchange.bundle2requested(bundlecaps):
285 285 # When requesting a bundle2, getbundle returns a stream to make the
286 286 # wire level function happier. We need to build a proper object
287 287 # from it in local peer.
288 288 return bundle2.getunbundler(self.ui, cb)
289 289 else:
290 290 return changegroup.getunbundler('01', cb, None)
291 291
292 292 def heads(self):
293 293 return self._repo.heads()
294 294
295 295 def known(self, nodes):
296 296 return self._repo.known(nodes)
297 297
298 298 def listkeys(self, namespace):
299 299 return self._repo.listkeys(namespace)
300 300
301 301 def lookup(self, key):
302 302 return self._repo.lookup(key)
303 303
304 304 def pushkey(self, namespace, key, old, new):
305 305 return self._repo.pushkey(namespace, key, old, new)
306 306
307 307 def stream_out(self):
308 308 raise error.Abort(_('cannot perform stream clone against local '
309 309 'peer'))
310 310
311 311 def unbundle(self, bundle, heads, url):
312 312 """apply a bundle on a repo
313 313
314 314 This function handles the repo locking itself."""
315 315 try:
316 316 try:
317 317 bundle = exchange.readbundle(self.ui, bundle, None)
318 318 ret = exchange.unbundle(self._repo, bundle, heads, 'push', url)
319 319 if util.safehasattr(ret, 'getchunks'):
320 320 # This is a bundle20 object, turn it into an unbundler.
321 321 # This little dance should be dropped eventually when the
322 322 # API is finally improved.
323 323 stream = util.chunkbuffer(ret.getchunks())
324 324 ret = bundle2.getunbundler(self.ui, stream)
325 325 return ret
326 326 except Exception as exc:
327 327 # If the exception contains output salvaged from a bundle2
328 328 # reply, we need to make sure it is printed before continuing
329 329 # to fail. So we build a bundle2 with such output and consume
330 330 # it directly.
331 331 #
332 332 # This is not very elegant but allows a "simple" solution for
333 333 # issue4594
334 334 output = getattr(exc, '_bundle2salvagedoutput', ())
335 335 if output:
336 336 bundler = bundle2.bundle20(self._repo.ui)
337 337 for out in output:
338 338 bundler.addpart(out)
339 339 stream = util.chunkbuffer(bundler.getchunks())
340 340 b = bundle2.getunbundler(self.ui, stream)
341 341 bundle2.processbundle(self._repo, b)
342 342 raise
343 343 except error.PushRaced as exc:
344 344 raise error.ResponseError(_('push failed:'),
345 345 stringutil.forcebytestr(exc))
346 346
347 347 # End of _basewirecommands interface.
348 348
349 349 # Begin of peer interface.
350 350
351 351 def commandexecutor(self):
352 352 return localcommandexecutor(self)
353 353
354 354 # End of peer interface.
355 355
356 356 @interfaceutil.implementer(repository.ipeerlegacycommands)
357 357 class locallegacypeer(localpeer):
358 358 '''peer extension which implements legacy methods too; used for tests with
359 359 restricted capabilities'''
360 360
361 361 def __init__(self, repo):
362 362 super(locallegacypeer, self).__init__(repo, caps=legacycaps)
363 363
364 364 # Begin of baselegacywirecommands interface.
365 365
366 366 def between(self, pairs):
367 367 return self._repo.between(pairs)
368 368
369 369 def branches(self, nodes):
370 370 return self._repo.branches(nodes)
371 371
372 372 def changegroup(self, nodes, source):
373 373 outgoing = discovery.outgoing(self._repo, missingroots=nodes,
374 374 missingheads=self._repo.heads())
375 375 return changegroup.makechangegroup(self._repo, outgoing, '01', source)
376 376
377 377 def changegroupsubset(self, bases, heads, source):
378 378 outgoing = discovery.outgoing(self._repo, missingroots=bases,
379 379 missingheads=heads)
380 380 return changegroup.makechangegroup(self._repo, outgoing, '01', source)
381 381
382 382 # End of baselegacywirecommands interface.
383 383
384 384 # Increment the sub-version when the revlog v2 format changes to lock out old
385 385 # clients.
386 386 REVLOGV2_REQUIREMENT = 'exp-revlogv2.1'
387 387
388 388 # A repository with the sparserevlog feature will have delta chains that
389 389 # can spread over a larger span. Sparse reading cuts these large spans into
390 390 # pieces, so that each piece isn't too big.
391 391 # Without the sparserevlog capability, reading from the repository could use
392 392 # huge amounts of memory, because the whole span would be read at once,
393 393 # including all the intermediate revisions that aren't pertinent for the chain.
394 394 # This is why once a repository has enabled sparse-read, it becomes required.
395 395 SPARSEREVLOG_REQUIREMENT = 'sparserevlog'
396 396
397 397 # Functions receiving (ui, features) that extensions can register to impact
398 398 # the ability to load repositories with custom requirements. Only
399 399 # functions defined in loaded extensions are called.
400 400 #
401 401 # The function receives a set of requirement strings that the repository
402 402 # is capable of opening. Functions will typically add elements to the
403 403 # set to reflect that the extension knows how to handle that requirements.
404 404 featuresetupfuncs = set()
405 405
406 406 def makelocalrepository(baseui, path, intents=None):
407 407 """Create a local repository object.
408 408
409 409 Given arguments needed to construct a local repository, this function
410 410 performs various early repository loading functionality (such as
411 411 reading the ``.hg/requires`` and ``.hg/hgrc`` files), validates that
412 412 the repository can be opened, derives a type suitable for representing
413 413 that repository, and returns an instance of it.
414 414
415 415 The returned object conforms to the ``repository.completelocalrepository``
416 416 interface.
417 417
418 418 The repository type is derived by calling a series of factory functions
419 419 for each aspect/interface of the final repository. These are defined by
420 420 ``REPO_INTERFACES``.
421 421
422 422 Each factory function is called to produce a type implementing a specific
423 423 interface. The cumulative list of returned types will be combined into a
424 424 new type and that type will be instantiated to represent the local
425 425 repository.
426 426
427 427 The factory functions each receive various state that may be consulted
428 428 as part of deriving a type.
429 429
430 430 Extensions should wrap these factory functions to customize repository type
431 431 creation. Note that an extension's wrapped function may be called even if
432 432 that extension is not loaded for the repo being constructed. Extensions
433 433 should check if their ``__name__`` appears in the
434 434 ``extensionmodulenames`` set passed to the factory function and no-op if
435 435 not.
436 436 """
437 437 ui = baseui.copy()
438 438 # Prevent copying repo configuration.
439 439 ui.copy = baseui.copy
440 440
441 441 # Working directory VFS rooted at repository root.
442 442 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
443 443
444 444 # Main VFS for .hg/ directory.
445 445 hgpath = wdirvfs.join(b'.hg')
446 446 hgvfs = vfsmod.vfs(hgpath, cacheaudited=True)
447 447
448 448 # The .hg/ path should exist and should be a directory. All other
449 449 # cases are errors.
450 450 if not hgvfs.isdir():
451 451 try:
452 452 hgvfs.stat()
453 453 except OSError as e:
454 454 if e.errno != errno.ENOENT:
455 455 raise
456 456
457 457 raise error.RepoError(_(b'repository %s not found') % path)
458 458
459 459 # .hg/requires file contains a newline-delimited list of
460 460 # features/capabilities the opener (us) must have in order to use
461 461 # the repository. This file was introduced in Mercurial 0.9.2,
462 462 # which means very old repositories may not have one. We assume
463 463 # a missing file translates to no requirements.
464 464 try:
465 465 requirements = set(hgvfs.read(b'requires').splitlines())
466 466 except IOError as e:
467 467 if e.errno != errno.ENOENT:
468 468 raise
469 469 requirements = set()
470 470
471 471 # The .hg/hgrc file may load extensions or contain config options
472 472 # that influence repository construction. Attempt to load it and
473 473 # process any new extensions that it may have pulled in.
474 474 if loadhgrc(ui, wdirvfs, hgvfs, requirements):
475 475 afterhgrcload(ui, wdirvfs, hgvfs, requirements)
476 476 extensions.loadall(ui)
477 477 extensions.populateui(ui)
478 478
479 479 # Set of module names of extensions loaded for this repository.
480 480 extensionmodulenames = {m.__name__ for n, m in extensions.extensions(ui)}
481 481
482 482 supportedrequirements = gathersupportedrequirements(ui)
483 483
484 484 # We first validate the requirements are known.
485 485 ensurerequirementsrecognized(requirements, supportedrequirements)
486 486
487 487 # Then we validate that the known set is reasonable to use together.
488 488 ensurerequirementscompatible(ui, requirements)
489 489
490 490 # TODO there are unhandled edge cases related to opening repositories with
491 491 # shared storage. If storage is shared, we should also test for requirements
492 492 # compatibility in the pointed-to repo. This entails loading the .hg/hgrc in
493 493 # that repo, as that repo may load extensions needed to open it. This is a
494 494 # bit complicated because we don't want the other hgrc to overwrite settings
495 495 # in this hgrc.
496 496 #
497 497 # This bug is somewhat mitigated by the fact that we copy the .hg/requires
498 498 # file when sharing repos. But if a requirement is added after the share is
499 499 # performed, thereby introducing a new requirement for the opener, we may
500 500 # will not see that and could encounter a run-time error interacting with
501 501 # that shared store since it has an unknown-to-us requirement.
502 502
503 503 # At this point, we know we should be capable of opening the repository.
504 504 # Now get on with doing that.
505 505
506 506 features = set()
507 507
508 508 # The "store" part of the repository holds versioned data. How it is
509 509 # accessed is determined by various requirements. The ``shared`` or
510 510 # ``relshared`` requirements indicate the store lives in the path contained
511 511 # in the ``.hg/sharedpath`` file. This is an absolute path for
512 512 # ``shared`` and relative to ``.hg/`` for ``relshared``.
513 513 if b'shared' in requirements or b'relshared' in requirements:
514 514 sharedpath = hgvfs.read(b'sharedpath').rstrip(b'\n')
515 515 if b'relshared' in requirements:
516 516 sharedpath = hgvfs.join(sharedpath)
517 517
518 518 sharedvfs = vfsmod.vfs(sharedpath, realpath=True)
519 519
520 520 if not sharedvfs.exists():
521 521 raise error.RepoError(_(b'.hg/sharedpath points to nonexistent '
522 522 b'directory %s') % sharedvfs.base)
523 523
524 524 features.add(repository.REPO_FEATURE_SHARED_STORAGE)
525 525
526 526 storebasepath = sharedvfs.base
527 527 cachepath = sharedvfs.join(b'cache')
528 528 else:
529 529 storebasepath = hgvfs.base
530 530 cachepath = hgvfs.join(b'cache')
531 531 wcachepath = hgvfs.join(b'wcache')
532 532
533 533
534 534 # The store has changed over time and the exact layout is dictated by
535 535 # requirements. The store interface abstracts differences across all
536 536 # of them.
537 537 store = makestore(requirements, storebasepath,
538 538 lambda base: vfsmod.vfs(base, cacheaudited=True))
539 539 hgvfs.createmode = store.createmode
540 540
541 541 storevfs = store.vfs
542 542 storevfs.options = resolvestorevfsoptions(ui, requirements, features)
543 543
544 544 # The cache vfs is used to manage cache files.
545 545 cachevfs = vfsmod.vfs(cachepath, cacheaudited=True)
546 546 cachevfs.createmode = store.createmode
547 547 # The cache vfs is used to manage cache files related to the working copy
548 548 wcachevfs = vfsmod.vfs(wcachepath, cacheaudited=True)
549 549 wcachevfs.createmode = store.createmode
550 550
551 551 # Now resolve the type for the repository object. We do this by repeatedly
552 552 # calling a factory function to produces types for specific aspects of the
553 553 # repo's operation. The aggregate returned types are used as base classes
554 554 # for a dynamically-derived type, which will represent our new repository.
555 555
556 556 bases = []
557 557 extrastate = {}
558 558
559 559 for iface, fn in REPO_INTERFACES:
560 560 # We pass all potentially useful state to give extensions tons of
561 561 # flexibility.
562 562 typ = fn()(ui=ui,
563 563 intents=intents,
564 564 requirements=requirements,
565 565 features=features,
566 566 wdirvfs=wdirvfs,
567 567 hgvfs=hgvfs,
568 568 store=store,
569 569 storevfs=storevfs,
570 570 storeoptions=storevfs.options,
571 571 cachevfs=cachevfs,
572 572 wcachevfs=wcachevfs,
573 573 extensionmodulenames=extensionmodulenames,
574 574 extrastate=extrastate,
575 575 baseclasses=bases)
576 576
577 577 if not isinstance(typ, type):
578 578 raise error.ProgrammingError('unable to construct type for %s' %
579 579 iface)
580 580
581 581 bases.append(typ)
582 582
583 583 # type() allows you to use characters in type names that wouldn't be
584 584 # recognized as Python symbols in source code. We abuse that to add
585 585 # rich information about our constructed repo.
586 586 name = pycompat.sysstr(b'derivedrepo:%s<%s>' % (
587 587 wdirvfs.base,
588 588 b','.join(sorted(requirements))))
589 589
590 590 cls = type(name, tuple(bases), {})
591 591
592 592 return cls(
593 593 baseui=baseui,
594 594 ui=ui,
595 595 origroot=path,
596 596 wdirvfs=wdirvfs,
597 597 hgvfs=hgvfs,
598 598 requirements=requirements,
599 599 supportedrequirements=supportedrequirements,
600 600 sharedpath=storebasepath,
601 601 store=store,
602 602 cachevfs=cachevfs,
603 603 wcachevfs=wcachevfs,
604 604 features=features,
605 605 intents=intents)
606 606
607 607 def loadhgrc(ui, wdirvfs, hgvfs, requirements):
608 608 """Load hgrc files/content into a ui instance.
609 609
610 610 This is called during repository opening to load any additional
611 611 config files or settings relevant to the current repository.
612 612
613 613 Returns a bool indicating whether any additional configs were loaded.
614 614
615 615 Extensions should monkeypatch this function to modify how per-repo
616 616 configs are loaded. For example, an extension may wish to pull in
617 617 configs from alternate files or sources.
618 618 """
619 619 try:
620 620 ui.readconfig(hgvfs.join(b'hgrc'), root=wdirvfs.base)
621 621 return True
622 622 except IOError:
623 623 return False
624 624
625 625 def afterhgrcload(ui, wdirvfs, hgvfs, requirements):
626 626 """Perform additional actions after .hg/hgrc is loaded.
627 627
628 628 This function is called during repository loading immediately after
629 629 the .hg/hgrc file is loaded and before per-repo extensions are loaded.
630 630
631 631 The function can be used to validate configs, automatically add
632 632 options (including extensions) based on requirements, etc.
633 633 """
634 634
635 635 # Map of requirements to list of extensions to load automatically when
636 636 # requirement is present.
637 637 autoextensions = {
638 638 b'largefiles': [b'largefiles'],
639 639 b'lfs': [b'lfs'],
640 640 }
641 641
642 642 for requirement, names in sorted(autoextensions.items()):
643 643 if requirement not in requirements:
644 644 continue
645 645
646 646 for name in names:
647 647 if not ui.hasconfig(b'extensions', name):
648 648 ui.setconfig(b'extensions', name, b'', source='autoload')
649 649
650 650 def gathersupportedrequirements(ui):
651 651 """Determine the complete set of recognized requirements."""
652 652 # Start with all requirements supported by this file.
653 653 supported = set(localrepository._basesupported)
654 654
655 655 # Execute ``featuresetupfuncs`` entries if they belong to an extension
656 656 # relevant to this ui instance.
657 657 modules = {m.__name__ for n, m in extensions.extensions(ui)}
658 658
659 659 for fn in featuresetupfuncs:
660 660 if fn.__module__ in modules:
661 661 fn(ui, supported)
662 662
663 663 # Add derived requirements from registered compression engines.
664 664 for name in util.compengines:
665 665 engine = util.compengines[name]
666 666 if engine.available() and engine.revlogheader():
667 667 supported.add(b'exp-compression-%s' % name)
668 668 if engine.name() == 'zstd':
669 669 supported.add(b'revlog-compression-zstd')
670 670
671 671 return supported
672 672
673 673 def ensurerequirementsrecognized(requirements, supported):
674 674 """Validate that a set of local requirements is recognized.
675 675
676 676 Receives a set of requirements. Raises an ``error.RepoError`` if there
677 677 exists any requirement in that set that currently loaded code doesn't
678 678 recognize.
679 679
680 680 Returns a set of supported requirements.
681 681 """
682 682 missing = set()
683 683
684 684 for requirement in requirements:
685 685 if requirement in supported:
686 686 continue
687 687
688 688 if not requirement or not requirement[0:1].isalnum():
689 689 raise error.RequirementError(_(b'.hg/requires file is corrupt'))
690 690
691 691 missing.add(requirement)
692 692
693 693 if missing:
694 694 raise error.RequirementError(
695 695 _(b'repository requires features unknown to this Mercurial: %s') %
696 696 b' '.join(sorted(missing)),
697 697 hint=_(b'see https://mercurial-scm.org/wiki/MissingRequirement '
698 698 b'for more information'))
699 699
700 700 def ensurerequirementscompatible(ui, requirements):
701 701 """Validates that a set of recognized requirements is mutually compatible.
702 702
703 703 Some requirements may not be compatible with others or require
704 704 config options that aren't enabled. This function is called during
705 705 repository opening to ensure that the set of requirements needed
706 706 to open a repository is sane and compatible with config options.
707 707
708 708 Extensions can monkeypatch this function to perform additional
709 709 checking.
710 710
711 711 ``error.RepoError`` should be raised on failure.
712 712 """
713 713 if b'exp-sparse' in requirements and not sparse.enabled:
714 714 raise error.RepoError(_(b'repository is using sparse feature but '
715 715 b'sparse is not enabled; enable the '
716 716 b'"sparse" extensions to access'))
717 717
718 718 def makestore(requirements, path, vfstype):
719 719 """Construct a storage object for a repository."""
720 720 if b'store' in requirements:
721 721 if b'fncache' in requirements:
722 722 return storemod.fncachestore(path, vfstype,
723 723 b'dotencode' in requirements)
724 724
725 725 return storemod.encodedstore(path, vfstype)
726 726
727 727 return storemod.basicstore(path, vfstype)
728 728
729 729 def resolvestorevfsoptions(ui, requirements, features):
730 730 """Resolve the options to pass to the store vfs opener.
731 731
732 732 The returned dict is used to influence behavior of the storage layer.
733 733 """
734 734 options = {}
735 735
736 736 if b'treemanifest' in requirements:
737 737 options[b'treemanifest'] = True
738 738
739 739 # experimental config: format.manifestcachesize
740 740 manifestcachesize = ui.configint(b'format', b'manifestcachesize')
741 741 if manifestcachesize is not None:
742 742 options[b'manifestcachesize'] = manifestcachesize
743 743
744 744 # In the absence of another requirement superseding a revlog-related
745 745 # requirement, we have to assume the repo is using revlog version 0.
746 746 # This revlog format is super old and we don't bother trying to parse
747 747 # opener options for it because those options wouldn't do anything
748 748 # meaningful on such old repos.
749 749 if b'revlogv1' in requirements or REVLOGV2_REQUIREMENT in requirements:
750 750 options.update(resolverevlogstorevfsoptions(ui, requirements, features))
751 751 else: # explicitly mark repo as using revlogv0
752 752 options['revlogv0'] = True
753 753
754 writecopiesto = ui.config('experimental', 'copies.write-to')
755 copiesextramode = ('changeset-only', 'compatibility')
756 if (writecopiesto in copiesextramode):
757 options['copies-storage'] = 'extra'
758
754 759 return options
755 760
756 761 def resolverevlogstorevfsoptions(ui, requirements, features):
757 762 """Resolve opener options specific to revlogs."""
758 763
759 764 options = {}
760 765 options[b'flagprocessors'] = {}
761 766
762 767 if b'revlogv1' in requirements:
763 768 options[b'revlogv1'] = True
764 769 if REVLOGV2_REQUIREMENT in requirements:
765 770 options[b'revlogv2'] = True
766 771
767 772 if b'generaldelta' in requirements:
768 773 options[b'generaldelta'] = True
769 774
770 775 # experimental config: format.chunkcachesize
771 776 chunkcachesize = ui.configint(b'format', b'chunkcachesize')
772 777 if chunkcachesize is not None:
773 778 options[b'chunkcachesize'] = chunkcachesize
774 779
775 780 deltabothparents = ui.configbool(b'storage',
776 781 b'revlog.optimize-delta-parent-choice')
777 782 options[b'deltabothparents'] = deltabothparents
778 783
779 784 lazydelta = ui.configbool(b'storage', b'revlog.reuse-external-delta')
780 785 lazydeltabase = False
781 786 if lazydelta:
782 787 lazydeltabase = ui.configbool(b'storage',
783 788 b'revlog.reuse-external-delta-parent')
784 789 if lazydeltabase is None:
785 790 lazydeltabase = not scmutil.gddeltaconfig(ui)
786 791 options[b'lazydelta'] = lazydelta
787 792 options[b'lazydeltabase'] = lazydeltabase
788 793
789 794 chainspan = ui.configbytes(b'experimental', b'maxdeltachainspan')
790 795 if 0 <= chainspan:
791 796 options[b'maxdeltachainspan'] = chainspan
792 797
793 798 mmapindexthreshold = ui.configbytes(b'experimental',
794 799 b'mmapindexthreshold')
795 800 if mmapindexthreshold is not None:
796 801 options[b'mmapindexthreshold'] = mmapindexthreshold
797 802
798 803 withsparseread = ui.configbool(b'experimental', b'sparse-read')
799 804 srdensitythres = float(ui.config(b'experimental',
800 805 b'sparse-read.density-threshold'))
801 806 srmingapsize = ui.configbytes(b'experimental',
802 807 b'sparse-read.min-gap-size')
803 808 options[b'with-sparse-read'] = withsparseread
804 809 options[b'sparse-read-density-threshold'] = srdensitythres
805 810 options[b'sparse-read-min-gap-size'] = srmingapsize
806 811
807 812 sparserevlog = SPARSEREVLOG_REQUIREMENT in requirements
808 813 options[b'sparse-revlog'] = sparserevlog
809 814 if sparserevlog:
810 815 options[b'generaldelta'] = True
811 816
812 817 maxchainlen = None
813 818 if sparserevlog:
814 819 maxchainlen = revlogconst.SPARSE_REVLOG_MAX_CHAIN_LENGTH
815 820 # experimental config: format.maxchainlen
816 821 maxchainlen = ui.configint(b'format', b'maxchainlen', maxchainlen)
817 822 if maxchainlen is not None:
818 823 options[b'maxchainlen'] = maxchainlen
819 824
820 825 for r in requirements:
821 826 # we allow multiple compression engine requirement to co-exist because
822 827 # strickly speaking, revlog seems to support mixed compression style.
823 828 #
824 829 # The compression used for new entries will be "the last one"
825 830 prefix = r.startswith
826 831 if prefix('revlog-compression-') or prefix('exp-compression-'):
827 832 options[b'compengine'] = r.split('-', 2)[2]
828 833
829 834 options[b'zlib.level'] = ui.configint(b'storage', b'revlog.zlib.level')
830 835 if options[b'zlib.level'] is not None:
831 836 if not (0 <= options[b'zlib.level'] <= 9):
832 837 msg = _('invalid value for `storage.revlog.zlib.level` config: %d')
833 838 raise error.Abort(msg % options[b'zlib.level'])
834 839 options[b'zstd.level'] = ui.configint(b'storage', b'revlog.zstd.level')
835 840 if options[b'zstd.level'] is not None:
836 841 if not (0 <= options[b'zstd.level'] <= 22):
837 842 msg = _('invalid value for `storage.revlog.zstd.level` config: %d')
838 843 raise error.Abort(msg % options[b'zstd.level'])
839 844
840 845 if repository.NARROW_REQUIREMENT in requirements:
841 846 options[b'enableellipsis'] = True
842 847
843 848 return options
844 849
845 850 def makemain(**kwargs):
846 851 """Produce a type conforming to ``ilocalrepositorymain``."""
847 852 return localrepository
848 853
849 854 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
850 855 class revlogfilestorage(object):
851 856 """File storage when using revlogs."""
852 857
853 858 def file(self, path):
854 859 if path[0] == b'/':
855 860 path = path[1:]
856 861
857 862 return filelog.filelog(self.svfs, path)
858 863
859 864 @interfaceutil.implementer(repository.ilocalrepositoryfilestorage)
860 865 class revlognarrowfilestorage(object):
861 866 """File storage when using revlogs and narrow files."""
862 867
863 868 def file(self, path):
864 869 if path[0] == b'/':
865 870 path = path[1:]
866 871
867 872 return filelog.narrowfilelog(self.svfs, path, self._storenarrowmatch)
868 873
869 874 def makefilestorage(requirements, features, **kwargs):
870 875 """Produce a type conforming to ``ilocalrepositoryfilestorage``."""
871 876 features.add(repository.REPO_FEATURE_REVLOG_FILE_STORAGE)
872 877 features.add(repository.REPO_FEATURE_STREAM_CLONE)
873 878
874 879 if repository.NARROW_REQUIREMENT in requirements:
875 880 return revlognarrowfilestorage
876 881 else:
877 882 return revlogfilestorage
878 883
879 884 # List of repository interfaces and factory functions for them. Each
880 885 # will be called in order during ``makelocalrepository()`` to iteratively
881 886 # derive the final type for a local repository instance. We capture the
882 887 # function as a lambda so we don't hold a reference and the module-level
883 888 # functions can be wrapped.
884 889 REPO_INTERFACES = [
885 890 (repository.ilocalrepositorymain, lambda: makemain),
886 891 (repository.ilocalrepositoryfilestorage, lambda: makefilestorage),
887 892 ]
888 893
889 894 @interfaceutil.implementer(repository.ilocalrepositorymain)
890 895 class localrepository(object):
891 896 """Main class for representing local repositories.
892 897
893 898 All local repositories are instances of this class.
894 899
895 900 Constructed on its own, instances of this class are not usable as
896 901 repository objects. To obtain a usable repository object, call
897 902 ``hg.repository()``, ``localrepo.instance()``, or
898 903 ``localrepo.makelocalrepository()``. The latter is the lowest-level.
899 904 ``instance()`` adds support for creating new repositories.
900 905 ``hg.repository()`` adds more extension integration, including calling
901 906 ``reposetup()``. Generally speaking, ``hg.repository()`` should be
902 907 used.
903 908 """
904 909
905 910 # obsolete experimental requirements:
906 911 # - manifestv2: An experimental new manifest format that allowed
907 912 # for stem compression of long paths. Experiment ended up not
908 913 # being successful (repository sizes went up due to worse delta
909 914 # chains), and the code was deleted in 4.6.
910 915 supportedformats = {
911 916 'revlogv1',
912 917 'generaldelta',
913 918 'treemanifest',
914 919 REVLOGV2_REQUIREMENT,
915 920 SPARSEREVLOG_REQUIREMENT,
916 921 bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT,
917 922 }
918 923 _basesupported = supportedformats | {
919 924 'store',
920 925 'fncache',
921 926 'shared',
922 927 'relshared',
923 928 'dotencode',
924 929 'exp-sparse',
925 930 'internal-phase'
926 931 }
927 932
928 933 # list of prefix for file which can be written without 'wlock'
929 934 # Extensions should extend this list when needed
930 935 _wlockfreeprefix = {
931 936 # We migh consider requiring 'wlock' for the next
932 937 # two, but pretty much all the existing code assume
933 938 # wlock is not needed so we keep them excluded for
934 939 # now.
935 940 'hgrc',
936 941 'requires',
937 942 # XXX cache is a complicatged business someone
938 943 # should investigate this in depth at some point
939 944 'cache/',
940 945 # XXX shouldn't be dirstate covered by the wlock?
941 946 'dirstate',
942 947 # XXX bisect was still a bit too messy at the time
943 948 # this changeset was introduced. Someone should fix
944 949 # the remainig bit and drop this line
945 950 'bisect.state',
946 951 }
947 952
948 953 def __init__(self, baseui, ui, origroot, wdirvfs, hgvfs, requirements,
949 954 supportedrequirements, sharedpath, store, cachevfs, wcachevfs,
950 955 features, intents=None):
951 956 """Create a new local repository instance.
952 957
953 958 Most callers should use ``hg.repository()``, ``localrepo.instance()``,
954 959 or ``localrepo.makelocalrepository()`` for obtaining a new repository
955 960 object.
956 961
957 962 Arguments:
958 963
959 964 baseui
960 965 ``ui.ui`` instance that ``ui`` argument was based off of.
961 966
962 967 ui
963 968 ``ui.ui`` instance for use by the repository.
964 969
965 970 origroot
966 971 ``bytes`` path to working directory root of this repository.
967 972
968 973 wdirvfs
969 974 ``vfs.vfs`` rooted at the working directory.
970 975
971 976 hgvfs
972 977 ``vfs.vfs`` rooted at .hg/
973 978
974 979 requirements
975 980 ``set`` of bytestrings representing repository opening requirements.
976 981
977 982 supportedrequirements
978 983 ``set`` of bytestrings representing repository requirements that we
979 984 know how to open. May be a supetset of ``requirements``.
980 985
981 986 sharedpath
982 987 ``bytes`` Defining path to storage base directory. Points to a
983 988 ``.hg/`` directory somewhere.
984 989
985 990 store
986 991 ``store.basicstore`` (or derived) instance providing access to
987 992 versioned storage.
988 993
989 994 cachevfs
990 995 ``vfs.vfs`` used for cache files.
991 996
992 997 wcachevfs
993 998 ``vfs.vfs`` used for cache files related to the working copy.
994 999
995 1000 features
996 1001 ``set`` of bytestrings defining features/capabilities of this
997 1002 instance.
998 1003
999 1004 intents
1000 1005 ``set`` of system strings indicating what this repo will be used
1001 1006 for.
1002 1007 """
1003 1008 self.baseui = baseui
1004 1009 self.ui = ui
1005 1010 self.origroot = origroot
1006 1011 # vfs rooted at working directory.
1007 1012 self.wvfs = wdirvfs
1008 1013 self.root = wdirvfs.base
1009 1014 # vfs rooted at .hg/. Used to access most non-store paths.
1010 1015 self.vfs = hgvfs
1011 1016 self.path = hgvfs.base
1012 1017 self.requirements = requirements
1013 1018 self.supported = supportedrequirements
1014 1019 self.sharedpath = sharedpath
1015 1020 self.store = store
1016 1021 self.cachevfs = cachevfs
1017 1022 self.wcachevfs = wcachevfs
1018 1023 self.features = features
1019 1024
1020 1025 self.filtername = None
1021 1026
1022 1027 if (self.ui.configbool('devel', 'all-warnings') or
1023 1028 self.ui.configbool('devel', 'check-locks')):
1024 1029 self.vfs.audit = self._getvfsward(self.vfs.audit)
1025 1030 # A list of callback to shape the phase if no data were found.
1026 1031 # Callback are in the form: func(repo, roots) --> processed root.
1027 1032 # This list it to be filled by extension during repo setup
1028 1033 self._phasedefaults = []
1029 1034
1030 1035 color.setup(self.ui)
1031 1036
1032 1037 self.spath = self.store.path
1033 1038 self.svfs = self.store.vfs
1034 1039 self.sjoin = self.store.join
1035 1040 if (self.ui.configbool('devel', 'all-warnings') or
1036 1041 self.ui.configbool('devel', 'check-locks')):
1037 1042 if util.safehasattr(self.svfs, 'vfs'): # this is filtervfs
1038 1043 self.svfs.vfs.audit = self._getsvfsward(self.svfs.vfs.audit)
1039 1044 else: # standard vfs
1040 1045 self.svfs.audit = self._getsvfsward(self.svfs.audit)
1041 1046
1042 1047 self._dirstatevalidatewarned = False
1043 1048
1044 1049 self._branchcaches = branchmap.BranchMapCache()
1045 1050 self._revbranchcache = None
1046 1051 self._filterpats = {}
1047 1052 self._datafilters = {}
1048 1053 self._transref = self._lockref = self._wlockref = None
1049 1054
1050 1055 # A cache for various files under .hg/ that tracks file changes,
1051 1056 # (used by the filecache decorator)
1052 1057 #
1053 1058 # Maps a property name to its util.filecacheentry
1054 1059 self._filecache = {}
1055 1060
1056 1061 # hold sets of revision to be filtered
1057 1062 # should be cleared when something might have changed the filter value:
1058 1063 # - new changesets,
1059 1064 # - phase change,
1060 1065 # - new obsolescence marker,
1061 1066 # - working directory parent change,
1062 1067 # - bookmark changes
1063 1068 self.filteredrevcache = {}
1064 1069
1065 1070 # post-dirstate-status hooks
1066 1071 self._postdsstatus = []
1067 1072
1068 1073 # generic mapping between names and nodes
1069 1074 self.names = namespaces.namespaces()
1070 1075
1071 1076 # Key to signature value.
1072 1077 self._sparsesignaturecache = {}
1073 1078 # Signature to cached matcher instance.
1074 1079 self._sparsematchercache = {}
1075 1080
1076 1081 self._extrafilterid = repoview.extrafilter(ui)
1077 1082
1078 1083 def _getvfsward(self, origfunc):
1079 1084 """build a ward for self.vfs"""
1080 1085 rref = weakref.ref(self)
1081 1086 def checkvfs(path, mode=None):
1082 1087 ret = origfunc(path, mode=mode)
1083 1088 repo = rref()
1084 1089 if (repo is None
1085 1090 or not util.safehasattr(repo, '_wlockref')
1086 1091 or not util.safehasattr(repo, '_lockref')):
1087 1092 return
1088 1093 if mode in (None, 'r', 'rb'):
1089 1094 return
1090 1095 if path.startswith(repo.path):
1091 1096 # truncate name relative to the repository (.hg)
1092 1097 path = path[len(repo.path) + 1:]
1093 1098 if path.startswith('cache/'):
1094 1099 msg = 'accessing cache with vfs instead of cachevfs: "%s"'
1095 1100 repo.ui.develwarn(msg % path, stacklevel=3, config="cache-vfs")
1096 1101 if path.startswith('journal.') or path.startswith('undo.'):
1097 1102 # journal is covered by 'lock'
1098 1103 if repo._currentlock(repo._lockref) is None:
1099 1104 repo.ui.develwarn('write with no lock: "%s"' % path,
1100 1105 stacklevel=3, config='check-locks')
1101 1106 elif repo._currentlock(repo._wlockref) is None:
1102 1107 # rest of vfs files are covered by 'wlock'
1103 1108 #
1104 1109 # exclude special files
1105 1110 for prefix in self._wlockfreeprefix:
1106 1111 if path.startswith(prefix):
1107 1112 return
1108 1113 repo.ui.develwarn('write with no wlock: "%s"' % path,
1109 1114 stacklevel=3, config='check-locks')
1110 1115 return ret
1111 1116 return checkvfs
1112 1117
1113 1118 def _getsvfsward(self, origfunc):
1114 1119 """build a ward for self.svfs"""
1115 1120 rref = weakref.ref(self)
1116 1121 def checksvfs(path, mode=None):
1117 1122 ret = origfunc(path, mode=mode)
1118 1123 repo = rref()
1119 1124 if repo is None or not util.safehasattr(repo, '_lockref'):
1120 1125 return
1121 1126 if mode in (None, 'r', 'rb'):
1122 1127 return
1123 1128 if path.startswith(repo.sharedpath):
1124 1129 # truncate name relative to the repository (.hg)
1125 1130 path = path[len(repo.sharedpath) + 1:]
1126 1131 if repo._currentlock(repo._lockref) is None:
1127 1132 repo.ui.develwarn('write with no lock: "%s"' % path,
1128 1133 stacklevel=4)
1129 1134 return ret
1130 1135 return checksvfs
1131 1136
1132 1137 def close(self):
1133 1138 self._writecaches()
1134 1139
1135 1140 def _writecaches(self):
1136 1141 if self._revbranchcache:
1137 1142 self._revbranchcache.write()
1138 1143
1139 1144 def _restrictcapabilities(self, caps):
1140 1145 if self.ui.configbool('experimental', 'bundle2-advertise'):
1141 1146 caps = set(caps)
1142 1147 capsblob = bundle2.encodecaps(bundle2.getrepocaps(self,
1143 1148 role='client'))
1144 1149 caps.add('bundle2=' + urlreq.quote(capsblob))
1145 1150 return caps
1146 1151
1147 1152 def _writerequirements(self):
1148 1153 scmutil.writerequires(self.vfs, self.requirements)
1149 1154
1150 1155 # Don't cache auditor/nofsauditor, or you'll end up with reference cycle:
1151 1156 # self -> auditor -> self._checknested -> self
1152 1157
1153 1158 @property
1154 1159 def auditor(self):
1155 1160 # This is only used by context.workingctx.match in order to
1156 1161 # detect files in subrepos.
1157 1162 return pathutil.pathauditor(self.root, callback=self._checknested)
1158 1163
1159 1164 @property
1160 1165 def nofsauditor(self):
1161 1166 # This is only used by context.basectx.match in order to detect
1162 1167 # files in subrepos.
1163 1168 return pathutil.pathauditor(self.root, callback=self._checknested,
1164 1169 realfs=False, cached=True)
1165 1170
1166 1171 def _checknested(self, path):
1167 1172 """Determine if path is a legal nested repository."""
1168 1173 if not path.startswith(self.root):
1169 1174 return False
1170 1175 subpath = path[len(self.root) + 1:]
1171 1176 normsubpath = util.pconvert(subpath)
1172 1177
1173 1178 # XXX: Checking against the current working copy is wrong in
1174 1179 # the sense that it can reject things like
1175 1180 #
1176 1181 # $ hg cat -r 10 sub/x.txt
1177 1182 #
1178 1183 # if sub/ is no longer a subrepository in the working copy
1179 1184 # parent revision.
1180 1185 #
1181 1186 # However, it can of course also allow things that would have
1182 1187 # been rejected before, such as the above cat command if sub/
1183 1188 # is a subrepository now, but was a normal directory before.
1184 1189 # The old path auditor would have rejected by mistake since it
1185 1190 # panics when it sees sub/.hg/.
1186 1191 #
1187 1192 # All in all, checking against the working copy seems sensible
1188 1193 # since we want to prevent access to nested repositories on
1189 1194 # the filesystem *now*.
1190 1195 ctx = self[None]
1191 1196 parts = util.splitpath(subpath)
1192 1197 while parts:
1193 1198 prefix = '/'.join(parts)
1194 1199 if prefix in ctx.substate:
1195 1200 if prefix == normsubpath:
1196 1201 return True
1197 1202 else:
1198 1203 sub = ctx.sub(prefix)
1199 1204 return sub.checknested(subpath[len(prefix) + 1:])
1200 1205 else:
1201 1206 parts.pop()
1202 1207 return False
1203 1208
1204 1209 def peer(self):
1205 1210 return localpeer(self) # not cached to avoid reference cycle
1206 1211
1207 1212 def unfiltered(self):
1208 1213 """Return unfiltered version of the repository
1209 1214
1210 1215 Intended to be overwritten by filtered repo."""
1211 1216 return self
1212 1217
1213 1218 def filtered(self, name, visibilityexceptions=None):
1214 1219 """Return a filtered version of a repository
1215 1220
1216 1221 The `name` parameter is the identifier of the requested view. This
1217 1222 will return a repoview object set "exactly" to the specified view.
1218 1223
1219 1224 This function does not apply recursive filtering to a repository. For
1220 1225 example calling `repo.filtered("served")` will return a repoview using
1221 1226 the "served" view, regardless of the initial view used by `repo`.
1222 1227
1223 1228 In other word, there is always only one level of `repoview` "filtering".
1224 1229 """
1225 1230 if self._extrafilterid is not None and '%' not in name:
1226 1231 name = name + '%' + self._extrafilterid
1227 1232
1228 1233 cls = repoview.newtype(self.unfiltered().__class__)
1229 1234 return cls(self, name, visibilityexceptions)
1230 1235
1231 1236 @mixedrepostorecache(('bookmarks', 'plain'), ('bookmarks.current', 'plain'),
1232 1237 ('bookmarks', ''), ('00changelog.i', ''))
1233 1238 def _bookmarks(self):
1234 1239 # Since the multiple files involved in the transaction cannot be
1235 1240 # written atomically (with current repository format), there is a race
1236 1241 # condition here.
1237 1242 #
1238 1243 # 1) changelog content A is read
1239 1244 # 2) outside transaction update changelog to content B
1240 1245 # 3) outside transaction update bookmark file referring to content B
1241 1246 # 4) bookmarks file content is read and filtered against changelog-A
1242 1247 #
1243 1248 # When this happens, bookmarks against nodes missing from A are dropped.
1244 1249 #
1245 1250 # Having this happening during read is not great, but it become worse
1246 1251 # when this happen during write because the bookmarks to the "unknown"
1247 1252 # nodes will be dropped for good. However, writes happen within locks.
1248 1253 # This locking makes it possible to have a race free consistent read.
1249 1254 # For this purpose data read from disc before locking are
1250 1255 # "invalidated" right after the locks are taken. This invalidations are
1251 1256 # "light", the `filecache` mechanism keep the data in memory and will
1252 1257 # reuse them if the underlying files did not changed. Not parsing the
1253 1258 # same data multiple times helps performances.
1254 1259 #
1255 1260 # Unfortunately in the case describe above, the files tracked by the
1256 1261 # bookmarks file cache might not have changed, but the in-memory
1257 1262 # content is still "wrong" because we used an older changelog content
1258 1263 # to process the on-disk data. So after locking, the changelog would be
1259 1264 # refreshed but `_bookmarks` would be preserved.
1260 1265 # Adding `00changelog.i` to the list of tracked file is not
1261 1266 # enough, because at the time we build the content for `_bookmarks` in
1262 1267 # (4), the changelog file has already diverged from the content used
1263 1268 # for loading `changelog` in (1)
1264 1269 #
1265 1270 # To prevent the issue, we force the changelog to be explicitly
1266 1271 # reloaded while computing `_bookmarks`. The data race can still happen
1267 1272 # without the lock (with a narrower window), but it would no longer go
1268 1273 # undetected during the lock time refresh.
1269 1274 #
1270 1275 # The new schedule is as follow
1271 1276 #
1272 1277 # 1) filecache logic detect that `_bookmarks` needs to be computed
1273 1278 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1274 1279 # 3) We force `changelog` filecache to be tested
1275 1280 # 4) cachestat for `changelog` are captured (for changelog)
1276 1281 # 5) `_bookmarks` is computed and cached
1277 1282 #
1278 1283 # The step in (3) ensure we have a changelog at least as recent as the
1279 1284 # cache stat computed in (1). As a result at locking time:
1280 1285 # * if the changelog did not changed since (1) -> we can reuse the data
1281 1286 # * otherwise -> the bookmarks get refreshed.
1282 1287 self._refreshchangelog()
1283 1288 return bookmarks.bmstore(self)
1284 1289
1285 1290 def _refreshchangelog(self):
1286 1291 """make sure the in memory changelog match the on-disk one"""
1287 1292 if ('changelog' in vars(self) and self.currenttransaction() is None):
1288 1293 del self.changelog
1289 1294
1290 1295 @property
1291 1296 def _activebookmark(self):
1292 1297 return self._bookmarks.active
1293 1298
1294 1299 # _phasesets depend on changelog. what we need is to call
1295 1300 # _phasecache.invalidate() if '00changelog.i' was changed, but it
1296 1301 # can't be easily expressed in filecache mechanism.
1297 1302 @storecache('phaseroots', '00changelog.i')
1298 1303 def _phasecache(self):
1299 1304 return phases.phasecache(self, self._phasedefaults)
1300 1305
1301 1306 @storecache('obsstore')
1302 1307 def obsstore(self):
1303 1308 return obsolete.makestore(self.ui, self)
1304 1309
1305 1310 @storecache('00changelog.i')
1306 1311 def changelog(self):
1307 1312 return self.store.changelog(txnutil.mayhavepending(self.root))
1308 1313
1309 1314 @storecache('00manifest.i')
1310 1315 def manifestlog(self):
1311 1316 return self.store.manifestlog(self, self._storenarrowmatch)
1312 1317
1313 1318 @repofilecache('dirstate')
1314 1319 def dirstate(self):
1315 1320 return self._makedirstate()
1316 1321
1317 1322 def _makedirstate(self):
1318 1323 """Extension point for wrapping the dirstate per-repo."""
1319 1324 sparsematchfn = lambda: sparse.matcher(self)
1320 1325
1321 1326 return dirstate.dirstate(self.vfs, self.ui, self.root,
1322 1327 self._dirstatevalidate, sparsematchfn)
1323 1328
1324 1329 def _dirstatevalidate(self, node):
1325 1330 try:
1326 1331 self.changelog.rev(node)
1327 1332 return node
1328 1333 except error.LookupError:
1329 1334 if not self._dirstatevalidatewarned:
1330 1335 self._dirstatevalidatewarned = True
1331 1336 self.ui.warn(_("warning: ignoring unknown"
1332 1337 " working parent %s!\n") % short(node))
1333 1338 return nullid
1334 1339
1335 1340 @storecache(narrowspec.FILENAME)
1336 1341 def narrowpats(self):
1337 1342 """matcher patterns for this repository's narrowspec
1338 1343
1339 1344 A tuple of (includes, excludes).
1340 1345 """
1341 1346 return narrowspec.load(self)
1342 1347
1343 1348 @storecache(narrowspec.FILENAME)
1344 1349 def _storenarrowmatch(self):
1345 1350 if repository.NARROW_REQUIREMENT not in self.requirements:
1346 1351 return matchmod.always()
1347 1352 include, exclude = self.narrowpats
1348 1353 return narrowspec.match(self.root, include=include, exclude=exclude)
1349 1354
1350 1355 @storecache(narrowspec.FILENAME)
1351 1356 def _narrowmatch(self):
1352 1357 if repository.NARROW_REQUIREMENT not in self.requirements:
1353 1358 return matchmod.always()
1354 1359 narrowspec.checkworkingcopynarrowspec(self)
1355 1360 include, exclude = self.narrowpats
1356 1361 return narrowspec.match(self.root, include=include, exclude=exclude)
1357 1362
1358 1363 def narrowmatch(self, match=None, includeexact=False):
1359 1364 """matcher corresponding the the repo's narrowspec
1360 1365
1361 1366 If `match` is given, then that will be intersected with the narrow
1362 1367 matcher.
1363 1368
1364 1369 If `includeexact` is True, then any exact matches from `match` will
1365 1370 be included even if they're outside the narrowspec.
1366 1371 """
1367 1372 if match:
1368 1373 if includeexact and not self._narrowmatch.always():
1369 1374 # do not exclude explicitly-specified paths so that they can
1370 1375 # be warned later on
1371 1376 em = matchmod.exact(match.files())
1372 1377 nm = matchmod.unionmatcher([self._narrowmatch, em])
1373 1378 return matchmod.intersectmatchers(match, nm)
1374 1379 return matchmod.intersectmatchers(match, self._narrowmatch)
1375 1380 return self._narrowmatch
1376 1381
1377 1382 def setnarrowpats(self, newincludes, newexcludes):
1378 1383 narrowspec.save(self, newincludes, newexcludes)
1379 1384 self.invalidate(clearfilecache=True)
1380 1385
1381 1386 def __getitem__(self, changeid):
1382 1387 if changeid is None:
1383 1388 return context.workingctx(self)
1384 1389 if isinstance(changeid, context.basectx):
1385 1390 return changeid
1386 1391 if isinstance(changeid, slice):
1387 1392 # wdirrev isn't contiguous so the slice shouldn't include it
1388 1393 return [self[i]
1389 1394 for i in pycompat.xrange(*changeid.indices(len(self)))
1390 1395 if i not in self.changelog.filteredrevs]
1391 1396 try:
1392 1397 if isinstance(changeid, int):
1393 1398 node = self.changelog.node(changeid)
1394 1399 rev = changeid
1395 1400 elif changeid == 'null':
1396 1401 node = nullid
1397 1402 rev = nullrev
1398 1403 elif changeid == 'tip':
1399 1404 node = self.changelog.tip()
1400 1405 rev = self.changelog.rev(node)
1401 1406 elif changeid == '.':
1402 1407 # this is a hack to delay/avoid loading obsmarkers
1403 1408 # when we know that '.' won't be hidden
1404 1409 node = self.dirstate.p1()
1405 1410 rev = self.unfiltered().changelog.rev(node)
1406 1411 elif len(changeid) == 20:
1407 1412 try:
1408 1413 node = changeid
1409 1414 rev = self.changelog.rev(changeid)
1410 1415 except error.FilteredLookupError:
1411 1416 changeid = hex(changeid) # for the error message
1412 1417 raise
1413 1418 except LookupError:
1414 1419 # check if it might have come from damaged dirstate
1415 1420 #
1416 1421 # XXX we could avoid the unfiltered if we had a recognizable
1417 1422 # exception for filtered changeset access
1418 1423 if (self.local()
1419 1424 and changeid in self.unfiltered().dirstate.parents()):
1420 1425 msg = _("working directory has unknown parent '%s'!")
1421 1426 raise error.Abort(msg % short(changeid))
1422 1427 changeid = hex(changeid) # for the error message
1423 1428 raise
1424 1429
1425 1430 elif len(changeid) == 40:
1426 1431 node = bin(changeid)
1427 1432 rev = self.changelog.rev(node)
1428 1433 else:
1429 1434 raise error.ProgrammingError(
1430 1435 "unsupported changeid '%s' of type %s" %
1431 1436 (changeid, type(changeid)))
1432 1437
1433 1438 return context.changectx(self, rev, node)
1434 1439
1435 1440 except (error.FilteredIndexError, error.FilteredLookupError):
1436 1441 raise error.FilteredRepoLookupError(_("filtered revision '%s'")
1437 1442 % pycompat.bytestr(changeid))
1438 1443 except (IndexError, LookupError):
1439 1444 raise error.RepoLookupError(
1440 1445 _("unknown revision '%s'") % pycompat.bytestr(changeid))
1441 1446 except error.WdirUnsupported:
1442 1447 return context.workingctx(self)
1443 1448
1444 1449 def __contains__(self, changeid):
1445 1450 """True if the given changeid exists
1446 1451
1447 1452 error.AmbiguousPrefixLookupError is raised if an ambiguous node
1448 1453 specified.
1449 1454 """
1450 1455 try:
1451 1456 self[changeid]
1452 1457 return True
1453 1458 except error.RepoLookupError:
1454 1459 return False
1455 1460
1456 1461 def __nonzero__(self):
1457 1462 return True
1458 1463
1459 1464 __bool__ = __nonzero__
1460 1465
1461 1466 def __len__(self):
1462 1467 # no need to pay the cost of repoview.changelog
1463 1468 unfi = self.unfiltered()
1464 1469 return len(unfi.changelog)
1465 1470
1466 1471 def __iter__(self):
1467 1472 return iter(self.changelog)
1468 1473
1469 1474 def revs(self, expr, *args):
1470 1475 '''Find revisions matching a revset.
1471 1476
1472 1477 The revset is specified as a string ``expr`` that may contain
1473 1478 %-formatting to escape certain types. See ``revsetlang.formatspec``.
1474 1479
1475 1480 Revset aliases from the configuration are not expanded. To expand
1476 1481 user aliases, consider calling ``scmutil.revrange()`` or
1477 1482 ``repo.anyrevs([expr], user=True)``.
1478 1483
1479 1484 Returns a revset.abstractsmartset, which is a list-like interface
1480 1485 that contains integer revisions.
1481 1486 '''
1482 1487 tree = revsetlang.spectree(expr, *args)
1483 1488 return revset.makematcher(tree)(self)
1484 1489
1485 1490 def set(self, expr, *args):
1486 1491 '''Find revisions matching a revset and emit changectx instances.
1487 1492
1488 1493 This is a convenience wrapper around ``revs()`` that iterates the
1489 1494 result and is a generator of changectx instances.
1490 1495
1491 1496 Revset aliases from the configuration are not expanded. To expand
1492 1497 user aliases, consider calling ``scmutil.revrange()``.
1493 1498 '''
1494 1499 for r in self.revs(expr, *args):
1495 1500 yield self[r]
1496 1501
1497 1502 def anyrevs(self, specs, user=False, localalias=None):
1498 1503 '''Find revisions matching one of the given revsets.
1499 1504
1500 1505 Revset aliases from the configuration are not expanded by default. To
1501 1506 expand user aliases, specify ``user=True``. To provide some local
1502 1507 definitions overriding user aliases, set ``localalias`` to
1503 1508 ``{name: definitionstring}``.
1504 1509 '''
1505 1510 if user:
1506 1511 m = revset.matchany(self.ui, specs,
1507 1512 lookup=revset.lookupfn(self),
1508 1513 localalias=localalias)
1509 1514 else:
1510 1515 m = revset.matchany(None, specs, localalias=localalias)
1511 1516 return m(self)
1512 1517
1513 1518 def url(self):
1514 1519 return 'file:' + self.root
1515 1520
1516 1521 def hook(self, name, throw=False, **args):
1517 1522 """Call a hook, passing this repo instance.
1518 1523
1519 1524 This a convenience method to aid invoking hooks. Extensions likely
1520 1525 won't call this unless they have registered a custom hook or are
1521 1526 replacing code that is expected to call a hook.
1522 1527 """
1523 1528 return hook.hook(self.ui, self, name, throw, **args)
1524 1529
1525 1530 @filteredpropertycache
1526 1531 def _tagscache(self):
1527 1532 '''Returns a tagscache object that contains various tags related
1528 1533 caches.'''
1529 1534
1530 1535 # This simplifies its cache management by having one decorated
1531 1536 # function (this one) and the rest simply fetch things from it.
1532 1537 class tagscache(object):
1533 1538 def __init__(self):
1534 1539 # These two define the set of tags for this repository. tags
1535 1540 # maps tag name to node; tagtypes maps tag name to 'global' or
1536 1541 # 'local'. (Global tags are defined by .hgtags across all
1537 1542 # heads, and local tags are defined in .hg/localtags.)
1538 1543 # They constitute the in-memory cache of tags.
1539 1544 self.tags = self.tagtypes = None
1540 1545
1541 1546 self.nodetagscache = self.tagslist = None
1542 1547
1543 1548 cache = tagscache()
1544 1549 cache.tags, cache.tagtypes = self._findtags()
1545 1550
1546 1551 return cache
1547 1552
1548 1553 def tags(self):
1549 1554 '''return a mapping of tag to node'''
1550 1555 t = {}
1551 1556 if self.changelog.filteredrevs:
1552 1557 tags, tt = self._findtags()
1553 1558 else:
1554 1559 tags = self._tagscache.tags
1555 1560 rev = self.changelog.rev
1556 1561 for k, v in tags.iteritems():
1557 1562 try:
1558 1563 # ignore tags to unknown nodes
1559 1564 rev(v)
1560 1565 t[k] = v
1561 1566 except (error.LookupError, ValueError):
1562 1567 pass
1563 1568 return t
1564 1569
1565 1570 def _findtags(self):
1566 1571 '''Do the hard work of finding tags. Return a pair of dicts
1567 1572 (tags, tagtypes) where tags maps tag name to node, and tagtypes
1568 1573 maps tag name to a string like \'global\' or \'local\'.
1569 1574 Subclasses or extensions are free to add their own tags, but
1570 1575 should be aware that the returned dicts will be retained for the
1571 1576 duration of the localrepo object.'''
1572 1577
1573 1578 # XXX what tagtype should subclasses/extensions use? Currently
1574 1579 # mq and bookmarks add tags, but do not set the tagtype at all.
1575 1580 # Should each extension invent its own tag type? Should there
1576 1581 # be one tagtype for all such "virtual" tags? Or is the status
1577 1582 # quo fine?
1578 1583
1579 1584
1580 1585 # map tag name to (node, hist)
1581 1586 alltags = tagsmod.findglobaltags(self.ui, self)
1582 1587 # map tag name to tag type
1583 1588 tagtypes = dict((tag, 'global') for tag in alltags)
1584 1589
1585 1590 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
1586 1591
1587 1592 # Build the return dicts. Have to re-encode tag names because
1588 1593 # the tags module always uses UTF-8 (in order not to lose info
1589 1594 # writing to the cache), but the rest of Mercurial wants them in
1590 1595 # local encoding.
1591 1596 tags = {}
1592 1597 for (name, (node, hist)) in alltags.iteritems():
1593 1598 if node != nullid:
1594 1599 tags[encoding.tolocal(name)] = node
1595 1600 tags['tip'] = self.changelog.tip()
1596 1601 tagtypes = dict([(encoding.tolocal(name), value)
1597 1602 for (name, value) in tagtypes.iteritems()])
1598 1603 return (tags, tagtypes)
1599 1604
1600 1605 def tagtype(self, tagname):
1601 1606 '''
1602 1607 return the type of the given tag. result can be:
1603 1608
1604 1609 'local' : a local tag
1605 1610 'global' : a global tag
1606 1611 None : tag does not exist
1607 1612 '''
1608 1613
1609 1614 return self._tagscache.tagtypes.get(tagname)
1610 1615
1611 1616 def tagslist(self):
1612 1617 '''return a list of tags ordered by revision'''
1613 1618 if not self._tagscache.tagslist:
1614 1619 l = []
1615 1620 for t, n in self.tags().iteritems():
1616 1621 l.append((self.changelog.rev(n), t, n))
1617 1622 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
1618 1623
1619 1624 return self._tagscache.tagslist
1620 1625
1621 1626 def nodetags(self, node):
1622 1627 '''return the tags associated with a node'''
1623 1628 if not self._tagscache.nodetagscache:
1624 1629 nodetagscache = {}
1625 1630 for t, n in self._tagscache.tags.iteritems():
1626 1631 nodetagscache.setdefault(n, []).append(t)
1627 1632 for tags in nodetagscache.itervalues():
1628 1633 tags.sort()
1629 1634 self._tagscache.nodetagscache = nodetagscache
1630 1635 return self._tagscache.nodetagscache.get(node, [])
1631 1636
1632 1637 def nodebookmarks(self, node):
1633 1638 """return the list of bookmarks pointing to the specified node"""
1634 1639 return self._bookmarks.names(node)
1635 1640
1636 1641 def branchmap(self):
1637 1642 '''returns a dictionary {branch: [branchheads]} with branchheads
1638 1643 ordered by increasing revision number'''
1639 1644 return self._branchcaches[self]
1640 1645
1641 1646 @unfilteredmethod
1642 1647 def revbranchcache(self):
1643 1648 if not self._revbranchcache:
1644 1649 self._revbranchcache = branchmap.revbranchcache(self.unfiltered())
1645 1650 return self._revbranchcache
1646 1651
1647 1652 def branchtip(self, branch, ignoremissing=False):
1648 1653 '''return the tip node for a given branch
1649 1654
1650 1655 If ignoremissing is True, then this method will not raise an error.
1651 1656 This is helpful for callers that only expect None for a missing branch
1652 1657 (e.g. namespace).
1653 1658
1654 1659 '''
1655 1660 try:
1656 1661 return self.branchmap().branchtip(branch)
1657 1662 except KeyError:
1658 1663 if not ignoremissing:
1659 1664 raise error.RepoLookupError(_("unknown branch '%s'") % branch)
1660 1665 else:
1661 1666 pass
1662 1667
1663 1668 def lookup(self, key):
1664 1669 node = scmutil.revsymbol(self, key).node()
1665 1670 if node is None:
1666 1671 raise error.RepoLookupError(_("unknown revision '%s'") % key)
1667 1672 return node
1668 1673
1669 1674 def lookupbranch(self, key):
1670 1675 if self.branchmap().hasbranch(key):
1671 1676 return key
1672 1677
1673 1678 return scmutil.revsymbol(self, key).branch()
1674 1679
1675 1680 def known(self, nodes):
1676 1681 cl = self.changelog
1677 1682 nm = cl.nodemap
1678 1683 filtered = cl.filteredrevs
1679 1684 result = []
1680 1685 for n in nodes:
1681 1686 r = nm.get(n)
1682 1687 resp = not (r is None or r in filtered)
1683 1688 result.append(resp)
1684 1689 return result
1685 1690
1686 1691 def local(self):
1687 1692 return self
1688 1693
1689 1694 def publishing(self):
1690 1695 # it's safe (and desirable) to trust the publish flag unconditionally
1691 1696 # so that we don't finalize changes shared between users via ssh or nfs
1692 1697 return self.ui.configbool('phases', 'publish', untrusted=True)
1693 1698
1694 1699 def cancopy(self):
1695 1700 # so statichttprepo's override of local() works
1696 1701 if not self.local():
1697 1702 return False
1698 1703 if not self.publishing():
1699 1704 return True
1700 1705 # if publishing we can't copy if there is filtered content
1701 1706 return not self.filtered('visible').changelog.filteredrevs
1702 1707
1703 1708 def shared(self):
1704 1709 '''the type of shared repository (None if not shared)'''
1705 1710 if self.sharedpath != self.path:
1706 1711 return 'store'
1707 1712 return None
1708 1713
1709 1714 def wjoin(self, f, *insidef):
1710 1715 return self.vfs.reljoin(self.root, f, *insidef)
1711 1716
1712 1717 def setparents(self, p1, p2=nullid):
1713 1718 with self.dirstate.parentchange():
1714 1719 copies = self.dirstate.setparents(p1, p2)
1715 1720 pctx = self[p1]
1716 1721 if copies:
1717 1722 # Adjust copy records, the dirstate cannot do it, it
1718 1723 # requires access to parents manifests. Preserve them
1719 1724 # only for entries added to first parent.
1720 1725 for f in copies:
1721 1726 if f not in pctx and copies[f] in pctx:
1722 1727 self.dirstate.copy(copies[f], f)
1723 1728 if p2 == nullid:
1724 1729 for f, s in sorted(self.dirstate.copies().items()):
1725 1730 if f not in pctx and s not in pctx:
1726 1731 self.dirstate.copy(None, f)
1727 1732
1728 1733 def filectx(self, path, changeid=None, fileid=None, changectx=None):
1729 1734 """changeid must be a changeset revision, if specified.
1730 1735 fileid can be a file revision or node."""
1731 1736 return context.filectx(self, path, changeid, fileid,
1732 1737 changectx=changectx)
1733 1738
1734 1739 def getcwd(self):
1735 1740 return self.dirstate.getcwd()
1736 1741
1737 1742 def pathto(self, f, cwd=None):
1738 1743 return self.dirstate.pathto(f, cwd)
1739 1744
1740 1745 def _loadfilter(self, filter):
1741 1746 if filter not in self._filterpats:
1742 1747 l = []
1743 1748 for pat, cmd in self.ui.configitems(filter):
1744 1749 if cmd == '!':
1745 1750 continue
1746 1751 mf = matchmod.match(self.root, '', [pat])
1747 1752 fn = None
1748 1753 params = cmd
1749 1754 for name, filterfn in self._datafilters.iteritems():
1750 1755 if cmd.startswith(name):
1751 1756 fn = filterfn
1752 1757 params = cmd[len(name):].lstrip()
1753 1758 break
1754 1759 if not fn:
1755 1760 fn = lambda s, c, **kwargs: procutil.filter(s, c)
1756 1761 # Wrap old filters not supporting keyword arguments
1757 1762 if not pycompat.getargspec(fn)[2]:
1758 1763 oldfn = fn
1759 1764 fn = lambda s, c, **kwargs: oldfn(s, c)
1760 1765 l.append((mf, fn, params))
1761 1766 self._filterpats[filter] = l
1762 1767 return self._filterpats[filter]
1763 1768
1764 1769 def _filter(self, filterpats, filename, data):
1765 1770 for mf, fn, cmd in filterpats:
1766 1771 if mf(filename):
1767 1772 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
1768 1773 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
1769 1774 break
1770 1775
1771 1776 return data
1772 1777
1773 1778 @unfilteredpropertycache
1774 1779 def _encodefilterpats(self):
1775 1780 return self._loadfilter('encode')
1776 1781
1777 1782 @unfilteredpropertycache
1778 1783 def _decodefilterpats(self):
1779 1784 return self._loadfilter('decode')
1780 1785
1781 1786 def adddatafilter(self, name, filter):
1782 1787 self._datafilters[name] = filter
1783 1788
1784 1789 def wread(self, filename):
1785 1790 if self.wvfs.islink(filename):
1786 1791 data = self.wvfs.readlink(filename)
1787 1792 else:
1788 1793 data = self.wvfs.read(filename)
1789 1794 return self._filter(self._encodefilterpats, filename, data)
1790 1795
1791 1796 def wwrite(self, filename, data, flags, backgroundclose=False, **kwargs):
1792 1797 """write ``data`` into ``filename`` in the working directory
1793 1798
1794 1799 This returns length of written (maybe decoded) data.
1795 1800 """
1796 1801 data = self._filter(self._decodefilterpats, filename, data)
1797 1802 if 'l' in flags:
1798 1803 self.wvfs.symlink(data, filename)
1799 1804 else:
1800 1805 self.wvfs.write(filename, data, backgroundclose=backgroundclose,
1801 1806 **kwargs)
1802 1807 if 'x' in flags:
1803 1808 self.wvfs.setflags(filename, False, True)
1804 1809 else:
1805 1810 self.wvfs.setflags(filename, False, False)
1806 1811 return len(data)
1807 1812
1808 1813 def wwritedata(self, filename, data):
1809 1814 return self._filter(self._decodefilterpats, filename, data)
1810 1815
1811 1816 def currenttransaction(self):
1812 1817 """return the current transaction or None if non exists"""
1813 1818 if self._transref:
1814 1819 tr = self._transref()
1815 1820 else:
1816 1821 tr = None
1817 1822
1818 1823 if tr and tr.running():
1819 1824 return tr
1820 1825 return None
1821 1826
1822 1827 def transaction(self, desc, report=None):
1823 1828 if (self.ui.configbool('devel', 'all-warnings')
1824 1829 or self.ui.configbool('devel', 'check-locks')):
1825 1830 if self._currentlock(self._lockref) is None:
1826 1831 raise error.ProgrammingError('transaction requires locking')
1827 1832 tr = self.currenttransaction()
1828 1833 if tr is not None:
1829 1834 return tr.nest(name=desc)
1830 1835
1831 1836 # abort here if the journal already exists
1832 1837 if self.svfs.exists("journal"):
1833 1838 raise error.RepoError(
1834 1839 _("abandoned transaction found"),
1835 1840 hint=_("run 'hg recover' to clean up transaction"))
1836 1841
1837 1842 idbase = "%.40f#%f" % (random.random(), time.time())
1838 1843 ha = hex(hashlib.sha1(idbase).digest())
1839 1844 txnid = 'TXN:' + ha
1840 1845 self.hook('pretxnopen', throw=True, txnname=desc, txnid=txnid)
1841 1846
1842 1847 self._writejournal(desc)
1843 1848 renames = [(vfs, x, undoname(x)) for vfs, x in self._journalfiles()]
1844 1849 if report:
1845 1850 rp = report
1846 1851 else:
1847 1852 rp = self.ui.warn
1848 1853 vfsmap = {'plain': self.vfs, 'store': self.svfs} # root of .hg/
1849 1854 # we must avoid cyclic reference between repo and transaction.
1850 1855 reporef = weakref.ref(self)
1851 1856 # Code to track tag movement
1852 1857 #
1853 1858 # Since tags are all handled as file content, it is actually quite hard
1854 1859 # to track these movement from a code perspective. So we fallback to a
1855 1860 # tracking at the repository level. One could envision to track changes
1856 1861 # to the '.hgtags' file through changegroup apply but that fails to
1857 1862 # cope with case where transaction expose new heads without changegroup
1858 1863 # being involved (eg: phase movement).
1859 1864 #
1860 1865 # For now, We gate the feature behind a flag since this likely comes
1861 1866 # with performance impacts. The current code run more often than needed
1862 1867 # and do not use caches as much as it could. The current focus is on
1863 1868 # the behavior of the feature so we disable it by default. The flag
1864 1869 # will be removed when we are happy with the performance impact.
1865 1870 #
1866 1871 # Once this feature is no longer experimental move the following
1867 1872 # documentation to the appropriate help section:
1868 1873 #
1869 1874 # The ``HG_TAG_MOVED`` variable will be set if the transaction touched
1870 1875 # tags (new or changed or deleted tags). In addition the details of
1871 1876 # these changes are made available in a file at:
1872 1877 # ``REPOROOT/.hg/changes/tags.changes``.
1873 1878 # Make sure you check for HG_TAG_MOVED before reading that file as it
1874 1879 # might exist from a previous transaction even if no tag were touched
1875 1880 # in this one. Changes are recorded in a line base format::
1876 1881 #
1877 1882 # <action> <hex-node> <tag-name>\n
1878 1883 #
1879 1884 # Actions are defined as follow:
1880 1885 # "-R": tag is removed,
1881 1886 # "+A": tag is added,
1882 1887 # "-M": tag is moved (old value),
1883 1888 # "+M": tag is moved (new value),
1884 1889 tracktags = lambda x: None
1885 1890 # experimental config: experimental.hook-track-tags
1886 1891 shouldtracktags = self.ui.configbool('experimental', 'hook-track-tags')
1887 1892 if desc != 'strip' and shouldtracktags:
1888 1893 oldheads = self.changelog.headrevs()
1889 1894 def tracktags(tr2):
1890 1895 repo = reporef()
1891 1896 oldfnodes = tagsmod.fnoderevs(repo.ui, repo, oldheads)
1892 1897 newheads = repo.changelog.headrevs()
1893 1898 newfnodes = tagsmod.fnoderevs(repo.ui, repo, newheads)
1894 1899 # notes: we compare lists here.
1895 1900 # As we do it only once buiding set would not be cheaper
1896 1901 changes = tagsmod.difftags(repo.ui, repo, oldfnodes, newfnodes)
1897 1902 if changes:
1898 1903 tr2.hookargs['tag_moved'] = '1'
1899 1904 with repo.vfs('changes/tags.changes', 'w',
1900 1905 atomictemp=True) as changesfile:
1901 1906 # note: we do not register the file to the transaction
1902 1907 # because we needs it to still exist on the transaction
1903 1908 # is close (for txnclose hooks)
1904 1909 tagsmod.writediff(changesfile, changes)
1905 1910 def validate(tr2):
1906 1911 """will run pre-closing hooks"""
1907 1912 # XXX the transaction API is a bit lacking here so we take a hacky
1908 1913 # path for now
1909 1914 #
1910 1915 # We cannot add this as a "pending" hooks since the 'tr.hookargs'
1911 1916 # dict is copied before these run. In addition we needs the data
1912 1917 # available to in memory hooks too.
1913 1918 #
1914 1919 # Moreover, we also need to make sure this runs before txnclose
1915 1920 # hooks and there is no "pending" mechanism that would execute
1916 1921 # logic only if hooks are about to run.
1917 1922 #
1918 1923 # Fixing this limitation of the transaction is also needed to track
1919 1924 # other families of changes (bookmarks, phases, obsolescence).
1920 1925 #
1921 1926 # This will have to be fixed before we remove the experimental
1922 1927 # gating.
1923 1928 tracktags(tr2)
1924 1929 repo = reporef()
1925 1930
1926 1931 r = repo.ui.configsuboptions('experimental',
1927 1932 'single-head-per-branch')
1928 1933 singlehead, singleheadsub = r
1929 1934 if singlehead:
1930 1935 accountclosed = singleheadsub.get("account-closed-heads", False)
1931 1936 scmutil.enforcesinglehead(repo, tr2, desc, accountclosed)
1932 1937 if hook.hashook(repo.ui, 'pretxnclose-bookmark'):
1933 1938 for name, (old, new) in sorted(tr.changes['bookmarks'].items()):
1934 1939 args = tr.hookargs.copy()
1935 1940 args.update(bookmarks.preparehookargs(name, old, new))
1936 1941 repo.hook('pretxnclose-bookmark', throw=True,
1937 1942 **pycompat.strkwargs(args))
1938 1943 if hook.hashook(repo.ui, 'pretxnclose-phase'):
1939 1944 cl = repo.unfiltered().changelog
1940 1945 for rev, (old, new) in tr.changes['phases'].items():
1941 1946 args = tr.hookargs.copy()
1942 1947 node = hex(cl.node(rev))
1943 1948 args.update(phases.preparehookargs(node, old, new))
1944 1949 repo.hook('pretxnclose-phase', throw=True,
1945 1950 **pycompat.strkwargs(args))
1946 1951
1947 1952 repo.hook('pretxnclose', throw=True,
1948 1953 **pycompat.strkwargs(tr.hookargs))
1949 1954 def releasefn(tr, success):
1950 1955 repo = reporef()
1951 1956 if repo is None:
1952 1957 # If the repo has been GC'd (and this release function is being
1953 1958 # called from transaction.__del__), there's not much we can do,
1954 1959 # so just leave the unfinished transaction there and let the
1955 1960 # user run `hg recover`.
1956 1961 return
1957 1962 if success:
1958 1963 # this should be explicitly invoked here, because
1959 1964 # in-memory changes aren't written out at closing
1960 1965 # transaction, if tr.addfilegenerator (via
1961 1966 # dirstate.write or so) isn't invoked while
1962 1967 # transaction running
1963 1968 repo.dirstate.write(None)
1964 1969 else:
1965 1970 # discard all changes (including ones already written
1966 1971 # out) in this transaction
1967 1972 narrowspec.restorebackup(self, 'journal.narrowspec')
1968 1973 narrowspec.restorewcbackup(self, 'journal.narrowspec.dirstate')
1969 1974 repo.dirstate.restorebackup(None, 'journal.dirstate')
1970 1975
1971 1976 repo.invalidate(clearfilecache=True)
1972 1977
1973 1978 tr = transaction.transaction(rp, self.svfs, vfsmap,
1974 1979 "journal",
1975 1980 "undo",
1976 1981 aftertrans(renames),
1977 1982 self.store.createmode,
1978 1983 validator=validate,
1979 1984 releasefn=releasefn,
1980 1985 checkambigfiles=_cachedfiles,
1981 1986 name=desc)
1982 1987 tr.changes['origrepolen'] = len(self)
1983 1988 tr.changes['obsmarkers'] = set()
1984 1989 tr.changes['phases'] = {}
1985 1990 tr.changes['bookmarks'] = {}
1986 1991
1987 1992 tr.hookargs['txnid'] = txnid
1988 1993 tr.hookargs['txnname'] = desc
1989 1994 # note: writing the fncache only during finalize mean that the file is
1990 1995 # outdated when running hooks. As fncache is used for streaming clone,
1991 1996 # this is not expected to break anything that happen during the hooks.
1992 1997 tr.addfinalize('flush-fncache', self.store.write)
1993 1998 def txnclosehook(tr2):
1994 1999 """To be run if transaction is successful, will schedule a hook run
1995 2000 """
1996 2001 # Don't reference tr2 in hook() so we don't hold a reference.
1997 2002 # This reduces memory consumption when there are multiple
1998 2003 # transactions per lock. This can likely go away if issue5045
1999 2004 # fixes the function accumulation.
2000 2005 hookargs = tr2.hookargs
2001 2006
2002 2007 def hookfunc():
2003 2008 repo = reporef()
2004 2009 if hook.hashook(repo.ui, 'txnclose-bookmark'):
2005 2010 bmchanges = sorted(tr.changes['bookmarks'].items())
2006 2011 for name, (old, new) in bmchanges:
2007 2012 args = tr.hookargs.copy()
2008 2013 args.update(bookmarks.preparehookargs(name, old, new))
2009 2014 repo.hook('txnclose-bookmark', throw=False,
2010 2015 **pycompat.strkwargs(args))
2011 2016
2012 2017 if hook.hashook(repo.ui, 'txnclose-phase'):
2013 2018 cl = repo.unfiltered().changelog
2014 2019 phasemv = sorted(tr.changes['phases'].items())
2015 2020 for rev, (old, new) in phasemv:
2016 2021 args = tr.hookargs.copy()
2017 2022 node = hex(cl.node(rev))
2018 2023 args.update(phases.preparehookargs(node, old, new))
2019 2024 repo.hook('txnclose-phase', throw=False,
2020 2025 **pycompat.strkwargs(args))
2021 2026
2022 2027 repo.hook('txnclose', throw=False,
2023 2028 **pycompat.strkwargs(hookargs))
2024 2029 reporef()._afterlock(hookfunc)
2025 2030 tr.addfinalize('txnclose-hook', txnclosehook)
2026 2031 # Include a leading "-" to make it happen before the transaction summary
2027 2032 # reports registered via scmutil.registersummarycallback() whose names
2028 2033 # are 00-txnreport etc. That way, the caches will be warm when the
2029 2034 # callbacks run.
2030 2035 tr.addpostclose('-warm-cache', self._buildcacheupdater(tr))
2031 2036 def txnaborthook(tr2):
2032 2037 """To be run if transaction is aborted
2033 2038 """
2034 2039 reporef().hook('txnabort', throw=False,
2035 2040 **pycompat.strkwargs(tr2.hookargs))
2036 2041 tr.addabort('txnabort-hook', txnaborthook)
2037 2042 # avoid eager cache invalidation. in-memory data should be identical
2038 2043 # to stored data if transaction has no error.
2039 2044 tr.addpostclose('refresh-filecachestats', self._refreshfilecachestats)
2040 2045 self._transref = weakref.ref(tr)
2041 2046 scmutil.registersummarycallback(self, tr, desc)
2042 2047 return tr
2043 2048
2044 2049 def _journalfiles(self):
2045 2050 return ((self.svfs, 'journal'),
2046 2051 (self.svfs, 'journal.narrowspec'),
2047 2052 (self.vfs, 'journal.narrowspec.dirstate'),
2048 2053 (self.vfs, 'journal.dirstate'),
2049 2054 (self.vfs, 'journal.branch'),
2050 2055 (self.vfs, 'journal.desc'),
2051 2056 (bookmarks.bookmarksvfs(self), 'journal.bookmarks'),
2052 2057 (self.svfs, 'journal.phaseroots'))
2053 2058
2054 2059 def undofiles(self):
2055 2060 return [(vfs, undoname(x)) for vfs, x in self._journalfiles()]
2056 2061
2057 2062 @unfilteredmethod
2058 2063 def _writejournal(self, desc):
2059 2064 self.dirstate.savebackup(None, 'journal.dirstate')
2060 2065 narrowspec.savewcbackup(self, 'journal.narrowspec.dirstate')
2061 2066 narrowspec.savebackup(self, 'journal.narrowspec')
2062 2067 self.vfs.write("journal.branch",
2063 2068 encoding.fromlocal(self.dirstate.branch()))
2064 2069 self.vfs.write("journal.desc",
2065 2070 "%d\n%s\n" % (len(self), desc))
2066 2071 bookmarksvfs = bookmarks.bookmarksvfs(self)
2067 2072 bookmarksvfs.write("journal.bookmarks",
2068 2073 bookmarksvfs.tryread("bookmarks"))
2069 2074 self.svfs.write("journal.phaseroots",
2070 2075 self.svfs.tryread("phaseroots"))
2071 2076
2072 2077 def recover(self):
2073 2078 with self.lock():
2074 2079 if self.svfs.exists("journal"):
2075 2080 self.ui.status(_("rolling back interrupted transaction\n"))
2076 2081 vfsmap = {'': self.svfs,
2077 2082 'plain': self.vfs,}
2078 2083 transaction.rollback(self.svfs, vfsmap, "journal",
2079 2084 self.ui.warn,
2080 2085 checkambigfiles=_cachedfiles)
2081 2086 self.invalidate()
2082 2087 return True
2083 2088 else:
2084 2089 self.ui.warn(_("no interrupted transaction available\n"))
2085 2090 return False
2086 2091
2087 2092 def rollback(self, dryrun=False, force=False):
2088 2093 wlock = lock = dsguard = None
2089 2094 try:
2090 2095 wlock = self.wlock()
2091 2096 lock = self.lock()
2092 2097 if self.svfs.exists("undo"):
2093 2098 dsguard = dirstateguard.dirstateguard(self, 'rollback')
2094 2099
2095 2100 return self._rollback(dryrun, force, dsguard)
2096 2101 else:
2097 2102 self.ui.warn(_("no rollback information available\n"))
2098 2103 return 1
2099 2104 finally:
2100 2105 release(dsguard, lock, wlock)
2101 2106
2102 2107 @unfilteredmethod # Until we get smarter cache management
2103 2108 def _rollback(self, dryrun, force, dsguard):
2104 2109 ui = self.ui
2105 2110 try:
2106 2111 args = self.vfs.read('undo.desc').splitlines()
2107 2112 (oldlen, desc, detail) = (int(args[0]), args[1], None)
2108 2113 if len(args) >= 3:
2109 2114 detail = args[2]
2110 2115 oldtip = oldlen - 1
2111 2116
2112 2117 if detail and ui.verbose:
2113 2118 msg = (_('repository tip rolled back to revision %d'
2114 2119 ' (undo %s: %s)\n')
2115 2120 % (oldtip, desc, detail))
2116 2121 else:
2117 2122 msg = (_('repository tip rolled back to revision %d'
2118 2123 ' (undo %s)\n')
2119 2124 % (oldtip, desc))
2120 2125 except IOError:
2121 2126 msg = _('rolling back unknown transaction\n')
2122 2127 desc = None
2123 2128
2124 2129 if not force and self['.'] != self['tip'] and desc == 'commit':
2125 2130 raise error.Abort(
2126 2131 _('rollback of last commit while not checked out '
2127 2132 'may lose data'), hint=_('use -f to force'))
2128 2133
2129 2134 ui.status(msg)
2130 2135 if dryrun:
2131 2136 return 0
2132 2137
2133 2138 parents = self.dirstate.parents()
2134 2139 self.destroying()
2135 2140 vfsmap = {'plain': self.vfs, '': self.svfs}
2136 2141 transaction.rollback(self.svfs, vfsmap, 'undo', ui.warn,
2137 2142 checkambigfiles=_cachedfiles)
2138 2143 bookmarksvfs = bookmarks.bookmarksvfs(self)
2139 2144 if bookmarksvfs.exists('undo.bookmarks'):
2140 2145 bookmarksvfs.rename('undo.bookmarks', 'bookmarks', checkambig=True)
2141 2146 if self.svfs.exists('undo.phaseroots'):
2142 2147 self.svfs.rename('undo.phaseroots', 'phaseroots', checkambig=True)
2143 2148 self.invalidate()
2144 2149
2145 2150 parentgone = any(p not in self.changelog.nodemap for p in parents)
2146 2151 if parentgone:
2147 2152 # prevent dirstateguard from overwriting already restored one
2148 2153 dsguard.close()
2149 2154
2150 2155 narrowspec.restorebackup(self, 'undo.narrowspec')
2151 2156 narrowspec.restorewcbackup(self, 'undo.narrowspec.dirstate')
2152 2157 self.dirstate.restorebackup(None, 'undo.dirstate')
2153 2158 try:
2154 2159 branch = self.vfs.read('undo.branch')
2155 2160 self.dirstate.setbranch(encoding.tolocal(branch))
2156 2161 except IOError:
2157 2162 ui.warn(_('named branch could not be reset: '
2158 2163 'current branch is still \'%s\'\n')
2159 2164 % self.dirstate.branch())
2160 2165
2161 2166 parents = tuple([p.rev() for p in self[None].parents()])
2162 2167 if len(parents) > 1:
2163 2168 ui.status(_('working directory now based on '
2164 2169 'revisions %d and %d\n') % parents)
2165 2170 else:
2166 2171 ui.status(_('working directory now based on '
2167 2172 'revision %d\n') % parents)
2168 2173 mergemod.mergestate.clean(self, self['.'].node())
2169 2174
2170 2175 # TODO: if we know which new heads may result from this rollback, pass
2171 2176 # them to destroy(), which will prevent the branchhead cache from being
2172 2177 # invalidated.
2173 2178 self.destroyed()
2174 2179 return 0
2175 2180
2176 2181 def _buildcacheupdater(self, newtransaction):
2177 2182 """called during transaction to build the callback updating cache
2178 2183
2179 2184 Lives on the repository to help extension who might want to augment
2180 2185 this logic. For this purpose, the created transaction is passed to the
2181 2186 method.
2182 2187 """
2183 2188 # we must avoid cyclic reference between repo and transaction.
2184 2189 reporef = weakref.ref(self)
2185 2190 def updater(tr):
2186 2191 repo = reporef()
2187 2192 repo.updatecaches(tr)
2188 2193 return updater
2189 2194
2190 2195 @unfilteredmethod
2191 2196 def updatecaches(self, tr=None, full=False):
2192 2197 """warm appropriate caches
2193 2198
2194 2199 If this function is called after a transaction closed. The transaction
2195 2200 will be available in the 'tr' argument. This can be used to selectively
2196 2201 update caches relevant to the changes in that transaction.
2197 2202
2198 2203 If 'full' is set, make sure all caches the function knows about have
2199 2204 up-to-date data. Even the ones usually loaded more lazily.
2200 2205 """
2201 2206 if tr is not None and tr.hookargs.get('source') == 'strip':
2202 2207 # During strip, many caches are invalid but
2203 2208 # later call to `destroyed` will refresh them.
2204 2209 return
2205 2210
2206 2211 if tr is None or tr.changes['origrepolen'] < len(self):
2207 2212 # accessing the 'ser ved' branchmap should refresh all the others,
2208 2213 self.ui.debug('updating the branch cache\n')
2209 2214 self.filtered('served').branchmap()
2210 2215 self.filtered('served.hidden').branchmap()
2211 2216
2212 2217 if full:
2213 2218 unfi = self.unfiltered()
2214 2219 rbc = unfi.revbranchcache()
2215 2220 for r in unfi.changelog:
2216 2221 rbc.branchinfo(r)
2217 2222 rbc.write()
2218 2223
2219 2224 # ensure the working copy parents are in the manifestfulltextcache
2220 2225 for ctx in self['.'].parents():
2221 2226 ctx.manifest() # accessing the manifest is enough
2222 2227
2223 2228 # accessing fnode cache warms the cache
2224 2229 tagsmod.fnoderevs(self.ui, unfi, unfi.changelog.revs())
2225 2230 # accessing tags warm the cache
2226 2231 self.tags()
2227 2232 self.filtered('served').tags()
2228 2233
2229 2234 # The `full` arg is documented as updating even the lazily-loaded
2230 2235 # caches immediately, so we're forcing a write to cause these caches
2231 2236 # to be warmed up even if they haven't explicitly been requested
2232 2237 # yet (if they've never been used by hg, they won't ever have been
2233 2238 # written, even if they're a subset of another kind of cache that
2234 2239 # *has* been used).
2235 2240 for filt in repoview.filtertable.keys():
2236 2241 filtered = self.filtered(filt)
2237 2242 filtered.branchmap().write(filtered)
2238 2243
2239 2244 def invalidatecaches(self):
2240 2245
2241 2246 if r'_tagscache' in vars(self):
2242 2247 # can't use delattr on proxy
2243 2248 del self.__dict__[r'_tagscache']
2244 2249
2245 2250 self._branchcaches.clear()
2246 2251 self.invalidatevolatilesets()
2247 2252 self._sparsesignaturecache.clear()
2248 2253
2249 2254 def invalidatevolatilesets(self):
2250 2255 self.filteredrevcache.clear()
2251 2256 obsolete.clearobscaches(self)
2252 2257
2253 2258 def invalidatedirstate(self):
2254 2259 '''Invalidates the dirstate, causing the next call to dirstate
2255 2260 to check if it was modified since the last time it was read,
2256 2261 rereading it if it has.
2257 2262
2258 2263 This is different to dirstate.invalidate() that it doesn't always
2259 2264 rereads the dirstate. Use dirstate.invalidate() if you want to
2260 2265 explicitly read the dirstate again (i.e. restoring it to a previous
2261 2266 known good state).'''
2262 2267 if hasunfilteredcache(self, r'dirstate'):
2263 2268 for k in self.dirstate._filecache:
2264 2269 try:
2265 2270 delattr(self.dirstate, k)
2266 2271 except AttributeError:
2267 2272 pass
2268 2273 delattr(self.unfiltered(), r'dirstate')
2269 2274
2270 2275 def invalidate(self, clearfilecache=False):
2271 2276 '''Invalidates both store and non-store parts other than dirstate
2272 2277
2273 2278 If a transaction is running, invalidation of store is omitted,
2274 2279 because discarding in-memory changes might cause inconsistency
2275 2280 (e.g. incomplete fncache causes unintentional failure, but
2276 2281 redundant one doesn't).
2277 2282 '''
2278 2283 unfiltered = self.unfiltered() # all file caches are stored unfiltered
2279 2284 for k in list(self._filecache.keys()):
2280 2285 # dirstate is invalidated separately in invalidatedirstate()
2281 2286 if k == 'dirstate':
2282 2287 continue
2283 2288 if (k == 'changelog' and
2284 2289 self.currenttransaction() and
2285 2290 self.changelog._delayed):
2286 2291 # The changelog object may store unwritten revisions. We don't
2287 2292 # want to lose them.
2288 2293 # TODO: Solve the problem instead of working around it.
2289 2294 continue
2290 2295
2291 2296 if clearfilecache:
2292 2297 del self._filecache[k]
2293 2298 try:
2294 2299 delattr(unfiltered, k)
2295 2300 except AttributeError:
2296 2301 pass
2297 2302 self.invalidatecaches()
2298 2303 if not self.currenttransaction():
2299 2304 # TODO: Changing contents of store outside transaction
2300 2305 # causes inconsistency. We should make in-memory store
2301 2306 # changes detectable, and abort if changed.
2302 2307 self.store.invalidatecaches()
2303 2308
2304 2309 def invalidateall(self):
2305 2310 '''Fully invalidates both store and non-store parts, causing the
2306 2311 subsequent operation to reread any outside changes.'''
2307 2312 # extension should hook this to invalidate its caches
2308 2313 self.invalidate()
2309 2314 self.invalidatedirstate()
2310 2315
2311 2316 @unfilteredmethod
2312 2317 def _refreshfilecachestats(self, tr):
2313 2318 """Reload stats of cached files so that they are flagged as valid"""
2314 2319 for k, ce in self._filecache.items():
2315 2320 k = pycompat.sysstr(k)
2316 2321 if k == r'dirstate' or k not in self.__dict__:
2317 2322 continue
2318 2323 ce.refresh()
2319 2324
2320 2325 def _lock(self, vfs, lockname, wait, releasefn, acquirefn, desc,
2321 2326 inheritchecker=None, parentenvvar=None):
2322 2327 parentlock = None
2323 2328 # the contents of parentenvvar are used by the underlying lock to
2324 2329 # determine whether it can be inherited
2325 2330 if parentenvvar is not None:
2326 2331 parentlock = encoding.environ.get(parentenvvar)
2327 2332
2328 2333 timeout = 0
2329 2334 warntimeout = 0
2330 2335 if wait:
2331 2336 timeout = self.ui.configint("ui", "timeout")
2332 2337 warntimeout = self.ui.configint("ui", "timeout.warn")
2333 2338 # internal config: ui.signal-safe-lock
2334 2339 signalsafe = self.ui.configbool('ui', 'signal-safe-lock')
2335 2340
2336 2341 l = lockmod.trylock(self.ui, vfs, lockname, timeout, warntimeout,
2337 2342 releasefn=releasefn,
2338 2343 acquirefn=acquirefn, desc=desc,
2339 2344 inheritchecker=inheritchecker,
2340 2345 parentlock=parentlock,
2341 2346 signalsafe=signalsafe)
2342 2347 return l
2343 2348
2344 2349 def _afterlock(self, callback):
2345 2350 """add a callback to be run when the repository is fully unlocked
2346 2351
2347 2352 The callback will be executed when the outermost lock is released
2348 2353 (with wlock being higher level than 'lock')."""
2349 2354 for ref in (self._wlockref, self._lockref):
2350 2355 l = ref and ref()
2351 2356 if l and l.held:
2352 2357 l.postrelease.append(callback)
2353 2358 break
2354 2359 else: # no lock have been found.
2355 2360 callback()
2356 2361
2357 2362 def lock(self, wait=True):
2358 2363 '''Lock the repository store (.hg/store) and return a weak reference
2359 2364 to the lock. Use this before modifying the store (e.g. committing or
2360 2365 stripping). If you are opening a transaction, get a lock as well.)
2361 2366
2362 2367 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2363 2368 'wlock' first to avoid a dead-lock hazard.'''
2364 2369 l = self._currentlock(self._lockref)
2365 2370 if l is not None:
2366 2371 l.lock()
2367 2372 return l
2368 2373
2369 2374 l = self._lock(vfs=self.svfs,
2370 2375 lockname="lock",
2371 2376 wait=wait,
2372 2377 releasefn=None,
2373 2378 acquirefn=self.invalidate,
2374 2379 desc=_('repository %s') % self.origroot)
2375 2380 self._lockref = weakref.ref(l)
2376 2381 return l
2377 2382
2378 2383 def _wlockchecktransaction(self):
2379 2384 if self.currenttransaction() is not None:
2380 2385 raise error.LockInheritanceContractViolation(
2381 2386 'wlock cannot be inherited in the middle of a transaction')
2382 2387
2383 2388 def wlock(self, wait=True):
2384 2389 '''Lock the non-store parts of the repository (everything under
2385 2390 .hg except .hg/store) and return a weak reference to the lock.
2386 2391
2387 2392 Use this before modifying files in .hg.
2388 2393
2389 2394 If both 'lock' and 'wlock' must be acquired, ensure you always acquires
2390 2395 'wlock' first to avoid a dead-lock hazard.'''
2391 2396 l = self._wlockref and self._wlockref()
2392 2397 if l is not None and l.held:
2393 2398 l.lock()
2394 2399 return l
2395 2400
2396 2401 # We do not need to check for non-waiting lock acquisition. Such
2397 2402 # acquisition would not cause dead-lock as they would just fail.
2398 2403 if wait and (self.ui.configbool('devel', 'all-warnings')
2399 2404 or self.ui.configbool('devel', 'check-locks')):
2400 2405 if self._currentlock(self._lockref) is not None:
2401 2406 self.ui.develwarn('"wlock" acquired after "lock"')
2402 2407
2403 2408 def unlock():
2404 2409 if self.dirstate.pendingparentchange():
2405 2410 self.dirstate.invalidate()
2406 2411 else:
2407 2412 self.dirstate.write(None)
2408 2413
2409 2414 self._filecache['dirstate'].refresh()
2410 2415
2411 2416 l = self._lock(self.vfs, "wlock", wait, unlock,
2412 2417 self.invalidatedirstate, _('working directory of %s') %
2413 2418 self.origroot,
2414 2419 inheritchecker=self._wlockchecktransaction,
2415 2420 parentenvvar='HG_WLOCK_LOCKER')
2416 2421 self._wlockref = weakref.ref(l)
2417 2422 return l
2418 2423
2419 2424 def _currentlock(self, lockref):
2420 2425 """Returns the lock if it's held, or None if it's not."""
2421 2426 if lockref is None:
2422 2427 return None
2423 2428 l = lockref()
2424 2429 if l is None or not l.held:
2425 2430 return None
2426 2431 return l
2427 2432
2428 2433 def currentwlock(self):
2429 2434 """Returns the wlock if it's held, or None if it's not."""
2430 2435 return self._currentlock(self._wlockref)
2431 2436
2432 2437 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist,
2433 2438 includecopymeta):
2434 2439 """
2435 2440 commit an individual file as part of a larger transaction
2436 2441 """
2437 2442
2438 2443 fname = fctx.path()
2439 2444 fparent1 = manifest1.get(fname, nullid)
2440 2445 fparent2 = manifest2.get(fname, nullid)
2441 2446 if isinstance(fctx, context.filectx):
2442 2447 node = fctx.filenode()
2443 2448 if node in [fparent1, fparent2]:
2444 2449 self.ui.debug('reusing %s filelog entry\n' % fname)
2445 2450 if ((fparent1 != nullid and
2446 2451 manifest1.flags(fname) != fctx.flags()) or
2447 2452 (fparent2 != nullid and
2448 2453 manifest2.flags(fname) != fctx.flags())):
2449 2454 changelist.append(fname)
2450 2455 return node
2451 2456
2452 2457 flog = self.file(fname)
2453 2458 meta = {}
2454 2459 cfname = fctx.copysource()
2455 2460 if cfname and cfname != fname:
2456 2461 # Mark the new revision of this file as a copy of another
2457 2462 # file. This copy data will effectively act as a parent
2458 2463 # of this new revision. If this is a merge, the first
2459 2464 # parent will be the nullid (meaning "look up the copy data")
2460 2465 # and the second one will be the other parent. For example:
2461 2466 #
2462 2467 # 0 --- 1 --- 3 rev1 changes file foo
2463 2468 # \ / rev2 renames foo to bar and changes it
2464 2469 # \- 2 -/ rev3 should have bar with all changes and
2465 2470 # should record that bar descends from
2466 2471 # bar in rev2 and foo in rev1
2467 2472 #
2468 2473 # this allows this merge to succeed:
2469 2474 #
2470 2475 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
2471 2476 # \ / merging rev3 and rev4 should use bar@rev2
2472 2477 # \- 2 --- 4 as the merge base
2473 2478 #
2474 2479
2475 2480 cnode = manifest1.get(cfname)
2476 2481 newfparent = fparent2
2477 2482
2478 2483 if manifest2: # branch merge
2479 2484 if fparent2 == nullid or cnode is None: # copied on remote side
2480 2485 if cfname in manifest2:
2481 2486 cnode = manifest2[cfname]
2482 2487 newfparent = fparent1
2483 2488
2484 2489 # Here, we used to search backwards through history to try to find
2485 2490 # where the file copy came from if the source of a copy was not in
2486 2491 # the parent directory. However, this doesn't actually make sense to
2487 2492 # do (what does a copy from something not in your working copy even
2488 2493 # mean?) and it causes bugs (eg, issue4476). Instead, we will warn
2489 2494 # the user that copy information was dropped, so if they didn't
2490 2495 # expect this outcome it can be fixed, but this is the correct
2491 2496 # behavior in this circumstance.
2492 2497
2493 2498 if cnode:
2494 2499 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(cnode)))
2495 2500 if includecopymeta:
2496 2501 meta["copy"] = cfname
2497 2502 meta["copyrev"] = hex(cnode)
2498 2503 fparent1, fparent2 = nullid, newfparent
2499 2504 else:
2500 2505 self.ui.warn(_("warning: can't find ancestor for '%s' "
2501 2506 "copied from '%s'!\n") % (fname, cfname))
2502 2507
2503 2508 elif fparent1 == nullid:
2504 2509 fparent1, fparent2 = fparent2, nullid
2505 2510 elif fparent2 != nullid:
2506 2511 # is one parent an ancestor of the other?
2507 2512 fparentancestors = flog.commonancestorsheads(fparent1, fparent2)
2508 2513 if fparent1 in fparentancestors:
2509 2514 fparent1, fparent2 = fparent2, nullid
2510 2515 elif fparent2 in fparentancestors:
2511 2516 fparent2 = nullid
2512 2517
2513 2518 # is the file changed?
2514 2519 text = fctx.data()
2515 2520 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
2516 2521 changelist.append(fname)
2517 2522 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
2518 2523 # are just the flags changed during merge?
2519 2524 elif fname in manifest1 and manifest1.flags(fname) != fctx.flags():
2520 2525 changelist.append(fname)
2521 2526
2522 2527 return fparent1
2523 2528
2524 2529 def checkcommitpatterns(self, wctx, vdirs, match, status, fail):
2525 2530 """check for commit arguments that aren't committable"""
2526 2531 if match.isexact() or match.prefix():
2527 2532 matched = set(status.modified + status.added + status.removed)
2528 2533
2529 2534 for f in match.files():
2530 2535 f = self.dirstate.normalize(f)
2531 2536 if f == '.' or f in matched or f in wctx.substate:
2532 2537 continue
2533 2538 if f in status.deleted:
2534 2539 fail(f, _('file not found!'))
2535 2540 if f in vdirs: # visited directory
2536 2541 d = f + '/'
2537 2542 for mf in matched:
2538 2543 if mf.startswith(d):
2539 2544 break
2540 2545 else:
2541 2546 fail(f, _("no match under directory!"))
2542 2547 elif f not in self.dirstate:
2543 2548 fail(f, _("file not tracked!"))
2544 2549
2545 2550 @unfilteredmethod
2546 2551 def commit(self, text="", user=None, date=None, match=None, force=False,
2547 2552 editor=False, extra=None):
2548 2553 """Add a new revision to current repository.
2549 2554
2550 2555 Revision information is gathered from the working directory,
2551 2556 match can be used to filter the committed files. If editor is
2552 2557 supplied, it is called to get a commit message.
2553 2558 """
2554 2559 if extra is None:
2555 2560 extra = {}
2556 2561
2557 2562 def fail(f, msg):
2558 2563 raise error.Abort('%s: %s' % (f, msg))
2559 2564
2560 2565 if not match:
2561 2566 match = matchmod.always()
2562 2567
2563 2568 if not force:
2564 2569 vdirs = []
2565 2570 match.explicitdir = vdirs.append
2566 2571 match.bad = fail
2567 2572
2568 2573 # lock() for recent changelog (see issue4368)
2569 2574 with self.wlock(), self.lock():
2570 2575 wctx = self[None]
2571 2576 merge = len(wctx.parents()) > 1
2572 2577
2573 2578 if not force and merge and not match.always():
2574 2579 raise error.Abort(_('cannot partially commit a merge '
2575 2580 '(do not specify files or patterns)'))
2576 2581
2577 2582 status = self.status(match=match, clean=force)
2578 2583 if force:
2579 2584 status.modified.extend(status.clean) # mq may commit clean files
2580 2585
2581 2586 # check subrepos
2582 2587 subs, commitsubs, newstate = subrepoutil.precommit(
2583 2588 self.ui, wctx, status, match, force=force)
2584 2589
2585 2590 # make sure all explicit patterns are matched
2586 2591 if not force:
2587 2592 self.checkcommitpatterns(wctx, vdirs, match, status, fail)
2588 2593
2589 2594 cctx = context.workingcommitctx(self, status,
2590 2595 text, user, date, extra)
2591 2596
2592 2597 # internal config: ui.allowemptycommit
2593 2598 allowemptycommit = (wctx.branch() != wctx.p1().branch()
2594 2599 or extra.get('close') or merge or cctx.files()
2595 2600 or self.ui.configbool('ui', 'allowemptycommit'))
2596 2601 if not allowemptycommit:
2597 2602 return None
2598 2603
2599 2604 if merge and cctx.deleted():
2600 2605 raise error.Abort(_("cannot commit merge with missing files"))
2601 2606
2602 2607 ms = mergemod.mergestate.read(self)
2603 2608 mergeutil.checkunresolved(ms)
2604 2609
2605 2610 if editor:
2606 2611 cctx._text = editor(self, cctx, subs)
2607 2612 edited = (text != cctx._text)
2608 2613
2609 2614 # Save commit message in case this transaction gets rolled back
2610 2615 # (e.g. by a pretxncommit hook). Leave the content alone on
2611 2616 # the assumption that the user will use the same editor again.
2612 2617 msgfn = self.savecommitmessage(cctx._text)
2613 2618
2614 2619 # commit subs and write new state
2615 2620 if subs:
2616 2621 uipathfn = scmutil.getuipathfn(self)
2617 2622 for s in sorted(commitsubs):
2618 2623 sub = wctx.sub(s)
2619 2624 self.ui.status(_('committing subrepository %s\n') %
2620 2625 uipathfn(subrepoutil.subrelpath(sub)))
2621 2626 sr = sub.commit(cctx._text, user, date)
2622 2627 newstate[s] = (newstate[s][0], sr)
2623 2628 subrepoutil.writestate(self, newstate)
2624 2629
2625 2630 p1, p2 = self.dirstate.parents()
2626 2631 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
2627 2632 try:
2628 2633 self.hook("precommit", throw=True, parent1=hookp1,
2629 2634 parent2=hookp2)
2630 2635 with self.transaction('commit'):
2631 2636 ret = self.commitctx(cctx, True)
2632 2637 # update bookmarks, dirstate and mergestate
2633 2638 bookmarks.update(self, [p1, p2], ret)
2634 2639 cctx.markcommitted(ret)
2635 2640 ms.reset()
2636 2641 except: # re-raises
2637 2642 if edited:
2638 2643 self.ui.write(
2639 2644 _('note: commit message saved in %s\n') % msgfn)
2640 2645 raise
2641 2646
2642 2647 def commithook():
2643 2648 # hack for command that use a temporary commit (eg: histedit)
2644 2649 # temporary commit got stripped before hook release
2645 2650 if self.changelog.hasnode(ret):
2646 2651 self.hook("commit", node=hex(ret), parent1=hookp1,
2647 2652 parent2=hookp2)
2648 2653 self._afterlock(commithook)
2649 2654 return ret
2650 2655
2651 2656 @unfilteredmethod
2652 2657 def commitctx(self, ctx, error=False, origctx=None):
2653 2658 """Add a new revision to current repository.
2654 2659 Revision information is passed via the context argument.
2655 2660
2656 2661 ctx.files() should list all files involved in this commit, i.e.
2657 2662 modified/added/removed files. On merge, it may be wider than the
2658 2663 ctx.files() to be committed, since any file nodes derived directly
2659 2664 from p1 or p2 are excluded from the committed ctx.files().
2660 2665
2661 2666 origctx is for convert to work around the problem that bug
2662 2667 fixes to the files list in changesets change hashes. For
2663 2668 convert to be the identity, it can pass an origctx and this
2664 2669 function will use the same files list when it makes sense to
2665 2670 do so.
2666 2671 """
2667 2672
2668 2673 p1, p2 = ctx.p1(), ctx.p2()
2669 2674 user = ctx.user()
2670 2675
2671 2676 writecopiesto = self.ui.config('experimental', 'copies.write-to')
2672 2677 writefilecopymeta = writecopiesto != 'changeset-only'
2673 2678 writechangesetcopy = (writecopiesto in
2674 2679 ('changeset-only', 'compatibility'))
2675 2680 p1copies, p2copies = None, None
2676 2681 if writechangesetcopy:
2677 2682 p1copies = ctx.p1copies()
2678 2683 p2copies = ctx.p2copies()
2679 2684 filesadded, filesremoved = None, None
2680 2685 with self.lock(), self.transaction("commit") as tr:
2681 2686 trp = weakref.proxy(tr)
2682 2687
2683 2688 if ctx.manifestnode():
2684 2689 # reuse an existing manifest revision
2685 2690 self.ui.debug('reusing known manifest\n')
2686 2691 mn = ctx.manifestnode()
2687 2692 files = ctx.files()
2688 2693 if writechangesetcopy:
2689 2694 filesadded = ctx.filesadded()
2690 2695 filesremoved = ctx.filesremoved()
2691 2696 elif ctx.files():
2692 2697 m1ctx = p1.manifestctx()
2693 2698 m2ctx = p2.manifestctx()
2694 2699 mctx = m1ctx.copy()
2695 2700
2696 2701 m = mctx.read()
2697 2702 m1 = m1ctx.read()
2698 2703 m2 = m2ctx.read()
2699 2704
2700 2705 # check in files
2701 2706 added = []
2702 2707 changed = []
2703 2708 removed = list(ctx.removed())
2704 2709 linkrev = len(self)
2705 2710 self.ui.note(_("committing files:\n"))
2706 2711 uipathfn = scmutil.getuipathfn(self)
2707 2712 for f in sorted(ctx.modified() + ctx.added()):
2708 2713 self.ui.note(uipathfn(f) + "\n")
2709 2714 try:
2710 2715 fctx = ctx[f]
2711 2716 if fctx is None:
2712 2717 removed.append(f)
2713 2718 else:
2714 2719 added.append(f)
2715 2720 m[f] = self._filecommit(fctx, m1, m2, linkrev,
2716 2721 trp, changed,
2717 2722 writefilecopymeta)
2718 2723 m.setflag(f, fctx.flags())
2719 2724 except OSError:
2720 2725 self.ui.warn(_("trouble committing %s!\n") %
2721 2726 uipathfn(f))
2722 2727 raise
2723 2728 except IOError as inst:
2724 2729 errcode = getattr(inst, 'errno', errno.ENOENT)
2725 2730 if error or errcode and errcode != errno.ENOENT:
2726 2731 self.ui.warn(_("trouble committing %s!\n") %
2727 2732 uipathfn(f))
2728 2733 raise
2729 2734
2730 2735 # update manifest
2731 2736 removed = [f for f in removed if f in m1 or f in m2]
2732 2737 drop = sorted([f for f in removed if f in m])
2733 2738 for f in drop:
2734 2739 del m[f]
2735 2740 if p2.rev() != nullrev:
2736 2741 @util.cachefunc
2737 2742 def mas():
2738 2743 p1n = p1.node()
2739 2744 p2n = p2.node()
2740 2745 cahs = self.changelog.commonancestorsheads(p1n, p2n)
2741 2746 if not cahs:
2742 2747 cahs = [nullrev]
2743 2748 return [self[r].manifest() for r in cahs]
2744 2749 def deletionfromparent(f):
2745 2750 # When a file is removed relative to p1 in a merge, this
2746 2751 # function determines whether the absence is due to a
2747 2752 # deletion from a parent, or whether the merge commit
2748 2753 # itself deletes the file. We decide this by doing a
2749 2754 # simplified three way merge of the manifest entry for
2750 2755 # the file. There are two ways we decide the merge
2751 2756 # itself didn't delete a file:
2752 2757 # - neither parent (nor the merge) contain the file
2753 2758 # - exactly one parent contains the file, and that
2754 2759 # parent has the same filelog entry as the merge
2755 2760 # ancestor (or all of them if there two). In other
2756 2761 # words, that parent left the file unchanged while the
2757 2762 # other one deleted it.
2758 2763 # One way to think about this is that deleting a file is
2759 2764 # similar to emptying it, so the list of changed files
2760 2765 # should be similar either way. The computation
2761 2766 # described above is not done directly in _filecommit
2762 2767 # when creating the list of changed files, however
2763 2768 # it does something very similar by comparing filelog
2764 2769 # nodes.
2765 2770 if f in m1:
2766 2771 return (f not in m2
2767 2772 and all(f in ma and ma.find(f) == m1.find(f)
2768 2773 for ma in mas()))
2769 2774 elif f in m2:
2770 2775 return all(f in ma and ma.find(f) == m2.find(f)
2771 2776 for ma in mas())
2772 2777 else:
2773 2778 return True
2774 2779 removed = [f for f in removed if not deletionfromparent(f)]
2775 2780
2776 2781 files = changed + removed
2777 2782 md = None
2778 2783 if not files:
2779 2784 # if no "files" actually changed in terms of the changelog,
2780 2785 # try hard to detect unmodified manifest entry so that the
2781 2786 # exact same commit can be reproduced later on convert.
2782 2787 md = m1.diff(m, scmutil.matchfiles(self, ctx.files()))
2783 2788 if not files and md:
2784 2789 self.ui.debug('not reusing manifest (no file change in '
2785 2790 'changelog, but manifest differs)\n')
2786 2791 if files or md:
2787 2792 self.ui.note(_("committing manifest\n"))
2788 2793 # we're using narrowmatch here since it's already applied at
2789 2794 # other stages (such as dirstate.walk), so we're already
2790 2795 # ignoring things outside of narrowspec in most cases. The
2791 2796 # one case where we might have files outside the narrowspec
2792 2797 # at this point is merges, and we already error out in the
2793 2798 # case where the merge has files outside of the narrowspec,
2794 2799 # so this is safe.
2795 2800 mn = mctx.write(trp, linkrev,
2796 2801 p1.manifestnode(), p2.manifestnode(),
2797 2802 added, drop, match=self.narrowmatch())
2798 2803
2799 2804 if writechangesetcopy:
2800 2805 filesadded = [f for f in changed
2801 2806 if not (f in m1 or f in m2)]
2802 2807 filesremoved = removed
2803 2808 else:
2804 2809 self.ui.debug('reusing manifest from p1 (listed files '
2805 2810 'actually unchanged)\n')
2806 2811 mn = p1.manifestnode()
2807 2812 else:
2808 2813 self.ui.debug('reusing manifest from p1 (no file change)\n')
2809 2814 mn = p1.manifestnode()
2810 2815 files = []
2811 2816
2812 2817 if writecopiesto == 'changeset-only':
2813 2818 # If writing only to changeset extras, use None to indicate that
2814 2819 # no entry should be written. If writing to both, write an empty
2815 2820 # entry to prevent the reader from falling back to reading
2816 2821 # filelogs.
2817 2822 p1copies = p1copies or None
2818 2823 p2copies = p2copies or None
2819 2824 filesadded = filesadded or None
2820 2825 filesremoved = filesremoved or None
2821 2826
2822 2827 if origctx and origctx.manifestnode() == mn:
2823 2828 files = origctx.files()
2824 2829
2825 2830 # update changelog
2826 2831 self.ui.note(_("committing changelog\n"))
2827 2832 self.changelog.delayupdate(tr)
2828 2833 n = self.changelog.add(mn, files, ctx.description(),
2829 2834 trp, p1.node(), p2.node(),
2830 2835 user, ctx.date(), ctx.extra().copy(),
2831 2836 p1copies, p2copies, filesadded, filesremoved)
2832 2837 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
2833 2838 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
2834 2839 parent2=xp2)
2835 2840 # set the new commit is proper phase
2836 2841 targetphase = subrepoutil.newcommitphase(self.ui, ctx)
2837 2842 if targetphase:
2838 2843 # retract boundary do not alter parent changeset.
2839 2844 # if a parent have higher the resulting phase will
2840 2845 # be compliant anyway
2841 2846 #
2842 2847 # if minimal phase was 0 we don't need to retract anything
2843 2848 phases.registernew(self, tr, targetphase, [n])
2844 2849 return n
2845 2850
2846 2851 @unfilteredmethod
2847 2852 def destroying(self):
2848 2853 '''Inform the repository that nodes are about to be destroyed.
2849 2854 Intended for use by strip and rollback, so there's a common
2850 2855 place for anything that has to be done before destroying history.
2851 2856
2852 2857 This is mostly useful for saving state that is in memory and waiting
2853 2858 to be flushed when the current lock is released. Because a call to
2854 2859 destroyed is imminent, the repo will be invalidated causing those
2855 2860 changes to stay in memory (waiting for the next unlock), or vanish
2856 2861 completely.
2857 2862 '''
2858 2863 # When using the same lock to commit and strip, the phasecache is left
2859 2864 # dirty after committing. Then when we strip, the repo is invalidated,
2860 2865 # causing those changes to disappear.
2861 2866 if '_phasecache' in vars(self):
2862 2867 self._phasecache.write()
2863 2868
2864 2869 @unfilteredmethod
2865 2870 def destroyed(self):
2866 2871 '''Inform the repository that nodes have been destroyed.
2867 2872 Intended for use by strip and rollback, so there's a common
2868 2873 place for anything that has to be done after destroying history.
2869 2874 '''
2870 2875 # When one tries to:
2871 2876 # 1) destroy nodes thus calling this method (e.g. strip)
2872 2877 # 2) use phasecache somewhere (e.g. commit)
2873 2878 #
2874 2879 # then 2) will fail because the phasecache contains nodes that were
2875 2880 # removed. We can either remove phasecache from the filecache,
2876 2881 # causing it to reload next time it is accessed, or simply filter
2877 2882 # the removed nodes now and write the updated cache.
2878 2883 self._phasecache.filterunknown(self)
2879 2884 self._phasecache.write()
2880 2885
2881 2886 # refresh all repository caches
2882 2887 self.updatecaches()
2883 2888
2884 2889 # Ensure the persistent tag cache is updated. Doing it now
2885 2890 # means that the tag cache only has to worry about destroyed
2886 2891 # heads immediately after a strip/rollback. That in turn
2887 2892 # guarantees that "cachetip == currenttip" (comparing both rev
2888 2893 # and node) always means no nodes have been added or destroyed.
2889 2894
2890 2895 # XXX this is suboptimal when qrefresh'ing: we strip the current
2891 2896 # head, refresh the tag cache, then immediately add a new head.
2892 2897 # But I think doing it this way is necessary for the "instant
2893 2898 # tag cache retrieval" case to work.
2894 2899 self.invalidate()
2895 2900
2896 2901 def status(self, node1='.', node2=None, match=None,
2897 2902 ignored=False, clean=False, unknown=False,
2898 2903 listsubrepos=False):
2899 2904 '''a convenience method that calls node1.status(node2)'''
2900 2905 return self[node1].status(node2, match, ignored, clean, unknown,
2901 2906 listsubrepos)
2902 2907
2903 2908 def addpostdsstatus(self, ps):
2904 2909 """Add a callback to run within the wlock, at the point at which status
2905 2910 fixups happen.
2906 2911
2907 2912 On status completion, callback(wctx, status) will be called with the
2908 2913 wlock held, unless the dirstate has changed from underneath or the wlock
2909 2914 couldn't be grabbed.
2910 2915
2911 2916 Callbacks should not capture and use a cached copy of the dirstate --
2912 2917 it might change in the meanwhile. Instead, they should access the
2913 2918 dirstate via wctx.repo().dirstate.
2914 2919
2915 2920 This list is emptied out after each status run -- extensions should
2916 2921 make sure it adds to this list each time dirstate.status is called.
2917 2922 Extensions should also make sure they don't call this for statuses
2918 2923 that don't involve the dirstate.
2919 2924 """
2920 2925
2921 2926 # The list is located here for uniqueness reasons -- it is actually
2922 2927 # managed by the workingctx, but that isn't unique per-repo.
2923 2928 self._postdsstatus.append(ps)
2924 2929
2925 2930 def postdsstatus(self):
2926 2931 """Used by workingctx to get the list of post-dirstate-status hooks."""
2927 2932 return self._postdsstatus
2928 2933
2929 2934 def clearpostdsstatus(self):
2930 2935 """Used by workingctx to clear post-dirstate-status hooks."""
2931 2936 del self._postdsstatus[:]
2932 2937
2933 2938 def heads(self, start=None):
2934 2939 if start is None:
2935 2940 cl = self.changelog
2936 2941 headrevs = reversed(cl.headrevs())
2937 2942 return [cl.node(rev) for rev in headrevs]
2938 2943
2939 2944 heads = self.changelog.heads(start)
2940 2945 # sort the output in rev descending order
2941 2946 return sorted(heads, key=self.changelog.rev, reverse=True)
2942 2947
2943 2948 def branchheads(self, branch=None, start=None, closed=False):
2944 2949 '''return a (possibly filtered) list of heads for the given branch
2945 2950
2946 2951 Heads are returned in topological order, from newest to oldest.
2947 2952 If branch is None, use the dirstate branch.
2948 2953 If start is not None, return only heads reachable from start.
2949 2954 If closed is True, return heads that are marked as closed as well.
2950 2955 '''
2951 2956 if branch is None:
2952 2957 branch = self[None].branch()
2953 2958 branches = self.branchmap()
2954 2959 if not branches.hasbranch(branch):
2955 2960 return []
2956 2961 # the cache returns heads ordered lowest to highest
2957 2962 bheads = list(reversed(branches.branchheads(branch, closed=closed)))
2958 2963 if start is not None:
2959 2964 # filter out the heads that cannot be reached from startrev
2960 2965 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
2961 2966 bheads = [h for h in bheads if h in fbheads]
2962 2967 return bheads
2963 2968
2964 2969 def branches(self, nodes):
2965 2970 if not nodes:
2966 2971 nodes = [self.changelog.tip()]
2967 2972 b = []
2968 2973 for n in nodes:
2969 2974 t = n
2970 2975 while True:
2971 2976 p = self.changelog.parents(n)
2972 2977 if p[1] != nullid or p[0] == nullid:
2973 2978 b.append((t, n, p[0], p[1]))
2974 2979 break
2975 2980 n = p[0]
2976 2981 return b
2977 2982
2978 2983 def between(self, pairs):
2979 2984 r = []
2980 2985
2981 2986 for top, bottom in pairs:
2982 2987 n, l, i = top, [], 0
2983 2988 f = 1
2984 2989
2985 2990 while n != bottom and n != nullid:
2986 2991 p = self.changelog.parents(n)[0]
2987 2992 if i == f:
2988 2993 l.append(n)
2989 2994 f = f * 2
2990 2995 n = p
2991 2996 i += 1
2992 2997
2993 2998 r.append(l)
2994 2999
2995 3000 return r
2996 3001
2997 3002 def checkpush(self, pushop):
2998 3003 """Extensions can override this function if additional checks have
2999 3004 to be performed before pushing, or call it if they override push
3000 3005 command.
3001 3006 """
3002 3007
3003 3008 @unfilteredpropertycache
3004 3009 def prepushoutgoinghooks(self):
3005 3010 """Return util.hooks consists of a pushop with repo, remote, outgoing
3006 3011 methods, which are called before pushing changesets.
3007 3012 """
3008 3013 return util.hooks()
3009 3014
3010 3015 def pushkey(self, namespace, key, old, new):
3011 3016 try:
3012 3017 tr = self.currenttransaction()
3013 3018 hookargs = {}
3014 3019 if tr is not None:
3015 3020 hookargs.update(tr.hookargs)
3016 3021 hookargs = pycompat.strkwargs(hookargs)
3017 3022 hookargs[r'namespace'] = namespace
3018 3023 hookargs[r'key'] = key
3019 3024 hookargs[r'old'] = old
3020 3025 hookargs[r'new'] = new
3021 3026 self.hook('prepushkey', throw=True, **hookargs)
3022 3027 except error.HookAbort as exc:
3023 3028 self.ui.write_err(_("pushkey-abort: %s\n") % exc)
3024 3029 if exc.hint:
3025 3030 self.ui.write_err(_("(%s)\n") % exc.hint)
3026 3031 return False
3027 3032 self.ui.debug('pushing key for "%s:%s"\n' % (namespace, key))
3028 3033 ret = pushkey.push(self, namespace, key, old, new)
3029 3034 def runhook():
3030 3035 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
3031 3036 ret=ret)
3032 3037 self._afterlock(runhook)
3033 3038 return ret
3034 3039
3035 3040 def listkeys(self, namespace):
3036 3041 self.hook('prelistkeys', throw=True, namespace=namespace)
3037 3042 self.ui.debug('listing keys for "%s"\n' % namespace)
3038 3043 values = pushkey.list(self, namespace)
3039 3044 self.hook('listkeys', namespace=namespace, values=values)
3040 3045 return values
3041 3046
3042 3047 def debugwireargs(self, one, two, three=None, four=None, five=None):
3043 3048 '''used to test argument passing over the wire'''
3044 3049 return "%s %s %s %s %s" % (one, two, pycompat.bytestr(three),
3045 3050 pycompat.bytestr(four),
3046 3051 pycompat.bytestr(five))
3047 3052
3048 3053 def savecommitmessage(self, text):
3049 3054 fp = self.vfs('last-message.txt', 'wb')
3050 3055 try:
3051 3056 fp.write(text)
3052 3057 finally:
3053 3058 fp.close()
3054 3059 return self.pathto(fp.name[len(self.root) + 1:])
3055 3060
3056 3061 # used to avoid circular references so destructors work
3057 3062 def aftertrans(files):
3058 3063 renamefiles = [tuple(t) for t in files]
3059 3064 def a():
3060 3065 for vfs, src, dest in renamefiles:
3061 3066 # if src and dest refer to a same file, vfs.rename is a no-op,
3062 3067 # leaving both src and dest on disk. delete dest to make sure
3063 3068 # the rename couldn't be such a no-op.
3064 3069 vfs.tryunlink(dest)
3065 3070 try:
3066 3071 vfs.rename(src, dest)
3067 3072 except OSError: # journal file does not yet exist
3068 3073 pass
3069 3074 return a
3070 3075
3071 3076 def undoname(fn):
3072 3077 base, name = os.path.split(fn)
3073 3078 assert name.startswith('journal')
3074 3079 return os.path.join(base, name.replace('journal', 'undo', 1))
3075 3080
3076 3081 def instance(ui, path, create, intents=None, createopts=None):
3077 3082 localpath = util.urllocalpath(path)
3078 3083 if create:
3079 3084 createrepository(ui, localpath, createopts=createopts)
3080 3085
3081 3086 return makelocalrepository(ui, localpath, intents=intents)
3082 3087
3083 3088 def islocal(path):
3084 3089 return True
3085 3090
3086 3091 def defaultcreateopts(ui, createopts=None):
3087 3092 """Populate the default creation options for a repository.
3088 3093
3089 3094 A dictionary of explicitly requested creation options can be passed
3090 3095 in. Missing keys will be populated.
3091 3096 """
3092 3097 createopts = dict(createopts or {})
3093 3098
3094 3099 if 'backend' not in createopts:
3095 3100 # experimental config: storage.new-repo-backend
3096 3101 createopts['backend'] = ui.config('storage', 'new-repo-backend')
3097 3102
3098 3103 return createopts
3099 3104
3100 3105 def newreporequirements(ui, createopts):
3101 3106 """Determine the set of requirements for a new local repository.
3102 3107
3103 3108 Extensions can wrap this function to specify custom requirements for
3104 3109 new repositories.
3105 3110 """
3106 3111 # If the repo is being created from a shared repository, we copy
3107 3112 # its requirements.
3108 3113 if 'sharedrepo' in createopts:
3109 3114 requirements = set(createopts['sharedrepo'].requirements)
3110 3115 if createopts.get('sharedrelative'):
3111 3116 requirements.add('relshared')
3112 3117 else:
3113 3118 requirements.add('shared')
3114 3119
3115 3120 return requirements
3116 3121
3117 3122 if 'backend' not in createopts:
3118 3123 raise error.ProgrammingError('backend key not present in createopts; '
3119 3124 'was defaultcreateopts() called?')
3120 3125
3121 3126 if createopts['backend'] != 'revlogv1':
3122 3127 raise error.Abort(_('unable to determine repository requirements for '
3123 3128 'storage backend: %s') % createopts['backend'])
3124 3129
3125 3130 requirements = {'revlogv1'}
3126 3131 if ui.configbool('format', 'usestore'):
3127 3132 requirements.add('store')
3128 3133 if ui.configbool('format', 'usefncache'):
3129 3134 requirements.add('fncache')
3130 3135 if ui.configbool('format', 'dotencode'):
3131 3136 requirements.add('dotencode')
3132 3137
3133 3138 compengine = ui.config('format', 'revlog-compression')
3134 3139 if compengine not in util.compengines:
3135 3140 raise error.Abort(_('compression engine %s defined by '
3136 3141 'format.revlog-compression not available') %
3137 3142 compengine,
3138 3143 hint=_('run "hg debuginstall" to list available '
3139 3144 'compression engines'))
3140 3145
3141 3146 # zlib is the historical default and doesn't need an explicit requirement.
3142 3147 elif compengine == 'zstd':
3143 3148 requirements.add('revlog-compression-zstd')
3144 3149 elif compengine != 'zlib':
3145 3150 requirements.add('exp-compression-%s' % compengine)
3146 3151
3147 3152 if scmutil.gdinitconfig(ui):
3148 3153 requirements.add('generaldelta')
3149 3154 if ui.configbool('format', 'sparse-revlog'):
3150 3155 requirements.add(SPARSEREVLOG_REQUIREMENT)
3151 3156 if ui.configbool('experimental', 'treemanifest'):
3152 3157 requirements.add('treemanifest')
3153 3158
3154 3159 revlogv2 = ui.config('experimental', 'revlogv2')
3155 3160 if revlogv2 == 'enable-unstable-format-and-corrupt-my-data':
3156 3161 requirements.remove('revlogv1')
3157 3162 # generaldelta is implied by revlogv2.
3158 3163 requirements.discard('generaldelta')
3159 3164 requirements.add(REVLOGV2_REQUIREMENT)
3160 3165 # experimental config: format.internal-phase
3161 3166 if ui.configbool('format', 'internal-phase'):
3162 3167 requirements.add('internal-phase')
3163 3168
3164 3169 if createopts.get('narrowfiles'):
3165 3170 requirements.add(repository.NARROW_REQUIREMENT)
3166 3171
3167 3172 if createopts.get('lfs'):
3168 3173 requirements.add('lfs')
3169 3174
3170 3175 if ui.configbool('format', 'bookmarks-in-store'):
3171 3176 requirements.add(bookmarks.BOOKMARKS_IN_STORE_REQUIREMENT)
3172 3177
3173 3178 return requirements
3174 3179
3175 3180 def filterknowncreateopts(ui, createopts):
3176 3181 """Filters a dict of repo creation options against options that are known.
3177 3182
3178 3183 Receives a dict of repo creation options and returns a dict of those
3179 3184 options that we don't know how to handle.
3180 3185
3181 3186 This function is called as part of repository creation. If the
3182 3187 returned dict contains any items, repository creation will not
3183 3188 be allowed, as it means there was a request to create a repository
3184 3189 with options not recognized by loaded code.
3185 3190
3186 3191 Extensions can wrap this function to filter out creation options
3187 3192 they know how to handle.
3188 3193 """
3189 3194 known = {
3190 3195 'backend',
3191 3196 'lfs',
3192 3197 'narrowfiles',
3193 3198 'sharedrepo',
3194 3199 'sharedrelative',
3195 3200 'shareditems',
3196 3201 'shallowfilestore',
3197 3202 }
3198 3203
3199 3204 return {k: v for k, v in createopts.items() if k not in known}
3200 3205
3201 3206 def createrepository(ui, path, createopts=None):
3202 3207 """Create a new repository in a vfs.
3203 3208
3204 3209 ``path`` path to the new repo's working directory.
3205 3210 ``createopts`` options for the new repository.
3206 3211
3207 3212 The following keys for ``createopts`` are recognized:
3208 3213
3209 3214 backend
3210 3215 The storage backend to use.
3211 3216 lfs
3212 3217 Repository will be created with ``lfs`` requirement. The lfs extension
3213 3218 will automatically be loaded when the repository is accessed.
3214 3219 narrowfiles
3215 3220 Set up repository to support narrow file storage.
3216 3221 sharedrepo
3217 3222 Repository object from which storage should be shared.
3218 3223 sharedrelative
3219 3224 Boolean indicating if the path to the shared repo should be
3220 3225 stored as relative. By default, the pointer to the "parent" repo
3221 3226 is stored as an absolute path.
3222 3227 shareditems
3223 3228 Set of items to share to the new repository (in addition to storage).
3224 3229 shallowfilestore
3225 3230 Indicates that storage for files should be shallow (not all ancestor
3226 3231 revisions are known).
3227 3232 """
3228 3233 createopts = defaultcreateopts(ui, createopts=createopts)
3229 3234
3230 3235 unknownopts = filterknowncreateopts(ui, createopts)
3231 3236
3232 3237 if not isinstance(unknownopts, dict):
3233 3238 raise error.ProgrammingError('filterknowncreateopts() did not return '
3234 3239 'a dict')
3235 3240
3236 3241 if unknownopts:
3237 3242 raise error.Abort(_('unable to create repository because of unknown '
3238 3243 'creation option: %s') %
3239 3244 ', '.join(sorted(unknownopts)),
3240 3245 hint=_('is a required extension not loaded?'))
3241 3246
3242 3247 requirements = newreporequirements(ui, createopts=createopts)
3243 3248
3244 3249 wdirvfs = vfsmod.vfs(path, expandpath=True, realpath=True)
3245 3250
3246 3251 hgvfs = vfsmod.vfs(wdirvfs.join(b'.hg'))
3247 3252 if hgvfs.exists():
3248 3253 raise error.RepoError(_('repository %s already exists') % path)
3249 3254
3250 3255 if 'sharedrepo' in createopts:
3251 3256 sharedpath = createopts['sharedrepo'].sharedpath
3252 3257
3253 3258 if createopts.get('sharedrelative'):
3254 3259 try:
3255 3260 sharedpath = os.path.relpath(sharedpath, hgvfs.base)
3256 3261 except (IOError, ValueError) as e:
3257 3262 # ValueError is raised on Windows if the drive letters differ
3258 3263 # on each path.
3259 3264 raise error.Abort(_('cannot calculate relative path'),
3260 3265 hint=stringutil.forcebytestr(e))
3261 3266
3262 3267 if not wdirvfs.exists():
3263 3268 wdirvfs.makedirs()
3264 3269
3265 3270 hgvfs.makedir(notindexed=True)
3266 3271 if 'sharedrepo' not in createopts:
3267 3272 hgvfs.mkdir(b'cache')
3268 3273 hgvfs.mkdir(b'wcache')
3269 3274
3270 3275 if b'store' in requirements and 'sharedrepo' not in createopts:
3271 3276 hgvfs.mkdir(b'store')
3272 3277
3273 3278 # We create an invalid changelog outside the store so very old
3274 3279 # Mercurial versions (which didn't know about the requirements
3275 3280 # file) encounter an error on reading the changelog. This
3276 3281 # effectively locks out old clients and prevents them from
3277 3282 # mucking with a repo in an unknown format.
3278 3283 #
3279 3284 # The revlog header has version 2, which won't be recognized by
3280 3285 # such old clients.
3281 3286 hgvfs.append(b'00changelog.i',
3282 3287 b'\0\0\0\2 dummy changelog to prevent using the old repo '
3283 3288 b'layout')
3284 3289
3285 3290 scmutil.writerequires(hgvfs, requirements)
3286 3291
3287 3292 # Write out file telling readers where to find the shared store.
3288 3293 if 'sharedrepo' in createopts:
3289 3294 hgvfs.write(b'sharedpath', sharedpath)
3290 3295
3291 3296 if createopts.get('shareditems'):
3292 3297 shared = b'\n'.join(sorted(createopts['shareditems'])) + b'\n'
3293 3298 hgvfs.write(b'shared', shared)
3294 3299
3295 3300 def poisonrepository(repo):
3296 3301 """Poison a repository instance so it can no longer be used."""
3297 3302 # Perform any cleanup on the instance.
3298 3303 repo.close()
3299 3304
3300 3305 # Our strategy is to replace the type of the object with one that
3301 3306 # has all attribute lookups result in error.
3302 3307 #
3303 3308 # But we have to allow the close() method because some constructors
3304 3309 # of repos call close() on repo references.
3305 3310 class poisonedrepository(object):
3306 3311 def __getattribute__(self, item):
3307 3312 if item == r'close':
3308 3313 return object.__getattribute__(self, item)
3309 3314
3310 3315 raise error.ProgrammingError('repo instances should not be used '
3311 3316 'after unshare')
3312 3317
3313 3318 def close(self):
3314 3319 pass
3315 3320
3316 3321 # We may have a repoview, which intercepts __setattr__. So be sure
3317 3322 # we operate at the lowest level possible.
3318 3323 object.__setattr__(repo, r'__class__', poisonedrepository)
General Comments 0
You need to be logged in to leave comments. Login now