##// END OF EJS Templates
context: fix filectx.undelete() (issue2388)
Patrick Mezard -
r12360:4ae3e5df stable
parent child Browse files
Show More
@@ -1,1086 +1,1086 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 node import nullid, nullrev, short, hex
9 9 from i18n import _
10 10 import ancestor, bdiff, error, util, subrepo, patch
11 11 import os, errno, stat
12 12
13 13 propertycache = util.propertycache
14 14
15 15 class changectx(object):
16 16 """A changecontext object makes access to data related to a particular
17 17 changeset convenient."""
18 18 def __init__(self, repo, changeid=''):
19 19 """changeid is a revision number, node, or tag"""
20 20 if changeid == '':
21 21 changeid = '.'
22 22 self._repo = repo
23 23 if isinstance(changeid, (long, int)):
24 24 self._rev = changeid
25 25 self._node = self._repo.changelog.node(changeid)
26 26 else:
27 27 self._node = self._repo.lookup(changeid)
28 28 self._rev = self._repo.changelog.rev(self._node)
29 29
30 30 def __str__(self):
31 31 return short(self.node())
32 32
33 33 def __int__(self):
34 34 return self.rev()
35 35
36 36 def __repr__(self):
37 37 return "<changectx %s>" % str(self)
38 38
39 39 def __hash__(self):
40 40 try:
41 41 return hash(self._rev)
42 42 except AttributeError:
43 43 return id(self)
44 44
45 45 def __eq__(self, other):
46 46 try:
47 47 return self._rev == other._rev
48 48 except AttributeError:
49 49 return False
50 50
51 51 def __ne__(self, other):
52 52 return not (self == other)
53 53
54 54 def __nonzero__(self):
55 55 return self._rev != nullrev
56 56
57 57 @propertycache
58 58 def _changeset(self):
59 59 return self._repo.changelog.read(self.node())
60 60
61 61 @propertycache
62 62 def _manifest(self):
63 63 return self._repo.manifest.read(self._changeset[0])
64 64
65 65 @propertycache
66 66 def _manifestdelta(self):
67 67 return self._repo.manifest.readdelta(self._changeset[0])
68 68
69 69 @propertycache
70 70 def _parents(self):
71 71 p = self._repo.changelog.parentrevs(self._rev)
72 72 if p[1] == nullrev:
73 73 p = p[:-1]
74 74 return [changectx(self._repo, x) for x in p]
75 75
76 76 @propertycache
77 77 def substate(self):
78 78 return subrepo.state(self)
79 79
80 80 def __contains__(self, key):
81 81 return key in self._manifest
82 82
83 83 def __getitem__(self, key):
84 84 return self.filectx(key)
85 85
86 86 def __iter__(self):
87 87 for f in sorted(self._manifest):
88 88 yield f
89 89
90 90 def changeset(self):
91 91 return self._changeset
92 92 def manifest(self):
93 93 return self._manifest
94 94 def manifestnode(self):
95 95 return self._changeset[0]
96 96
97 97 def rev(self):
98 98 return self._rev
99 99 def node(self):
100 100 return self._node
101 101 def hex(self):
102 102 return hex(self._node)
103 103 def user(self):
104 104 return self._changeset[1]
105 105 def date(self):
106 106 return self._changeset[2]
107 107 def files(self):
108 108 return self._changeset[3]
109 109 def description(self):
110 110 return self._changeset[4]
111 111 def branch(self):
112 112 return self._changeset[5].get("branch")
113 113 def extra(self):
114 114 return self._changeset[5]
115 115 def tags(self):
116 116 return self._repo.nodetags(self._node)
117 117
118 118 def parents(self):
119 119 """return contexts for each parent changeset"""
120 120 return self._parents
121 121
122 122 def p1(self):
123 123 return self._parents[0]
124 124
125 125 def p2(self):
126 126 if len(self._parents) == 2:
127 127 return self._parents[1]
128 128 return changectx(self._repo, -1)
129 129
130 130 def children(self):
131 131 """return contexts for each child changeset"""
132 132 c = self._repo.changelog.children(self._node)
133 133 return [changectx(self._repo, x) for x in c]
134 134
135 135 def ancestors(self):
136 136 for a in self._repo.changelog.ancestors(self._rev):
137 137 yield changectx(self._repo, a)
138 138
139 139 def descendants(self):
140 140 for d in self._repo.changelog.descendants(self._rev):
141 141 yield changectx(self._repo, d)
142 142
143 143 def _fileinfo(self, path):
144 144 if '_manifest' in self.__dict__:
145 145 try:
146 146 return self._manifest[path], self._manifest.flags(path)
147 147 except KeyError:
148 148 raise error.LookupError(self._node, path,
149 149 _('not found in manifest'))
150 150 if '_manifestdelta' in self.__dict__ or path in self.files():
151 151 if path in self._manifestdelta:
152 152 return self._manifestdelta[path], self._manifestdelta.flags(path)
153 153 node, flag = self._repo.manifest.find(self._changeset[0], path)
154 154 if not node:
155 155 raise error.LookupError(self._node, path,
156 156 _('not found in manifest'))
157 157
158 158 return node, flag
159 159
160 160 def filenode(self, path):
161 161 return self._fileinfo(path)[0]
162 162
163 163 def flags(self, path):
164 164 try:
165 165 return self._fileinfo(path)[1]
166 166 except error.LookupError:
167 167 return ''
168 168
169 169 def filectx(self, path, fileid=None, filelog=None):
170 170 """get a file context from this changeset"""
171 171 if fileid is None:
172 172 fileid = self.filenode(path)
173 173 return filectx(self._repo, path, fileid=fileid,
174 174 changectx=self, filelog=filelog)
175 175
176 176 def ancestor(self, c2):
177 177 """
178 178 return the ancestor context of self and c2
179 179 """
180 180 # deal with workingctxs
181 181 n2 = c2._node
182 182 if n2 == None:
183 183 n2 = c2._parents[0]._node
184 184 n = self._repo.changelog.ancestor(self._node, n2)
185 185 return changectx(self._repo, n)
186 186
187 187 def walk(self, match):
188 188 fset = set(match.files())
189 189 # for dirstate.walk, files=['.'] means "walk the whole tree".
190 190 # follow that here, too
191 191 fset.discard('.')
192 192 for fn in self:
193 193 for ffn in fset:
194 194 # match if the file is the exact name or a directory
195 195 if ffn == fn or fn.startswith("%s/" % ffn):
196 196 fset.remove(ffn)
197 197 break
198 198 if match(fn):
199 199 yield fn
200 200 for fn in sorted(fset):
201 201 if match.bad(fn, _('no such file in rev %s') % self) and match(fn):
202 202 yield fn
203 203
204 204 def sub(self, path):
205 205 return subrepo.subrepo(self, path)
206 206
207 207 def diff(self, ctx2=None, match=None, **opts):
208 208 """Returns a diff generator for the given contexts and matcher"""
209 209 if ctx2 is None:
210 210 ctx2 = self.p1()
211 211 if ctx2 is not None and not isinstance(ctx2, changectx):
212 212 ctx2 = self._repo[ctx2]
213 213 diffopts = patch.diffopts(self._repo.ui, opts)
214 214 return patch.diff(self._repo, ctx2.node(), self.node(),
215 215 match=match, opts=diffopts)
216 216
217 217 class filectx(object):
218 218 """A filecontext object makes access to data related to a particular
219 219 filerevision convenient."""
220 220 def __init__(self, repo, path, changeid=None, fileid=None,
221 221 filelog=None, changectx=None):
222 222 """changeid can be a changeset revision, node, or tag.
223 223 fileid can be a file revision or node."""
224 224 self._repo = repo
225 225 self._path = path
226 226
227 227 assert (changeid is not None
228 228 or fileid is not None
229 229 or changectx is not None), \
230 230 ("bad args: changeid=%r, fileid=%r, changectx=%r"
231 231 % (changeid, fileid, changectx))
232 232
233 233 if filelog:
234 234 self._filelog = filelog
235 235
236 236 if changeid is not None:
237 237 self._changeid = changeid
238 238 if changectx is not None:
239 239 self._changectx = changectx
240 240 if fileid is not None:
241 241 self._fileid = fileid
242 242
243 243 @propertycache
244 244 def _changectx(self):
245 245 return changectx(self._repo, self._changeid)
246 246
247 247 @propertycache
248 248 def _filelog(self):
249 249 return self._repo.file(self._path)
250 250
251 251 @propertycache
252 252 def _changeid(self):
253 253 if '_changectx' in self.__dict__:
254 254 return self._changectx.rev()
255 255 else:
256 256 return self._filelog.linkrev(self._filerev)
257 257
258 258 @propertycache
259 259 def _filenode(self):
260 260 if '_fileid' in self.__dict__:
261 261 return self._filelog.lookup(self._fileid)
262 262 else:
263 263 return self._changectx.filenode(self._path)
264 264
265 265 @propertycache
266 266 def _filerev(self):
267 267 return self._filelog.rev(self._filenode)
268 268
269 269 @propertycache
270 270 def _repopath(self):
271 271 return self._path
272 272
273 273 def __nonzero__(self):
274 274 try:
275 275 self._filenode
276 276 return True
277 277 except error.LookupError:
278 278 # file is missing
279 279 return False
280 280
281 281 def __str__(self):
282 282 return "%s@%s" % (self.path(), short(self.node()))
283 283
284 284 def __repr__(self):
285 285 return "<filectx %s>" % str(self)
286 286
287 287 def __hash__(self):
288 288 try:
289 289 return hash((self._path, self._filenode))
290 290 except AttributeError:
291 291 return id(self)
292 292
293 293 def __eq__(self, other):
294 294 try:
295 295 return (self._path == other._path
296 296 and self._filenode == other._filenode)
297 297 except AttributeError:
298 298 return False
299 299
300 300 def __ne__(self, other):
301 301 return not (self == other)
302 302
303 303 def filectx(self, fileid):
304 304 '''opens an arbitrary revision of the file without
305 305 opening a new filelog'''
306 306 return filectx(self._repo, self._path, fileid=fileid,
307 307 filelog=self._filelog)
308 308
309 309 def filerev(self):
310 310 return self._filerev
311 311 def filenode(self):
312 312 return self._filenode
313 313 def flags(self):
314 314 return self._changectx.flags(self._path)
315 315 def filelog(self):
316 316 return self._filelog
317 317
318 318 def rev(self):
319 319 if '_changectx' in self.__dict__:
320 320 return self._changectx.rev()
321 321 if '_changeid' in self.__dict__:
322 322 return self._changectx.rev()
323 323 return self._filelog.linkrev(self._filerev)
324 324
325 325 def linkrev(self):
326 326 return self._filelog.linkrev(self._filerev)
327 327 def node(self):
328 328 return self._changectx.node()
329 329 def hex(self):
330 330 return hex(self.node())
331 331 def user(self):
332 332 return self._changectx.user()
333 333 def date(self):
334 334 return self._changectx.date()
335 335 def files(self):
336 336 return self._changectx.files()
337 337 def description(self):
338 338 return self._changectx.description()
339 339 def branch(self):
340 340 return self._changectx.branch()
341 341 def extra(self):
342 342 return self._changectx.extra()
343 343 def manifest(self):
344 344 return self._changectx.manifest()
345 345 def changectx(self):
346 346 return self._changectx
347 347
348 348 def data(self):
349 349 return self._filelog.read(self._filenode)
350 350 def path(self):
351 351 return self._path
352 352 def size(self):
353 353 return self._filelog.size(self._filerev)
354 354
355 355 def cmp(self, text):
356 356 """compare text with stored file revision
357 357
358 358 returns True if text is different than what is stored.
359 359 """
360 360 return self._filelog.cmp(self._filenode, text)
361 361
362 362 def renamed(self):
363 363 """check if file was actually renamed in this changeset revision
364 364
365 365 If rename logged in file revision, we report copy for changeset only
366 366 if file revisions linkrev points back to the changeset in question
367 367 or both changeset parents contain different file revisions.
368 368 """
369 369
370 370 renamed = self._filelog.renamed(self._filenode)
371 371 if not renamed:
372 372 return renamed
373 373
374 374 if self.rev() == self.linkrev():
375 375 return renamed
376 376
377 377 name = self.path()
378 378 fnode = self._filenode
379 379 for p in self._changectx.parents():
380 380 try:
381 381 if fnode == p.filenode(name):
382 382 return None
383 383 except error.LookupError:
384 384 pass
385 385 return renamed
386 386
387 387 def parents(self):
388 388 p = self._path
389 389 fl = self._filelog
390 390 pl = [(p, n, fl) for n in self._filelog.parents(self._filenode)]
391 391
392 392 r = self._filelog.renamed(self._filenode)
393 393 if r:
394 394 pl[0] = (r[0], r[1], None)
395 395
396 396 return [filectx(self._repo, p, fileid=n, filelog=l)
397 397 for p, n, l in pl if n != nullid]
398 398
399 399 def children(self):
400 400 # hard for renames
401 401 c = self._filelog.children(self._filenode)
402 402 return [filectx(self._repo, self._path, fileid=x,
403 403 filelog=self._filelog) for x in c]
404 404
405 405 def annotate(self, follow=False, linenumber=None):
406 406 '''returns a list of tuples of (ctx, line) for each line
407 407 in the file, where ctx is the filectx of the node where
408 408 that line was last changed.
409 409 This returns tuples of ((ctx, linenumber), line) for each line,
410 410 if "linenumber" parameter is NOT "None".
411 411 In such tuples, linenumber means one at the first appearance
412 412 in the managed file.
413 413 To reduce annotation cost,
414 414 this returns fixed value(False is used) as linenumber,
415 415 if "linenumber" parameter is "False".'''
416 416
417 417 def decorate_compat(text, rev):
418 418 return ([rev] * len(text.splitlines()), text)
419 419
420 420 def without_linenumber(text, rev):
421 421 return ([(rev, False)] * len(text.splitlines()), text)
422 422
423 423 def with_linenumber(text, rev):
424 424 size = len(text.splitlines())
425 425 return ([(rev, i) for i in xrange(1, size + 1)], text)
426 426
427 427 decorate = (((linenumber is None) and decorate_compat) or
428 428 (linenumber and with_linenumber) or
429 429 without_linenumber)
430 430
431 431 def pair(parent, child):
432 432 for a1, a2, b1, b2 in bdiff.blocks(parent[1], child[1]):
433 433 child[0][b1:b2] = parent[0][a1:a2]
434 434 return child
435 435
436 436 getlog = util.lrucachefunc(lambda x: self._repo.file(x))
437 437 def getctx(path, fileid):
438 438 log = path == self._path and self._filelog or getlog(path)
439 439 return filectx(self._repo, path, fileid=fileid, filelog=log)
440 440 getctx = util.lrucachefunc(getctx)
441 441
442 442 def parents(f):
443 443 # we want to reuse filectx objects as much as possible
444 444 p = f._path
445 445 if f._filerev is None: # working dir
446 446 pl = [(n.path(), n.filerev()) for n in f.parents()]
447 447 else:
448 448 pl = [(p, n) for n in f._filelog.parentrevs(f._filerev)]
449 449
450 450 if follow:
451 451 r = f.renamed()
452 452 if r:
453 453 pl[0] = (r[0], getlog(r[0]).rev(r[1]))
454 454
455 455 return [getctx(p, n) for p, n in pl if n != nullrev]
456 456
457 457 # use linkrev to find the first changeset where self appeared
458 458 if self.rev() != self.linkrev():
459 459 base = self.filectx(self.filerev())
460 460 else:
461 461 base = self
462 462
463 463 # find all ancestors
464 464 needed = {base: 1}
465 465 visit = [base]
466 466 files = [base._path]
467 467 while visit:
468 468 f = visit.pop(0)
469 469 for p in parents(f):
470 470 if p not in needed:
471 471 needed[p] = 1
472 472 visit.append(p)
473 473 if p._path not in files:
474 474 files.append(p._path)
475 475 else:
476 476 # count how many times we'll use this
477 477 needed[p] += 1
478 478
479 479 # sort by revision (per file) which is a topological order
480 480 visit = []
481 481 for f in files:
482 482 visit.extend(n for n in needed if n._path == f)
483 483
484 484 hist = {}
485 485 for f in sorted(visit, key=lambda x: x.rev()):
486 486 curr = decorate(f.data(), f)
487 487 for p in parents(f):
488 488 curr = pair(hist[p], curr)
489 489 # trim the history of unneeded revs
490 490 needed[p] -= 1
491 491 if not needed[p]:
492 492 del hist[p]
493 493 hist[f] = curr
494 494
495 495 return zip(hist[f][0], hist[f][1].splitlines(True))
496 496
497 497 def ancestor(self, fc2, actx=None):
498 498 """
499 499 find the common ancestor file context, if any, of self, and fc2
500 500
501 501 If actx is given, it must be the changectx of the common ancestor
502 502 of self's and fc2's respective changesets.
503 503 """
504 504
505 505 if actx is None:
506 506 actx = self.changectx().ancestor(fc2.changectx())
507 507
508 508 # the trivial case: changesets are unrelated, files must be too
509 509 if not actx:
510 510 return None
511 511
512 512 # the easy case: no (relevant) renames
513 513 if fc2.path() == self.path() and self.path() in actx:
514 514 return actx[self.path()]
515 515 acache = {}
516 516
517 517 # prime the ancestor cache for the working directory
518 518 for c in (self, fc2):
519 519 if c._filerev is None:
520 520 pl = [(n.path(), n.filenode()) for n in c.parents()]
521 521 acache[(c._path, None)] = pl
522 522
523 523 flcache = {self._repopath:self._filelog, fc2._repopath:fc2._filelog}
524 524 def parents(vertex):
525 525 if vertex in acache:
526 526 return acache[vertex]
527 527 f, n = vertex
528 528 if f not in flcache:
529 529 flcache[f] = self._repo.file(f)
530 530 fl = flcache[f]
531 531 pl = [(f, p) for p in fl.parents(n) if p != nullid]
532 532 re = fl.renamed(n)
533 533 if re:
534 534 pl.append(re)
535 535 acache[vertex] = pl
536 536 return pl
537 537
538 538 a, b = (self._path, self._filenode), (fc2._path, fc2._filenode)
539 539 v = ancestor.ancestor(a, b, parents)
540 540 if v:
541 541 f, n = v
542 542 return filectx(self._repo, f, fileid=n, filelog=flcache[f])
543 543
544 544 return None
545 545
546 546 def ancestors(self):
547 547 seen = set(str(self))
548 548 visit = [self]
549 549 while visit:
550 550 for parent in visit.pop(0).parents():
551 551 s = str(parent)
552 552 if s not in seen:
553 553 visit.append(parent)
554 554 seen.add(s)
555 555 yield parent
556 556
557 557 class workingctx(changectx):
558 558 """A workingctx object makes access to data related to
559 559 the current working directory convenient.
560 560 date - any valid date string or (unixtime, offset), or None.
561 561 user - username string, or None.
562 562 extra - a dictionary of extra values, or None.
563 563 changes - a list of file lists as returned by localrepo.status()
564 564 or None to use the repository status.
565 565 """
566 566 def __init__(self, repo, text="", user=None, date=None, extra=None,
567 567 changes=None):
568 568 self._repo = repo
569 569 self._rev = None
570 570 self._node = None
571 571 self._text = text
572 572 if date:
573 573 self._date = util.parsedate(date)
574 574 if user:
575 575 self._user = user
576 576 if changes:
577 577 self._status = list(changes[:4])
578 578 self._unknown = changes[4]
579 579 self._ignored = changes[5]
580 580 self._clean = changes[6]
581 581 else:
582 582 self._unknown = None
583 583 self._ignored = None
584 584 self._clean = None
585 585
586 586 self._extra = {}
587 587 if extra:
588 588 self._extra = extra.copy()
589 589 if 'branch' not in self._extra:
590 590 branch = self._repo.dirstate.branch()
591 591 try:
592 592 branch = branch.decode('UTF-8').encode('UTF-8')
593 593 except UnicodeDecodeError:
594 594 raise util.Abort(_('branch name not in UTF-8!'))
595 595 self._extra['branch'] = branch
596 596 if self._extra['branch'] == '':
597 597 self._extra['branch'] = 'default'
598 598
599 599 def __str__(self):
600 600 return str(self._parents[0]) + "+"
601 601
602 602 def __nonzero__(self):
603 603 return True
604 604
605 605 def __contains__(self, key):
606 606 return self._repo.dirstate[key] not in "?r"
607 607
608 608 @propertycache
609 609 def _manifest(self):
610 610 """generate a manifest corresponding to the working directory"""
611 611
612 612 if self._unknown is None:
613 613 self.status(unknown=True)
614 614
615 615 man = self._parents[0].manifest().copy()
616 616 copied = self._repo.dirstate.copies()
617 617 if len(self._parents) > 1:
618 618 man2 = self.p2().manifest()
619 619 def getman(f):
620 620 if f in man:
621 621 return man
622 622 return man2
623 623 else:
624 624 getman = lambda f: man
625 625 def cf(f):
626 626 f = copied.get(f, f)
627 627 return getman(f).flags(f)
628 628 ff = self._repo.dirstate.flagfunc(cf)
629 629 modified, added, removed, deleted = self._status
630 630 unknown = self._unknown
631 631 for i, l in (("a", added), ("m", modified), ("u", unknown)):
632 632 for f in l:
633 633 orig = copied.get(f, f)
634 634 man[f] = getman(orig).get(orig, nullid) + i
635 635 try:
636 636 man.set(f, ff(f))
637 637 except OSError:
638 638 pass
639 639
640 640 for f in deleted + removed:
641 641 if f in man:
642 642 del man[f]
643 643
644 644 return man
645 645
646 646 @propertycache
647 647 def _status(self):
648 648 return self._repo.status()[:4]
649 649
650 650 @propertycache
651 651 def _user(self):
652 652 return self._repo.ui.username()
653 653
654 654 @propertycache
655 655 def _date(self):
656 656 return util.makedate()
657 657
658 658 @propertycache
659 659 def _parents(self):
660 660 p = self._repo.dirstate.parents()
661 661 if p[1] == nullid:
662 662 p = p[:-1]
663 663 self._parents = [changectx(self._repo, x) for x in p]
664 664 return self._parents
665 665
666 666 def status(self, ignored=False, clean=False, unknown=False):
667 667 """Explicit status query
668 668 Unless this method is used to query the working copy status, the
669 669 _status property will implicitly read the status using its default
670 670 arguments."""
671 671 stat = self._repo.status(ignored=ignored, clean=clean, unknown=unknown)
672 672 self._unknown = self._ignored = self._clean = None
673 673 if unknown:
674 674 self._unknown = stat[4]
675 675 if ignored:
676 676 self._ignored = stat[5]
677 677 if clean:
678 678 self._clean = stat[6]
679 679 self._status = stat[:4]
680 680 return stat
681 681
682 682 def manifest(self):
683 683 return self._manifest
684 684 def user(self):
685 685 return self._user or self._repo.ui.username()
686 686 def date(self):
687 687 return self._date
688 688 def description(self):
689 689 return self._text
690 690 def files(self):
691 691 return sorted(self._status[0] + self._status[1] + self._status[2])
692 692
693 693 def modified(self):
694 694 return self._status[0]
695 695 def added(self):
696 696 return self._status[1]
697 697 def removed(self):
698 698 return self._status[2]
699 699 def deleted(self):
700 700 return self._status[3]
701 701 def unknown(self):
702 702 assert self._unknown is not None # must call status first
703 703 return self._unknown
704 704 def ignored(self):
705 705 assert self._ignored is not None # must call status first
706 706 return self._ignored
707 707 def clean(self):
708 708 assert self._clean is not None # must call status first
709 709 return self._clean
710 710 def branch(self):
711 711 return self._extra['branch']
712 712 def extra(self):
713 713 return self._extra
714 714
715 715 def tags(self):
716 716 t = []
717 717 [t.extend(p.tags()) for p in self.parents()]
718 718 return t
719 719
720 720 def children(self):
721 721 return []
722 722
723 723 def flags(self, path):
724 724 if '_manifest' in self.__dict__:
725 725 try:
726 726 return self._manifest.flags(path)
727 727 except KeyError:
728 728 return ''
729 729
730 730 orig = self._repo.dirstate.copies().get(path, path)
731 731
732 732 def findflag(ctx):
733 733 mnode = ctx.changeset()[0]
734 734 node, flag = self._repo.manifest.find(mnode, orig)
735 735 ff = self._repo.dirstate.flagfunc(lambda x: flag or '')
736 736 try:
737 737 return ff(path)
738 738 except OSError:
739 739 pass
740 740
741 741 flag = findflag(self._parents[0])
742 742 if flag is None and len(self.parents()) > 1:
743 743 flag = findflag(self._parents[1])
744 744 if flag is None or self._repo.dirstate[path] == 'r':
745 745 return ''
746 746 return flag
747 747
748 748 def filectx(self, path, filelog=None):
749 749 """get a file context from the working directory"""
750 750 return workingfilectx(self._repo, path, workingctx=self,
751 751 filelog=filelog)
752 752
753 753 def ancestor(self, c2):
754 754 """return the ancestor context of self and c2"""
755 755 return self._parents[0].ancestor(c2) # punt on two parents for now
756 756
757 757 def walk(self, match):
758 758 return sorted(self._repo.dirstate.walk(match, self.substate.keys(),
759 759 True, False))
760 760
761 761 def dirty(self, missing=False):
762 762 "check whether a working directory is modified"
763 763 # check subrepos first
764 764 for s in self.substate:
765 765 if self.sub(s).dirty():
766 766 return True
767 767 # check current working dir
768 768 return (self.p2() or self.branch() != self.p1().branch() or
769 769 self.modified() or self.added() or self.removed() or
770 770 (missing and self.deleted()))
771 771
772 772 def add(self, list):
773 773 wlock = self._repo.wlock()
774 774 ui, ds = self._repo.ui, self._repo.dirstate
775 775 try:
776 776 rejected = []
777 777 for f in list:
778 778 p = self._repo.wjoin(f)
779 779 try:
780 780 st = os.lstat(p)
781 781 except:
782 782 ui.warn(_("%s does not exist!\n") % f)
783 783 rejected.append(f)
784 784 continue
785 785 if st.st_size > 10000000:
786 786 ui.warn(_("%s: up to %d MB of RAM may be required "
787 787 "to manage this file\n"
788 788 "(use 'hg revert %s' to cancel the "
789 789 "pending addition)\n")
790 790 % (f, 3 * st.st_size // 1000000, f))
791 791 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
792 792 ui.warn(_("%s not added: only files and symlinks "
793 793 "supported currently\n") % f)
794 794 rejected.append(p)
795 795 elif ds[f] in 'amn':
796 796 ui.warn(_("%s already tracked!\n") % f)
797 797 elif ds[f] == 'r':
798 798 ds.normallookup(f)
799 799 else:
800 800 ds.add(f)
801 801 return rejected
802 802 finally:
803 803 wlock.release()
804 804
805 805 def forget(self, list):
806 806 wlock = self._repo.wlock()
807 807 try:
808 808 for f in list:
809 809 if self._repo.dirstate[f] != 'a':
810 810 self._repo.ui.warn(_("%s not added!\n") % f)
811 811 else:
812 812 self._repo.dirstate.forget(f)
813 813 finally:
814 814 wlock.release()
815 815
816 816 def remove(self, list, unlink=False):
817 817 if unlink:
818 818 for f in list:
819 819 try:
820 820 util.unlink(self._repo.wjoin(f))
821 821 except OSError, inst:
822 822 if inst.errno != errno.ENOENT:
823 823 raise
824 824 wlock = self._repo.wlock()
825 825 try:
826 826 for f in list:
827 827 if unlink and os.path.lexists(self._repo.wjoin(f)):
828 828 self._repo.ui.warn(_("%s still exists!\n") % f)
829 829 elif self._repo.dirstate[f] == 'a':
830 830 self._repo.dirstate.forget(f)
831 831 elif f not in self._repo.dirstate:
832 832 self._repo.ui.warn(_("%s not tracked!\n") % f)
833 833 else:
834 834 self._repo.dirstate.remove(f)
835 835 finally:
836 836 wlock.release()
837 837
838 838 def undelete(self, list):
839 839 pctxs = self.parents()
840 840 wlock = self._repo.wlock()
841 841 try:
842 842 for f in list:
843 843 if self._repo.dirstate[f] != 'r':
844 844 self._repo.ui.warn(_("%s not removed!\n") % f)
845 845 else:
846 fctx = f in pctxs[0] and pctxs[0] or pctxs[1]
846 fctx = f in pctxs[0] and pctxs[0][f] or pctxs[1][f]
847 847 t = fctx.data()
848 848 self._repo.wwrite(f, t, fctx.flags())
849 849 self._repo.dirstate.normal(f)
850 850 finally:
851 851 wlock.release()
852 852
853 853 def copy(self, source, dest):
854 854 p = self._repo.wjoin(dest)
855 855 if not os.path.lexists(p):
856 856 self._repo.ui.warn(_("%s does not exist!\n") % dest)
857 857 elif not (os.path.isfile(p) or os.path.islink(p)):
858 858 self._repo.ui.warn(_("copy failed: %s is not a file or a "
859 859 "symbolic link\n") % dest)
860 860 else:
861 861 wlock = self._repo.wlock()
862 862 try:
863 863 if self._repo.dirstate[dest] in '?r':
864 864 self._repo.dirstate.add(dest)
865 865 self._repo.dirstate.copy(source, dest)
866 866 finally:
867 867 wlock.release()
868 868
869 869 class workingfilectx(filectx):
870 870 """A workingfilectx object makes access to data related to a particular
871 871 file in the working directory convenient."""
872 872 def __init__(self, repo, path, filelog=None, workingctx=None):
873 873 """changeid can be a changeset revision, node, or tag.
874 874 fileid can be a file revision or node."""
875 875 self._repo = repo
876 876 self._path = path
877 877 self._changeid = None
878 878 self._filerev = self._filenode = None
879 879
880 880 if filelog:
881 881 self._filelog = filelog
882 882 if workingctx:
883 883 self._changectx = workingctx
884 884
885 885 @propertycache
886 886 def _changectx(self):
887 887 return workingctx(self._repo)
888 888
889 889 def __nonzero__(self):
890 890 return True
891 891
892 892 def __str__(self):
893 893 return "%s@%s" % (self.path(), self._changectx)
894 894
895 895 def data(self):
896 896 return self._repo.wread(self._path)
897 897 def renamed(self):
898 898 rp = self._repo.dirstate.copied(self._path)
899 899 if not rp:
900 900 return None
901 901 return rp, self._changectx._parents[0]._manifest.get(rp, nullid)
902 902
903 903 def parents(self):
904 904 '''return parent filectxs, following copies if necessary'''
905 905 def filenode(ctx, path):
906 906 return ctx._manifest.get(path, nullid)
907 907
908 908 path = self._path
909 909 fl = self._filelog
910 910 pcl = self._changectx._parents
911 911 renamed = self.renamed()
912 912
913 913 if renamed:
914 914 pl = [renamed + (None,)]
915 915 else:
916 916 pl = [(path, filenode(pcl[0], path), fl)]
917 917
918 918 for pc in pcl[1:]:
919 919 pl.append((path, filenode(pc, path), fl))
920 920
921 921 return [filectx(self._repo, p, fileid=n, filelog=l)
922 922 for p, n, l in pl if n != nullid]
923 923
924 924 def children(self):
925 925 return []
926 926
927 927 def size(self):
928 928 return os.lstat(self._repo.wjoin(self._path)).st_size
929 929 def date(self):
930 930 t, tz = self._changectx.date()
931 931 try:
932 932 return (int(os.lstat(self._repo.wjoin(self._path)).st_mtime), tz)
933 933 except OSError, err:
934 934 if err.errno != errno.ENOENT:
935 935 raise
936 936 return (t, tz)
937 937
938 938 def cmp(self, text):
939 939 """compare text with disk content
940 940
941 941 returns True if text is different than what is on disk.
942 942 """
943 943 return self._repo.wread(self._path) != text
944 944
945 945 class memctx(object):
946 946 """Use memctx to perform in-memory commits via localrepo.commitctx().
947 947
948 948 Revision information is supplied at initialization time while
949 949 related files data and is made available through a callback
950 950 mechanism. 'repo' is the current localrepo, 'parents' is a
951 951 sequence of two parent revisions identifiers (pass None for every
952 952 missing parent), 'text' is the commit message and 'files' lists
953 953 names of files touched by the revision (normalized and relative to
954 954 repository root).
955 955
956 956 filectxfn(repo, memctx, path) is a callable receiving the
957 957 repository, the current memctx object and the normalized path of
958 958 requested file, relative to repository root. It is fired by the
959 959 commit function for every file in 'files', but calls order is
960 960 undefined. If the file is available in the revision being
961 961 committed (updated or added), filectxfn returns a memfilectx
962 962 object. If the file was removed, filectxfn raises an
963 963 IOError. Moved files are represented by marking the source file
964 964 removed and the new file added with copy information (see
965 965 memfilectx).
966 966
967 967 user receives the committer name and defaults to current
968 968 repository username, date is the commit date in any format
969 969 supported by util.parsedate() and defaults to current date, extra
970 970 is a dictionary of metadata or is left empty.
971 971 """
972 972 def __init__(self, repo, parents, text, files, filectxfn, user=None,
973 973 date=None, extra=None):
974 974 self._repo = repo
975 975 self._rev = None
976 976 self._node = None
977 977 self._text = text
978 978 self._date = date and util.parsedate(date) or util.makedate()
979 979 self._user = user
980 980 parents = [(p or nullid) for p in parents]
981 981 p1, p2 = parents
982 982 self._parents = [changectx(self._repo, p) for p in (p1, p2)]
983 983 files = sorted(set(files))
984 984 self._status = [files, [], [], [], []]
985 985 self._filectxfn = filectxfn
986 986
987 987 self._extra = extra and extra.copy() or {}
988 988 if 'branch' not in self._extra:
989 989 self._extra['branch'] = 'default'
990 990 elif self._extra.get('branch') == '':
991 991 self._extra['branch'] = 'default'
992 992
993 993 def __str__(self):
994 994 return str(self._parents[0]) + "+"
995 995
996 996 def __int__(self):
997 997 return self._rev
998 998
999 999 def __nonzero__(self):
1000 1000 return True
1001 1001
1002 1002 def __getitem__(self, key):
1003 1003 return self.filectx(key)
1004 1004
1005 1005 def p1(self):
1006 1006 return self._parents[0]
1007 1007 def p2(self):
1008 1008 return self._parents[1]
1009 1009
1010 1010 def user(self):
1011 1011 return self._user or self._repo.ui.username()
1012 1012 def date(self):
1013 1013 return self._date
1014 1014 def description(self):
1015 1015 return self._text
1016 1016 def files(self):
1017 1017 return self.modified()
1018 1018 def modified(self):
1019 1019 return self._status[0]
1020 1020 def added(self):
1021 1021 return self._status[1]
1022 1022 def removed(self):
1023 1023 return self._status[2]
1024 1024 def deleted(self):
1025 1025 return self._status[3]
1026 1026 def unknown(self):
1027 1027 return self._status[4]
1028 1028 def ignored(self):
1029 1029 return self._status[5]
1030 1030 def clean(self):
1031 1031 return self._status[6]
1032 1032 def branch(self):
1033 1033 return self._extra['branch']
1034 1034 def extra(self):
1035 1035 return self._extra
1036 1036 def flags(self, f):
1037 1037 return self[f].flags()
1038 1038
1039 1039 def parents(self):
1040 1040 """return contexts for each parent changeset"""
1041 1041 return self._parents
1042 1042
1043 1043 def filectx(self, path, filelog=None):
1044 1044 """get a file context from the working directory"""
1045 1045 return self._filectxfn(self._repo, self, path)
1046 1046
1047 1047 def commit(self):
1048 1048 """commit context to the repo"""
1049 1049 return self._repo.commitctx(self)
1050 1050
1051 1051 class memfilectx(object):
1052 1052 """memfilectx represents an in-memory file to commit.
1053 1053
1054 1054 See memctx for more details.
1055 1055 """
1056 1056 def __init__(self, path, data, islink=False, isexec=False, copied=None):
1057 1057 """
1058 1058 path is the normalized file path relative to repository root.
1059 1059 data is the file content as a string.
1060 1060 islink is True if the file is a symbolic link.
1061 1061 isexec is True if the file is executable.
1062 1062 copied is the source file path if current file was copied in the
1063 1063 revision being committed, or None."""
1064 1064 self._path = path
1065 1065 self._data = data
1066 1066 self._flags = (islink and 'l' or '') + (isexec and 'x' or '')
1067 1067 self._copied = None
1068 1068 if copied:
1069 1069 self._copied = (copied, nullid)
1070 1070
1071 1071 def __nonzero__(self):
1072 1072 return True
1073 1073 def __str__(self):
1074 1074 return "%s@%s" % (self.path(), self._changectx)
1075 1075 def path(self):
1076 1076 return self._path
1077 1077 def data(self):
1078 1078 return self._data
1079 1079 def flags(self):
1080 1080 return self._flags
1081 1081 def isexec(self):
1082 1082 return 'x' in self._flags
1083 1083 def islink(self):
1084 1084 return 'l' in self._flags
1085 1085 def renamed(self):
1086 1086 return self._copied
@@ -1,39 +1,53 b''
1 1 #!/bin/sh
2 2
3 3 echo "[extensions]" >> $HGRCPATH
4 4 echo "mq=" >> $HGRCPATH
5 5
6 6 hg init a
7 7 cd a
8 8
9 9 echo 'base' > base
10 10 hg ci -Ambase -d '1 0'
11 11
12 12 hg qnew -mmqbase mqbase
13 13 hg qrename mqbase renamed
14 14 mkdir .hg/patches/foo
15 15 hg qrename renamed foo
16 16 hg qseries
17 17 ls .hg/patches/foo
18 18 mkdir .hg/patches/bar
19 19 hg qrename foo/renamed bar
20 20 hg qseries
21 21 ls .hg/patches/bar
22 22 hg qrename bar/renamed baz
23 23 hg qseries
24 24 ls .hg/patches/baz
25 25 hg qrename baz new/dir
26 26 hg qseries
27 27 ls .hg/patches/new/dir
28 28 cd ..
29 29
30 30 echo % test patch being renamed before committed
31 31 hg init b
32 32 cd b
33 33 hg qinit -c
34 34 hg qnew x
35 35 hg qrename y
36 36 hg qcommit -m rename
37 37 cd ..
38 38
39
39 echo '% test overlapping renames (issue2388)'
40 hg init c
41 cd c
42 hg qinit -c
43 echo a > a
44 hg add
45 hg qnew patcha
46 echo b > b
47 hg add
48 hg qnew patchb
49 hg ci --mq -m c1
50 hg qrename patchb patchc
51 hg qrename patcha patchb
52 hg st --mq
53 cd .. No newline at end of file
@@ -1,10 +1,17 b''
1 1 adding base
2 2 foo/renamed
3 3 renamed
4 4 bar/renamed
5 5 renamed
6 6 baz
7 7 .hg/patches/baz
8 8 new/dir
9 9 .hg/patches/new/dir
10 10 % test patch being renamed before committed
11 % test overlapping renames (issue2388)
12 adding a
13 adding b
14 M patchb
15 M series
16 A patchc
17 R patcha
General Comments 0
You need to be logged in to leave comments. Login now