##// END OF EJS Templates
localrepo: lookup now goes through context
Matt Mackall -
r16378:c463f46f default
parent child Browse files
Show More
@@ -1,2356 +1,2326 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, bookmarks, phases
12 12 import lock, transaction, store, encoding
13 13 import scmutil, util, extensions, hook, error, revset
14 14 import match as matchmod
15 15 import merge as mergemod
16 16 import tags as tagsmod
17 17 from lock import release
18 18 import weakref, errno, os, time, inspect
19 19 propertycache = util.propertycache
20 20 filecache = scmutil.filecache
21 21
22 22 class storecache(filecache):
23 23 """filecache for files in the store"""
24 24 def join(self, obj, fname):
25 25 return obj.sjoin(fname)
26 26
27 27 class localrepository(repo.repository):
28 28 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey',
29 29 'known', 'getbundle'))
30 30 supportedformats = set(('revlogv1', 'generaldelta'))
31 31 supported = supportedformats | set(('store', 'fncache', 'shared',
32 32 'dotencode'))
33 33
34 34 def __init__(self, baseui, path=None, create=False):
35 35 repo.repository.__init__(self)
36 36 self.root = os.path.realpath(util.expandpath(path))
37 37 self.path = os.path.join(self.root, ".hg")
38 38 self.origroot = path
39 39 self.auditor = scmutil.pathauditor(self.root, self._checknested)
40 40 self.opener = scmutil.opener(self.path)
41 41 self.wopener = scmutil.opener(self.root)
42 42 self.baseui = baseui
43 43 self.ui = baseui.copy()
44 44 self._dirtyphases = False
45 45 # A list of callback to shape the phase if no data were found.
46 46 # Callback are in the form: func(repo, roots) --> processed root.
47 47 # This list it to be filled by extension during repo setup
48 48 self._phasedefaults = []
49 49
50 50 try:
51 51 self.ui.readconfig(self.join("hgrc"), self.root)
52 52 extensions.loadall(self.ui)
53 53 except IOError:
54 54 pass
55 55
56 56 if not os.path.isdir(self.path):
57 57 if create:
58 58 if not os.path.exists(path):
59 59 util.makedirs(path)
60 60 util.makedir(self.path, notindexed=True)
61 61 requirements = ["revlogv1"]
62 62 if self.ui.configbool('format', 'usestore', True):
63 63 os.mkdir(os.path.join(self.path, "store"))
64 64 requirements.append("store")
65 65 if self.ui.configbool('format', 'usefncache', True):
66 66 requirements.append("fncache")
67 67 if self.ui.configbool('format', 'dotencode', True):
68 68 requirements.append('dotencode')
69 69 # create an invalid changelog
70 70 self.opener.append(
71 71 "00changelog.i",
72 72 '\0\0\0\2' # represents revlogv2
73 73 ' dummy changelog to prevent using the old repo layout'
74 74 )
75 75 if self.ui.configbool('format', 'generaldelta', False):
76 76 requirements.append("generaldelta")
77 77 requirements = set(requirements)
78 78 else:
79 79 raise error.RepoError(_("repository %s not found") % path)
80 80 elif create:
81 81 raise error.RepoError(_("repository %s already exists") % path)
82 82 else:
83 83 try:
84 84 requirements = scmutil.readrequires(self.opener, self.supported)
85 85 except IOError, inst:
86 86 if inst.errno != errno.ENOENT:
87 87 raise
88 88 requirements = set()
89 89
90 90 self.sharedpath = self.path
91 91 try:
92 92 s = os.path.realpath(self.opener.read("sharedpath").rstrip('\n'))
93 93 if not os.path.exists(s):
94 94 raise error.RepoError(
95 95 _('.hg/sharedpath points to nonexistent directory %s') % s)
96 96 self.sharedpath = s
97 97 except IOError, inst:
98 98 if inst.errno != errno.ENOENT:
99 99 raise
100 100
101 101 self.store = store.store(requirements, self.sharedpath, scmutil.opener)
102 102 self.spath = self.store.path
103 103 self.sopener = self.store.opener
104 104 self.sjoin = self.store.join
105 105 self.opener.createmode = self.store.createmode
106 106 self._applyrequirements(requirements)
107 107 if create:
108 108 self._writerequirements()
109 109
110 110
111 111 self._branchcache = None
112 112 self._branchcachetip = None
113 113 self.filterpats = {}
114 114 self._datafilters = {}
115 115 self._transref = self._lockref = self._wlockref = None
116 116
117 117 # A cache for various files under .hg/ that tracks file changes,
118 118 # (used by the filecache decorator)
119 119 #
120 120 # Maps a property name to its util.filecacheentry
121 121 self._filecache = {}
122 122
123 123 def _applyrequirements(self, requirements):
124 124 self.requirements = requirements
125 125 openerreqs = set(('revlogv1', 'generaldelta'))
126 126 self.sopener.options = dict((r, 1) for r in requirements
127 127 if r in openerreqs)
128 128
129 129 def _writerequirements(self):
130 130 reqfile = self.opener("requires", "w")
131 131 for r in self.requirements:
132 132 reqfile.write("%s\n" % r)
133 133 reqfile.close()
134 134
135 135 def _checknested(self, path):
136 136 """Determine if path is a legal nested repository."""
137 137 if not path.startswith(self.root):
138 138 return False
139 139 subpath = path[len(self.root) + 1:]
140 140 normsubpath = util.pconvert(subpath)
141 141
142 142 # XXX: Checking against the current working copy is wrong in
143 143 # the sense that it can reject things like
144 144 #
145 145 # $ hg cat -r 10 sub/x.txt
146 146 #
147 147 # if sub/ is no longer a subrepository in the working copy
148 148 # parent revision.
149 149 #
150 150 # However, it can of course also allow things that would have
151 151 # been rejected before, such as the above cat command if sub/
152 152 # is a subrepository now, but was a normal directory before.
153 153 # The old path auditor would have rejected by mistake since it
154 154 # panics when it sees sub/.hg/.
155 155 #
156 156 # All in all, checking against the working copy seems sensible
157 157 # since we want to prevent access to nested repositories on
158 158 # the filesystem *now*.
159 159 ctx = self[None]
160 160 parts = util.splitpath(subpath)
161 161 while parts:
162 162 prefix = '/'.join(parts)
163 163 if prefix in ctx.substate:
164 164 if prefix == normsubpath:
165 165 return True
166 166 else:
167 167 sub = ctx.sub(prefix)
168 168 return sub.checknested(subpath[len(prefix) + 1:])
169 169 else:
170 170 parts.pop()
171 171 return False
172 172
173 173 @filecache('bookmarks')
174 174 def _bookmarks(self):
175 175 return bookmarks.read(self)
176 176
177 177 @filecache('bookmarks.current')
178 178 def _bookmarkcurrent(self):
179 179 return bookmarks.readcurrent(self)
180 180
181 181 def _writebookmarks(self, marks):
182 182 bookmarks.write(self)
183 183
184 184 @storecache('phaseroots')
185 185 def _phaseroots(self):
186 186 self._dirtyphases = False
187 187 phaseroots = phases.readroots(self)
188 188 phases.filterunknown(self, phaseroots)
189 189 return phaseroots
190 190
191 191 @propertycache
192 192 def _phaserev(self):
193 193 cache = [phases.public] * len(self)
194 194 for phase in phases.trackedphases:
195 195 roots = map(self.changelog.rev, self._phaseroots[phase])
196 196 if roots:
197 197 for rev in roots:
198 198 cache[rev] = phase
199 199 for rev in self.changelog.descendants(*roots):
200 200 cache[rev] = phase
201 201 return cache
202 202
203 203 @storecache('00changelog.i')
204 204 def changelog(self):
205 205 c = changelog.changelog(self.sopener)
206 206 if 'HG_PENDING' in os.environ:
207 207 p = os.environ['HG_PENDING']
208 208 if p.startswith(self.root):
209 209 c.readpending('00changelog.i.a')
210 210 return c
211 211
212 212 @storecache('00manifest.i')
213 213 def manifest(self):
214 214 return manifest.manifest(self.sopener)
215 215
216 216 @filecache('dirstate')
217 217 def dirstate(self):
218 218 warned = [0]
219 219 def validate(node):
220 220 try:
221 221 self.changelog.rev(node)
222 222 return node
223 223 except error.LookupError:
224 224 if not warned[0]:
225 225 warned[0] = True
226 226 self.ui.warn(_("warning: ignoring unknown"
227 227 " working parent %s!\n") % short(node))
228 228 return nullid
229 229
230 230 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
231 231
232 232 def __getitem__(self, changeid):
233 233 if changeid is None:
234 234 return context.workingctx(self)
235 235 return context.changectx(self, changeid)
236 236
237 237 def __contains__(self, changeid):
238 238 try:
239 239 return bool(self.lookup(changeid))
240 240 except error.RepoLookupError:
241 241 return False
242 242
243 243 def __nonzero__(self):
244 244 return True
245 245
246 246 def __len__(self):
247 247 return len(self.changelog)
248 248
249 249 def __iter__(self):
250 250 for i in xrange(len(self)):
251 251 yield i
252 252
253 253 def revs(self, expr, *args):
254 254 '''Return a list of revisions matching the given revset'''
255 255 expr = revset.formatspec(expr, *args)
256 256 m = revset.match(None, expr)
257 257 return [r for r in m(self, range(len(self)))]
258 258
259 259 def set(self, expr, *args):
260 260 '''
261 261 Yield a context for each matching revision, after doing arg
262 262 replacement via revset.formatspec
263 263 '''
264 264 for r in self.revs(expr, *args):
265 265 yield self[r]
266 266
267 267 def url(self):
268 268 return 'file:' + self.root
269 269
270 270 def hook(self, name, throw=False, **args):
271 271 return hook.hook(self.ui, self, name, throw, **args)
272 272
273 273 tag_disallowed = ':\r\n'
274 274
275 275 def _tag(self, names, node, message, local, user, date, extra={}):
276 276 if isinstance(names, str):
277 277 allchars = names
278 278 names = (names,)
279 279 else:
280 280 allchars = ''.join(names)
281 281 for c in self.tag_disallowed:
282 282 if c in allchars:
283 283 raise util.Abort(_('%r cannot be used in a tag name') % c)
284 284
285 285 branches = self.branchmap()
286 286 for name in names:
287 287 self.hook('pretag', throw=True, node=hex(node), tag=name,
288 288 local=local)
289 289 if name in branches:
290 290 self.ui.warn(_("warning: tag %s conflicts with existing"
291 291 " branch name\n") % name)
292 292
293 293 def writetags(fp, names, munge, prevtags):
294 294 fp.seek(0, 2)
295 295 if prevtags and prevtags[-1] != '\n':
296 296 fp.write('\n')
297 297 for name in names:
298 298 m = munge and munge(name) or name
299 299 if self._tagscache.tagtypes and name in self._tagscache.tagtypes:
300 300 old = self.tags().get(name, nullid)
301 301 fp.write('%s %s\n' % (hex(old), m))
302 302 fp.write('%s %s\n' % (hex(node), m))
303 303 fp.close()
304 304
305 305 prevtags = ''
306 306 if local:
307 307 try:
308 308 fp = self.opener('localtags', 'r+')
309 309 except IOError:
310 310 fp = self.opener('localtags', 'a')
311 311 else:
312 312 prevtags = fp.read()
313 313
314 314 # local tags are stored in the current charset
315 315 writetags(fp, names, None, prevtags)
316 316 for name in names:
317 317 self.hook('tag', node=hex(node), tag=name, local=local)
318 318 return
319 319
320 320 try:
321 321 fp = self.wfile('.hgtags', 'rb+')
322 322 except IOError, e:
323 323 if e.errno != errno.ENOENT:
324 324 raise
325 325 fp = self.wfile('.hgtags', 'ab')
326 326 else:
327 327 prevtags = fp.read()
328 328
329 329 # committed tags are stored in UTF-8
330 330 writetags(fp, names, encoding.fromlocal, prevtags)
331 331
332 332 fp.close()
333 333
334 334 self.invalidatecaches()
335 335
336 336 if '.hgtags' not in self.dirstate:
337 337 self[None].add(['.hgtags'])
338 338
339 339 m = matchmod.exact(self.root, '', ['.hgtags'])
340 340 tagnode = self.commit(message, user, date, extra=extra, match=m)
341 341
342 342 for name in names:
343 343 self.hook('tag', node=hex(node), tag=name, local=local)
344 344
345 345 return tagnode
346 346
347 347 def tag(self, names, node, message, local, user, date):
348 348 '''tag a revision with one or more symbolic names.
349 349
350 350 names is a list of strings or, when adding a single tag, names may be a
351 351 string.
352 352
353 353 if local is True, the tags are stored in a per-repository file.
354 354 otherwise, they are stored in the .hgtags file, and a new
355 355 changeset is committed with the change.
356 356
357 357 keyword arguments:
358 358
359 359 local: whether to store tags in non-version-controlled file
360 360 (default False)
361 361
362 362 message: commit message to use if committing
363 363
364 364 user: name of user to use if committing
365 365
366 366 date: date tuple to use if committing'''
367 367
368 368 if not local:
369 369 for x in self.status()[:5]:
370 370 if '.hgtags' in x:
371 371 raise util.Abort(_('working copy of .hgtags is changed '
372 372 '(please commit .hgtags manually)'))
373 373
374 374 self.tags() # instantiate the cache
375 375 self._tag(names, node, message, local, user, date)
376 376
377 377 @propertycache
378 378 def _tagscache(self):
379 379 '''Returns a tagscache object that contains various tags related caches.'''
380 380
381 381 # This simplifies its cache management by having one decorated
382 382 # function (this one) and the rest simply fetch things from it.
383 383 class tagscache(object):
384 384 def __init__(self):
385 385 # These two define the set of tags for this repository. tags
386 386 # maps tag name to node; tagtypes maps tag name to 'global' or
387 387 # 'local'. (Global tags are defined by .hgtags across all
388 388 # heads, and local tags are defined in .hg/localtags.)
389 389 # They constitute the in-memory cache of tags.
390 390 self.tags = self.tagtypes = None
391 391
392 392 self.nodetagscache = self.tagslist = None
393 393
394 394 cache = tagscache()
395 395 cache.tags, cache.tagtypes = self._findtags()
396 396
397 397 return cache
398 398
399 399 def tags(self):
400 400 '''return a mapping of tag to node'''
401 401 t = {}
402 402 for k, v in self._tagscache.tags.iteritems():
403 403 try:
404 404 # ignore tags to unknown nodes
405 405 self.changelog.rev(v)
406 406 t[k] = v
407 407 except error.LookupError:
408 408 pass
409 409 return t
410 410
411 411 def _findtags(self):
412 412 '''Do the hard work of finding tags. Return a pair of dicts
413 413 (tags, tagtypes) where tags maps tag name to node, and tagtypes
414 414 maps tag name to a string like \'global\' or \'local\'.
415 415 Subclasses or extensions are free to add their own tags, but
416 416 should be aware that the returned dicts will be retained for the
417 417 duration of the localrepo object.'''
418 418
419 419 # XXX what tagtype should subclasses/extensions use? Currently
420 420 # mq and bookmarks add tags, but do not set the tagtype at all.
421 421 # Should each extension invent its own tag type? Should there
422 422 # be one tagtype for all such "virtual" tags? Or is the status
423 423 # quo fine?
424 424
425 425 alltags = {} # map tag name to (node, hist)
426 426 tagtypes = {}
427 427
428 428 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
429 429 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
430 430
431 431 # Build the return dicts. Have to re-encode tag names because
432 432 # the tags module always uses UTF-8 (in order not to lose info
433 433 # writing to the cache), but the rest of Mercurial wants them in
434 434 # local encoding.
435 435 tags = {}
436 436 for (name, (node, hist)) in alltags.iteritems():
437 437 if node != nullid:
438 438 tags[encoding.tolocal(name)] = node
439 439 tags['tip'] = self.changelog.tip()
440 440 tagtypes = dict([(encoding.tolocal(name), value)
441 441 for (name, value) in tagtypes.iteritems()])
442 442 return (tags, tagtypes)
443 443
444 444 def tagtype(self, tagname):
445 445 '''
446 446 return the type of the given tag. result can be:
447 447
448 448 'local' : a local tag
449 449 'global' : a global tag
450 450 None : tag does not exist
451 451 '''
452 452
453 453 return self._tagscache.tagtypes.get(tagname)
454 454
455 455 def tagslist(self):
456 456 '''return a list of tags ordered by revision'''
457 457 if not self._tagscache.tagslist:
458 458 l = []
459 459 for t, n in self.tags().iteritems():
460 460 r = self.changelog.rev(n)
461 461 l.append((r, t, n))
462 462 self._tagscache.tagslist = [(t, n) for r, t, n in sorted(l)]
463 463
464 464 return self._tagscache.tagslist
465 465
466 466 def nodetags(self, node):
467 467 '''return the tags associated with a node'''
468 468 if not self._tagscache.nodetagscache:
469 469 nodetagscache = {}
470 470 for t, n in self._tagscache.tags.iteritems():
471 471 nodetagscache.setdefault(n, []).append(t)
472 472 for tags in nodetagscache.itervalues():
473 473 tags.sort()
474 474 self._tagscache.nodetagscache = nodetagscache
475 475 return self._tagscache.nodetagscache.get(node, [])
476 476
477 477 def nodebookmarks(self, node):
478 478 marks = []
479 479 for bookmark, n in self._bookmarks.iteritems():
480 480 if n == node:
481 481 marks.append(bookmark)
482 482 return sorted(marks)
483 483
484 484 def _branchtags(self, partial, lrev):
485 485 # TODO: rename this function?
486 486 tiprev = len(self) - 1
487 487 if lrev != tiprev:
488 488 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
489 489 self._updatebranchcache(partial, ctxgen)
490 490 self._writebranchcache(partial, self.changelog.tip(), tiprev)
491 491
492 492 return partial
493 493
494 494 def updatebranchcache(self):
495 495 tip = self.changelog.tip()
496 496 if self._branchcache is not None and self._branchcachetip == tip:
497 497 return
498 498
499 499 oldtip = self._branchcachetip
500 500 self._branchcachetip = tip
501 501 if oldtip is None or oldtip not in self.changelog.nodemap:
502 502 partial, last, lrev = self._readbranchcache()
503 503 else:
504 504 lrev = self.changelog.rev(oldtip)
505 505 partial = self._branchcache
506 506
507 507 self._branchtags(partial, lrev)
508 508 # this private cache holds all heads (not just tips)
509 509 self._branchcache = partial
510 510
511 511 def branchmap(self):
512 512 '''returns a dictionary {branch: [branchheads]}'''
513 513 self.updatebranchcache()
514 514 return self._branchcache
515 515
516 516 def branchtags(self):
517 517 '''return a dict where branch names map to the tipmost head of
518 518 the branch, open heads come before closed'''
519 519 bt = {}
520 520 for bn, heads in self.branchmap().iteritems():
521 521 tip = heads[-1]
522 522 for h in reversed(heads):
523 523 if 'close' not in self.changelog.read(h)[5]:
524 524 tip = h
525 525 break
526 526 bt[bn] = tip
527 527 return bt
528 528
529 529 def _readbranchcache(self):
530 530 partial = {}
531 531 try:
532 532 f = self.opener("cache/branchheads")
533 533 lines = f.read().split('\n')
534 534 f.close()
535 535 except (IOError, OSError):
536 536 return {}, nullid, nullrev
537 537
538 538 try:
539 539 last, lrev = lines.pop(0).split(" ", 1)
540 540 last, lrev = bin(last), int(lrev)
541 541 if lrev >= len(self) or self[lrev].node() != last:
542 542 # invalidate the cache
543 543 raise ValueError('invalidating branch cache (tip differs)')
544 544 for l in lines:
545 545 if not l:
546 546 continue
547 547 node, label = l.split(" ", 1)
548 548 label = encoding.tolocal(label.strip())
549 549 partial.setdefault(label, []).append(bin(node))
550 550 except KeyboardInterrupt:
551 551 raise
552 552 except Exception, inst:
553 553 if self.ui.debugflag:
554 554 self.ui.warn(str(inst), '\n')
555 555 partial, last, lrev = {}, nullid, nullrev
556 556 return partial, last, lrev
557 557
558 558 def _writebranchcache(self, branches, tip, tiprev):
559 559 try:
560 560 f = self.opener("cache/branchheads", "w", atomictemp=True)
561 561 f.write("%s %s\n" % (hex(tip), tiprev))
562 562 for label, nodes in branches.iteritems():
563 563 for node in nodes:
564 564 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
565 565 f.close()
566 566 except (IOError, OSError):
567 567 pass
568 568
569 569 def _updatebranchcache(self, partial, ctxgen):
570 570 # collect new branch entries
571 571 newbranches = {}
572 572 for c in ctxgen:
573 573 newbranches.setdefault(c.branch(), []).append(c.node())
574 574 # if older branchheads are reachable from new ones, they aren't
575 575 # really branchheads. Note checking parents is insufficient:
576 576 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
577 577 for branch, newnodes in newbranches.iteritems():
578 578 bheads = partial.setdefault(branch, [])
579 579 bheads.extend(newnodes)
580 580 if len(bheads) <= 1:
581 581 continue
582 582 bheads = sorted(bheads, key=lambda x: self[x].rev())
583 583 # starting from tip means fewer passes over reachable
584 584 while newnodes:
585 585 latest = newnodes.pop()
586 586 if latest not in bheads:
587 587 continue
588 588 minbhrev = self[bheads[0]].node()
589 589 reachable = self.changelog.reachable(latest, minbhrev)
590 590 reachable.remove(latest)
591 591 if reachable:
592 592 bheads = [b for b in bheads if b not in reachable]
593 593 partial[branch] = bheads
594 594
595 595 def lookup(self, key):
596 if isinstance(key, int):
597 return self.changelog.node(key)
598 elif key == '.':
599 return self.dirstate.p1()
600 elif key == 'null':
601 return nullid
602 elif key == 'tip':
603 return self.changelog.tip()
604 n = self.changelog._match(key)
605 if n:
606 return n
607 if key in self._bookmarks:
608 return self._bookmarks[key]
609 if key in self.tags():
610 return self.tags()[key]
611 if key in self.branchtags():
612 return self.branchtags()[key]
613 n = self.changelog._partialmatch(key)
614 if n:
615 return n
616
617 # can't find key, check if it might have come from damaged dirstate
618 if key in self.dirstate.parents():
619 raise error.Abort(_("working directory has unknown parent '%s'!")
620 % short(key))
621 try:
622 if len(key) == 20:
623 key = hex(key)
624 except TypeError:
625 pass
626 raise error.RepoLookupError(_("unknown revision '%s'") % key)
596 return self[key].node()
627 597
628 598 def lookupbranch(self, key, remote=None):
629 599 repo = remote or self
630 600 if key in repo.branchmap():
631 601 return key
632 602
633 603 repo = (remote and remote.local()) and remote or self
634 604 return repo[key].branch()
635 605
636 606 def known(self, nodes):
637 607 nm = self.changelog.nodemap
638 608 result = []
639 609 for n in nodes:
640 610 r = nm.get(n)
641 611 resp = not (r is None or self._phaserev[r] >= phases.secret)
642 612 result.append(resp)
643 613 return result
644 614
645 615 def local(self):
646 616 return self
647 617
648 618 def join(self, f):
649 619 return os.path.join(self.path, f)
650 620
651 621 def wjoin(self, f):
652 622 return os.path.join(self.root, f)
653 623
654 624 def file(self, f):
655 625 if f[0] == '/':
656 626 f = f[1:]
657 627 return filelog.filelog(self.sopener, f)
658 628
659 629 def changectx(self, changeid):
660 630 return self[changeid]
661 631
662 632 def parents(self, changeid=None):
663 633 '''get list of changectxs for parents of changeid'''
664 634 return self[changeid].parents()
665 635
666 636 def filectx(self, path, changeid=None, fileid=None):
667 637 """changeid can be a changeset revision, node, or tag.
668 638 fileid can be a file revision or node."""
669 639 return context.filectx(self, path, changeid, fileid)
670 640
671 641 def getcwd(self):
672 642 return self.dirstate.getcwd()
673 643
674 644 def pathto(self, f, cwd=None):
675 645 return self.dirstate.pathto(f, cwd)
676 646
677 647 def wfile(self, f, mode='r'):
678 648 return self.wopener(f, mode)
679 649
680 650 def _link(self, f):
681 651 return os.path.islink(self.wjoin(f))
682 652
683 653 def _loadfilter(self, filter):
684 654 if filter not in self.filterpats:
685 655 l = []
686 656 for pat, cmd in self.ui.configitems(filter):
687 657 if cmd == '!':
688 658 continue
689 659 mf = matchmod.match(self.root, '', [pat])
690 660 fn = None
691 661 params = cmd
692 662 for name, filterfn in self._datafilters.iteritems():
693 663 if cmd.startswith(name):
694 664 fn = filterfn
695 665 params = cmd[len(name):].lstrip()
696 666 break
697 667 if not fn:
698 668 fn = lambda s, c, **kwargs: util.filter(s, c)
699 669 # Wrap old filters not supporting keyword arguments
700 670 if not inspect.getargspec(fn)[2]:
701 671 oldfn = fn
702 672 fn = lambda s, c, **kwargs: oldfn(s, c)
703 673 l.append((mf, fn, params))
704 674 self.filterpats[filter] = l
705 675 return self.filterpats[filter]
706 676
707 677 def _filter(self, filterpats, filename, data):
708 678 for mf, fn, cmd in filterpats:
709 679 if mf(filename):
710 680 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
711 681 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
712 682 break
713 683
714 684 return data
715 685
716 686 @propertycache
717 687 def _encodefilterpats(self):
718 688 return self._loadfilter('encode')
719 689
720 690 @propertycache
721 691 def _decodefilterpats(self):
722 692 return self._loadfilter('decode')
723 693
724 694 def adddatafilter(self, name, filter):
725 695 self._datafilters[name] = filter
726 696
727 697 def wread(self, filename):
728 698 if self._link(filename):
729 699 data = os.readlink(self.wjoin(filename))
730 700 else:
731 701 data = self.wopener.read(filename)
732 702 return self._filter(self._encodefilterpats, filename, data)
733 703
734 704 def wwrite(self, filename, data, flags):
735 705 data = self._filter(self._decodefilterpats, filename, data)
736 706 if 'l' in flags:
737 707 self.wopener.symlink(data, filename)
738 708 else:
739 709 self.wopener.write(filename, data)
740 710 if 'x' in flags:
741 711 util.setflags(self.wjoin(filename), False, True)
742 712
743 713 def wwritedata(self, filename, data):
744 714 return self._filter(self._decodefilterpats, filename, data)
745 715
746 716 def transaction(self, desc):
747 717 tr = self._transref and self._transref() or None
748 718 if tr and tr.running():
749 719 return tr.nest()
750 720
751 721 # abort here if the journal already exists
752 722 if os.path.exists(self.sjoin("journal")):
753 723 raise error.RepoError(
754 724 _("abandoned transaction found - run hg recover"))
755 725
756 726 self._writejournal(desc)
757 727 renames = [(x, undoname(x)) for x in self._journalfiles()]
758 728
759 729 tr = transaction.transaction(self.ui.warn, self.sopener,
760 730 self.sjoin("journal"),
761 731 aftertrans(renames),
762 732 self.store.createmode)
763 733 self._transref = weakref.ref(tr)
764 734 return tr
765 735
766 736 def _journalfiles(self):
767 737 return (self.sjoin('journal'), self.join('journal.dirstate'),
768 738 self.join('journal.branch'), self.join('journal.desc'),
769 739 self.join('journal.bookmarks'),
770 740 self.sjoin('journal.phaseroots'))
771 741
772 742 def undofiles(self):
773 743 return [undoname(x) for x in self._journalfiles()]
774 744
775 745 def _writejournal(self, desc):
776 746 # save dirstate for rollback
777 747 try:
778 748 ds = self.opener.read("dirstate")
779 749 except IOError:
780 750 ds = ""
781 751 self.opener.write("journal.dirstate", ds)
782 752 self.opener.write("journal.branch",
783 753 encoding.fromlocal(self.dirstate.branch()))
784 754 self.opener.write("journal.desc",
785 755 "%d\n%s\n" % (len(self), desc))
786 756
787 757 try:
788 758 bk = self.opener.read("bookmarks")
789 759 except IOError:
790 760 bk = ""
791 761 self.opener.write("journal.bookmarks", bk)
792 762
793 763 phasesname = self.sjoin('phaseroots')
794 764 if os.path.exists(phasesname):
795 765 util.copyfile(phasesname, self.sjoin('journal.phaseroots'))
796 766 else:
797 767 self.sopener.write('journal.phaseroots', '')
798 768
799 769 def recover(self):
800 770 lock = self.lock()
801 771 try:
802 772 if os.path.exists(self.sjoin("journal")):
803 773 self.ui.status(_("rolling back interrupted transaction\n"))
804 774 transaction.rollback(self.sopener, self.sjoin("journal"),
805 775 self.ui.warn)
806 776 self.invalidate()
807 777 return True
808 778 else:
809 779 self.ui.warn(_("no interrupted transaction available\n"))
810 780 return False
811 781 finally:
812 782 lock.release()
813 783
814 784 def rollback(self, dryrun=False, force=False):
815 785 wlock = lock = None
816 786 try:
817 787 wlock = self.wlock()
818 788 lock = self.lock()
819 789 if os.path.exists(self.sjoin("undo")):
820 790 return self._rollback(dryrun, force)
821 791 else:
822 792 self.ui.warn(_("no rollback information available\n"))
823 793 return 1
824 794 finally:
825 795 release(lock, wlock)
826 796
827 797 def _rollback(self, dryrun, force):
828 798 ui = self.ui
829 799 try:
830 800 args = self.opener.read('undo.desc').splitlines()
831 801 (oldlen, desc, detail) = (int(args[0]), args[1], None)
832 802 if len(args) >= 3:
833 803 detail = args[2]
834 804 oldtip = oldlen - 1
835 805
836 806 if detail and ui.verbose:
837 807 msg = (_('repository tip rolled back to revision %s'
838 808 ' (undo %s: %s)\n')
839 809 % (oldtip, desc, detail))
840 810 else:
841 811 msg = (_('repository tip rolled back to revision %s'
842 812 ' (undo %s)\n')
843 813 % (oldtip, desc))
844 814 except IOError:
845 815 msg = _('rolling back unknown transaction\n')
846 816 desc = None
847 817
848 818 if not force and self['.'] != self['tip'] and desc == 'commit':
849 819 raise util.Abort(
850 820 _('rollback of last commit while not checked out '
851 821 'may lose data'), hint=_('use -f to force'))
852 822
853 823 ui.status(msg)
854 824 if dryrun:
855 825 return 0
856 826
857 827 parents = self.dirstate.parents()
858 828 transaction.rollback(self.sopener, self.sjoin('undo'), ui.warn)
859 829 if os.path.exists(self.join('undo.bookmarks')):
860 830 util.rename(self.join('undo.bookmarks'),
861 831 self.join('bookmarks'))
862 832 if os.path.exists(self.sjoin('undo.phaseroots')):
863 833 util.rename(self.sjoin('undo.phaseroots'),
864 834 self.sjoin('phaseroots'))
865 835 self.invalidate()
866 836
867 837 parentgone = (parents[0] not in self.changelog.nodemap or
868 838 parents[1] not in self.changelog.nodemap)
869 839 if parentgone:
870 840 util.rename(self.join('undo.dirstate'), self.join('dirstate'))
871 841 try:
872 842 branch = self.opener.read('undo.branch')
873 843 self.dirstate.setbranch(branch)
874 844 except IOError:
875 845 ui.warn(_('named branch could not be reset: '
876 846 'current branch is still \'%s\'\n')
877 847 % self.dirstate.branch())
878 848
879 849 self.dirstate.invalidate()
880 850 parents = tuple([p.rev() for p in self.parents()])
881 851 if len(parents) > 1:
882 852 ui.status(_('working directory now based on '
883 853 'revisions %d and %d\n') % parents)
884 854 else:
885 855 ui.status(_('working directory now based on '
886 856 'revision %d\n') % parents)
887 857 self.destroyed()
888 858 return 0
889 859
890 860 def invalidatecaches(self):
891 861 def delcache(name):
892 862 try:
893 863 delattr(self, name)
894 864 except AttributeError:
895 865 pass
896 866
897 867 delcache('_tagscache')
898 868 delcache('_phaserev')
899 869
900 870 self._branchcache = None # in UTF-8
901 871 self._branchcachetip = None
902 872
903 873 def invalidatedirstate(self):
904 874 '''Invalidates the dirstate, causing the next call to dirstate
905 875 to check if it was modified since the last time it was read,
906 876 rereading it if it has.
907 877
908 878 This is different to dirstate.invalidate() that it doesn't always
909 879 rereads the dirstate. Use dirstate.invalidate() if you want to
910 880 explicitly read the dirstate again (i.e. restoring it to a previous
911 881 known good state).'''
912 882 if 'dirstate' in self.__dict__:
913 883 for k in self.dirstate._filecache:
914 884 try:
915 885 delattr(self.dirstate, k)
916 886 except AttributeError:
917 887 pass
918 888 delattr(self, 'dirstate')
919 889
920 890 def invalidate(self):
921 891 for k in self._filecache:
922 892 # dirstate is invalidated separately in invalidatedirstate()
923 893 if k == 'dirstate':
924 894 continue
925 895
926 896 try:
927 897 delattr(self, k)
928 898 except AttributeError:
929 899 pass
930 900 self.invalidatecaches()
931 901
932 902 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
933 903 try:
934 904 l = lock.lock(lockname, 0, releasefn, desc=desc)
935 905 except error.LockHeld, inst:
936 906 if not wait:
937 907 raise
938 908 self.ui.warn(_("waiting for lock on %s held by %r\n") %
939 909 (desc, inst.locker))
940 910 # default to 600 seconds timeout
941 911 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
942 912 releasefn, desc=desc)
943 913 if acquirefn:
944 914 acquirefn()
945 915 return l
946 916
947 917 def _afterlock(self, callback):
948 918 """add a callback to the current repository lock.
949 919
950 920 The callback will be executed on lock release."""
951 921 l = self._lockref and self._lockref()
952 922 if l:
953 923 l.postrelease.append(callback)
954 924
955 925 def lock(self, wait=True):
956 926 '''Lock the repository store (.hg/store) and return a weak reference
957 927 to the lock. Use this before modifying the store (e.g. committing or
958 928 stripping). If you are opening a transaction, get a lock as well.)'''
959 929 l = self._lockref and self._lockref()
960 930 if l is not None and l.held:
961 931 l.lock()
962 932 return l
963 933
964 934 def unlock():
965 935 self.store.write()
966 936 if self._dirtyphases:
967 937 phases.writeroots(self)
968 938 self._dirtyphases = False
969 939 for k, ce in self._filecache.items():
970 940 if k == 'dirstate':
971 941 continue
972 942 ce.refresh()
973 943
974 944 l = self._lock(self.sjoin("lock"), wait, unlock,
975 945 self.invalidate, _('repository %s') % self.origroot)
976 946 self._lockref = weakref.ref(l)
977 947 return l
978 948
979 949 def wlock(self, wait=True):
980 950 '''Lock the non-store parts of the repository (everything under
981 951 .hg except .hg/store) and return a weak reference to the lock.
982 952 Use this before modifying files in .hg.'''
983 953 l = self._wlockref and self._wlockref()
984 954 if l is not None and l.held:
985 955 l.lock()
986 956 return l
987 957
988 958 def unlock():
989 959 self.dirstate.write()
990 960 ce = self._filecache.get('dirstate')
991 961 if ce:
992 962 ce.refresh()
993 963
994 964 l = self._lock(self.join("wlock"), wait, unlock,
995 965 self.invalidatedirstate, _('working directory of %s') %
996 966 self.origroot)
997 967 self._wlockref = weakref.ref(l)
998 968 return l
999 969
1000 970 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
1001 971 """
1002 972 commit an individual file as part of a larger transaction
1003 973 """
1004 974
1005 975 fname = fctx.path()
1006 976 text = fctx.data()
1007 977 flog = self.file(fname)
1008 978 fparent1 = manifest1.get(fname, nullid)
1009 979 fparent2 = fparent2o = manifest2.get(fname, nullid)
1010 980
1011 981 meta = {}
1012 982 copy = fctx.renamed()
1013 983 if copy and copy[0] != fname:
1014 984 # Mark the new revision of this file as a copy of another
1015 985 # file. This copy data will effectively act as a parent
1016 986 # of this new revision. If this is a merge, the first
1017 987 # parent will be the nullid (meaning "look up the copy data")
1018 988 # and the second one will be the other parent. For example:
1019 989 #
1020 990 # 0 --- 1 --- 3 rev1 changes file foo
1021 991 # \ / rev2 renames foo to bar and changes it
1022 992 # \- 2 -/ rev3 should have bar with all changes and
1023 993 # should record that bar descends from
1024 994 # bar in rev2 and foo in rev1
1025 995 #
1026 996 # this allows this merge to succeed:
1027 997 #
1028 998 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
1029 999 # \ / merging rev3 and rev4 should use bar@rev2
1030 1000 # \- 2 --- 4 as the merge base
1031 1001 #
1032 1002
1033 1003 cfname = copy[0]
1034 1004 crev = manifest1.get(cfname)
1035 1005 newfparent = fparent2
1036 1006
1037 1007 if manifest2: # branch merge
1038 1008 if fparent2 == nullid or crev is None: # copied on remote side
1039 1009 if cfname in manifest2:
1040 1010 crev = manifest2[cfname]
1041 1011 newfparent = fparent1
1042 1012
1043 1013 # find source in nearest ancestor if we've lost track
1044 1014 if not crev:
1045 1015 self.ui.debug(" %s: searching for copy revision for %s\n" %
1046 1016 (fname, cfname))
1047 1017 for ancestor in self[None].ancestors():
1048 1018 if cfname in ancestor:
1049 1019 crev = ancestor[cfname].filenode()
1050 1020 break
1051 1021
1052 1022 if crev:
1053 1023 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1054 1024 meta["copy"] = cfname
1055 1025 meta["copyrev"] = hex(crev)
1056 1026 fparent1, fparent2 = nullid, newfparent
1057 1027 else:
1058 1028 self.ui.warn(_("warning: can't find ancestor for '%s' "
1059 1029 "copied from '%s'!\n") % (fname, cfname))
1060 1030
1061 1031 elif fparent2 != nullid:
1062 1032 # is one parent an ancestor of the other?
1063 1033 fparentancestor = flog.ancestor(fparent1, fparent2)
1064 1034 if fparentancestor == fparent1:
1065 1035 fparent1, fparent2 = fparent2, nullid
1066 1036 elif fparentancestor == fparent2:
1067 1037 fparent2 = nullid
1068 1038
1069 1039 # is the file changed?
1070 1040 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1071 1041 changelist.append(fname)
1072 1042 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1073 1043
1074 1044 # are just the flags changed during merge?
1075 1045 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
1076 1046 changelist.append(fname)
1077 1047
1078 1048 return fparent1
1079 1049
1080 1050 def commit(self, text="", user=None, date=None, match=None, force=False,
1081 1051 editor=False, extra={}):
1082 1052 """Add a new revision to current repository.
1083 1053
1084 1054 Revision information is gathered from the working directory,
1085 1055 match can be used to filter the committed files. If editor is
1086 1056 supplied, it is called to get a commit message.
1087 1057 """
1088 1058
1089 1059 def fail(f, msg):
1090 1060 raise util.Abort('%s: %s' % (f, msg))
1091 1061
1092 1062 if not match:
1093 1063 match = matchmod.always(self.root, '')
1094 1064
1095 1065 if not force:
1096 1066 vdirs = []
1097 1067 match.dir = vdirs.append
1098 1068 match.bad = fail
1099 1069
1100 1070 wlock = self.wlock()
1101 1071 try:
1102 1072 wctx = self[None]
1103 1073 merge = len(wctx.parents()) > 1
1104 1074
1105 1075 if (not force and merge and match and
1106 1076 (match.files() or match.anypats())):
1107 1077 raise util.Abort(_('cannot partially commit a merge '
1108 1078 '(do not specify files or patterns)'))
1109 1079
1110 1080 changes = self.status(match=match, clean=force)
1111 1081 if force:
1112 1082 changes[0].extend(changes[6]) # mq may commit unchanged files
1113 1083
1114 1084 # check subrepos
1115 1085 subs = []
1116 1086 commitsubs = set()
1117 1087 newstate = wctx.substate.copy()
1118 1088 # only manage subrepos and .hgsubstate if .hgsub is present
1119 1089 if '.hgsub' in wctx:
1120 1090 # we'll decide whether to track this ourselves, thanks
1121 1091 if '.hgsubstate' in changes[0]:
1122 1092 changes[0].remove('.hgsubstate')
1123 1093 if '.hgsubstate' in changes[2]:
1124 1094 changes[2].remove('.hgsubstate')
1125 1095
1126 1096 # compare current state to last committed state
1127 1097 # build new substate based on last committed state
1128 1098 oldstate = wctx.p1().substate
1129 1099 for s in sorted(newstate.keys()):
1130 1100 if not match(s):
1131 1101 # ignore working copy, use old state if present
1132 1102 if s in oldstate:
1133 1103 newstate[s] = oldstate[s]
1134 1104 continue
1135 1105 if not force:
1136 1106 raise util.Abort(
1137 1107 _("commit with new subrepo %s excluded") % s)
1138 1108 if wctx.sub(s).dirty(True):
1139 1109 if not self.ui.configbool('ui', 'commitsubrepos'):
1140 1110 raise util.Abort(
1141 1111 _("uncommitted changes in subrepo %s") % s,
1142 1112 hint=_("use --subrepos for recursive commit"))
1143 1113 subs.append(s)
1144 1114 commitsubs.add(s)
1145 1115 else:
1146 1116 bs = wctx.sub(s).basestate()
1147 1117 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1148 1118 if oldstate.get(s, (None, None, None))[1] != bs:
1149 1119 subs.append(s)
1150 1120
1151 1121 # check for removed subrepos
1152 1122 for p in wctx.parents():
1153 1123 r = [s for s in p.substate if s not in newstate]
1154 1124 subs += [s for s in r if match(s)]
1155 1125 if subs:
1156 1126 if (not match('.hgsub') and
1157 1127 '.hgsub' in (wctx.modified() + wctx.added())):
1158 1128 raise util.Abort(
1159 1129 _("can't commit subrepos without .hgsub"))
1160 1130 changes[0].insert(0, '.hgsubstate')
1161 1131
1162 1132 elif '.hgsub' in changes[2]:
1163 1133 # clean up .hgsubstate when .hgsub is removed
1164 1134 if ('.hgsubstate' in wctx and
1165 1135 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
1166 1136 changes[2].insert(0, '.hgsubstate')
1167 1137
1168 1138 # make sure all explicit patterns are matched
1169 1139 if not force and match.files():
1170 1140 matched = set(changes[0] + changes[1] + changes[2])
1171 1141
1172 1142 for f in match.files():
1173 1143 if f == '.' or f in matched or f in wctx.substate:
1174 1144 continue
1175 1145 if f in changes[3]: # missing
1176 1146 fail(f, _('file not found!'))
1177 1147 if f in vdirs: # visited directory
1178 1148 d = f + '/'
1179 1149 for mf in matched:
1180 1150 if mf.startswith(d):
1181 1151 break
1182 1152 else:
1183 1153 fail(f, _("no match under directory!"))
1184 1154 elif f not in self.dirstate:
1185 1155 fail(f, _("file not tracked!"))
1186 1156
1187 1157 if (not force and not extra.get("close") and not merge
1188 1158 and not (changes[0] or changes[1] or changes[2])
1189 1159 and wctx.branch() == wctx.p1().branch()):
1190 1160 return None
1191 1161
1192 1162 ms = mergemod.mergestate(self)
1193 1163 for f in changes[0]:
1194 1164 if f in ms and ms[f] == 'u':
1195 1165 raise util.Abort(_("unresolved merge conflicts "
1196 1166 "(see hg help resolve)"))
1197 1167
1198 1168 cctx = context.workingctx(self, text, user, date, extra, changes)
1199 1169 if editor:
1200 1170 cctx._text = editor(self, cctx, subs)
1201 1171 edited = (text != cctx._text)
1202 1172
1203 1173 # commit subs and write new state
1204 1174 if subs:
1205 1175 for s in sorted(commitsubs):
1206 1176 sub = wctx.sub(s)
1207 1177 self.ui.status(_('committing subrepository %s\n') %
1208 1178 subrepo.subrelpath(sub))
1209 1179 sr = sub.commit(cctx._text, user, date)
1210 1180 newstate[s] = (newstate[s][0], sr)
1211 1181 subrepo.writestate(self, newstate)
1212 1182
1213 1183 # Save commit message in case this transaction gets rolled back
1214 1184 # (e.g. by a pretxncommit hook). Leave the content alone on
1215 1185 # the assumption that the user will use the same editor again.
1216 1186 msgfn = self.savecommitmessage(cctx._text)
1217 1187
1218 1188 p1, p2 = self.dirstate.parents()
1219 1189 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1220 1190 try:
1221 1191 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1222 1192 ret = self.commitctx(cctx, True)
1223 1193 except:
1224 1194 if edited:
1225 1195 self.ui.write(
1226 1196 _('note: commit message saved in %s\n') % msgfn)
1227 1197 raise
1228 1198
1229 1199 # update bookmarks, dirstate and mergestate
1230 1200 bookmarks.update(self, p1, ret)
1231 1201 for f in changes[0] + changes[1]:
1232 1202 self.dirstate.normal(f)
1233 1203 for f in changes[2]:
1234 1204 self.dirstate.drop(f)
1235 1205 self.dirstate.setparents(ret)
1236 1206 ms.reset()
1237 1207 finally:
1238 1208 wlock.release()
1239 1209
1240 1210 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1241 1211 return ret
1242 1212
1243 1213 def commitctx(self, ctx, error=False):
1244 1214 """Add a new revision to current repository.
1245 1215 Revision information is passed via the context argument.
1246 1216 """
1247 1217
1248 1218 tr = lock = None
1249 1219 removed = list(ctx.removed())
1250 1220 p1, p2 = ctx.p1(), ctx.p2()
1251 1221 user = ctx.user()
1252 1222
1253 1223 lock = self.lock()
1254 1224 try:
1255 1225 tr = self.transaction("commit")
1256 1226 trp = weakref.proxy(tr)
1257 1227
1258 1228 if ctx.files():
1259 1229 m1 = p1.manifest().copy()
1260 1230 m2 = p2.manifest()
1261 1231
1262 1232 # check in files
1263 1233 new = {}
1264 1234 changed = []
1265 1235 linkrev = len(self)
1266 1236 for f in sorted(ctx.modified() + ctx.added()):
1267 1237 self.ui.note(f + "\n")
1268 1238 try:
1269 1239 fctx = ctx[f]
1270 1240 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1271 1241 changed)
1272 1242 m1.set(f, fctx.flags())
1273 1243 except OSError, inst:
1274 1244 self.ui.warn(_("trouble committing %s!\n") % f)
1275 1245 raise
1276 1246 except IOError, inst:
1277 1247 errcode = getattr(inst, 'errno', errno.ENOENT)
1278 1248 if error or errcode and errcode != errno.ENOENT:
1279 1249 self.ui.warn(_("trouble committing %s!\n") % f)
1280 1250 raise
1281 1251 else:
1282 1252 removed.append(f)
1283 1253
1284 1254 # update manifest
1285 1255 m1.update(new)
1286 1256 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1287 1257 drop = [f for f in removed if f in m1]
1288 1258 for f in drop:
1289 1259 del m1[f]
1290 1260 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1291 1261 p2.manifestnode(), (new, drop))
1292 1262 files = changed + removed
1293 1263 else:
1294 1264 mn = p1.manifestnode()
1295 1265 files = []
1296 1266
1297 1267 # update changelog
1298 1268 self.changelog.delayupdate()
1299 1269 n = self.changelog.add(mn, files, ctx.description(),
1300 1270 trp, p1.node(), p2.node(),
1301 1271 user, ctx.date(), ctx.extra().copy())
1302 1272 p = lambda: self.changelog.writepending() and self.root or ""
1303 1273 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1304 1274 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1305 1275 parent2=xp2, pending=p)
1306 1276 self.changelog.finalize(trp)
1307 1277 # set the new commit is proper phase
1308 1278 targetphase = phases.newcommitphase(self.ui)
1309 1279 if targetphase:
1310 1280 # retract boundary do not alter parent changeset.
1311 1281 # if a parent have higher the resulting phase will
1312 1282 # be compliant anyway
1313 1283 #
1314 1284 # if minimal phase was 0 we don't need to retract anything
1315 1285 phases.retractboundary(self, targetphase, [n])
1316 1286 tr.close()
1317 1287 self.updatebranchcache()
1318 1288 return n
1319 1289 finally:
1320 1290 if tr:
1321 1291 tr.release()
1322 1292 lock.release()
1323 1293
1324 1294 def destroyed(self):
1325 1295 '''Inform the repository that nodes have been destroyed.
1326 1296 Intended for use by strip and rollback, so there's a common
1327 1297 place for anything that has to be done after destroying history.'''
1328 1298 # XXX it might be nice if we could take the list of destroyed
1329 1299 # nodes, but I don't see an easy way for rollback() to do that
1330 1300
1331 1301 # Ensure the persistent tag cache is updated. Doing it now
1332 1302 # means that the tag cache only has to worry about destroyed
1333 1303 # heads immediately after a strip/rollback. That in turn
1334 1304 # guarantees that "cachetip == currenttip" (comparing both rev
1335 1305 # and node) always means no nodes have been added or destroyed.
1336 1306
1337 1307 # XXX this is suboptimal when qrefresh'ing: we strip the current
1338 1308 # head, refresh the tag cache, then immediately add a new head.
1339 1309 # But I think doing it this way is necessary for the "instant
1340 1310 # tag cache retrieval" case to work.
1341 1311 self.invalidatecaches()
1342 1312
1343 1313 # Discard all cache entries to force reloading everything.
1344 1314 self._filecache.clear()
1345 1315
1346 1316 def walk(self, match, node=None):
1347 1317 '''
1348 1318 walk recursively through the directory tree or a given
1349 1319 changeset, finding all files matched by the match
1350 1320 function
1351 1321 '''
1352 1322 return self[node].walk(match)
1353 1323
1354 1324 def status(self, node1='.', node2=None, match=None,
1355 1325 ignored=False, clean=False, unknown=False,
1356 1326 listsubrepos=False):
1357 1327 """return status of files between two nodes or node and working directory
1358 1328
1359 1329 If node1 is None, use the first dirstate parent instead.
1360 1330 If node2 is None, compare node1 with working directory.
1361 1331 """
1362 1332
1363 1333 def mfmatches(ctx):
1364 1334 mf = ctx.manifest().copy()
1365 1335 for fn in mf.keys():
1366 1336 if not match(fn):
1367 1337 del mf[fn]
1368 1338 return mf
1369 1339
1370 1340 if isinstance(node1, context.changectx):
1371 1341 ctx1 = node1
1372 1342 else:
1373 1343 ctx1 = self[node1]
1374 1344 if isinstance(node2, context.changectx):
1375 1345 ctx2 = node2
1376 1346 else:
1377 1347 ctx2 = self[node2]
1378 1348
1379 1349 working = ctx2.rev() is None
1380 1350 parentworking = working and ctx1 == self['.']
1381 1351 match = match or matchmod.always(self.root, self.getcwd())
1382 1352 listignored, listclean, listunknown = ignored, clean, unknown
1383 1353
1384 1354 # load earliest manifest first for caching reasons
1385 1355 if not working and ctx2.rev() < ctx1.rev():
1386 1356 ctx2.manifest()
1387 1357
1388 1358 if not parentworking:
1389 1359 def bad(f, msg):
1390 1360 # 'f' may be a directory pattern from 'match.files()',
1391 1361 # so 'f not in ctx1' is not enough
1392 1362 if f not in ctx1 and f not in ctx1.dirs():
1393 1363 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1394 1364 match.bad = bad
1395 1365
1396 1366 if working: # we need to scan the working dir
1397 1367 subrepos = []
1398 1368 if '.hgsub' in self.dirstate:
1399 1369 subrepos = ctx2.substate.keys()
1400 1370 s = self.dirstate.status(match, subrepos, listignored,
1401 1371 listclean, listunknown)
1402 1372 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1403 1373
1404 1374 # check for any possibly clean files
1405 1375 if parentworking and cmp:
1406 1376 fixup = []
1407 1377 # do a full compare of any files that might have changed
1408 1378 for f in sorted(cmp):
1409 1379 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1410 1380 or ctx1[f].cmp(ctx2[f])):
1411 1381 modified.append(f)
1412 1382 else:
1413 1383 fixup.append(f)
1414 1384
1415 1385 # update dirstate for files that are actually clean
1416 1386 if fixup:
1417 1387 if listclean:
1418 1388 clean += fixup
1419 1389
1420 1390 try:
1421 1391 # updating the dirstate is optional
1422 1392 # so we don't wait on the lock
1423 1393 wlock = self.wlock(False)
1424 1394 try:
1425 1395 for f in fixup:
1426 1396 self.dirstate.normal(f)
1427 1397 finally:
1428 1398 wlock.release()
1429 1399 except error.LockError:
1430 1400 pass
1431 1401
1432 1402 if not parentworking:
1433 1403 mf1 = mfmatches(ctx1)
1434 1404 if working:
1435 1405 # we are comparing working dir against non-parent
1436 1406 # generate a pseudo-manifest for the working dir
1437 1407 mf2 = mfmatches(self['.'])
1438 1408 for f in cmp + modified + added:
1439 1409 mf2[f] = None
1440 1410 mf2.set(f, ctx2.flags(f))
1441 1411 for f in removed:
1442 1412 if f in mf2:
1443 1413 del mf2[f]
1444 1414 else:
1445 1415 # we are comparing two revisions
1446 1416 deleted, unknown, ignored = [], [], []
1447 1417 mf2 = mfmatches(ctx2)
1448 1418
1449 1419 modified, added, clean = [], [], []
1450 1420 for fn in mf2:
1451 1421 if fn in mf1:
1452 1422 if (fn not in deleted and
1453 1423 (mf1.flags(fn) != mf2.flags(fn) or
1454 1424 (mf1[fn] != mf2[fn] and
1455 1425 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1456 1426 modified.append(fn)
1457 1427 elif listclean:
1458 1428 clean.append(fn)
1459 1429 del mf1[fn]
1460 1430 elif fn not in deleted:
1461 1431 added.append(fn)
1462 1432 removed = mf1.keys()
1463 1433
1464 1434 if working and modified and not self.dirstate._checklink:
1465 1435 # Symlink placeholders may get non-symlink-like contents
1466 1436 # via user error or dereferencing by NFS or Samba servers,
1467 1437 # so we filter out any placeholders that don't look like a
1468 1438 # symlink
1469 1439 sane = []
1470 1440 for f in modified:
1471 1441 if ctx2.flags(f) == 'l':
1472 1442 d = ctx2[f].data()
1473 1443 if len(d) >= 1024 or '\n' in d or util.binary(d):
1474 1444 self.ui.debug('ignoring suspect symlink placeholder'
1475 1445 ' "%s"\n' % f)
1476 1446 continue
1477 1447 sane.append(f)
1478 1448 modified = sane
1479 1449
1480 1450 r = modified, added, removed, deleted, unknown, ignored, clean
1481 1451
1482 1452 if listsubrepos:
1483 1453 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1484 1454 if working:
1485 1455 rev2 = None
1486 1456 else:
1487 1457 rev2 = ctx2.substate[subpath][1]
1488 1458 try:
1489 1459 submatch = matchmod.narrowmatcher(subpath, match)
1490 1460 s = sub.status(rev2, match=submatch, ignored=listignored,
1491 1461 clean=listclean, unknown=listunknown,
1492 1462 listsubrepos=True)
1493 1463 for rfiles, sfiles in zip(r, s):
1494 1464 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1495 1465 except error.LookupError:
1496 1466 self.ui.status(_("skipping missing subrepository: %s\n")
1497 1467 % subpath)
1498 1468
1499 1469 for l in r:
1500 1470 l.sort()
1501 1471 return r
1502 1472
1503 1473 def heads(self, start=None):
1504 1474 heads = self.changelog.heads(start)
1505 1475 # sort the output in rev descending order
1506 1476 return sorted(heads, key=self.changelog.rev, reverse=True)
1507 1477
1508 1478 def branchheads(self, branch=None, start=None, closed=False):
1509 1479 '''return a (possibly filtered) list of heads for the given branch
1510 1480
1511 1481 Heads are returned in topological order, from newest to oldest.
1512 1482 If branch is None, use the dirstate branch.
1513 1483 If start is not None, return only heads reachable from start.
1514 1484 If closed is True, return heads that are marked as closed as well.
1515 1485 '''
1516 1486 if branch is None:
1517 1487 branch = self[None].branch()
1518 1488 branches = self.branchmap()
1519 1489 if branch not in branches:
1520 1490 return []
1521 1491 # the cache returns heads ordered lowest to highest
1522 1492 bheads = list(reversed(branches[branch]))
1523 1493 if start is not None:
1524 1494 # filter out the heads that cannot be reached from startrev
1525 1495 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1526 1496 bheads = [h for h in bheads if h in fbheads]
1527 1497 if not closed:
1528 1498 bheads = [h for h in bheads if
1529 1499 ('close' not in self.changelog.read(h)[5])]
1530 1500 return bheads
1531 1501
1532 1502 def branches(self, nodes):
1533 1503 if not nodes:
1534 1504 nodes = [self.changelog.tip()]
1535 1505 b = []
1536 1506 for n in nodes:
1537 1507 t = n
1538 1508 while True:
1539 1509 p = self.changelog.parents(n)
1540 1510 if p[1] != nullid or p[0] == nullid:
1541 1511 b.append((t, n, p[0], p[1]))
1542 1512 break
1543 1513 n = p[0]
1544 1514 return b
1545 1515
1546 1516 def between(self, pairs):
1547 1517 r = []
1548 1518
1549 1519 for top, bottom in pairs:
1550 1520 n, l, i = top, [], 0
1551 1521 f = 1
1552 1522
1553 1523 while n != bottom and n != nullid:
1554 1524 p = self.changelog.parents(n)[0]
1555 1525 if i == f:
1556 1526 l.append(n)
1557 1527 f = f * 2
1558 1528 n = p
1559 1529 i += 1
1560 1530
1561 1531 r.append(l)
1562 1532
1563 1533 return r
1564 1534
1565 1535 def pull(self, remote, heads=None, force=False):
1566 1536 lock = self.lock()
1567 1537 try:
1568 1538 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1569 1539 force=force)
1570 1540 common, fetch, rheads = tmp
1571 1541 if not fetch:
1572 1542 self.ui.status(_("no changes found\n"))
1573 1543 added = []
1574 1544 result = 0
1575 1545 else:
1576 1546 if heads is None and list(common) == [nullid]:
1577 1547 self.ui.status(_("requesting all changes\n"))
1578 1548 elif heads is None and remote.capable('changegroupsubset'):
1579 1549 # issue1320, avoid a race if remote changed after discovery
1580 1550 heads = rheads
1581 1551
1582 1552 if remote.capable('getbundle'):
1583 1553 cg = remote.getbundle('pull', common=common,
1584 1554 heads=heads or rheads)
1585 1555 elif heads is None:
1586 1556 cg = remote.changegroup(fetch, 'pull')
1587 1557 elif not remote.capable('changegroupsubset'):
1588 1558 raise util.Abort(_("partial pull cannot be done because "
1589 1559 "other repository doesn't support "
1590 1560 "changegroupsubset."))
1591 1561 else:
1592 1562 cg = remote.changegroupsubset(fetch, heads, 'pull')
1593 1563 clstart = len(self.changelog)
1594 1564 result = self.addchangegroup(cg, 'pull', remote.url())
1595 1565 clend = len(self.changelog)
1596 1566 added = [self.changelog.node(r) for r in xrange(clstart, clend)]
1597 1567
1598 1568 # compute target subset
1599 1569 if heads is None:
1600 1570 # We pulled every thing possible
1601 1571 # sync on everything common
1602 1572 subset = common + added
1603 1573 else:
1604 1574 # We pulled a specific subset
1605 1575 # sync on this subset
1606 1576 subset = heads
1607 1577
1608 1578 # Get remote phases data from remote
1609 1579 remotephases = remote.listkeys('phases')
1610 1580 publishing = bool(remotephases.get('publishing', False))
1611 1581 if remotephases and not publishing:
1612 1582 # remote is new and unpublishing
1613 1583 pheads, _dr = phases.analyzeremotephases(self, subset,
1614 1584 remotephases)
1615 1585 phases.advanceboundary(self, phases.public, pheads)
1616 1586 phases.advanceboundary(self, phases.draft, subset)
1617 1587 else:
1618 1588 # Remote is old or publishing all common changesets
1619 1589 # should be seen as public
1620 1590 phases.advanceboundary(self, phases.public, subset)
1621 1591 finally:
1622 1592 lock.release()
1623 1593
1624 1594 return result
1625 1595
1626 1596 def checkpush(self, force, revs):
1627 1597 """Extensions can override this function if additional checks have
1628 1598 to be performed before pushing, or call it if they override push
1629 1599 command.
1630 1600 """
1631 1601 pass
1632 1602
1633 1603 def push(self, remote, force=False, revs=None, newbranch=False):
1634 1604 '''Push outgoing changesets (limited by revs) from the current
1635 1605 repository to remote. Return an integer:
1636 1606 - None means nothing to push
1637 1607 - 0 means HTTP error
1638 1608 - 1 means we pushed and remote head count is unchanged *or*
1639 1609 we have outgoing changesets but refused to push
1640 1610 - other values as described by addchangegroup()
1641 1611 '''
1642 1612 # there are two ways to push to remote repo:
1643 1613 #
1644 1614 # addchangegroup assumes local user can lock remote
1645 1615 # repo (local filesystem, old ssh servers).
1646 1616 #
1647 1617 # unbundle assumes local user cannot lock remote repo (new ssh
1648 1618 # servers, http servers).
1649 1619
1650 1620 # get local lock as we might write phase data
1651 1621 locallock = self.lock()
1652 1622 try:
1653 1623 self.checkpush(force, revs)
1654 1624 lock = None
1655 1625 unbundle = remote.capable('unbundle')
1656 1626 if not unbundle:
1657 1627 lock = remote.lock()
1658 1628 try:
1659 1629 # discovery
1660 1630 fci = discovery.findcommonincoming
1661 1631 commoninc = fci(self, remote, force=force)
1662 1632 common, inc, remoteheads = commoninc
1663 1633 fco = discovery.findcommonoutgoing
1664 1634 outgoing = fco(self, remote, onlyheads=revs,
1665 1635 commoninc=commoninc, force=force)
1666 1636
1667 1637
1668 1638 if not outgoing.missing:
1669 1639 # nothing to push
1670 1640 scmutil.nochangesfound(self.ui, outgoing.excluded)
1671 1641 ret = None
1672 1642 else:
1673 1643 # something to push
1674 1644 if not force:
1675 1645 discovery.checkheads(self, remote, outgoing,
1676 1646 remoteheads, newbranch,
1677 1647 bool(inc))
1678 1648
1679 1649 # create a changegroup from local
1680 1650 if revs is None and not outgoing.excluded:
1681 1651 # push everything,
1682 1652 # use the fast path, no race possible on push
1683 1653 cg = self._changegroup(outgoing.missing, 'push')
1684 1654 else:
1685 1655 cg = self.getlocalbundle('push', outgoing)
1686 1656
1687 1657 # apply changegroup to remote
1688 1658 if unbundle:
1689 1659 # local repo finds heads on server, finds out what
1690 1660 # revs it must push. once revs transferred, if server
1691 1661 # finds it has different heads (someone else won
1692 1662 # commit/push race), server aborts.
1693 1663 if force:
1694 1664 remoteheads = ['force']
1695 1665 # ssh: return remote's addchangegroup()
1696 1666 # http: return remote's addchangegroup() or 0 for error
1697 1667 ret = remote.unbundle(cg, remoteheads, 'push')
1698 1668 else:
1699 1669 # we return an integer indicating remote head count change
1700 1670 ret = remote.addchangegroup(cg, 'push', self.url())
1701 1671
1702 1672 if ret:
1703 1673 # push succeed, synchonize target of the push
1704 1674 cheads = outgoing.missingheads
1705 1675 elif revs is None:
1706 1676 # All out push fails. synchronize all common
1707 1677 cheads = outgoing.commonheads
1708 1678 else:
1709 1679 # I want cheads = heads(::missingheads and ::commonheads)
1710 1680 # (missingheads is revs with secret changeset filtered out)
1711 1681 #
1712 1682 # This can be expressed as:
1713 1683 # cheads = ( (missingheads and ::commonheads)
1714 1684 # + (commonheads and ::missingheads))"
1715 1685 # )
1716 1686 #
1717 1687 # while trying to push we already computed the following:
1718 1688 # common = (::commonheads)
1719 1689 # missing = ((commonheads::missingheads) - commonheads)
1720 1690 #
1721 1691 # We can pick:
1722 1692 # * missingheads part of comon (::commonheads)
1723 1693 common = set(outgoing.common)
1724 1694 cheads = [node for node in revs if node in common]
1725 1695 # and
1726 1696 # * commonheads parents on missing
1727 1697 revset = self.set('%ln and parents(roots(%ln))',
1728 1698 outgoing.commonheads,
1729 1699 outgoing.missing)
1730 1700 cheads.extend(c.node() for c in revset)
1731 1701 # even when we don't push, exchanging phase data is useful
1732 1702 remotephases = remote.listkeys('phases')
1733 1703 if not remotephases: # old server or public only repo
1734 1704 phases.advanceboundary(self, phases.public, cheads)
1735 1705 # don't push any phase data as there is nothing to push
1736 1706 else:
1737 1707 ana = phases.analyzeremotephases(self, cheads, remotephases)
1738 1708 pheads, droots = ana
1739 1709 ### Apply remote phase on local
1740 1710 if remotephases.get('publishing', False):
1741 1711 phases.advanceboundary(self, phases.public, cheads)
1742 1712 else: # publish = False
1743 1713 phases.advanceboundary(self, phases.public, pheads)
1744 1714 phases.advanceboundary(self, phases.draft, cheads)
1745 1715 ### Apply local phase on remote
1746 1716
1747 1717 # Get the list of all revs draft on remote by public here.
1748 1718 # XXX Beware that revset break if droots is not strictly
1749 1719 # XXX root we may want to ensure it is but it is costly
1750 1720 outdated = self.set('heads((%ln::%ln) and public())',
1751 1721 droots, cheads)
1752 1722 for newremotehead in outdated:
1753 1723 r = remote.pushkey('phases',
1754 1724 newremotehead.hex(),
1755 1725 str(phases.draft),
1756 1726 str(phases.public))
1757 1727 if not r:
1758 1728 self.ui.warn(_('updating %s to public failed!\n')
1759 1729 % newremotehead)
1760 1730 finally:
1761 1731 if lock is not None:
1762 1732 lock.release()
1763 1733 finally:
1764 1734 locallock.release()
1765 1735
1766 1736 self.ui.debug("checking for updated bookmarks\n")
1767 1737 rb = remote.listkeys('bookmarks')
1768 1738 for k in rb.keys():
1769 1739 if k in self._bookmarks:
1770 1740 nr, nl = rb[k], hex(self._bookmarks[k])
1771 1741 if nr in self:
1772 1742 cr = self[nr]
1773 1743 cl = self[nl]
1774 1744 if cl in cr.descendants():
1775 1745 r = remote.pushkey('bookmarks', k, nr, nl)
1776 1746 if r:
1777 1747 self.ui.status(_("updating bookmark %s\n") % k)
1778 1748 else:
1779 1749 self.ui.warn(_('updating bookmark %s'
1780 1750 ' failed!\n') % k)
1781 1751
1782 1752 return ret
1783 1753
1784 1754 def changegroupinfo(self, nodes, source):
1785 1755 if self.ui.verbose or source == 'bundle':
1786 1756 self.ui.status(_("%d changesets found\n") % len(nodes))
1787 1757 if self.ui.debugflag:
1788 1758 self.ui.debug("list of changesets:\n")
1789 1759 for node in nodes:
1790 1760 self.ui.debug("%s\n" % hex(node))
1791 1761
1792 1762 def changegroupsubset(self, bases, heads, source):
1793 1763 """Compute a changegroup consisting of all the nodes that are
1794 1764 descendants of any of the bases and ancestors of any of the heads.
1795 1765 Return a chunkbuffer object whose read() method will return
1796 1766 successive changegroup chunks.
1797 1767
1798 1768 It is fairly complex as determining which filenodes and which
1799 1769 manifest nodes need to be included for the changeset to be complete
1800 1770 is non-trivial.
1801 1771
1802 1772 Another wrinkle is doing the reverse, figuring out which changeset in
1803 1773 the changegroup a particular filenode or manifestnode belongs to.
1804 1774 """
1805 1775 cl = self.changelog
1806 1776 if not bases:
1807 1777 bases = [nullid]
1808 1778 csets, bases, heads = cl.nodesbetween(bases, heads)
1809 1779 # We assume that all ancestors of bases are known
1810 1780 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1811 1781 return self._changegroupsubset(common, csets, heads, source)
1812 1782
1813 1783 def getlocalbundle(self, source, outgoing):
1814 1784 """Like getbundle, but taking a discovery.outgoing as an argument.
1815 1785
1816 1786 This is only implemented for local repos and reuses potentially
1817 1787 precomputed sets in outgoing."""
1818 1788 if not outgoing.missing:
1819 1789 return None
1820 1790 return self._changegroupsubset(outgoing.common,
1821 1791 outgoing.missing,
1822 1792 outgoing.missingheads,
1823 1793 source)
1824 1794
1825 1795 def getbundle(self, source, heads=None, common=None):
1826 1796 """Like changegroupsubset, but returns the set difference between the
1827 1797 ancestors of heads and the ancestors common.
1828 1798
1829 1799 If heads is None, use the local heads. If common is None, use [nullid].
1830 1800
1831 1801 The nodes in common might not all be known locally due to the way the
1832 1802 current discovery protocol works.
1833 1803 """
1834 1804 cl = self.changelog
1835 1805 if common:
1836 1806 nm = cl.nodemap
1837 1807 common = [n for n in common if n in nm]
1838 1808 else:
1839 1809 common = [nullid]
1840 1810 if not heads:
1841 1811 heads = cl.heads()
1842 1812 return self.getlocalbundle(source,
1843 1813 discovery.outgoing(cl, common, heads))
1844 1814
1845 1815 def _changegroupsubset(self, commonrevs, csets, heads, source):
1846 1816
1847 1817 cl = self.changelog
1848 1818 mf = self.manifest
1849 1819 mfs = {} # needed manifests
1850 1820 fnodes = {} # needed file nodes
1851 1821 changedfiles = set()
1852 1822 fstate = ['', {}]
1853 1823 count = [0]
1854 1824
1855 1825 # can we go through the fast path ?
1856 1826 heads.sort()
1857 1827 if heads == sorted(self.heads()):
1858 1828 return self._changegroup(csets, source)
1859 1829
1860 1830 # slow path
1861 1831 self.hook('preoutgoing', throw=True, source=source)
1862 1832 self.changegroupinfo(csets, source)
1863 1833
1864 1834 # filter any nodes that claim to be part of the known set
1865 1835 def prune(revlog, missing):
1866 1836 return [n for n in missing
1867 1837 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1868 1838
1869 1839 def lookup(revlog, x):
1870 1840 if revlog == cl:
1871 1841 c = cl.read(x)
1872 1842 changedfiles.update(c[3])
1873 1843 mfs.setdefault(c[0], x)
1874 1844 count[0] += 1
1875 1845 self.ui.progress(_('bundling'), count[0],
1876 1846 unit=_('changesets'), total=len(csets))
1877 1847 return x
1878 1848 elif revlog == mf:
1879 1849 clnode = mfs[x]
1880 1850 mdata = mf.readfast(x)
1881 1851 for f in changedfiles:
1882 1852 if f in mdata:
1883 1853 fnodes.setdefault(f, {}).setdefault(mdata[f], clnode)
1884 1854 count[0] += 1
1885 1855 self.ui.progress(_('bundling'), count[0],
1886 1856 unit=_('manifests'), total=len(mfs))
1887 1857 return mfs[x]
1888 1858 else:
1889 1859 self.ui.progress(
1890 1860 _('bundling'), count[0], item=fstate[0],
1891 1861 unit=_('files'), total=len(changedfiles))
1892 1862 return fstate[1][x]
1893 1863
1894 1864 bundler = changegroup.bundle10(lookup)
1895 1865 reorder = self.ui.config('bundle', 'reorder', 'auto')
1896 1866 if reorder == 'auto':
1897 1867 reorder = None
1898 1868 else:
1899 1869 reorder = util.parsebool(reorder)
1900 1870
1901 1871 def gengroup():
1902 1872 # Create a changenode group generator that will call our functions
1903 1873 # back to lookup the owning changenode and collect information.
1904 1874 for chunk in cl.group(csets, bundler, reorder=reorder):
1905 1875 yield chunk
1906 1876 self.ui.progress(_('bundling'), None)
1907 1877
1908 1878 # Create a generator for the manifestnodes that calls our lookup
1909 1879 # and data collection functions back.
1910 1880 count[0] = 0
1911 1881 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1912 1882 yield chunk
1913 1883 self.ui.progress(_('bundling'), None)
1914 1884
1915 1885 mfs.clear()
1916 1886
1917 1887 # Go through all our files in order sorted by name.
1918 1888 count[0] = 0
1919 1889 for fname in sorted(changedfiles):
1920 1890 filerevlog = self.file(fname)
1921 1891 if not len(filerevlog):
1922 1892 raise util.Abort(_("empty or missing revlog for %s") % fname)
1923 1893 fstate[0] = fname
1924 1894 fstate[1] = fnodes.pop(fname, {})
1925 1895
1926 1896 nodelist = prune(filerevlog, fstate[1])
1927 1897 if nodelist:
1928 1898 count[0] += 1
1929 1899 yield bundler.fileheader(fname)
1930 1900 for chunk in filerevlog.group(nodelist, bundler, reorder):
1931 1901 yield chunk
1932 1902
1933 1903 # Signal that no more groups are left.
1934 1904 yield bundler.close()
1935 1905 self.ui.progress(_('bundling'), None)
1936 1906
1937 1907 if csets:
1938 1908 self.hook('outgoing', node=hex(csets[0]), source=source)
1939 1909
1940 1910 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1941 1911
1942 1912 def changegroup(self, basenodes, source):
1943 1913 # to avoid a race we use changegroupsubset() (issue1320)
1944 1914 return self.changegroupsubset(basenodes, self.heads(), source)
1945 1915
1946 1916 def _changegroup(self, nodes, source):
1947 1917 """Compute the changegroup of all nodes that we have that a recipient
1948 1918 doesn't. Return a chunkbuffer object whose read() method will return
1949 1919 successive changegroup chunks.
1950 1920
1951 1921 This is much easier than the previous function as we can assume that
1952 1922 the recipient has any changenode we aren't sending them.
1953 1923
1954 1924 nodes is the set of nodes to send"""
1955 1925
1956 1926 cl = self.changelog
1957 1927 mf = self.manifest
1958 1928 mfs = {}
1959 1929 changedfiles = set()
1960 1930 fstate = ['']
1961 1931 count = [0]
1962 1932
1963 1933 self.hook('preoutgoing', throw=True, source=source)
1964 1934 self.changegroupinfo(nodes, source)
1965 1935
1966 1936 revset = set([cl.rev(n) for n in nodes])
1967 1937
1968 1938 def gennodelst(log):
1969 1939 return [log.node(r) for r in log if log.linkrev(r) in revset]
1970 1940
1971 1941 def lookup(revlog, x):
1972 1942 if revlog == cl:
1973 1943 c = cl.read(x)
1974 1944 changedfiles.update(c[3])
1975 1945 mfs.setdefault(c[0], x)
1976 1946 count[0] += 1
1977 1947 self.ui.progress(_('bundling'), count[0],
1978 1948 unit=_('changesets'), total=len(nodes))
1979 1949 return x
1980 1950 elif revlog == mf:
1981 1951 count[0] += 1
1982 1952 self.ui.progress(_('bundling'), count[0],
1983 1953 unit=_('manifests'), total=len(mfs))
1984 1954 return cl.node(revlog.linkrev(revlog.rev(x)))
1985 1955 else:
1986 1956 self.ui.progress(
1987 1957 _('bundling'), count[0], item=fstate[0],
1988 1958 total=len(changedfiles), unit=_('files'))
1989 1959 return cl.node(revlog.linkrev(revlog.rev(x)))
1990 1960
1991 1961 bundler = changegroup.bundle10(lookup)
1992 1962 reorder = self.ui.config('bundle', 'reorder', 'auto')
1993 1963 if reorder == 'auto':
1994 1964 reorder = None
1995 1965 else:
1996 1966 reorder = util.parsebool(reorder)
1997 1967
1998 1968 def gengroup():
1999 1969 '''yield a sequence of changegroup chunks (strings)'''
2000 1970 # construct a list of all changed files
2001 1971
2002 1972 for chunk in cl.group(nodes, bundler, reorder=reorder):
2003 1973 yield chunk
2004 1974 self.ui.progress(_('bundling'), None)
2005 1975
2006 1976 count[0] = 0
2007 1977 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
2008 1978 yield chunk
2009 1979 self.ui.progress(_('bundling'), None)
2010 1980
2011 1981 count[0] = 0
2012 1982 for fname in sorted(changedfiles):
2013 1983 filerevlog = self.file(fname)
2014 1984 if not len(filerevlog):
2015 1985 raise util.Abort(_("empty or missing revlog for %s") % fname)
2016 1986 fstate[0] = fname
2017 1987 nodelist = gennodelst(filerevlog)
2018 1988 if nodelist:
2019 1989 count[0] += 1
2020 1990 yield bundler.fileheader(fname)
2021 1991 for chunk in filerevlog.group(nodelist, bundler, reorder):
2022 1992 yield chunk
2023 1993 yield bundler.close()
2024 1994 self.ui.progress(_('bundling'), None)
2025 1995
2026 1996 if nodes:
2027 1997 self.hook('outgoing', node=hex(nodes[0]), source=source)
2028 1998
2029 1999 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
2030 2000
2031 2001 def addchangegroup(self, source, srctype, url, emptyok=False):
2032 2002 """Add the changegroup returned by source.read() to this repo.
2033 2003 srctype is a string like 'push', 'pull', or 'unbundle'. url is
2034 2004 the URL of the repo where this changegroup is coming from.
2035 2005
2036 2006 Return an integer summarizing the change to this repo:
2037 2007 - nothing changed or no source: 0
2038 2008 - more heads than before: 1+added heads (2..n)
2039 2009 - fewer heads than before: -1-removed heads (-2..-n)
2040 2010 - number of heads stays the same: 1
2041 2011 """
2042 2012 def csmap(x):
2043 2013 self.ui.debug("add changeset %s\n" % short(x))
2044 2014 return len(cl)
2045 2015
2046 2016 def revmap(x):
2047 2017 return cl.rev(x)
2048 2018
2049 2019 if not source:
2050 2020 return 0
2051 2021
2052 2022 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2053 2023
2054 2024 changesets = files = revisions = 0
2055 2025 efiles = set()
2056 2026
2057 2027 # write changelog data to temp files so concurrent readers will not see
2058 2028 # inconsistent view
2059 2029 cl = self.changelog
2060 2030 cl.delayupdate()
2061 2031 oldheads = cl.heads()
2062 2032
2063 2033 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
2064 2034 try:
2065 2035 trp = weakref.proxy(tr)
2066 2036 # pull off the changeset group
2067 2037 self.ui.status(_("adding changesets\n"))
2068 2038 clstart = len(cl)
2069 2039 class prog(object):
2070 2040 step = _('changesets')
2071 2041 count = 1
2072 2042 ui = self.ui
2073 2043 total = None
2074 2044 def __call__(self):
2075 2045 self.ui.progress(self.step, self.count, unit=_('chunks'),
2076 2046 total=self.total)
2077 2047 self.count += 1
2078 2048 pr = prog()
2079 2049 source.callback = pr
2080 2050
2081 2051 source.changelogheader()
2082 2052 srccontent = cl.addgroup(source, csmap, trp)
2083 2053 if not (srccontent or emptyok):
2084 2054 raise util.Abort(_("received changelog group is empty"))
2085 2055 clend = len(cl)
2086 2056 changesets = clend - clstart
2087 2057 for c in xrange(clstart, clend):
2088 2058 efiles.update(self[c].files())
2089 2059 efiles = len(efiles)
2090 2060 self.ui.progress(_('changesets'), None)
2091 2061
2092 2062 # pull off the manifest group
2093 2063 self.ui.status(_("adding manifests\n"))
2094 2064 pr.step = _('manifests')
2095 2065 pr.count = 1
2096 2066 pr.total = changesets # manifests <= changesets
2097 2067 # no need to check for empty manifest group here:
2098 2068 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2099 2069 # no new manifest will be created and the manifest group will
2100 2070 # be empty during the pull
2101 2071 source.manifestheader()
2102 2072 self.manifest.addgroup(source, revmap, trp)
2103 2073 self.ui.progress(_('manifests'), None)
2104 2074
2105 2075 needfiles = {}
2106 2076 if self.ui.configbool('server', 'validate', default=False):
2107 2077 # validate incoming csets have their manifests
2108 2078 for cset in xrange(clstart, clend):
2109 2079 mfest = self.changelog.read(self.changelog.node(cset))[0]
2110 2080 mfest = self.manifest.readdelta(mfest)
2111 2081 # store file nodes we must see
2112 2082 for f, n in mfest.iteritems():
2113 2083 needfiles.setdefault(f, set()).add(n)
2114 2084
2115 2085 # process the files
2116 2086 self.ui.status(_("adding file changes\n"))
2117 2087 pr.step = _('files')
2118 2088 pr.count = 1
2119 2089 pr.total = efiles
2120 2090 source.callback = None
2121 2091
2122 2092 while True:
2123 2093 chunkdata = source.filelogheader()
2124 2094 if not chunkdata:
2125 2095 break
2126 2096 f = chunkdata["filename"]
2127 2097 self.ui.debug("adding %s revisions\n" % f)
2128 2098 pr()
2129 2099 fl = self.file(f)
2130 2100 o = len(fl)
2131 2101 if not fl.addgroup(source, revmap, trp):
2132 2102 raise util.Abort(_("received file revlog group is empty"))
2133 2103 revisions += len(fl) - o
2134 2104 files += 1
2135 2105 if f in needfiles:
2136 2106 needs = needfiles[f]
2137 2107 for new in xrange(o, len(fl)):
2138 2108 n = fl.node(new)
2139 2109 if n in needs:
2140 2110 needs.remove(n)
2141 2111 if not needs:
2142 2112 del needfiles[f]
2143 2113 self.ui.progress(_('files'), None)
2144 2114
2145 2115 for f, needs in needfiles.iteritems():
2146 2116 fl = self.file(f)
2147 2117 for n in needs:
2148 2118 try:
2149 2119 fl.rev(n)
2150 2120 except error.LookupError:
2151 2121 raise util.Abort(
2152 2122 _('missing file data for %s:%s - run hg verify') %
2153 2123 (f, hex(n)))
2154 2124
2155 2125 dh = 0
2156 2126 if oldheads:
2157 2127 heads = cl.heads()
2158 2128 dh = len(heads) - len(oldheads)
2159 2129 for h in heads:
2160 2130 if h not in oldheads and 'close' in self[h].extra():
2161 2131 dh -= 1
2162 2132 htext = ""
2163 2133 if dh:
2164 2134 htext = _(" (%+d heads)") % dh
2165 2135
2166 2136 self.ui.status(_("added %d changesets"
2167 2137 " with %d changes to %d files%s\n")
2168 2138 % (changesets, revisions, files, htext))
2169 2139
2170 2140 if changesets > 0:
2171 2141 p = lambda: cl.writepending() and self.root or ""
2172 2142 self.hook('pretxnchangegroup', throw=True,
2173 2143 node=hex(cl.node(clstart)), source=srctype,
2174 2144 url=url, pending=p)
2175 2145
2176 2146 added = [cl.node(r) for r in xrange(clstart, clend)]
2177 2147 publishing = self.ui.configbool('phases', 'publish', True)
2178 2148 if srctype == 'push':
2179 2149 # Old server can not push the boundary themself.
2180 2150 # New server won't push the boundary if changeset already
2181 2151 # existed locally as secrete
2182 2152 #
2183 2153 # We should not use added here but the list of all change in
2184 2154 # the bundle
2185 2155 if publishing:
2186 2156 phases.advanceboundary(self, phases.public, srccontent)
2187 2157 else:
2188 2158 phases.advanceboundary(self, phases.draft, srccontent)
2189 2159 phases.retractboundary(self, phases.draft, added)
2190 2160 elif srctype != 'strip':
2191 2161 # publishing only alter behavior during push
2192 2162 #
2193 2163 # strip should not touch boundary at all
2194 2164 phases.retractboundary(self, phases.draft, added)
2195 2165
2196 2166 # make changelog see real files again
2197 2167 cl.finalize(trp)
2198 2168
2199 2169 tr.close()
2200 2170
2201 2171 if changesets > 0:
2202 2172 def runhooks():
2203 2173 # forcefully update the on-disk branch cache
2204 2174 self.ui.debug("updating the branch cache\n")
2205 2175 self.updatebranchcache()
2206 2176 self.hook("changegroup", node=hex(cl.node(clstart)),
2207 2177 source=srctype, url=url)
2208 2178
2209 2179 for n in added:
2210 2180 self.hook("incoming", node=hex(n), source=srctype,
2211 2181 url=url)
2212 2182 self._afterlock(runhooks)
2213 2183
2214 2184 finally:
2215 2185 tr.release()
2216 2186 # never return 0 here:
2217 2187 if dh < 0:
2218 2188 return dh - 1
2219 2189 else:
2220 2190 return dh + 1
2221 2191
2222 2192 def stream_in(self, remote, requirements):
2223 2193 lock = self.lock()
2224 2194 try:
2225 2195 fp = remote.stream_out()
2226 2196 l = fp.readline()
2227 2197 try:
2228 2198 resp = int(l)
2229 2199 except ValueError:
2230 2200 raise error.ResponseError(
2231 2201 _('Unexpected response from remote server:'), l)
2232 2202 if resp == 1:
2233 2203 raise util.Abort(_('operation forbidden by server'))
2234 2204 elif resp == 2:
2235 2205 raise util.Abort(_('locking the remote repository failed'))
2236 2206 elif resp != 0:
2237 2207 raise util.Abort(_('the server sent an unknown error code'))
2238 2208 self.ui.status(_('streaming all changes\n'))
2239 2209 l = fp.readline()
2240 2210 try:
2241 2211 total_files, total_bytes = map(int, l.split(' ', 1))
2242 2212 except (ValueError, TypeError):
2243 2213 raise error.ResponseError(
2244 2214 _('Unexpected response from remote server:'), l)
2245 2215 self.ui.status(_('%d files to transfer, %s of data\n') %
2246 2216 (total_files, util.bytecount(total_bytes)))
2247 2217 start = time.time()
2248 2218 for i in xrange(total_files):
2249 2219 # XXX doesn't support '\n' or '\r' in filenames
2250 2220 l = fp.readline()
2251 2221 try:
2252 2222 name, size = l.split('\0', 1)
2253 2223 size = int(size)
2254 2224 except (ValueError, TypeError):
2255 2225 raise error.ResponseError(
2256 2226 _('Unexpected response from remote server:'), l)
2257 2227 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
2258 2228 # for backwards compat, name was partially encoded
2259 2229 ofp = self.sopener(store.decodedir(name), 'w')
2260 2230 for chunk in util.filechunkiter(fp, limit=size):
2261 2231 ofp.write(chunk)
2262 2232 ofp.close()
2263 2233 elapsed = time.time() - start
2264 2234 if elapsed <= 0:
2265 2235 elapsed = 0.001
2266 2236 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2267 2237 (util.bytecount(total_bytes), elapsed,
2268 2238 util.bytecount(total_bytes / elapsed)))
2269 2239
2270 2240 # new requirements = old non-format requirements + new format-related
2271 2241 # requirements from the streamed-in repository
2272 2242 requirements.update(set(self.requirements) - self.supportedformats)
2273 2243 self._applyrequirements(requirements)
2274 2244 self._writerequirements()
2275 2245
2276 2246 self.invalidate()
2277 2247 return len(self.heads()) + 1
2278 2248 finally:
2279 2249 lock.release()
2280 2250
2281 2251 def clone(self, remote, heads=[], stream=False):
2282 2252 '''clone remote repository.
2283 2253
2284 2254 keyword arguments:
2285 2255 heads: list of revs to clone (forces use of pull)
2286 2256 stream: use streaming clone if possible'''
2287 2257
2288 2258 # now, all clients that can request uncompressed clones can
2289 2259 # read repo formats supported by all servers that can serve
2290 2260 # them.
2291 2261
2292 2262 # if revlog format changes, client will have to check version
2293 2263 # and format flags on "stream" capability, and use
2294 2264 # uncompressed only if compatible.
2295 2265
2296 2266 if not stream:
2297 2267 # if the server explicitely prefer to stream (for fast LANs)
2298 2268 stream = remote.capable('stream-preferred')
2299 2269
2300 2270 if stream and not heads:
2301 2271 # 'stream' means remote revlog format is revlogv1 only
2302 2272 if remote.capable('stream'):
2303 2273 return self.stream_in(remote, set(('revlogv1',)))
2304 2274 # otherwise, 'streamreqs' contains the remote revlog format
2305 2275 streamreqs = remote.capable('streamreqs')
2306 2276 if streamreqs:
2307 2277 streamreqs = set(streamreqs.split(','))
2308 2278 # if we support it, stream in and adjust our requirements
2309 2279 if not streamreqs - self.supportedformats:
2310 2280 return self.stream_in(remote, streamreqs)
2311 2281 return self.pull(remote, heads)
2312 2282
2313 2283 def pushkey(self, namespace, key, old, new):
2314 2284 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
2315 2285 old=old, new=new)
2316 2286 ret = pushkey.push(self, namespace, key, old, new)
2317 2287 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2318 2288 ret=ret)
2319 2289 return ret
2320 2290
2321 2291 def listkeys(self, namespace):
2322 2292 self.hook('prelistkeys', throw=True, namespace=namespace)
2323 2293 values = pushkey.list(self, namespace)
2324 2294 self.hook('listkeys', namespace=namespace, values=values)
2325 2295 return values
2326 2296
2327 2297 def debugwireargs(self, one, two, three=None, four=None, five=None):
2328 2298 '''used to test argument passing over the wire'''
2329 2299 return "%s %s %s %s %s" % (one, two, three, four, five)
2330 2300
2331 2301 def savecommitmessage(self, text):
2332 2302 fp = self.opener('last-message.txt', 'wb')
2333 2303 try:
2334 2304 fp.write(text)
2335 2305 finally:
2336 2306 fp.close()
2337 2307 return self.pathto(fp.name[len(self.root)+1:])
2338 2308
2339 2309 # used to avoid circular references so destructors work
2340 2310 def aftertrans(files):
2341 2311 renamefiles = [tuple(t) for t in files]
2342 2312 def a():
2343 2313 for src, dest in renamefiles:
2344 2314 util.rename(src, dest)
2345 2315 return a
2346 2316
2347 2317 def undoname(fn):
2348 2318 base, name = os.path.split(fn)
2349 2319 assert name.startswith('journal')
2350 2320 return os.path.join(base, name.replace('journal', 'undo', 1))
2351 2321
2352 2322 def instance(ui, path, create):
2353 2323 return localrepository(ui, util.urllocalpath(path), create)
2354 2324
2355 2325 def islocal(path):
2356 2326 return True
General Comments 0
You need to be logged in to leave comments. Login now