##// END OF EJS Templates
manifest: adds manifestctx.readfast...
Durham Goode -
r29939:80be4436 default
parent child Browse files
Show More
@@ -1,1985 +1,1985 b''
1 1 # context.py - changeset and file context objects for mercurial
2 2 #
3 3 # Copyright 2006, 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 os
12 12 import re
13 13 import stat
14 14
15 15 from .i18n import _
16 16 from .node import (
17 17 bin,
18 18 hex,
19 19 nullid,
20 20 nullrev,
21 21 short,
22 22 wdirid,
23 23 )
24 24 from . import (
25 25 encoding,
26 26 error,
27 27 fileset,
28 28 match as matchmod,
29 29 mdiff,
30 30 obsolete as obsmod,
31 31 patch,
32 32 phases,
33 33 repoview,
34 34 revlog,
35 35 scmutil,
36 36 subrepo,
37 37 util,
38 38 )
39 39
40 40 propertycache = util.propertycache
41 41
42 42 # Phony node value to stand-in for new files in some uses of
43 43 # manifests. Manifests support 21-byte hashes for nodes which are
44 44 # dirty in the working copy.
45 45 _newnode = '!' * 21
46 46
47 47 nonascii = re.compile(r'[^\x21-\x7f]').search
48 48
49 49 class basectx(object):
50 50 """A basectx object represents the common logic for its children:
51 51 changectx: read-only context that is already present in the repo,
52 52 workingctx: a context that represents the working directory and can
53 53 be committed,
54 54 memctx: a context that represents changes in-memory and can also
55 55 be committed."""
56 56 def __new__(cls, repo, changeid='', *args, **kwargs):
57 57 if isinstance(changeid, basectx):
58 58 return changeid
59 59
60 60 o = super(basectx, cls).__new__(cls)
61 61
62 62 o._repo = repo
63 63 o._rev = nullrev
64 64 o._node = nullid
65 65
66 66 return o
67 67
68 68 def __str__(self):
69 69 return short(self.node())
70 70
71 71 def __int__(self):
72 72 return self.rev()
73 73
74 74 def __repr__(self):
75 75 return "<%s %s>" % (type(self).__name__, str(self))
76 76
77 77 def __eq__(self, other):
78 78 try:
79 79 return type(self) == type(other) and self._rev == other._rev
80 80 except AttributeError:
81 81 return False
82 82
83 83 def __ne__(self, other):
84 84 return not (self == other)
85 85
86 86 def __contains__(self, key):
87 87 return key in self._manifest
88 88
89 89 def __getitem__(self, key):
90 90 return self.filectx(key)
91 91
92 92 def __iter__(self):
93 93 return iter(self._manifest)
94 94
95 95 def _manifestmatches(self, match, s):
96 96 """generate a new manifest filtered by the match argument
97 97
98 98 This method is for internal use only and mainly exists to provide an
99 99 object oriented way for other contexts to customize the manifest
100 100 generation.
101 101 """
102 102 return self.manifest().matches(match)
103 103
104 104 def _matchstatus(self, other, match):
105 105 """return match.always if match is none
106 106
107 107 This internal method provides a way for child objects to override the
108 108 match operator.
109 109 """
110 110 return match or matchmod.always(self._repo.root, self._repo.getcwd())
111 111
112 112 def _buildstatus(self, other, s, match, listignored, listclean,
113 113 listunknown):
114 114 """build a status with respect to another context"""
115 115 # Load earliest manifest first for caching reasons. More specifically,
116 116 # if you have revisions 1000 and 1001, 1001 is probably stored as a
117 117 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
118 118 # 1000 and cache it so that when you read 1001, we just need to apply a
119 119 # delta to what's in the cache. So that's one full reconstruction + one
120 120 # delta application.
121 121 if self.rev() is not None and self.rev() < other.rev():
122 122 self.manifest()
123 123 mf1 = other._manifestmatches(match, s)
124 124 mf2 = self._manifestmatches(match, s)
125 125
126 126 modified, added = [], []
127 127 removed = []
128 128 clean = []
129 129 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
130 130 deletedset = set(deleted)
131 131 d = mf1.diff(mf2, clean=listclean)
132 132 for fn, value in d.iteritems():
133 133 if fn in deletedset:
134 134 continue
135 135 if value is None:
136 136 clean.append(fn)
137 137 continue
138 138 (node1, flag1), (node2, flag2) = value
139 139 if node1 is None:
140 140 added.append(fn)
141 141 elif node2 is None:
142 142 removed.append(fn)
143 143 elif flag1 != flag2:
144 144 modified.append(fn)
145 145 elif node2 != _newnode:
146 146 # When comparing files between two commits, we save time by
147 147 # not comparing the file contents when the nodeids differ.
148 148 # Note that this means we incorrectly report a reverted change
149 149 # to a file as a modification.
150 150 modified.append(fn)
151 151 elif self[fn].cmp(other[fn]):
152 152 modified.append(fn)
153 153 else:
154 154 clean.append(fn)
155 155
156 156 if removed:
157 157 # need to filter files if they are already reported as removed
158 158 unknown = [fn for fn in unknown if fn not in mf1]
159 159 ignored = [fn for fn in ignored if fn not in mf1]
160 160 # if they're deleted, don't report them as removed
161 161 removed = [fn for fn in removed if fn not in deletedset]
162 162
163 163 return scmutil.status(modified, added, removed, deleted, unknown,
164 164 ignored, clean)
165 165
166 166 @propertycache
167 167 def substate(self):
168 168 return subrepo.state(self, self._repo.ui)
169 169
170 170 def subrev(self, subpath):
171 171 return self.substate[subpath][1]
172 172
173 173 def rev(self):
174 174 return self._rev
175 175 def node(self):
176 176 return self._node
177 177 def hex(self):
178 178 return hex(self.node())
179 179 def manifest(self):
180 180 return self._manifest
181 181 def repo(self):
182 182 return self._repo
183 183 def phasestr(self):
184 184 return phases.phasenames[self.phase()]
185 185 def mutable(self):
186 186 return self.phase() > phases.public
187 187
188 188 def getfileset(self, expr):
189 189 return fileset.getfileset(self, expr)
190 190
191 191 def obsolete(self):
192 192 """True if the changeset is obsolete"""
193 193 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
194 194
195 195 def extinct(self):
196 196 """True if the changeset is extinct"""
197 197 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
198 198
199 199 def unstable(self):
200 200 """True if the changeset is not obsolete but it's ancestor are"""
201 201 return self.rev() in obsmod.getrevs(self._repo, 'unstable')
202 202
203 203 def bumped(self):
204 204 """True if the changeset try to be a successor of a public changeset
205 205
206 206 Only non-public and non-obsolete changesets may be bumped.
207 207 """
208 208 return self.rev() in obsmod.getrevs(self._repo, 'bumped')
209 209
210 210 def divergent(self):
211 211 """Is a successors of a changeset with multiple possible successors set
212 212
213 213 Only non-public and non-obsolete changesets may be divergent.
214 214 """
215 215 return self.rev() in obsmod.getrevs(self._repo, 'divergent')
216 216
217 217 def troubled(self):
218 218 """True if the changeset is either unstable, bumped or divergent"""
219 219 return self.unstable() or self.bumped() or self.divergent()
220 220
221 221 def troubles(self):
222 222 """return the list of troubles affecting this changesets.
223 223
224 224 Troubles are returned as strings. possible values are:
225 225 - unstable,
226 226 - bumped,
227 227 - divergent.
228 228 """
229 229 troubles = []
230 230 if self.unstable():
231 231 troubles.append('unstable')
232 232 if self.bumped():
233 233 troubles.append('bumped')
234 234 if self.divergent():
235 235 troubles.append('divergent')
236 236 return troubles
237 237
238 238 def parents(self):
239 239 """return contexts for each parent changeset"""
240 240 return self._parents
241 241
242 242 def p1(self):
243 243 return self._parents[0]
244 244
245 245 def p2(self):
246 246 parents = self._parents
247 247 if len(parents) == 2:
248 248 return parents[1]
249 249 return changectx(self._repo, nullrev)
250 250
251 251 def _fileinfo(self, path):
252 252 if '_manifest' in self.__dict__:
253 253 try:
254 254 return self._manifest[path], self._manifest.flags(path)
255 255 except KeyError:
256 256 raise error.ManifestLookupError(self._node, path,
257 257 _('not found in manifest'))
258 258 if '_manifestdelta' in self.__dict__ or path in self.files():
259 259 if path in self._manifestdelta:
260 260 return (self._manifestdelta[path],
261 261 self._manifestdelta.flags(path))
262 262 node, flag = self._repo.manifest.find(self._changeset.manifest, path)
263 263 if not node:
264 264 raise error.ManifestLookupError(self._node, path,
265 265 _('not found in manifest'))
266 266
267 267 return node, flag
268 268
269 269 def filenode(self, path):
270 270 return self._fileinfo(path)[0]
271 271
272 272 def flags(self, path):
273 273 try:
274 274 return self._fileinfo(path)[1]
275 275 except error.LookupError:
276 276 return ''
277 277
278 278 def sub(self, path, allowcreate=True):
279 279 '''return a subrepo for the stored revision of path, never wdir()'''
280 280 return subrepo.subrepo(self, path, allowcreate=allowcreate)
281 281
282 282 def nullsub(self, path, pctx):
283 283 return subrepo.nullsubrepo(self, path, pctx)
284 284
285 285 def workingsub(self, path):
286 286 '''return a subrepo for the stored revision, or wdir if this is a wdir
287 287 context.
288 288 '''
289 289 return subrepo.subrepo(self, path, allowwdir=True)
290 290
291 291 def match(self, pats=[], include=None, exclude=None, default='glob',
292 292 listsubrepos=False, badfn=None):
293 293 r = self._repo
294 294 return matchmod.match(r.root, r.getcwd(), pats,
295 295 include, exclude, default,
296 296 auditor=r.nofsauditor, ctx=self,
297 297 listsubrepos=listsubrepos, badfn=badfn)
298 298
299 299 def diff(self, ctx2=None, match=None, **opts):
300 300 """Returns a diff generator for the given contexts and matcher"""
301 301 if ctx2 is None:
302 302 ctx2 = self.p1()
303 303 if ctx2 is not None:
304 304 ctx2 = self._repo[ctx2]
305 305 diffopts = patch.diffopts(self._repo.ui, opts)
306 306 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
307 307
308 308 def dirs(self):
309 309 return self._manifest.dirs()
310 310
311 311 def hasdir(self, dir):
312 312 return self._manifest.hasdir(dir)
313 313
314 314 def dirty(self, missing=False, merge=True, branch=True):
315 315 return False
316 316
317 317 def status(self, other=None, match=None, listignored=False,
318 318 listclean=False, listunknown=False, listsubrepos=False):
319 319 """return status of files between two nodes or node and working
320 320 directory.
321 321
322 322 If other is None, compare this node with working directory.
323 323
324 324 returns (modified, added, removed, deleted, unknown, ignored, clean)
325 325 """
326 326
327 327 ctx1 = self
328 328 ctx2 = self._repo[other]
329 329
330 330 # This next code block is, admittedly, fragile logic that tests for
331 331 # reversing the contexts and wouldn't need to exist if it weren't for
332 332 # the fast (and common) code path of comparing the working directory
333 333 # with its first parent.
334 334 #
335 335 # What we're aiming for here is the ability to call:
336 336 #
337 337 # workingctx.status(parentctx)
338 338 #
339 339 # If we always built the manifest for each context and compared those,
340 340 # then we'd be done. But the special case of the above call means we
341 341 # just copy the manifest of the parent.
342 342 reversed = False
343 343 if (not isinstance(ctx1, changectx)
344 344 and isinstance(ctx2, changectx)):
345 345 reversed = True
346 346 ctx1, ctx2 = ctx2, ctx1
347 347
348 348 match = ctx2._matchstatus(ctx1, match)
349 349 r = scmutil.status([], [], [], [], [], [], [])
350 350 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
351 351 listunknown)
352 352
353 353 if reversed:
354 354 # Reverse added and removed. Clear deleted, unknown and ignored as
355 355 # these make no sense to reverse.
356 356 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
357 357 r.clean)
358 358
359 359 if listsubrepos:
360 360 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
361 361 try:
362 362 rev2 = ctx2.subrev(subpath)
363 363 except KeyError:
364 364 # A subrepo that existed in node1 was deleted between
365 365 # node1 and node2 (inclusive). Thus, ctx2's substate
366 366 # won't contain that subpath. The best we can do ignore it.
367 367 rev2 = None
368 368 submatch = matchmod.subdirmatcher(subpath, match)
369 369 s = sub.status(rev2, match=submatch, ignored=listignored,
370 370 clean=listclean, unknown=listunknown,
371 371 listsubrepos=True)
372 372 for rfiles, sfiles in zip(r, s):
373 373 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
374 374
375 375 for l in r:
376 376 l.sort()
377 377
378 378 return r
379 379
380 380
381 381 def makememctx(repo, parents, text, user, date, branch, files, store,
382 382 editor=None, extra=None):
383 383 def getfilectx(repo, memctx, path):
384 384 data, mode, copied = store.getfile(path)
385 385 if data is None:
386 386 return None
387 387 islink, isexec = mode
388 388 return memfilectx(repo, path, data, islink=islink, isexec=isexec,
389 389 copied=copied, memctx=memctx)
390 390 if extra is None:
391 391 extra = {}
392 392 if branch:
393 393 extra['branch'] = encoding.fromlocal(branch)
394 394 ctx = memctx(repo, parents, text, files, getfilectx, user,
395 395 date, extra, editor)
396 396 return ctx
397 397
398 398 class changectx(basectx):
399 399 """A changecontext object makes access to data related to a particular
400 400 changeset convenient. It represents a read-only context already present in
401 401 the repo."""
402 402 def __init__(self, repo, changeid=''):
403 403 """changeid is a revision number, node, or tag"""
404 404
405 405 # since basectx.__new__ already took care of copying the object, we
406 406 # don't need to do anything in __init__, so we just exit here
407 407 if isinstance(changeid, basectx):
408 408 return
409 409
410 410 if changeid == '':
411 411 changeid = '.'
412 412 self._repo = repo
413 413
414 414 try:
415 415 if isinstance(changeid, int):
416 416 self._node = repo.changelog.node(changeid)
417 417 self._rev = changeid
418 418 return
419 419 if isinstance(changeid, long):
420 420 changeid = str(changeid)
421 421 if changeid == 'null':
422 422 self._node = nullid
423 423 self._rev = nullrev
424 424 return
425 425 if changeid == 'tip':
426 426 self._node = repo.changelog.tip()
427 427 self._rev = repo.changelog.rev(self._node)
428 428 return
429 429 if changeid == '.' or changeid == repo.dirstate.p1():
430 430 # this is a hack to delay/avoid loading obsmarkers
431 431 # when we know that '.' won't be hidden
432 432 self._node = repo.dirstate.p1()
433 433 self._rev = repo.unfiltered().changelog.rev(self._node)
434 434 return
435 435 if len(changeid) == 20:
436 436 try:
437 437 self._node = changeid
438 438 self._rev = repo.changelog.rev(changeid)
439 439 return
440 440 except error.FilteredRepoLookupError:
441 441 raise
442 442 except LookupError:
443 443 pass
444 444
445 445 try:
446 446 r = int(changeid)
447 447 if str(r) != changeid:
448 448 raise ValueError
449 449 l = len(repo.changelog)
450 450 if r < 0:
451 451 r += l
452 452 if r < 0 or r >= l:
453 453 raise ValueError
454 454 self._rev = r
455 455 self._node = repo.changelog.node(r)
456 456 return
457 457 except error.FilteredIndexError:
458 458 raise
459 459 except (ValueError, OverflowError, IndexError):
460 460 pass
461 461
462 462 if len(changeid) == 40:
463 463 try:
464 464 self._node = bin(changeid)
465 465 self._rev = repo.changelog.rev(self._node)
466 466 return
467 467 except error.FilteredLookupError:
468 468 raise
469 469 except (TypeError, LookupError):
470 470 pass
471 471
472 472 # lookup bookmarks through the name interface
473 473 try:
474 474 self._node = repo.names.singlenode(repo, changeid)
475 475 self._rev = repo.changelog.rev(self._node)
476 476 return
477 477 except KeyError:
478 478 pass
479 479 except error.FilteredRepoLookupError:
480 480 raise
481 481 except error.RepoLookupError:
482 482 pass
483 483
484 484 self._node = repo.unfiltered().changelog._partialmatch(changeid)
485 485 if self._node is not None:
486 486 self._rev = repo.changelog.rev(self._node)
487 487 return
488 488
489 489 # lookup failed
490 490 # check if it might have come from damaged dirstate
491 491 #
492 492 # XXX we could avoid the unfiltered if we had a recognizable
493 493 # exception for filtered changeset access
494 494 if changeid in repo.unfiltered().dirstate.parents():
495 495 msg = _("working directory has unknown parent '%s'!")
496 496 raise error.Abort(msg % short(changeid))
497 497 try:
498 498 if len(changeid) == 20 and nonascii(changeid):
499 499 changeid = hex(changeid)
500 500 except TypeError:
501 501 pass
502 502 except (error.FilteredIndexError, error.FilteredLookupError,
503 503 error.FilteredRepoLookupError):
504 504 if repo.filtername.startswith('visible'):
505 505 msg = _("hidden revision '%s'") % changeid
506 506 hint = _('use --hidden to access hidden revisions')
507 507 raise error.FilteredRepoLookupError(msg, hint=hint)
508 508 msg = _("filtered revision '%s' (not in '%s' subset)")
509 509 msg %= (changeid, repo.filtername)
510 510 raise error.FilteredRepoLookupError(msg)
511 511 except IndexError:
512 512 pass
513 513 raise error.RepoLookupError(
514 514 _("unknown revision '%s'") % changeid)
515 515
516 516 def __hash__(self):
517 517 try:
518 518 return hash(self._rev)
519 519 except AttributeError:
520 520 return id(self)
521 521
522 522 def __nonzero__(self):
523 523 return self._rev != nullrev
524 524
525 525 @propertycache
526 526 def _changeset(self):
527 527 return self._repo.changelog.changelogrevision(self.rev())
528 528
529 529 @propertycache
530 530 def _manifest(self):
531 531 return self._repo.manifestlog[self._changeset.manifest].read()
532 532
533 533 @propertycache
534 534 def _manifestdelta(self):
535 535 mfnode = self._changeset.manifest
536 536 return self._repo.manifestlog[mfnode].readdelta()
537 537
538 538 @propertycache
539 539 def _parents(self):
540 540 repo = self._repo
541 541 p1, p2 = repo.changelog.parentrevs(self._rev)
542 542 if p2 == nullrev:
543 543 return [changectx(repo, p1)]
544 544 return [changectx(repo, p1), changectx(repo, p2)]
545 545
546 546 def changeset(self):
547 547 c = self._changeset
548 548 return (
549 549 c.manifest,
550 550 c.user,
551 551 c.date,
552 552 c.files,
553 553 c.description,
554 554 c.extra,
555 555 )
556 556 def manifestnode(self):
557 557 return self._changeset.manifest
558 558
559 559 def user(self):
560 560 return self._changeset.user
561 561 def date(self):
562 562 return self._changeset.date
563 563 def files(self):
564 564 return self._changeset.files
565 565 def description(self):
566 566 return self._changeset.description
567 567 def branch(self):
568 568 return encoding.tolocal(self._changeset.extra.get("branch"))
569 569 def closesbranch(self):
570 570 return 'close' in self._changeset.extra
571 571 def extra(self):
572 572 return self._changeset.extra
573 573 def tags(self):
574 574 return self._repo.nodetags(self._node)
575 575 def bookmarks(self):
576 576 return self._repo.nodebookmarks(self._node)
577 577 def phase(self):
578 578 return self._repo._phasecache.phase(self._repo, self._rev)
579 579 def hidden(self):
580 580 return self._rev in repoview.filterrevs(self._repo, 'visible')
581 581
582 582 def children(self):
583 583 """return contexts for each child changeset"""
584 584 c = self._repo.changelog.children(self._node)
585 585 return [changectx(self._repo, x) for x in c]
586 586
587 587 def ancestors(self):
588 588 for a in self._repo.changelog.ancestors([self._rev]):
589 589 yield changectx(self._repo, a)
590 590
591 591 def descendants(self):
592 592 for d in self._repo.changelog.descendants([self._rev]):
593 593 yield changectx(self._repo, d)
594 594
595 595 def filectx(self, path, fileid=None, filelog=None):
596 596 """get a file context from this changeset"""
597 597 if fileid is None:
598 598 fileid = self.filenode(path)
599 599 return filectx(self._repo, path, fileid=fileid,
600 600 changectx=self, filelog=filelog)
601 601
602 602 def ancestor(self, c2, warn=False):
603 603 """return the "best" ancestor context of self and c2
604 604
605 605 If there are multiple candidates, it will show a message and check
606 606 merge.preferancestor configuration before falling back to the
607 607 revlog ancestor."""
608 608 # deal with workingctxs
609 609 n2 = c2._node
610 610 if n2 is None:
611 611 n2 = c2._parents[0]._node
612 612 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
613 613 if not cahs:
614 614 anc = nullid
615 615 elif len(cahs) == 1:
616 616 anc = cahs[0]
617 617 else:
618 618 # experimental config: merge.preferancestor
619 619 for r in self._repo.ui.configlist('merge', 'preferancestor', ['*']):
620 620 try:
621 621 ctx = changectx(self._repo, r)
622 622 except error.RepoLookupError:
623 623 continue
624 624 anc = ctx.node()
625 625 if anc in cahs:
626 626 break
627 627 else:
628 628 anc = self._repo.changelog.ancestor(self._node, n2)
629 629 if warn:
630 630 self._repo.ui.status(
631 631 (_("note: using %s as ancestor of %s and %s\n") %
632 632 (short(anc), short(self._node), short(n2))) +
633 633 ''.join(_(" alternatively, use --config "
634 634 "merge.preferancestor=%s\n") %
635 635 short(n) for n in sorted(cahs) if n != anc))
636 636 return changectx(self._repo, anc)
637 637
638 638 def descendant(self, other):
639 639 """True if other is descendant of this changeset"""
640 640 return self._repo.changelog.descendant(self._rev, other._rev)
641 641
642 642 def walk(self, match):
643 643 '''Generates matching file names.'''
644 644
645 645 # Wrap match.bad method to have message with nodeid
646 646 def bad(fn, msg):
647 647 # The manifest doesn't know about subrepos, so don't complain about
648 648 # paths into valid subrepos.
649 649 if any(fn == s or fn.startswith(s + '/')
650 650 for s in self.substate):
651 651 return
652 652 match.bad(fn, _('no such file in rev %s') % self)
653 653
654 654 m = matchmod.badmatch(match, bad)
655 655 return self._manifest.walk(m)
656 656
657 657 def matches(self, match):
658 658 return self.walk(match)
659 659
660 660 class basefilectx(object):
661 661 """A filecontext object represents the common logic for its children:
662 662 filectx: read-only access to a filerevision that is already present
663 663 in the repo,
664 664 workingfilectx: a filecontext that represents files from the working
665 665 directory,
666 666 memfilectx: a filecontext that represents files in-memory."""
667 667 def __new__(cls, repo, path, *args, **kwargs):
668 668 return super(basefilectx, cls).__new__(cls)
669 669
670 670 @propertycache
671 671 def _filelog(self):
672 672 return self._repo.file(self._path)
673 673
674 674 @propertycache
675 675 def _changeid(self):
676 676 if '_changeid' in self.__dict__:
677 677 return self._changeid
678 678 elif '_changectx' in self.__dict__:
679 679 return self._changectx.rev()
680 680 elif '_descendantrev' in self.__dict__:
681 681 # this file context was created from a revision with a known
682 682 # descendant, we can (lazily) correct for linkrev aliases
683 683 return self._adjustlinkrev(self._path, self._filelog,
684 684 self._filenode, self._descendantrev)
685 685 else:
686 686 return self._filelog.linkrev(self._filerev)
687 687
688 688 @propertycache
689 689 def _filenode(self):
690 690 if '_fileid' in self.__dict__:
691 691 return self._filelog.lookup(self._fileid)
692 692 else:
693 693 return self._changectx.filenode(self._path)
694 694
695 695 @propertycache
696 696 def _filerev(self):
697 697 return self._filelog.rev(self._filenode)
698 698
699 699 @propertycache
700 700 def _repopath(self):
701 701 return self._path
702 702
703 703 def __nonzero__(self):
704 704 try:
705 705 self._filenode
706 706 return True
707 707 except error.LookupError:
708 708 # file is missing
709 709 return False
710 710
711 711 def __str__(self):
712 712 return "%s@%s" % (self.path(), self._changectx)
713 713
714 714 def __repr__(self):
715 715 return "<%s %s>" % (type(self).__name__, str(self))
716 716
717 717 def __hash__(self):
718 718 try:
719 719 return hash((self._path, self._filenode))
720 720 except AttributeError:
721 721 return id(self)
722 722
723 723 def __eq__(self, other):
724 724 try:
725 725 return (type(self) == type(other) and self._path == other._path
726 726 and self._filenode == other._filenode)
727 727 except AttributeError:
728 728 return False
729 729
730 730 def __ne__(self, other):
731 731 return not (self == other)
732 732
733 733 def filerev(self):
734 734 return self._filerev
735 735 def filenode(self):
736 736 return self._filenode
737 737 def flags(self):
738 738 return self._changectx.flags(self._path)
739 739 def filelog(self):
740 740 return self._filelog
741 741 def rev(self):
742 742 return self._changeid
743 743 def linkrev(self):
744 744 return self._filelog.linkrev(self._filerev)
745 745 def node(self):
746 746 return self._changectx.node()
747 747 def hex(self):
748 748 return self._changectx.hex()
749 749 def user(self):
750 750 return self._changectx.user()
751 751 def date(self):
752 752 return self._changectx.date()
753 753 def files(self):
754 754 return self._changectx.files()
755 755 def description(self):
756 756 return self._changectx.description()
757 757 def branch(self):
758 758 return self._changectx.branch()
759 759 def extra(self):
760 760 return self._changectx.extra()
761 761 def phase(self):
762 762 return self._changectx.phase()
763 763 def phasestr(self):
764 764 return self._changectx.phasestr()
765 765 def manifest(self):
766 766 return self._changectx.manifest()
767 767 def changectx(self):
768 768 return self._changectx
769 769 def repo(self):
770 770 return self._repo
771 771
772 772 def path(self):
773 773 return self._path
774 774
775 775 def isbinary(self):
776 776 try:
777 777 return util.binary(self.data())
778 778 except IOError:
779 779 return False
780 780 def isexec(self):
781 781 return 'x' in self.flags()
782 782 def islink(self):
783 783 return 'l' in self.flags()
784 784
785 785 def isabsent(self):
786 786 """whether this filectx represents a file not in self._changectx
787 787
788 788 This is mainly for merge code to detect change/delete conflicts. This is
789 789 expected to be True for all subclasses of basectx."""
790 790 return False
791 791
792 792 _customcmp = False
793 793 def cmp(self, fctx):
794 794 """compare with other file context
795 795
796 796 returns True if different than fctx.
797 797 """
798 798 if fctx._customcmp:
799 799 return fctx.cmp(self)
800 800
801 801 if (fctx._filenode is None
802 802 and (self._repo._encodefilterpats
803 803 # if file data starts with '\1\n', empty metadata block is
804 804 # prepended, which adds 4 bytes to filelog.size().
805 805 or self.size() - 4 == fctx.size())
806 806 or self.size() == fctx.size()):
807 807 return self._filelog.cmp(self._filenode, fctx.data())
808 808
809 809 return True
810 810
811 811 def _adjustlinkrev(self, path, filelog, fnode, srcrev, inclusive=False):
812 812 """return the first ancestor of <srcrev> introducing <fnode>
813 813
814 814 If the linkrev of the file revision does not point to an ancestor of
815 815 srcrev, we'll walk down the ancestors until we find one introducing
816 816 this file revision.
817 817
818 818 :repo: a localrepository object (used to access changelog and manifest)
819 819 :path: the file path
820 820 :fnode: the nodeid of the file revision
821 821 :filelog: the filelog of this path
822 822 :srcrev: the changeset revision we search ancestors from
823 823 :inclusive: if true, the src revision will also be checked
824 824 """
825 825 repo = self._repo
826 826 cl = repo.unfiltered().changelog
827 ma = repo.manifest
827 mfl = repo.manifestlog
828 828 # fetch the linkrev
829 829 fr = filelog.rev(fnode)
830 830 lkr = filelog.linkrev(fr)
831 831 # hack to reuse ancestor computation when searching for renames
832 832 memberanc = getattr(self, '_ancestrycontext', None)
833 833 iteranc = None
834 834 if srcrev is None:
835 835 # wctx case, used by workingfilectx during mergecopy
836 836 revs = [p.rev() for p in self._repo[None].parents()]
837 837 inclusive = True # we skipped the real (revless) source
838 838 else:
839 839 revs = [srcrev]
840 840 if memberanc is None:
841 841 memberanc = iteranc = cl.ancestors(revs, lkr,
842 842 inclusive=inclusive)
843 843 # check if this linkrev is an ancestor of srcrev
844 844 if lkr not in memberanc:
845 845 if iteranc is None:
846 846 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
847 847 for a in iteranc:
848 848 ac = cl.read(a) # get changeset data (we avoid object creation)
849 849 if path in ac[3]: # checking the 'files' field.
850 850 # The file has been touched, check if the content is
851 851 # similar to the one we search for.
852 if fnode == ma.readfast(ac[0]).get(path):
852 if fnode == mfl[ac[0]].readfast().get(path):
853 853 return a
854 854 # In theory, we should never get out of that loop without a result.
855 855 # But if manifest uses a buggy file revision (not children of the
856 856 # one it replaces) we could. Such a buggy situation will likely
857 857 # result is crash somewhere else at to some point.
858 858 return lkr
859 859
860 860 def introrev(self):
861 861 """return the rev of the changeset which introduced this file revision
862 862
863 863 This method is different from linkrev because it take into account the
864 864 changeset the filectx was created from. It ensures the returned
865 865 revision is one of its ancestors. This prevents bugs from
866 866 'linkrev-shadowing' when a file revision is used by multiple
867 867 changesets.
868 868 """
869 869 lkr = self.linkrev()
870 870 attrs = vars(self)
871 871 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
872 872 if noctx or self.rev() == lkr:
873 873 return self.linkrev()
874 874 return self._adjustlinkrev(self._path, self._filelog, self._filenode,
875 875 self.rev(), inclusive=True)
876 876
877 877 def _parentfilectx(self, path, fileid, filelog):
878 878 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
879 879 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
880 880 if '_changeid' in vars(self) or '_changectx' in vars(self):
881 881 # If self is associated with a changeset (probably explicitly
882 882 # fed), ensure the created filectx is associated with a
883 883 # changeset that is an ancestor of self.changectx.
884 884 # This lets us later use _adjustlinkrev to get a correct link.
885 885 fctx._descendantrev = self.rev()
886 886 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
887 887 elif '_descendantrev' in vars(self):
888 888 # Otherwise propagate _descendantrev if we have one associated.
889 889 fctx._descendantrev = self._descendantrev
890 890 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
891 891 return fctx
892 892
893 893 def parents(self):
894 894 _path = self._path
895 895 fl = self._filelog
896 896 parents = self._filelog.parents(self._filenode)
897 897 pl = [(_path, node, fl) for node in parents if node != nullid]
898 898
899 899 r = fl.renamed(self._filenode)
900 900 if r:
901 901 # - In the simple rename case, both parent are nullid, pl is empty.
902 902 # - In case of merge, only one of the parent is null id and should
903 903 # be replaced with the rename information. This parent is -always-
904 904 # the first one.
905 905 #
906 906 # As null id have always been filtered out in the previous list
907 907 # comprehension, inserting to 0 will always result in "replacing
908 908 # first nullid parent with rename information.
909 909 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
910 910
911 911 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
912 912
913 913 def p1(self):
914 914 return self.parents()[0]
915 915
916 916 def p2(self):
917 917 p = self.parents()
918 918 if len(p) == 2:
919 919 return p[1]
920 920 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
921 921
922 922 def annotate(self, follow=False, linenumber=False, diffopts=None):
923 923 '''returns a list of tuples of ((ctx, number), line) for each line
924 924 in the file, where ctx is the filectx of the node where
925 925 that line was last changed; if linenumber parameter is true, number is
926 926 the line number at the first appearance in the managed file, otherwise,
927 927 number has a fixed value of False.
928 928 '''
929 929
930 930 def lines(text):
931 931 if text.endswith("\n"):
932 932 return text.count("\n")
933 933 return text.count("\n") + 1
934 934
935 935 if linenumber:
936 936 def decorate(text, rev):
937 937 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
938 938 else:
939 939 def decorate(text, rev):
940 940 return ([(rev, False)] * lines(text), text)
941 941
942 942 def pair(parent, child):
943 943 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts,
944 944 refine=True)
945 945 for (a1, a2, b1, b2), t in blocks:
946 946 # Changed blocks ('!') or blocks made only of blank lines ('~')
947 947 # belong to the child.
948 948 if t == '=':
949 949 child[0][b1:b2] = parent[0][a1:a2]
950 950 return child
951 951
952 952 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
953 953
954 954 def parents(f):
955 955 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
956 956 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
957 957 # from the topmost introrev (= srcrev) down to p.linkrev() if it
958 958 # isn't an ancestor of the srcrev.
959 959 f._changeid
960 960 pl = f.parents()
961 961
962 962 # Don't return renamed parents if we aren't following.
963 963 if not follow:
964 964 pl = [p for p in pl if p.path() == f.path()]
965 965
966 966 # renamed filectx won't have a filelog yet, so set it
967 967 # from the cache to save time
968 968 for p in pl:
969 969 if not '_filelog' in p.__dict__:
970 970 p._filelog = getlog(p.path())
971 971
972 972 return pl
973 973
974 974 # use linkrev to find the first changeset where self appeared
975 975 base = self
976 976 introrev = self.introrev()
977 977 if self.rev() != introrev:
978 978 base = self.filectx(self.filenode(), changeid=introrev)
979 979 if getattr(base, '_ancestrycontext', None) is None:
980 980 cl = self._repo.changelog
981 981 if introrev is None:
982 982 # wctx is not inclusive, but works because _ancestrycontext
983 983 # is used to test filelog revisions
984 984 ac = cl.ancestors([p.rev() for p in base.parents()],
985 985 inclusive=True)
986 986 else:
987 987 ac = cl.ancestors([introrev], inclusive=True)
988 988 base._ancestrycontext = ac
989 989
990 990 # This algorithm would prefer to be recursive, but Python is a
991 991 # bit recursion-hostile. Instead we do an iterative
992 992 # depth-first search.
993 993
994 994 # 1st DFS pre-calculates pcache and needed
995 995 visit = [base]
996 996 pcache = {}
997 997 needed = {base: 1}
998 998 while visit:
999 999 f = visit.pop()
1000 1000 if f in pcache:
1001 1001 continue
1002 1002 pl = parents(f)
1003 1003 pcache[f] = pl
1004 1004 for p in pl:
1005 1005 needed[p] = needed.get(p, 0) + 1
1006 1006 if p not in pcache:
1007 1007 visit.append(p)
1008 1008
1009 1009 # 2nd DFS does the actual annotate
1010 1010 visit[:] = [base]
1011 1011 hist = {}
1012 1012 while visit:
1013 1013 f = visit[-1]
1014 1014 if f in hist:
1015 1015 visit.pop()
1016 1016 continue
1017 1017
1018 1018 ready = True
1019 1019 pl = pcache[f]
1020 1020 for p in pl:
1021 1021 if p not in hist:
1022 1022 ready = False
1023 1023 visit.append(p)
1024 1024 if ready:
1025 1025 visit.pop()
1026 1026 curr = decorate(f.data(), f)
1027 1027 for p in pl:
1028 1028 curr = pair(hist[p], curr)
1029 1029 if needed[p] == 1:
1030 1030 del hist[p]
1031 1031 del needed[p]
1032 1032 else:
1033 1033 needed[p] -= 1
1034 1034
1035 1035 hist[f] = curr
1036 1036 del pcache[f]
1037 1037
1038 1038 return zip(hist[base][0], hist[base][1].splitlines(True))
1039 1039
1040 1040 def ancestors(self, followfirst=False):
1041 1041 visit = {}
1042 1042 c = self
1043 1043 if followfirst:
1044 1044 cut = 1
1045 1045 else:
1046 1046 cut = None
1047 1047
1048 1048 while True:
1049 1049 for parent in c.parents()[:cut]:
1050 1050 visit[(parent.linkrev(), parent.filenode())] = parent
1051 1051 if not visit:
1052 1052 break
1053 1053 c = visit.pop(max(visit))
1054 1054 yield c
1055 1055
1056 1056 class filectx(basefilectx):
1057 1057 """A filecontext object makes access to data related to a particular
1058 1058 filerevision convenient."""
1059 1059 def __init__(self, repo, path, changeid=None, fileid=None,
1060 1060 filelog=None, changectx=None):
1061 1061 """changeid can be a changeset revision, node, or tag.
1062 1062 fileid can be a file revision or node."""
1063 1063 self._repo = repo
1064 1064 self._path = path
1065 1065
1066 1066 assert (changeid is not None
1067 1067 or fileid is not None
1068 1068 or changectx is not None), \
1069 1069 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1070 1070 % (changeid, fileid, changectx))
1071 1071
1072 1072 if filelog is not None:
1073 1073 self._filelog = filelog
1074 1074
1075 1075 if changeid is not None:
1076 1076 self._changeid = changeid
1077 1077 if changectx is not None:
1078 1078 self._changectx = changectx
1079 1079 if fileid is not None:
1080 1080 self._fileid = fileid
1081 1081
1082 1082 @propertycache
1083 1083 def _changectx(self):
1084 1084 try:
1085 1085 return changectx(self._repo, self._changeid)
1086 1086 except error.FilteredRepoLookupError:
1087 1087 # Linkrev may point to any revision in the repository. When the
1088 1088 # repository is filtered this may lead to `filectx` trying to build
1089 1089 # `changectx` for filtered revision. In such case we fallback to
1090 1090 # creating `changectx` on the unfiltered version of the reposition.
1091 1091 # This fallback should not be an issue because `changectx` from
1092 1092 # `filectx` are not used in complex operations that care about
1093 1093 # filtering.
1094 1094 #
1095 1095 # This fallback is a cheap and dirty fix that prevent several
1096 1096 # crashes. It does not ensure the behavior is correct. However the
1097 1097 # behavior was not correct before filtering either and "incorrect
1098 1098 # behavior" is seen as better as "crash"
1099 1099 #
1100 1100 # Linkrevs have several serious troubles with filtering that are
1101 1101 # complicated to solve. Proper handling of the issue here should be
1102 1102 # considered when solving linkrev issue are on the table.
1103 1103 return changectx(self._repo.unfiltered(), self._changeid)
1104 1104
1105 1105 def filectx(self, fileid, changeid=None):
1106 1106 '''opens an arbitrary revision of the file without
1107 1107 opening a new filelog'''
1108 1108 return filectx(self._repo, self._path, fileid=fileid,
1109 1109 filelog=self._filelog, changeid=changeid)
1110 1110
1111 1111 def data(self):
1112 1112 try:
1113 1113 return self._filelog.read(self._filenode)
1114 1114 except error.CensoredNodeError:
1115 1115 if self._repo.ui.config("censor", "policy", "abort") == "ignore":
1116 1116 return ""
1117 1117 raise error.Abort(_("censored node: %s") % short(self._filenode),
1118 1118 hint=_("set censor.policy to ignore errors"))
1119 1119
1120 1120 def size(self):
1121 1121 return self._filelog.size(self._filerev)
1122 1122
1123 1123 def renamed(self):
1124 1124 """check if file was actually renamed in this changeset revision
1125 1125
1126 1126 If rename logged in file revision, we report copy for changeset only
1127 1127 if file revisions linkrev points back to the changeset in question
1128 1128 or both changeset parents contain different file revisions.
1129 1129 """
1130 1130
1131 1131 renamed = self._filelog.renamed(self._filenode)
1132 1132 if not renamed:
1133 1133 return renamed
1134 1134
1135 1135 if self.rev() == self.linkrev():
1136 1136 return renamed
1137 1137
1138 1138 name = self.path()
1139 1139 fnode = self._filenode
1140 1140 for p in self._changectx.parents():
1141 1141 try:
1142 1142 if fnode == p.filenode(name):
1143 1143 return None
1144 1144 except error.LookupError:
1145 1145 pass
1146 1146 return renamed
1147 1147
1148 1148 def children(self):
1149 1149 # hard for renames
1150 1150 c = self._filelog.children(self._filenode)
1151 1151 return [filectx(self._repo, self._path, fileid=x,
1152 1152 filelog=self._filelog) for x in c]
1153 1153
1154 1154 class committablectx(basectx):
1155 1155 """A committablectx object provides common functionality for a context that
1156 1156 wants the ability to commit, e.g. workingctx or memctx."""
1157 1157 def __init__(self, repo, text="", user=None, date=None, extra=None,
1158 1158 changes=None):
1159 1159 self._repo = repo
1160 1160 self._rev = None
1161 1161 self._node = None
1162 1162 self._text = text
1163 1163 if date:
1164 1164 self._date = util.parsedate(date)
1165 1165 if user:
1166 1166 self._user = user
1167 1167 if changes:
1168 1168 self._status = changes
1169 1169
1170 1170 self._extra = {}
1171 1171 if extra:
1172 1172 self._extra = extra.copy()
1173 1173 if 'branch' not in self._extra:
1174 1174 try:
1175 1175 branch = encoding.fromlocal(self._repo.dirstate.branch())
1176 1176 except UnicodeDecodeError:
1177 1177 raise error.Abort(_('branch name not in UTF-8!'))
1178 1178 self._extra['branch'] = branch
1179 1179 if self._extra['branch'] == '':
1180 1180 self._extra['branch'] = 'default'
1181 1181
1182 1182 def __str__(self):
1183 1183 return str(self._parents[0]) + "+"
1184 1184
1185 1185 def __nonzero__(self):
1186 1186 return True
1187 1187
1188 1188 def _buildflagfunc(self):
1189 1189 # Create a fallback function for getting file flags when the
1190 1190 # filesystem doesn't support them
1191 1191
1192 1192 copiesget = self._repo.dirstate.copies().get
1193 1193 parents = self.parents()
1194 1194 if len(parents) < 2:
1195 1195 # when we have one parent, it's easy: copy from parent
1196 1196 man = parents[0].manifest()
1197 1197 def func(f):
1198 1198 f = copiesget(f, f)
1199 1199 return man.flags(f)
1200 1200 else:
1201 1201 # merges are tricky: we try to reconstruct the unstored
1202 1202 # result from the merge (issue1802)
1203 1203 p1, p2 = parents
1204 1204 pa = p1.ancestor(p2)
1205 1205 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1206 1206
1207 1207 def func(f):
1208 1208 f = copiesget(f, f) # may be wrong for merges with copies
1209 1209 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1210 1210 if fl1 == fl2:
1211 1211 return fl1
1212 1212 if fl1 == fla:
1213 1213 return fl2
1214 1214 if fl2 == fla:
1215 1215 return fl1
1216 1216 return '' # punt for conflicts
1217 1217
1218 1218 return func
1219 1219
1220 1220 @propertycache
1221 1221 def _flagfunc(self):
1222 1222 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1223 1223
1224 1224 @propertycache
1225 1225 def _manifest(self):
1226 1226 """generate a manifest corresponding to the values in self._status
1227 1227
1228 1228 This reuse the file nodeid from parent, but we append an extra letter
1229 1229 when modified. Modified files get an extra 'm' while added files get
1230 1230 an extra 'a'. This is used by manifests merge to see that files
1231 1231 are different and by update logic to avoid deleting newly added files.
1232 1232 """
1233 1233 parents = self.parents()
1234 1234
1235 1235 man1 = parents[0].manifest()
1236 1236 man = man1.copy()
1237 1237 if len(parents) > 1:
1238 1238 man2 = self.p2().manifest()
1239 1239 def getman(f):
1240 1240 if f in man1:
1241 1241 return man1
1242 1242 return man2
1243 1243 else:
1244 1244 getman = lambda f: man1
1245 1245
1246 1246 copied = self._repo.dirstate.copies()
1247 1247 ff = self._flagfunc
1248 1248 for i, l in (("a", self._status.added), ("m", self._status.modified)):
1249 1249 for f in l:
1250 1250 orig = copied.get(f, f)
1251 1251 man[f] = getman(orig).get(orig, nullid) + i
1252 1252 try:
1253 1253 man.setflag(f, ff(f))
1254 1254 except OSError:
1255 1255 pass
1256 1256
1257 1257 for f in self._status.deleted + self._status.removed:
1258 1258 if f in man:
1259 1259 del man[f]
1260 1260
1261 1261 return man
1262 1262
1263 1263 @propertycache
1264 1264 def _status(self):
1265 1265 return self._repo.status()
1266 1266
1267 1267 @propertycache
1268 1268 def _user(self):
1269 1269 return self._repo.ui.username()
1270 1270
1271 1271 @propertycache
1272 1272 def _date(self):
1273 1273 return util.makedate()
1274 1274
1275 1275 def subrev(self, subpath):
1276 1276 return None
1277 1277
1278 1278 def manifestnode(self):
1279 1279 return None
1280 1280 def user(self):
1281 1281 return self._user or self._repo.ui.username()
1282 1282 def date(self):
1283 1283 return self._date
1284 1284 def description(self):
1285 1285 return self._text
1286 1286 def files(self):
1287 1287 return sorted(self._status.modified + self._status.added +
1288 1288 self._status.removed)
1289 1289
1290 1290 def modified(self):
1291 1291 return self._status.modified
1292 1292 def added(self):
1293 1293 return self._status.added
1294 1294 def removed(self):
1295 1295 return self._status.removed
1296 1296 def deleted(self):
1297 1297 return self._status.deleted
1298 1298 def branch(self):
1299 1299 return encoding.tolocal(self._extra['branch'])
1300 1300 def closesbranch(self):
1301 1301 return 'close' in self._extra
1302 1302 def extra(self):
1303 1303 return self._extra
1304 1304
1305 1305 def tags(self):
1306 1306 return []
1307 1307
1308 1308 def bookmarks(self):
1309 1309 b = []
1310 1310 for p in self.parents():
1311 1311 b.extend(p.bookmarks())
1312 1312 return b
1313 1313
1314 1314 def phase(self):
1315 1315 phase = phases.draft # default phase to draft
1316 1316 for p in self.parents():
1317 1317 phase = max(phase, p.phase())
1318 1318 return phase
1319 1319
1320 1320 def hidden(self):
1321 1321 return False
1322 1322
1323 1323 def children(self):
1324 1324 return []
1325 1325
1326 1326 def flags(self, path):
1327 1327 if '_manifest' in self.__dict__:
1328 1328 try:
1329 1329 return self._manifest.flags(path)
1330 1330 except KeyError:
1331 1331 return ''
1332 1332
1333 1333 try:
1334 1334 return self._flagfunc(path)
1335 1335 except OSError:
1336 1336 return ''
1337 1337
1338 1338 def ancestor(self, c2):
1339 1339 """return the "best" ancestor context of self and c2"""
1340 1340 return self._parents[0].ancestor(c2) # punt on two parents for now
1341 1341
1342 1342 def walk(self, match):
1343 1343 '''Generates matching file names.'''
1344 1344 return sorted(self._repo.dirstate.walk(match, sorted(self.substate),
1345 1345 True, False))
1346 1346
1347 1347 def matches(self, match):
1348 1348 return sorted(self._repo.dirstate.matches(match))
1349 1349
1350 1350 def ancestors(self):
1351 1351 for p in self._parents:
1352 1352 yield p
1353 1353 for a in self._repo.changelog.ancestors(
1354 1354 [p.rev() for p in self._parents]):
1355 1355 yield changectx(self._repo, a)
1356 1356
1357 1357 def markcommitted(self, node):
1358 1358 """Perform post-commit cleanup necessary after committing this ctx
1359 1359
1360 1360 Specifically, this updates backing stores this working context
1361 1361 wraps to reflect the fact that the changes reflected by this
1362 1362 workingctx have been committed. For example, it marks
1363 1363 modified and added files as normal in the dirstate.
1364 1364
1365 1365 """
1366 1366
1367 1367 self._repo.dirstate.beginparentchange()
1368 1368 for f in self.modified() + self.added():
1369 1369 self._repo.dirstate.normal(f)
1370 1370 for f in self.removed():
1371 1371 self._repo.dirstate.drop(f)
1372 1372 self._repo.dirstate.setparents(node)
1373 1373 self._repo.dirstate.endparentchange()
1374 1374
1375 1375 # write changes out explicitly, because nesting wlock at
1376 1376 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1377 1377 # from immediately doing so for subsequent changing files
1378 1378 self._repo.dirstate.write(self._repo.currenttransaction())
1379 1379
1380 1380 class workingctx(committablectx):
1381 1381 """A workingctx object makes access to data related to
1382 1382 the current working directory convenient.
1383 1383 date - any valid date string or (unixtime, offset), or None.
1384 1384 user - username string, or None.
1385 1385 extra - a dictionary of extra values, or None.
1386 1386 changes - a list of file lists as returned by localrepo.status()
1387 1387 or None to use the repository status.
1388 1388 """
1389 1389 def __init__(self, repo, text="", user=None, date=None, extra=None,
1390 1390 changes=None):
1391 1391 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1392 1392
1393 1393 def __iter__(self):
1394 1394 d = self._repo.dirstate
1395 1395 for f in d:
1396 1396 if d[f] != 'r':
1397 1397 yield f
1398 1398
1399 1399 def __contains__(self, key):
1400 1400 return self._repo.dirstate[key] not in "?r"
1401 1401
1402 1402 def hex(self):
1403 1403 return hex(wdirid)
1404 1404
1405 1405 @propertycache
1406 1406 def _parents(self):
1407 1407 p = self._repo.dirstate.parents()
1408 1408 if p[1] == nullid:
1409 1409 p = p[:-1]
1410 1410 return [changectx(self._repo, x) for x in p]
1411 1411
1412 1412 def filectx(self, path, filelog=None):
1413 1413 """get a file context from the working directory"""
1414 1414 return workingfilectx(self._repo, path, workingctx=self,
1415 1415 filelog=filelog)
1416 1416
1417 1417 def dirty(self, missing=False, merge=True, branch=True):
1418 1418 "check whether a working directory is modified"
1419 1419 # check subrepos first
1420 1420 for s in sorted(self.substate):
1421 1421 if self.sub(s).dirty():
1422 1422 return True
1423 1423 # check current working dir
1424 1424 return ((merge and self.p2()) or
1425 1425 (branch and self.branch() != self.p1().branch()) or
1426 1426 self.modified() or self.added() or self.removed() or
1427 1427 (missing and self.deleted()))
1428 1428
1429 1429 def add(self, list, prefix=""):
1430 1430 join = lambda f: os.path.join(prefix, f)
1431 1431 with self._repo.wlock():
1432 1432 ui, ds = self._repo.ui, self._repo.dirstate
1433 1433 rejected = []
1434 1434 lstat = self._repo.wvfs.lstat
1435 1435 for f in list:
1436 1436 scmutil.checkportable(ui, join(f))
1437 1437 try:
1438 1438 st = lstat(f)
1439 1439 except OSError:
1440 1440 ui.warn(_("%s does not exist!\n") % join(f))
1441 1441 rejected.append(f)
1442 1442 continue
1443 1443 if st.st_size > 10000000:
1444 1444 ui.warn(_("%s: up to %d MB of RAM may be required "
1445 1445 "to manage this file\n"
1446 1446 "(use 'hg revert %s' to cancel the "
1447 1447 "pending addition)\n")
1448 1448 % (f, 3 * st.st_size // 1000000, join(f)))
1449 1449 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1450 1450 ui.warn(_("%s not added: only files and symlinks "
1451 1451 "supported currently\n") % join(f))
1452 1452 rejected.append(f)
1453 1453 elif ds[f] in 'amn':
1454 1454 ui.warn(_("%s already tracked!\n") % join(f))
1455 1455 elif ds[f] == 'r':
1456 1456 ds.normallookup(f)
1457 1457 else:
1458 1458 ds.add(f)
1459 1459 return rejected
1460 1460
1461 1461 def forget(self, files, prefix=""):
1462 1462 join = lambda f: os.path.join(prefix, f)
1463 1463 with self._repo.wlock():
1464 1464 rejected = []
1465 1465 for f in files:
1466 1466 if f not in self._repo.dirstate:
1467 1467 self._repo.ui.warn(_("%s not tracked!\n") % join(f))
1468 1468 rejected.append(f)
1469 1469 elif self._repo.dirstate[f] != 'a':
1470 1470 self._repo.dirstate.remove(f)
1471 1471 else:
1472 1472 self._repo.dirstate.drop(f)
1473 1473 return rejected
1474 1474
1475 1475 def undelete(self, list):
1476 1476 pctxs = self.parents()
1477 1477 with self._repo.wlock():
1478 1478 for f in list:
1479 1479 if self._repo.dirstate[f] != 'r':
1480 1480 self._repo.ui.warn(_("%s not removed!\n") % f)
1481 1481 else:
1482 1482 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1483 1483 t = fctx.data()
1484 1484 self._repo.wwrite(f, t, fctx.flags())
1485 1485 self._repo.dirstate.normal(f)
1486 1486
1487 1487 def copy(self, source, dest):
1488 1488 try:
1489 1489 st = self._repo.wvfs.lstat(dest)
1490 1490 except OSError as err:
1491 1491 if err.errno != errno.ENOENT:
1492 1492 raise
1493 1493 self._repo.ui.warn(_("%s does not exist!\n") % dest)
1494 1494 return
1495 1495 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1496 1496 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1497 1497 "symbolic link\n") % dest)
1498 1498 else:
1499 1499 with self._repo.wlock():
1500 1500 if self._repo.dirstate[dest] in '?':
1501 1501 self._repo.dirstate.add(dest)
1502 1502 elif self._repo.dirstate[dest] in 'r':
1503 1503 self._repo.dirstate.normallookup(dest)
1504 1504 self._repo.dirstate.copy(source, dest)
1505 1505
1506 1506 def match(self, pats=[], include=None, exclude=None, default='glob',
1507 1507 listsubrepos=False, badfn=None):
1508 1508 r = self._repo
1509 1509
1510 1510 # Only a case insensitive filesystem needs magic to translate user input
1511 1511 # to actual case in the filesystem.
1512 1512 if not util.fscasesensitive(r.root):
1513 1513 return matchmod.icasefsmatcher(r.root, r.getcwd(), pats, include,
1514 1514 exclude, default, r.auditor, self,
1515 1515 listsubrepos=listsubrepos,
1516 1516 badfn=badfn)
1517 1517 return matchmod.match(r.root, r.getcwd(), pats,
1518 1518 include, exclude, default,
1519 1519 auditor=r.auditor, ctx=self,
1520 1520 listsubrepos=listsubrepos, badfn=badfn)
1521 1521
1522 1522 def _filtersuspectsymlink(self, files):
1523 1523 if not files or self._repo.dirstate._checklink:
1524 1524 return files
1525 1525
1526 1526 # Symlink placeholders may get non-symlink-like contents
1527 1527 # via user error or dereferencing by NFS or Samba servers,
1528 1528 # so we filter out any placeholders that don't look like a
1529 1529 # symlink
1530 1530 sane = []
1531 1531 for f in files:
1532 1532 if self.flags(f) == 'l':
1533 1533 d = self[f].data()
1534 1534 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1535 1535 self._repo.ui.debug('ignoring suspect symlink placeholder'
1536 1536 ' "%s"\n' % f)
1537 1537 continue
1538 1538 sane.append(f)
1539 1539 return sane
1540 1540
1541 1541 def _checklookup(self, files):
1542 1542 # check for any possibly clean files
1543 1543 if not files:
1544 1544 return [], []
1545 1545
1546 1546 modified = []
1547 1547 fixup = []
1548 1548 pctx = self._parents[0]
1549 1549 # do a full compare of any files that might have changed
1550 1550 for f in sorted(files):
1551 1551 if (f not in pctx or self.flags(f) != pctx.flags(f)
1552 1552 or pctx[f].cmp(self[f])):
1553 1553 modified.append(f)
1554 1554 else:
1555 1555 fixup.append(f)
1556 1556
1557 1557 # update dirstate for files that are actually clean
1558 1558 if fixup:
1559 1559 try:
1560 1560 # updating the dirstate is optional
1561 1561 # so we don't wait on the lock
1562 1562 # wlock can invalidate the dirstate, so cache normal _after_
1563 1563 # taking the lock
1564 1564 with self._repo.wlock(False):
1565 1565 normal = self._repo.dirstate.normal
1566 1566 for f in fixup:
1567 1567 normal(f)
1568 1568 # write changes out explicitly, because nesting
1569 1569 # wlock at runtime may prevent 'wlock.release()'
1570 1570 # after this block from doing so for subsequent
1571 1571 # changing files
1572 1572 self._repo.dirstate.write(self._repo.currenttransaction())
1573 1573 except error.LockError:
1574 1574 pass
1575 1575 return modified, fixup
1576 1576
1577 1577 def _manifestmatches(self, match, s):
1578 1578 """Slow path for workingctx
1579 1579
1580 1580 The fast path is when we compare the working directory to its parent
1581 1581 which means this function is comparing with a non-parent; therefore we
1582 1582 need to build a manifest and return what matches.
1583 1583 """
1584 1584 mf = self._repo['.']._manifestmatches(match, s)
1585 1585 for f in s.modified + s.added:
1586 1586 mf[f] = _newnode
1587 1587 mf.setflag(f, self.flags(f))
1588 1588 for f in s.removed:
1589 1589 if f in mf:
1590 1590 del mf[f]
1591 1591 return mf
1592 1592
1593 1593 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1594 1594 unknown=False):
1595 1595 '''Gets the status from the dirstate -- internal use only.'''
1596 1596 listignored, listclean, listunknown = ignored, clean, unknown
1597 1597 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1598 1598 subrepos = []
1599 1599 if '.hgsub' in self:
1600 1600 subrepos = sorted(self.substate)
1601 1601 cmp, s = self._repo.dirstate.status(match, subrepos, listignored,
1602 1602 listclean, listunknown)
1603 1603
1604 1604 # check for any possibly clean files
1605 1605 if cmp:
1606 1606 modified2, fixup = self._checklookup(cmp)
1607 1607 s.modified.extend(modified2)
1608 1608
1609 1609 # update dirstate for files that are actually clean
1610 1610 if fixup and listclean:
1611 1611 s.clean.extend(fixup)
1612 1612
1613 1613 if match.always():
1614 1614 # cache for performance
1615 1615 if s.unknown or s.ignored or s.clean:
1616 1616 # "_status" is cached with list*=False in the normal route
1617 1617 self._status = scmutil.status(s.modified, s.added, s.removed,
1618 1618 s.deleted, [], [], [])
1619 1619 else:
1620 1620 self._status = s
1621 1621
1622 1622 return s
1623 1623
1624 1624 def _buildstatus(self, other, s, match, listignored, listclean,
1625 1625 listunknown):
1626 1626 """build a status with respect to another context
1627 1627
1628 1628 This includes logic for maintaining the fast path of status when
1629 1629 comparing the working directory against its parent, which is to skip
1630 1630 building a new manifest if self (working directory) is not comparing
1631 1631 against its parent (repo['.']).
1632 1632 """
1633 1633 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1634 1634 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1635 1635 # might have accidentally ended up with the entire contents of the file
1636 1636 # they are supposed to be linking to.
1637 1637 s.modified[:] = self._filtersuspectsymlink(s.modified)
1638 1638 if other != self._repo['.']:
1639 1639 s = super(workingctx, self)._buildstatus(other, s, match,
1640 1640 listignored, listclean,
1641 1641 listunknown)
1642 1642 return s
1643 1643
1644 1644 def _matchstatus(self, other, match):
1645 1645 """override the match method with a filter for directory patterns
1646 1646
1647 1647 We use inheritance to customize the match.bad method only in cases of
1648 1648 workingctx since it belongs only to the working directory when
1649 1649 comparing against the parent changeset.
1650 1650
1651 1651 If we aren't comparing against the working directory's parent, then we
1652 1652 just use the default match object sent to us.
1653 1653 """
1654 1654 superself = super(workingctx, self)
1655 1655 match = superself._matchstatus(other, match)
1656 1656 if other != self._repo['.']:
1657 1657 def bad(f, msg):
1658 1658 # 'f' may be a directory pattern from 'match.files()',
1659 1659 # so 'f not in ctx1' is not enough
1660 1660 if f not in other and not other.hasdir(f):
1661 1661 self._repo.ui.warn('%s: %s\n' %
1662 1662 (self._repo.dirstate.pathto(f), msg))
1663 1663 match.bad = bad
1664 1664 return match
1665 1665
1666 1666 class committablefilectx(basefilectx):
1667 1667 """A committablefilectx provides common functionality for a file context
1668 1668 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1669 1669 def __init__(self, repo, path, filelog=None, ctx=None):
1670 1670 self._repo = repo
1671 1671 self._path = path
1672 1672 self._changeid = None
1673 1673 self._filerev = self._filenode = None
1674 1674
1675 1675 if filelog is not None:
1676 1676 self._filelog = filelog
1677 1677 if ctx:
1678 1678 self._changectx = ctx
1679 1679
1680 1680 def __nonzero__(self):
1681 1681 return True
1682 1682
1683 1683 def linkrev(self):
1684 1684 # linked to self._changectx no matter if file is modified or not
1685 1685 return self.rev()
1686 1686
1687 1687 def parents(self):
1688 1688 '''return parent filectxs, following copies if necessary'''
1689 1689 def filenode(ctx, path):
1690 1690 return ctx._manifest.get(path, nullid)
1691 1691
1692 1692 path = self._path
1693 1693 fl = self._filelog
1694 1694 pcl = self._changectx._parents
1695 1695 renamed = self.renamed()
1696 1696
1697 1697 if renamed:
1698 1698 pl = [renamed + (None,)]
1699 1699 else:
1700 1700 pl = [(path, filenode(pcl[0], path), fl)]
1701 1701
1702 1702 for pc in pcl[1:]:
1703 1703 pl.append((path, filenode(pc, path), fl))
1704 1704
1705 1705 return [self._parentfilectx(p, fileid=n, filelog=l)
1706 1706 for p, n, l in pl if n != nullid]
1707 1707
1708 1708 def children(self):
1709 1709 return []
1710 1710
1711 1711 class workingfilectx(committablefilectx):
1712 1712 """A workingfilectx object makes access to data related to a particular
1713 1713 file in the working directory convenient."""
1714 1714 def __init__(self, repo, path, filelog=None, workingctx=None):
1715 1715 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1716 1716
1717 1717 @propertycache
1718 1718 def _changectx(self):
1719 1719 return workingctx(self._repo)
1720 1720
1721 1721 def data(self):
1722 1722 return self._repo.wread(self._path)
1723 1723 def renamed(self):
1724 1724 rp = self._repo.dirstate.copied(self._path)
1725 1725 if not rp:
1726 1726 return None
1727 1727 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1728 1728
1729 1729 def size(self):
1730 1730 return self._repo.wvfs.lstat(self._path).st_size
1731 1731 def date(self):
1732 1732 t, tz = self._changectx.date()
1733 1733 try:
1734 1734 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1735 1735 except OSError as err:
1736 1736 if err.errno != errno.ENOENT:
1737 1737 raise
1738 1738 return (t, tz)
1739 1739
1740 1740 def cmp(self, fctx):
1741 1741 """compare with other file context
1742 1742
1743 1743 returns True if different than fctx.
1744 1744 """
1745 1745 # fctx should be a filectx (not a workingfilectx)
1746 1746 # invert comparison to reuse the same code path
1747 1747 return fctx.cmp(self)
1748 1748
1749 1749 def remove(self, ignoremissing=False):
1750 1750 """wraps unlink for a repo's working directory"""
1751 1751 util.unlinkpath(self._repo.wjoin(self._path), ignoremissing)
1752 1752
1753 1753 def write(self, data, flags):
1754 1754 """wraps repo.wwrite"""
1755 1755 self._repo.wwrite(self._path, data, flags)
1756 1756
1757 1757 class workingcommitctx(workingctx):
1758 1758 """A workingcommitctx object makes access to data related to
1759 1759 the revision being committed convenient.
1760 1760
1761 1761 This hides changes in the working directory, if they aren't
1762 1762 committed in this context.
1763 1763 """
1764 1764 def __init__(self, repo, changes,
1765 1765 text="", user=None, date=None, extra=None):
1766 1766 super(workingctx, self).__init__(repo, text, user, date, extra,
1767 1767 changes)
1768 1768
1769 1769 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1770 1770 unknown=False):
1771 1771 """Return matched files only in ``self._status``
1772 1772
1773 1773 Uncommitted files appear "clean" via this context, even if
1774 1774 they aren't actually so in the working directory.
1775 1775 """
1776 1776 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1777 1777 if clean:
1778 1778 clean = [f for f in self._manifest if f not in self._changedset]
1779 1779 else:
1780 1780 clean = []
1781 1781 return scmutil.status([f for f in self._status.modified if match(f)],
1782 1782 [f for f in self._status.added if match(f)],
1783 1783 [f for f in self._status.removed if match(f)],
1784 1784 [], [], [], clean)
1785 1785
1786 1786 @propertycache
1787 1787 def _changedset(self):
1788 1788 """Return the set of files changed in this context
1789 1789 """
1790 1790 changed = set(self._status.modified)
1791 1791 changed.update(self._status.added)
1792 1792 changed.update(self._status.removed)
1793 1793 return changed
1794 1794
1795 1795 def makecachingfilectxfn(func):
1796 1796 """Create a filectxfn that caches based on the path.
1797 1797
1798 1798 We can't use util.cachefunc because it uses all arguments as the cache
1799 1799 key and this creates a cycle since the arguments include the repo and
1800 1800 memctx.
1801 1801 """
1802 1802 cache = {}
1803 1803
1804 1804 def getfilectx(repo, memctx, path):
1805 1805 if path not in cache:
1806 1806 cache[path] = func(repo, memctx, path)
1807 1807 return cache[path]
1808 1808
1809 1809 return getfilectx
1810 1810
1811 1811 class memctx(committablectx):
1812 1812 """Use memctx to perform in-memory commits via localrepo.commitctx().
1813 1813
1814 1814 Revision information is supplied at initialization time while
1815 1815 related files data and is made available through a callback
1816 1816 mechanism. 'repo' is the current localrepo, 'parents' is a
1817 1817 sequence of two parent revisions identifiers (pass None for every
1818 1818 missing parent), 'text' is the commit message and 'files' lists
1819 1819 names of files touched by the revision (normalized and relative to
1820 1820 repository root).
1821 1821
1822 1822 filectxfn(repo, memctx, path) is a callable receiving the
1823 1823 repository, the current memctx object and the normalized path of
1824 1824 requested file, relative to repository root. It is fired by the
1825 1825 commit function for every file in 'files', but calls order is
1826 1826 undefined. If the file is available in the revision being
1827 1827 committed (updated or added), filectxfn returns a memfilectx
1828 1828 object. If the file was removed, filectxfn raises an
1829 1829 IOError. Moved files are represented by marking the source file
1830 1830 removed and the new file added with copy information (see
1831 1831 memfilectx).
1832 1832
1833 1833 user receives the committer name and defaults to current
1834 1834 repository username, date is the commit date in any format
1835 1835 supported by util.parsedate() and defaults to current date, extra
1836 1836 is a dictionary of metadata or is left empty.
1837 1837 """
1838 1838
1839 1839 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
1840 1840 # Extensions that need to retain compatibility across Mercurial 3.1 can use
1841 1841 # this field to determine what to do in filectxfn.
1842 1842 _returnnoneformissingfiles = True
1843 1843
1844 1844 def __init__(self, repo, parents, text, files, filectxfn, user=None,
1845 1845 date=None, extra=None, editor=False):
1846 1846 super(memctx, self).__init__(repo, text, user, date, extra)
1847 1847 self._rev = None
1848 1848 self._node = None
1849 1849 parents = [(p or nullid) for p in parents]
1850 1850 p1, p2 = parents
1851 1851 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
1852 1852 files = sorted(set(files))
1853 1853 self._files = files
1854 1854 self.substate = {}
1855 1855
1856 1856 # if store is not callable, wrap it in a function
1857 1857 if not callable(filectxfn):
1858 1858 def getfilectx(repo, memctx, path):
1859 1859 fctx = filectxfn[path]
1860 1860 # this is weird but apparently we only keep track of one parent
1861 1861 # (why not only store that instead of a tuple?)
1862 1862 copied = fctx.renamed()
1863 1863 if copied:
1864 1864 copied = copied[0]
1865 1865 return memfilectx(repo, path, fctx.data(),
1866 1866 islink=fctx.islink(), isexec=fctx.isexec(),
1867 1867 copied=copied, memctx=memctx)
1868 1868 self._filectxfn = getfilectx
1869 1869 else:
1870 1870 # memoizing increases performance for e.g. vcs convert scenarios.
1871 1871 self._filectxfn = makecachingfilectxfn(filectxfn)
1872 1872
1873 1873 if extra:
1874 1874 self._extra = extra.copy()
1875 1875 else:
1876 1876 self._extra = {}
1877 1877
1878 1878 if self._extra.get('branch', '') == '':
1879 1879 self._extra['branch'] = 'default'
1880 1880
1881 1881 if editor:
1882 1882 self._text = editor(self._repo, self, [])
1883 1883 self._repo.savecommitmessage(self._text)
1884 1884
1885 1885 def filectx(self, path, filelog=None):
1886 1886 """get a file context from the working directory
1887 1887
1888 1888 Returns None if file doesn't exist and should be removed."""
1889 1889 return self._filectxfn(self._repo, self, path)
1890 1890
1891 1891 def commit(self):
1892 1892 """commit context to the repo"""
1893 1893 return self._repo.commitctx(self)
1894 1894
1895 1895 @propertycache
1896 1896 def _manifest(self):
1897 1897 """generate a manifest based on the return values of filectxfn"""
1898 1898
1899 1899 # keep this simple for now; just worry about p1
1900 1900 pctx = self._parents[0]
1901 1901 man = pctx.manifest().copy()
1902 1902
1903 1903 for f in self._status.modified:
1904 1904 p1node = nullid
1905 1905 p2node = nullid
1906 1906 p = pctx[f].parents() # if file isn't in pctx, check p2?
1907 1907 if len(p) > 0:
1908 1908 p1node = p[0].filenode()
1909 1909 if len(p) > 1:
1910 1910 p2node = p[1].filenode()
1911 1911 man[f] = revlog.hash(self[f].data(), p1node, p2node)
1912 1912
1913 1913 for f in self._status.added:
1914 1914 man[f] = revlog.hash(self[f].data(), nullid, nullid)
1915 1915
1916 1916 for f in self._status.removed:
1917 1917 if f in man:
1918 1918 del man[f]
1919 1919
1920 1920 return man
1921 1921
1922 1922 @propertycache
1923 1923 def _status(self):
1924 1924 """Calculate exact status from ``files`` specified at construction
1925 1925 """
1926 1926 man1 = self.p1().manifest()
1927 1927 p2 = self._parents[1]
1928 1928 # "1 < len(self._parents)" can't be used for checking
1929 1929 # existence of the 2nd parent, because "memctx._parents" is
1930 1930 # explicitly initialized by the list, of which length is 2.
1931 1931 if p2.node() != nullid:
1932 1932 man2 = p2.manifest()
1933 1933 managing = lambda f: f in man1 or f in man2
1934 1934 else:
1935 1935 managing = lambda f: f in man1
1936 1936
1937 1937 modified, added, removed = [], [], []
1938 1938 for f in self._files:
1939 1939 if not managing(f):
1940 1940 added.append(f)
1941 1941 elif self[f]:
1942 1942 modified.append(f)
1943 1943 else:
1944 1944 removed.append(f)
1945 1945
1946 1946 return scmutil.status(modified, added, removed, [], [], [], [])
1947 1947
1948 1948 class memfilectx(committablefilectx):
1949 1949 """memfilectx represents an in-memory file to commit.
1950 1950
1951 1951 See memctx and committablefilectx for more details.
1952 1952 """
1953 1953 def __init__(self, repo, path, data, islink=False,
1954 1954 isexec=False, copied=None, memctx=None):
1955 1955 """
1956 1956 path is the normalized file path relative to repository root.
1957 1957 data is the file content as a string.
1958 1958 islink is True if the file is a symbolic link.
1959 1959 isexec is True if the file is executable.
1960 1960 copied is the source file path if current file was copied in the
1961 1961 revision being committed, or None."""
1962 1962 super(memfilectx, self).__init__(repo, path, None, memctx)
1963 1963 self._data = data
1964 1964 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1965 1965 self._copied = None
1966 1966 if copied:
1967 1967 self._copied = (copied, nullid)
1968 1968
1969 1969 def data(self):
1970 1970 return self._data
1971 1971 def size(self):
1972 1972 return len(self.data())
1973 1973 def flags(self):
1974 1974 return self._flags
1975 1975 def renamed(self):
1976 1976 return self._copied
1977 1977
1978 1978 def remove(self, ignoremissing=False):
1979 1979 """wraps unlink for a repo's working directory"""
1980 1980 # need to figure out what to do here
1981 1981 del self._changectx[self._path]
1982 1982
1983 1983 def write(self, data, flags):
1984 1984 """wraps repo.wwrite"""
1985 1985 self._data = data
@@ -1,1278 +1,1280 b''
1 1 # manifest.py - manifest revision 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 array
11 11 import heapq
12 12 import os
13 13 import struct
14 14
15 15 from .i18n import _
16 16 from . import (
17 17 error,
18 18 mdiff,
19 19 parsers,
20 20 revlog,
21 21 util,
22 22 )
23 23
24 24 propertycache = util.propertycache
25 25
26 26 def _parsev1(data):
27 27 # This method does a little bit of excessive-looking
28 28 # precondition checking. This is so that the behavior of this
29 29 # class exactly matches its C counterpart to try and help
30 30 # prevent surprise breakage for anyone that develops against
31 31 # the pure version.
32 32 if data and data[-1] != '\n':
33 33 raise ValueError('Manifest did not end in a newline.')
34 34 prev = None
35 35 for l in data.splitlines():
36 36 if prev is not None and prev > l:
37 37 raise ValueError('Manifest lines not in sorted order.')
38 38 prev = l
39 39 f, n = l.split('\0')
40 40 if len(n) > 40:
41 41 yield f, revlog.bin(n[:40]), n[40:]
42 42 else:
43 43 yield f, revlog.bin(n), ''
44 44
45 45 def _parsev2(data):
46 46 metadataend = data.find('\n')
47 47 # Just ignore metadata for now
48 48 pos = metadataend + 1
49 49 prevf = ''
50 50 while pos < len(data):
51 51 end = data.find('\n', pos + 1) # +1 to skip stem length byte
52 52 if end == -1:
53 53 raise ValueError('Manifest ended with incomplete file entry.')
54 54 stemlen = ord(data[pos])
55 55 items = data[pos + 1:end].split('\0')
56 56 f = prevf[:stemlen] + items[0]
57 57 if prevf > f:
58 58 raise ValueError('Manifest entries not in sorted order.')
59 59 fl = items[1]
60 60 # Just ignore metadata (items[2:] for now)
61 61 n = data[end + 1:end + 21]
62 62 yield f, n, fl
63 63 pos = end + 22
64 64 prevf = f
65 65
66 66 def _parse(data):
67 67 """Generates (path, node, flags) tuples from a manifest text"""
68 68 if data.startswith('\0'):
69 69 return iter(_parsev2(data))
70 70 else:
71 71 return iter(_parsev1(data))
72 72
73 73 def _text(it, usemanifestv2):
74 74 """Given an iterator over (path, node, flags) tuples, returns a manifest
75 75 text"""
76 76 if usemanifestv2:
77 77 return _textv2(it)
78 78 else:
79 79 return _textv1(it)
80 80
81 81 def _textv1(it):
82 82 files = []
83 83 lines = []
84 84 _hex = revlog.hex
85 85 for f, n, fl in it:
86 86 files.append(f)
87 87 # if this is changed to support newlines in filenames,
88 88 # be sure to check the templates/ dir again (especially *-raw.tmpl)
89 89 lines.append("%s\0%s%s\n" % (f, _hex(n), fl))
90 90
91 91 _checkforbidden(files)
92 92 return ''.join(lines)
93 93
94 94 def _textv2(it):
95 95 files = []
96 96 lines = ['\0\n']
97 97 prevf = ''
98 98 for f, n, fl in it:
99 99 files.append(f)
100 100 stem = os.path.commonprefix([prevf, f])
101 101 stemlen = min(len(stem), 255)
102 102 lines.append("%c%s\0%s\n%s\n" % (stemlen, f[stemlen:], fl, n))
103 103 prevf = f
104 104 _checkforbidden(files)
105 105 return ''.join(lines)
106 106
107 107 class _lazymanifest(dict):
108 108 """This is the pure implementation of lazymanifest.
109 109
110 110 It has not been optimized *at all* and is not lazy.
111 111 """
112 112
113 113 def __init__(self, data):
114 114 dict.__init__(self)
115 115 for f, n, fl in _parse(data):
116 116 self[f] = n, fl
117 117
118 118 def __setitem__(self, k, v):
119 119 node, flag = v
120 120 assert node is not None
121 121 if len(node) > 21:
122 122 node = node[:21] # match c implementation behavior
123 123 dict.__setitem__(self, k, (node, flag))
124 124
125 125 def __iter__(self):
126 126 return iter(sorted(dict.keys(self)))
127 127
128 128 def iterkeys(self):
129 129 return iter(sorted(dict.keys(self)))
130 130
131 131 def iterentries(self):
132 132 return ((f, e[0], e[1]) for f, e in sorted(self.iteritems()))
133 133
134 134 def copy(self):
135 135 c = _lazymanifest('')
136 136 c.update(self)
137 137 return c
138 138
139 139 def diff(self, m2, clean=False):
140 140 '''Finds changes between the current manifest and m2.'''
141 141 diff = {}
142 142
143 143 for fn, e1 in self.iteritems():
144 144 if fn not in m2:
145 145 diff[fn] = e1, (None, '')
146 146 else:
147 147 e2 = m2[fn]
148 148 if e1 != e2:
149 149 diff[fn] = e1, e2
150 150 elif clean:
151 151 diff[fn] = None
152 152
153 153 for fn, e2 in m2.iteritems():
154 154 if fn not in self:
155 155 diff[fn] = (None, ''), e2
156 156
157 157 return diff
158 158
159 159 def filtercopy(self, filterfn):
160 160 c = _lazymanifest('')
161 161 for f, n, fl in self.iterentries():
162 162 if filterfn(f):
163 163 c[f] = n, fl
164 164 return c
165 165
166 166 def text(self):
167 167 """Get the full data of this manifest as a bytestring."""
168 168 return _textv1(self.iterentries())
169 169
170 170 try:
171 171 _lazymanifest = parsers.lazymanifest
172 172 except AttributeError:
173 173 pass
174 174
175 175 class manifestdict(object):
176 176 def __init__(self, data=''):
177 177 if data.startswith('\0'):
178 178 #_lazymanifest can not parse v2
179 179 self._lm = _lazymanifest('')
180 180 for f, n, fl in _parsev2(data):
181 181 self._lm[f] = n, fl
182 182 else:
183 183 self._lm = _lazymanifest(data)
184 184
185 185 def __getitem__(self, key):
186 186 return self._lm[key][0]
187 187
188 188 def find(self, key):
189 189 return self._lm[key]
190 190
191 191 def __len__(self):
192 192 return len(self._lm)
193 193
194 194 def __setitem__(self, key, node):
195 195 self._lm[key] = node, self.flags(key, '')
196 196
197 197 def __contains__(self, key):
198 198 return key in self._lm
199 199
200 200 def __delitem__(self, key):
201 201 del self._lm[key]
202 202
203 203 def __iter__(self):
204 204 return self._lm.__iter__()
205 205
206 206 def iterkeys(self):
207 207 return self._lm.iterkeys()
208 208
209 209 def keys(self):
210 210 return list(self.iterkeys())
211 211
212 212 def filesnotin(self, m2):
213 213 '''Set of files in this manifest that are not in the other'''
214 214 diff = self.diff(m2)
215 215 files = set(filepath
216 216 for filepath, hashflags in diff.iteritems()
217 217 if hashflags[1][0] is None)
218 218 return files
219 219
220 220 @propertycache
221 221 def _dirs(self):
222 222 return util.dirs(self)
223 223
224 224 def dirs(self):
225 225 return self._dirs
226 226
227 227 def hasdir(self, dir):
228 228 return dir in self._dirs
229 229
230 230 def _filesfastpath(self, match):
231 231 '''Checks whether we can correctly and quickly iterate over matcher
232 232 files instead of over manifest files.'''
233 233 files = match.files()
234 234 return (len(files) < 100 and (match.isexact() or
235 235 (match.prefix() and all(fn in self for fn in files))))
236 236
237 237 def walk(self, match):
238 238 '''Generates matching file names.
239 239
240 240 Equivalent to manifest.matches(match).iterkeys(), but without creating
241 241 an entirely new manifest.
242 242
243 243 It also reports nonexistent files by marking them bad with match.bad().
244 244 '''
245 245 if match.always():
246 246 for f in iter(self):
247 247 yield f
248 248 return
249 249
250 250 fset = set(match.files())
251 251
252 252 # avoid the entire walk if we're only looking for specific files
253 253 if self._filesfastpath(match):
254 254 for fn in sorted(fset):
255 255 yield fn
256 256 return
257 257
258 258 for fn in self:
259 259 if fn in fset:
260 260 # specified pattern is the exact name
261 261 fset.remove(fn)
262 262 if match(fn):
263 263 yield fn
264 264
265 265 # for dirstate.walk, files=['.'] means "walk the whole tree".
266 266 # follow that here, too
267 267 fset.discard('.')
268 268
269 269 for fn in sorted(fset):
270 270 if not self.hasdir(fn):
271 271 match.bad(fn, None)
272 272
273 273 def matches(self, match):
274 274 '''generate a new manifest filtered by the match argument'''
275 275 if match.always():
276 276 return self.copy()
277 277
278 278 if self._filesfastpath(match):
279 279 m = manifestdict()
280 280 lm = self._lm
281 281 for fn in match.files():
282 282 if fn in lm:
283 283 m._lm[fn] = lm[fn]
284 284 return m
285 285
286 286 m = manifestdict()
287 287 m._lm = self._lm.filtercopy(match)
288 288 return m
289 289
290 290 def diff(self, m2, clean=False):
291 291 '''Finds changes between the current manifest and m2.
292 292
293 293 Args:
294 294 m2: the manifest to which this manifest should be compared.
295 295 clean: if true, include files unchanged between these manifests
296 296 with a None value in the returned dictionary.
297 297
298 298 The result is returned as a dict with filename as key and
299 299 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
300 300 nodeid in the current/other manifest and fl1/fl2 is the flag
301 301 in the current/other manifest. Where the file does not exist,
302 302 the nodeid will be None and the flags will be the empty
303 303 string.
304 304 '''
305 305 return self._lm.diff(m2._lm, clean)
306 306
307 307 def setflag(self, key, flag):
308 308 self._lm[key] = self[key], flag
309 309
310 310 def get(self, key, default=None):
311 311 try:
312 312 return self._lm[key][0]
313 313 except KeyError:
314 314 return default
315 315
316 316 def flags(self, key, default=''):
317 317 try:
318 318 return self._lm[key][1]
319 319 except KeyError:
320 320 return default
321 321
322 322 def copy(self):
323 323 c = manifestdict()
324 324 c._lm = self._lm.copy()
325 325 return c
326 326
327 327 def iteritems(self):
328 328 return (x[:2] for x in self._lm.iterentries())
329 329
330 330 def iterentries(self):
331 331 return self._lm.iterentries()
332 332
333 333 def text(self, usemanifestv2=False):
334 334 if usemanifestv2:
335 335 return _textv2(self._lm.iterentries())
336 336 else:
337 337 # use (probably) native version for v1
338 338 return self._lm.text()
339 339
340 340 def fastdelta(self, base, changes):
341 341 """Given a base manifest text as an array.array and a list of changes
342 342 relative to that text, compute a delta that can be used by revlog.
343 343 """
344 344 delta = []
345 345 dstart = None
346 346 dend = None
347 347 dline = [""]
348 348 start = 0
349 349 # zero copy representation of base as a buffer
350 350 addbuf = util.buffer(base)
351 351
352 352 changes = list(changes)
353 353 if len(changes) < 1000:
354 354 # start with a readonly loop that finds the offset of
355 355 # each line and creates the deltas
356 356 for f, todelete in changes:
357 357 # bs will either be the index of the item or the insert point
358 358 start, end = _msearch(addbuf, f, start)
359 359 if not todelete:
360 360 h, fl = self._lm[f]
361 361 l = "%s\0%s%s\n" % (f, revlog.hex(h), fl)
362 362 else:
363 363 if start == end:
364 364 # item we want to delete was not found, error out
365 365 raise AssertionError(
366 366 _("failed to remove %s from manifest") % f)
367 367 l = ""
368 368 if dstart is not None and dstart <= start and dend >= start:
369 369 if dend < end:
370 370 dend = end
371 371 if l:
372 372 dline.append(l)
373 373 else:
374 374 if dstart is not None:
375 375 delta.append([dstart, dend, "".join(dline)])
376 376 dstart = start
377 377 dend = end
378 378 dline = [l]
379 379
380 380 if dstart is not None:
381 381 delta.append([dstart, dend, "".join(dline)])
382 382 # apply the delta to the base, and get a delta for addrevision
383 383 deltatext, arraytext = _addlistdelta(base, delta)
384 384 else:
385 385 # For large changes, it's much cheaper to just build the text and
386 386 # diff it.
387 387 arraytext = array.array('c', self.text())
388 388 deltatext = mdiff.textdiff(base, arraytext)
389 389
390 390 return arraytext, deltatext
391 391
392 392 def _msearch(m, s, lo=0, hi=None):
393 393 '''return a tuple (start, end) that says where to find s within m.
394 394
395 395 If the string is found m[start:end] are the line containing
396 396 that string. If start == end the string was not found and
397 397 they indicate the proper sorted insertion point.
398 398
399 399 m should be a buffer or a string
400 400 s is a string'''
401 401 def advance(i, c):
402 402 while i < lenm and m[i] != c:
403 403 i += 1
404 404 return i
405 405 if not s:
406 406 return (lo, lo)
407 407 lenm = len(m)
408 408 if not hi:
409 409 hi = lenm
410 410 while lo < hi:
411 411 mid = (lo + hi) // 2
412 412 start = mid
413 413 while start > 0 and m[start - 1] != '\n':
414 414 start -= 1
415 415 end = advance(start, '\0')
416 416 if m[start:end] < s:
417 417 # we know that after the null there are 40 bytes of sha1
418 418 # this translates to the bisect lo = mid + 1
419 419 lo = advance(end + 40, '\n') + 1
420 420 else:
421 421 # this translates to the bisect hi = mid
422 422 hi = start
423 423 end = advance(lo, '\0')
424 424 found = m[lo:end]
425 425 if s == found:
426 426 # we know that after the null there are 40 bytes of sha1
427 427 end = advance(end + 40, '\n')
428 428 return (lo, end + 1)
429 429 else:
430 430 return (lo, lo)
431 431
432 432 def _checkforbidden(l):
433 433 """Check filenames for illegal characters."""
434 434 for f in l:
435 435 if '\n' in f or '\r' in f:
436 436 raise error.RevlogError(
437 437 _("'\\n' and '\\r' disallowed in filenames: %r") % f)
438 438
439 439
440 440 # apply the changes collected during the bisect loop to our addlist
441 441 # return a delta suitable for addrevision
442 442 def _addlistdelta(addlist, x):
443 443 # for large addlist arrays, building a new array is cheaper
444 444 # than repeatedly modifying the existing one
445 445 currentposition = 0
446 446 newaddlist = array.array('c')
447 447
448 448 for start, end, content in x:
449 449 newaddlist += addlist[currentposition:start]
450 450 if content:
451 451 newaddlist += array.array('c', content)
452 452
453 453 currentposition = end
454 454
455 455 newaddlist += addlist[currentposition:]
456 456
457 457 deltatext = "".join(struct.pack(">lll", start, end, len(content))
458 458 + content for start, end, content in x)
459 459 return deltatext, newaddlist
460 460
461 461 def _splittopdir(f):
462 462 if '/' in f:
463 463 dir, subpath = f.split('/', 1)
464 464 return dir + '/', subpath
465 465 else:
466 466 return '', f
467 467
468 468 _noop = lambda s: None
469 469
470 470 class treemanifest(object):
471 471 def __init__(self, dir='', text=''):
472 472 self._dir = dir
473 473 self._node = revlog.nullid
474 474 self._loadfunc = _noop
475 475 self._copyfunc = _noop
476 476 self._dirty = False
477 477 self._dirs = {}
478 478 # Using _lazymanifest here is a little slower than plain old dicts
479 479 self._files = {}
480 480 self._flags = {}
481 481 if text:
482 482 def readsubtree(subdir, subm):
483 483 raise AssertionError('treemanifest constructor only accepts '
484 484 'flat manifests')
485 485 self.parse(text, readsubtree)
486 486 self._dirty = True # Mark flat manifest dirty after parsing
487 487
488 488 def _subpath(self, path):
489 489 return self._dir + path
490 490
491 491 def __len__(self):
492 492 self._load()
493 493 size = len(self._files)
494 494 for m in self._dirs.values():
495 495 size += m.__len__()
496 496 return size
497 497
498 498 def _isempty(self):
499 499 self._load() # for consistency; already loaded by all callers
500 500 return (not self._files and (not self._dirs or
501 501 all(m._isempty() for m in self._dirs.values())))
502 502
503 503 def __repr__(self):
504 504 return ('<treemanifest dir=%s, node=%s, loaded=%s, dirty=%s at 0x%x>' %
505 505 (self._dir, revlog.hex(self._node),
506 506 bool(self._loadfunc is _noop),
507 507 self._dirty, id(self)))
508 508
509 509 def dir(self):
510 510 '''The directory that this tree manifest represents, including a
511 511 trailing '/'. Empty string for the repo root directory.'''
512 512 return self._dir
513 513
514 514 def node(self):
515 515 '''This node of this instance. nullid for unsaved instances. Should
516 516 be updated when the instance is read or written from a revlog.
517 517 '''
518 518 assert not self._dirty
519 519 return self._node
520 520
521 521 def setnode(self, node):
522 522 self._node = node
523 523 self._dirty = False
524 524
525 525 def iterentries(self):
526 526 self._load()
527 527 for p, n in sorted(self._dirs.items() + self._files.items()):
528 528 if p in self._files:
529 529 yield self._subpath(p), n, self._flags.get(p, '')
530 530 else:
531 531 for x in n.iterentries():
532 532 yield x
533 533
534 534 def iteritems(self):
535 535 self._load()
536 536 for p, n in sorted(self._dirs.items() + self._files.items()):
537 537 if p in self._files:
538 538 yield self._subpath(p), n
539 539 else:
540 540 for f, sn in n.iteritems():
541 541 yield f, sn
542 542
543 543 def iterkeys(self):
544 544 self._load()
545 545 for p in sorted(self._dirs.keys() + self._files.keys()):
546 546 if p in self._files:
547 547 yield self._subpath(p)
548 548 else:
549 549 for f in self._dirs[p].iterkeys():
550 550 yield f
551 551
552 552 def keys(self):
553 553 return list(self.iterkeys())
554 554
555 555 def __iter__(self):
556 556 return self.iterkeys()
557 557
558 558 def __contains__(self, f):
559 559 if f is None:
560 560 return False
561 561 self._load()
562 562 dir, subpath = _splittopdir(f)
563 563 if dir:
564 564 if dir not in self._dirs:
565 565 return False
566 566 return self._dirs[dir].__contains__(subpath)
567 567 else:
568 568 return f in self._files
569 569
570 570 def get(self, f, default=None):
571 571 self._load()
572 572 dir, subpath = _splittopdir(f)
573 573 if dir:
574 574 if dir not in self._dirs:
575 575 return default
576 576 return self._dirs[dir].get(subpath, default)
577 577 else:
578 578 return self._files.get(f, default)
579 579
580 580 def __getitem__(self, f):
581 581 self._load()
582 582 dir, subpath = _splittopdir(f)
583 583 if dir:
584 584 return self._dirs[dir].__getitem__(subpath)
585 585 else:
586 586 return self._files[f]
587 587
588 588 def flags(self, f):
589 589 self._load()
590 590 dir, subpath = _splittopdir(f)
591 591 if dir:
592 592 if dir not in self._dirs:
593 593 return ''
594 594 return self._dirs[dir].flags(subpath)
595 595 else:
596 596 if f in self._dirs:
597 597 return ''
598 598 return self._flags.get(f, '')
599 599
600 600 def find(self, f):
601 601 self._load()
602 602 dir, subpath = _splittopdir(f)
603 603 if dir:
604 604 return self._dirs[dir].find(subpath)
605 605 else:
606 606 return self._files[f], self._flags.get(f, '')
607 607
608 608 def __delitem__(self, f):
609 609 self._load()
610 610 dir, subpath = _splittopdir(f)
611 611 if dir:
612 612 self._dirs[dir].__delitem__(subpath)
613 613 # If the directory is now empty, remove it
614 614 if self._dirs[dir]._isempty():
615 615 del self._dirs[dir]
616 616 else:
617 617 del self._files[f]
618 618 if f in self._flags:
619 619 del self._flags[f]
620 620 self._dirty = True
621 621
622 622 def __setitem__(self, f, n):
623 623 assert n is not None
624 624 self._load()
625 625 dir, subpath = _splittopdir(f)
626 626 if dir:
627 627 if dir not in self._dirs:
628 628 self._dirs[dir] = treemanifest(self._subpath(dir))
629 629 self._dirs[dir].__setitem__(subpath, n)
630 630 else:
631 631 self._files[f] = n[:21] # to match manifestdict's behavior
632 632 self._dirty = True
633 633
634 634 def _load(self):
635 635 if self._loadfunc is not _noop:
636 636 lf, self._loadfunc = self._loadfunc, _noop
637 637 lf(self)
638 638 elif self._copyfunc is not _noop:
639 639 cf, self._copyfunc = self._copyfunc, _noop
640 640 cf(self)
641 641
642 642 def setflag(self, f, flags):
643 643 """Set the flags (symlink, executable) for path f."""
644 644 self._load()
645 645 dir, subpath = _splittopdir(f)
646 646 if dir:
647 647 if dir not in self._dirs:
648 648 self._dirs[dir] = treemanifest(self._subpath(dir))
649 649 self._dirs[dir].setflag(subpath, flags)
650 650 else:
651 651 self._flags[f] = flags
652 652 self._dirty = True
653 653
654 654 def copy(self):
655 655 copy = treemanifest(self._dir)
656 656 copy._node = self._node
657 657 copy._dirty = self._dirty
658 658 if self._copyfunc is _noop:
659 659 def _copyfunc(s):
660 660 self._load()
661 661 for d in self._dirs:
662 662 s._dirs[d] = self._dirs[d].copy()
663 663 s._files = dict.copy(self._files)
664 664 s._flags = dict.copy(self._flags)
665 665 if self._loadfunc is _noop:
666 666 _copyfunc(copy)
667 667 else:
668 668 copy._copyfunc = _copyfunc
669 669 else:
670 670 copy._copyfunc = self._copyfunc
671 671 return copy
672 672
673 673 def filesnotin(self, m2):
674 674 '''Set of files in this manifest that are not in the other'''
675 675 files = set()
676 676 def _filesnotin(t1, t2):
677 677 if t1._node == t2._node and not t1._dirty and not t2._dirty:
678 678 return
679 679 t1._load()
680 680 t2._load()
681 681 for d, m1 in t1._dirs.iteritems():
682 682 if d in t2._dirs:
683 683 m2 = t2._dirs[d]
684 684 _filesnotin(m1, m2)
685 685 else:
686 686 files.update(m1.iterkeys())
687 687
688 688 for fn in t1._files.iterkeys():
689 689 if fn not in t2._files:
690 690 files.add(t1._subpath(fn))
691 691
692 692 _filesnotin(self, m2)
693 693 return files
694 694
695 695 @propertycache
696 696 def _alldirs(self):
697 697 return util.dirs(self)
698 698
699 699 def dirs(self):
700 700 return self._alldirs
701 701
702 702 def hasdir(self, dir):
703 703 self._load()
704 704 topdir, subdir = _splittopdir(dir)
705 705 if topdir:
706 706 if topdir in self._dirs:
707 707 return self._dirs[topdir].hasdir(subdir)
708 708 return False
709 709 return (dir + '/') in self._dirs
710 710
711 711 def walk(self, match):
712 712 '''Generates matching file names.
713 713
714 714 Equivalent to manifest.matches(match).iterkeys(), but without creating
715 715 an entirely new manifest.
716 716
717 717 It also reports nonexistent files by marking them bad with match.bad().
718 718 '''
719 719 if match.always():
720 720 for f in iter(self):
721 721 yield f
722 722 return
723 723
724 724 fset = set(match.files())
725 725
726 726 for fn in self._walk(match):
727 727 if fn in fset:
728 728 # specified pattern is the exact name
729 729 fset.remove(fn)
730 730 yield fn
731 731
732 732 # for dirstate.walk, files=['.'] means "walk the whole tree".
733 733 # follow that here, too
734 734 fset.discard('.')
735 735
736 736 for fn in sorted(fset):
737 737 if not self.hasdir(fn):
738 738 match.bad(fn, None)
739 739
740 740 def _walk(self, match):
741 741 '''Recursively generates matching file names for walk().'''
742 742 if not match.visitdir(self._dir[:-1] or '.'):
743 743 return
744 744
745 745 # yield this dir's files and walk its submanifests
746 746 self._load()
747 747 for p in sorted(self._dirs.keys() + self._files.keys()):
748 748 if p in self._files:
749 749 fullp = self._subpath(p)
750 750 if match(fullp):
751 751 yield fullp
752 752 else:
753 753 for f in self._dirs[p]._walk(match):
754 754 yield f
755 755
756 756 def matches(self, match):
757 757 '''generate a new manifest filtered by the match argument'''
758 758 if match.always():
759 759 return self.copy()
760 760
761 761 return self._matches(match)
762 762
763 763 def _matches(self, match):
764 764 '''recursively generate a new manifest filtered by the match argument.
765 765 '''
766 766
767 767 visit = match.visitdir(self._dir[:-1] or '.')
768 768 if visit == 'all':
769 769 return self.copy()
770 770 ret = treemanifest(self._dir)
771 771 if not visit:
772 772 return ret
773 773
774 774 self._load()
775 775 for fn in self._files:
776 776 fullp = self._subpath(fn)
777 777 if not match(fullp):
778 778 continue
779 779 ret._files[fn] = self._files[fn]
780 780 if fn in self._flags:
781 781 ret._flags[fn] = self._flags[fn]
782 782
783 783 for dir, subm in self._dirs.iteritems():
784 784 m = subm._matches(match)
785 785 if not m._isempty():
786 786 ret._dirs[dir] = m
787 787
788 788 if not ret._isempty():
789 789 ret._dirty = True
790 790 return ret
791 791
792 792 def diff(self, m2, clean=False):
793 793 '''Finds changes between the current manifest and m2.
794 794
795 795 Args:
796 796 m2: the manifest to which this manifest should be compared.
797 797 clean: if true, include files unchanged between these manifests
798 798 with a None value in the returned dictionary.
799 799
800 800 The result is returned as a dict with filename as key and
801 801 values of the form ((n1,fl1),(n2,fl2)), where n1/n2 is the
802 802 nodeid in the current/other manifest and fl1/fl2 is the flag
803 803 in the current/other manifest. Where the file does not exist,
804 804 the nodeid will be None and the flags will be the empty
805 805 string.
806 806 '''
807 807 result = {}
808 808 emptytree = treemanifest()
809 809 def _diff(t1, t2):
810 810 if t1._node == t2._node and not t1._dirty and not t2._dirty:
811 811 return
812 812 t1._load()
813 813 t2._load()
814 814 for d, m1 in t1._dirs.iteritems():
815 815 m2 = t2._dirs.get(d, emptytree)
816 816 _diff(m1, m2)
817 817
818 818 for d, m2 in t2._dirs.iteritems():
819 819 if d not in t1._dirs:
820 820 _diff(emptytree, m2)
821 821
822 822 for fn, n1 in t1._files.iteritems():
823 823 fl1 = t1._flags.get(fn, '')
824 824 n2 = t2._files.get(fn, None)
825 825 fl2 = t2._flags.get(fn, '')
826 826 if n1 != n2 or fl1 != fl2:
827 827 result[t1._subpath(fn)] = ((n1, fl1), (n2, fl2))
828 828 elif clean:
829 829 result[t1._subpath(fn)] = None
830 830
831 831 for fn, n2 in t2._files.iteritems():
832 832 if fn not in t1._files:
833 833 fl2 = t2._flags.get(fn, '')
834 834 result[t2._subpath(fn)] = ((None, ''), (n2, fl2))
835 835
836 836 _diff(self, m2)
837 837 return result
838 838
839 839 def unmodifiedsince(self, m2):
840 840 return not self._dirty and not m2._dirty and self._node == m2._node
841 841
842 842 def parse(self, text, readsubtree):
843 843 for f, n, fl in _parse(text):
844 844 if fl == 't':
845 845 f = f + '/'
846 846 self._dirs[f] = readsubtree(self._subpath(f), n)
847 847 elif '/' in f:
848 848 # This is a flat manifest, so use __setitem__ and setflag rather
849 849 # than assigning directly to _files and _flags, so we can
850 850 # assign a path in a subdirectory, and to mark dirty (compared
851 851 # to nullid).
852 852 self[f] = n
853 853 if fl:
854 854 self.setflag(f, fl)
855 855 else:
856 856 # Assigning to _files and _flags avoids marking as dirty,
857 857 # and should be a little faster.
858 858 self._files[f] = n
859 859 if fl:
860 860 self._flags[f] = fl
861 861
862 862 def text(self, usemanifestv2=False):
863 863 """Get the full data of this manifest as a bytestring."""
864 864 self._load()
865 865 return _text(self.iterentries(), usemanifestv2)
866 866
867 867 def dirtext(self, usemanifestv2=False):
868 868 """Get the full data of this directory as a bytestring. Make sure that
869 869 any submanifests have been written first, so their nodeids are correct.
870 870 """
871 871 self._load()
872 872 flags = self.flags
873 873 dirs = [(d[:-1], self._dirs[d]._node, 't') for d in self._dirs]
874 874 files = [(f, self._files[f], flags(f)) for f in self._files]
875 875 return _text(sorted(dirs + files), usemanifestv2)
876 876
877 877 def read(self, gettext, readsubtree):
878 878 def _load_for_read(s):
879 879 s.parse(gettext(), readsubtree)
880 880 s._dirty = False
881 881 self._loadfunc = _load_for_read
882 882
883 883 def writesubtrees(self, m1, m2, writesubtree):
884 884 self._load() # for consistency; should never have any effect here
885 885 m1._load()
886 886 m2._load()
887 887 emptytree = treemanifest()
888 888 for d, subm in self._dirs.iteritems():
889 889 subp1 = m1._dirs.get(d, emptytree)._node
890 890 subp2 = m2._dirs.get(d, emptytree)._node
891 891 if subp1 == revlog.nullid:
892 892 subp1, subp2 = subp2, subp1
893 893 writesubtree(subm, subp1, subp2)
894 894
895 895 class manifestrevlog(revlog.revlog):
896 896 '''A revlog that stores manifest texts. This is responsible for caching the
897 897 full-text manifest contents.
898 898 '''
899 899 def __init__(self, opener, indexfile):
900 900 super(manifestrevlog, self).__init__(opener, indexfile)
901 901
902 902 # During normal operations, we expect to deal with not more than four
903 903 # revs at a time (such as during commit --amend). When rebasing large
904 904 # stacks of commits, the number can go up, hence the config knob below.
905 905 cachesize = 4
906 906 opts = getattr(opener, 'options', None)
907 907 if opts is not None:
908 908 cachesize = opts.get('manifestcachesize', cachesize)
909 909 self._fulltextcache = util.lrucachedict(cachesize)
910 910
911 911 @property
912 912 def fulltextcache(self):
913 913 return self._fulltextcache
914 914
915 915 def clearcaches(self):
916 916 super(manifestrevlog, self).clearcaches()
917 917 self._fulltextcache.clear()
918 918
919 919 class manifestlog(object):
920 920 """A collection class representing the collection of manifest snapshots
921 921 referenced by commits in the repository.
922 922
923 923 In this situation, 'manifest' refers to the abstract concept of a snapshot
924 924 of the list of files in the given commit. Consumers of the output of this
925 925 class do not care about the implementation details of the actual manifests
926 926 they receive (i.e. tree or flat or lazily loaded, etc)."""
927 927 def __init__(self, opener, repo):
928 928 self._repo = repo
929 929
930 930 # We'll separate this into it's own cache once oldmanifest is no longer
931 931 # used
932 932 self._mancache = repo.manifest._mancache
933 933
934 934 @property
935 935 def _revlog(self):
936 936 return self._repo.manifest
937 937
938 938 @property
939 939 def _oldmanifest(self):
940 940 # _revlog is the same as _oldmanifest right now, but we eventually want
941 941 # to delete _oldmanifest while still allowing manifestlog to access the
942 942 # revlog specific apis.
943 943 return self._repo.manifest
944 944
945 945 def __getitem__(self, node):
946 946 """Retrieves the manifest instance for the given node. Throws a KeyError
947 947 if not found.
948 948 """
949 949 if node in self._mancache:
950 950 cachemf = self._mancache[node]
951 951 # The old manifest may put non-ctx manifests in the cache, so skip
952 952 # those since they don't implement the full api.
953 953 if (isinstance(cachemf, manifestctx) or
954 954 isinstance(cachemf, treemanifestctx)):
955 955 return cachemf
956 956
957 957 if self._oldmanifest._treeinmem:
958 958 m = treemanifestctx(self._revlog, '', node)
959 959 else:
960 960 m = manifestctx(self._revlog, node)
961 961 if node != revlog.nullid:
962 962 self._mancache[node] = m
963 963 return m
964 964
965 965 class manifestctx(object):
966 966 """A class representing a single revision of a manifest, including its
967 967 contents, its parent revs, and its linkrev.
968 968 """
969 969 def __init__(self, revlog, node):
970 970 self._revlog = revlog
971 971 self._data = None
972 972
973 973 self._node = node
974 974
975 975 # TODO: We eventually want p1, p2, and linkrev exposed on this class,
976 976 # but let's add it later when something needs it and we can load it
977 977 # lazily.
978 978 #self.p1, self.p2 = revlog.parents(node)
979 979 #rev = revlog.rev(node)
980 980 #self.linkrev = revlog.linkrev(rev)
981 981
982 982 def node(self):
983 983 return self._node
984 984
985 985 def read(self):
986 986 if not self._data:
987 987 if self._node == revlog.nullid:
988 988 self._data = manifestdict()
989 989 else:
990 990 text = self._revlog.revision(self._node)
991 991 arraytext = array.array('c', text)
992 992 self._revlog._fulltextcache[self._node] = arraytext
993 993 self._data = manifestdict(text)
994 994 return self._data
995 995
996 def readfast(self):
997 rl = self._revlog
998 r = rl.rev(self._node)
999 deltaparent = rl.deltaparent(r)
1000 if deltaparent != revlog.nullrev and deltaparent in rl.parentrevs(r):
1001 return self.readdelta()
1002 return self.read()
1003
996 1004 def readdelta(self):
997 1005 revlog = self._revlog
998 1006 if revlog._usemanifestv2:
999 1007 # Need to perform a slow delta
1000 1008 r0 = revlog.deltaparent(revlog.rev(self._node))
1001 1009 m0 = manifestctx(revlog, revlog.node(r0)).read()
1002 1010 m1 = self.read()
1003 1011 md = manifestdict()
1004 1012 for f, ((n0, fl0), (n1, fl1)) in m0.diff(m1).iteritems():
1005 1013 if n1:
1006 1014 md[f] = n1
1007 1015 if fl1:
1008 1016 md.setflag(f, fl1)
1009 1017 return md
1010 1018
1011 1019 r = revlog.rev(self._node)
1012 1020 d = mdiff.patchtext(revlog.revdiff(revlog.deltaparent(r), r))
1013 1021 return manifestdict(d)
1014 1022
1015 1023 class treemanifestctx(object):
1016 1024 def __init__(self, revlog, dir, node):
1017 1025 revlog = revlog.dirlog(dir)
1018 1026 self._revlog = revlog
1019 1027 self._dir = dir
1020 1028 self._data = None
1021 1029
1022 1030 self._node = node
1023 1031
1024 1032 # TODO: Load p1/p2/linkrev lazily. They need to be lazily loaded so that
1025 1033 # we can instantiate treemanifestctx objects for directories we don't
1026 1034 # have on disk.
1027 1035 #self.p1, self.p2 = revlog.parents(node)
1028 1036 #rev = revlog.rev(node)
1029 1037 #self.linkrev = revlog.linkrev(rev)
1030 1038
1031 1039 def read(self):
1032 1040 if not self._data:
1033 1041 if self._node == revlog.nullid:
1034 1042 self._data = treemanifest()
1035 1043 elif self._revlog._treeondisk:
1036 1044 m = treemanifest(dir=self._dir)
1037 1045 def gettext():
1038 1046 return self._revlog.revision(self._node)
1039 1047 def readsubtree(dir, subm):
1040 1048 return treemanifestctx(self._revlog, dir, subm).read()
1041 1049 m.read(gettext, readsubtree)
1042 1050 m.setnode(self._node)
1043 1051 self._data = m
1044 1052 else:
1045 1053 text = self._revlog.revision(self._node)
1046 1054 arraytext = array.array('c', text)
1047 1055 self._revlog.fulltextcache[self._node] = arraytext
1048 1056 self._data = treemanifest(dir=self._dir, text=text)
1049 1057
1050 1058 return self._data
1051 1059
1052 1060 def node(self):
1053 1061 return self._node
1054 1062
1055 1063 def readdelta(self):
1056 1064 # Need to perform a slow delta
1057 1065 revlog = self._revlog
1058 1066 r0 = revlog.deltaparent(revlog.rev(self._node))
1059 1067 m0 = treemanifestctx(revlog, revlog.node(r0), dir=self._dir).read()
1060 1068 m1 = self.read()
1061 1069 md = treemanifest(dir=self._dir)
1062 1070 for f, ((n0, fl0), (n1, fl1)) in m0.diff(m1).iteritems():
1063 1071 if n1:
1064 1072 md[f] = n1
1065 1073 if fl1:
1066 1074 md.setflag(f, fl1)
1067 1075 return md
1068 1076
1077 def readfast(self):
1078 rl = self._revlog
1079 r = rl.rev(self._node)
1080 deltaparent = rl.deltaparent(r)
1081 if deltaparent != revlog.nullrev and deltaparent in rl.parentrevs(r):
1082 return self.readdelta()
1083 return self.read()
1084
1069 1085 class manifest(manifestrevlog):
1070 1086 def __init__(self, opener, dir='', dirlogcache=None):
1071 1087 '''The 'dir' and 'dirlogcache' arguments are for internal use by
1072 1088 manifest.manifest only. External users should create a root manifest
1073 1089 log with manifest.manifest(opener) and call dirlog() on it.
1074 1090 '''
1075 1091 # During normal operations, we expect to deal with not more than four
1076 1092 # revs at a time (such as during commit --amend). When rebasing large
1077 1093 # stacks of commits, the number can go up, hence the config knob below.
1078 1094 cachesize = 4
1079 1095 usetreemanifest = False
1080 1096 usemanifestv2 = False
1081 1097 opts = getattr(opener, 'options', None)
1082 1098 if opts is not None:
1083 1099 cachesize = opts.get('manifestcachesize', cachesize)
1084 1100 usetreemanifest = opts.get('treemanifest', usetreemanifest)
1085 1101 usemanifestv2 = opts.get('manifestv2', usemanifestv2)
1086 1102 self._mancache = util.lrucachedict(cachesize)
1087 1103 self._treeinmem = usetreemanifest
1088 1104 self._treeondisk = usetreemanifest
1089 1105 self._usemanifestv2 = usemanifestv2
1090 1106 indexfile = "00manifest.i"
1091 1107 if dir:
1092 1108 assert self._treeondisk, 'opts is %r' % opts
1093 1109 if not dir.endswith('/'):
1094 1110 dir = dir + '/'
1095 1111 indexfile = "meta/" + dir + "00manifest.i"
1096 1112 super(manifest, self).__init__(opener, indexfile)
1097 1113 self._dir = dir
1098 1114 # The dirlogcache is kept on the root manifest log
1099 1115 if dir:
1100 1116 self._dirlogcache = dirlogcache
1101 1117 else:
1102 1118 self._dirlogcache = {'': self}
1103 1119
1104 1120 def _newmanifest(self, data=''):
1105 1121 if self._treeinmem:
1106 1122 return treemanifest(self._dir, data)
1107 1123 return manifestdict(data)
1108 1124
1109 1125 def dirlog(self, dir):
1110 1126 if dir:
1111 1127 assert self._treeondisk
1112 1128 if dir not in self._dirlogcache:
1113 1129 self._dirlogcache[dir] = manifest(self.opener, dir,
1114 1130 self._dirlogcache)
1115 1131 return self._dirlogcache[dir]
1116 1132
1117 1133 def _slowreaddelta(self, node):
1118 1134 r0 = self.deltaparent(self.rev(node))
1119 1135 m0 = self.read(self.node(r0))
1120 1136 m1 = self.read(node)
1121 1137 md = self._newmanifest()
1122 1138 for f, ((n0, fl0), (n1, fl1)) in m0.diff(m1).iteritems():
1123 1139 if n1:
1124 1140 md[f] = n1
1125 1141 if fl1:
1126 1142 md.setflag(f, fl1)
1127 1143 return md
1128 1144
1129 1145 def readdelta(self, node):
1130 1146 if self._usemanifestv2 or self._treeondisk:
1131 1147 return self._slowreaddelta(node)
1132 1148 r = self.rev(node)
1133 1149 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1134 1150 return self._newmanifest(d)
1135 1151
1136 1152 def readshallowdelta(self, node):
1137 1153 '''For flat manifests, this is the same as readdelta(). For
1138 1154 treemanifests, this will read the delta for this revlog's directory,
1139 1155 without recursively reading subdirectory manifests. Instead, any
1140 1156 subdirectory entry will be reported as it appears in the manifests, i.e.
1141 1157 the subdirectory will be reported among files and distinguished only by
1142 1158 its 't' flag.'''
1143 1159 if not self._treeondisk:
1144 1160 return self.readdelta(node)
1145 1161 if self._usemanifestv2:
1146 1162 raise error.Abort(
1147 1163 _("readshallowdelta() not implemented for manifestv2"))
1148 1164 r = self.rev(node)
1149 1165 d = mdiff.patchtext(self.revdiff(self.deltaparent(r), r))
1150 1166 return manifestdict(d)
1151 1167
1152 def readfast(self, node):
1153 '''use the faster of readdelta or read
1154
1155 This will return a manifest which is either only the files
1156 added/modified relative to p1, or all files in the
1157 manifest. Which one is returned depends on the codepath used
1158 to retrieve the data.
1159 '''
1160 r = self.rev(node)
1161 deltaparent = self.deltaparent(r)
1162 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1163 return self.readdelta(node)
1164 return self.read(node)
1165
1166 1168 def readshallowfast(self, node):
1167 1169 '''like readfast(), but calls readshallowdelta() instead of readdelta()
1168 1170 '''
1169 1171 r = self.rev(node)
1170 1172 deltaparent = self.deltaparent(r)
1171 1173 if deltaparent != revlog.nullrev and deltaparent in self.parentrevs(r):
1172 1174 return self.readshallowdelta(node)
1173 1175 return self.readshallow(node)
1174 1176
1175 1177 def read(self, node):
1176 1178 if node == revlog.nullid:
1177 1179 return self._newmanifest() # don't upset local cache
1178 1180 if node in self._mancache:
1179 1181 cached = self._mancache[node]
1180 1182 if (isinstance(cached, manifestctx) or
1181 1183 isinstance(cached, treemanifestctx)):
1182 1184 cached = cached.read()
1183 1185 return cached
1184 1186 if self._treeondisk:
1185 1187 def gettext():
1186 1188 return self.revision(node)
1187 1189 def readsubtree(dir, subm):
1188 1190 return self.dirlog(dir).read(subm)
1189 1191 m = self._newmanifest()
1190 1192 m.read(gettext, readsubtree)
1191 1193 m.setnode(node)
1192 1194 arraytext = None
1193 1195 else:
1194 1196 text = self.revision(node)
1195 1197 m = self._newmanifest(text)
1196 1198 arraytext = array.array('c', text)
1197 1199 self._mancache[node] = m
1198 1200 self.fulltextcache[node] = arraytext
1199 1201 return m
1200 1202
1201 1203 def readshallow(self, node):
1202 1204 '''Reads the manifest in this directory. When using flat manifests,
1203 1205 this manifest will generally have files in subdirectories in it. Does
1204 1206 not cache the manifest as the callers generally do not read the same
1205 1207 version twice.'''
1206 1208 return manifestdict(self.revision(node))
1207 1209
1208 1210 def find(self, node, f):
1209 1211 '''look up entry for a single file efficiently.
1210 1212 return (node, flags) pair if found, (None, None) if not.'''
1211 1213 m = self.read(node)
1212 1214 try:
1213 1215 return m.find(f)
1214 1216 except KeyError:
1215 1217 return None, None
1216 1218
1217 1219 def add(self, m, transaction, link, p1, p2, added, removed):
1218 1220 if (p1 in self.fulltextcache and not self._treeinmem
1219 1221 and not self._usemanifestv2):
1220 1222 # If our first parent is in the manifest cache, we can
1221 1223 # compute a delta here using properties we know about the
1222 1224 # manifest up-front, which may save time later for the
1223 1225 # revlog layer.
1224 1226
1225 1227 _checkforbidden(added)
1226 1228 # combine the changed lists into one sorted iterator
1227 1229 work = heapq.merge([(x, False) for x in added],
1228 1230 [(x, True) for x in removed])
1229 1231
1230 1232 arraytext, deltatext = m.fastdelta(self.fulltextcache[p1], work)
1231 1233 cachedelta = self.rev(p1), deltatext
1232 1234 text = util.buffer(arraytext)
1233 1235 n = self.addrevision(text, transaction, link, p1, p2, cachedelta)
1234 1236 else:
1235 1237 # The first parent manifest isn't already loaded, so we'll
1236 1238 # just encode a fulltext of the manifest and pass that
1237 1239 # through to the revlog layer, and let it handle the delta
1238 1240 # process.
1239 1241 if self._treeondisk:
1240 1242 m1 = self.read(p1)
1241 1243 m2 = self.read(p2)
1242 1244 n = self._addtree(m, transaction, link, m1, m2)
1243 1245 arraytext = None
1244 1246 else:
1245 1247 text = m.text(self._usemanifestv2)
1246 1248 n = self.addrevision(text, transaction, link, p1, p2)
1247 1249 arraytext = array.array('c', text)
1248 1250
1249 1251 self._mancache[n] = m
1250 1252 self.fulltextcache[n] = arraytext
1251 1253
1252 1254 return n
1253 1255
1254 1256 def _addtree(self, m, transaction, link, m1, m2):
1255 1257 # If the manifest is unchanged compared to one parent,
1256 1258 # don't write a new revision
1257 1259 if m.unmodifiedsince(m1) or m.unmodifiedsince(m2):
1258 1260 return m.node()
1259 1261 def writesubtree(subm, subp1, subp2):
1260 1262 sublog = self.dirlog(subm.dir())
1261 1263 sublog.add(subm, transaction, link, subp1, subp2, None, None)
1262 1264 m.writesubtrees(m1, m2, writesubtree)
1263 1265 text = m.dirtext(self._usemanifestv2)
1264 1266 # Double-check whether contents are unchanged to one parent
1265 1267 if text == m1.dirtext(self._usemanifestv2):
1266 1268 n = m1.node()
1267 1269 elif text == m2.dirtext(self._usemanifestv2):
1268 1270 n = m2.node()
1269 1271 else:
1270 1272 n = self.addrevision(text, transaction, link, m1.node(), m2.node())
1271 1273 # Save nodeid so parent manifest can calculate its nodeid
1272 1274 m.setnode(n)
1273 1275 return n
1274 1276
1275 1277 def clearcaches(self):
1276 1278 super(manifest, self).clearcaches()
1277 1279 self._mancache.clear()
1278 1280 self._dirlogcache = {'': self}
General Comments 0
You need to be logged in to leave comments. Login now