##// END OF EJS Templates
context.status: make _dirstatestatus() return an status tuple...
Martin von Zweigbergk -
r23302:24f67ad4 default
parent child Browse files
Show More
@@ -1,1690 +1,1691
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 node import nullid, nullrev, short, hex, bin
9 9 from i18n import _
10 10 import mdiff, error, util, scmutil, subrepo, patch, encoding, phases
11 11 import match as matchmod
12 12 import os, errno, stat
13 13 import obsolete as obsmod
14 14 import repoview
15 15 import fileset
16 16 import revlog
17 17
18 18 propertycache = util.propertycache
19 19
20 20 class basectx(object):
21 21 """A basectx object represents the common logic for its children:
22 22 changectx: read-only context that is already present in the repo,
23 23 workingctx: a context that represents the working directory and can
24 24 be committed,
25 25 memctx: a context that represents changes in-memory and can also
26 26 be committed."""
27 27 def __new__(cls, repo, changeid='', *args, **kwargs):
28 28 if isinstance(changeid, basectx):
29 29 return changeid
30 30
31 31 o = super(basectx, cls).__new__(cls)
32 32
33 33 o._repo = repo
34 34 o._rev = nullrev
35 35 o._node = nullid
36 36
37 37 return o
38 38
39 39 def __str__(self):
40 40 return short(self.node())
41 41
42 42 def __int__(self):
43 43 return self.rev()
44 44
45 45 def __repr__(self):
46 46 return "<%s %s>" % (type(self).__name__, str(self))
47 47
48 48 def __eq__(self, other):
49 49 try:
50 50 return type(self) == type(other) and self._rev == other._rev
51 51 except AttributeError:
52 52 return False
53 53
54 54 def __ne__(self, other):
55 55 return not (self == other)
56 56
57 57 def __contains__(self, key):
58 58 return key in self._manifest
59 59
60 60 def __getitem__(self, key):
61 61 return self.filectx(key)
62 62
63 63 def __iter__(self):
64 64 for f in sorted(self._manifest):
65 65 yield f
66 66
67 67 def _manifestmatches(self, match, s):
68 68 """generate a new manifest filtered by the match argument
69 69
70 70 This method is for internal use only and mainly exists to provide an
71 71 object oriented way for other contexts to customize the manifest
72 72 generation.
73 73 """
74 74 if match.always():
75 75 return self.manifest().copy()
76 76
77 77 files = match.files()
78 78 if (match.matchfn == match.exact or
79 79 (not match.anypats() and util.all(fn in self for fn in files))):
80 80 return self.manifest().intersectfiles(files)
81 81
82 82 mf = self.manifest().copy()
83 83 for fn in mf.keys():
84 84 if not match(fn):
85 85 del mf[fn]
86 86 return mf
87 87
88 88 def _matchstatus(self, other, match):
89 89 """return match.always if match is none
90 90
91 91 This internal method provides a way for child objects to override the
92 92 match operator.
93 93 """
94 94 return match or matchmod.always(self._repo.root, self._repo.getcwd())
95 95
96 96 def _buildstatus(self, other, s, match, listignored, listclean,
97 97 listunknown):
98 98 """build a status with respect to another context"""
99 99 # Load earliest manifest first for caching reasons. More specifically,
100 100 # if you have revisions 1000 and 1001, 1001 is probably stored as a
101 101 # delta against 1000. Thus, if you read 1000 first, we'll reconstruct
102 102 # 1000 and cache it so that when you read 1001, we just need to apply a
103 103 # delta to what's in the cache. So that's one full reconstruction + one
104 104 # delta application.
105 105 if self.rev() is not None and self.rev() < other.rev():
106 106 self.manifest()
107 107 mf1 = other._manifestmatches(match, s)
108 108 mf2 = self._manifestmatches(match, s)
109 109
110 110 modified, added, clean = [], [], []
111 111 deleted, unknown, ignored = s[3], s[4], s[5]
112 112 deletedset = set(deleted)
113 113 withflags = mf1.withflags() | mf2.withflags()
114 114 for fn, mf2node in mf2.iteritems():
115 115 if fn in mf1:
116 116 if (fn not in deletedset and
117 117 ((fn in withflags and mf1.flags(fn) != mf2.flags(fn)) or
118 118 (mf1[fn] != mf2node and
119 119 (mf2node or self[fn].cmp(other[fn]))))):
120 120 modified.append(fn)
121 121 elif listclean:
122 122 clean.append(fn)
123 123 del mf1[fn]
124 124 elif fn not in deletedset:
125 125 added.append(fn)
126 126 removed = mf1.keys()
127 127 if removed:
128 128 # need to filter files if they are already reported as removed
129 129 unknown = [fn for fn in unknown if fn not in mf1]
130 130 ignored = [fn for fn in ignored if fn not in mf1]
131 131
132 return [modified, added, removed, deleted, unknown, ignored, clean]
132 return scmutil.status(modified, added, removed, deleted, unknown,
133 ignored, clean)
133 134
134 135 @propertycache
135 136 def substate(self):
136 137 return subrepo.state(self, self._repo.ui)
137 138
138 139 def subrev(self, subpath):
139 140 return self.substate[subpath][1]
140 141
141 142 def rev(self):
142 143 return self._rev
143 144 def node(self):
144 145 return self._node
145 146 def hex(self):
146 147 return hex(self.node())
147 148 def manifest(self):
148 149 return self._manifest
149 150 def phasestr(self):
150 151 return phases.phasenames[self.phase()]
151 152 def mutable(self):
152 153 return self.phase() > phases.public
153 154
154 155 def getfileset(self, expr):
155 156 return fileset.getfileset(self, expr)
156 157
157 158 def obsolete(self):
158 159 """True if the changeset is obsolete"""
159 160 return self.rev() in obsmod.getrevs(self._repo, 'obsolete')
160 161
161 162 def extinct(self):
162 163 """True if the changeset is extinct"""
163 164 return self.rev() in obsmod.getrevs(self._repo, 'extinct')
164 165
165 166 def unstable(self):
166 167 """True if the changeset is not obsolete but it's ancestor are"""
167 168 return self.rev() in obsmod.getrevs(self._repo, 'unstable')
168 169
169 170 def bumped(self):
170 171 """True if the changeset try to be a successor of a public changeset
171 172
172 173 Only non-public and non-obsolete changesets may be bumped.
173 174 """
174 175 return self.rev() in obsmod.getrevs(self._repo, 'bumped')
175 176
176 177 def divergent(self):
177 178 """Is a successors of a changeset with multiple possible successors set
178 179
179 180 Only non-public and non-obsolete changesets may be divergent.
180 181 """
181 182 return self.rev() in obsmod.getrevs(self._repo, 'divergent')
182 183
183 184 def troubled(self):
184 185 """True if the changeset is either unstable, bumped or divergent"""
185 186 return self.unstable() or self.bumped() or self.divergent()
186 187
187 188 def troubles(self):
188 189 """return the list of troubles affecting this changesets.
189 190
190 191 Troubles are returned as strings. possible values are:
191 192 - unstable,
192 193 - bumped,
193 194 - divergent.
194 195 """
195 196 troubles = []
196 197 if self.unstable():
197 198 troubles.append('unstable')
198 199 if self.bumped():
199 200 troubles.append('bumped')
200 201 if self.divergent():
201 202 troubles.append('divergent')
202 203 return troubles
203 204
204 205 def parents(self):
205 206 """return contexts for each parent changeset"""
206 207 return self._parents
207 208
208 209 def p1(self):
209 210 return self._parents[0]
210 211
211 212 def p2(self):
212 213 if len(self._parents) == 2:
213 214 return self._parents[1]
214 215 return changectx(self._repo, -1)
215 216
216 217 def _fileinfo(self, path):
217 218 if '_manifest' in self.__dict__:
218 219 try:
219 220 return self._manifest[path], self._manifest.flags(path)
220 221 except KeyError:
221 222 raise error.ManifestLookupError(self._node, path,
222 223 _('not found in manifest'))
223 224 if '_manifestdelta' in self.__dict__ or path in self.files():
224 225 if path in self._manifestdelta:
225 226 return (self._manifestdelta[path],
226 227 self._manifestdelta.flags(path))
227 228 node, flag = self._repo.manifest.find(self._changeset[0], path)
228 229 if not node:
229 230 raise error.ManifestLookupError(self._node, path,
230 231 _('not found in manifest'))
231 232
232 233 return node, flag
233 234
234 235 def filenode(self, path):
235 236 return self._fileinfo(path)[0]
236 237
237 238 def flags(self, path):
238 239 try:
239 240 return self._fileinfo(path)[1]
240 241 except error.LookupError:
241 242 return ''
242 243
243 244 def sub(self, path):
244 245 return subrepo.subrepo(self, path)
245 246
246 247 def match(self, pats=[], include=None, exclude=None, default='glob'):
247 248 r = self._repo
248 249 return matchmod.match(r.root, r.getcwd(), pats,
249 250 include, exclude, default,
250 251 auditor=r.auditor, ctx=self)
251 252
252 253 def diff(self, ctx2=None, match=None, **opts):
253 254 """Returns a diff generator for the given contexts and matcher"""
254 255 if ctx2 is None:
255 256 ctx2 = self.p1()
256 257 if ctx2 is not None:
257 258 ctx2 = self._repo[ctx2]
258 259 diffopts = patch.diffopts(self._repo.ui, opts)
259 260 return patch.diff(self._repo, ctx2, self, match=match, opts=diffopts)
260 261
261 262 @propertycache
262 263 def _dirs(self):
263 264 return scmutil.dirs(self._manifest)
264 265
265 266 def dirs(self):
266 267 return self._dirs
267 268
268 269 def dirty(self, missing=False, merge=True, branch=True):
269 270 return False
270 271
271 272 def status(self, other=None, match=None, listignored=False,
272 273 listclean=False, listunknown=False, listsubrepos=False):
273 274 """return status of files between two nodes or node and working
274 275 directory.
275 276
276 277 If other is None, compare this node with working directory.
277 278
278 279 returns (modified, added, removed, deleted, unknown, ignored, clean)
279 280 """
280 281
281 282 ctx1 = self
282 283 ctx2 = self._repo[other]
283 284
284 285 # This next code block is, admittedly, fragile logic that tests for
285 286 # reversing the contexts and wouldn't need to exist if it weren't for
286 287 # the fast (and common) code path of comparing the working directory
287 288 # with its first parent.
288 289 #
289 290 # What we're aiming for here is the ability to call:
290 291 #
291 292 # workingctx.status(parentctx)
292 293 #
293 294 # If we always built the manifest for each context and compared those,
294 295 # then we'd be done. But the special case of the above call means we
295 296 # just copy the manifest of the parent.
296 297 reversed = False
297 298 if (not isinstance(ctx1, changectx)
298 299 and isinstance(ctx2, changectx)):
299 300 reversed = True
300 301 ctx1, ctx2 = ctx2, ctx1
301 302
302 303 match = ctx2._matchstatus(ctx1, match)
303 304 r = [[], [], [], [], [], [], []]
304 305 r = ctx2._buildstatus(ctx1, r, match, listignored, listclean,
305 306 listunknown)
306 307
307 r = scmutil.status(*r)
308 308 if reversed:
309 309 # Reverse added and removed. Clear deleted, unknown and ignored as
310 310 # these make no sense to reverse.
311 311 r = scmutil.status(r.modified, r.removed, r.added, [], [], [],
312 312 r.clean)
313 313
314 314 if listsubrepos:
315 315 for subpath, sub in scmutil.itersubrepos(ctx1, ctx2):
316 316 rev2 = ctx2.subrev(subpath)
317 317 try:
318 318 submatch = matchmod.narrowmatcher(subpath, match)
319 319 s = sub.status(rev2, match=submatch, ignored=listignored,
320 320 clean=listclean, unknown=listunknown,
321 321 listsubrepos=True)
322 322 for rfiles, sfiles in zip(r, s):
323 323 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
324 324 except error.LookupError:
325 325 self._repo.ui.status(_("skipping missing "
326 326 "subrepository: %s\n") % subpath)
327 327
328 328 for l in r:
329 329 l.sort()
330 330
331 331 return r
332 332
333 333
334 334 def makememctx(repo, parents, text, user, date, branch, files, store,
335 335 editor=None):
336 336 def getfilectx(repo, memctx, path):
337 337 data, mode, copied = store.getfile(path)
338 338 if data is None:
339 339 return None
340 340 islink, isexec = mode
341 341 return memfilectx(repo, path, data, islink=islink, isexec=isexec,
342 342 copied=copied, memctx=memctx)
343 343 extra = {}
344 344 if branch:
345 345 extra['branch'] = encoding.fromlocal(branch)
346 346 ctx = memctx(repo, parents, text, files, getfilectx, user,
347 347 date, extra, editor)
348 348 return ctx
349 349
350 350 class changectx(basectx):
351 351 """A changecontext object makes access to data related to a particular
352 352 changeset convenient. It represents a read-only context already present in
353 353 the repo."""
354 354 def __init__(self, repo, changeid=''):
355 355 """changeid is a revision number, node, or tag"""
356 356
357 357 # since basectx.__new__ already took care of copying the object, we
358 358 # don't need to do anything in __init__, so we just exit here
359 359 if isinstance(changeid, basectx):
360 360 return
361 361
362 362 if changeid == '':
363 363 changeid = '.'
364 364 self._repo = repo
365 365
366 366 try:
367 367 if isinstance(changeid, int):
368 368 self._node = repo.changelog.node(changeid)
369 369 self._rev = changeid
370 370 return
371 371 if isinstance(changeid, long):
372 372 changeid = str(changeid)
373 373 if changeid == '.':
374 374 self._node = repo.dirstate.p1()
375 375 self._rev = repo.changelog.rev(self._node)
376 376 return
377 377 if changeid == 'null':
378 378 self._node = nullid
379 379 self._rev = nullrev
380 380 return
381 381 if changeid == 'tip':
382 382 self._node = repo.changelog.tip()
383 383 self._rev = repo.changelog.rev(self._node)
384 384 return
385 385 if len(changeid) == 20:
386 386 try:
387 387 self._node = changeid
388 388 self._rev = repo.changelog.rev(changeid)
389 389 return
390 390 except error.FilteredRepoLookupError:
391 391 raise
392 392 except LookupError:
393 393 pass
394 394
395 395 try:
396 396 r = int(changeid)
397 397 if str(r) != changeid:
398 398 raise ValueError
399 399 l = len(repo.changelog)
400 400 if r < 0:
401 401 r += l
402 402 if r < 0 or r >= l:
403 403 raise ValueError
404 404 self._rev = r
405 405 self._node = repo.changelog.node(r)
406 406 return
407 407 except error.FilteredIndexError:
408 408 raise
409 409 except (ValueError, OverflowError, IndexError):
410 410 pass
411 411
412 412 if len(changeid) == 40:
413 413 try:
414 414 self._node = bin(changeid)
415 415 self._rev = repo.changelog.rev(self._node)
416 416 return
417 417 except error.FilteredLookupError:
418 418 raise
419 419 except (TypeError, LookupError):
420 420 pass
421 421
422 422 if changeid in repo._bookmarks:
423 423 self._node = repo._bookmarks[changeid]
424 424 self._rev = repo.changelog.rev(self._node)
425 425 return
426 426 if changeid in repo._tagscache.tags:
427 427 self._node = repo._tagscache.tags[changeid]
428 428 self._rev = repo.changelog.rev(self._node)
429 429 return
430 430 try:
431 431 self._node = repo.branchtip(changeid)
432 432 self._rev = repo.changelog.rev(self._node)
433 433 return
434 434 except error.FilteredRepoLookupError:
435 435 raise
436 436 except error.RepoLookupError:
437 437 pass
438 438
439 439 self._node = repo.unfiltered().changelog._partialmatch(changeid)
440 440 if self._node is not None:
441 441 self._rev = repo.changelog.rev(self._node)
442 442 return
443 443
444 444 # lookup failed
445 445 # check if it might have come from damaged dirstate
446 446 #
447 447 # XXX we could avoid the unfiltered if we had a recognizable
448 448 # exception for filtered changeset access
449 449 if changeid in repo.unfiltered().dirstate.parents():
450 450 msg = _("working directory has unknown parent '%s'!")
451 451 raise error.Abort(msg % short(changeid))
452 452 try:
453 453 if len(changeid) == 20:
454 454 changeid = hex(changeid)
455 455 except TypeError:
456 456 pass
457 457 except (error.FilteredIndexError, error.FilteredLookupError,
458 458 error.FilteredRepoLookupError):
459 459 if repo.filtername == 'visible':
460 460 msg = _("hidden revision '%s'") % changeid
461 461 hint = _('use --hidden to access hidden revisions')
462 462 raise error.FilteredRepoLookupError(msg, hint=hint)
463 463 msg = _("filtered revision '%s' (not in '%s' subset)")
464 464 msg %= (changeid, repo.filtername)
465 465 raise error.FilteredRepoLookupError(msg)
466 466 except IndexError:
467 467 pass
468 468 raise error.RepoLookupError(
469 469 _("unknown revision '%s'") % changeid)
470 470
471 471 def __hash__(self):
472 472 try:
473 473 return hash(self._rev)
474 474 except AttributeError:
475 475 return id(self)
476 476
477 477 def __nonzero__(self):
478 478 return self._rev != nullrev
479 479
480 480 @propertycache
481 481 def _changeset(self):
482 482 return self._repo.changelog.read(self.rev())
483 483
484 484 @propertycache
485 485 def _manifest(self):
486 486 return self._repo.manifest.read(self._changeset[0])
487 487
488 488 @propertycache
489 489 def _manifestdelta(self):
490 490 return self._repo.manifest.readdelta(self._changeset[0])
491 491
492 492 @propertycache
493 493 def _parents(self):
494 494 p = self._repo.changelog.parentrevs(self._rev)
495 495 if p[1] == nullrev:
496 496 p = p[:-1]
497 497 return [changectx(self._repo, x) for x in p]
498 498
499 499 def changeset(self):
500 500 return self._changeset
501 501 def manifestnode(self):
502 502 return self._changeset[0]
503 503
504 504 def user(self):
505 505 return self._changeset[1]
506 506 def date(self):
507 507 return self._changeset[2]
508 508 def files(self):
509 509 return self._changeset[3]
510 510 def description(self):
511 511 return self._changeset[4]
512 512 def branch(self):
513 513 return encoding.tolocal(self._changeset[5].get("branch"))
514 514 def closesbranch(self):
515 515 return 'close' in self._changeset[5]
516 516 def extra(self):
517 517 return self._changeset[5]
518 518 def tags(self):
519 519 return self._repo.nodetags(self._node)
520 520 def bookmarks(self):
521 521 return self._repo.nodebookmarks(self._node)
522 522 def phase(self):
523 523 return self._repo._phasecache.phase(self._repo, self._rev)
524 524 def hidden(self):
525 525 return self._rev in repoview.filterrevs(self._repo, 'visible')
526 526
527 527 def children(self):
528 528 """return contexts for each child changeset"""
529 529 c = self._repo.changelog.children(self._node)
530 530 return [changectx(self._repo, x) for x in c]
531 531
532 532 def ancestors(self):
533 533 for a in self._repo.changelog.ancestors([self._rev]):
534 534 yield changectx(self._repo, a)
535 535
536 536 def descendants(self):
537 537 for d in self._repo.changelog.descendants([self._rev]):
538 538 yield changectx(self._repo, d)
539 539
540 540 def filectx(self, path, fileid=None, filelog=None):
541 541 """get a file context from this changeset"""
542 542 if fileid is None:
543 543 fileid = self.filenode(path)
544 544 return filectx(self._repo, path, fileid=fileid,
545 545 changectx=self, filelog=filelog)
546 546
547 547 def ancestor(self, c2, warn=False):
548 548 """return the "best" ancestor context of self and c2
549 549
550 550 If there are multiple candidates, it will show a message and check
551 551 merge.preferancestor configuration before falling back to the
552 552 revlog ancestor."""
553 553 # deal with workingctxs
554 554 n2 = c2._node
555 555 if n2 is None:
556 556 n2 = c2._parents[0]._node
557 557 cahs = self._repo.changelog.commonancestorsheads(self._node, n2)
558 558 if not cahs:
559 559 anc = nullid
560 560 elif len(cahs) == 1:
561 561 anc = cahs[0]
562 562 else:
563 563 for r in self._repo.ui.configlist('merge', 'preferancestor'):
564 564 try:
565 565 ctx = changectx(self._repo, r)
566 566 except error.RepoLookupError:
567 567 continue
568 568 anc = ctx.node()
569 569 if anc in cahs:
570 570 break
571 571 else:
572 572 anc = self._repo.changelog.ancestor(self._node, n2)
573 573 if warn:
574 574 self._repo.ui.status(
575 575 (_("note: using %s as ancestor of %s and %s\n") %
576 576 (short(anc), short(self._node), short(n2))) +
577 577 ''.join(_(" alternatively, use --config "
578 578 "merge.preferancestor=%s\n") %
579 579 short(n) for n in sorted(cahs) if n != anc))
580 580 return changectx(self._repo, anc)
581 581
582 582 def descendant(self, other):
583 583 """True if other is descendant of this changeset"""
584 584 return self._repo.changelog.descendant(self._rev, other._rev)
585 585
586 586 def walk(self, match):
587 587 fset = set(match.files())
588 588 # for dirstate.walk, files=['.'] means "walk the whole tree".
589 589 # follow that here, too
590 590 fset.discard('.')
591 591
592 592 # avoid the entire walk if we're only looking for specific files
593 593 if fset and not match.anypats():
594 594 if util.all([fn in self for fn in fset]):
595 595 for fn in sorted(fset):
596 596 if match(fn):
597 597 yield fn
598 598 raise StopIteration
599 599
600 600 for fn in self:
601 601 if fn in fset:
602 602 # specified pattern is the exact name
603 603 fset.remove(fn)
604 604 if match(fn):
605 605 yield fn
606 606 for fn in sorted(fset):
607 607 if fn in self._dirs:
608 608 # specified pattern is a directory
609 609 continue
610 610 match.bad(fn, _('no such file in rev %s') % self)
611 611
612 612 def matches(self, match):
613 613 return self.walk(match)
614 614
615 615 class basefilectx(object):
616 616 """A filecontext object represents the common logic for its children:
617 617 filectx: read-only access to a filerevision that is already present
618 618 in the repo,
619 619 workingfilectx: a filecontext that represents files from the working
620 620 directory,
621 621 memfilectx: a filecontext that represents files in-memory."""
622 622 def __new__(cls, repo, path, *args, **kwargs):
623 623 return super(basefilectx, cls).__new__(cls)
624 624
625 625 @propertycache
626 626 def _filelog(self):
627 627 return self._repo.file(self._path)
628 628
629 629 @propertycache
630 630 def _changeid(self):
631 631 if '_changeid' in self.__dict__:
632 632 return self._changeid
633 633 elif '_changectx' in self.__dict__:
634 634 return self._changectx.rev()
635 635 else:
636 636 return self._filelog.linkrev(self._filerev)
637 637
638 638 @propertycache
639 639 def _filenode(self):
640 640 if '_fileid' in self.__dict__:
641 641 return self._filelog.lookup(self._fileid)
642 642 else:
643 643 return self._changectx.filenode(self._path)
644 644
645 645 @propertycache
646 646 def _filerev(self):
647 647 return self._filelog.rev(self._filenode)
648 648
649 649 @propertycache
650 650 def _repopath(self):
651 651 return self._path
652 652
653 653 def __nonzero__(self):
654 654 try:
655 655 self._filenode
656 656 return True
657 657 except error.LookupError:
658 658 # file is missing
659 659 return False
660 660
661 661 def __str__(self):
662 662 return "%s@%s" % (self.path(), self._changectx)
663 663
664 664 def __repr__(self):
665 665 return "<%s %s>" % (type(self).__name__, str(self))
666 666
667 667 def __hash__(self):
668 668 try:
669 669 return hash((self._path, self._filenode))
670 670 except AttributeError:
671 671 return id(self)
672 672
673 673 def __eq__(self, other):
674 674 try:
675 675 return (type(self) == type(other) and self._path == other._path
676 676 and self._filenode == other._filenode)
677 677 except AttributeError:
678 678 return False
679 679
680 680 def __ne__(self, other):
681 681 return not (self == other)
682 682
683 683 def filerev(self):
684 684 return self._filerev
685 685 def filenode(self):
686 686 return self._filenode
687 687 def flags(self):
688 688 return self._changectx.flags(self._path)
689 689 def filelog(self):
690 690 return self._filelog
691 691 def rev(self):
692 692 return self._changeid
693 693 def linkrev(self):
694 694 return self._filelog.linkrev(self._filerev)
695 695 def node(self):
696 696 return self._changectx.node()
697 697 def hex(self):
698 698 return self._changectx.hex()
699 699 def user(self):
700 700 return self._changectx.user()
701 701 def date(self):
702 702 return self._changectx.date()
703 703 def files(self):
704 704 return self._changectx.files()
705 705 def description(self):
706 706 return self._changectx.description()
707 707 def branch(self):
708 708 return self._changectx.branch()
709 709 def extra(self):
710 710 return self._changectx.extra()
711 711 def phase(self):
712 712 return self._changectx.phase()
713 713 def phasestr(self):
714 714 return self._changectx.phasestr()
715 715 def manifest(self):
716 716 return self._changectx.manifest()
717 717 def changectx(self):
718 718 return self._changectx
719 719
720 720 def path(self):
721 721 return self._path
722 722
723 723 def isbinary(self):
724 724 try:
725 725 return util.binary(self.data())
726 726 except IOError:
727 727 return False
728 728 def isexec(self):
729 729 return 'x' in self.flags()
730 730 def islink(self):
731 731 return 'l' in self.flags()
732 732
733 733 def cmp(self, fctx):
734 734 """compare with other file context
735 735
736 736 returns True if different than fctx.
737 737 """
738 738 if (fctx._filerev is None
739 739 and (self._repo._encodefilterpats
740 740 # if file data starts with '\1\n', empty metadata block is
741 741 # prepended, which adds 4 bytes to filelog.size().
742 742 or self.size() - 4 == fctx.size())
743 743 or self.size() == fctx.size()):
744 744 return self._filelog.cmp(self._filenode, fctx.data())
745 745
746 746 return True
747 747
748 748 def parents(self):
749 749 _path = self._path
750 750 fl = self._filelog
751 751 pl = [(_path, n, fl) for n in self._filelog.parents(self._filenode)]
752 752
753 753 r = self._filelog.renamed(self._filenode)
754 754 if r:
755 755 pl[0] = (r[0], r[1], None)
756 756
757 757 return [filectx(self._repo, p, fileid=n, filelog=l)
758 758 for p, n, l in pl if n != nullid]
759 759
760 760 def p1(self):
761 761 return self.parents()[0]
762 762
763 763 def p2(self):
764 764 p = self.parents()
765 765 if len(p) == 2:
766 766 return p[1]
767 767 return filectx(self._repo, self._path, fileid=-1, filelog=self._filelog)
768 768
769 769 def annotate(self, follow=False, linenumber=None, diffopts=None):
770 770 '''returns a list of tuples of (ctx, line) for each line
771 771 in the file, where ctx is the filectx of the node where
772 772 that line was last changed.
773 773 This returns tuples of ((ctx, linenumber), line) for each line,
774 774 if "linenumber" parameter is NOT "None".
775 775 In such tuples, linenumber means one at the first appearance
776 776 in the managed file.
777 777 To reduce annotation cost,
778 778 this returns fixed value(False is used) as linenumber,
779 779 if "linenumber" parameter is "False".'''
780 780
781 781 if linenumber is None:
782 782 def decorate(text, rev):
783 783 return ([rev] * len(text.splitlines()), text)
784 784 elif linenumber:
785 785 def decorate(text, rev):
786 786 size = len(text.splitlines())
787 787 return ([(rev, i) for i in xrange(1, size + 1)], text)
788 788 else:
789 789 def decorate(text, rev):
790 790 return ([(rev, False)] * len(text.splitlines()), text)
791 791
792 792 def pair(parent, child):
793 793 blocks = mdiff.allblocks(parent[1], child[1], opts=diffopts,
794 794 refine=True)
795 795 for (a1, a2, b1, b2), t in blocks:
796 796 # Changed blocks ('!') or blocks made only of blank lines ('~')
797 797 # belong to the child.
798 798 if t == '=':
799 799 child[0][b1:b2] = parent[0][a1:a2]
800 800 return child
801 801
802 802 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
803 803
804 804 def parents(f):
805 805 pl = f.parents()
806 806
807 807 # Don't return renamed parents if we aren't following.
808 808 if not follow:
809 809 pl = [p for p in pl if p.path() == f.path()]
810 810
811 811 # renamed filectx won't have a filelog yet, so set it
812 812 # from the cache to save time
813 813 for p in pl:
814 814 if not '_filelog' in p.__dict__:
815 815 p._filelog = getlog(p.path())
816 816
817 817 return pl
818 818
819 819 # use linkrev to find the first changeset where self appeared
820 820 if self.rev() != self.linkrev():
821 821 base = self.filectx(self.filenode())
822 822 else:
823 823 base = self
824 824
825 825 # This algorithm would prefer to be recursive, but Python is a
826 826 # bit recursion-hostile. Instead we do an iterative
827 827 # depth-first search.
828 828
829 829 visit = [base]
830 830 hist = {}
831 831 pcache = {}
832 832 needed = {base: 1}
833 833 while visit:
834 834 f = visit[-1]
835 835 pcached = f in pcache
836 836 if not pcached:
837 837 pcache[f] = parents(f)
838 838
839 839 ready = True
840 840 pl = pcache[f]
841 841 for p in pl:
842 842 if p not in hist:
843 843 ready = False
844 844 visit.append(p)
845 845 if not pcached:
846 846 needed[p] = needed.get(p, 0) + 1
847 847 if ready:
848 848 visit.pop()
849 849 reusable = f in hist
850 850 if reusable:
851 851 curr = hist[f]
852 852 else:
853 853 curr = decorate(f.data(), f)
854 854 for p in pl:
855 855 if not reusable:
856 856 curr = pair(hist[p], curr)
857 857 if needed[p] == 1:
858 858 del hist[p]
859 859 del needed[p]
860 860 else:
861 861 needed[p] -= 1
862 862
863 863 hist[f] = curr
864 864 pcache[f] = []
865 865
866 866 return zip(hist[base][0], hist[base][1].splitlines(True))
867 867
868 868 def ancestors(self, followfirst=False):
869 869 visit = {}
870 870 c = self
871 871 cut = followfirst and 1 or None
872 872 while True:
873 873 for parent in c.parents()[:cut]:
874 874 visit[(parent.rev(), parent.node())] = parent
875 875 if not visit:
876 876 break
877 877 c = visit.pop(max(visit))
878 878 yield c
879 879
880 880 class filectx(basefilectx):
881 881 """A filecontext object makes access to data related to a particular
882 882 filerevision convenient."""
883 883 def __init__(self, repo, path, changeid=None, fileid=None,
884 884 filelog=None, changectx=None):
885 885 """changeid can be a changeset revision, node, or tag.
886 886 fileid can be a file revision or node."""
887 887 self._repo = repo
888 888 self._path = path
889 889
890 890 assert (changeid is not None
891 891 or fileid is not None
892 892 or changectx is not None), \
893 893 ("bad args: changeid=%r, fileid=%r, changectx=%r"
894 894 % (changeid, fileid, changectx))
895 895
896 896 if filelog is not None:
897 897 self._filelog = filelog
898 898
899 899 if changeid is not None:
900 900 self._changeid = changeid
901 901 if changectx is not None:
902 902 self._changectx = changectx
903 903 if fileid is not None:
904 904 self._fileid = fileid
905 905
906 906 @propertycache
907 907 def _changectx(self):
908 908 try:
909 909 return changectx(self._repo, self._changeid)
910 910 except error.RepoLookupError:
911 911 # Linkrev may point to any revision in the repository. When the
912 912 # repository is filtered this may lead to `filectx` trying to build
913 913 # `changectx` for filtered revision. In such case we fallback to
914 914 # creating `changectx` on the unfiltered version of the reposition.
915 915 # This fallback should not be an issue because `changectx` from
916 916 # `filectx` are not used in complex operations that care about
917 917 # filtering.
918 918 #
919 919 # This fallback is a cheap and dirty fix that prevent several
920 920 # crashes. It does not ensure the behavior is correct. However the
921 921 # behavior was not correct before filtering either and "incorrect
922 922 # behavior" is seen as better as "crash"
923 923 #
924 924 # Linkrevs have several serious troubles with filtering that are
925 925 # complicated to solve. Proper handling of the issue here should be
926 926 # considered when solving linkrev issue are on the table.
927 927 return changectx(self._repo.unfiltered(), self._changeid)
928 928
929 929 def filectx(self, fileid):
930 930 '''opens an arbitrary revision of the file without
931 931 opening a new filelog'''
932 932 return filectx(self._repo, self._path, fileid=fileid,
933 933 filelog=self._filelog)
934 934
935 935 def data(self):
936 936 try:
937 937 return self._filelog.read(self._filenode)
938 938 except error.CensoredNodeError:
939 939 if self._repo.ui.config("censor", "policy", "abort") == "ignore":
940 940 return ""
941 941 raise util.Abort(_("censored node: %s") % short(self._filenode),
942 942 hint=_("set censor.policy to ignore errors"))
943 943
944 944 def size(self):
945 945 return self._filelog.size(self._filerev)
946 946
947 947 def renamed(self):
948 948 """check if file was actually renamed in this changeset revision
949 949
950 950 If rename logged in file revision, we report copy for changeset only
951 951 if file revisions linkrev points back to the changeset in question
952 952 or both changeset parents contain different file revisions.
953 953 """
954 954
955 955 renamed = self._filelog.renamed(self._filenode)
956 956 if not renamed:
957 957 return renamed
958 958
959 959 if self.rev() == self.linkrev():
960 960 return renamed
961 961
962 962 name = self.path()
963 963 fnode = self._filenode
964 964 for p in self._changectx.parents():
965 965 try:
966 966 if fnode == p.filenode(name):
967 967 return None
968 968 except error.LookupError:
969 969 pass
970 970 return renamed
971 971
972 972 def children(self):
973 973 # hard for renames
974 974 c = self._filelog.children(self._filenode)
975 975 return [filectx(self._repo, self._path, fileid=x,
976 976 filelog=self._filelog) for x in c]
977 977
978 978 class committablectx(basectx):
979 979 """A committablectx object provides common functionality for a context that
980 980 wants the ability to commit, e.g. workingctx or memctx."""
981 981 def __init__(self, repo, text="", user=None, date=None, extra=None,
982 982 changes=None):
983 983 self._repo = repo
984 984 self._rev = None
985 985 self._node = None
986 986 self._text = text
987 987 if date:
988 988 self._date = util.parsedate(date)
989 989 if user:
990 990 self._user = user
991 991 if changes:
992 992 self._status = changes
993 993
994 994 self._extra = {}
995 995 if extra:
996 996 self._extra = extra.copy()
997 997 if 'branch' not in self._extra:
998 998 try:
999 999 branch = encoding.fromlocal(self._repo.dirstate.branch())
1000 1000 except UnicodeDecodeError:
1001 1001 raise util.Abort(_('branch name not in UTF-8!'))
1002 1002 self._extra['branch'] = branch
1003 1003 if self._extra['branch'] == '':
1004 1004 self._extra['branch'] = 'default'
1005 1005
1006 1006 def __str__(self):
1007 1007 return str(self._parents[0]) + "+"
1008 1008
1009 1009 def __nonzero__(self):
1010 1010 return True
1011 1011
1012 1012 def _buildflagfunc(self):
1013 1013 # Create a fallback function for getting file flags when the
1014 1014 # filesystem doesn't support them
1015 1015
1016 1016 copiesget = self._repo.dirstate.copies().get
1017 1017
1018 1018 if len(self._parents) < 2:
1019 1019 # when we have one parent, it's easy: copy from parent
1020 1020 man = self._parents[0].manifest()
1021 1021 def func(f):
1022 1022 f = copiesget(f, f)
1023 1023 return man.flags(f)
1024 1024 else:
1025 1025 # merges are tricky: we try to reconstruct the unstored
1026 1026 # result from the merge (issue1802)
1027 1027 p1, p2 = self._parents
1028 1028 pa = p1.ancestor(p2)
1029 1029 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
1030 1030
1031 1031 def func(f):
1032 1032 f = copiesget(f, f) # may be wrong for merges with copies
1033 1033 fl1, fl2, fla = m1.flags(f), m2.flags(f), ma.flags(f)
1034 1034 if fl1 == fl2:
1035 1035 return fl1
1036 1036 if fl1 == fla:
1037 1037 return fl2
1038 1038 if fl2 == fla:
1039 1039 return fl1
1040 1040 return '' # punt for conflicts
1041 1041
1042 1042 return func
1043 1043
1044 1044 @propertycache
1045 1045 def _flagfunc(self):
1046 1046 return self._repo.dirstate.flagfunc(self._buildflagfunc)
1047 1047
1048 1048 @propertycache
1049 1049 def _manifest(self):
1050 1050 """generate a manifest corresponding to the values in self._status"""
1051 1051
1052 1052 man = self._parents[0].manifest().copy()
1053 1053 if len(self._parents) > 1:
1054 1054 man2 = self.p2().manifest()
1055 1055 def getman(f):
1056 1056 if f in man:
1057 1057 return man
1058 1058 return man2
1059 1059 else:
1060 1060 getman = lambda f: man
1061 1061
1062 1062 copied = self._repo.dirstate.copies()
1063 1063 ff = self._flagfunc
1064 1064 for i, l in (("a", self._status.added), ("m", self._status.modified)):
1065 1065 for f in l:
1066 1066 orig = copied.get(f, f)
1067 1067 man[f] = getman(orig).get(orig, nullid) + i
1068 1068 try:
1069 1069 man.setflag(f, ff(f))
1070 1070 except OSError:
1071 1071 pass
1072 1072
1073 1073 for f in self._status.deleted + self._status.removed:
1074 1074 if f in man:
1075 1075 del man[f]
1076 1076
1077 1077 return man
1078 1078
1079 1079 @propertycache
1080 1080 def _status(self):
1081 1081 return self._repo.status()
1082 1082
1083 1083 @propertycache
1084 1084 def _user(self):
1085 1085 return self._repo.ui.username()
1086 1086
1087 1087 @propertycache
1088 1088 def _date(self):
1089 1089 return util.makedate()
1090 1090
1091 1091 def subrev(self, subpath):
1092 1092 return None
1093 1093
1094 1094 def user(self):
1095 1095 return self._user or self._repo.ui.username()
1096 1096 def date(self):
1097 1097 return self._date
1098 1098 def description(self):
1099 1099 return self._text
1100 1100 def files(self):
1101 1101 return sorted(self._status.modified + self._status.added +
1102 1102 self._status.removed)
1103 1103
1104 1104 def modified(self):
1105 1105 return self._status.modified
1106 1106 def added(self):
1107 1107 return self._status.added
1108 1108 def removed(self):
1109 1109 return self._status.removed
1110 1110 def deleted(self):
1111 1111 return self._status.deleted
1112 1112 def unknown(self):
1113 1113 return self._status.unknown
1114 1114 def ignored(self):
1115 1115 return self._status.ignored
1116 1116 def clean(self):
1117 1117 return self._status.clean
1118 1118 def branch(self):
1119 1119 return encoding.tolocal(self._extra['branch'])
1120 1120 def closesbranch(self):
1121 1121 return 'close' in self._extra
1122 1122 def extra(self):
1123 1123 return self._extra
1124 1124
1125 1125 def tags(self):
1126 1126 t = []
1127 1127 for p in self.parents():
1128 1128 t.extend(p.tags())
1129 1129 return t
1130 1130
1131 1131 def bookmarks(self):
1132 1132 b = []
1133 1133 for p in self.parents():
1134 1134 b.extend(p.bookmarks())
1135 1135 return b
1136 1136
1137 1137 def phase(self):
1138 1138 phase = phases.draft # default phase to draft
1139 1139 for p in self.parents():
1140 1140 phase = max(phase, p.phase())
1141 1141 return phase
1142 1142
1143 1143 def hidden(self):
1144 1144 return False
1145 1145
1146 1146 def children(self):
1147 1147 return []
1148 1148
1149 1149 def flags(self, path):
1150 1150 if '_manifest' in self.__dict__:
1151 1151 try:
1152 1152 return self._manifest.flags(path)
1153 1153 except KeyError:
1154 1154 return ''
1155 1155
1156 1156 try:
1157 1157 return self._flagfunc(path)
1158 1158 except OSError:
1159 1159 return ''
1160 1160
1161 1161 def ancestor(self, c2):
1162 1162 """return the "best" ancestor context of self and c2"""
1163 1163 return self._parents[0].ancestor(c2) # punt on two parents for now
1164 1164
1165 1165 def walk(self, match):
1166 1166 return sorted(self._repo.dirstate.walk(match, sorted(self.substate),
1167 1167 True, False))
1168 1168
1169 1169 def matches(self, match):
1170 1170 return sorted(self._repo.dirstate.matches(match))
1171 1171
1172 1172 def ancestors(self):
1173 1173 for a in self._repo.changelog.ancestors(
1174 1174 [p.rev() for p in self._parents]):
1175 1175 yield changectx(self._repo, a)
1176 1176
1177 1177 def markcommitted(self, node):
1178 1178 """Perform post-commit cleanup necessary after committing this ctx
1179 1179
1180 1180 Specifically, this updates backing stores this working context
1181 1181 wraps to reflect the fact that the changes reflected by this
1182 1182 workingctx have been committed. For example, it marks
1183 1183 modified and added files as normal in the dirstate.
1184 1184
1185 1185 """
1186 1186
1187 1187 self._repo.dirstate.beginparentchange()
1188 1188 for f in self.modified() + self.added():
1189 1189 self._repo.dirstate.normal(f)
1190 1190 for f in self.removed():
1191 1191 self._repo.dirstate.drop(f)
1192 1192 self._repo.dirstate.setparents(node)
1193 1193 self._repo.dirstate.endparentchange()
1194 1194
1195 1195 def dirs(self):
1196 1196 return self._repo.dirstate.dirs()
1197 1197
1198 1198 class workingctx(committablectx):
1199 1199 """A workingctx object makes access to data related to
1200 1200 the current working directory convenient.
1201 1201 date - any valid date string or (unixtime, offset), or None.
1202 1202 user - username string, or None.
1203 1203 extra - a dictionary of extra values, or None.
1204 1204 changes - a list of file lists as returned by localrepo.status()
1205 1205 or None to use the repository status.
1206 1206 """
1207 1207 def __init__(self, repo, text="", user=None, date=None, extra=None,
1208 1208 changes=None):
1209 1209 super(workingctx, self).__init__(repo, text, user, date, extra, changes)
1210 1210
1211 1211 def __iter__(self):
1212 1212 d = self._repo.dirstate
1213 1213 for f in d:
1214 1214 if d[f] != 'r':
1215 1215 yield f
1216 1216
1217 1217 def __contains__(self, key):
1218 1218 return self._repo.dirstate[key] not in "?r"
1219 1219
1220 1220 @propertycache
1221 1221 def _parents(self):
1222 1222 p = self._repo.dirstate.parents()
1223 1223 if p[1] == nullid:
1224 1224 p = p[:-1]
1225 1225 return [changectx(self._repo, x) for x in p]
1226 1226
1227 1227 def filectx(self, path, filelog=None):
1228 1228 """get a file context from the working directory"""
1229 1229 return workingfilectx(self._repo, path, workingctx=self,
1230 1230 filelog=filelog)
1231 1231
1232 1232 def dirty(self, missing=False, merge=True, branch=True):
1233 1233 "check whether a working directory is modified"
1234 1234 # check subrepos first
1235 1235 for s in sorted(self.substate):
1236 1236 if self.sub(s).dirty():
1237 1237 return True
1238 1238 # check current working dir
1239 1239 return ((merge and self.p2()) or
1240 1240 (branch and self.branch() != self.p1().branch()) or
1241 1241 self.modified() or self.added() or self.removed() or
1242 1242 (missing and self.deleted()))
1243 1243
1244 1244 def add(self, list, prefix=""):
1245 1245 join = lambda f: os.path.join(prefix, f)
1246 1246 wlock = self._repo.wlock()
1247 1247 ui, ds = self._repo.ui, self._repo.dirstate
1248 1248 try:
1249 1249 rejected = []
1250 1250 lstat = self._repo.wvfs.lstat
1251 1251 for f in list:
1252 1252 scmutil.checkportable(ui, join(f))
1253 1253 try:
1254 1254 st = lstat(f)
1255 1255 except OSError:
1256 1256 ui.warn(_("%s does not exist!\n") % join(f))
1257 1257 rejected.append(f)
1258 1258 continue
1259 1259 if st.st_size > 10000000:
1260 1260 ui.warn(_("%s: up to %d MB of RAM may be required "
1261 1261 "to manage this file\n"
1262 1262 "(use 'hg revert %s' to cancel the "
1263 1263 "pending addition)\n")
1264 1264 % (f, 3 * st.st_size // 1000000, join(f)))
1265 1265 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1266 1266 ui.warn(_("%s not added: only files and symlinks "
1267 1267 "supported currently\n") % join(f))
1268 1268 rejected.append(f)
1269 1269 elif ds[f] in 'amn':
1270 1270 ui.warn(_("%s already tracked!\n") % join(f))
1271 1271 elif ds[f] == 'r':
1272 1272 ds.normallookup(f)
1273 1273 else:
1274 1274 ds.add(f)
1275 1275 return rejected
1276 1276 finally:
1277 1277 wlock.release()
1278 1278
1279 1279 def forget(self, files, prefix=""):
1280 1280 join = lambda f: os.path.join(prefix, f)
1281 1281 wlock = self._repo.wlock()
1282 1282 try:
1283 1283 rejected = []
1284 1284 for f in files:
1285 1285 if f not in self._repo.dirstate:
1286 1286 self._repo.ui.warn(_("%s not tracked!\n") % join(f))
1287 1287 rejected.append(f)
1288 1288 elif self._repo.dirstate[f] != 'a':
1289 1289 self._repo.dirstate.remove(f)
1290 1290 else:
1291 1291 self._repo.dirstate.drop(f)
1292 1292 return rejected
1293 1293 finally:
1294 1294 wlock.release()
1295 1295
1296 1296 def undelete(self, list):
1297 1297 pctxs = self.parents()
1298 1298 wlock = self._repo.wlock()
1299 1299 try:
1300 1300 for f in list:
1301 1301 if self._repo.dirstate[f] != 'r':
1302 1302 self._repo.ui.warn(_("%s not removed!\n") % f)
1303 1303 else:
1304 1304 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
1305 1305 t = fctx.data()
1306 1306 self._repo.wwrite(f, t, fctx.flags())
1307 1307 self._repo.dirstate.normal(f)
1308 1308 finally:
1309 1309 wlock.release()
1310 1310
1311 1311 def copy(self, source, dest):
1312 1312 try:
1313 1313 st = self._repo.wvfs.lstat(dest)
1314 1314 except OSError, err:
1315 1315 if err.errno != errno.ENOENT:
1316 1316 raise
1317 1317 self._repo.ui.warn(_("%s does not exist!\n") % dest)
1318 1318 return
1319 1319 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1320 1320 self._repo.ui.warn(_("copy failed: %s is not a file or a "
1321 1321 "symbolic link\n") % dest)
1322 1322 else:
1323 1323 wlock = self._repo.wlock()
1324 1324 try:
1325 1325 if self._repo.dirstate[dest] in '?r':
1326 1326 self._repo.dirstate.add(dest)
1327 1327 self._repo.dirstate.copy(source, dest)
1328 1328 finally:
1329 1329 wlock.release()
1330 1330
1331 1331 def _filtersuspectsymlink(self, files):
1332 1332 if not files or self._repo.dirstate._checklink:
1333 1333 return files
1334 1334
1335 1335 # Symlink placeholders may get non-symlink-like contents
1336 1336 # via user error or dereferencing by NFS or Samba servers,
1337 1337 # so we filter out any placeholders that don't look like a
1338 1338 # symlink
1339 1339 sane = []
1340 1340 for f in files:
1341 1341 if self.flags(f) == 'l':
1342 1342 d = self[f].data()
1343 1343 if d == '' or len(d) >= 1024 or '\n' in d or util.binary(d):
1344 1344 self._repo.ui.debug('ignoring suspect symlink placeholder'
1345 1345 ' "%s"\n' % f)
1346 1346 continue
1347 1347 sane.append(f)
1348 1348 return sane
1349 1349
1350 1350 def _checklookup(self, files):
1351 1351 # check for any possibly clean files
1352 1352 if not files:
1353 1353 return [], []
1354 1354
1355 1355 modified = []
1356 1356 fixup = []
1357 1357 pctx = self._parents[0]
1358 1358 # do a full compare of any files that might have changed
1359 1359 for f in sorted(files):
1360 1360 if (f not in pctx or self.flags(f) != pctx.flags(f)
1361 1361 or pctx[f].cmp(self[f])):
1362 1362 modified.append(f)
1363 1363 else:
1364 1364 fixup.append(f)
1365 1365
1366 1366 # update dirstate for files that are actually clean
1367 1367 if fixup:
1368 1368 try:
1369 1369 # updating the dirstate is optional
1370 1370 # so we don't wait on the lock
1371 1371 # wlock can invalidate the dirstate, so cache normal _after_
1372 1372 # taking the lock
1373 1373 wlock = self._repo.wlock(False)
1374 1374 normal = self._repo.dirstate.normal
1375 1375 try:
1376 1376 for f in fixup:
1377 1377 normal(f)
1378 1378 finally:
1379 1379 wlock.release()
1380 1380 except error.LockError:
1381 1381 pass
1382 1382 return modified, fixup
1383 1383
1384 1384 def _manifestmatches(self, match, s):
1385 1385 """Slow path for workingctx
1386 1386
1387 1387 The fast path is when we compare the working directory to its parent
1388 1388 which means this function is comparing with a non-parent; therefore we
1389 1389 need to build a manifest and return what matches.
1390 1390 """
1391 1391 mf = self._repo['.']._manifestmatches(match, s)
1392 1392 modified, added, removed = s[0:3]
1393 1393 for f in modified + added:
1394 1394 mf[f] = None
1395 1395 mf.setflag(f, self.flags(f))
1396 1396 for f in removed:
1397 1397 if f in mf:
1398 1398 del mf[f]
1399 1399 return mf
1400 1400
1401 1401 def _dirstatestatus(self, match=None, ignored=False, clean=False,
1402 1402 unknown=False):
1403 1403 '''Gets the status from the dirstate -- internal use only.'''
1404 1404 listignored, listclean, listunknown = ignored, clean, unknown
1405 1405 match = match or matchmod.always(self._repo.root, self._repo.getcwd())
1406 1406 subrepos = []
1407 1407 if '.hgsub' in self:
1408 1408 subrepos = sorted(self.substate)
1409 1409 cmp, s = self._repo.dirstate.status(match, subrepos, listignored,
1410 1410 listclean, listunknown)
1411 1411 modified, added, removed, deleted, unknown, ignored, clean = s
1412 1412
1413 1413 # check for any possibly clean files
1414 1414 if cmp:
1415 1415 modified2, fixup = self._checklookup(cmp)
1416 1416 modified += modified2
1417 1417
1418 1418 # update dirstate for files that are actually clean
1419 1419 if fixup and listclean:
1420 1420 clean += fixup
1421 1421
1422 return [modified, added, removed, deleted, unknown, ignored, clean]
1422 return scmutil.status(modified, added, removed, deleted, unknown,
1423 ignored, clean)
1423 1424
1424 1425 def _buildstatus(self, other, s, match, listignored, listclean,
1425 1426 listunknown):
1426 1427 """build a status with respect to another context
1427 1428
1428 1429 This includes logic for maintaining the fast path of status when
1429 1430 comparing the working directory against its parent, which is to skip
1430 1431 building a new manifest if self (working directory) is not comparing
1431 1432 against its parent (repo['.']).
1432 1433 """
1433 1434 s = self._dirstatestatus(match, listignored, listclean, listunknown)
1434 1435 # Filter out symlinks that, in the case of FAT32 and NTFS filesytems,
1435 1436 # might have accidentally ended up with the entire contents of the file
1436 1437 # they are susposed to be linking to.
1437 s[0] = self._filtersuspectsymlink(s[0])
1438 s.modified[:] = self._filtersuspectsymlink(s.modified)
1438 1439 if other != self._repo['.']:
1439 1440 s = super(workingctx, self)._buildstatus(other, s, match,
1440 1441 listignored, listclean,
1441 1442 listunknown)
1442 self._status = scmutil.status(*s)
1443 self._status = s
1443 1444 return s
1444 1445
1445 1446 def _matchstatus(self, other, match):
1446 1447 """override the match method with a filter for directory patterns
1447 1448
1448 1449 We use inheritance to customize the match.bad method only in cases of
1449 1450 workingctx since it belongs only to the working directory when
1450 1451 comparing against the parent changeset.
1451 1452
1452 1453 If we aren't comparing against the working directory's parent, then we
1453 1454 just use the default match object sent to us.
1454 1455 """
1455 1456 superself = super(workingctx, self)
1456 1457 match = superself._matchstatus(other, match)
1457 1458 if other != self._repo['.']:
1458 1459 def bad(f, msg):
1459 1460 # 'f' may be a directory pattern from 'match.files()',
1460 1461 # so 'f not in ctx1' is not enough
1461 1462 if f not in other and f not in other.dirs():
1462 1463 self._repo.ui.warn('%s: %s\n' %
1463 1464 (self._repo.dirstate.pathto(f), msg))
1464 1465 match.bad = bad
1465 1466 return match
1466 1467
1467 1468 class committablefilectx(basefilectx):
1468 1469 """A committablefilectx provides common functionality for a file context
1469 1470 that wants the ability to commit, e.g. workingfilectx or memfilectx."""
1470 1471 def __init__(self, repo, path, filelog=None, ctx=None):
1471 1472 self._repo = repo
1472 1473 self._path = path
1473 1474 self._changeid = None
1474 1475 self._filerev = self._filenode = None
1475 1476
1476 1477 if filelog is not None:
1477 1478 self._filelog = filelog
1478 1479 if ctx:
1479 1480 self._changectx = ctx
1480 1481
1481 1482 def __nonzero__(self):
1482 1483 return True
1483 1484
1484 1485 def parents(self):
1485 1486 '''return parent filectxs, following copies if necessary'''
1486 1487 def filenode(ctx, path):
1487 1488 return ctx._manifest.get(path, nullid)
1488 1489
1489 1490 path = self._path
1490 1491 fl = self._filelog
1491 1492 pcl = self._changectx._parents
1492 1493 renamed = self.renamed()
1493 1494
1494 1495 if renamed:
1495 1496 pl = [renamed + (None,)]
1496 1497 else:
1497 1498 pl = [(path, filenode(pcl[0], path), fl)]
1498 1499
1499 1500 for pc in pcl[1:]:
1500 1501 pl.append((path, filenode(pc, path), fl))
1501 1502
1502 1503 return [filectx(self._repo, p, fileid=n, filelog=l)
1503 1504 for p, n, l in pl if n != nullid]
1504 1505
1505 1506 def children(self):
1506 1507 return []
1507 1508
1508 1509 class workingfilectx(committablefilectx):
1509 1510 """A workingfilectx object makes access to data related to a particular
1510 1511 file in the working directory convenient."""
1511 1512 def __init__(self, repo, path, filelog=None, workingctx=None):
1512 1513 super(workingfilectx, self).__init__(repo, path, filelog, workingctx)
1513 1514
1514 1515 @propertycache
1515 1516 def _changectx(self):
1516 1517 return workingctx(self._repo)
1517 1518
1518 1519 def data(self):
1519 1520 return self._repo.wread(self._path)
1520 1521 def renamed(self):
1521 1522 rp = self._repo.dirstate.copied(self._path)
1522 1523 if not rp:
1523 1524 return None
1524 1525 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
1525 1526
1526 1527 def size(self):
1527 1528 return self._repo.wvfs.lstat(self._path).st_size
1528 1529 def date(self):
1529 1530 t, tz = self._changectx.date()
1530 1531 try:
1531 1532 return (int(self._repo.wvfs.lstat(self._path).st_mtime), tz)
1532 1533 except OSError, err:
1533 1534 if err.errno != errno.ENOENT:
1534 1535 raise
1535 1536 return (t, tz)
1536 1537
1537 1538 def cmp(self, fctx):
1538 1539 """compare with other file context
1539 1540
1540 1541 returns True if different than fctx.
1541 1542 """
1542 1543 # fctx should be a filectx (not a workingfilectx)
1543 1544 # invert comparison to reuse the same code path
1544 1545 return fctx.cmp(self)
1545 1546
1546 1547 def remove(self, ignoremissing=False):
1547 1548 """wraps unlink for a repo's working directory"""
1548 1549 util.unlinkpath(self._repo.wjoin(self._path), ignoremissing)
1549 1550
1550 1551 def write(self, data, flags):
1551 1552 """wraps repo.wwrite"""
1552 1553 self._repo.wwrite(self._path, data, flags)
1553 1554
1554 1555 class memctx(committablectx):
1555 1556 """Use memctx to perform in-memory commits via localrepo.commitctx().
1556 1557
1557 1558 Revision information is supplied at initialization time while
1558 1559 related files data and is made available through a callback
1559 1560 mechanism. 'repo' is the current localrepo, 'parents' is a
1560 1561 sequence of two parent revisions identifiers (pass None for every
1561 1562 missing parent), 'text' is the commit message and 'files' lists
1562 1563 names of files touched by the revision (normalized and relative to
1563 1564 repository root).
1564 1565
1565 1566 filectxfn(repo, memctx, path) is a callable receiving the
1566 1567 repository, the current memctx object and the normalized path of
1567 1568 requested file, relative to repository root. It is fired by the
1568 1569 commit function for every file in 'files', but calls order is
1569 1570 undefined. If the file is available in the revision being
1570 1571 committed (updated or added), filectxfn returns a memfilectx
1571 1572 object. If the file was removed, filectxfn raises an
1572 1573 IOError. Moved files are represented by marking the source file
1573 1574 removed and the new file added with copy information (see
1574 1575 memfilectx).
1575 1576
1576 1577 user receives the committer name and defaults to current
1577 1578 repository username, date is the commit date in any format
1578 1579 supported by util.parsedate() and defaults to current date, extra
1579 1580 is a dictionary of metadata or is left empty.
1580 1581 """
1581 1582
1582 1583 # Mercurial <= 3.1 expects the filectxfn to raise IOError for missing files.
1583 1584 # Extensions that need to retain compatibility across Mercurial 3.1 can use
1584 1585 # this field to determine what to do in filectxfn.
1585 1586 _returnnoneformissingfiles = True
1586 1587
1587 1588 def __init__(self, repo, parents, text, files, filectxfn, user=None,
1588 1589 date=None, extra=None, editor=False):
1589 1590 super(memctx, self).__init__(repo, text, user, date, extra)
1590 1591 self._rev = None
1591 1592 self._node = None
1592 1593 parents = [(p or nullid) for p in parents]
1593 1594 p1, p2 = parents
1594 1595 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
1595 1596 files = sorted(set(files))
1596 1597 self._status = scmutil.status(files, [], [], [], [], [], [])
1597 1598 self._filectxfn = filectxfn
1598 1599 self.substate = {}
1599 1600
1600 1601 # if store is not callable, wrap it in a function
1601 1602 if not callable(filectxfn):
1602 1603 def getfilectx(repo, memctx, path):
1603 1604 fctx = filectxfn[path]
1604 1605 # this is weird but apparently we only keep track of one parent
1605 1606 # (why not only store that instead of a tuple?)
1606 1607 copied = fctx.renamed()
1607 1608 if copied:
1608 1609 copied = copied[0]
1609 1610 return memfilectx(repo, path, fctx.data(),
1610 1611 islink=fctx.islink(), isexec=fctx.isexec(),
1611 1612 copied=copied, memctx=memctx)
1612 1613 self._filectxfn = getfilectx
1613 1614
1614 1615 self._extra = extra and extra.copy() or {}
1615 1616 if self._extra.get('branch', '') == '':
1616 1617 self._extra['branch'] = 'default'
1617 1618
1618 1619 if editor:
1619 1620 self._text = editor(self._repo, self, [])
1620 1621 self._repo.savecommitmessage(self._text)
1621 1622
1622 1623 def filectx(self, path, filelog=None):
1623 1624 """get a file context from the working directory
1624 1625
1625 1626 Returns None if file doesn't exist and should be removed."""
1626 1627 return self._filectxfn(self._repo, self, path)
1627 1628
1628 1629 def commit(self):
1629 1630 """commit context to the repo"""
1630 1631 return self._repo.commitctx(self)
1631 1632
1632 1633 @propertycache
1633 1634 def _manifest(self):
1634 1635 """generate a manifest based on the return values of filectxfn"""
1635 1636
1636 1637 # keep this simple for now; just worry about p1
1637 1638 pctx = self._parents[0]
1638 1639 man = pctx.manifest().copy()
1639 1640
1640 1641 for f, fnode in man.iteritems():
1641 1642 p1node = nullid
1642 1643 p2node = nullid
1643 1644 p = pctx[f].parents() # if file isn't in pctx, check p2?
1644 1645 if len(p) > 0:
1645 1646 p1node = p[0].node()
1646 1647 if len(p) > 1:
1647 1648 p2node = p[1].node()
1648 1649 man[f] = revlog.hash(self[f].data(), p1node, p2node)
1649 1650
1650 1651 return man
1651 1652
1652 1653
1653 1654 class memfilectx(committablefilectx):
1654 1655 """memfilectx represents an in-memory file to commit.
1655 1656
1656 1657 See memctx and committablefilectx for more details.
1657 1658 """
1658 1659 def __init__(self, repo, path, data, islink=False,
1659 1660 isexec=False, copied=None, memctx=None):
1660 1661 """
1661 1662 path is the normalized file path relative to repository root.
1662 1663 data is the file content as a string.
1663 1664 islink is True if the file is a symbolic link.
1664 1665 isexec is True if the file is executable.
1665 1666 copied is the source file path if current file was copied in the
1666 1667 revision being committed, or None."""
1667 1668 super(memfilectx, self).__init__(repo, path, None, memctx)
1668 1669 self._data = data
1669 1670 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1670 1671 self._copied = None
1671 1672 if copied:
1672 1673 self._copied = (copied, nullid)
1673 1674
1674 1675 def data(self):
1675 1676 return self._data
1676 1677 def size(self):
1677 1678 return len(self.data())
1678 1679 def flags(self):
1679 1680 return self._flags
1680 1681 def renamed(self):
1681 1682 return self._copied
1682 1683
1683 1684 def remove(self, ignoremissing=False):
1684 1685 """wraps unlink for a repo's working directory"""
1685 1686 # need to figure out what to do here
1686 1687 del self._changectx[self._path]
1687 1688
1688 1689 def write(self, data, flags):
1689 1690 """wraps repo.wwrite"""
1690 1691 self._data = data
General Comments 0
You need to be logged in to leave comments. Login now