##// END OF EJS Templates
filectx: use cmp(self, fctx) instead of cmp(self, text)...
Nicolas Dumazet -
r11702:eb07fbc2 default
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 ' + str(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 def cmp(self, text):
356 """compare text with stored file revision
355 def cmp(self, fctx):
356 """compare with other file context
357 357
358 returns True if text is different than what is stored.
358 returns True if different than fctx.
359 359 """
360 return self._filelog.cmp(self._filenode, text)
360 return self._filelog.cmp(self._filenode, fctx.data())
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.exists(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 846 fctx = f in pctxs[0] and pctxs[0] or pctxs[1]
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.exists(p) or os.path.islink(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 def cmp(self, text):
939 """compare text with disk content
938 def cmp(self, fctx):
939 """compare with other file context
940 940
941 returns True if text is different than what is on disk.
941 returns True if different than fctx.
942 942 """
943 return self._repo.wread(self._path) != text
943 return self._repo.wread(self._path) != fctx.data()
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,259 +1,259 b''
1 1 # filemerge.py - file-level merge handling for Mercurial
2 2 #
3 3 # Copyright 2006, 2007, 2008 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 short
9 9 from i18n import _
10 10 import util, simplemerge, match, error
11 11 import os, tempfile, re, filecmp
12 12
13 13 def _toolstr(ui, tool, part, default=""):
14 14 return ui.config("merge-tools", tool + "." + part, default)
15 15
16 16 def _toolbool(ui, tool, part, default=False):
17 17 return ui.configbool("merge-tools", tool + "." + part, default)
18 18
19 19 def _toollist(ui, tool, part, default=[]):
20 20 return ui.configlist("merge-tools", tool + "." + part, default)
21 21
22 22 _internal = ['internal:' + s
23 23 for s in 'fail local other merge prompt dump'.split()]
24 24
25 25 def _findtool(ui, tool):
26 26 if tool in _internal:
27 27 return tool
28 28 k = _toolstr(ui, tool, "regkey")
29 29 if k:
30 30 p = util.lookup_reg(k, _toolstr(ui, tool, "regname"))
31 31 if p:
32 32 p = util.find_exe(p + _toolstr(ui, tool, "regappend"))
33 33 if p:
34 34 return p
35 35 return util.find_exe(_toolstr(ui, tool, "executable", tool))
36 36
37 37 def _picktool(repo, ui, path, binary, symlink):
38 38 def check(tool, pat, symlink, binary):
39 39 tmsg = tool
40 40 if pat:
41 41 tmsg += " specified for " + pat
42 42 if not _findtool(ui, tool):
43 43 if pat: # explicitly requested tool deserves a warning
44 44 ui.warn(_("couldn't find merge tool %s\n") % tmsg)
45 45 else: # configured but non-existing tools are more silent
46 46 ui.note(_("couldn't find merge tool %s\n") % tmsg)
47 47 elif symlink and not _toolbool(ui, tool, "symlink"):
48 48 ui.warn(_("tool %s can't handle symlinks\n") % tmsg)
49 49 elif binary and not _toolbool(ui, tool, "binary"):
50 50 ui.warn(_("tool %s can't handle binary\n") % tmsg)
51 51 elif not util.gui() and _toolbool(ui, tool, "gui"):
52 52 ui.warn(_("tool %s requires a GUI\n") % tmsg)
53 53 else:
54 54 return True
55 55 return False
56 56
57 57 # HGMERGE takes precedence
58 58 hgmerge = os.environ.get("HGMERGE")
59 59 if hgmerge:
60 60 return (hgmerge, hgmerge)
61 61
62 62 # then patterns
63 63 for pat, tool in ui.configitems("merge-patterns"):
64 64 mf = match.match(repo.root, '', [pat])
65 65 if mf(path) and check(tool, pat, symlink, False):
66 66 toolpath = _findtool(ui, tool)
67 67 return (tool, '"' + toolpath + '"')
68 68
69 69 # then merge tools
70 70 tools = {}
71 71 for k, v in ui.configitems("merge-tools"):
72 72 t = k.split('.')[0]
73 73 if t not in tools:
74 74 tools[t] = int(_toolstr(ui, t, "priority", "0"))
75 75 names = tools.keys()
76 76 tools = sorted([(-p, t) for t, p in tools.items()])
77 77 uimerge = ui.config("ui", "merge")
78 78 if uimerge:
79 79 if uimerge not in names:
80 80 return (uimerge, uimerge)
81 81 tools.insert(0, (None, uimerge)) # highest priority
82 82 tools.append((None, "hgmerge")) # the old default, if found
83 83 for p, t in tools:
84 84 if check(t, None, symlink, binary):
85 85 toolpath = _findtool(ui, t)
86 86 return (t, '"' + toolpath + '"')
87 87 # internal merge as last resort
88 88 return (not (symlink or binary) and "internal:merge" or None, None)
89 89
90 90 def _eoltype(data):
91 91 "Guess the EOL type of a file"
92 92 if '\0' in data: # binary
93 93 return None
94 94 if '\r\n' in data: # Windows
95 95 return '\r\n'
96 96 if '\r' in data: # Old Mac
97 97 return '\r'
98 98 if '\n' in data: # UNIX
99 99 return '\n'
100 100 return None # unknown
101 101
102 102 def _matcheol(file, origfile):
103 103 "Convert EOL markers in a file to match origfile"
104 104 tostyle = _eoltype(open(origfile, "rb").read())
105 105 if tostyle:
106 106 data = open(file, "rb").read()
107 107 style = _eoltype(data)
108 108 if style:
109 109 newdata = data.replace(style, tostyle)
110 110 if newdata != data:
111 111 open(file, "wb").write(newdata)
112 112
113 113 def filemerge(repo, mynode, orig, fcd, fco, fca):
114 114 """perform a 3-way merge in the working directory
115 115
116 116 mynode = parent node before merge
117 117 orig = original local filename before merge
118 118 fco = other file context
119 119 fca = ancestor file context
120 120 fcd = local file context for current/destination file
121 121 """
122 122
123 123 def temp(prefix, ctx):
124 124 pre = "%s~%s." % (os.path.basename(ctx.path()), prefix)
125 125 (fd, name) = tempfile.mkstemp(prefix=pre)
126 126 data = repo.wwritedata(ctx.path(), ctx.data())
127 127 f = os.fdopen(fd, "wb")
128 128 f.write(data)
129 129 f.close()
130 130 return name
131 131
132 132 def isbin(ctx):
133 133 try:
134 134 return util.binary(ctx.data())
135 135 except IOError:
136 136 return False
137 137
138 if not fco.cmp(fcd.data()): # files identical?
138 if not fco.cmp(fcd): # files identical?
139 139 return None
140 140
141 141 if fca == fco: # backwards, use working dir parent as ancestor
142 142 fca = fcd.parents()[0]
143 143
144 144 ui = repo.ui
145 145 fd = fcd.path()
146 146 binary = isbin(fcd) or isbin(fco) or isbin(fca)
147 147 symlink = 'l' in fcd.flags() + fco.flags()
148 148 tool, toolpath = _picktool(repo, ui, fd, binary, symlink)
149 149 ui.debug("picked tool '%s' for %s (binary %s symlink %s)\n" %
150 150 (tool, fd, binary, symlink))
151 151
152 152 if not tool or tool == 'internal:prompt':
153 153 tool = "internal:local"
154 154 if ui.promptchoice(_(" no tool found to merge %s\n"
155 155 "keep (l)ocal or take (o)ther?") % fd,
156 156 (_("&Local"), _("&Other")), 0):
157 157 tool = "internal:other"
158 158 if tool == "internal:local":
159 159 return 0
160 160 if tool == "internal:other":
161 161 repo.wwrite(fd, fco.data(), fco.flags())
162 162 return 0
163 163 if tool == "internal:fail":
164 164 return 1
165 165
166 166 # do the actual merge
167 167 a = repo.wjoin(fd)
168 168 b = temp("base", fca)
169 169 c = temp("other", fco)
170 170 out = ""
171 171 back = a + ".orig"
172 172 util.copyfile(a, back)
173 173
174 174 if orig != fco.path():
175 175 ui.status(_("merging %s and %s to %s\n") % (orig, fco.path(), fd))
176 176 else:
177 177 ui.status(_("merging %s\n") % fd)
178 178
179 179 ui.debug("my %s other %s ancestor %s\n" % (fcd, fco, fca))
180 180
181 181 # do we attempt to simplemerge first?
182 182 try:
183 183 premerge = _toolbool(ui, tool, "premerge", not (binary or symlink))
184 184 except error.ConfigError:
185 185 premerge = _toolstr(ui, tool, "premerge").lower()
186 186 valid = 'keep'.split()
187 187 if premerge not in valid:
188 188 _valid = ', '.join(["'" + v + "'" for v in valid])
189 189 raise error.ConfigError(_("%s.premerge not valid "
190 190 "('%s' is neither boolean nor %s)") %
191 191 (tool, premerge, _valid))
192 192
193 193 if premerge:
194 194 r = simplemerge.simplemerge(ui, a, b, c, quiet=True)
195 195 if not r:
196 196 ui.debug(" premerge successful\n")
197 197 os.unlink(back)
198 198 os.unlink(b)
199 199 os.unlink(c)
200 200 return 0
201 201 if premerge != 'keep':
202 202 util.copyfile(back, a) # restore from backup and try again
203 203
204 204 env = dict(HG_FILE=fd,
205 205 HG_MY_NODE=short(mynode),
206 206 HG_OTHER_NODE=str(fco.changectx()),
207 207 HG_BASE_NODE=str(fca.changectx()),
208 208 HG_MY_ISLINK='l' in fcd.flags(),
209 209 HG_OTHER_ISLINK='l' in fco.flags(),
210 210 HG_BASE_ISLINK='l' in fca.flags())
211 211
212 212 if tool == "internal:merge":
213 213 r = simplemerge.simplemerge(ui, a, b, c, label=['local', 'other'])
214 214 elif tool == 'internal:dump':
215 215 a = repo.wjoin(fd)
216 216 util.copyfile(a, a + ".local")
217 217 repo.wwrite(fd + ".other", fco.data(), fco.flags())
218 218 repo.wwrite(fd + ".base", fca.data(), fca.flags())
219 219 return 1 # unresolved
220 220 else:
221 221 args = _toolstr(ui, tool, "args", '$local $base $other')
222 222 if "$output" in args:
223 223 out, a = a, back # read input from backup, write to original
224 224 replace = dict(local=a, base=b, other=c, output=out)
225 225 args = re.sub("\$(local|base|other|output)",
226 226 lambda x: '"%s"' % util.localpath(replace[x.group()[1:]]), args)
227 227 r = util.system(toolpath + ' ' + args, cwd=repo.root, environ=env)
228 228
229 229 if not r and (_toolbool(ui, tool, "checkconflicts") or
230 230 'conflicts' in _toollist(ui, tool, "check")):
231 231 if re.match("^(<<<<<<< .*|=======|>>>>>>> .*)$", fcd.data()):
232 232 r = 1
233 233
234 234 checked = False
235 235 if 'prompt' in _toollist(ui, tool, "check"):
236 236 checked = True
237 237 if ui.promptchoice(_("was merge of '%s' successful (yn)?") % fd,
238 238 (_("&Yes"), _("&No")), 1):
239 239 r = 1
240 240
241 241 if not r and not checked and (_toolbool(ui, tool, "checkchanged") or
242 242 'changed' in _toollist(ui, tool, "check")):
243 243 if filecmp.cmp(repo.wjoin(fd), back):
244 244 if ui.promptchoice(_(" output file %s appears unchanged\n"
245 245 "was merge successful (yn)?") % fd,
246 246 (_("&Yes"), _("&No")), 1):
247 247 r = 1
248 248
249 249 if _toolbool(ui, tool, "fixeol"):
250 250 _matcheol(repo.wjoin(fd), back)
251 251
252 252 if r:
253 253 ui.warn(_("merging %s failed!\n") % fd)
254 254 else:
255 255 os.unlink(back)
256 256
257 257 os.unlink(b)
258 258 os.unlink(c)
259 259 return r
@@ -1,1802 +1,1802 b''
1 1 # localrepo.py - read/write repository class for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from node import bin, hex, nullid, nullrev, short
9 9 from i18n import _
10 10 import repo, changegroup, subrepo, discovery, pushkey
11 11 import changelog, dirstate, filelog, manifest, context
12 12 import lock, transaction, store, encoding
13 13 import util, extensions, hook, error
14 14 import match as matchmod
15 15 import merge as mergemod
16 16 import tags as tagsmod
17 17 import url as urlmod
18 18 from lock import release
19 19 import weakref, errno, os, time, inspect
20 20 propertycache = util.propertycache
21 21
22 22 class localrepository(repo.repository):
23 23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
24 24 supported = set('revlogv1 store fncache shared'.split())
25 25
26 26 def __init__(self, baseui, path=None, create=0):
27 27 repo.repository.__init__(self)
28 28 self.root = os.path.realpath(util.expandpath(path))
29 29 self.path = os.path.join(self.root, ".hg")
30 30 self.origroot = path
31 31 self.opener = util.opener(self.path)
32 32 self.wopener = util.opener(self.root)
33 33 self.baseui = baseui
34 34 self.ui = baseui.copy()
35 35
36 36 try:
37 37 self.ui.readconfig(self.join("hgrc"), self.root)
38 38 extensions.loadall(self.ui)
39 39 except IOError:
40 40 pass
41 41
42 42 if not os.path.isdir(self.path):
43 43 if create:
44 44 if not os.path.exists(path):
45 45 util.makedirs(path)
46 46 os.mkdir(self.path)
47 47 requirements = ["revlogv1"]
48 48 if self.ui.configbool('format', 'usestore', True):
49 49 os.mkdir(os.path.join(self.path, "store"))
50 50 requirements.append("store")
51 51 if self.ui.configbool('format', 'usefncache', True):
52 52 requirements.append("fncache")
53 53 # create an invalid changelog
54 54 self.opener("00changelog.i", "a").write(
55 55 '\0\0\0\2' # represents revlogv2
56 56 ' dummy changelog to prevent using the old repo layout'
57 57 )
58 58 reqfile = self.opener("requires", "w")
59 59 for r in requirements:
60 60 reqfile.write("%s\n" % r)
61 61 reqfile.close()
62 62 else:
63 63 raise error.RepoError(_("repository %s not found") % path)
64 64 elif create:
65 65 raise error.RepoError(_("repository %s already exists") % path)
66 66 else:
67 67 # find requirements
68 68 requirements = set()
69 69 try:
70 70 requirements = set(self.opener("requires").read().splitlines())
71 71 except IOError, inst:
72 72 if inst.errno != errno.ENOENT:
73 73 raise
74 74 for r in requirements - self.supported:
75 75 raise error.RepoError(_("requirement '%s' not supported") % r)
76 76
77 77 self.sharedpath = self.path
78 78 try:
79 79 s = os.path.realpath(self.opener("sharedpath").read())
80 80 if not os.path.exists(s):
81 81 raise error.RepoError(
82 82 _('.hg/sharedpath points to nonexistent directory %s') % s)
83 83 self.sharedpath = s
84 84 except IOError, inst:
85 85 if inst.errno != errno.ENOENT:
86 86 raise
87 87
88 88 self.store = store.store(requirements, self.sharedpath, util.opener)
89 89 self.spath = self.store.path
90 90 self.sopener = self.store.opener
91 91 self.sjoin = self.store.join
92 92 self.opener.createmode = self.store.createmode
93 93 self.sopener.options = {}
94 94
95 95 # These two define the set of tags for this repository. _tags
96 96 # maps tag name to node; _tagtypes maps tag name to 'global' or
97 97 # 'local'. (Global tags are defined by .hgtags across all
98 98 # heads, and local tags are defined in .hg/localtags.) They
99 99 # constitute the in-memory cache of tags.
100 100 self._tags = None
101 101 self._tagtypes = None
102 102
103 103 self._branchcache = None # in UTF-8
104 104 self._branchcachetip = None
105 105 self.nodetagscache = None
106 106 self.filterpats = {}
107 107 self._datafilters = {}
108 108 self._transref = self._lockref = self._wlockref = None
109 109
110 110 @propertycache
111 111 def changelog(self):
112 112 c = changelog.changelog(self.sopener)
113 113 if 'HG_PENDING' in os.environ:
114 114 p = os.environ['HG_PENDING']
115 115 if p.startswith(self.root):
116 116 c.readpending('00changelog.i.a')
117 117 self.sopener.options['defversion'] = c.version
118 118 return c
119 119
120 120 @propertycache
121 121 def manifest(self):
122 122 return manifest.manifest(self.sopener)
123 123
124 124 @propertycache
125 125 def dirstate(self):
126 126 return dirstate.dirstate(self.opener, self.ui, self.root)
127 127
128 128 def __getitem__(self, changeid):
129 129 if changeid is None:
130 130 return context.workingctx(self)
131 131 return context.changectx(self, changeid)
132 132
133 133 def __contains__(self, changeid):
134 134 try:
135 135 return bool(self.lookup(changeid))
136 136 except error.RepoLookupError:
137 137 return False
138 138
139 139 def __nonzero__(self):
140 140 return True
141 141
142 142 def __len__(self):
143 143 return len(self.changelog)
144 144
145 145 def __iter__(self):
146 146 for i in xrange(len(self)):
147 147 yield i
148 148
149 149 def url(self):
150 150 return 'file:' + self.root
151 151
152 152 def hook(self, name, throw=False, **args):
153 153 return hook.hook(self.ui, self, name, throw, **args)
154 154
155 155 tag_disallowed = ':\r\n'
156 156
157 157 def _tag(self, names, node, message, local, user, date, extra={}):
158 158 if isinstance(names, str):
159 159 allchars = names
160 160 names = (names,)
161 161 else:
162 162 allchars = ''.join(names)
163 163 for c in self.tag_disallowed:
164 164 if c in allchars:
165 165 raise util.Abort(_('%r cannot be used in a tag name') % c)
166 166
167 167 branches = self.branchmap()
168 168 for name in names:
169 169 self.hook('pretag', throw=True, node=hex(node), tag=name,
170 170 local=local)
171 171 if name in branches:
172 172 self.ui.warn(_("warning: tag %s conflicts with existing"
173 173 " branch name\n") % name)
174 174
175 175 def writetags(fp, names, munge, prevtags):
176 176 fp.seek(0, 2)
177 177 if prevtags and prevtags[-1] != '\n':
178 178 fp.write('\n')
179 179 for name in names:
180 180 m = munge and munge(name) or name
181 181 if self._tagtypes and name in self._tagtypes:
182 182 old = self._tags.get(name, nullid)
183 183 fp.write('%s %s\n' % (hex(old), m))
184 184 fp.write('%s %s\n' % (hex(node), m))
185 185 fp.close()
186 186
187 187 prevtags = ''
188 188 if local:
189 189 try:
190 190 fp = self.opener('localtags', 'r+')
191 191 except IOError:
192 192 fp = self.opener('localtags', 'a')
193 193 else:
194 194 prevtags = fp.read()
195 195
196 196 # local tags are stored in the current charset
197 197 writetags(fp, names, None, prevtags)
198 198 for name in names:
199 199 self.hook('tag', node=hex(node), tag=name, local=local)
200 200 return
201 201
202 202 try:
203 203 fp = self.wfile('.hgtags', 'rb+')
204 204 except IOError:
205 205 fp = self.wfile('.hgtags', 'ab')
206 206 else:
207 207 prevtags = fp.read()
208 208
209 209 # committed tags are stored in UTF-8
210 210 writetags(fp, names, encoding.fromlocal, prevtags)
211 211
212 212 if '.hgtags' not in self.dirstate:
213 213 self[None].add(['.hgtags'])
214 214
215 215 m = matchmod.exact(self.root, '', ['.hgtags'])
216 216 tagnode = self.commit(message, user, date, extra=extra, match=m)
217 217
218 218 for name in names:
219 219 self.hook('tag', node=hex(node), tag=name, local=local)
220 220
221 221 return tagnode
222 222
223 223 def tag(self, names, node, message, local, user, date):
224 224 '''tag a revision with one or more symbolic names.
225 225
226 226 names is a list of strings or, when adding a single tag, names may be a
227 227 string.
228 228
229 229 if local is True, the tags are stored in a per-repository file.
230 230 otherwise, they are stored in the .hgtags file, and a new
231 231 changeset is committed with the change.
232 232
233 233 keyword arguments:
234 234
235 235 local: whether to store tags in non-version-controlled file
236 236 (default False)
237 237
238 238 message: commit message to use if committing
239 239
240 240 user: name of user to use if committing
241 241
242 242 date: date tuple to use if committing'''
243 243
244 244 for x in self.status()[:5]:
245 245 if '.hgtags' in x:
246 246 raise util.Abort(_('working copy of .hgtags is changed '
247 247 '(please commit .hgtags manually)'))
248 248
249 249 self.tags() # instantiate the cache
250 250 self._tag(names, node, message, local, user, date)
251 251
252 252 def tags(self):
253 253 '''return a mapping of tag to node'''
254 254 if self._tags is None:
255 255 (self._tags, self._tagtypes) = self._findtags()
256 256
257 257 return self._tags
258 258
259 259 def _findtags(self):
260 260 '''Do the hard work of finding tags. Return a pair of dicts
261 261 (tags, tagtypes) where tags maps tag name to node, and tagtypes
262 262 maps tag name to a string like \'global\' or \'local\'.
263 263 Subclasses or extensions are free to add their own tags, but
264 264 should be aware that the returned dicts will be retained for the
265 265 duration of the localrepo object.'''
266 266
267 267 # XXX what tagtype should subclasses/extensions use? Currently
268 268 # mq and bookmarks add tags, but do not set the tagtype at all.
269 269 # Should each extension invent its own tag type? Should there
270 270 # be one tagtype for all such "virtual" tags? Or is the status
271 271 # quo fine?
272 272
273 273 alltags = {} # map tag name to (node, hist)
274 274 tagtypes = {}
275 275
276 276 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
277 277 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
278 278
279 279 # Build the return dicts. Have to re-encode tag names because
280 280 # the tags module always uses UTF-8 (in order not to lose info
281 281 # writing to the cache), but the rest of Mercurial wants them in
282 282 # local encoding.
283 283 tags = {}
284 284 for (name, (node, hist)) in alltags.iteritems():
285 285 if node != nullid:
286 286 tags[encoding.tolocal(name)] = node
287 287 tags['tip'] = self.changelog.tip()
288 288 tagtypes = dict([(encoding.tolocal(name), value)
289 289 for (name, value) in tagtypes.iteritems()])
290 290 return (tags, tagtypes)
291 291
292 292 def tagtype(self, tagname):
293 293 '''
294 294 return the type of the given tag. result can be:
295 295
296 296 'local' : a local tag
297 297 'global' : a global tag
298 298 None : tag does not exist
299 299 '''
300 300
301 301 self.tags()
302 302
303 303 return self._tagtypes.get(tagname)
304 304
305 305 def tagslist(self):
306 306 '''return a list of tags ordered by revision'''
307 307 l = []
308 308 for t, n in self.tags().iteritems():
309 309 try:
310 310 r = self.changelog.rev(n)
311 311 except:
312 312 r = -2 # sort to the beginning of the list if unknown
313 313 l.append((r, t, n))
314 314 return [(t, n) for r, t, n in sorted(l)]
315 315
316 316 def nodetags(self, node):
317 317 '''return the tags associated with a node'''
318 318 if not self.nodetagscache:
319 319 self.nodetagscache = {}
320 320 for t, n in self.tags().iteritems():
321 321 self.nodetagscache.setdefault(n, []).append(t)
322 322 for tags in self.nodetagscache.itervalues():
323 323 tags.sort()
324 324 return self.nodetagscache.get(node, [])
325 325
326 326 def _branchtags(self, partial, lrev):
327 327 # TODO: rename this function?
328 328 tiprev = len(self) - 1
329 329 if lrev != tiprev:
330 330 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
331 331 self._updatebranchcache(partial, ctxgen)
332 332 self._writebranchcache(partial, self.changelog.tip(), tiprev)
333 333
334 334 return partial
335 335
336 336 def branchmap(self):
337 337 '''returns a dictionary {branch: [branchheads]}'''
338 338 tip = self.changelog.tip()
339 339 if self._branchcache is not None and self._branchcachetip == tip:
340 340 return self._branchcache
341 341
342 342 oldtip = self._branchcachetip
343 343 self._branchcachetip = tip
344 344 if oldtip is None or oldtip not in self.changelog.nodemap:
345 345 partial, last, lrev = self._readbranchcache()
346 346 else:
347 347 lrev = self.changelog.rev(oldtip)
348 348 partial = self._branchcache
349 349
350 350 self._branchtags(partial, lrev)
351 351 # this private cache holds all heads (not just tips)
352 352 self._branchcache = partial
353 353
354 354 return self._branchcache
355 355
356 356 def branchtags(self):
357 357 '''return a dict where branch names map to the tipmost head of
358 358 the branch, open heads come before closed'''
359 359 bt = {}
360 360 for bn, heads in self.branchmap().iteritems():
361 361 tip = heads[-1]
362 362 for h in reversed(heads):
363 363 if 'close' not in self.changelog.read(h)[5]:
364 364 tip = h
365 365 break
366 366 bt[bn] = tip
367 367 return bt
368 368
369 369
370 370 def _readbranchcache(self):
371 371 partial = {}
372 372 try:
373 373 f = self.opener("branchheads.cache")
374 374 lines = f.read().split('\n')
375 375 f.close()
376 376 except (IOError, OSError):
377 377 return {}, nullid, nullrev
378 378
379 379 try:
380 380 last, lrev = lines.pop(0).split(" ", 1)
381 381 last, lrev = bin(last), int(lrev)
382 382 if lrev >= len(self) or self[lrev].node() != last:
383 383 # invalidate the cache
384 384 raise ValueError('invalidating branch cache (tip differs)')
385 385 for l in lines:
386 386 if not l:
387 387 continue
388 388 node, label = l.split(" ", 1)
389 389 partial.setdefault(label.strip(), []).append(bin(node))
390 390 except KeyboardInterrupt:
391 391 raise
392 392 except Exception, inst:
393 393 if self.ui.debugflag:
394 394 self.ui.warn(str(inst), '\n')
395 395 partial, last, lrev = {}, nullid, nullrev
396 396 return partial, last, lrev
397 397
398 398 def _writebranchcache(self, branches, tip, tiprev):
399 399 try:
400 400 f = self.opener("branchheads.cache", "w", atomictemp=True)
401 401 f.write("%s %s\n" % (hex(tip), tiprev))
402 402 for label, nodes in branches.iteritems():
403 403 for node in nodes:
404 404 f.write("%s %s\n" % (hex(node), label))
405 405 f.rename()
406 406 except (IOError, OSError):
407 407 pass
408 408
409 409 def _updatebranchcache(self, partial, ctxgen):
410 410 # collect new branch entries
411 411 newbranches = {}
412 412 for c in ctxgen:
413 413 newbranches.setdefault(c.branch(), []).append(c.node())
414 414 # if older branchheads are reachable from new ones, they aren't
415 415 # really branchheads. Note checking parents is insufficient:
416 416 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
417 417 for branch, newnodes in newbranches.iteritems():
418 418 bheads = partial.setdefault(branch, [])
419 419 bheads.extend(newnodes)
420 420 if len(bheads) <= 1:
421 421 continue
422 422 # starting from tip means fewer passes over reachable
423 423 while newnodes:
424 424 latest = newnodes.pop()
425 425 if latest not in bheads:
426 426 continue
427 427 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
428 428 reachable = self.changelog.reachable(latest, minbhrev)
429 429 reachable.remove(latest)
430 430 bheads = [b for b in bheads if b not in reachable]
431 431 partial[branch] = bheads
432 432
433 433 def lookup(self, key):
434 434 if isinstance(key, int):
435 435 return self.changelog.node(key)
436 436 elif key == '.':
437 437 return self.dirstate.parents()[0]
438 438 elif key == 'null':
439 439 return nullid
440 440 elif key == 'tip':
441 441 return self.changelog.tip()
442 442 n = self.changelog._match(key)
443 443 if n:
444 444 return n
445 445 if key in self.tags():
446 446 return self.tags()[key]
447 447 if key in self.branchtags():
448 448 return self.branchtags()[key]
449 449 n = self.changelog._partialmatch(key)
450 450 if n:
451 451 return n
452 452
453 453 # can't find key, check if it might have come from damaged dirstate
454 454 if key in self.dirstate.parents():
455 455 raise error.Abort(_("working directory has unknown parent '%s'!")
456 456 % short(key))
457 457 try:
458 458 if len(key) == 20:
459 459 key = hex(key)
460 460 except:
461 461 pass
462 462 raise error.RepoLookupError(_("unknown revision '%s'") % key)
463 463
464 464 def lookupbranch(self, key, remote=None):
465 465 repo = remote or self
466 466 if key in repo.branchmap():
467 467 return key
468 468
469 469 repo = (remote and remote.local()) and remote or self
470 470 return repo[key].branch()
471 471
472 472 def local(self):
473 473 return True
474 474
475 475 def join(self, f):
476 476 return os.path.join(self.path, f)
477 477
478 478 def wjoin(self, f):
479 479 return os.path.join(self.root, f)
480 480
481 481 def rjoin(self, f):
482 482 return os.path.join(self.root, util.pconvert(f))
483 483
484 484 def file(self, f):
485 485 if f[0] == '/':
486 486 f = f[1:]
487 487 return filelog.filelog(self.sopener, f)
488 488
489 489 def changectx(self, changeid):
490 490 return self[changeid]
491 491
492 492 def parents(self, changeid=None):
493 493 '''get list of changectxs for parents of changeid'''
494 494 return self[changeid].parents()
495 495
496 496 def filectx(self, path, changeid=None, fileid=None):
497 497 """changeid can be a changeset revision, node, or tag.
498 498 fileid can be a file revision or node."""
499 499 return context.filectx(self, path, changeid, fileid)
500 500
501 501 def getcwd(self):
502 502 return self.dirstate.getcwd()
503 503
504 504 def pathto(self, f, cwd=None):
505 505 return self.dirstate.pathto(f, cwd)
506 506
507 507 def wfile(self, f, mode='r'):
508 508 return self.wopener(f, mode)
509 509
510 510 def _link(self, f):
511 511 return os.path.islink(self.wjoin(f))
512 512
513 513 def _loadfilter(self, filter):
514 514 if filter not in self.filterpats:
515 515 l = []
516 516 for pat, cmd in self.ui.configitems(filter):
517 517 if cmd == '!':
518 518 continue
519 519 mf = matchmod.match(self.root, '', [pat])
520 520 fn = None
521 521 params = cmd
522 522 for name, filterfn in self._datafilters.iteritems():
523 523 if cmd.startswith(name):
524 524 fn = filterfn
525 525 params = cmd[len(name):].lstrip()
526 526 break
527 527 if not fn:
528 528 fn = lambda s, c, **kwargs: util.filter(s, c)
529 529 # Wrap old filters not supporting keyword arguments
530 530 if not inspect.getargspec(fn)[2]:
531 531 oldfn = fn
532 532 fn = lambda s, c, **kwargs: oldfn(s, c)
533 533 l.append((mf, fn, params))
534 534 self.filterpats[filter] = l
535 535
536 536 def _filter(self, filter, filename, data):
537 537 self._loadfilter(filter)
538 538
539 539 for mf, fn, cmd in self.filterpats[filter]:
540 540 if mf(filename):
541 541 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
542 542 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
543 543 break
544 544
545 545 return data
546 546
547 547 def adddatafilter(self, name, filter):
548 548 self._datafilters[name] = filter
549 549
550 550 def wread(self, filename):
551 551 if self._link(filename):
552 552 data = os.readlink(self.wjoin(filename))
553 553 else:
554 554 data = self.wopener(filename, 'r').read()
555 555 return self._filter("encode", filename, data)
556 556
557 557 def wwrite(self, filename, data, flags):
558 558 data = self._filter("decode", filename, data)
559 559 try:
560 560 os.unlink(self.wjoin(filename))
561 561 except OSError:
562 562 pass
563 563 if 'l' in flags:
564 564 self.wopener.symlink(data, filename)
565 565 else:
566 566 self.wopener(filename, 'w').write(data)
567 567 if 'x' in flags:
568 568 util.set_flags(self.wjoin(filename), False, True)
569 569
570 570 def wwritedata(self, filename, data):
571 571 return self._filter("decode", filename, data)
572 572
573 573 def transaction(self, desc):
574 574 tr = self._transref and self._transref() or None
575 575 if tr and tr.running():
576 576 return tr.nest()
577 577
578 578 # abort here if the journal already exists
579 579 if os.path.exists(self.sjoin("journal")):
580 580 raise error.RepoError(
581 581 _("abandoned transaction found - run hg recover"))
582 582
583 583 # save dirstate for rollback
584 584 try:
585 585 ds = self.opener("dirstate").read()
586 586 except IOError:
587 587 ds = ""
588 588 self.opener("journal.dirstate", "w").write(ds)
589 589 self.opener("journal.branch", "w").write(self.dirstate.branch())
590 590 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
591 591
592 592 renames = [(self.sjoin("journal"), self.sjoin("undo")),
593 593 (self.join("journal.dirstate"), self.join("undo.dirstate")),
594 594 (self.join("journal.branch"), self.join("undo.branch")),
595 595 (self.join("journal.desc"), self.join("undo.desc"))]
596 596 tr = transaction.transaction(self.ui.warn, self.sopener,
597 597 self.sjoin("journal"),
598 598 aftertrans(renames),
599 599 self.store.createmode)
600 600 self._transref = weakref.ref(tr)
601 601 return tr
602 602
603 603 def recover(self):
604 604 lock = self.lock()
605 605 try:
606 606 if os.path.exists(self.sjoin("journal")):
607 607 self.ui.status(_("rolling back interrupted transaction\n"))
608 608 transaction.rollback(self.sopener, self.sjoin("journal"),
609 609 self.ui.warn)
610 610 self.invalidate()
611 611 return True
612 612 else:
613 613 self.ui.warn(_("no interrupted transaction available\n"))
614 614 return False
615 615 finally:
616 616 lock.release()
617 617
618 618 def rollback(self, dryrun=False):
619 619 wlock = lock = None
620 620 try:
621 621 wlock = self.wlock()
622 622 lock = self.lock()
623 623 if os.path.exists(self.sjoin("undo")):
624 624 try:
625 625 args = self.opener("undo.desc", "r").read().splitlines()
626 626 if len(args) >= 3 and self.ui.verbose:
627 627 desc = _("rolling back to revision %s"
628 628 " (undo %s: %s)\n") % (
629 629 int(args[0]) - 1, args[1], args[2])
630 630 elif len(args) >= 2:
631 631 desc = _("rolling back to revision %s (undo %s)\n") % (
632 632 int(args[0]) - 1, args[1])
633 633 except IOError:
634 634 desc = _("rolling back unknown transaction\n")
635 635 self.ui.status(desc)
636 636 if dryrun:
637 637 return
638 638 transaction.rollback(self.sopener, self.sjoin("undo"),
639 639 self.ui.warn)
640 640 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
641 641 try:
642 642 branch = self.opener("undo.branch").read()
643 643 self.dirstate.setbranch(branch)
644 644 except IOError:
645 645 self.ui.warn(_("Named branch could not be reset, "
646 646 "current branch still is: %s\n")
647 647 % encoding.tolocal(self.dirstate.branch()))
648 648 self.invalidate()
649 649 self.dirstate.invalidate()
650 650 self.destroyed()
651 651 else:
652 652 self.ui.warn(_("no rollback information available\n"))
653 653 return 1
654 654 finally:
655 655 release(lock, wlock)
656 656
657 657 def invalidatecaches(self):
658 658 self._tags = None
659 659 self._tagtypes = None
660 660 self.nodetagscache = None
661 661 self._branchcache = None # in UTF-8
662 662 self._branchcachetip = None
663 663
664 664 def invalidate(self):
665 665 for a in "changelog manifest".split():
666 666 if a in self.__dict__:
667 667 delattr(self, a)
668 668 self.invalidatecaches()
669 669
670 670 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
671 671 try:
672 672 l = lock.lock(lockname, 0, releasefn, desc=desc)
673 673 except error.LockHeld, inst:
674 674 if not wait:
675 675 raise
676 676 self.ui.warn(_("waiting for lock on %s held by %r\n") %
677 677 (desc, inst.locker))
678 678 # default to 600 seconds timeout
679 679 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
680 680 releasefn, desc=desc)
681 681 if acquirefn:
682 682 acquirefn()
683 683 return l
684 684
685 685 def lock(self, wait=True):
686 686 '''Lock the repository store (.hg/store) and return a weak reference
687 687 to the lock. Use this before modifying the store (e.g. committing or
688 688 stripping). If you are opening a transaction, get a lock as well.)'''
689 689 l = self._lockref and self._lockref()
690 690 if l is not None and l.held:
691 691 l.lock()
692 692 return l
693 693
694 694 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
695 695 _('repository %s') % self.origroot)
696 696 self._lockref = weakref.ref(l)
697 697 return l
698 698
699 699 def wlock(self, wait=True):
700 700 '''Lock the non-store parts of the repository (everything under
701 701 .hg except .hg/store) and return a weak reference to the lock.
702 702 Use this before modifying files in .hg.'''
703 703 l = self._wlockref and self._wlockref()
704 704 if l is not None and l.held:
705 705 l.lock()
706 706 return l
707 707
708 708 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
709 709 self.dirstate.invalidate, _('working directory of %s') %
710 710 self.origroot)
711 711 self._wlockref = weakref.ref(l)
712 712 return l
713 713
714 714 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
715 715 """
716 716 commit an individual file as part of a larger transaction
717 717 """
718 718
719 719 fname = fctx.path()
720 720 text = fctx.data()
721 721 flog = self.file(fname)
722 722 fparent1 = manifest1.get(fname, nullid)
723 723 fparent2 = fparent2o = manifest2.get(fname, nullid)
724 724
725 725 meta = {}
726 726 copy = fctx.renamed()
727 727 if copy and copy[0] != fname:
728 728 # Mark the new revision of this file as a copy of another
729 729 # file. This copy data will effectively act as a parent
730 730 # of this new revision. If this is a merge, the first
731 731 # parent will be the nullid (meaning "look up the copy data")
732 732 # and the second one will be the other parent. For example:
733 733 #
734 734 # 0 --- 1 --- 3 rev1 changes file foo
735 735 # \ / rev2 renames foo to bar and changes it
736 736 # \- 2 -/ rev3 should have bar with all changes and
737 737 # should record that bar descends from
738 738 # bar in rev2 and foo in rev1
739 739 #
740 740 # this allows this merge to succeed:
741 741 #
742 742 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
743 743 # \ / merging rev3 and rev4 should use bar@rev2
744 744 # \- 2 --- 4 as the merge base
745 745 #
746 746
747 747 cfname = copy[0]
748 748 crev = manifest1.get(cfname)
749 749 newfparent = fparent2
750 750
751 751 if manifest2: # branch merge
752 752 if fparent2 == nullid or crev is None: # copied on remote side
753 753 if cfname in manifest2:
754 754 crev = manifest2[cfname]
755 755 newfparent = fparent1
756 756
757 757 # find source in nearest ancestor if we've lost track
758 758 if not crev:
759 759 self.ui.debug(" %s: searching for copy revision for %s\n" %
760 760 (fname, cfname))
761 761 for ancestor in self['.'].ancestors():
762 762 if cfname in ancestor:
763 763 crev = ancestor[cfname].filenode()
764 764 break
765 765
766 766 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
767 767 meta["copy"] = cfname
768 768 meta["copyrev"] = hex(crev)
769 769 fparent1, fparent2 = nullid, newfparent
770 770 elif fparent2 != nullid:
771 771 # is one parent an ancestor of the other?
772 772 fparentancestor = flog.ancestor(fparent1, fparent2)
773 773 if fparentancestor == fparent1:
774 774 fparent1, fparent2 = fparent2, nullid
775 775 elif fparentancestor == fparent2:
776 776 fparent2 = nullid
777 777
778 778 # is the file changed?
779 779 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
780 780 changelist.append(fname)
781 781 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
782 782
783 783 # are just the flags changed during merge?
784 784 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
785 785 changelist.append(fname)
786 786
787 787 return fparent1
788 788
789 789 def commit(self, text="", user=None, date=None, match=None, force=False,
790 790 editor=False, extra={}):
791 791 """Add a new revision to current repository.
792 792
793 793 Revision information is gathered from the working directory,
794 794 match can be used to filter the committed files. If editor is
795 795 supplied, it is called to get a commit message.
796 796 """
797 797
798 798 def fail(f, msg):
799 799 raise util.Abort('%s: %s' % (f, msg))
800 800
801 801 if not match:
802 802 match = matchmod.always(self.root, '')
803 803
804 804 if not force:
805 805 vdirs = []
806 806 match.dir = vdirs.append
807 807 match.bad = fail
808 808
809 809 wlock = self.wlock()
810 810 try:
811 811 wctx = self[None]
812 812 merge = len(wctx.parents()) > 1
813 813
814 814 if (not force and merge and match and
815 815 (match.files() or match.anypats())):
816 816 raise util.Abort(_('cannot partially commit a merge '
817 817 '(do not specify files or patterns)'))
818 818
819 819 changes = self.status(match=match, clean=force)
820 820 if force:
821 821 changes[0].extend(changes[6]) # mq may commit unchanged files
822 822
823 823 # check subrepos
824 824 subs = []
825 825 removedsubs = set()
826 826 for p in wctx.parents():
827 827 removedsubs.update(s for s in p.substate if match(s))
828 828 for s in wctx.substate:
829 829 removedsubs.discard(s)
830 830 if match(s) and wctx.sub(s).dirty():
831 831 subs.append(s)
832 832 if (subs or removedsubs):
833 833 if (not match('.hgsub') and
834 834 '.hgsub' in (wctx.modified() + wctx.added())):
835 835 raise util.Abort(_("can't commit subrepos without .hgsub"))
836 836 if '.hgsubstate' not in changes[0]:
837 837 changes[0].insert(0, '.hgsubstate')
838 838
839 839 # make sure all explicit patterns are matched
840 840 if not force and match.files():
841 841 matched = set(changes[0] + changes[1] + changes[2])
842 842
843 843 for f in match.files():
844 844 if f == '.' or f in matched or f in wctx.substate:
845 845 continue
846 846 if f in changes[3]: # missing
847 847 fail(f, _('file not found!'))
848 848 if f in vdirs: # visited directory
849 849 d = f + '/'
850 850 for mf in matched:
851 851 if mf.startswith(d):
852 852 break
853 853 else:
854 854 fail(f, _("no match under directory!"))
855 855 elif f not in self.dirstate:
856 856 fail(f, _("file not tracked!"))
857 857
858 858 if (not force and not extra.get("close") and not merge
859 859 and not (changes[0] or changes[1] or changes[2])
860 860 and wctx.branch() == wctx.p1().branch()):
861 861 return None
862 862
863 863 ms = mergemod.mergestate(self)
864 864 for f in changes[0]:
865 865 if f in ms and ms[f] == 'u':
866 866 raise util.Abort(_("unresolved merge conflicts "
867 867 "(see hg resolve)"))
868 868
869 869 cctx = context.workingctx(self, text, user, date, extra, changes)
870 870 if editor:
871 871 cctx._text = editor(self, cctx, subs)
872 872 edited = (text != cctx._text)
873 873
874 874 # commit subs
875 875 if subs or removedsubs:
876 876 state = wctx.substate.copy()
877 877 for s in subs:
878 878 sub = wctx.sub(s)
879 879 self.ui.status(_('committing subrepository %s\n') %
880 880 subrepo.relpath(sub))
881 881 sr = sub.commit(cctx._text, user, date)
882 882 state[s] = (state[s][0], sr)
883 883 subrepo.writestate(self, state)
884 884
885 885 # Save commit message in case this transaction gets rolled back
886 886 # (e.g. by a pretxncommit hook). Leave the content alone on
887 887 # the assumption that the user will use the same editor again.
888 888 msgfile = self.opener('last-message.txt', 'wb')
889 889 msgfile.write(cctx._text)
890 890 msgfile.close()
891 891
892 892 p1, p2 = self.dirstate.parents()
893 893 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
894 894 try:
895 895 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
896 896 ret = self.commitctx(cctx, True)
897 897 except:
898 898 if edited:
899 899 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
900 900 self.ui.write(
901 901 _('note: commit message saved in %s\n') % msgfn)
902 902 raise
903 903
904 904 # update dirstate and mergestate
905 905 for f in changes[0] + changes[1]:
906 906 self.dirstate.normal(f)
907 907 for f in changes[2]:
908 908 self.dirstate.forget(f)
909 909 self.dirstate.setparents(ret)
910 910 ms.reset()
911 911 finally:
912 912 wlock.release()
913 913
914 914 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
915 915 return ret
916 916
917 917 def commitctx(self, ctx, error=False):
918 918 """Add a new revision to current repository.
919 919 Revision information is passed via the context argument.
920 920 """
921 921
922 922 tr = lock = None
923 923 removed = ctx.removed()
924 924 p1, p2 = ctx.p1(), ctx.p2()
925 925 m1 = p1.manifest().copy()
926 926 m2 = p2.manifest()
927 927 user = ctx.user()
928 928
929 929 lock = self.lock()
930 930 try:
931 931 tr = self.transaction("commit")
932 932 trp = weakref.proxy(tr)
933 933
934 934 # check in files
935 935 new = {}
936 936 changed = []
937 937 linkrev = len(self)
938 938 for f in sorted(ctx.modified() + ctx.added()):
939 939 self.ui.note(f + "\n")
940 940 try:
941 941 fctx = ctx[f]
942 942 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
943 943 changed)
944 944 m1.set(f, fctx.flags())
945 945 except OSError, inst:
946 946 self.ui.warn(_("trouble committing %s!\n") % f)
947 947 raise
948 948 except IOError, inst:
949 949 errcode = getattr(inst, 'errno', errno.ENOENT)
950 950 if error or errcode and errcode != errno.ENOENT:
951 951 self.ui.warn(_("trouble committing %s!\n") % f)
952 952 raise
953 953 else:
954 954 removed.append(f)
955 955
956 956 # update manifest
957 957 m1.update(new)
958 958 removed = [f for f in sorted(removed) if f in m1 or f in m2]
959 959 drop = [f for f in removed if f in m1]
960 960 for f in drop:
961 961 del m1[f]
962 962 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
963 963 p2.manifestnode(), (new, drop))
964 964
965 965 # update changelog
966 966 self.changelog.delayupdate()
967 967 n = self.changelog.add(mn, changed + removed, ctx.description(),
968 968 trp, p1.node(), p2.node(),
969 969 user, ctx.date(), ctx.extra().copy())
970 970 p = lambda: self.changelog.writepending() and self.root or ""
971 971 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
972 972 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
973 973 parent2=xp2, pending=p)
974 974 self.changelog.finalize(trp)
975 975 tr.close()
976 976
977 977 if self._branchcache:
978 978 self.branchtags()
979 979 return n
980 980 finally:
981 981 if tr:
982 982 tr.release()
983 983 lock.release()
984 984
985 985 def destroyed(self):
986 986 '''Inform the repository that nodes have been destroyed.
987 987 Intended for use by strip and rollback, so there's a common
988 988 place for anything that has to be done after destroying history.'''
989 989 # XXX it might be nice if we could take the list of destroyed
990 990 # nodes, but I don't see an easy way for rollback() to do that
991 991
992 992 # Ensure the persistent tag cache is updated. Doing it now
993 993 # means that the tag cache only has to worry about destroyed
994 994 # heads immediately after a strip/rollback. That in turn
995 995 # guarantees that "cachetip == currenttip" (comparing both rev
996 996 # and node) always means no nodes have been added or destroyed.
997 997
998 998 # XXX this is suboptimal when qrefresh'ing: we strip the current
999 999 # head, refresh the tag cache, then immediately add a new head.
1000 1000 # But I think doing it this way is necessary for the "instant
1001 1001 # tag cache retrieval" case to work.
1002 1002 self.invalidatecaches()
1003 1003
1004 1004 def walk(self, match, node=None):
1005 1005 '''
1006 1006 walk recursively through the directory tree or a given
1007 1007 changeset, finding all files matched by the match
1008 1008 function
1009 1009 '''
1010 1010 return self[node].walk(match)
1011 1011
1012 1012 def status(self, node1='.', node2=None, match=None,
1013 1013 ignored=False, clean=False, unknown=False):
1014 1014 """return status of files between two nodes or node and working directory
1015 1015
1016 1016 If node1 is None, use the first dirstate parent instead.
1017 1017 If node2 is None, compare node1 with working directory.
1018 1018 """
1019 1019
1020 1020 def mfmatches(ctx):
1021 1021 mf = ctx.manifest().copy()
1022 1022 for fn in mf.keys():
1023 1023 if not match(fn):
1024 1024 del mf[fn]
1025 1025 return mf
1026 1026
1027 1027 if isinstance(node1, context.changectx):
1028 1028 ctx1 = node1
1029 1029 else:
1030 1030 ctx1 = self[node1]
1031 1031 if isinstance(node2, context.changectx):
1032 1032 ctx2 = node2
1033 1033 else:
1034 1034 ctx2 = self[node2]
1035 1035
1036 1036 working = ctx2.rev() is None
1037 1037 parentworking = working and ctx1 == self['.']
1038 1038 match = match or matchmod.always(self.root, self.getcwd())
1039 1039 listignored, listclean, listunknown = ignored, clean, unknown
1040 1040
1041 1041 # load earliest manifest first for caching reasons
1042 1042 if not working and ctx2.rev() < ctx1.rev():
1043 1043 ctx2.manifest()
1044 1044
1045 1045 if not parentworking:
1046 1046 def bad(f, msg):
1047 1047 if f not in ctx1:
1048 1048 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1049 1049 match.bad = bad
1050 1050
1051 1051 if working: # we need to scan the working dir
1052 1052 subrepos = []
1053 1053 if '.hgsub' in self.dirstate:
1054 1054 subrepos = ctx1.substate.keys()
1055 1055 s = self.dirstate.status(match, subrepos, listignored,
1056 1056 listclean, listunknown)
1057 1057 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1058 1058
1059 1059 # check for any possibly clean files
1060 1060 if parentworking and cmp:
1061 1061 fixup = []
1062 1062 # do a full compare of any files that might have changed
1063 1063 for f in sorted(cmp):
1064 1064 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1065 or ctx1[f].cmp(ctx2[f].data())):
1065 or ctx1[f].cmp(ctx2[f])):
1066 1066 modified.append(f)
1067 1067 else:
1068 1068 fixup.append(f)
1069 1069
1070 1070 # update dirstate for files that are actually clean
1071 1071 if fixup:
1072 1072 if listclean:
1073 1073 clean += fixup
1074 1074
1075 1075 try:
1076 1076 # updating the dirstate is optional
1077 1077 # so we don't wait on the lock
1078 1078 wlock = self.wlock(False)
1079 1079 try:
1080 1080 for f in fixup:
1081 1081 self.dirstate.normal(f)
1082 1082 finally:
1083 1083 wlock.release()
1084 1084 except error.LockError:
1085 1085 pass
1086 1086
1087 1087 if not parentworking:
1088 1088 mf1 = mfmatches(ctx1)
1089 1089 if working:
1090 1090 # we are comparing working dir against non-parent
1091 1091 # generate a pseudo-manifest for the working dir
1092 1092 mf2 = mfmatches(self['.'])
1093 1093 for f in cmp + modified + added:
1094 1094 mf2[f] = None
1095 1095 mf2.set(f, ctx2.flags(f))
1096 1096 for f in removed:
1097 1097 if f in mf2:
1098 1098 del mf2[f]
1099 1099 else:
1100 1100 # we are comparing two revisions
1101 1101 deleted, unknown, ignored = [], [], []
1102 1102 mf2 = mfmatches(ctx2)
1103 1103
1104 1104 modified, added, clean = [], [], []
1105 1105 for fn in mf2:
1106 1106 if fn in mf1:
1107 1107 if (mf1.flags(fn) != mf2.flags(fn) or
1108 1108 (mf1[fn] != mf2[fn] and
1109 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1109 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1110 1110 modified.append(fn)
1111 1111 elif listclean:
1112 1112 clean.append(fn)
1113 1113 del mf1[fn]
1114 1114 else:
1115 1115 added.append(fn)
1116 1116 removed = mf1.keys()
1117 1117
1118 1118 r = modified, added, removed, deleted, unknown, ignored, clean
1119 1119 [l.sort() for l in r]
1120 1120 return r
1121 1121
1122 1122 def heads(self, start=None):
1123 1123 heads = self.changelog.heads(start)
1124 1124 # sort the output in rev descending order
1125 1125 heads = [(-self.changelog.rev(h), h) for h in heads]
1126 1126 return [n for (r, n) in sorted(heads)]
1127 1127
1128 1128 def branchheads(self, branch=None, start=None, closed=False):
1129 1129 '''return a (possibly filtered) list of heads for the given branch
1130 1130
1131 1131 Heads are returned in topological order, from newest to oldest.
1132 1132 If branch is None, use the dirstate branch.
1133 1133 If start is not None, return only heads reachable from start.
1134 1134 If closed is True, return heads that are marked as closed as well.
1135 1135 '''
1136 1136 if branch is None:
1137 1137 branch = self[None].branch()
1138 1138 branches = self.branchmap()
1139 1139 if branch not in branches:
1140 1140 return []
1141 1141 # the cache returns heads ordered lowest to highest
1142 1142 bheads = list(reversed(branches[branch]))
1143 1143 if start is not None:
1144 1144 # filter out the heads that cannot be reached from startrev
1145 1145 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1146 1146 bheads = [h for h in bheads if h in fbheads]
1147 1147 if not closed:
1148 1148 bheads = [h for h in bheads if
1149 1149 ('close' not in self.changelog.read(h)[5])]
1150 1150 return bheads
1151 1151
1152 1152 def branches(self, nodes):
1153 1153 if not nodes:
1154 1154 nodes = [self.changelog.tip()]
1155 1155 b = []
1156 1156 for n in nodes:
1157 1157 t = n
1158 1158 while 1:
1159 1159 p = self.changelog.parents(n)
1160 1160 if p[1] != nullid or p[0] == nullid:
1161 1161 b.append((t, n, p[0], p[1]))
1162 1162 break
1163 1163 n = p[0]
1164 1164 return b
1165 1165
1166 1166 def between(self, pairs):
1167 1167 r = []
1168 1168
1169 1169 for top, bottom in pairs:
1170 1170 n, l, i = top, [], 0
1171 1171 f = 1
1172 1172
1173 1173 while n != bottom and n != nullid:
1174 1174 p = self.changelog.parents(n)[0]
1175 1175 if i == f:
1176 1176 l.append(n)
1177 1177 f = f * 2
1178 1178 n = p
1179 1179 i += 1
1180 1180
1181 1181 r.append(l)
1182 1182
1183 1183 return r
1184 1184
1185 1185 def pull(self, remote, heads=None, force=False):
1186 1186 lock = self.lock()
1187 1187 try:
1188 1188 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1189 1189 force=force)
1190 1190 common, fetch, rheads = tmp
1191 1191 if not fetch:
1192 1192 self.ui.status(_("no changes found\n"))
1193 1193 return 0
1194 1194
1195 1195 if fetch == [nullid]:
1196 1196 self.ui.status(_("requesting all changes\n"))
1197 1197 elif heads is None and remote.capable('changegroupsubset'):
1198 1198 # issue1320, avoid a race if remote changed after discovery
1199 1199 heads = rheads
1200 1200
1201 1201 if heads is None:
1202 1202 cg = remote.changegroup(fetch, 'pull')
1203 1203 else:
1204 1204 if not remote.capable('changegroupsubset'):
1205 1205 raise util.Abort(_("Partial pull cannot be done because "
1206 1206 "other repository doesn't support "
1207 1207 "changegroupsubset."))
1208 1208 cg = remote.changegroupsubset(fetch, heads, 'pull')
1209 1209 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1210 1210 finally:
1211 1211 lock.release()
1212 1212
1213 1213 def push(self, remote, force=False, revs=None, newbranch=False):
1214 1214 '''Push outgoing changesets (limited by revs) from the current
1215 1215 repository to remote. Return an integer:
1216 1216 - 0 means HTTP error *or* nothing to push
1217 1217 - 1 means we pushed and remote head count is unchanged *or*
1218 1218 we have outgoing changesets but refused to push
1219 1219 - other values as described by addchangegroup()
1220 1220 '''
1221 1221 # there are two ways to push to remote repo:
1222 1222 #
1223 1223 # addchangegroup assumes local user can lock remote
1224 1224 # repo (local filesystem, old ssh servers).
1225 1225 #
1226 1226 # unbundle assumes local user cannot lock remote repo (new ssh
1227 1227 # servers, http servers).
1228 1228
1229 1229 lock = None
1230 1230 unbundle = remote.capable('unbundle')
1231 1231 if not unbundle:
1232 1232 lock = remote.lock()
1233 1233 try:
1234 1234 ret = discovery.prepush(self, remote, force, revs, newbranch)
1235 1235 if ret[0] is None:
1236 1236 # and here we return 0 for "nothing to push" or 1 for
1237 1237 # "something to push but I refuse"
1238 1238 return ret[1]
1239 1239
1240 1240 cg, remote_heads = ret
1241 1241 if unbundle:
1242 1242 # local repo finds heads on server, finds out what revs it must
1243 1243 # push. once revs transferred, if server finds it has
1244 1244 # different heads (someone else won commit/push race), server
1245 1245 # aborts.
1246 1246 if force:
1247 1247 remote_heads = ['force']
1248 1248 # ssh: return remote's addchangegroup()
1249 1249 # http: return remote's addchangegroup() or 0 for error
1250 1250 return remote.unbundle(cg, remote_heads, 'push')
1251 1251 else:
1252 1252 # we return an integer indicating remote head count change
1253 1253 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1254 1254 finally:
1255 1255 if lock is not None:
1256 1256 lock.release()
1257 1257
1258 1258 def changegroupinfo(self, nodes, source):
1259 1259 if self.ui.verbose or source == 'bundle':
1260 1260 self.ui.status(_("%d changesets found\n") % len(nodes))
1261 1261 if self.ui.debugflag:
1262 1262 self.ui.debug("list of changesets:\n")
1263 1263 for node in nodes:
1264 1264 self.ui.debug("%s\n" % hex(node))
1265 1265
1266 1266 def changegroupsubset(self, bases, heads, source, extranodes=None):
1267 1267 """Compute a changegroup consisting of all the nodes that are
1268 1268 descendents of any of the bases and ancestors of any of the heads.
1269 1269 Return a chunkbuffer object whose read() method will return
1270 1270 successive changegroup chunks.
1271 1271
1272 1272 It is fairly complex as determining which filenodes and which
1273 1273 manifest nodes need to be included for the changeset to be complete
1274 1274 is non-trivial.
1275 1275
1276 1276 Another wrinkle is doing the reverse, figuring out which changeset in
1277 1277 the changegroup a particular filenode or manifestnode belongs to.
1278 1278
1279 1279 The caller can specify some nodes that must be included in the
1280 1280 changegroup using the extranodes argument. It should be a dict
1281 1281 where the keys are the filenames (or 1 for the manifest), and the
1282 1282 values are lists of (node, linknode) tuples, where node is a wanted
1283 1283 node and linknode is the changelog node that should be transmitted as
1284 1284 the linkrev.
1285 1285 """
1286 1286
1287 1287 # Set up some initial variables
1288 1288 # Make it easy to refer to self.changelog
1289 1289 cl = self.changelog
1290 1290 # Compute the list of changesets in this changegroup.
1291 1291 # Some bases may turn out to be superfluous, and some heads may be
1292 1292 # too. nodesbetween will return the minimal set of bases and heads
1293 1293 # necessary to re-create the changegroup.
1294 1294 if not bases:
1295 1295 bases = [nullid]
1296 1296 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1297 1297
1298 1298 if extranodes is None:
1299 1299 # can we go through the fast path ?
1300 1300 heads.sort()
1301 1301 allheads = self.heads()
1302 1302 allheads.sort()
1303 1303 if heads == allheads:
1304 1304 return self._changegroup(msng_cl_lst, source)
1305 1305
1306 1306 # slow path
1307 1307 self.hook('preoutgoing', throw=True, source=source)
1308 1308
1309 1309 self.changegroupinfo(msng_cl_lst, source)
1310 1310
1311 1311 # We assume that all ancestors of bases are known
1312 1312 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1313 1313
1314 1314 # Make it easy to refer to self.manifest
1315 1315 mnfst = self.manifest
1316 1316 # We don't know which manifests are missing yet
1317 1317 msng_mnfst_set = {}
1318 1318 # Nor do we know which filenodes are missing.
1319 1319 msng_filenode_set = {}
1320 1320
1321 1321 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1322 1322 junk = None
1323 1323
1324 1324 # A changeset always belongs to itself, so the changenode lookup
1325 1325 # function for a changenode is identity.
1326 1326 def identity(x):
1327 1327 return x
1328 1328
1329 1329 # A function generating function that sets up the initial environment
1330 1330 # the inner function.
1331 1331 def filenode_collector(changedfiles):
1332 1332 # This gathers information from each manifestnode included in the
1333 1333 # changegroup about which filenodes the manifest node references
1334 1334 # so we can include those in the changegroup too.
1335 1335 #
1336 1336 # It also remembers which changenode each filenode belongs to. It
1337 1337 # does this by assuming the a filenode belongs to the changenode
1338 1338 # the first manifest that references it belongs to.
1339 1339 def collect_msng_filenodes(mnfstnode):
1340 1340 r = mnfst.rev(mnfstnode)
1341 1341 if r - 1 in mnfst.parentrevs(r):
1342 1342 # If the previous rev is one of the parents,
1343 1343 # we only need to see a diff.
1344 1344 deltamf = mnfst.readdelta(mnfstnode)
1345 1345 # For each line in the delta
1346 1346 for f, fnode in deltamf.iteritems():
1347 1347 # And if the file is in the list of files we care
1348 1348 # about.
1349 1349 if f in changedfiles:
1350 1350 # Get the changenode this manifest belongs to
1351 1351 clnode = msng_mnfst_set[mnfstnode]
1352 1352 # Create the set of filenodes for the file if
1353 1353 # there isn't one already.
1354 1354 ndset = msng_filenode_set.setdefault(f, {})
1355 1355 # And set the filenode's changelog node to the
1356 1356 # manifest's if it hasn't been set already.
1357 1357 ndset.setdefault(fnode, clnode)
1358 1358 else:
1359 1359 # Otherwise we need a full manifest.
1360 1360 m = mnfst.read(mnfstnode)
1361 1361 # For every file in we care about.
1362 1362 for f in changedfiles:
1363 1363 fnode = m.get(f, None)
1364 1364 # If it's in the manifest
1365 1365 if fnode is not None:
1366 1366 # See comments above.
1367 1367 clnode = msng_mnfst_set[mnfstnode]
1368 1368 ndset = msng_filenode_set.setdefault(f, {})
1369 1369 ndset.setdefault(fnode, clnode)
1370 1370 return collect_msng_filenodes
1371 1371
1372 1372 # If we determine that a particular file or manifest node must be a
1373 1373 # node that the recipient of the changegroup will already have, we can
1374 1374 # also assume the recipient will have all the parents. This function
1375 1375 # prunes them from the set of missing nodes.
1376 1376 def prune(revlog, missingnodes):
1377 1377 hasset = set()
1378 1378 # If a 'missing' filenode thinks it belongs to a changenode we
1379 1379 # assume the recipient must have, then the recipient must have
1380 1380 # that filenode.
1381 1381 for n in missingnodes:
1382 1382 clrev = revlog.linkrev(revlog.rev(n))
1383 1383 if clrev in commonrevs:
1384 1384 hasset.add(n)
1385 1385 for n in hasset:
1386 1386 missingnodes.pop(n, None)
1387 1387 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1388 1388 missingnodes.pop(revlog.node(r), None)
1389 1389
1390 1390 # Add the nodes that were explicitly requested.
1391 1391 def add_extra_nodes(name, nodes):
1392 1392 if not extranodes or name not in extranodes:
1393 1393 return
1394 1394
1395 1395 for node, linknode in extranodes[name]:
1396 1396 if node not in nodes:
1397 1397 nodes[node] = linknode
1398 1398
1399 1399 # Now that we have all theses utility functions to help out and
1400 1400 # logically divide up the task, generate the group.
1401 1401 def gengroup():
1402 1402 # The set of changed files starts empty.
1403 1403 changedfiles = set()
1404 1404 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1405 1405
1406 1406 # Create a changenode group generator that will call our functions
1407 1407 # back to lookup the owning changenode and collect information.
1408 1408 group = cl.group(msng_cl_lst, identity, collect)
1409 1409 for cnt, chnk in enumerate(group):
1410 1410 yield chnk
1411 1411 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1412 1412 self.ui.progress(_('bundling changes'), None)
1413 1413
1414 1414 prune(mnfst, msng_mnfst_set)
1415 1415 add_extra_nodes(1, msng_mnfst_set)
1416 1416 msng_mnfst_lst = msng_mnfst_set.keys()
1417 1417 # Sort the manifestnodes by revision number.
1418 1418 msng_mnfst_lst.sort(key=mnfst.rev)
1419 1419 # Create a generator for the manifestnodes that calls our lookup
1420 1420 # and data collection functions back.
1421 1421 group = mnfst.group(msng_mnfst_lst,
1422 1422 lambda mnode: msng_mnfst_set[mnode],
1423 1423 filenode_collector(changedfiles))
1424 1424 for cnt, chnk in enumerate(group):
1425 1425 yield chnk
1426 1426 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1427 1427 self.ui.progress(_('bundling manifests'), None)
1428 1428
1429 1429 # These are no longer needed, dereference and toss the memory for
1430 1430 # them.
1431 1431 msng_mnfst_lst = None
1432 1432 msng_mnfst_set.clear()
1433 1433
1434 1434 if extranodes:
1435 1435 for fname in extranodes:
1436 1436 if isinstance(fname, int):
1437 1437 continue
1438 1438 msng_filenode_set.setdefault(fname, {})
1439 1439 changedfiles.add(fname)
1440 1440 # Go through all our files in order sorted by name.
1441 1441 cnt = 0
1442 1442 for fname in sorted(changedfiles):
1443 1443 filerevlog = self.file(fname)
1444 1444 if not len(filerevlog):
1445 1445 raise util.Abort(_("empty or missing revlog for %s") % fname)
1446 1446 # Toss out the filenodes that the recipient isn't really
1447 1447 # missing.
1448 1448 missingfnodes = msng_filenode_set.pop(fname, {})
1449 1449 prune(filerevlog, missingfnodes)
1450 1450 add_extra_nodes(fname, missingfnodes)
1451 1451 # If any filenodes are left, generate the group for them,
1452 1452 # otherwise don't bother.
1453 1453 if missingfnodes:
1454 1454 yield changegroup.chunkheader(len(fname))
1455 1455 yield fname
1456 1456 # Sort the filenodes by their revision # (topological order)
1457 1457 nodeiter = list(missingfnodes)
1458 1458 nodeiter.sort(key=filerevlog.rev)
1459 1459 # Create a group generator and only pass in a changenode
1460 1460 # lookup function as we need to collect no information
1461 1461 # from filenodes.
1462 1462 group = filerevlog.group(nodeiter,
1463 1463 lambda fnode: missingfnodes[fnode])
1464 1464 for chnk in group:
1465 1465 self.ui.progress(
1466 1466 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1467 1467 cnt += 1
1468 1468 yield chnk
1469 1469 # Signal that no more groups are left.
1470 1470 yield changegroup.closechunk()
1471 1471 self.ui.progress(_('bundling files'), None)
1472 1472
1473 1473 if msng_cl_lst:
1474 1474 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1475 1475
1476 1476 return util.chunkbuffer(gengroup())
1477 1477
1478 1478 def changegroup(self, basenodes, source):
1479 1479 # to avoid a race we use changegroupsubset() (issue1320)
1480 1480 return self.changegroupsubset(basenodes, self.heads(), source)
1481 1481
1482 1482 def _changegroup(self, nodes, source):
1483 1483 """Compute the changegroup of all nodes that we have that a recipient
1484 1484 doesn't. Return a chunkbuffer object whose read() method will return
1485 1485 successive changegroup chunks.
1486 1486
1487 1487 This is much easier than the previous function as we can assume that
1488 1488 the recipient has any changenode we aren't sending them.
1489 1489
1490 1490 nodes is the set of nodes to send"""
1491 1491
1492 1492 self.hook('preoutgoing', throw=True, source=source)
1493 1493
1494 1494 cl = self.changelog
1495 1495 revset = set([cl.rev(n) for n in nodes])
1496 1496 self.changegroupinfo(nodes, source)
1497 1497
1498 1498 def identity(x):
1499 1499 return x
1500 1500
1501 1501 def gennodelst(log):
1502 1502 for r in log:
1503 1503 if log.linkrev(r) in revset:
1504 1504 yield log.node(r)
1505 1505
1506 1506 def lookuplinkrev_func(revlog):
1507 1507 def lookuplinkrev(n):
1508 1508 return cl.node(revlog.linkrev(revlog.rev(n)))
1509 1509 return lookuplinkrev
1510 1510
1511 1511 def gengroup():
1512 1512 '''yield a sequence of changegroup chunks (strings)'''
1513 1513 # construct a list of all changed files
1514 1514 changedfiles = set()
1515 1515 mmfs = {}
1516 1516 collect = changegroup.collector(cl, mmfs, changedfiles)
1517 1517
1518 1518 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1519 1519 self.ui.progress(_('bundling changes'), cnt, unit=_('chunks'))
1520 1520 yield chnk
1521 1521 self.ui.progress(_('bundling changes'), None)
1522 1522
1523 1523 mnfst = self.manifest
1524 1524 nodeiter = gennodelst(mnfst)
1525 1525 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1526 1526 lookuplinkrev_func(mnfst))):
1527 1527 self.ui.progress(_('bundling manifests'), cnt, unit=_('chunks'))
1528 1528 yield chnk
1529 1529 self.ui.progress(_('bundling manifests'), None)
1530 1530
1531 1531 cnt = 0
1532 1532 for fname in sorted(changedfiles):
1533 1533 filerevlog = self.file(fname)
1534 1534 if not len(filerevlog):
1535 1535 raise util.Abort(_("empty or missing revlog for %s") % fname)
1536 1536 nodeiter = gennodelst(filerevlog)
1537 1537 nodeiter = list(nodeiter)
1538 1538 if nodeiter:
1539 1539 yield changegroup.chunkheader(len(fname))
1540 1540 yield fname
1541 1541 lookup = lookuplinkrev_func(filerevlog)
1542 1542 for chnk in filerevlog.group(nodeiter, lookup):
1543 1543 self.ui.progress(
1544 1544 _('bundling files'), cnt, item=fname, unit=_('chunks'))
1545 1545 cnt += 1
1546 1546 yield chnk
1547 1547 self.ui.progress(_('bundling files'), None)
1548 1548
1549 1549 yield changegroup.closechunk()
1550 1550
1551 1551 if nodes:
1552 1552 self.hook('outgoing', node=hex(nodes[0]), source=source)
1553 1553
1554 1554 return util.chunkbuffer(gengroup())
1555 1555
1556 1556 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1557 1557 """Add the changegroup returned by source.read() to this repo.
1558 1558 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1559 1559 the URL of the repo where this changegroup is coming from.
1560 1560
1561 1561 Return an integer summarizing the change to this repo:
1562 1562 - nothing changed or no source: 0
1563 1563 - more heads than before: 1+added heads (2..n)
1564 1564 - fewer heads than before: -1-removed heads (-2..-n)
1565 1565 - number of heads stays the same: 1
1566 1566 """
1567 1567 def csmap(x):
1568 1568 self.ui.debug("add changeset %s\n" % short(x))
1569 1569 return len(cl)
1570 1570
1571 1571 def revmap(x):
1572 1572 return cl.rev(x)
1573 1573
1574 1574 if not source:
1575 1575 return 0
1576 1576
1577 1577 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1578 1578
1579 1579 changesets = files = revisions = 0
1580 1580 efiles = set()
1581 1581
1582 1582 # write changelog data to temp files so concurrent readers will not see
1583 1583 # inconsistent view
1584 1584 cl = self.changelog
1585 1585 cl.delayupdate()
1586 1586 oldheads = len(cl.heads())
1587 1587
1588 1588 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1589 1589 try:
1590 1590 trp = weakref.proxy(tr)
1591 1591 # pull off the changeset group
1592 1592 self.ui.status(_("adding changesets\n"))
1593 1593 clstart = len(cl)
1594 1594 class prog(object):
1595 1595 step = _('changesets')
1596 1596 count = 1
1597 1597 ui = self.ui
1598 1598 total = None
1599 1599 def __call__(self):
1600 1600 self.ui.progress(self.step, self.count, unit=_('chunks'),
1601 1601 total=self.total)
1602 1602 self.count += 1
1603 1603 pr = prog()
1604 1604 chunkiter = changegroup.chunkiter(source, progress=pr)
1605 1605 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
1606 1606 raise util.Abort(_("received changelog group is empty"))
1607 1607 clend = len(cl)
1608 1608 changesets = clend - clstart
1609 1609 for c in xrange(clstart, clend):
1610 1610 efiles.update(self[c].files())
1611 1611 efiles = len(efiles)
1612 1612 self.ui.progress(_('changesets'), None)
1613 1613
1614 1614 # pull off the manifest group
1615 1615 self.ui.status(_("adding manifests\n"))
1616 1616 pr.step = _('manifests')
1617 1617 pr.count = 1
1618 1618 pr.total = changesets # manifests <= changesets
1619 1619 chunkiter = changegroup.chunkiter(source, progress=pr)
1620 1620 # no need to check for empty manifest group here:
1621 1621 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1622 1622 # no new manifest will be created and the manifest group will
1623 1623 # be empty during the pull
1624 1624 self.manifest.addgroup(chunkiter, revmap, trp)
1625 1625 self.ui.progress(_('manifests'), None)
1626 1626
1627 1627 needfiles = {}
1628 1628 if self.ui.configbool('server', 'validate', default=False):
1629 1629 # validate incoming csets have their manifests
1630 1630 for cset in xrange(clstart, clend):
1631 1631 mfest = self.changelog.read(self.changelog.node(cset))[0]
1632 1632 mfest = self.manifest.readdelta(mfest)
1633 1633 # store file nodes we must see
1634 1634 for f, n in mfest.iteritems():
1635 1635 needfiles.setdefault(f, set()).add(n)
1636 1636
1637 1637 # process the files
1638 1638 self.ui.status(_("adding file changes\n"))
1639 1639 pr.step = 'files'
1640 1640 pr.count = 1
1641 1641 pr.total = efiles
1642 1642 while 1:
1643 1643 f = changegroup.getchunk(source)
1644 1644 if not f:
1645 1645 break
1646 1646 self.ui.debug("adding %s revisions\n" % f)
1647 1647 pr()
1648 1648 fl = self.file(f)
1649 1649 o = len(fl)
1650 1650 chunkiter = changegroup.chunkiter(source)
1651 1651 if fl.addgroup(chunkiter, revmap, trp) is None:
1652 1652 raise util.Abort(_("received file revlog group is empty"))
1653 1653 revisions += len(fl) - o
1654 1654 files += 1
1655 1655 if f in needfiles:
1656 1656 needs = needfiles[f]
1657 1657 for new in xrange(o, len(fl)):
1658 1658 n = fl.node(new)
1659 1659 if n in needs:
1660 1660 needs.remove(n)
1661 1661 if not needs:
1662 1662 del needfiles[f]
1663 1663 self.ui.progress(_('files'), None)
1664 1664
1665 1665 for f, needs in needfiles.iteritems():
1666 1666 fl = self.file(f)
1667 1667 for n in needs:
1668 1668 try:
1669 1669 fl.rev(n)
1670 1670 except error.LookupError:
1671 1671 raise util.Abort(
1672 1672 _('missing file data for %s:%s - run hg verify') %
1673 1673 (f, hex(n)))
1674 1674
1675 1675 newheads = len(cl.heads())
1676 1676 heads = ""
1677 1677 if oldheads and newheads != oldheads:
1678 1678 heads = _(" (%+d heads)") % (newheads - oldheads)
1679 1679
1680 1680 self.ui.status(_("added %d changesets"
1681 1681 " with %d changes to %d files%s\n")
1682 1682 % (changesets, revisions, files, heads))
1683 1683
1684 1684 if changesets > 0:
1685 1685 p = lambda: cl.writepending() and self.root or ""
1686 1686 self.hook('pretxnchangegroup', throw=True,
1687 1687 node=hex(cl.node(clstart)), source=srctype,
1688 1688 url=url, pending=p)
1689 1689
1690 1690 # make changelog see real files again
1691 1691 cl.finalize(trp)
1692 1692
1693 1693 tr.close()
1694 1694 finally:
1695 1695 tr.release()
1696 1696 if lock:
1697 1697 lock.release()
1698 1698
1699 1699 if changesets > 0:
1700 1700 # forcefully update the on-disk branch cache
1701 1701 self.ui.debug("updating the branch cache\n")
1702 1702 self.branchtags()
1703 1703 self.hook("changegroup", node=hex(cl.node(clstart)),
1704 1704 source=srctype, url=url)
1705 1705
1706 1706 for i in xrange(clstart, clend):
1707 1707 self.hook("incoming", node=hex(cl.node(i)),
1708 1708 source=srctype, url=url)
1709 1709
1710 1710 # never return 0 here:
1711 1711 if newheads < oldheads:
1712 1712 return newheads - oldheads - 1
1713 1713 else:
1714 1714 return newheads - oldheads + 1
1715 1715
1716 1716
1717 1717 def stream_in(self, remote):
1718 1718 fp = remote.stream_out()
1719 1719 l = fp.readline()
1720 1720 try:
1721 1721 resp = int(l)
1722 1722 except ValueError:
1723 1723 raise error.ResponseError(
1724 1724 _('Unexpected response from remote server:'), l)
1725 1725 if resp == 1:
1726 1726 raise util.Abort(_('operation forbidden by server'))
1727 1727 elif resp == 2:
1728 1728 raise util.Abort(_('locking the remote repository failed'))
1729 1729 elif resp != 0:
1730 1730 raise util.Abort(_('the server sent an unknown error code'))
1731 1731 self.ui.status(_('streaming all changes\n'))
1732 1732 l = fp.readline()
1733 1733 try:
1734 1734 total_files, total_bytes = map(int, l.split(' ', 1))
1735 1735 except (ValueError, TypeError):
1736 1736 raise error.ResponseError(
1737 1737 _('Unexpected response from remote server:'), l)
1738 1738 self.ui.status(_('%d files to transfer, %s of data\n') %
1739 1739 (total_files, util.bytecount(total_bytes)))
1740 1740 start = time.time()
1741 1741 for i in xrange(total_files):
1742 1742 # XXX doesn't support '\n' or '\r' in filenames
1743 1743 l = fp.readline()
1744 1744 try:
1745 1745 name, size = l.split('\0', 1)
1746 1746 size = int(size)
1747 1747 except (ValueError, TypeError):
1748 1748 raise error.ResponseError(
1749 1749 _('Unexpected response from remote server:'), l)
1750 1750 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1751 1751 # for backwards compat, name was partially encoded
1752 1752 ofp = self.sopener(store.decodedir(name), 'w')
1753 1753 for chunk in util.filechunkiter(fp, limit=size):
1754 1754 ofp.write(chunk)
1755 1755 ofp.close()
1756 1756 elapsed = time.time() - start
1757 1757 if elapsed <= 0:
1758 1758 elapsed = 0.001
1759 1759 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1760 1760 (util.bytecount(total_bytes), elapsed,
1761 1761 util.bytecount(total_bytes / elapsed)))
1762 1762 self.invalidate()
1763 1763 return len(self.heads()) + 1
1764 1764
1765 1765 def clone(self, remote, heads=[], stream=False):
1766 1766 '''clone remote repository.
1767 1767
1768 1768 keyword arguments:
1769 1769 heads: list of revs to clone (forces use of pull)
1770 1770 stream: use streaming clone if possible'''
1771 1771
1772 1772 # now, all clients that can request uncompressed clones can
1773 1773 # read repo formats supported by all servers that can serve
1774 1774 # them.
1775 1775
1776 1776 # if revlog format changes, client will have to check version
1777 1777 # and format flags on "stream" capability, and use
1778 1778 # uncompressed only if compatible.
1779 1779
1780 1780 if stream and not heads and remote.capable('stream'):
1781 1781 return self.stream_in(remote)
1782 1782 return self.pull(remote, heads)
1783 1783
1784 1784 def pushkey(self, namespace, key, old, new):
1785 1785 return pushkey.push(self, namespace, key, old, new)
1786 1786
1787 1787 def listkeys(self, namespace):
1788 1788 return pushkey.list(self, namespace)
1789 1789
1790 1790 # used to avoid circular references so destructors work
1791 1791 def aftertrans(files):
1792 1792 renamefiles = [tuple(t) for t in files]
1793 1793 def a():
1794 1794 for src, dest in renamefiles:
1795 1795 util.rename(src, dest)
1796 1796 return a
1797 1797
1798 1798 def instance(ui, path, create):
1799 1799 return localrepository(ui, util.drop_scheme('file', path), create)
1800 1800
1801 1801 def islocal(path):
1802 1802 return True
@@ -1,527 +1,527 b''
1 1 # merge.py - directory-level update/merge handling 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, hex, bin
9 9 from i18n import _
10 10 import util, filemerge, copies, subrepo
11 11 import errno, os, shutil
12 12
13 13 class mergestate(object):
14 14 '''track 3-way merge state of individual files'''
15 15 def __init__(self, repo):
16 16 self._repo = repo
17 17 self._read()
18 18 def reset(self, node=None):
19 19 self._state = {}
20 20 if node:
21 21 self._local = node
22 22 shutil.rmtree(self._repo.join("merge"), True)
23 23 def _read(self):
24 24 self._state = {}
25 25 try:
26 26 f = self._repo.opener("merge/state")
27 27 for i, l in enumerate(f):
28 28 if i == 0:
29 29 self._local = bin(l[:-1])
30 30 else:
31 31 bits = l[:-1].split("\0")
32 32 self._state[bits[0]] = bits[1:]
33 33 except IOError, err:
34 34 if err.errno != errno.ENOENT:
35 35 raise
36 36 def _write(self):
37 37 f = self._repo.opener("merge/state", "w")
38 38 f.write(hex(self._local) + "\n")
39 39 for d, v in self._state.iteritems():
40 40 f.write("\0".join([d] + v) + "\n")
41 41 def add(self, fcl, fco, fca, fd, flags):
42 42 hash = util.sha1(fcl.path()).hexdigest()
43 43 self._repo.opener("merge/" + hash, "w").write(fcl.data())
44 44 self._state[fd] = ['u', hash, fcl.path(), fca.path(),
45 45 hex(fca.filenode()), fco.path(), flags]
46 46 self._write()
47 47 def __contains__(self, dfile):
48 48 return dfile in self._state
49 49 def __getitem__(self, dfile):
50 50 return self._state[dfile][0]
51 51 def __iter__(self):
52 52 l = self._state.keys()
53 53 l.sort()
54 54 for f in l:
55 55 yield f
56 56 def mark(self, dfile, state):
57 57 self._state[dfile][0] = state
58 58 self._write()
59 59 def resolve(self, dfile, wctx, octx):
60 60 if self[dfile] == 'r':
61 61 return 0
62 62 state, hash, lfile, afile, anode, ofile, flags = self._state[dfile]
63 63 f = self._repo.opener("merge/" + hash)
64 64 self._repo.wwrite(dfile, f.read(), flags)
65 65 fcd = wctx[dfile]
66 66 fco = octx[ofile]
67 67 fca = self._repo.filectx(afile, fileid=anode)
68 68 r = filemerge.filemerge(self._repo, self._local, lfile, fcd, fco, fca)
69 69 if not r:
70 70 self.mark(dfile, 'r')
71 71 return r
72 72
73 73 def _checkunknown(wctx, mctx):
74 74 "check for collisions between unknown files and files in mctx"
75 75 for f in wctx.unknown():
76 if f in mctx and mctx[f].cmp(wctx[f].data()):
76 if f in mctx and mctx[f].cmp(wctx[f]):
77 77 raise util.Abort(_("untracked file in working directory differs"
78 78 " from file in requested revision: '%s'") % f)
79 79
80 80 def _checkcollision(mctx):
81 81 "check for case folding collisions in the destination context"
82 82 folded = {}
83 83 for fn in mctx:
84 84 fold = fn.lower()
85 85 if fold in folded:
86 86 raise util.Abort(_("case-folding collision between %s and %s")
87 87 % (fn, folded[fold]))
88 88 folded[fold] = fn
89 89
90 90 def _forgetremoved(wctx, mctx, branchmerge):
91 91 """
92 92 Forget removed files
93 93
94 94 If we're jumping between revisions (as opposed to merging), and if
95 95 neither the working directory nor the target rev has the file,
96 96 then we need to remove it from the dirstate, to prevent the
97 97 dirstate from listing the file when it is no longer in the
98 98 manifest.
99 99
100 100 If we're merging, and the other revision has removed a file
101 101 that is not present in the working directory, we need to mark it
102 102 as removed.
103 103 """
104 104
105 105 action = []
106 106 state = branchmerge and 'r' or 'f'
107 107 for f in wctx.deleted():
108 108 if f not in mctx:
109 109 action.append((f, state))
110 110
111 111 if not branchmerge:
112 112 for f in wctx.removed():
113 113 if f not in mctx:
114 114 action.append((f, "f"))
115 115
116 116 return action
117 117
118 118 def manifestmerge(repo, p1, p2, pa, overwrite, partial):
119 119 """
120 120 Merge p1 and p2 with ancestor ma and generate merge action list
121 121
122 122 overwrite = whether we clobber working files
123 123 partial = function to filter file lists
124 124 """
125 125
126 126 def fmerge(f, f2, fa):
127 127 """merge flags"""
128 128 a, m, n = ma.flags(fa), m1.flags(f), m2.flags(f2)
129 129 if m == n: # flags agree
130 130 return m # unchanged
131 131 if m and n and not a: # flags set, don't agree, differ from parent
132 132 r = repo.ui.promptchoice(
133 133 _(" conflicting flags for %s\n"
134 134 "(n)one, e(x)ec or sym(l)ink?") % f,
135 135 (_("&None"), _("E&xec"), _("Sym&link")), 0)
136 136 if r == 1:
137 137 return "x" # Exec
138 138 if r == 2:
139 139 return "l" # Symlink
140 140 return ""
141 141 if m and m != a: # changed from a to m
142 142 return m
143 143 if n and n != a: # changed from a to n
144 144 return n
145 145 return '' # flag was cleared
146 146
147 147 def act(msg, m, f, *args):
148 148 repo.ui.debug(" %s: %s -> %s\n" % (f, msg, m))
149 149 action.append((f, m) + args)
150 150
151 151 action, copy = [], {}
152 152
153 153 if overwrite:
154 154 pa = p1
155 155 elif pa == p2: # backwards
156 156 pa = p1.p1()
157 157 elif pa and repo.ui.configbool("merge", "followcopies", True):
158 158 dirs = repo.ui.configbool("merge", "followdirs", True)
159 159 copy, diverge = copies.copies(repo, p1, p2, pa, dirs)
160 160 for of, fl in diverge.iteritems():
161 161 act("divergent renames", "dr", of, fl)
162 162
163 163 repo.ui.note(_("resolving manifests\n"))
164 164 repo.ui.debug(" overwrite %s partial %s\n" % (overwrite, bool(partial)))
165 165 repo.ui.debug(" ancestor %s local %s remote %s\n" % (pa, p1, p2))
166 166
167 167 m1, m2, ma = p1.manifest(), p2.manifest(), pa.manifest()
168 168 copied = set(copy.values())
169 169
170 170 if '.hgsubstate' in m1:
171 171 # check whether sub state is modified
172 172 for s in p1.substate:
173 173 if p1.sub(s).dirty():
174 174 m1['.hgsubstate'] += "+"
175 175 break
176 176
177 177 # Compare manifests
178 178 for f, n in m1.iteritems():
179 179 if partial and not partial(f):
180 180 continue
181 181 if f in m2:
182 182 rflags = fmerge(f, f, f)
183 183 a = ma.get(f, nullid)
184 184 if n == m2[f] or m2[f] == a: # same or local newer
185 185 # is file locally modified or flags need changing?
186 186 # dirstate flags may need to be made current
187 187 if m1.flags(f) != rflags or n[20:]:
188 188 act("update permissions", "e", f, rflags)
189 189 elif n == a: # remote newer
190 190 act("remote is newer", "g", f, rflags)
191 191 else: # both changed
192 192 act("versions differ", "m", f, f, f, rflags, False)
193 193 elif f in copied: # files we'll deal with on m2 side
194 194 pass
195 195 elif f in copy:
196 196 f2 = copy[f]
197 197 if f2 not in m2: # directory rename
198 198 act("remote renamed directory to " + f2, "d",
199 199 f, None, f2, m1.flags(f))
200 200 else: # case 2 A,B/B/B or case 4,21 A/B/B
201 201 act("local copied/moved to " + f2, "m",
202 202 f, f2, f, fmerge(f, f2, f2), False)
203 203 elif f in ma: # clean, a different, no remote
204 204 if n != ma[f]:
205 205 if repo.ui.promptchoice(
206 206 _(" local changed %s which remote deleted\n"
207 207 "use (c)hanged version or (d)elete?") % f,
208 208 (_("&Changed"), _("&Delete")), 0):
209 209 act("prompt delete", "r", f)
210 210 else:
211 211 act("prompt keep", "a", f)
212 212 elif n[20:] == "a": # added, no remote
213 213 act("remote deleted", "f", f)
214 214 elif n[20:] != "u":
215 215 act("other deleted", "r", f)
216 216
217 217 for f, n in m2.iteritems():
218 218 if partial and not partial(f):
219 219 continue
220 220 if f in m1 or f in copied: # files already visited
221 221 continue
222 222 if f in copy:
223 223 f2 = copy[f]
224 224 if f2 not in m1: # directory rename
225 225 act("local renamed directory to " + f2, "d",
226 226 None, f, f2, m2.flags(f))
227 227 elif f2 in m2: # rename case 1, A/A,B/A
228 228 act("remote copied to " + f, "m",
229 229 f2, f, f, fmerge(f2, f, f2), False)
230 230 else: # case 3,20 A/B/A
231 231 act("remote moved to " + f, "m",
232 232 f2, f, f, fmerge(f2, f, f2), True)
233 233 elif f not in ma:
234 234 act("remote created", "g", f, m2.flags(f))
235 235 elif n != ma[f]:
236 236 if repo.ui.promptchoice(
237 237 _("remote changed %s which local deleted\n"
238 238 "use (c)hanged version or leave (d)eleted?") % f,
239 239 (_("&Changed"), _("&Deleted")), 0) == 0:
240 240 act("prompt recreating", "g", f, m2.flags(f))
241 241
242 242 return action
243 243
244 244 def actionkey(a):
245 245 return a[1] == 'r' and -1 or 0, a
246 246
247 247 def applyupdates(repo, action, wctx, mctx, actx):
248 248 """apply the merge action list to the working directory
249 249
250 250 wctx is the working copy context
251 251 mctx is the context to be merged into the working copy
252 252 actx is the context of the common ancestor
253 253 """
254 254
255 255 updated, merged, removed, unresolved = 0, 0, 0, 0
256 256 ms = mergestate(repo)
257 257 ms.reset(wctx.parents()[0].node())
258 258 moves = []
259 259 action.sort(key=actionkey)
260 260 substate = wctx.substate # prime
261 261
262 262 # prescan for merges
263 263 u = repo.ui
264 264 for a in action:
265 265 f, m = a[:2]
266 266 if m == 'm': # merge
267 267 f2, fd, flags, move = a[2:]
268 268 if f == '.hgsubstate': # merged internally
269 269 continue
270 270 repo.ui.debug("preserving %s for resolve of %s\n" % (f, fd))
271 271 fcl = wctx[f]
272 272 fco = mctx[f2]
273 273 fca = fcl.ancestor(fco, actx) or repo.filectx(f, fileid=nullrev)
274 274 ms.add(fcl, fco, fca, fd, flags)
275 275 if f != fd and move:
276 276 moves.append(f)
277 277
278 278 # remove renamed files after safely stored
279 279 for f in moves:
280 280 if util.lexists(repo.wjoin(f)):
281 281 repo.ui.debug("removing %s\n" % f)
282 282 os.unlink(repo.wjoin(f))
283 283
284 284 audit_path = util.path_auditor(repo.root)
285 285
286 286 numupdates = len(action)
287 287 for i, a in enumerate(action):
288 288 f, m = a[:2]
289 289 u.progress('update', i + 1, item=f, total=numupdates, unit='files')
290 290 if f and f[0] == "/":
291 291 continue
292 292 if m == "r": # remove
293 293 repo.ui.note(_("removing %s\n") % f)
294 294 audit_path(f)
295 295 if f == '.hgsubstate': # subrepo states need updating
296 296 subrepo.submerge(repo, wctx, mctx, wctx)
297 297 try:
298 298 util.unlink(repo.wjoin(f))
299 299 except OSError, inst:
300 300 if inst.errno != errno.ENOENT:
301 301 repo.ui.warn(_("update failed to remove %s: %s!\n") %
302 302 (f, inst.strerror))
303 303 removed += 1
304 304 elif m == "m": # merge
305 305 if f == '.hgsubstate': # subrepo states need updating
306 306 subrepo.submerge(repo, wctx, mctx, wctx.ancestor(mctx))
307 307 continue
308 308 f2, fd, flags, move = a[2:]
309 309 r = ms.resolve(fd, wctx, mctx)
310 310 if r is not None and r > 0:
311 311 unresolved += 1
312 312 else:
313 313 if r is None:
314 314 updated += 1
315 315 else:
316 316 merged += 1
317 317 util.set_flags(repo.wjoin(fd), 'l' in flags, 'x' in flags)
318 318 if f != fd and move and util.lexists(repo.wjoin(f)):
319 319 repo.ui.debug("removing %s\n" % f)
320 320 os.unlink(repo.wjoin(f))
321 321 elif m == "g": # get
322 322 flags = a[2]
323 323 repo.ui.note(_("getting %s\n") % f)
324 324 t = mctx.filectx(f).data()
325 325 repo.wwrite(f, t, flags)
326 326 updated += 1
327 327 if f == '.hgsubstate': # subrepo states need updating
328 328 subrepo.submerge(repo, wctx, mctx, wctx)
329 329 elif m == "d": # directory rename
330 330 f2, fd, flags = a[2:]
331 331 if f:
332 332 repo.ui.note(_("moving %s to %s\n") % (f, fd))
333 333 t = wctx.filectx(f).data()
334 334 repo.wwrite(fd, t, flags)
335 335 util.unlink(repo.wjoin(f))
336 336 if f2:
337 337 repo.ui.note(_("getting %s to %s\n") % (f2, fd))
338 338 t = mctx.filectx(f2).data()
339 339 repo.wwrite(fd, t, flags)
340 340 updated += 1
341 341 elif m == "dr": # divergent renames
342 342 fl = a[2]
343 343 repo.ui.warn(_("warning: detected divergent renames of %s to:\n") % f)
344 344 for nf in fl:
345 345 repo.ui.warn(" %s\n" % nf)
346 346 elif m == "e": # exec
347 347 flags = a[2]
348 348 util.set_flags(repo.wjoin(f), 'l' in flags, 'x' in flags)
349 349 u.progress('update', None, total=numupdates, unit='files')
350 350
351 351 return updated, merged, removed, unresolved
352 352
353 353 def recordupdates(repo, action, branchmerge):
354 354 "record merge actions to the dirstate"
355 355
356 356 for a in action:
357 357 f, m = a[:2]
358 358 if m == "r": # remove
359 359 if branchmerge:
360 360 repo.dirstate.remove(f)
361 361 else:
362 362 repo.dirstate.forget(f)
363 363 elif m == "a": # re-add
364 364 if not branchmerge:
365 365 repo.dirstate.add(f)
366 366 elif m == "f": # forget
367 367 repo.dirstate.forget(f)
368 368 elif m == "e": # exec change
369 369 repo.dirstate.normallookup(f)
370 370 elif m == "g": # get
371 371 if branchmerge:
372 372 repo.dirstate.otherparent(f)
373 373 else:
374 374 repo.dirstate.normal(f)
375 375 elif m == "m": # merge
376 376 f2, fd, flag, move = a[2:]
377 377 if branchmerge:
378 378 # We've done a branch merge, mark this file as merged
379 379 # so that we properly record the merger later
380 380 repo.dirstate.merge(fd)
381 381 if f != f2: # copy/rename
382 382 if move:
383 383 repo.dirstate.remove(f)
384 384 if f != fd:
385 385 repo.dirstate.copy(f, fd)
386 386 else:
387 387 repo.dirstate.copy(f2, fd)
388 388 else:
389 389 # We've update-merged a locally modified file, so
390 390 # we set the dirstate to emulate a normal checkout
391 391 # of that file some time in the past. Thus our
392 392 # merge will appear as a normal local file
393 393 # modification.
394 394 if f2 == fd: # file not locally copied/moved
395 395 repo.dirstate.normallookup(fd)
396 396 if move:
397 397 repo.dirstate.forget(f)
398 398 elif m == "d": # directory rename
399 399 f2, fd, flag = a[2:]
400 400 if not f2 and f not in repo.dirstate:
401 401 # untracked file moved
402 402 continue
403 403 if branchmerge:
404 404 repo.dirstate.add(fd)
405 405 if f:
406 406 repo.dirstate.remove(f)
407 407 repo.dirstate.copy(f, fd)
408 408 if f2:
409 409 repo.dirstate.copy(f2, fd)
410 410 else:
411 411 repo.dirstate.normal(fd)
412 412 if f:
413 413 repo.dirstate.forget(f)
414 414
415 415 def update(repo, node, branchmerge, force, partial):
416 416 """
417 417 Perform a merge between the working directory and the given node
418 418
419 419 node = the node to update to, or None if unspecified
420 420 branchmerge = whether to merge between branches
421 421 force = whether to force branch merging or file overwriting
422 422 partial = a function to filter file lists (dirstate not updated)
423 423
424 424 The table below shows all the behaviors of the update command
425 425 given the -c and -C or no options, whether the working directory
426 426 is dirty, whether a revision is specified, and the relationship of
427 427 the parent rev to the target rev (linear, on the same named
428 428 branch, or on another named branch).
429 429
430 430 This logic is tested by test-update-branches.
431 431
432 432 -c -C dirty rev | linear same cross
433 433 n n n n | ok (1) x
434 434 n n n y | ok ok ok
435 435 n n y * | merge (2) (2)
436 436 n y * * | --- discard ---
437 437 y n y * | --- (3) ---
438 438 y n n * | --- ok ---
439 439 y y * * | --- (4) ---
440 440
441 441 x = can't happen
442 442 * = don't-care
443 443 1 = abort: crosses branches (use 'hg merge' or 'hg update -c')
444 444 2 = abort: crosses branches (use 'hg merge' to merge or
445 445 use 'hg update -C' to discard changes)
446 446 3 = abort: uncommitted local changes
447 447 4 = incompatible options (checked in commands.py)
448 448 """
449 449
450 450 onode = node
451 451 wlock = repo.wlock()
452 452 try:
453 453 wc = repo[None]
454 454 if node is None:
455 455 # tip of current branch
456 456 try:
457 457 node = repo.branchtags()[wc.branch()]
458 458 except KeyError:
459 459 if wc.branch() == "default": # no default branch!
460 460 node = repo.lookup("tip") # update to tip
461 461 else:
462 462 raise util.Abort(_("branch %s not found") % wc.branch())
463 463 overwrite = force and not branchmerge
464 464 pl = wc.parents()
465 465 p1, p2 = pl[0], repo[node]
466 466 pa = p1.ancestor(p2)
467 467 fp1, fp2, xp1, xp2 = p1.node(), p2.node(), str(p1), str(p2)
468 468 fastforward = False
469 469
470 470 ### check phase
471 471 if not overwrite and len(pl) > 1:
472 472 raise util.Abort(_("outstanding uncommitted merges"))
473 473 if branchmerge:
474 474 if pa == p2:
475 475 raise util.Abort(_("merging with a working directory ancestor"
476 476 " has no effect"))
477 477 elif pa == p1:
478 478 if p1.branch() != p2.branch():
479 479 fastforward = True
480 480 else:
481 481 raise util.Abort(_("nothing to merge (use 'hg update'"
482 482 " or check 'hg heads')"))
483 483 if not force and (wc.files() or wc.deleted()):
484 484 raise util.Abort(_("outstanding uncommitted changes "
485 485 "(use 'hg status' to list changes)"))
486 486 elif not overwrite:
487 487 if pa == p1 or pa == p2: # linear
488 488 pass # all good
489 489 elif wc.files() or wc.deleted():
490 490 raise util.Abort(_("crosses branches (use 'hg merge' to merge "
491 491 "or use 'hg update -C' to discard changes)"))
492 492 elif onode is None:
493 493 raise util.Abort(_("crosses branches (use 'hg merge' or use "
494 494 "'hg update -c')"))
495 495 else:
496 496 # Allow jumping branches if clean and specific rev given
497 497 overwrite = True
498 498
499 499 ### calculate phase
500 500 action = []
501 501 wc.status(unknown=True) # prime cache
502 502 if not force:
503 503 _checkunknown(wc, p2)
504 504 if not util.checkcase(repo.path):
505 505 _checkcollision(p2)
506 506 action += _forgetremoved(wc, p2, branchmerge)
507 507 action += manifestmerge(repo, wc, p2, pa, overwrite, partial)
508 508
509 509 ### apply phase
510 510 if not branchmerge: # just jump to the new rev
511 511 fp1, fp2, xp1, xp2 = fp2, nullid, xp2, ''
512 512 if not partial:
513 513 repo.hook('preupdate', throw=True, parent1=xp1, parent2=xp2)
514 514
515 515 stats = applyupdates(repo, action, wc, p2, pa)
516 516
517 517 if not partial:
518 518 repo.dirstate.setparents(fp1, fp2)
519 519 recordupdates(repo, action, branchmerge)
520 520 if not branchmerge and not fastforward:
521 521 repo.dirstate.setbranch(p2.branch())
522 522 finally:
523 523 wlock.release()
524 524
525 525 if not partial:
526 526 repo.hook('update', parent1=xp1, parent2=xp2, error=stats[3])
527 527 return stats
General Comments 0
You need to be logged in to leave comments. Login now