##// END OF EJS Templates
workingctx: factor out post-status dirstate fixup...
Siddharth Agarwal -
r32812:add613cd default
parent child Browse files
Show More
@@ -1,2376 +1,2380 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 addednodeid,
18 18 bin,
19 19 hex,
20 20 modifiednodeid,
21 21 nullid,
22 22 nullrev,
23 23 short,
24 24 wdirid,
25 25 wdirnodes,
26 26 wdirrev,
27 27 )
28 28 from . import (
29 29 encoding,
30 30 error,
31 31 fileset,
32 32 match as matchmod,
33 33 mdiff,
34 34 obsolete as obsmod,
35 35 patch,
36 36 phases,
37 37 pycompat,
38 38 repoview,
39 39 revlog,
40 40 scmutil,
41 41 subrepo,
42 42 util,
43 43 )
44 44
45 45 propertycache = util.propertycache
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 r = short(self.node())
70 70 if pycompat.ispy3:
71 71 return r.decode('ascii')
72 72 return r
73 73
74 74 def __bytes__(self):
75 75 return short(self.node())
76 76
77 77 def __int__(self):
78 78 return self.rev()
79 79
80 80 def __repr__(self):
81 81 return r"<%s %s>" % (type(self).__name__, str(self))
82 82
83 83 def __eq__(self, other):
84 84 try:
85 85 return type(self) == type(other) and self._rev == other._rev
86 86 except AttributeError:
87 87 return False
88 88
89 89 def __ne__(self, other):
90 90 return not (self == other)
91 91
92 92 def __contains__(self, key):
93 93 return key in self._manifest
94 94
95 95 def __getitem__(self, key):
96 96 return self.filectx(key)
97 97
98 98 def __iter__(self):
99 99 return iter(self._manifest)
100 100
101 101 def _buildstatusmanifest(self, status):
102 102 """Builds a manifest that includes the given status results, if this is
103 103 a working copy context. For non-working copy contexts, it just returns
104 104 the normal manifest."""
105 105 return self.manifest()
106 106
107 107 def _matchstatus(self, other, match):
108 108 """return match.always if match is none
109 109
110 110 This internal method provides a way for child objects to override the
111 111 match operator.
112 112 """
113 113 return match or matchmod.always(self._repo.root, self._repo.getcwd())
114 114
115 115 def _buildstatus(self, other, s, match, listignored, listclean,
116 116 listunknown):
117 117 """build a status with respect to another context"""
118 118 # Load earliest manifest first for caching reasons. More specifically,
119 119 # if you have revisions 1000 and 1001, 1001 is probably stored as a
120 120 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
121 121 # 1000 and cache it so that when you read 1001, we just need to apply a
122 122 # delta to what's in the cache. So that's one full reconstruction + one
123 123 # delta application.
124 124 mf2 = None
125 125 if self.rev() is not None and self.rev() < other.rev():
126 126 mf2 = self._buildstatusmanifest(s)
127 127 mf1 = other._buildstatusmanifest(s)
128 128 if mf2 is None:
129 129 mf2 = self._buildstatusmanifest(s)
130 130
131 131 modified, added = [], []
132 132 removed = []
133 133 clean = []
134 134 deleted, unknown, ignored = s.deleted, s.unknown, s.ignored
135 135 deletedset = set(deleted)
136 136 d = mf1.diff(mf2, match=match, clean=listclean)
137 137 for fn, value in d.iteritems():
138 138 if fn in deletedset:
139 139 continue
140 140 if value is None:
141 141 clean.append(fn)
142 142 continue
143 143 (node1, flag1), (node2, flag2) = value
144 144 if node1 is None:
145 145 added.append(fn)
146 146 elif node2 is None:
147 147 removed.append(fn)
148 148 elif flag1 != flag2:
149 149 modified.append(fn)
150 150 elif node2 not in wdirnodes:
151 151 # When comparing files between two commits, we save time by
152 152 # not comparing the file contents when the nodeids differ.
153 153 # Note that this means we incorrectly report a reverted change
154 154 # to a file as a modification.
155 155 modified.append(fn)
156 156 elif self[fn].cmp(other[fn]):
157 157 modified.append(fn)
158 158 else:
159 159 clean.append(fn)
160 160
161 161 if removed:
162 162 # need to filter files if they are already reported as removed
163 163 unknown = [fn for fn in unknown if fn not in mf1 and
164 164 (not match or match(fn))]
165 165 ignored = [fn for fn in ignored if fn not in mf1 and
166 166 (not match or match(fn))]
167 167 # if they're deleted, don't report them as removed
168 168 removed = [fn for fn in removed if fn not in deletedset]
169 169
170 170 return scmutil.status(modified, added, removed, deleted, unknown,
171 171 ignored, clean)
172 172
173 173 @propertycache
174 174 def substate(self):
175 175 return subrepo.state(self, self._repo.ui)
176 176
177 177 def subrev(self, subpath):
178 178 return self.substate[subpath][1]
179 179
180 180 def rev(self):
181 181 return self._rev
182 182 def node(self):
183 183 return self._node
184 184 def hex(self):
185 185 return hex(self.node())
186 186 def manifest(self):
187 187 return self._manifest
188 188 def manifestctx(self):
189 189 return self._manifestctx
190 190 def repo(self):
191 191 return self._repo
192 192 def phasestr(self):
193 193 return phases.phasenames[self.phase()]
194 194 def mutable(self):
195 195 return self.phase() > phases.public
196 196
197 197 def getfileset(self, expr):
198 198 return fileset.getfileset(self, expr)
199 199
200 200 def obsolete(self):
201 201 """True if the changeset is obsolete"""
202 202 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
203 203
204 204 def extinct(self):
205 205 """True if the changeset is extinct"""
206 206 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
207 207
208 208 def unstable(self):
209 209 """True if the changeset is not obsolete but it's ancestor are"""
210 210 return self.rev() in obsmod.getrevs(self._repo, 'unstable')
211 211
212 212 def bumped(self):
213 213 """True if the changeset try to be a successor of a public changeset
214 214
215 215 Only non-public and non-obsolete changesets may be bumped.
216 216 """
217 217 return self.rev() in obsmod.getrevs(self._repo, 'bumped')
218 218
219 219 def divergent(self):
220 220 """Is a successors of a changeset with multiple possible successors set
221 221
222 222 Only non-public and non-obsolete changesets may be divergent.
223 223 """
224 224 return self.rev() in obsmod.getrevs(self._repo, 'divergent')
225 225
226 226 def troubled(self):
227 227 """True if the changeset is either unstable, bumped or divergent"""
228 228 return self.unstable() or self.bumped() or self.divergent()
229 229
230 230 def troubles(self):
231 231 """return the list of troubles affecting this changesets.
232 232
233 233 Troubles are returned as strings. possible values are:
234 234 - unstable,
235 235 - bumped,
236 236 - divergent.
237 237 """
238 238 troubles = []
239 239 if self.unstable():
240 240 troubles.append('unstable')
241 241 if self.bumped():
242 242 troubles.append('bumped')
243 243 if self.divergent():
244 244 troubles.append('divergent')
245 245 return troubles
246 246
247 247 def parents(self):
248 248 """return contexts for each parent changeset"""
249 249 return self._parents
250 250
251 251 def p1(self):
252 252 return self._parents[0]
253 253
254 254 def p2(self):
255 255 parents = self._parents
256 256 if len(parents) == 2:
257 257 return parents[1]
258 258 return changectx(self._repo, nullrev)
259 259
260 260 def _fileinfo(self, path):
261 261 if r'_manifest' in self.__dict__:
262 262 try:
263 263 return self._manifest[path], self._manifest.flags(path)
264 264 except KeyError:
265 265 raise error.ManifestLookupError(self._node, path,
266 266 _('not found in manifest'))
267 267 if r'_manifestdelta' in self.__dict__ or path in self.files():
268 268 if path in self._manifestdelta:
269 269 return (self._manifestdelta[path],
270 270 self._manifestdelta.flags(path))
271 271 mfl = self._repo.manifestlog
272 272 try:
273 273 node, flag = mfl[self._changeset.manifest].find(path)
274 274 except KeyError:
275 275 raise error.ManifestLookupError(self._node, path,
276 276 _('not found in manifest'))
277 277
278 278 return node, flag
279 279
280 280 def filenode(self, path):
281 281 return self._fileinfo(path)[0]
282 282
283 283 def flags(self, path):
284 284 try:
285 285 return self._fileinfo(path)[1]
286 286 except error.LookupError:
287 287 return ''
288 288
289 289 def sub(self, path, allowcreate=True):
290 290 '''return a subrepo for the stored revision of path, never wdir()'''
291 291 return subrepo.subrepo(self, path, allowcreate=allowcreate)
292 292
293 293 def nullsub(self, path, pctx):
294 294 return subrepo.nullsubrepo(self, path, pctx)
295 295
296 296 def workingsub(self, path):
297 297 '''return a subrepo for the stored revision, or wdir if this is a wdir
298 298 context.
299 299 '''
300 300 return subrepo.subrepo(self, path, allowwdir=True)
301 301
302 302 def match(self, pats=None, include=None, exclude=None, default='glob',
303 303 listsubrepos=False, badfn=None):
304 304 r = self._repo
305 305 return matchmod.match(r.root, r.getcwd(), pats,
306 306 include, exclude, default,
307 307 auditor=r.nofsauditor, ctx=self,
308 308 listsubrepos=listsubrepos, badfn=badfn)
309 309
310 310 def diff(self, ctx2=None, match=None, **opts):
311 311 """Returns a diff generator for the given contexts and matcher"""
312 312 if ctx2 is None:
313 313 ctx2 = self.p1()
314 314 if ctx2 is not None:
315 315 ctx2 = self._repo[ctx2]
316 316 diffopts = patch.diffopts(self._repo.ui, opts)
317 317 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
318 318
319 319 def dirs(self):
320 320 return self._manifest.dirs()
321 321
322 322 def hasdir(self, dir):
323 323 return self._manifest.hasdir(dir)
324 324
325 325 def status(self, other=None, match=None, listignored=False,
326 326 listclean=False, listunknown=False, listsubrepos=False):
327 327 """return status of files between two nodes or node and working
328 328 directory.
329 329
330 330 If other is None, compare this node with working directory.
331 331
332 332 returns (modified, added, removed, deleted, unknown, ignored, clean)
333 333 """
334 334
335 335 ctx1 = self
336 336 ctx2 = self._repo[other]
337 337
338 338 # This next code block is, admittedly, fragile logic that tests for
339 339 # reversing the contexts and wouldn't need to exist if it weren't for
340 340 # the fast (and common) code path of comparing the working directory
341 341 # with its first parent.
342 342 #
343 343 # What we're aiming for here is the ability to call:
344 344 #
345 345 # workingctx.status(parentctx)
346 346 #
347 347 # If we always built the manifest for each context and compared those,
348 348 # then we'd be done. But the special case of the above call means we
349 349 # just copy the manifest of the parent.
350 350 reversed = False
351 351 if (not isinstance(ctx1, changectx)
352 352 and isinstance(ctx2, changectx)):
353 353 reversed = True
354 354 ctx1, ctx2 = ctx2, ctx1
355 355
356 356 match = ctx2._matchstatus(ctx1, match)
357 357 r = scmutil.status([], [], [], [], [], [], [])
358 358 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
359 359 listunknown)
360 360
361 361 if reversed:
362 362 # Reverse added and removed. Clear deleted, unknown and ignored as
363 363 # these make no sense to reverse.
364 364 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
365 365 r.clean)
366 366
367 367 if listsubrepos:
368 368 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
369 369 try:
370 370 rev2 = ctx2.subrev(subpath)
371 371 except KeyError:
372 372 # A subrepo that existed in node1 was deleted between
373 373 # node1 and node2 (inclusive). Thus, ctx2's substate
374 374 # won't contain that subpath. The best we can do ignore it.
375 375 rev2 = None
376 376 submatch = matchmod.subdirmatcher(subpath, match)
377 377 s = sub.status(rev2, match=submatch, ignored=listignored,
378 378 clean=listclean, unknown=listunknown,
379 379 listsubrepos=True)
380 380 for rfiles, sfiles in zip(r, s):
381 381 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
382 382
383 383 for l in r:
384 384 l.sort()
385 385
386 386 return r
387 387
388 388 def _filterederror(repo, changeid):
389 389 """build an exception to be raised about a filtered changeid
390 390
391 391 This is extracted in a function to help extensions (eg: evolve) to
392 392 experiment with various message variants."""
393 393 if repo.filtername.startswith('visible'):
394 394 msg = _("hidden revision '%s'") % changeid
395 395 hint = _('use --hidden to access hidden revisions')
396 396 return error.FilteredRepoLookupError(msg, hint=hint)
397 397 msg = _("filtered revision '%s' (not in '%s' subset)")
398 398 msg %= (changeid, repo.filtername)
399 399 return error.FilteredRepoLookupError(msg)
400 400
401 401 class changectx(basectx):
402 402 """A changecontext object makes access to data related to a particular
403 403 changeset convenient. It represents a read-only context already present in
404 404 the repo."""
405 405 def __init__(self, repo, changeid=''):
406 406 """changeid is a revision number, node, or tag"""
407 407
408 408 # since basectx.__new__ already took care of copying the object, we
409 409 # don't need to do anything in __init__, so we just exit here
410 410 if isinstance(changeid, basectx):
411 411 return
412 412
413 413 if changeid == '':
414 414 changeid = '.'
415 415 self._repo = repo
416 416
417 417 try:
418 418 if isinstance(changeid, int):
419 419 self._node = repo.changelog.node(changeid)
420 420 self._rev = changeid
421 421 return
422 422 if not pycompat.ispy3 and isinstance(changeid, long):
423 423 changeid = str(changeid)
424 424 if changeid == 'null':
425 425 self._node = nullid
426 426 self._rev = nullrev
427 427 return
428 428 if changeid == 'tip':
429 429 self._node = repo.changelog.tip()
430 430 self._rev = repo.changelog.rev(self._node)
431 431 return
432 432 if changeid == '.' or changeid == repo.dirstate.p1():
433 433 # this is a hack to delay/avoid loading obsmarkers
434 434 # when we know that '.' won't be hidden
435 435 self._node = repo.dirstate.p1()
436 436 self._rev = repo.unfiltered().changelog.rev(self._node)
437 437 return
438 438 if len(changeid) == 20:
439 439 try:
440 440 self._node = changeid
441 441 self._rev = repo.changelog.rev(changeid)
442 442 return
443 443 except error.FilteredRepoLookupError:
444 444 raise
445 445 except LookupError:
446 446 pass
447 447
448 448 try:
449 449 r = int(changeid)
450 450 if '%d' % r != changeid:
451 451 raise ValueError
452 452 l = len(repo.changelog)
453 453 if r < 0:
454 454 r += l
455 455 if r < 0 or r >= l and r != wdirrev:
456 456 raise ValueError
457 457 self._rev = r
458 458 self._node = repo.changelog.node(r)
459 459 return
460 460 except error.FilteredIndexError:
461 461 raise
462 462 except (ValueError, OverflowError, IndexError):
463 463 pass
464 464
465 465 if len(changeid) == 40:
466 466 try:
467 467 self._node = bin(changeid)
468 468 self._rev = repo.changelog.rev(self._node)
469 469 return
470 470 except error.FilteredLookupError:
471 471 raise
472 472 except (TypeError, LookupError):
473 473 pass
474 474
475 475 # lookup bookmarks through the name interface
476 476 try:
477 477 self._node = repo.names.singlenode(repo, changeid)
478 478 self._rev = repo.changelog.rev(self._node)
479 479 return
480 480 except KeyError:
481 481 pass
482 482 except error.FilteredRepoLookupError:
483 483 raise
484 484 except error.RepoLookupError:
485 485 pass
486 486
487 487 self._node = repo.unfiltered().changelog._partialmatch(changeid)
488 488 if self._node is not None:
489 489 self._rev = repo.changelog.rev(self._node)
490 490 return
491 491
492 492 # lookup failed
493 493 # check if it might have come from damaged dirstate
494 494 #
495 495 # XXX we could avoid the unfiltered if we had a recognizable
496 496 # exception for filtered changeset access
497 497 if changeid in repo.unfiltered().dirstate.parents():
498 498 msg = _("working directory has unknown parent '%s'!")
499 499 raise error.Abort(msg % short(changeid))
500 500 try:
501 501 if len(changeid) == 20 and nonascii(changeid):
502 502 changeid = hex(changeid)
503 503 except TypeError:
504 504 pass
505 505 except (error.FilteredIndexError, error.FilteredLookupError,
506 506 error.FilteredRepoLookupError):
507 507 raise _filterederror(repo, changeid)
508 508 except IndexError:
509 509 pass
510 510 raise error.RepoLookupError(
511 511 _("unknown revision '%s'") % changeid)
512 512
513 513 def __hash__(self):
514 514 try:
515 515 return hash(self._rev)
516 516 except AttributeError:
517 517 return id(self)
518 518
519 519 def __nonzero__(self):
520 520 return self._rev != nullrev
521 521
522 522 __bool__ = __nonzero__
523 523
524 524 @propertycache
525 525 def _changeset(self):
526 526 return self._repo.changelog.changelogrevision(self.rev())
527 527
528 528 @propertycache
529 529 def _manifest(self):
530 530 return self._manifestctx.read()
531 531
532 532 @property
533 533 def _manifestctx(self):
534 534 return self._repo.manifestlog[self._changeset.manifest]
535 535
536 536 @propertycache
537 537 def _manifestdelta(self):
538 538 return self._manifestctx.readdelta()
539 539
540 540 @propertycache
541 541 def _parents(self):
542 542 repo = self._repo
543 543 p1, p2 = repo.changelog.parentrevs(self._rev)
544 544 if p2 == nullrev:
545 545 return [changectx(repo, p1)]
546 546 return [changectx(repo, p1), changectx(repo, p2)]
547 547
548 548 def changeset(self):
549 549 c = self._changeset
550 550 return (
551 551 c.manifest,
552 552 c.user,
553 553 c.date,
554 554 c.files,
555 555 c.description,
556 556 c.extra,
557 557 )
558 558 def manifestnode(self):
559 559 return self._changeset.manifest
560 560
561 561 def user(self):
562 562 return self._changeset.user
563 563 def date(self):
564 564 return self._changeset.date
565 565 def files(self):
566 566 return self._changeset.files
567 567 def description(self):
568 568 return self._changeset.description
569 569 def branch(self):
570 570 return encoding.tolocal(self._changeset.extra.get("branch"))
571 571 def closesbranch(self):
572 572 return 'close' in self._changeset.extra
573 573 def extra(self):
574 574 return self._changeset.extra
575 575 def tags(self):
576 576 return self._repo.nodetags(self._node)
577 577 def bookmarks(self):
578 578 return self._repo.nodebookmarks(self._node)
579 579 def phase(self):
580 580 return self._repo._phasecache.phase(self._repo, self._rev)
581 581 def hidden(self):
582 582 return self._rev in repoview.filterrevs(self._repo, 'visible')
583 583
584 584 def children(self):
585 585 """return contexts for each child changeset"""
586 586 c = self._repo.changelog.children(self._node)
587 587 return [changectx(self._repo, x) for x in c]
588 588
589 589 def ancestors(self):
590 590 for a in self._repo.changelog.ancestors([self._rev]):
591 591 yield changectx(self._repo, a)
592 592
593 593 def descendants(self):
594 594 for d in self._repo.changelog.descendants([self._rev]):
595 595 yield changectx(self._repo, d)
596 596
597 597 def filectx(self, path, fileid=None, filelog=None):
598 598 """get a file context from this changeset"""
599 599 if fileid is None:
600 600 fileid = self.filenode(path)
601 601 return filectx(self._repo, path, fileid=fileid,
602 602 changectx=self, filelog=filelog)
603 603
604 604 def ancestor(self, c2, warn=False):
605 605 """return the "best" ancestor context of self and c2
606 606
607 607 If there are multiple candidates, it will show a message and check
608 608 merge.preferancestor configuration before falling back to the
609 609 revlog ancestor."""
610 610 # deal with workingctxs
611 611 n2 = c2._node
612 612 if n2 is None:
613 613 n2 = c2._parents[0]._node
614 614 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
615 615 if not cahs:
616 616 anc = nullid
617 617 elif len(cahs) == 1:
618 618 anc = cahs[0]
619 619 else:
620 620 # experimental config: merge.preferancestor
621 621 for r in self._repo.ui.configlist('merge', 'preferancestor', ['*']):
622 622 try:
623 623 ctx = changectx(self._repo, r)
624 624 except error.RepoLookupError:
625 625 continue
626 626 anc = ctx.node()
627 627 if anc in cahs:
628 628 break
629 629 else:
630 630 anc = self._repo.changelog.ancestor(self._node, n2)
631 631 if warn:
632 632 self._repo.ui.status(
633 633 (_("note: using %s as ancestor of %s and %s\n") %
634 634 (short(anc), short(self._node), short(n2))) +
635 635 ''.join(_(" alternatively, use --config "
636 636 "merge.preferancestor=%s\n") %
637 637 short(n) for n in sorted(cahs) if n != anc))
638 638 return changectx(self._repo, anc)
639 639
640 640 def descendant(self, other):
641 641 """True if other is descendant of this changeset"""
642 642 return self._repo.changelog.descendant(self._rev, other._rev)
643 643
644 644 def walk(self, match):
645 645 '''Generates matching file names.'''
646 646
647 647 # Wrap match.bad method to have message with nodeid
648 648 def bad(fn, msg):
649 649 # The manifest doesn't know about subrepos, so don't complain about
650 650 # paths into valid subrepos.
651 651 if any(fn == s or fn.startswith(s + '/')
652 652 for s in self.substate):
653 653 return
654 654 match.bad(fn, _('no such file in rev %s') % self)
655 655
656 656 m = matchmod.badmatch(match, bad)
657 657 return self._manifest.walk(m)
658 658
659 659 def matches(self, match):
660 660 return self.walk(match)
661 661
662 662 class basefilectx(object):
663 663 """A filecontext object represents the common logic for its children:
664 664 filectx: read-only access to a filerevision that is already present
665 665 in the repo,
666 666 workingfilectx: a filecontext that represents files from the working
667 667 directory,
668 668 memfilectx: a filecontext that represents files in-memory,
669 669 overlayfilectx: duplicate another filecontext with some fields overridden.
670 670 """
671 671 @propertycache
672 672 def _filelog(self):
673 673 return self._repo.file(self._path)
674 674
675 675 @propertycache
676 676 def _changeid(self):
677 677 if r'_changeid' in self.__dict__:
678 678 return self._changeid
679 679 elif r'_changectx' in self.__dict__:
680 680 return self._changectx.rev()
681 681 elif r'_descendantrev' in self.__dict__:
682 682 # this file context was created from a revision with a known
683 683 # descendant, we can (lazily) correct for linkrev aliases
684 684 return self._adjustlinkrev(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 r'_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 __bool__ = __nonzero__
712 712
713 713 def __str__(self):
714 714 try:
715 715 return "%s@%s" % (self.path(), self._changectx)
716 716 except error.LookupError:
717 717 return "%s@???" % self.path()
718 718
719 719 def __repr__(self):
720 720 return "<%s %s>" % (type(self).__name__, str(self))
721 721
722 722 def __hash__(self):
723 723 try:
724 724 return hash((self._path, self._filenode))
725 725 except AttributeError:
726 726 return id(self)
727 727
728 728 def __eq__(self, other):
729 729 try:
730 730 return (type(self) == type(other) and self._path == other._path
731 731 and self._filenode == other._filenode)
732 732 except AttributeError:
733 733 return False
734 734
735 735 def __ne__(self, other):
736 736 return not (self == other)
737 737
738 738 def filerev(self):
739 739 return self._filerev
740 740 def filenode(self):
741 741 return self._filenode
742 742 @propertycache
743 743 def _flags(self):
744 744 return self._changectx.flags(self._path)
745 745 def flags(self):
746 746 return self._flags
747 747 def filelog(self):
748 748 return self._filelog
749 749 def rev(self):
750 750 return self._changeid
751 751 def linkrev(self):
752 752 return self._filelog.linkrev(self._filerev)
753 753 def node(self):
754 754 return self._changectx.node()
755 755 def hex(self):
756 756 return self._changectx.hex()
757 757 def user(self):
758 758 return self._changectx.user()
759 759 def date(self):
760 760 return self._changectx.date()
761 761 def files(self):
762 762 return self._changectx.files()
763 763 def description(self):
764 764 return self._changectx.description()
765 765 def branch(self):
766 766 return self._changectx.branch()
767 767 def extra(self):
768 768 return self._changectx.extra()
769 769 def phase(self):
770 770 return self._changectx.phase()
771 771 def phasestr(self):
772 772 return self._changectx.phasestr()
773 773 def manifest(self):
774 774 return self._changectx.manifest()
775 775 def changectx(self):
776 776 return self._changectx
777 777 def renamed(self):
778 778 return self._copied
779 779 def repo(self):
780 780 return self._repo
781 781 def size(self):
782 782 return len(self.data())
783 783
784 784 def path(self):
785 785 return self._path
786 786
787 787 def isbinary(self):
788 788 try:
789 789 return util.binary(self.data())
790 790 except IOError:
791 791 return False
792 792 def isexec(self):
793 793 return 'x' in self.flags()
794 794 def islink(self):
795 795 return 'l' in self.flags()
796 796
797 797 def isabsent(self):
798 798 """whether this filectx represents a file not in self._changectx
799 799
800 800 This is mainly for merge code to detect change/delete conflicts. This is
801 801 expected to be True for all subclasses of basectx."""
802 802 return False
803 803
804 804 _customcmp = False
805 805 def cmp(self, fctx):
806 806 """compare with other file context
807 807
808 808 returns True if different than fctx.
809 809 """
810 810 if fctx._customcmp:
811 811 return fctx.cmp(self)
812 812
813 813 if (fctx._filenode is None
814 814 and (self._repo._encodefilterpats
815 815 # if file data starts with '\1\n', empty metadata block is
816 816 # prepended, which adds 4 bytes to filelog.size().
817 817 or self.size() - 4 == fctx.size())
818 818 or self.size() == fctx.size()):
819 819 return self._filelog.cmp(self._filenode, fctx.data())
820 820
821 821 return True
822 822
823 823 def _adjustlinkrev(self, srcrev, inclusive=False):
824 824 """return the first ancestor of <srcrev> introducing <fnode>
825 825
826 826 If the linkrev of the file revision does not point to an ancestor of
827 827 srcrev, we'll walk down the ancestors until we find one introducing
828 828 this file revision.
829 829
830 830 :srcrev: the changeset revision we search ancestors from
831 831 :inclusive: if true, the src revision will also be checked
832 832 """
833 833 repo = self._repo
834 834 cl = repo.unfiltered().changelog
835 835 mfl = repo.manifestlog
836 836 # fetch the linkrev
837 837 lkr = self.linkrev()
838 838 # hack to reuse ancestor computation when searching for renames
839 839 memberanc = getattr(self, '_ancestrycontext', None)
840 840 iteranc = None
841 841 if srcrev is None:
842 842 # wctx case, used by workingfilectx during mergecopy
843 843 revs = [p.rev() for p in self._repo[None].parents()]
844 844 inclusive = True # we skipped the real (revless) source
845 845 else:
846 846 revs = [srcrev]
847 847 if memberanc is None:
848 848 memberanc = iteranc = cl.ancestors(revs, lkr,
849 849 inclusive=inclusive)
850 850 # check if this linkrev is an ancestor of srcrev
851 851 if lkr not in memberanc:
852 852 if iteranc is None:
853 853 iteranc = cl.ancestors(revs, lkr, inclusive=inclusive)
854 854 fnode = self._filenode
855 855 path = self._path
856 856 for a in iteranc:
857 857 ac = cl.read(a) # get changeset data (we avoid object creation)
858 858 if path in ac[3]: # checking the 'files' field.
859 859 # The file has been touched, check if the content is
860 860 # similar to the one we search for.
861 861 if fnode == mfl[ac[0]].readfast().get(path):
862 862 return a
863 863 # In theory, we should never get out of that loop without a result.
864 864 # But if manifest uses a buggy file revision (not children of the
865 865 # one it replaces) we could. Such a buggy situation will likely
866 866 # result is crash somewhere else at to some point.
867 867 return lkr
868 868
869 869 def introrev(self):
870 870 """return the rev of the changeset which introduced this file revision
871 871
872 872 This method is different from linkrev because it take into account the
873 873 changeset the filectx was created from. It ensures the returned
874 874 revision is one of its ancestors. This prevents bugs from
875 875 'linkrev-shadowing' when a file revision is used by multiple
876 876 changesets.
877 877 """
878 878 lkr = self.linkrev()
879 879 attrs = vars(self)
880 880 noctx = not ('_changeid' in attrs or '_changectx' in attrs)
881 881 if noctx or self.rev() == lkr:
882 882 return self.linkrev()
883 883 return self._adjustlinkrev(self.rev(), inclusive=True)
884 884
885 885 def _parentfilectx(self, path, fileid, filelog):
886 886 """create parent filectx keeping ancestry info for _adjustlinkrev()"""
887 887 fctx = filectx(self._repo, path, fileid=fileid, filelog=filelog)
888 888 if '_changeid' in vars(self) or '_changectx' in vars(self):
889 889 # If self is associated with a changeset (probably explicitly
890 890 # fed), ensure the created filectx is associated with a
891 891 # changeset that is an ancestor of self.changectx.
892 892 # This lets us later use _adjustlinkrev to get a correct link.
893 893 fctx._descendantrev = self.rev()
894 894 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
895 895 elif '_descendantrev' in vars(self):
896 896 # Otherwise propagate _descendantrev if we have one associated.
897 897 fctx._descendantrev = self._descendantrev
898 898 fctx._ancestrycontext = getattr(self, '_ancestrycontext', None)
899 899 return fctx
900 900
901 901 def parents(self):
902 902 _path = self._path
903 903 fl = self._filelog
904 904 parents = self._filelog.parents(self._filenode)
905 905 pl = [(_path, node, fl) for node in parents if node != nullid]
906 906
907 907 r = fl.renamed(self._filenode)
908 908 if r:
909 909 # - In the simple rename case, both parent are nullid, pl is empty.
910 910 # - In case of merge, only one of the parent is null id and should
911 911 # be replaced with the rename information. This parent is -always-
912 912 # the first one.
913 913 #
914 914 # As null id have always been filtered out in the previous list
915 915 # comprehension, inserting to 0 will always result in "replacing
916 916 # first nullid parent with rename information.
917 917 pl.insert(0, (r[0], r[1], self._repo.file(r[0])))
918 918
919 919 return [self._parentfilectx(path, fnode, l) for path, fnode, l in pl]
920 920
921 921 def p1(self):
922 922 return self.parents()[0]
923 923
924 924 def p2(self):
925 925 p = self.parents()
926 926 if len(p) == 2:
927 927 return p[1]
928 928 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
929 929
930 930 def annotate(self, follow=False, linenumber=False, skiprevs=None,
931 931 diffopts=None):
932 932 '''returns a list of tuples of ((ctx, number), line) for each line
933 933 in the file, where ctx is the filectx of the node where
934 934 that line was last changed; if linenumber parameter is true, number is
935 935 the line number at the first appearance in the managed file, otherwise,
936 936 number has a fixed value of False.
937 937 '''
938 938
939 939 def lines(text):
940 940 if text.endswith("\n"):
941 941 return text.count("\n")
942 942 return text.count("\n") + int(bool(text))
943 943
944 944 if linenumber:
945 945 def decorate(text, rev):
946 946 return ([(rev, i) for i in xrange(1, lines(text) + 1)], text)
947 947 else:
948 948 def decorate(text, rev):
949 949 return ([(rev, False)] * lines(text), text)
950 950
951 951 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
952 952
953 953 def parents(f):
954 954 # Cut _descendantrev here to mitigate the penalty of lazy linkrev
955 955 # adjustment. Otherwise, p._adjustlinkrev() would walk changelog
956 956 # from the topmost introrev (= srcrev) down to p.linkrev() if it
957 957 # isn't an ancestor of the srcrev.
958 958 f._changeid
959 959 pl = f.parents()
960 960
961 961 # Don't return renamed parents if we aren't following.
962 962 if not follow:
963 963 pl = [p for p in pl if p.path() == f.path()]
964 964
965 965 # renamed filectx won't have a filelog yet, so set it
966 966 # from the cache to save time
967 967 for p in pl:
968 968 if not '_filelog' in p.__dict__:
969 969 p._filelog = getlog(p.path())
970 970
971 971 return pl
972 972
973 973 # use linkrev to find the first changeset where self appeared
974 974 base = self
975 975 introrev = self.introrev()
976 976 if self.rev() != introrev:
977 977 base = self.filectx(self.filenode(), changeid=introrev)
978 978 if getattr(base, '_ancestrycontext', None) is None:
979 979 cl = self._repo.changelog
980 980 if introrev is None:
981 981 # wctx is not inclusive, but works because _ancestrycontext
982 982 # is used to test filelog revisions
983 983 ac = cl.ancestors([p.rev() for p in base.parents()],
984 984 inclusive=True)
985 985 else:
986 986 ac = cl.ancestors([introrev], inclusive=True)
987 987 base._ancestrycontext = ac
988 988
989 989 # This algorithm would prefer to be recursive, but Python is a
990 990 # bit recursion-hostile. Instead we do an iterative
991 991 # depth-first search.
992 992
993 993 # 1st DFS pre-calculates pcache and needed
994 994 visit = [base]
995 995 pcache = {}
996 996 needed = {base: 1}
997 997 while visit:
998 998 f = visit.pop()
999 999 if f in pcache:
1000 1000 continue
1001 1001 pl = parents(f)
1002 1002 pcache[f] = pl
1003 1003 for p in pl:
1004 1004 needed[p] = needed.get(p, 0) + 1
1005 1005 if p not in pcache:
1006 1006 visit.append(p)
1007 1007
1008 1008 # 2nd DFS does the actual annotate
1009 1009 visit[:] = [base]
1010 1010 hist = {}
1011 1011 while visit:
1012 1012 f = visit[-1]
1013 1013 if f in hist:
1014 1014 visit.pop()
1015 1015 continue
1016 1016
1017 1017 ready = True
1018 1018 pl = pcache[f]
1019 1019 for p in pl:
1020 1020 if p not in hist:
1021 1021 ready = False
1022 1022 visit.append(p)
1023 1023 if ready:
1024 1024 visit.pop()
1025 1025 curr = decorate(f.data(), f)
1026 1026 skipchild = False
1027 1027 if skiprevs is not None:
1028 1028 skipchild = f._changeid in skiprevs
1029 1029 curr = _annotatepair([hist[p] for p in pl], f, curr, skipchild,
1030 1030 diffopts)
1031 1031 for p in pl:
1032 1032 if needed[p] == 1:
1033 1033 del hist[p]
1034 1034 del needed[p]
1035 1035 else:
1036 1036 needed[p] -= 1
1037 1037
1038 1038 hist[f] = curr
1039 1039 del pcache[f]
1040 1040
1041 1041 return zip(hist[base][0], hist[base][1].splitlines(True))
1042 1042
1043 1043 def ancestors(self, followfirst=False):
1044 1044 visit = {}
1045 1045 c = self
1046 1046 if followfirst:
1047 1047 cut = 1
1048 1048 else:
1049 1049 cut = None
1050 1050
1051 1051 while True:
1052 1052 for parent in c.parents()[:cut]:
1053 1053 visit[(parent.linkrev(), parent.filenode())] = parent
1054 1054 if not visit:
1055 1055 break
1056 1056 c = visit.pop(max(visit))
1057 1057 yield c
1058 1058
1059 1059 def _annotatepair(parents, childfctx, child, skipchild, diffopts):
1060 1060 r'''
1061 1061 Given parent and child fctxes and annotate data for parents, for all lines
1062 1062 in either parent that match the child, annotate the child with the parent's
1063 1063 data.
1064 1064
1065 1065 Additionally, if `skipchild` is True, replace all other lines with parent
1066 1066 annotate data as well such that child is never blamed for any lines.
1067 1067
1068 1068 >>> oldfctx = 'old'
1069 1069 >>> p1fctx, p2fctx, childfctx = 'p1', 'p2', 'c'
1070 1070 >>> olddata = 'a\nb\n'
1071 1071 >>> p1data = 'a\nb\nc\n'
1072 1072 >>> p2data = 'a\nc\nd\n'
1073 1073 >>> childdata = 'a\nb2\nc\nc2\nd\n'
1074 1074 >>> diffopts = mdiff.diffopts()
1075 1075
1076 1076 >>> def decorate(text, rev):
1077 1077 ... return ([(rev, i) for i in xrange(1, text.count('\n') + 1)], text)
1078 1078
1079 1079 Basic usage:
1080 1080
1081 1081 >>> oldann = decorate(olddata, oldfctx)
1082 1082 >>> p1ann = decorate(p1data, p1fctx)
1083 1083 >>> p1ann = _annotatepair([oldann], p1fctx, p1ann, False, diffopts)
1084 1084 >>> p1ann[0]
1085 1085 [('old', 1), ('old', 2), ('p1', 3)]
1086 1086 >>> p2ann = decorate(p2data, p2fctx)
1087 1087 >>> p2ann = _annotatepair([oldann], p2fctx, p2ann, False, diffopts)
1088 1088 >>> p2ann[0]
1089 1089 [('old', 1), ('p2', 2), ('p2', 3)]
1090 1090
1091 1091 Test with multiple parents (note the difference caused by ordering):
1092 1092
1093 1093 >>> childann = decorate(childdata, childfctx)
1094 1094 >>> childann = _annotatepair([p1ann, p2ann], childfctx, childann, False,
1095 1095 ... diffopts)
1096 1096 >>> childann[0]
1097 1097 [('old', 1), ('c', 2), ('p2', 2), ('c', 4), ('p2', 3)]
1098 1098
1099 1099 >>> childann = decorate(childdata, childfctx)
1100 1100 >>> childann = _annotatepair([p2ann, p1ann], childfctx, childann, False,
1101 1101 ... diffopts)
1102 1102 >>> childann[0]
1103 1103 [('old', 1), ('c', 2), ('p1', 3), ('c', 4), ('p2', 3)]
1104 1104
1105 1105 Test with skipchild (note the difference caused by ordering):
1106 1106
1107 1107 >>> childann = decorate(childdata, childfctx)
1108 1108 >>> childann = _annotatepair([p1ann, p2ann], childfctx, childann, True,
1109 1109 ... diffopts)
1110 1110 >>> childann[0]
1111 1111 [('old', 1), ('old', 2), ('p2', 2), ('p2', 2), ('p2', 3)]
1112 1112
1113 1113 >>> childann = decorate(childdata, childfctx)
1114 1114 >>> childann = _annotatepair([p2ann, p1ann], childfctx, childann, True,
1115 1115 ... diffopts)
1116 1116 >>> childann[0]
1117 1117 [('old', 1), ('old', 2), ('p1', 3), ('p1', 3), ('p2', 3)]
1118 1118 '''
1119 1119 pblocks = [(parent, mdiff.allblocks(parent[1], child[1], opts=diffopts))
1120 1120 for parent in parents]
1121 1121
1122 1122 if skipchild:
1123 1123 # Need to iterate over the blocks twice -- make it a list
1124 1124 pblocks = [(p, list(blocks)) for (p, blocks) in pblocks]
1125 1125 # Mercurial currently prefers p2 over p1 for annotate.
1126 1126 # TODO: change this?
1127 1127 for parent, blocks in pblocks:
1128 1128 for (a1, a2, b1, b2), t in blocks:
1129 1129 # Changed blocks ('!') or blocks made only of blank lines ('~')
1130 1130 # belong to the child.
1131 1131 if t == '=':
1132 1132 child[0][b1:b2] = parent[0][a1:a2]
1133 1133
1134 1134 if skipchild:
1135 1135 # Now try and match up anything that couldn't be matched,
1136 1136 # Reversing pblocks maintains bias towards p2, matching above
1137 1137 # behavior.
1138 1138 pblocks.reverse()
1139 1139
1140 1140 # The heuristics are:
1141 1141 # * Work on blocks of changed lines (effectively diff hunks with -U0).
1142 1142 # This could potentially be smarter but works well enough.
1143 1143 # * For a non-matching section, do a best-effort fit. Match lines in
1144 1144 # diff hunks 1:1, dropping lines as necessary.
1145 1145 # * Repeat the last line as a last resort.
1146 1146
1147 1147 # First, replace as much as possible without repeating the last line.
1148 1148 remaining = [(parent, []) for parent, _blocks in pblocks]
1149 1149 for idx, (parent, blocks) in enumerate(pblocks):
1150 1150 for (a1, a2, b1, b2), _t in blocks:
1151 1151 if a2 - a1 >= b2 - b1:
1152 1152 for bk in xrange(b1, b2):
1153 1153 if child[0][bk][0] == childfctx:
1154 1154 ak = min(a1 + (bk - b1), a2 - 1)
1155 1155 child[0][bk] = parent[0][ak]
1156 1156 else:
1157 1157 remaining[idx][1].append((a1, a2, b1, b2))
1158 1158
1159 1159 # Then, look at anything left, which might involve repeating the last
1160 1160 # line.
1161 1161 for parent, blocks in remaining:
1162 1162 for a1, a2, b1, b2 in blocks:
1163 1163 for bk in xrange(b1, b2):
1164 1164 if child[0][bk][0] == childfctx:
1165 1165 ak = min(a1 + (bk - b1), a2 - 1)
1166 1166 child[0][bk] = parent[0][ak]
1167 1167 return child
1168 1168
1169 1169 class filectx(basefilectx):
1170 1170 """A filecontext object makes access to data related to a particular
1171 1171 filerevision convenient."""
1172 1172 def __init__(self, repo, path, changeid=None, fileid=None,
1173 1173 filelog=None, changectx=None):
1174 1174 """changeid can be a changeset revision, node, or tag.
1175 1175 fileid can be a file revision or node."""
1176 1176 self._repo = repo
1177 1177 self._path = path
1178 1178
1179 1179 assert (changeid is not None
1180 1180 or fileid is not None
1181 1181 or changectx is not None), \
1182 1182 ("bad args: changeid=%r, fileid=%r, changectx=%r"
1183 1183 % (changeid, fileid, changectx))
1184 1184
1185 1185 if filelog is not None:
1186 1186 self._filelog = filelog
1187 1187
1188 1188 if changeid is not None:
1189 1189 self._changeid = changeid
1190 1190 if changectx is not None:
1191 1191 self._changectx = changectx
1192 1192 if fileid is not None:
1193 1193 self._fileid = fileid
1194 1194
1195 1195 @propertycache
1196 1196 def _changectx(self):
1197 1197 try:
1198 1198 return changectx(self._repo, self._changeid)
1199 1199 except error.FilteredRepoLookupError:
1200 1200 # Linkrev may point to any revision in the repository. When the
1201 1201 # repository is filtered this may lead to `filectx` trying to build
1202 1202 # `changectx` for filtered revision. In such case we fallback to
1203 1203 # creating `changectx` on the unfiltered version of the reposition.
1204 1204 # This fallback should not be an issue because `changectx` from
1205 1205 # `filectx` are not used in complex operations that care about
1206 1206 # filtering.
1207 1207 #
1208 1208 # This fallback is a cheap and dirty fix that prevent several
1209 1209 # crashes. It does not ensure the behavior is correct. However the
1210 1210 # behavior was not correct before filtering either and "incorrect
1211 1211 # behavior" is seen as better as "crash"
1212 1212 #
1213 1213 # Linkrevs have several serious troubles with filtering that are
1214 1214 # complicated to solve. Proper handling of the issue here should be
1215 1215 # considered when solving linkrev issue are on the table.
1216 1216 return changectx(self._repo.unfiltered(), self._changeid)
1217 1217
1218 1218 def filectx(self, fileid, changeid=None):
1219 1219 '''opens an arbitrary revision of the file without
1220 1220 opening a new filelog'''
1221 1221 return filectx(self._repo, self._path, fileid=fileid,
1222 1222 filelog=self._filelog, changeid=changeid)
1223 1223
1224 1224 def rawdata(self):
1225 1225 return self._filelog.revision(self._filenode, raw=True)
1226 1226
1227 1227 def rawflags(self):
1228 1228 """low-level revlog flags"""
1229 1229 return self._filelog.flags(self._filerev)
1230 1230
1231 1231 def data(self):
1232 1232 try:
1233 1233 return self._filelog.read(self._filenode)
1234 1234 except error.CensoredNodeError:
1235 1235 if self._repo.ui.config("censor", "policy", "abort") == "ignore":
1236 1236 return ""
1237 1237 raise error.Abort(_("censored node: %s") % short(self._filenode),
1238 1238 hint=_("set censor.policy to ignore errors"))
1239 1239
1240 1240 def size(self):
1241 1241 return self._filelog.size(self._filerev)
1242 1242
1243 1243 @propertycache
1244 1244 def _copied(self):
1245 1245 """check if file was actually renamed in this changeset revision
1246 1246
1247 1247 If rename logged in file revision, we report copy for changeset only
1248 1248 if file revisions linkrev points back to the changeset in question
1249 1249 or both changeset parents contain different file revisions.
1250 1250 """
1251 1251
1252 1252 renamed = self._filelog.renamed(self._filenode)
1253 1253 if not renamed:
1254 1254 return renamed
1255 1255
1256 1256 if self.rev() == self.linkrev():
1257 1257 return renamed
1258 1258
1259 1259 name = self.path()
1260 1260 fnode = self._filenode
1261 1261 for p in self._changectx.parents():
1262 1262 try:
1263 1263 if fnode == p.filenode(name):
1264 1264 return None
1265 1265 except error.LookupError:
1266 1266 pass
1267 1267 return renamed
1268 1268
1269 1269 def children(self):
1270 1270 # hard for renames
1271 1271 c = self._filelog.children(self._filenode)
1272 1272 return [filectx(self._repo, self._path, fileid=x,
1273 1273 filelog=self._filelog) for x in c]
1274 1274
1275 1275 def _changesrange(fctx1, fctx2, linerange2, diffopts):
1276 1276 """Return `(diffinrange, linerange1)` where `diffinrange` is True
1277 1277 if diff from fctx2 to fctx1 has changes in linerange2 and
1278 1278 `linerange1` is the new line range for fctx1.
1279 1279 """
1280 1280 blocks = mdiff.allblocks(fctx1.data(), fctx2.data(), diffopts)
1281 1281 filteredblocks, linerange1 = mdiff.blocksinrange(blocks, linerange2)
1282 1282 diffinrange = any(stype == '!' for _, stype in filteredblocks)
1283 1283 return diffinrange, linerange1
1284 1284
1285 1285 def blockancestors(fctx, fromline, toline, followfirst=False):
1286 1286 """Yield ancestors of `fctx` with respect to the block of lines within
1287 1287 `fromline`-`toline` range.
1288 1288 """
1289 1289 diffopts = patch.diffopts(fctx._repo.ui)
1290 1290 introrev = fctx.introrev()
1291 1291 if fctx.rev() != introrev:
1292 1292 fctx = fctx.filectx(fctx.filenode(), changeid=introrev)
1293 1293 visit = {(fctx.linkrev(), fctx.filenode()): (fctx, (fromline, toline))}
1294 1294 while visit:
1295 1295 c, linerange2 = visit.pop(max(visit))
1296 1296 pl = c.parents()
1297 1297 if followfirst:
1298 1298 pl = pl[:1]
1299 1299 if not pl:
1300 1300 # The block originates from the initial revision.
1301 1301 yield c, linerange2
1302 1302 continue
1303 1303 inrange = False
1304 1304 for p in pl:
1305 1305 inrangep, linerange1 = _changesrange(p, c, linerange2, diffopts)
1306 1306 inrange = inrange or inrangep
1307 1307 if linerange1[0] == linerange1[1]:
1308 1308 # Parent's linerange is empty, meaning that the block got
1309 1309 # introduced in this revision; no need to go futher in this
1310 1310 # branch.
1311 1311 continue
1312 1312 # Set _descendantrev with 'c' (a known descendant) so that, when
1313 1313 # _adjustlinkrev is called for 'p', it receives this descendant
1314 1314 # (as srcrev) instead possibly topmost introrev.
1315 1315 p._descendantrev = c.rev()
1316 1316 visit[p.linkrev(), p.filenode()] = p, linerange1
1317 1317 if inrange:
1318 1318 yield c, linerange2
1319 1319
1320 1320 def blockdescendants(fctx, fromline, toline):
1321 1321 """Yield descendants of `fctx` with respect to the block of lines within
1322 1322 `fromline`-`toline` range.
1323 1323 """
1324 1324 # First possibly yield 'fctx' if it has changes in range with respect to
1325 1325 # its parents.
1326 1326 try:
1327 1327 c, linerange1 = next(blockancestors(fctx, fromline, toline))
1328 1328 except StopIteration:
1329 1329 pass
1330 1330 else:
1331 1331 if c == fctx:
1332 1332 yield c, linerange1
1333 1333
1334 1334 diffopts = patch.diffopts(fctx._repo.ui)
1335 1335 fl = fctx.filelog()
1336 1336 seen = {fctx.filerev(): (fctx, (fromline, toline))}
1337 1337 for i in fl.descendants([fctx.filerev()]):
1338 1338 c = fctx.filectx(i)
1339 1339 inrange = False
1340 1340 for x in fl.parentrevs(i):
1341 1341 try:
1342 1342 p, linerange2 = seen[x]
1343 1343 except KeyError:
1344 1344 # nullrev or other branch
1345 1345 continue
1346 1346 inrangep, linerange1 = _changesrange(c, p, linerange2, diffopts)
1347 1347 inrange = inrange or inrangep
1348 1348 # If revision 'i' has been seen (it's a merge), we assume that its
1349 1349 # line range is the same independently of which parents was used
1350 1350 # to compute it.
1351 1351 assert i not in seen or seen[i][1] == linerange1, (
1352 1352 'computed line range for %s is not consistent between '
1353 1353 'ancestor branches' % c)
1354 1354 seen[i] = c, linerange1
1355 1355 if inrange:
1356 1356 yield c, linerange1
1357 1357
1358 1358 class committablectx(basectx):
1359 1359 """A committablectx object provides common functionality for a context that
1360 1360 wants the ability to commit, e.g. workingctx or memctx."""
1361 1361 def __init__(self, repo, text="", user=None, date=None, extra=None,
1362 1362 changes=None):
1363 1363 self._repo = repo
1364 1364 self._rev = None
1365 1365 self._node = None
1366 1366 self._text = text
1367 1367 if date:
1368 1368 self._date = util.parsedate(date)
1369 1369 if user:
1370 1370 self._user = user
1371 1371 if changes:
1372 1372 self._status = changes
1373 1373
1374 1374 self._extra = {}
1375 1375 if extra:
1376 1376 self._extra = extra.copy()
1377 1377 if 'branch' not in self._extra:
1378 1378 try:
1379 1379 branch = encoding.fromlocal(self._repo.dirstate.branch())
1380 1380 except UnicodeDecodeError:
1381 1381 raise error.Abort(_('branch name not in UTF-8!'))
1382 1382 self._extra['branch'] = branch
1383 1383 if self._extra['branch'] == '':
1384 1384 self._extra['branch'] = 'default'
1385 1385
1386 1386 def __str__(self):
1387 1387 return str(self._parents[0]) + r"+"
1388 1388
1389 1389 def __bytes__(self):
1390 1390 return bytes(self._parents[0]) + "+"
1391 1391
1392 1392 def __nonzero__(self):
1393 1393 return True
1394 1394
1395 1395 __bool__ = __nonzero__
1396 1396
1397 1397 def _buildflagfunc(self):
1398 1398 # Create a fallback function for getting file flags when the
1399 1399 # filesystem doesn't support them
1400 1400
1401 1401 copiesget = self._repo.dirstate.copies().get
1402 1402 parents = self.parents()
1403 1403 if len(parents) < 2:
1404 1404 # when we have one parent, it's easy: copy from parent
1405 1405 man = parents[0].manifest()
1406 1406 def func(f):
1407 1407 f = copiesget(f, f)
1408 1408 return man.flags(f)
1409 1409 else:
1410 1410 # merges are tricky: we try to reconstruct the unstored
1411 1411 # result from the merge (issue1802)
1412 1412 p1, p2 = parents
1413 1413 pa = p1.ancestor(p2)
1414 1414 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1415 1415
1416 1416 def func(f):
1417 1417 f = copiesget(f, f) # may be wrong for merges with copies
1418 1418 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1419 1419 if fl1 == fl2:
1420 1420 return fl1
1421 1421 if fl1 == fla:
1422 1422 return fl2
1423 1423 if fl2 == fla:
1424 1424 return fl1
1425 1425 return '' # punt for conflicts
1426 1426
1427 1427 return func
1428 1428
1429 1429 @propertycache
1430 1430 def _flagfunc(self):
1431 1431 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1432 1432
1433 1433 @propertycache
1434 1434 def _status(self):
1435 1435 return self._repo.status()
1436 1436
1437 1437 @propertycache
1438 1438 def _user(self):
1439 1439 return self._repo.ui.username()
1440 1440
1441 1441 @propertycache
1442 1442 def _date(self):
1443 1443 ui = self._repo.ui
1444 1444 date = ui.configdate('devel', 'default-date')
1445 1445 if date is None:
1446 1446 date = util.makedate()
1447 1447 return date
1448 1448
1449 1449 def subrev(self, subpath):
1450 1450 return None
1451 1451
1452 1452 def manifestnode(self):
1453 1453 return None
1454 1454 def user(self):
1455 1455 return self._user or self._repo.ui.username()
1456 1456 def date(self):
1457 1457 return self._date
1458 1458 def description(self):
1459 1459 return self._text
1460 1460 def files(self):
1461 1461 return sorted(self._status.modified + self._status.added +
1462 1462 self._status.removed)
1463 1463
1464 1464 def modified(self):
1465 1465 return self._status.modified
1466 1466 def added(self):
1467 1467 return self._status.added
1468 1468 def removed(self):
1469 1469 return self._status.removed
1470 1470 def deleted(self):
1471 1471 return self._status.deleted
1472 1472 def branch(self):
1473 1473 return encoding.tolocal(self._extra['branch'])
1474 1474 def closesbranch(self):
1475 1475 return 'close' in self._extra
1476 1476 def extra(self):
1477 1477 return self._extra
1478 1478
1479 1479 def tags(self):
1480 1480 return []
1481 1481
1482 1482 def bookmarks(self):
1483 1483 b = []
1484 1484 for p in self.parents():
1485 1485 b.extend(p.bookmarks())
1486 1486 return b
1487 1487
1488 1488 def phase(self):
1489 1489 phase = phases.draft # default phase to draft
1490 1490 for p in self.parents():
1491 1491 phase = max(phase, p.phase())
1492 1492 return phase
1493 1493
1494 1494 def hidden(self):
1495 1495 return False
1496 1496
1497 1497 def children(self):
1498 1498 return []
1499 1499
1500 1500 def flags(self, path):
1501 1501 if r'_manifest' in self.__dict__:
1502 1502 try:
1503 1503 return self._manifest.flags(path)
1504 1504 except KeyError:
1505 1505 return ''
1506 1506
1507 1507 try:
1508 1508 return self._flagfunc(path)
1509 1509 except OSError:
1510 1510 return ''
1511 1511
1512 1512 def ancestor(self, c2):
1513 1513 """return the "best" ancestor context of self and c2"""
1514 1514 return self._parents[0].ancestor(c2) # punt on two parents for now
1515 1515
1516 1516 def walk(self, match):
1517 1517 '''Generates matching file names.'''
1518 1518 return sorted(self._repo.dirstate.walk(match, sorted(self.substate),
1519 1519 True, False))
1520 1520
1521 1521 def matches(self, match):
1522 1522 return sorted(self._repo.dirstate.matches(match))
1523 1523
1524 1524 def ancestors(self):
1525 1525 for p in self._parents:
1526 1526 yield p
1527 1527 for a in self._repo.changelog.ancestors(
1528 1528 [p.rev() for p in self._parents]):
1529 1529 yield changectx(self._repo, a)
1530 1530
1531 1531 def markcommitted(self, node):
1532 1532 """Perform post-commit cleanup necessary after committing this ctx
1533 1533
1534 1534 Specifically, this updates backing stores this working context
1535 1535 wraps to reflect the fact that the changes reflected by this
1536 1536 workingctx have been committed. For example, it marks
1537 1537 modified and added files as normal in the dirstate.
1538 1538
1539 1539 """
1540 1540
1541 1541 with self._repo.dirstate.parentchange():
1542 1542 for f in self.modified() + self.added():
1543 1543 self._repo.dirstate.normal(f)
1544 1544 for f in self.removed():
1545 1545 self._repo.dirstate.drop(f)
1546 1546 self._repo.dirstate.setparents(node)
1547 1547
1548 1548 # write changes out explicitly, because nesting wlock at
1549 1549 # runtime may prevent 'wlock.release()' in 'repo.commit()'
1550 1550 # from immediately doing so for subsequent changing files
1551 1551 self._repo.dirstate.write(self._repo.currenttransaction())
1552 1552
1553 1553 def dirty(self, missing=False, merge=True, branch=True):
1554 1554 return False
1555 1555
1556 1556 class workingctx(committablectx):
1557 1557 """A workingctx object makes access to data related to
1558 1558 the current working directory convenient.
1559 1559 date - any valid date string or (unixtime, offset), or None.
1560 1560 user - username string, or None.
1561 1561 extra - a dictionary of extra values, or None.
1562 1562 changes - a list of file lists as returned by localrepo.status()
1563 1563 or None to use the repository status.
1564 1564 """
1565 1565 def __init__(self, repo, text="", user=None, date=None, extra=None,
1566 1566 changes=None):
1567 1567 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1568 1568
1569 1569 def __iter__(self):
1570 1570 d = self._repo.dirstate
1571 1571 for f in d:
1572 1572 if d[f] != 'r':
1573 1573 yield f
1574 1574
1575 1575 def __contains__(self, key):
1576 1576 return self._repo.dirstate[key] not in "?r"
1577 1577
1578 1578 def hex(self):
1579 1579 return hex(wdirid)
1580 1580
1581 1581 @propertycache
1582 1582 def _parents(self):
1583 1583 p = self._repo.dirstate.parents()
1584 1584 if p[1] == nullid:
1585 1585 p = p[:-1]
1586 1586 return [changectx(self._repo, x) for x in p]
1587 1587
1588 1588 def filectx(self, path, filelog=None):
1589 1589 """get a file context from the working directory"""
1590 1590 return workingfilectx(self._repo, path, workingctx=self,
1591 1591 filelog=filelog)
1592 1592
1593 1593 def dirty(self, missing=False, merge=True, branch=True):
1594 1594 "check whether a working directory is modified"
1595 1595 # check subrepos first
1596 1596 for s in sorted(self.substate):
1597 1597 if self.sub(s).dirty():
1598 1598 return True
1599 1599 # check current working dir
1600 1600 return ((merge and self.p2()) or
1601 1601 (branch and self.branch() != self.p1().branch()) or
1602 1602 self.modified() or self.added() or self.removed() or
1603 1603 (missing and self.deleted()))
1604 1604
1605 1605 def add(self, list, prefix=""):
1606 1606 join = lambda f: os.path.join(prefix, f)
1607 1607 with self._repo.wlock():
1608 1608 ui, ds = self._repo.ui, self._repo.dirstate
1609 1609 rejected = []
1610 1610 lstat = self._repo.wvfs.lstat
1611 1611 for f in list:
1612 1612 scmutil.checkportable(ui, join(f))
1613 1613 try:
1614 1614 st = lstat(f)
1615 1615 except OSError:
1616 1616 ui.warn(_("%s does not exist!\n") % join(f))
1617 1617 rejected.append(f)
1618 1618 continue
1619 1619 if st.st_size > 10000000:
1620 1620 ui.warn(_("%s: up to %d MB of RAM may be required "
1621 1621 "to manage this file\n"
1622 1622 "(use 'hg revert %s' to cancel the "
1623 1623 "pending addition)\n")
1624 1624 % (f, 3 * st.st_size // 1000000, join(f)))
1625 1625 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1626 1626 ui.warn(_("%s not added: only files and symlinks "
1627 1627 "supported currently\n") % join(f))
1628 1628 rejected.append(f)
1629 1629 elif ds[f] in 'amn':
1630 1630 ui.warn(_("%s already tracked!\n") % join(f))
1631 1631 elif ds[f] == 'r':
1632 1632 ds.normallookup(f)
1633 1633 else:
1634 1634 ds.add(f)
1635 1635 return rejected
1636 1636
1637 1637 def forget(self, files, prefix=""):
1638 1638 join = lambda f: os.path.join(prefix, f)
1639 1639 with self._repo.wlock():
1640 1640 rejected = []
1641 1641 for f in files:
1642 1642 if f not in self._repo.dirstate:
1643 1643 self._repo.ui.warn(_("%s not tracked!\n") % join(f))
1644 1644 rejected.append(f)
1645 1645 elif self._repo.dirstate[f] != 'a':
1646 1646 self._repo.dirstate.remove(f)
1647 1647 else:
1648 1648 self._repo.dirstate.drop(f)
1649 1649 return rejected
1650 1650
1651 1651 def undelete(self, list):
1652 1652 pctxs = self.parents()
1653 1653 with self._repo.wlock():
1654 1654 for f in list:
1655 1655 if self._repo.dirstate[f] != 'r':
1656 1656 self._repo.ui.warn(_("%s not removed!\n") % f)
1657 1657 else:
1658 1658 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1659 1659 t = fctx.data()
1660 1660 self._repo.wwrite(f, t, fctx.flags())
1661 1661 self._repo.dirstate.normal(f)
1662 1662
1663 1663 def copy(self, source, dest):
1664 1664 try:
1665 1665 st = self._repo.wvfs.lstat(dest)
1666 1666 except OSError as err:
1667 1667 if err.errno != errno.ENOENT:
1668 1668 raise
1669 1669 self._repo.ui.warn(_("%s does not exist!\n") % dest)
1670 1670 return
1671 1671 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1672 1672 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1673 1673 "symbolic link\n") % dest)
1674 1674 else:
1675 1675 with self._repo.wlock():
1676 1676 if self._repo.dirstate[dest] in '?':
1677 1677 self._repo.dirstate.add(dest)
1678 1678 elif self._repo.dirstate[dest] in 'r':
1679 1679 self._repo.dirstate.normallookup(dest)
1680 1680 self._repo.dirstate.copy(source, dest)
1681 1681
1682 1682 def match(self, pats=None, include=None, exclude=None, default='glob',
1683 1683 listsubrepos=False, badfn=None):
1684 1684 r = self._repo
1685 1685
1686 1686 # Only a case insensitive filesystem needs magic to translate user input
1687 1687 # to actual case in the filesystem.
1688 1688 icasefs = not util.fscasesensitive(r.root)
1689 1689 return matchmod.match(r.root, r.getcwd(), pats, include, exclude,
1690 1690 default, auditor=r.auditor, ctx=self,
1691 1691 listsubrepos=listsubrepos, badfn=badfn,
1692 1692 icasefs=icasefs)
1693 1693
1694 1694 def _filtersuspectsymlink(self, files):
1695 1695 if not files or self._repo.dirstate._checklink:
1696 1696 return files
1697 1697
1698 1698 # Symlink placeholders may get non-symlink-like contents
1699 1699 # via user error or dereferencing by NFS or Samba servers,
1700 1700 # so we filter out any placeholders that don't look like a
1701 1701 # symlink
1702 1702 sane = []
1703 1703 for f in files:
1704 1704 if self.flags(f) == 'l':
1705 1705 d = self[f].data()
1706 1706 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1707 1707 self._repo.ui.debug('ignoring suspect symlink placeholder'
1708 1708 ' "%s"\n' % f)
1709 1709 continue
1710 1710 sane.append(f)
1711 1711 return sane
1712 1712
1713 1713 def _checklookup(self, files):
1714 1714 # check for any possibly clean files
1715 1715 if not files:
1716 1716 return [], [], []
1717 1717
1718 1718 modified = []
1719 1719 deleted = []
1720 1720 fixup = []
1721 1721 pctx = self._parents[0]
1722 1722 # do a full compare of any files that might have changed
1723 1723 for f in sorted(files):
1724 1724 try:
1725 1725 # This will return True for a file that got replaced by a
1726 1726 # directory in the interim, but fixing that is pretty hard.
1727 1727 if (f not in pctx or self.flags(f) != pctx.flags(f)
1728 1728 or pctx[f].cmp(self[f])):
1729 1729 modified.append(f)
1730 1730 else:
1731 1731 fixup.append(f)
1732 1732 except (IOError, OSError):
1733 1733 # A file become inaccessible in between? Mark it as deleted,
1734 1734 # matching dirstate behavior (issue5584).
1735 1735 # The dirstate has more complex behavior around whether a
1736 1736 # missing file matches a directory, etc, but we don't need to
1737 1737 # bother with that: if f has made it to this point, we're sure
1738 1738 # it's in the dirstate.
1739 1739 deleted.append(f)
1740 1740
1741 # update dirstate for files that are actually clean
1741 return modified, deleted, fixup
1742
1743 def _poststatusfixup(self, fixup):
1744 """update dirstate for files that are actually clean"""
1742 1745 if fixup:
1743 1746 try:
1744 1747 oldid = self._repo.dirstate.identity()
1745 1748
1746 1749 # updating the dirstate is optional
1747 1750 # so we don't wait on the lock
1748 1751 # wlock can invalidate the dirstate, so cache normal _after_
1749 1752 # taking the lock
1750 1753 with self._repo.wlock(False):
1751 1754 if self._repo.dirstate.identity() == oldid:
1752 1755 normal = self._repo.dirstate.normal
1753 1756 for f in fixup:
1754 1757 normal(f)
1755 1758 # write changes out explicitly, because nesting
1756 1759 # wlock at runtime may prevent 'wlock.release()'
1757 1760 # after this block from doing so for subsequent
1758 1761 # changing files
1759 1762 tr = self._repo.currenttransaction()
1760 1763 self._repo.dirstate.write(tr)
1761 1764 else:
1762 1765 # in this case, writing changes out breaks
1763 1766 # consistency, because .hg/dirstate was
1764 1767 # already changed simultaneously after last
1765 1768 # caching (see also issue5584 for detail)
1766 1769 self._repo.ui.debug('skip updating dirstate: '
1767 1770 'identity mismatch\n')
1768 1771 except error.LockError:
1769 1772 pass
1770 return modified, deleted, fixup
1771 1773
1772 1774 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1773 1775 unknown=False):
1774 1776 '''Gets the status from the dirstate -- internal use only.'''
1775 1777 listignored, listclean, listunknown = ignored, clean, unknown
1776 1778 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1777 1779 subrepos = []
1778 1780 if '.hgsub' in self:
1779 1781 subrepos = sorted(self.substate)
1780 1782 cmp, s = self._repo.dirstate.status(match, subrepos, listignored,
1781 1783 listclean, listunknown)
1782 1784
1783 1785 # check for any possibly clean files
1786 fixup = []
1784 1787 if cmp:
1785 1788 modified2, deleted2, fixup = self._checklookup(cmp)
1786 1789 s.modified.extend(modified2)
1787 1790 s.deleted.extend(deleted2)
1788 1791
1789 # update dirstate for files that are actually clean
1790 1792 if fixup and listclean:
1791 1793 s.clean.extend(fixup)
1792 1794
1795 self._poststatusfixup(fixup)
1796
1793 1797 if match.always():
1794 1798 # cache for performance
1795 1799 if s.unknown or s.ignored or s.clean:
1796 1800 # "_status" is cached with list*=False in the normal route
1797 1801 self._status = scmutil.status(s.modified, s.added, s.removed,
1798 1802 s.deleted, [], [], [])
1799 1803 else:
1800 1804 self._status = s
1801 1805
1802 1806 return s
1803 1807
1804 1808 @propertycache
1805 1809 def _manifest(self):
1806 1810 """generate a manifest corresponding to the values in self._status
1807 1811
1808 1812 This reuse the file nodeid from parent, but we use special node
1809 1813 identifiers for added and modified files. This is used by manifests
1810 1814 merge to see that files are different and by update logic to avoid
1811 1815 deleting newly added files.
1812 1816 """
1813 1817 return self._buildstatusmanifest(self._status)
1814 1818
1815 1819 def _buildstatusmanifest(self, status):
1816 1820 """Builds a manifest that includes the given status results."""
1817 1821 parents = self.parents()
1818 1822
1819 1823 man = parents[0].manifest().copy()
1820 1824
1821 1825 ff = self._flagfunc
1822 1826 for i, l in ((addednodeid, status.added),
1823 1827 (modifiednodeid, status.modified)):
1824 1828 for f in l:
1825 1829 man[f] = i
1826 1830 try:
1827 1831 man.setflag(f, ff(f))
1828 1832 except OSError:
1829 1833 pass
1830 1834
1831 1835 for f in status.deleted + status.removed:
1832 1836 if f in man:
1833 1837 del man[f]
1834 1838
1835 1839 return man
1836 1840
1837 1841 def _buildstatus(self, other, s, match, listignored, listclean,
1838 1842 listunknown):
1839 1843 """build a status with respect to another context
1840 1844
1841 1845 This includes logic for maintaining the fast path of status when
1842 1846 comparing the working directory against its parent, which is to skip
1843 1847 building a new manifest if self (working directory) is not comparing
1844 1848 against its parent (repo['.']).
1845 1849 """
1846 1850 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1847 1851 # Filter out symlinks that, in the case of FAT32 and NTFS filesystems,
1848 1852 # might have accidentally ended up with the entire contents of the file
1849 1853 # they are supposed to be linking to.
1850 1854 s.modified[:] = self._filtersuspectsymlink(s.modified)
1851 1855 if other != self._repo['.']:
1852 1856 s = super(workingctx, self)._buildstatus(other, s, match,
1853 1857 listignored, listclean,
1854 1858 listunknown)
1855 1859 return s
1856 1860
1857 1861 def _matchstatus(self, other, match):
1858 1862 """override the match method with a filter for directory patterns
1859 1863
1860 1864 We use inheritance to customize the match.bad method only in cases of
1861 1865 workingctx since it belongs only to the working directory when
1862 1866 comparing against the parent changeset.
1863 1867
1864 1868 If we aren't comparing against the working directory's parent, then we
1865 1869 just use the default match object sent to us.
1866 1870 """
1867 1871 superself = super(workingctx, self)
1868 1872 match = superself._matchstatus(other, match)
1869 1873 if other != self._repo['.']:
1870 1874 def bad(f, msg):
1871 1875 # 'f' may be a directory pattern from 'match.files()',
1872 1876 # so 'f not in ctx1' is not enough
1873 1877 if f not in other and not other.hasdir(f):
1874 1878 self._repo.ui.warn('%s: %s\n' %
1875 1879 (self._repo.dirstate.pathto(f), msg))
1876 1880 match.bad = bad
1877 1881 return match
1878 1882
1879 1883 class committablefilectx(basefilectx):
1880 1884 """A committablefilectx provides common functionality for a file context
1881 1885 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1882 1886 def __init__(self, repo, path, filelog=None, ctx=None):
1883 1887 self._repo = repo
1884 1888 self._path = path
1885 1889 self._changeid = None
1886 1890 self._filerev = self._filenode = None
1887 1891
1888 1892 if filelog is not None:
1889 1893 self._filelog = filelog
1890 1894 if ctx:
1891 1895 self._changectx = ctx
1892 1896
1893 1897 def __nonzero__(self):
1894 1898 return True
1895 1899
1896 1900 __bool__ = __nonzero__
1897 1901
1898 1902 def linkrev(self):
1899 1903 # linked to self._changectx no matter if file is modified or not
1900 1904 return self.rev()
1901 1905
1902 1906 def parents(self):
1903 1907 '''return parent filectxs, following copies if necessary'''
1904 1908 def filenode(ctx, path):
1905 1909 return ctx._manifest.get(path, nullid)
1906 1910
1907 1911 path = self._path
1908 1912 fl = self._filelog
1909 1913 pcl = self._changectx._parents
1910 1914 renamed = self.renamed()
1911 1915
1912 1916 if renamed:
1913 1917 pl = [renamed + (None,)]
1914 1918 else:
1915 1919 pl = [(path, filenode(pcl[0], path), fl)]
1916 1920
1917 1921 for pc in pcl[1:]:
1918 1922 pl.append((path, filenode(pc, path), fl))
1919 1923
1920 1924 return [self._parentfilectx(p, fileid=n, filelog=l)
1921 1925 for p, n, l in pl if n != nullid]
1922 1926
1923 1927 def children(self):
1924 1928 return []
1925 1929
1926 1930 class workingfilectx(committablefilectx):
1927 1931 """A workingfilectx object makes access to data related to a particular
1928 1932 file in the working directory convenient."""
1929 1933 def __init__(self, repo, path, filelog=None, workingctx=None):
1930 1934 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1931 1935
1932 1936 @propertycache
1933 1937 def _changectx(self):
1934 1938 return workingctx(self._repo)
1935 1939
1936 1940 def data(self):
1937 1941 return self._repo.wread(self._path)
1938 1942 def renamed(self):
1939 1943 rp = self._repo.dirstate.copied(self._path)
1940 1944 if not rp:
1941 1945 return None
1942 1946 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1943 1947
1944 1948 def size(self):
1945 1949 return self._repo.wvfs.lstat(self._path).st_size
1946 1950 def date(self):
1947 1951 t, tz = self._changectx.date()
1948 1952 try:
1949 1953 return (self._repo.wvfs.lstat(self._path).st_mtime, tz)
1950 1954 except OSError as err:
1951 1955 if err.errno != errno.ENOENT:
1952 1956 raise
1953 1957 return (t, tz)
1954 1958
1955 1959 def cmp(self, fctx):
1956 1960 """compare with other file context
1957 1961
1958 1962 returns True if different than fctx.
1959 1963 """
1960 1964 # fctx should be a filectx (not a workingfilectx)
1961 1965 # invert comparison to reuse the same code path
1962 1966 return fctx.cmp(self)
1963 1967
1964 1968 def remove(self, ignoremissing=False):
1965 1969 """wraps unlink for a repo's working directory"""
1966 1970 self._repo.wvfs.unlinkpath(self._path, ignoremissing=ignoremissing)
1967 1971
1968 1972 def write(self, data, flags):
1969 1973 """wraps repo.wwrite"""
1970 1974 self._repo.wwrite(self._path, data, flags)
1971 1975
1972 1976 class workingcommitctx(workingctx):
1973 1977 """A workingcommitctx object makes access to data related to
1974 1978 the revision being committed convenient.
1975 1979
1976 1980 This hides changes in the working directory, if they aren't
1977 1981 committed in this context.
1978 1982 """
1979 1983 def __init__(self, repo, changes,
1980 1984 text="", user=None, date=None, extra=None):
1981 1985 super(workingctx, self).__init__(repo, text, user, date, extra,
1982 1986 changes)
1983 1987
1984 1988 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1985 1989 unknown=False):
1986 1990 """Return matched files only in ``self._status``
1987 1991
1988 1992 Uncommitted files appear "clean" via this context, even if
1989 1993 they aren't actually so in the working directory.
1990 1994 """
1991 1995 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1992 1996 if clean:
1993 1997 clean = [f for f in self._manifest if f not in self._changedset]
1994 1998 else:
1995 1999 clean = []
1996 2000 return scmutil.status([f for f in self._status.modified if match(f)],
1997 2001 [f for f in self._status.added if match(f)],
1998 2002 [f for f in self._status.removed if match(f)],
1999 2003 [], [], [], clean)
2000 2004
2001 2005 @propertycache
2002 2006 def _changedset(self):
2003 2007 """Return the set of files changed in this context
2004 2008 """
2005 2009 changed = set(self._status.modified)
2006 2010 changed.update(self._status.added)
2007 2011 changed.update(self._status.removed)
2008 2012 return changed
2009 2013
2010 2014 def makecachingfilectxfn(func):
2011 2015 """Create a filectxfn that caches based on the path.
2012 2016
2013 2017 We can't use util.cachefunc because it uses all arguments as the cache
2014 2018 key and this creates a cycle since the arguments include the repo and
2015 2019 memctx.
2016 2020 """
2017 2021 cache = {}
2018 2022
2019 2023 def getfilectx(repo, memctx, path):
2020 2024 if path not in cache:
2021 2025 cache[path] = func(repo, memctx, path)
2022 2026 return cache[path]
2023 2027
2024 2028 return getfilectx
2025 2029
2026 2030 def memfilefromctx(ctx):
2027 2031 """Given a context return a memfilectx for ctx[path]
2028 2032
2029 2033 This is a convenience method for building a memctx based on another
2030 2034 context.
2031 2035 """
2032 2036 def getfilectx(repo, memctx, path):
2033 2037 fctx = ctx[path]
2034 2038 # this is weird but apparently we only keep track of one parent
2035 2039 # (why not only store that instead of a tuple?)
2036 2040 copied = fctx.renamed()
2037 2041 if copied:
2038 2042 copied = copied[0]
2039 2043 return memfilectx(repo, path, fctx.data(),
2040 2044 islink=fctx.islink(), isexec=fctx.isexec(),
2041 2045 copied=copied, memctx=memctx)
2042 2046
2043 2047 return getfilectx
2044 2048
2045 2049 def memfilefrompatch(patchstore):
2046 2050 """Given a patch (e.g. patchstore object) return a memfilectx
2047 2051
2048 2052 This is a convenience method for building a memctx based on a patchstore.
2049 2053 """
2050 2054 def getfilectx(repo, memctx, path):
2051 2055 data, mode, copied = patchstore.getfile(path)
2052 2056 if data is None:
2053 2057 return None
2054 2058 islink, isexec = mode
2055 2059 return memfilectx(repo, path, data, islink=islink,
2056 2060 isexec=isexec, copied=copied,
2057 2061 memctx=memctx)
2058 2062
2059 2063 return getfilectx
2060 2064
2061 2065 class memctx(committablectx):
2062 2066 """Use memctx to perform in-memory commits via localrepo.commitctx().
2063 2067
2064 2068 Revision information is supplied at initialization time while
2065 2069 related files data and is made available through a callback
2066 2070 mechanism. 'repo' is the current localrepo, 'parents' is a
2067 2071 sequence of two parent revisions identifiers (pass None for every
2068 2072 missing parent), 'text' is the commit message and 'files' lists
2069 2073 names of files touched by the revision (normalized and relative to
2070 2074 repository root).
2071 2075
2072 2076 filectxfn(repo, memctx, path) is a callable receiving the
2073 2077 repository, the current memctx object and the normalized path of
2074 2078 requested file, relative to repository root. It is fired by the
2075 2079 commit function for every file in 'files', but calls order is
2076 2080 undefined. If the file is available in the revision being
2077 2081 committed (updated or added), filectxfn returns a memfilectx
2078 2082 object. If the file was removed, filectxfn return None for recent
2079 2083 Mercurial. Moved files are represented by marking the source file
2080 2084 removed and the new file added with copy information (see
2081 2085 memfilectx).
2082 2086
2083 2087 user receives the committer name and defaults to current
2084 2088 repository username, date is the commit date in any format
2085 2089 supported by util.parsedate() and defaults to current date, extra
2086 2090 is a dictionary of metadata or is left empty.
2087 2091 """
2088 2092
2089 2093 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
2090 2094 # Extensions that need to retain compatibility across Mercurial 3.1 can use
2091 2095 # this field to determine what to do in filectxfn.
2092 2096 _returnnoneformissingfiles = True
2093 2097
2094 2098 def __init__(self, repo, parents, text, files, filectxfn, user=None,
2095 2099 date=None, extra=None, branch=None, editor=False):
2096 2100 super(memctx, self).__init__(repo, text, user, date, extra)
2097 2101 self._rev = None
2098 2102 self._node = None
2099 2103 parents = [(p or nullid) for p in parents]
2100 2104 p1, p2 = parents
2101 2105 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
2102 2106 files = sorted(set(files))
2103 2107 self._files = files
2104 2108 if branch is not None:
2105 2109 self._extra['branch'] = encoding.fromlocal(branch)
2106 2110 self.substate = {}
2107 2111
2108 2112 if isinstance(filectxfn, patch.filestore):
2109 2113 filectxfn = memfilefrompatch(filectxfn)
2110 2114 elif not callable(filectxfn):
2111 2115 # if store is not callable, wrap it in a function
2112 2116 filectxfn = memfilefromctx(filectxfn)
2113 2117
2114 2118 # memoizing increases performance for e.g. vcs convert scenarios.
2115 2119 self._filectxfn = makecachingfilectxfn(filectxfn)
2116 2120
2117 2121 if editor:
2118 2122 self._text = editor(self._repo, self, [])
2119 2123 self._repo.savecommitmessage(self._text)
2120 2124
2121 2125 def filectx(self, path, filelog=None):
2122 2126 """get a file context from the working directory
2123 2127
2124 2128 Returns None if file doesn't exist and should be removed."""
2125 2129 return self._filectxfn(self._repo, self, path)
2126 2130
2127 2131 def commit(self):
2128 2132 """commit context to the repo"""
2129 2133 return self._repo.commitctx(self)
2130 2134
2131 2135 @propertycache
2132 2136 def _manifest(self):
2133 2137 """generate a manifest based on the return values of filectxfn"""
2134 2138
2135 2139 # keep this simple for now; just worry about p1
2136 2140 pctx = self._parents[0]
2137 2141 man = pctx.manifest().copy()
2138 2142
2139 2143 for f in self._status.modified:
2140 2144 p1node = nullid
2141 2145 p2node = nullid
2142 2146 p = pctx[f].parents() # if file isn't in pctx, check p2?
2143 2147 if len(p) > 0:
2144 2148 p1node = p[0].filenode()
2145 2149 if len(p) > 1:
2146 2150 p2node = p[1].filenode()
2147 2151 man[f] = revlog.hash(self[f].data(), p1node, p2node)
2148 2152
2149 2153 for f in self._status.added:
2150 2154 man[f] = revlog.hash(self[f].data(), nullid, nullid)
2151 2155
2152 2156 for f in self._status.removed:
2153 2157 if f in man:
2154 2158 del man[f]
2155 2159
2156 2160 return man
2157 2161
2158 2162 @propertycache
2159 2163 def _status(self):
2160 2164 """Calculate exact status from ``files`` specified at construction
2161 2165 """
2162 2166 man1 = self.p1().manifest()
2163 2167 p2 = self._parents[1]
2164 2168 # "1 < len(self._parents)" can't be used for checking
2165 2169 # existence of the 2nd parent, because "memctx._parents" is
2166 2170 # explicitly initialized by the list, of which length is 2.
2167 2171 if p2.node() != nullid:
2168 2172 man2 = p2.manifest()
2169 2173 managing = lambda f: f in man1 or f in man2
2170 2174 else:
2171 2175 managing = lambda f: f in man1
2172 2176
2173 2177 modified, added, removed = [], [], []
2174 2178 for f in self._files:
2175 2179 if not managing(f):
2176 2180 added.append(f)
2177 2181 elif self[f]:
2178 2182 modified.append(f)
2179 2183 else:
2180 2184 removed.append(f)
2181 2185
2182 2186 return scmutil.status(modified, added, removed, [], [], [], [])
2183 2187
2184 2188 class memfilectx(committablefilectx):
2185 2189 """memfilectx represents an in-memory file to commit.
2186 2190
2187 2191 See memctx and committablefilectx for more details.
2188 2192 """
2189 2193 def __init__(self, repo, path, data, islink=False,
2190 2194 isexec=False, copied=None, memctx=None):
2191 2195 """
2192 2196 path is the normalized file path relative to repository root.
2193 2197 data is the file content as a string.
2194 2198 islink is True if the file is a symbolic link.
2195 2199 isexec is True if the file is executable.
2196 2200 copied is the source file path if current file was copied in the
2197 2201 revision being committed, or None."""
2198 2202 super(memfilectx, self).__init__(repo, path, None, memctx)
2199 2203 self._data = data
2200 2204 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
2201 2205 self._copied = None
2202 2206 if copied:
2203 2207 self._copied = (copied, nullid)
2204 2208
2205 2209 def data(self):
2206 2210 return self._data
2207 2211
2208 2212 def remove(self, ignoremissing=False):
2209 2213 """wraps unlink for a repo's working directory"""
2210 2214 # need to figure out what to do here
2211 2215 del self._changectx[self._path]
2212 2216
2213 2217 def write(self, data, flags):
2214 2218 """wraps repo.wwrite"""
2215 2219 self._data = data
2216 2220
2217 2221 class overlayfilectx(committablefilectx):
2218 2222 """Like memfilectx but take an original filectx and optional parameters to
2219 2223 override parts of it. This is useful when fctx.data() is expensive (i.e.
2220 2224 flag processor is expensive) and raw data, flags, and filenode could be
2221 2225 reused (ex. rebase or mode-only amend a REVIDX_EXTSTORED file).
2222 2226 """
2223 2227
2224 2228 def __init__(self, originalfctx, datafunc=None, path=None, flags=None,
2225 2229 copied=None, ctx=None):
2226 2230 """originalfctx: filecontext to duplicate
2227 2231
2228 2232 datafunc: None or a function to override data (file content). It is a
2229 2233 function to be lazy. path, flags, copied, ctx: None or overridden value
2230 2234
2231 2235 copied could be (path, rev), or False. copied could also be just path,
2232 2236 and will be converted to (path, nullid). This simplifies some callers.
2233 2237 """
2234 2238
2235 2239 if path is None:
2236 2240 path = originalfctx.path()
2237 2241 if ctx is None:
2238 2242 ctx = originalfctx.changectx()
2239 2243 ctxmatch = lambda: True
2240 2244 else:
2241 2245 ctxmatch = lambda: ctx == originalfctx.changectx()
2242 2246
2243 2247 repo = originalfctx.repo()
2244 2248 flog = originalfctx.filelog()
2245 2249 super(overlayfilectx, self).__init__(repo, path, flog, ctx)
2246 2250
2247 2251 if copied is None:
2248 2252 copied = originalfctx.renamed()
2249 2253 copiedmatch = lambda: True
2250 2254 else:
2251 2255 if copied and not isinstance(copied, tuple):
2252 2256 # repo._filecommit will recalculate copyrev so nullid is okay
2253 2257 copied = (copied, nullid)
2254 2258 copiedmatch = lambda: copied == originalfctx.renamed()
2255 2259
2256 2260 # When data, copied (could affect data), ctx (could affect filelog
2257 2261 # parents) are not overridden, rawdata, rawflags, and filenode may be
2258 2262 # reused (repo._filecommit should double check filelog parents).
2259 2263 #
2260 2264 # path, flags are not hashed in filelog (but in manifestlog) so they do
2261 2265 # not affect reusable here.
2262 2266 #
2263 2267 # If ctx or copied is overridden to a same value with originalfctx,
2264 2268 # still consider it's reusable. originalfctx.renamed() may be a bit
2265 2269 # expensive so it's not called unless necessary. Assuming datafunc is
2266 2270 # always expensive, do not call it for this "reusable" test.
2267 2271 reusable = datafunc is None and ctxmatch() and copiedmatch()
2268 2272
2269 2273 if datafunc is None:
2270 2274 datafunc = originalfctx.data
2271 2275 if flags is None:
2272 2276 flags = originalfctx.flags()
2273 2277
2274 2278 self._datafunc = datafunc
2275 2279 self._flags = flags
2276 2280 self._copied = copied
2277 2281
2278 2282 if reusable:
2279 2283 # copy extra fields from originalfctx
2280 2284 attrs = ['rawdata', 'rawflags', '_filenode', '_filerev']
2281 2285 for attr in attrs:
2282 2286 if util.safehasattr(originalfctx, attr):
2283 2287 setattr(self, attr, getattr(originalfctx, attr))
2284 2288
2285 2289 def data(self):
2286 2290 return self._datafunc()
2287 2291
2288 2292 class metadataonlyctx(committablectx):
2289 2293 """Like memctx but it's reusing the manifest of different commit.
2290 2294 Intended to be used by lightweight operations that are creating
2291 2295 metadata-only changes.
2292 2296
2293 2297 Revision information is supplied at initialization time. 'repo' is the
2294 2298 current localrepo, 'ctx' is original revision which manifest we're reuisng
2295 2299 'parents' is a sequence of two parent revisions identifiers (pass None for
2296 2300 every missing parent), 'text' is the commit.
2297 2301
2298 2302 user receives the committer name and defaults to current repository
2299 2303 username, date is the commit date in any format supported by
2300 2304 util.parsedate() and defaults to current date, extra is a dictionary of
2301 2305 metadata or is left empty.
2302 2306 """
2303 2307 def __new__(cls, repo, originalctx, *args, **kwargs):
2304 2308 return super(metadataonlyctx, cls).__new__(cls, repo)
2305 2309
2306 2310 def __init__(self, repo, originalctx, parents, text, user=None, date=None,
2307 2311 extra=None, editor=False):
2308 2312 super(metadataonlyctx, self).__init__(repo, text, user, date, extra)
2309 2313 self._rev = None
2310 2314 self._node = None
2311 2315 self._originalctx = originalctx
2312 2316 self._manifestnode = originalctx.manifestnode()
2313 2317 parents = [(p or nullid) for p in parents]
2314 2318 p1, p2 = self._parents = [changectx(self._repo, p) for p in parents]
2315 2319
2316 2320 # sanity check to ensure that the reused manifest parents are
2317 2321 # manifests of our commit parents
2318 2322 mp1, mp2 = self.manifestctx().parents
2319 2323 if p1 != nullid and p1.manifestnode() != mp1:
2320 2324 raise RuntimeError('can\'t reuse the manifest: '
2321 2325 'its p1 doesn\'t match the new ctx p1')
2322 2326 if p2 != nullid and p2.manifestnode() != mp2:
2323 2327 raise RuntimeError('can\'t reuse the manifest: '
2324 2328 'its p2 doesn\'t match the new ctx p2')
2325 2329
2326 2330 self._files = originalctx.files()
2327 2331 self.substate = {}
2328 2332
2329 2333 if editor:
2330 2334 self._text = editor(self._repo, self, [])
2331 2335 self._repo.savecommitmessage(self._text)
2332 2336
2333 2337 def manifestnode(self):
2334 2338 return self._manifestnode
2335 2339
2336 2340 @property
2337 2341 def _manifestctx(self):
2338 2342 return self._repo.manifestlog[self._manifestnode]
2339 2343
2340 2344 def filectx(self, path, filelog=None):
2341 2345 return self._originalctx.filectx(path, filelog=filelog)
2342 2346
2343 2347 def commit(self):
2344 2348 """commit context to the repo"""
2345 2349 return self._repo.commitctx(self)
2346 2350
2347 2351 @property
2348 2352 def _manifest(self):
2349 2353 return self._originalctx.manifest()
2350 2354
2351 2355 @propertycache
2352 2356 def _status(self):
2353 2357 """Calculate exact status from ``files`` specified in the ``origctx``
2354 2358 and parents manifests.
2355 2359 """
2356 2360 man1 = self.p1().manifest()
2357 2361 p2 = self._parents[1]
2358 2362 # "1 < len(self._parents)" can't be used for checking
2359 2363 # existence of the 2nd parent, because "metadataonlyctx._parents" is
2360 2364 # explicitly initialized by the list, of which length is 2.
2361 2365 if p2.node() != nullid:
2362 2366 man2 = p2.manifest()
2363 2367 managing = lambda f: f in man1 or f in man2
2364 2368 else:
2365 2369 managing = lambda f: f in man1
2366 2370
2367 2371 modified, added, removed = [], [], []
2368 2372 for f in self._files:
2369 2373 if not managing(f):
2370 2374 added.append(f)
2371 2375 elif self[f]:
2372 2376 modified.append(f)
2373 2377 else:
2374 2378 removed.append(f)
2375 2379
2376 2380 return scmutil.status(modified, added, removed, [], [], [], [])
@@ -1,70 +1,70 b''
1 1 # extension to emulate invoking 'dirstate.write()' at the time
2 2 # specified by '[fakedirstatewritetime] fakenow', only when
3 3 # 'dirstate.write()' is invoked via functions below:
4 4 #
5 # - 'workingctx._checklookup()' (= 'repo.status()')
5 # - 'workingctx._poststatusfixup()' (= 'repo.status()')
6 6 # - 'committablectx.markcommitted()'
7 7
8 8 from __future__ import absolute_import
9 9
10 10 from mercurial import (
11 11 context,
12 12 dirstate,
13 13 extensions,
14 14 policy,
15 15 util,
16 16 )
17 17
18 18 parsers = policy.importmod(r'parsers')
19 19
20 20 def pack_dirstate(fakenow, orig, dmap, copymap, pl, now):
21 21 # execute what original parsers.pack_dirstate should do actually
22 22 # for consistency
23 23 actualnow = int(now)
24 24 for f, e in dmap.iteritems():
25 25 if e[0] == 'n' and e[3] == actualnow:
26 26 e = parsers.dirstatetuple(e[0], e[1], e[2], -1)
27 27 dmap[f] = e
28 28
29 29 return orig(dmap, copymap, pl, fakenow)
30 30
31 31 def fakewrite(ui, func):
32 32 # fake "now" of 'pack_dirstate' only if it is invoked while 'func'
33 33
34 34 fakenow = ui.config('fakedirstatewritetime', 'fakenow')
35 35 if not fakenow:
36 36 # Execute original one, if fakenow isn't configured. This is
37 37 # useful to prevent subrepos from executing replaced one,
38 38 # because replacing 'parsers.pack_dirstate' is also effective
39 39 # in subrepos.
40 40 return func()
41 41
42 42 # parsing 'fakenow' in YYYYmmddHHMM format makes comparison between
43 43 # 'fakenow' value and 'touch -t YYYYmmddHHMM' argument easy
44 44 fakenow = util.parsedate(fakenow, ['%Y%m%d%H%M'])[0]
45 45
46 46 orig_pack_dirstate = parsers.pack_dirstate
47 47 orig_dirstate_getfsnow = dirstate._getfsnow
48 48 wrapper = lambda *args: pack_dirstate(fakenow, orig_pack_dirstate, *args)
49 49
50 50 parsers.pack_dirstate = wrapper
51 51 dirstate._getfsnow = lambda *args: fakenow
52 52 try:
53 53 return func()
54 54 finally:
55 55 parsers.pack_dirstate = orig_pack_dirstate
56 56 dirstate._getfsnow = orig_dirstate_getfsnow
57 57
58 def _checklookup(orig, workingctx, files):
58 def _poststatusfixup(orig, workingctx, fixup):
59 59 ui = workingctx.repo().ui
60 return fakewrite(ui, lambda : orig(workingctx, files))
60 return fakewrite(ui, lambda : orig(workingctx, fixup))
61 61
62 62 def markcommitted(orig, committablectx, node):
63 63 ui = committablectx.repo().ui
64 64 return fakewrite(ui, lambda : orig(committablectx, node))
65 65
66 66 def extsetup(ui):
67 extensions.wrapfunction(context.workingctx, '_checklookup',
68 _checklookup)
67 extensions.wrapfunction(context.workingctx, '_poststatusfixup',
68 _poststatusfixup)
69 69 extensions.wrapfunction(context.committablectx, 'markcommitted',
70 70 markcommitted)
@@ -1,159 +1,159 b''
1 1 $ hg init repo
2 2 $ cd repo
3 3 $ echo a > a
4 4 $ hg add a
5 5 $ hg commit -m test
6 6
7 7 Do we ever miss a sub-second change?:
8 8
9 9 $ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
10 10 > hg co -qC 0
11 11 > echo b > a
12 12 > hg st
13 13 > done
14 14 M a
15 15 M a
16 16 M a
17 17 M a
18 18 M a
19 19 M a
20 20 M a
21 21 M a
22 22 M a
23 23 M a
24 24 M a
25 25 M a
26 26 M a
27 27 M a
28 28 M a
29 29 M a
30 30 M a
31 31 M a
32 32 M a
33 33 M a
34 34
35 35 $ echo test > b
36 36 $ mkdir dir1
37 37 $ echo test > dir1/c
38 38 $ echo test > d
39 39
40 40 $ echo test > e
41 41 #if execbit
42 42 A directory will typically have the execute bit -- make sure it doesn't get
43 43 confused with a file with the exec bit set
44 44 $ chmod +x e
45 45 #endif
46 46
47 47 $ hg add b dir1 d e
48 48 adding dir1/c (glob)
49 49 $ hg commit -m test2
50 50
51 51 $ cat >> $TESTTMP/dirstaterace.py << EOF
52 52 > from mercurial import (
53 53 > context,
54 54 > extensions,
55 55 > )
56 56 > def extsetup():
57 57 > extensions.wrapfunction(context.workingctx, '_checklookup', overridechecklookup)
58 58 > def overridechecklookup(orig, self, files):
59 59 > # make an update that changes the dirstate from underneath
60 60 > self._repo.ui.system(r"sh '$TESTTMP/dirstaterace.sh'",
61 61 > cwd=self._repo.root)
62 62 > return orig(self, files)
63 63 > EOF
64 64
65 65 $ hg debugrebuilddirstate
66 66 $ hg debugdirstate
67 67 n 0 -1 unset a
68 68 n 0 -1 unset b
69 69 n 0 -1 unset d
70 70 n 0 -1 unset dir1/c
71 71 n 0 -1 unset e
72 72
73 73 XXX Note that this returns M for files that got replaced by directories. This is
74 74 definitely a bug, but the fix for that is hard and the next status run is fine
75 75 anyway.
76 76
77 77 $ cat > $TESTTMP/dirstaterace.sh <<EOF
78 78 > rm b && rm -r dir1 && rm d && mkdir d && rm e && mkdir e
79 79 > EOF
80 80
81 81 $ hg status --config extensions.dirstaterace=$TESTTMP/dirstaterace.py
82 82 M d
83 83 M e
84 84 ! b
85 85 ! dir1/c
86 86 $ hg debugdirstate
87 87 n 644 2 * a (glob)
88 88 n 0 -1 unset b
89 89 n 0 -1 unset d
90 90 n 0 -1 unset dir1/c
91 91 n 0 -1 unset e
92 92
93 93 $ hg status
94 94 ! b
95 95 ! d
96 96 ! dir1/c
97 97 ! e
98 98
99 99 $ rmdir d e
100 100 $ hg update -C -q .
101 101
102 102 Test that dirstate changes aren't written out at the end of "hg
103 103 status", if .hg/dirstate is already changed simultaneously before
104 acquisition of wlock in workingctx._checklookup().
104 acquisition of wlock in workingctx._poststatusfixup().
105 105
106 106 This avoidance is important to keep consistency of dirstate in race
107 107 condition (see issue5584 for detail).
108 108
109 109 $ hg parents -q
110 110 1:* (glob)
111 111
112 112 $ hg debugrebuilddirstate
113 113 $ hg debugdirstate
114 114 n 0 -1 unset a
115 115 n 0 -1 unset b
116 116 n 0 -1 unset d
117 117 n 0 -1 unset dir1/c
118 118 n 0 -1 unset e
119 119
120 120 $ cat > $TESTTMP/dirstaterace.sh <<EOF
121 121 > # This script assumes timetable of typical issue5584 case below:
122 122 > #
123 123 > # 1. "hg status" loads .hg/dirstate
124 124 > # 2. "hg status" confirms clean-ness of FILE
125 125 > # 3. "hg update -C 0" updates the working directory simultaneously
126 126 > # (FILE is removed, and FILE is dropped from .hg/dirstate)
127 127 > # 4. "hg status" acquires wlock
128 128 > # (.hg/dirstate is re-loaded = no FILE entry in dirstate)
129 129 > # 5. "hg status" marks FILE in dirstate as clean
130 130 > # (FILE entry is added to in-memory dirstate)
131 131 > # 6. "hg status" writes dirstate changes into .hg/dirstate
132 132 > # (FILE entry is written into .hg/dirstate)
133 133 > #
134 134 > # To reproduce similar situation easily and certainly, #2 and #3
135 135 > # are swapped. "hg cat" below ensures #2 on "hg status" side.
136 136 >
137 137 > hg update -q -C 0
138 138 > hg cat -r 1 b > b
139 139 > EOF
140 140
141 141 "hg status" below should excludes "e", of which exec flag is set, for
142 142 portability of test scenario, because unsure but missing "e" is
143 143 treated differently in _checklookup() according to runtime platform.
144 144
145 145 - "missing(!)" on POSIX, "pctx[f].cmp(self[f])" raises ENOENT
146 146 - "modified(M)" on Windows, "self.flags(f) != pctx.flags(f)" is True
147 147
148 148 $ hg status --config extensions.dirstaterace=$TESTTMP/dirstaterace.py --debug -X path:e
149 149 skip updating dirstate: identity mismatch
150 150 M a
151 151 ! d
152 152 ! dir1/c
153 153
154 154 $ hg parents -q
155 155 0:* (glob)
156 156 $ hg files
157 157 a
158 158 $ hg debugdirstate
159 159 n * * * a (glob)
General Comments 0
You need to be logged in to leave comments. Login now