##// END OF EJS Templates
changegroup: optimize gennodelist
Matt Mackall -
r16425:70bf4dc5 default
parent child Browse files
Show More
@@ -1,2341 +1,2343 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 596 return self[key].node()
597 597
598 598 def lookupbranch(self, key, remote=None):
599 599 repo = remote or self
600 600 if key in repo.branchmap():
601 601 return key
602 602
603 603 repo = (remote and remote.local()) and remote or self
604 604 return repo[key].branch()
605 605
606 606 def known(self, nodes):
607 607 nm = self.changelog.nodemap
608 608 result = []
609 609 for n in nodes:
610 610 r = nm.get(n)
611 611 resp = not (r is None or self._phaserev[r] >= phases.secret)
612 612 result.append(resp)
613 613 return result
614 614
615 615 def local(self):
616 616 return self
617 617
618 618 def join(self, f):
619 619 return os.path.join(self.path, f)
620 620
621 621 def wjoin(self, f):
622 622 return os.path.join(self.root, f)
623 623
624 624 def file(self, f):
625 625 if f[0] == '/':
626 626 f = f[1:]
627 627 return filelog.filelog(self.sopener, f)
628 628
629 629 def changectx(self, changeid):
630 630 return self[changeid]
631 631
632 632 def parents(self, changeid=None):
633 633 '''get list of changectxs for parents of changeid'''
634 634 return self[changeid].parents()
635 635
636 636 def filectx(self, path, changeid=None, fileid=None):
637 637 """changeid can be a changeset revision, node, or tag.
638 638 fileid can be a file revision or node."""
639 639 return context.filectx(self, path, changeid, fileid)
640 640
641 641 def getcwd(self):
642 642 return self.dirstate.getcwd()
643 643
644 644 def pathto(self, f, cwd=None):
645 645 return self.dirstate.pathto(f, cwd)
646 646
647 647 def wfile(self, f, mode='r'):
648 648 return self.wopener(f, mode)
649 649
650 650 def _link(self, f):
651 651 return os.path.islink(self.wjoin(f))
652 652
653 653 def _loadfilter(self, filter):
654 654 if filter not in self.filterpats:
655 655 l = []
656 656 for pat, cmd in self.ui.configitems(filter):
657 657 if cmd == '!':
658 658 continue
659 659 mf = matchmod.match(self.root, '', [pat])
660 660 fn = None
661 661 params = cmd
662 662 for name, filterfn in self._datafilters.iteritems():
663 663 if cmd.startswith(name):
664 664 fn = filterfn
665 665 params = cmd[len(name):].lstrip()
666 666 break
667 667 if not fn:
668 668 fn = lambda s, c, **kwargs: util.filter(s, c)
669 669 # Wrap old filters not supporting keyword arguments
670 670 if not inspect.getargspec(fn)[2]:
671 671 oldfn = fn
672 672 fn = lambda s, c, **kwargs: oldfn(s, c)
673 673 l.append((mf, fn, params))
674 674 self.filterpats[filter] = l
675 675 return self.filterpats[filter]
676 676
677 677 def _filter(self, filterpats, filename, data):
678 678 for mf, fn, cmd in filterpats:
679 679 if mf(filename):
680 680 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
681 681 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
682 682 break
683 683
684 684 return data
685 685
686 686 @propertycache
687 687 def _encodefilterpats(self):
688 688 return self._loadfilter('encode')
689 689
690 690 @propertycache
691 691 def _decodefilterpats(self):
692 692 return self._loadfilter('decode')
693 693
694 694 def adddatafilter(self, name, filter):
695 695 self._datafilters[name] = filter
696 696
697 697 def wread(self, filename):
698 698 if self._link(filename):
699 699 data = os.readlink(self.wjoin(filename))
700 700 else:
701 701 data = self.wopener.read(filename)
702 702 return self._filter(self._encodefilterpats, filename, data)
703 703
704 704 def wwrite(self, filename, data, flags):
705 705 data = self._filter(self._decodefilterpats, filename, data)
706 706 if 'l' in flags:
707 707 self.wopener.symlink(data, filename)
708 708 else:
709 709 self.wopener.write(filename, data)
710 710 if 'x' in flags:
711 711 util.setflags(self.wjoin(filename), False, True)
712 712
713 713 def wwritedata(self, filename, data):
714 714 return self._filter(self._decodefilterpats, filename, data)
715 715
716 716 def transaction(self, desc):
717 717 tr = self._transref and self._transref() or None
718 718 if tr and tr.running():
719 719 return tr.nest()
720 720
721 721 # abort here if the journal already exists
722 722 if os.path.exists(self.sjoin("journal")):
723 723 raise error.RepoError(
724 724 _("abandoned transaction found - run hg recover"))
725 725
726 726 self._writejournal(desc)
727 727 renames = [(x, undoname(x)) for x in self._journalfiles()]
728 728
729 729 tr = transaction.transaction(self.ui.warn, self.sopener,
730 730 self.sjoin("journal"),
731 731 aftertrans(renames),
732 732 self.store.createmode)
733 733 self._transref = weakref.ref(tr)
734 734 return tr
735 735
736 736 def _journalfiles(self):
737 737 return (self.sjoin('journal'), self.join('journal.dirstate'),
738 738 self.join('journal.branch'), self.join('journal.desc'),
739 739 self.join('journal.bookmarks'),
740 740 self.sjoin('journal.phaseroots'))
741 741
742 742 def undofiles(self):
743 743 return [undoname(x) for x in self._journalfiles()]
744 744
745 745 def _writejournal(self, desc):
746 746 # save dirstate for rollback
747 747 try:
748 748 ds = self.opener.read("dirstate")
749 749 except IOError:
750 750 ds = ""
751 751 self.opener.write("journal.dirstate", ds)
752 752 self.opener.write("journal.branch",
753 753 encoding.fromlocal(self.dirstate.branch()))
754 754 self.opener.write("journal.desc",
755 755 "%d\n%s\n" % (len(self), desc))
756 756
757 757 try:
758 758 bk = self.opener.read("bookmarks")
759 759 except IOError:
760 760 bk = ""
761 761 self.opener.write("journal.bookmarks", bk)
762 762
763 763 phasesname = self.sjoin('phaseroots')
764 764 if os.path.exists(phasesname):
765 765 util.copyfile(phasesname, self.sjoin('journal.phaseroots'))
766 766 else:
767 767 self.sopener.write('journal.phaseroots', '')
768 768
769 769 def recover(self):
770 770 lock = self.lock()
771 771 try:
772 772 if os.path.exists(self.sjoin("journal")):
773 773 self.ui.status(_("rolling back interrupted transaction\n"))
774 774 transaction.rollback(self.sopener, self.sjoin("journal"),
775 775 self.ui.warn)
776 776 self.invalidate()
777 777 return True
778 778 else:
779 779 self.ui.warn(_("no interrupted transaction available\n"))
780 780 return False
781 781 finally:
782 782 lock.release()
783 783
784 784 def rollback(self, dryrun=False, force=False):
785 785 wlock = lock = None
786 786 try:
787 787 wlock = self.wlock()
788 788 lock = self.lock()
789 789 if os.path.exists(self.sjoin("undo")):
790 790 return self._rollback(dryrun, force)
791 791 else:
792 792 self.ui.warn(_("no rollback information available\n"))
793 793 return 1
794 794 finally:
795 795 release(lock, wlock)
796 796
797 797 def _rollback(self, dryrun, force):
798 798 ui = self.ui
799 799 try:
800 800 args = self.opener.read('undo.desc').splitlines()
801 801 (oldlen, desc, detail) = (int(args[0]), args[1], None)
802 802 if len(args) >= 3:
803 803 detail = args[2]
804 804 oldtip = oldlen - 1
805 805
806 806 if detail and ui.verbose:
807 807 msg = (_('repository tip rolled back to revision %s'
808 808 ' (undo %s: %s)\n')
809 809 % (oldtip, desc, detail))
810 810 else:
811 811 msg = (_('repository tip rolled back to revision %s'
812 812 ' (undo %s)\n')
813 813 % (oldtip, desc))
814 814 except IOError:
815 815 msg = _('rolling back unknown transaction\n')
816 816 desc = None
817 817
818 818 if not force and self['.'] != self['tip'] and desc == 'commit':
819 819 raise util.Abort(
820 820 _('rollback of last commit while not checked out '
821 821 'may lose data'), hint=_('use -f to force'))
822 822
823 823 ui.status(msg)
824 824 if dryrun:
825 825 return 0
826 826
827 827 parents = self.dirstate.parents()
828 828 transaction.rollback(self.sopener, self.sjoin('undo'), ui.warn)
829 829 if os.path.exists(self.join('undo.bookmarks')):
830 830 util.rename(self.join('undo.bookmarks'),
831 831 self.join('bookmarks'))
832 832 if os.path.exists(self.sjoin('undo.phaseroots')):
833 833 util.rename(self.sjoin('undo.phaseroots'),
834 834 self.sjoin('phaseroots'))
835 835 self.invalidate()
836 836
837 837 parentgone = (parents[0] not in self.changelog.nodemap or
838 838 parents[1] not in self.changelog.nodemap)
839 839 if parentgone:
840 840 util.rename(self.join('undo.dirstate'), self.join('dirstate'))
841 841 try:
842 842 branch = self.opener.read('undo.branch')
843 843 self.dirstate.setbranch(branch)
844 844 except IOError:
845 845 ui.warn(_('named branch could not be reset: '
846 846 'current branch is still \'%s\'\n')
847 847 % self.dirstate.branch())
848 848
849 849 self.dirstate.invalidate()
850 850 parents = tuple([p.rev() for p in self.parents()])
851 851 if len(parents) > 1:
852 852 ui.status(_('working directory now based on '
853 853 'revisions %d and %d\n') % parents)
854 854 else:
855 855 ui.status(_('working directory now based on '
856 856 'revision %d\n') % parents)
857 857 self.destroyed()
858 858 return 0
859 859
860 860 def invalidatecaches(self):
861 861 def delcache(name):
862 862 try:
863 863 delattr(self, name)
864 864 except AttributeError:
865 865 pass
866 866
867 867 delcache('_tagscache')
868 868 delcache('_phaserev')
869 869
870 870 self._branchcache = None # in UTF-8
871 871 self._branchcachetip = None
872 872
873 873 def invalidatedirstate(self):
874 874 '''Invalidates the dirstate, causing the next call to dirstate
875 875 to check if it was modified since the last time it was read,
876 876 rereading it if it has.
877 877
878 878 This is different to dirstate.invalidate() that it doesn't always
879 879 rereads the dirstate. Use dirstate.invalidate() if you want to
880 880 explicitly read the dirstate again (i.e. restoring it to a previous
881 881 known good state).'''
882 882 if 'dirstate' in self.__dict__:
883 883 for k in self.dirstate._filecache:
884 884 try:
885 885 delattr(self.dirstate, k)
886 886 except AttributeError:
887 887 pass
888 888 delattr(self, 'dirstate')
889 889
890 890 def invalidate(self):
891 891 for k in self._filecache:
892 892 # dirstate is invalidated separately in invalidatedirstate()
893 893 if k == 'dirstate':
894 894 continue
895 895
896 896 try:
897 897 delattr(self, k)
898 898 except AttributeError:
899 899 pass
900 900 self.invalidatecaches()
901 901
902 902 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
903 903 try:
904 904 l = lock.lock(lockname, 0, releasefn, desc=desc)
905 905 except error.LockHeld, inst:
906 906 if not wait:
907 907 raise
908 908 self.ui.warn(_("waiting for lock on %s held by %r\n") %
909 909 (desc, inst.locker))
910 910 # default to 600 seconds timeout
911 911 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
912 912 releasefn, desc=desc)
913 913 if acquirefn:
914 914 acquirefn()
915 915 return l
916 916
917 917 def _afterlock(self, callback):
918 918 """add a callback to the current repository lock.
919 919
920 920 The callback will be executed on lock release."""
921 921 l = self._lockref and self._lockref()
922 922 if l:
923 923 l.postrelease.append(callback)
924 924
925 925 def lock(self, wait=True):
926 926 '''Lock the repository store (.hg/store) and return a weak reference
927 927 to the lock. Use this before modifying the store (e.g. committing or
928 928 stripping). If you are opening a transaction, get a lock as well.)'''
929 929 l = self._lockref and self._lockref()
930 930 if l is not None and l.held:
931 931 l.lock()
932 932 return l
933 933
934 934 def unlock():
935 935 self.store.write()
936 936 if self._dirtyphases:
937 937 phases.writeroots(self)
938 938 self._dirtyphases = False
939 939 for k, ce in self._filecache.items():
940 940 if k == 'dirstate':
941 941 continue
942 942 ce.refresh()
943 943
944 944 l = self._lock(self.sjoin("lock"), wait, unlock,
945 945 self.invalidate, _('repository %s') % self.origroot)
946 946 self._lockref = weakref.ref(l)
947 947 return l
948 948
949 949 def wlock(self, wait=True):
950 950 '''Lock the non-store parts of the repository (everything under
951 951 .hg except .hg/store) and return a weak reference to the lock.
952 952 Use this before modifying files in .hg.'''
953 953 l = self._wlockref and self._wlockref()
954 954 if l is not None and l.held:
955 955 l.lock()
956 956 return l
957 957
958 958 def unlock():
959 959 self.dirstate.write()
960 960 ce = self._filecache.get('dirstate')
961 961 if ce:
962 962 ce.refresh()
963 963
964 964 l = self._lock(self.join("wlock"), wait, unlock,
965 965 self.invalidatedirstate, _('working directory of %s') %
966 966 self.origroot)
967 967 self._wlockref = weakref.ref(l)
968 968 return l
969 969
970 970 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
971 971 """
972 972 commit an individual file as part of a larger transaction
973 973 """
974 974
975 975 fname = fctx.path()
976 976 text = fctx.data()
977 977 flog = self.file(fname)
978 978 fparent1 = manifest1.get(fname, nullid)
979 979 fparent2 = fparent2o = manifest2.get(fname, nullid)
980 980
981 981 meta = {}
982 982 copy = fctx.renamed()
983 983 if copy and copy[0] != fname:
984 984 # Mark the new revision of this file as a copy of another
985 985 # file. This copy data will effectively act as a parent
986 986 # of this new revision. If this is a merge, the first
987 987 # parent will be the nullid (meaning "look up the copy data")
988 988 # and the second one will be the other parent. For example:
989 989 #
990 990 # 0 --- 1 --- 3 rev1 changes file foo
991 991 # \ / rev2 renames foo to bar and changes it
992 992 # \- 2 -/ rev3 should have bar with all changes and
993 993 # should record that bar descends from
994 994 # bar in rev2 and foo in rev1
995 995 #
996 996 # this allows this merge to succeed:
997 997 #
998 998 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
999 999 # \ / merging rev3 and rev4 should use bar@rev2
1000 1000 # \- 2 --- 4 as the merge base
1001 1001 #
1002 1002
1003 1003 cfname = copy[0]
1004 1004 crev = manifest1.get(cfname)
1005 1005 newfparent = fparent2
1006 1006
1007 1007 if manifest2: # branch merge
1008 1008 if fparent2 == nullid or crev is None: # copied on remote side
1009 1009 if cfname in manifest2:
1010 1010 crev = manifest2[cfname]
1011 1011 newfparent = fparent1
1012 1012
1013 1013 # find source in nearest ancestor if we've lost track
1014 1014 if not crev:
1015 1015 self.ui.debug(" %s: searching for copy revision for %s\n" %
1016 1016 (fname, cfname))
1017 1017 for ancestor in self[None].ancestors():
1018 1018 if cfname in ancestor:
1019 1019 crev = ancestor[cfname].filenode()
1020 1020 break
1021 1021
1022 1022 if crev:
1023 1023 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
1024 1024 meta["copy"] = cfname
1025 1025 meta["copyrev"] = hex(crev)
1026 1026 fparent1, fparent2 = nullid, newfparent
1027 1027 else:
1028 1028 self.ui.warn(_("warning: can't find ancestor for '%s' "
1029 1029 "copied from '%s'!\n") % (fname, cfname))
1030 1030
1031 1031 elif fparent2 != nullid:
1032 1032 # is one parent an ancestor of the other?
1033 1033 fparentancestor = flog.ancestor(fparent1, fparent2)
1034 1034 if fparentancestor == fparent1:
1035 1035 fparent1, fparent2 = fparent2, nullid
1036 1036 elif fparentancestor == fparent2:
1037 1037 fparent2 = nullid
1038 1038
1039 1039 # is the file changed?
1040 1040 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
1041 1041 changelist.append(fname)
1042 1042 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
1043 1043
1044 1044 # are just the flags changed during merge?
1045 1045 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
1046 1046 changelist.append(fname)
1047 1047
1048 1048 return fparent1
1049 1049
1050 1050 def commit(self, text="", user=None, date=None, match=None, force=False,
1051 1051 editor=False, extra={}):
1052 1052 """Add a new revision to current repository.
1053 1053
1054 1054 Revision information is gathered from the working directory,
1055 1055 match can be used to filter the committed files. If editor is
1056 1056 supplied, it is called to get a commit message.
1057 1057 """
1058 1058
1059 1059 def fail(f, msg):
1060 1060 raise util.Abort('%s: %s' % (f, msg))
1061 1061
1062 1062 if not match:
1063 1063 match = matchmod.always(self.root, '')
1064 1064
1065 1065 if not force:
1066 1066 vdirs = []
1067 1067 match.dir = vdirs.append
1068 1068 match.bad = fail
1069 1069
1070 1070 wlock = self.wlock()
1071 1071 try:
1072 1072 wctx = self[None]
1073 1073 merge = len(wctx.parents()) > 1
1074 1074
1075 1075 if (not force and merge and match and
1076 1076 (match.files() or match.anypats())):
1077 1077 raise util.Abort(_('cannot partially commit a merge '
1078 1078 '(do not specify files or patterns)'))
1079 1079
1080 1080 changes = self.status(match=match, clean=force)
1081 1081 if force:
1082 1082 changes[0].extend(changes[6]) # mq may commit unchanged files
1083 1083
1084 1084 # check subrepos
1085 1085 subs = []
1086 1086 commitsubs = set()
1087 1087 newstate = wctx.substate.copy()
1088 1088 # only manage subrepos and .hgsubstate if .hgsub is present
1089 1089 if '.hgsub' in wctx:
1090 1090 # we'll decide whether to track this ourselves, thanks
1091 1091 if '.hgsubstate' in changes[0]:
1092 1092 changes[0].remove('.hgsubstate')
1093 1093 if '.hgsubstate' in changes[2]:
1094 1094 changes[2].remove('.hgsubstate')
1095 1095
1096 1096 # compare current state to last committed state
1097 1097 # build new substate based on last committed state
1098 1098 oldstate = wctx.p1().substate
1099 1099 for s in sorted(newstate.keys()):
1100 1100 if not match(s):
1101 1101 # ignore working copy, use old state if present
1102 1102 if s in oldstate:
1103 1103 newstate[s] = oldstate[s]
1104 1104 continue
1105 1105 if not force:
1106 1106 raise util.Abort(
1107 1107 _("commit with new subrepo %s excluded") % s)
1108 1108 if wctx.sub(s).dirty(True):
1109 1109 if not self.ui.configbool('ui', 'commitsubrepos'):
1110 1110 raise util.Abort(
1111 1111 _("uncommitted changes in subrepo %s") % s,
1112 1112 hint=_("use --subrepos for recursive commit"))
1113 1113 subs.append(s)
1114 1114 commitsubs.add(s)
1115 1115 else:
1116 1116 bs = wctx.sub(s).basestate()
1117 1117 newstate[s] = (newstate[s][0], bs, newstate[s][2])
1118 1118 if oldstate.get(s, (None, None, None))[1] != bs:
1119 1119 subs.append(s)
1120 1120
1121 1121 # check for removed subrepos
1122 1122 for p in wctx.parents():
1123 1123 r = [s for s in p.substate if s not in newstate]
1124 1124 subs += [s for s in r if match(s)]
1125 1125 if subs:
1126 1126 if (not match('.hgsub') and
1127 1127 '.hgsub' in (wctx.modified() + wctx.added())):
1128 1128 raise util.Abort(
1129 1129 _("can't commit subrepos without .hgsub"))
1130 1130 changes[0].insert(0, '.hgsubstate')
1131 1131
1132 1132 elif '.hgsub' in changes[2]:
1133 1133 # clean up .hgsubstate when .hgsub is removed
1134 1134 if ('.hgsubstate' in wctx and
1135 1135 '.hgsubstate' not in changes[0] + changes[1] + changes[2]):
1136 1136 changes[2].insert(0, '.hgsubstate')
1137 1137
1138 1138 # make sure all explicit patterns are matched
1139 1139 if not force and match.files():
1140 1140 matched = set(changes[0] + changes[1] + changes[2])
1141 1141
1142 1142 for f in match.files():
1143 1143 if f == '.' or f in matched or f in wctx.substate:
1144 1144 continue
1145 1145 if f in changes[3]: # missing
1146 1146 fail(f, _('file not found!'))
1147 1147 if f in vdirs: # visited directory
1148 1148 d = f + '/'
1149 1149 for mf in matched:
1150 1150 if mf.startswith(d):
1151 1151 break
1152 1152 else:
1153 1153 fail(f, _("no match under directory!"))
1154 1154 elif f not in self.dirstate:
1155 1155 fail(f, _("file not tracked!"))
1156 1156
1157 1157 if (not force and not extra.get("close") and not merge
1158 1158 and not (changes[0] or changes[1] or changes[2])
1159 1159 and wctx.branch() == wctx.p1().branch()):
1160 1160 return None
1161 1161
1162 1162 ms = mergemod.mergestate(self)
1163 1163 for f in changes[0]:
1164 1164 if f in ms and ms[f] == 'u':
1165 1165 raise util.Abort(_("unresolved merge conflicts "
1166 1166 "(see hg help resolve)"))
1167 1167
1168 1168 cctx = context.workingctx(self, text, user, date, extra, changes)
1169 1169 if editor:
1170 1170 cctx._text = editor(self, cctx, subs)
1171 1171 edited = (text != cctx._text)
1172 1172
1173 1173 # commit subs and write new state
1174 1174 if subs:
1175 1175 for s in sorted(commitsubs):
1176 1176 sub = wctx.sub(s)
1177 1177 self.ui.status(_('committing subrepository %s\n') %
1178 1178 subrepo.subrelpath(sub))
1179 1179 sr = sub.commit(cctx._text, user, date)
1180 1180 newstate[s] = (newstate[s][0], sr)
1181 1181 subrepo.writestate(self, newstate)
1182 1182
1183 1183 # Save commit message in case this transaction gets rolled back
1184 1184 # (e.g. by a pretxncommit hook). Leave the content alone on
1185 1185 # the assumption that the user will use the same editor again.
1186 1186 msgfn = self.savecommitmessage(cctx._text)
1187 1187
1188 1188 p1, p2 = self.dirstate.parents()
1189 1189 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
1190 1190 try:
1191 1191 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
1192 1192 ret = self.commitctx(cctx, True)
1193 1193 except:
1194 1194 if edited:
1195 1195 self.ui.write(
1196 1196 _('note: commit message saved in %s\n') % msgfn)
1197 1197 raise
1198 1198
1199 1199 # update bookmarks, dirstate and mergestate
1200 1200 bookmarks.update(self, p1, ret)
1201 1201 for f in changes[0] + changes[1]:
1202 1202 self.dirstate.normal(f)
1203 1203 for f in changes[2]:
1204 1204 self.dirstate.drop(f)
1205 1205 self.dirstate.setparents(ret)
1206 1206 ms.reset()
1207 1207 finally:
1208 1208 wlock.release()
1209 1209
1210 1210 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
1211 1211 return ret
1212 1212
1213 1213 def commitctx(self, ctx, error=False):
1214 1214 """Add a new revision to current repository.
1215 1215 Revision information is passed via the context argument.
1216 1216 """
1217 1217
1218 1218 tr = lock = None
1219 1219 removed = list(ctx.removed())
1220 1220 p1, p2 = ctx.p1(), ctx.p2()
1221 1221 user = ctx.user()
1222 1222
1223 1223 lock = self.lock()
1224 1224 try:
1225 1225 tr = self.transaction("commit")
1226 1226 trp = weakref.proxy(tr)
1227 1227
1228 1228 if ctx.files():
1229 1229 m1 = p1.manifest().copy()
1230 1230 m2 = p2.manifest()
1231 1231
1232 1232 # check in files
1233 1233 new = {}
1234 1234 changed = []
1235 1235 linkrev = len(self)
1236 1236 for f in sorted(ctx.modified() + ctx.added()):
1237 1237 self.ui.note(f + "\n")
1238 1238 try:
1239 1239 fctx = ctx[f]
1240 1240 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1241 1241 changed)
1242 1242 m1.set(f, fctx.flags())
1243 1243 except OSError, inst:
1244 1244 self.ui.warn(_("trouble committing %s!\n") % f)
1245 1245 raise
1246 1246 except IOError, inst:
1247 1247 errcode = getattr(inst, 'errno', errno.ENOENT)
1248 1248 if error or errcode and errcode != errno.ENOENT:
1249 1249 self.ui.warn(_("trouble committing %s!\n") % f)
1250 1250 raise
1251 1251 else:
1252 1252 removed.append(f)
1253 1253
1254 1254 # update manifest
1255 1255 m1.update(new)
1256 1256 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1257 1257 drop = [f for f in removed if f in m1]
1258 1258 for f in drop:
1259 1259 del m1[f]
1260 1260 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1261 1261 p2.manifestnode(), (new, drop))
1262 1262 files = changed + removed
1263 1263 else:
1264 1264 mn = p1.manifestnode()
1265 1265 files = []
1266 1266
1267 1267 # update changelog
1268 1268 self.changelog.delayupdate()
1269 1269 n = self.changelog.add(mn, files, ctx.description(),
1270 1270 trp, p1.node(), p2.node(),
1271 1271 user, ctx.date(), ctx.extra().copy())
1272 1272 p = lambda: self.changelog.writepending() and self.root or ""
1273 1273 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1274 1274 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1275 1275 parent2=xp2, pending=p)
1276 1276 self.changelog.finalize(trp)
1277 1277 # set the new commit is proper phase
1278 1278 targetphase = phases.newcommitphase(self.ui)
1279 1279 if targetphase:
1280 1280 # retract boundary do not alter parent changeset.
1281 1281 # if a parent have higher the resulting phase will
1282 1282 # be compliant anyway
1283 1283 #
1284 1284 # if minimal phase was 0 we don't need to retract anything
1285 1285 phases.retractboundary(self, targetphase, [n])
1286 1286 tr.close()
1287 1287 self.updatebranchcache()
1288 1288 return n
1289 1289 finally:
1290 1290 if tr:
1291 1291 tr.release()
1292 1292 lock.release()
1293 1293
1294 1294 def destroyed(self):
1295 1295 '''Inform the repository that nodes have been destroyed.
1296 1296 Intended for use by strip and rollback, so there's a common
1297 1297 place for anything that has to be done after destroying history.'''
1298 1298 # XXX it might be nice if we could take the list of destroyed
1299 1299 # nodes, but I don't see an easy way for rollback() to do that
1300 1300
1301 1301 # Ensure the persistent tag cache is updated. Doing it now
1302 1302 # means that the tag cache only has to worry about destroyed
1303 1303 # heads immediately after a strip/rollback. That in turn
1304 1304 # guarantees that "cachetip == currenttip" (comparing both rev
1305 1305 # and node) always means no nodes have been added or destroyed.
1306 1306
1307 1307 # XXX this is suboptimal when qrefresh'ing: we strip the current
1308 1308 # head, refresh the tag cache, then immediately add a new head.
1309 1309 # But I think doing it this way is necessary for the "instant
1310 1310 # tag cache retrieval" case to work.
1311 1311 self.invalidatecaches()
1312 1312
1313 1313 # Discard all cache entries to force reloading everything.
1314 1314 self._filecache.clear()
1315 1315
1316 1316 def walk(self, match, node=None):
1317 1317 '''
1318 1318 walk recursively through the directory tree or a given
1319 1319 changeset, finding all files matched by the match
1320 1320 function
1321 1321 '''
1322 1322 return self[node].walk(match)
1323 1323
1324 1324 def status(self, node1='.', node2=None, match=None,
1325 1325 ignored=False, clean=False, unknown=False,
1326 1326 listsubrepos=False):
1327 1327 """return status of files between two nodes or node and working directory
1328 1328
1329 1329 If node1 is None, use the first dirstate parent instead.
1330 1330 If node2 is None, compare node1 with working directory.
1331 1331 """
1332 1332
1333 1333 def mfmatches(ctx):
1334 1334 mf = ctx.manifest().copy()
1335 1335 for fn in mf.keys():
1336 1336 if not match(fn):
1337 1337 del mf[fn]
1338 1338 return mf
1339 1339
1340 1340 if isinstance(node1, context.changectx):
1341 1341 ctx1 = node1
1342 1342 else:
1343 1343 ctx1 = self[node1]
1344 1344 if isinstance(node2, context.changectx):
1345 1345 ctx2 = node2
1346 1346 else:
1347 1347 ctx2 = self[node2]
1348 1348
1349 1349 working = ctx2.rev() is None
1350 1350 parentworking = working and ctx1 == self['.']
1351 1351 match = match or matchmod.always(self.root, self.getcwd())
1352 1352 listignored, listclean, listunknown = ignored, clean, unknown
1353 1353
1354 1354 # load earliest manifest first for caching reasons
1355 1355 if not working and ctx2.rev() < ctx1.rev():
1356 1356 ctx2.manifest()
1357 1357
1358 1358 if not parentworking:
1359 1359 def bad(f, msg):
1360 1360 # 'f' may be a directory pattern from 'match.files()',
1361 1361 # so 'f not in ctx1' is not enough
1362 1362 if f not in ctx1 and f not in ctx1.dirs():
1363 1363 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1364 1364 match.bad = bad
1365 1365
1366 1366 if working: # we need to scan the working dir
1367 1367 subrepos = []
1368 1368 if '.hgsub' in self.dirstate:
1369 1369 subrepos = ctx2.substate.keys()
1370 1370 s = self.dirstate.status(match, subrepos, listignored,
1371 1371 listclean, listunknown)
1372 1372 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1373 1373
1374 1374 # check for any possibly clean files
1375 1375 if parentworking and cmp:
1376 1376 fixup = []
1377 1377 # do a full compare of any files that might have changed
1378 1378 for f in sorted(cmp):
1379 1379 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1380 1380 or ctx1[f].cmp(ctx2[f])):
1381 1381 modified.append(f)
1382 1382 else:
1383 1383 fixup.append(f)
1384 1384
1385 1385 # update dirstate for files that are actually clean
1386 1386 if fixup:
1387 1387 if listclean:
1388 1388 clean += fixup
1389 1389
1390 1390 try:
1391 1391 # updating the dirstate is optional
1392 1392 # so we don't wait on the lock
1393 1393 wlock = self.wlock(False)
1394 1394 try:
1395 1395 for f in fixup:
1396 1396 self.dirstate.normal(f)
1397 1397 finally:
1398 1398 wlock.release()
1399 1399 except error.LockError:
1400 1400 pass
1401 1401
1402 1402 if not parentworking:
1403 1403 mf1 = mfmatches(ctx1)
1404 1404 if working:
1405 1405 # we are comparing working dir against non-parent
1406 1406 # generate a pseudo-manifest for the working dir
1407 1407 mf2 = mfmatches(self['.'])
1408 1408 for f in cmp + modified + added:
1409 1409 mf2[f] = None
1410 1410 mf2.set(f, ctx2.flags(f))
1411 1411 for f in removed:
1412 1412 if f in mf2:
1413 1413 del mf2[f]
1414 1414 else:
1415 1415 # we are comparing two revisions
1416 1416 deleted, unknown, ignored = [], [], []
1417 1417 mf2 = mfmatches(ctx2)
1418 1418
1419 1419 modified, added, clean = [], [], []
1420 1420 for fn in mf2:
1421 1421 if fn in mf1:
1422 1422 if (fn not in deleted and
1423 1423 (mf1.flags(fn) != mf2.flags(fn) or
1424 1424 (mf1[fn] != mf2[fn] and
1425 1425 (mf2[fn] or ctx1[fn].cmp(ctx2[fn]))))):
1426 1426 modified.append(fn)
1427 1427 elif listclean:
1428 1428 clean.append(fn)
1429 1429 del mf1[fn]
1430 1430 elif fn not in deleted:
1431 1431 added.append(fn)
1432 1432 removed = mf1.keys()
1433 1433
1434 1434 if working and modified and not self.dirstate._checklink:
1435 1435 # Symlink placeholders may get non-symlink-like contents
1436 1436 # via user error or dereferencing by NFS or Samba servers,
1437 1437 # so we filter out any placeholders that don't look like a
1438 1438 # symlink
1439 1439 sane = []
1440 1440 for f in modified:
1441 1441 if ctx2.flags(f) == 'l':
1442 1442 d = ctx2[f].data()
1443 1443 if len(d) >= 1024 or '\n' in d or util.binary(d):
1444 1444 self.ui.debug('ignoring suspect symlink placeholder'
1445 1445 ' "%s"\n' % f)
1446 1446 continue
1447 1447 sane.append(f)
1448 1448 modified = sane
1449 1449
1450 1450 r = modified, added, removed, deleted, unknown, ignored, clean
1451 1451
1452 1452 if listsubrepos:
1453 1453 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1454 1454 if working:
1455 1455 rev2 = None
1456 1456 else:
1457 1457 rev2 = ctx2.substate[subpath][1]
1458 1458 try:
1459 1459 submatch = matchmod.narrowmatcher(subpath, match)
1460 1460 s = sub.status(rev2, match=submatch, ignored=listignored,
1461 1461 clean=listclean, unknown=listunknown,
1462 1462 listsubrepos=True)
1463 1463 for rfiles, sfiles in zip(r, s):
1464 1464 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1465 1465 except error.LookupError:
1466 1466 self.ui.status(_("skipping missing subrepository: %s\n")
1467 1467 % subpath)
1468 1468
1469 1469 for l in r:
1470 1470 l.sort()
1471 1471 return r
1472 1472
1473 1473 def heads(self, start=None):
1474 1474 heads = self.changelog.heads(start)
1475 1475 # sort the output in rev descending order
1476 1476 return sorted(heads, key=self.changelog.rev, reverse=True)
1477 1477
1478 1478 def branchheads(self, branch=None, start=None, closed=False):
1479 1479 '''return a (possibly filtered) list of heads for the given branch
1480 1480
1481 1481 Heads are returned in topological order, from newest to oldest.
1482 1482 If branch is None, use the dirstate branch.
1483 1483 If start is not None, return only heads reachable from start.
1484 1484 If closed is True, return heads that are marked as closed as well.
1485 1485 '''
1486 1486 if branch is None:
1487 1487 branch = self[None].branch()
1488 1488 branches = self.branchmap()
1489 1489 if branch not in branches:
1490 1490 return []
1491 1491 # the cache returns heads ordered lowest to highest
1492 1492 bheads = list(reversed(branches[branch]))
1493 1493 if start is not None:
1494 1494 # filter out the heads that cannot be reached from startrev
1495 1495 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1496 1496 bheads = [h for h in bheads if h in fbheads]
1497 1497 if not closed:
1498 1498 bheads = [h for h in bheads if
1499 1499 ('close' not in self.changelog.read(h)[5])]
1500 1500 return bheads
1501 1501
1502 1502 def branches(self, nodes):
1503 1503 if not nodes:
1504 1504 nodes = [self.changelog.tip()]
1505 1505 b = []
1506 1506 for n in nodes:
1507 1507 t = n
1508 1508 while True:
1509 1509 p = self.changelog.parents(n)
1510 1510 if p[1] != nullid or p[0] == nullid:
1511 1511 b.append((t, n, p[0], p[1]))
1512 1512 break
1513 1513 n = p[0]
1514 1514 return b
1515 1515
1516 1516 def between(self, pairs):
1517 1517 r = []
1518 1518
1519 1519 for top, bottom in pairs:
1520 1520 n, l, i = top, [], 0
1521 1521 f = 1
1522 1522
1523 1523 while n != bottom and n != nullid:
1524 1524 p = self.changelog.parents(n)[0]
1525 1525 if i == f:
1526 1526 l.append(n)
1527 1527 f = f * 2
1528 1528 n = p
1529 1529 i += 1
1530 1530
1531 1531 r.append(l)
1532 1532
1533 1533 return r
1534 1534
1535 1535 def pull(self, remote, heads=None, force=False):
1536 1536 lock = self.lock()
1537 1537 try:
1538 1538 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1539 1539 force=force)
1540 1540 common, fetch, rheads = tmp
1541 1541 if not fetch:
1542 1542 self.ui.status(_("no changes found\n"))
1543 1543 added = []
1544 1544 result = 0
1545 1545 else:
1546 1546 if heads is None and list(common) == [nullid]:
1547 1547 self.ui.status(_("requesting all changes\n"))
1548 1548 elif heads is None and remote.capable('changegroupsubset'):
1549 1549 # issue1320, avoid a race if remote changed after discovery
1550 1550 heads = rheads
1551 1551
1552 1552 if remote.capable('getbundle'):
1553 1553 cg = remote.getbundle('pull', common=common,
1554 1554 heads=heads or rheads)
1555 1555 elif heads is None:
1556 1556 cg = remote.changegroup(fetch, 'pull')
1557 1557 elif not remote.capable('changegroupsubset'):
1558 1558 raise util.Abort(_("partial pull cannot be done because "
1559 1559 "other repository doesn't support "
1560 1560 "changegroupsubset."))
1561 1561 else:
1562 1562 cg = remote.changegroupsubset(fetch, heads, 'pull')
1563 1563 clstart = len(self.changelog)
1564 1564 result = self.addchangegroup(cg, 'pull', remote.url())
1565 1565 clend = len(self.changelog)
1566 1566 added = [self.changelog.node(r) for r in xrange(clstart, clend)]
1567 1567
1568 1568 # compute target subset
1569 1569 if heads is None:
1570 1570 # We pulled every thing possible
1571 1571 # sync on everything common
1572 1572 subset = common + added
1573 1573 else:
1574 1574 # We pulled a specific subset
1575 1575 # sync on this subset
1576 1576 subset = heads
1577 1577
1578 1578 # Get remote phases data from remote
1579 1579 remotephases = remote.listkeys('phases')
1580 1580 publishing = bool(remotephases.get('publishing', False))
1581 1581 if remotephases and not publishing:
1582 1582 # remote is new and unpublishing
1583 1583 pheads, _dr = phases.analyzeremotephases(self, subset,
1584 1584 remotephases)
1585 1585 phases.advanceboundary(self, phases.public, pheads)
1586 1586 phases.advanceboundary(self, phases.draft, subset)
1587 1587 else:
1588 1588 # Remote is old or publishing all common changesets
1589 1589 # should be seen as public
1590 1590 phases.advanceboundary(self, phases.public, subset)
1591 1591 finally:
1592 1592 lock.release()
1593 1593
1594 1594 return result
1595 1595
1596 1596 def checkpush(self, force, revs):
1597 1597 """Extensions can override this function if additional checks have
1598 1598 to be performed before pushing, or call it if they override push
1599 1599 command.
1600 1600 """
1601 1601 pass
1602 1602
1603 1603 def push(self, remote, force=False, revs=None, newbranch=False):
1604 1604 '''Push outgoing changesets (limited by revs) from the current
1605 1605 repository to remote. Return an integer:
1606 1606 - None means nothing to push
1607 1607 - 0 means HTTP error
1608 1608 - 1 means we pushed and remote head count is unchanged *or*
1609 1609 we have outgoing changesets but refused to push
1610 1610 - other values as described by addchangegroup()
1611 1611 '''
1612 1612 # there are two ways to push to remote repo:
1613 1613 #
1614 1614 # addchangegroup assumes local user can lock remote
1615 1615 # repo (local filesystem, old ssh servers).
1616 1616 #
1617 1617 # unbundle assumes local user cannot lock remote repo (new ssh
1618 1618 # servers, http servers).
1619 1619
1620 1620 # get local lock as we might write phase data
1621 1621 locallock = self.lock()
1622 1622 try:
1623 1623 self.checkpush(force, revs)
1624 1624 lock = None
1625 1625 unbundle = remote.capable('unbundle')
1626 1626 if not unbundle:
1627 1627 lock = remote.lock()
1628 1628 try:
1629 1629 # discovery
1630 1630 fci = discovery.findcommonincoming
1631 1631 commoninc = fci(self, remote, force=force)
1632 1632 common, inc, remoteheads = commoninc
1633 1633 fco = discovery.findcommonoutgoing
1634 1634 outgoing = fco(self, remote, onlyheads=revs,
1635 1635 commoninc=commoninc, force=force)
1636 1636
1637 1637
1638 1638 if not outgoing.missing:
1639 1639 # nothing to push
1640 1640 scmutil.nochangesfound(self.ui, outgoing.excluded)
1641 1641 ret = None
1642 1642 else:
1643 1643 # something to push
1644 1644 if not force:
1645 1645 discovery.checkheads(self, remote, outgoing,
1646 1646 remoteheads, newbranch,
1647 1647 bool(inc))
1648 1648
1649 1649 # create a changegroup from local
1650 1650 if revs is None and not outgoing.excluded:
1651 1651 # push everything,
1652 1652 # use the fast path, no race possible on push
1653 1653 cg = self._changegroup(outgoing.missing, 'push')
1654 1654 else:
1655 1655 cg = self.getlocalbundle('push', outgoing)
1656 1656
1657 1657 # apply changegroup to remote
1658 1658 if unbundle:
1659 1659 # local repo finds heads on server, finds out what
1660 1660 # revs it must push. once revs transferred, if server
1661 1661 # finds it has different heads (someone else won
1662 1662 # commit/push race), server aborts.
1663 1663 if force:
1664 1664 remoteheads = ['force']
1665 1665 # ssh: return remote's addchangegroup()
1666 1666 # http: return remote's addchangegroup() or 0 for error
1667 1667 ret = remote.unbundle(cg, remoteheads, 'push')
1668 1668 else:
1669 1669 # we return an integer indicating remote head count change
1670 1670 ret = remote.addchangegroup(cg, 'push', self.url())
1671 1671
1672 1672 if ret:
1673 1673 # push succeed, synchonize target of the push
1674 1674 cheads = outgoing.missingheads
1675 1675 elif revs is None:
1676 1676 # All out push fails. synchronize all common
1677 1677 cheads = outgoing.commonheads
1678 1678 else:
1679 1679 # I want cheads = heads(::missingheads and ::commonheads)
1680 1680 # (missingheads is revs with secret changeset filtered out)
1681 1681 #
1682 1682 # This can be expressed as:
1683 1683 # cheads = ( (missingheads and ::commonheads)
1684 1684 # + (commonheads and ::missingheads))"
1685 1685 # )
1686 1686 #
1687 1687 # while trying to push we already computed the following:
1688 1688 # common = (::commonheads)
1689 1689 # missing = ((commonheads::missingheads) - commonheads)
1690 1690 #
1691 1691 # We can pick:
1692 1692 # * missingheads part of comon (::commonheads)
1693 1693 common = set(outgoing.common)
1694 1694 cheads = [node for node in revs if node in common]
1695 1695 # and
1696 1696 # * commonheads parents on missing
1697 1697 revset = self.set('%ln and parents(roots(%ln))',
1698 1698 outgoing.commonheads,
1699 1699 outgoing.missing)
1700 1700 cheads.extend(c.node() for c in revset)
1701 1701 # even when we don't push, exchanging phase data is useful
1702 1702 remotephases = remote.listkeys('phases')
1703 1703 if not remotephases: # old server or public only repo
1704 1704 phases.advanceboundary(self, phases.public, cheads)
1705 1705 # don't push any phase data as there is nothing to push
1706 1706 else:
1707 1707 ana = phases.analyzeremotephases(self, cheads, remotephases)
1708 1708 pheads, droots = ana
1709 1709 ### Apply remote phase on local
1710 1710 if remotephases.get('publishing', False):
1711 1711 phases.advanceboundary(self, phases.public, cheads)
1712 1712 else: # publish = False
1713 1713 phases.advanceboundary(self, phases.public, pheads)
1714 1714 phases.advanceboundary(self, phases.draft, cheads)
1715 1715 ### Apply local phase on remote
1716 1716
1717 1717 # Get the list of all revs draft on remote by public here.
1718 1718 # XXX Beware that revset break if droots is not strictly
1719 1719 # XXX root we may want to ensure it is but it is costly
1720 1720 outdated = self.set('heads((%ln::%ln) and public())',
1721 1721 droots, cheads)
1722 1722 for newremotehead in outdated:
1723 1723 r = remote.pushkey('phases',
1724 1724 newremotehead.hex(),
1725 1725 str(phases.draft),
1726 1726 str(phases.public))
1727 1727 if not r:
1728 1728 self.ui.warn(_('updating %s to public failed!\n')
1729 1729 % newremotehead)
1730 1730 finally:
1731 1731 if lock is not None:
1732 1732 lock.release()
1733 1733 finally:
1734 1734 locallock.release()
1735 1735
1736 1736 self.ui.debug("checking for updated bookmarks\n")
1737 1737 rb = remote.listkeys('bookmarks')
1738 1738 for k in rb.keys():
1739 1739 if k in self._bookmarks:
1740 1740 nr, nl = rb[k], hex(self._bookmarks[k])
1741 1741 if nr in self:
1742 1742 cr = self[nr]
1743 1743 cl = self[nl]
1744 1744 if cl in cr.descendants():
1745 1745 r = remote.pushkey('bookmarks', k, nr, nl)
1746 1746 if r:
1747 1747 self.ui.status(_("updating bookmark %s\n") % k)
1748 1748 else:
1749 1749 self.ui.warn(_('updating bookmark %s'
1750 1750 ' failed!\n') % k)
1751 1751
1752 1752 return ret
1753 1753
1754 1754 def changegroupinfo(self, nodes, source):
1755 1755 if self.ui.verbose or source == 'bundle':
1756 1756 self.ui.status(_("%d changesets found\n") % len(nodes))
1757 1757 if self.ui.debugflag:
1758 1758 self.ui.debug("list of changesets:\n")
1759 1759 for node in nodes:
1760 1760 self.ui.debug("%s\n" % hex(node))
1761 1761
1762 1762 def changegroupsubset(self, bases, heads, source):
1763 1763 """Compute a changegroup consisting of all the nodes that are
1764 1764 descendants of any of the bases and ancestors of any of the heads.
1765 1765 Return a chunkbuffer object whose read() method will return
1766 1766 successive changegroup chunks.
1767 1767
1768 1768 It is fairly complex as determining which filenodes and which
1769 1769 manifest nodes need to be included for the changeset to be complete
1770 1770 is non-trivial.
1771 1771
1772 1772 Another wrinkle is doing the reverse, figuring out which changeset in
1773 1773 the changegroup a particular filenode or manifestnode belongs to.
1774 1774 """
1775 1775 cl = self.changelog
1776 1776 if not bases:
1777 1777 bases = [nullid]
1778 1778 csets, bases, heads = cl.nodesbetween(bases, heads)
1779 1779 # We assume that all ancestors of bases are known
1780 1780 common = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1781 1781 return self._changegroupsubset(common, csets, heads, source)
1782 1782
1783 1783 def getlocalbundle(self, source, outgoing):
1784 1784 """Like getbundle, but taking a discovery.outgoing as an argument.
1785 1785
1786 1786 This is only implemented for local repos and reuses potentially
1787 1787 precomputed sets in outgoing."""
1788 1788 if not outgoing.missing:
1789 1789 return None
1790 1790 return self._changegroupsubset(outgoing.common,
1791 1791 outgoing.missing,
1792 1792 outgoing.missingheads,
1793 1793 source)
1794 1794
1795 1795 def getbundle(self, source, heads=None, common=None):
1796 1796 """Like changegroupsubset, but returns the set difference between the
1797 1797 ancestors of heads and the ancestors common.
1798 1798
1799 1799 If heads is None, use the local heads. If common is None, use [nullid].
1800 1800
1801 1801 The nodes in common might not all be known locally due to the way the
1802 1802 current discovery protocol works.
1803 1803 """
1804 1804 cl = self.changelog
1805 1805 if common:
1806 1806 nm = cl.nodemap
1807 1807 common = [n for n in common if n in nm]
1808 1808 else:
1809 1809 common = [nullid]
1810 1810 if not heads:
1811 1811 heads = cl.heads()
1812 1812 return self.getlocalbundle(source,
1813 1813 discovery.outgoing(cl, common, heads))
1814 1814
1815 1815 def _changegroupsubset(self, commonrevs, csets, heads, source):
1816 1816
1817 1817 cl = self.changelog
1818 1818 mf = self.manifest
1819 1819 mfs = {} # needed manifests
1820 1820 fnodes = {} # needed file nodes
1821 1821 changedfiles = set()
1822 1822 fstate = ['', {}]
1823 1823 count = [0, 0]
1824 1824
1825 1825 # can we go through the fast path ?
1826 1826 heads.sort()
1827 1827 if heads == sorted(self.heads()):
1828 1828 return self._changegroup(csets, source)
1829 1829
1830 1830 # slow path
1831 1831 self.hook('preoutgoing', throw=True, source=source)
1832 1832 self.changegroupinfo(csets, source)
1833 1833
1834 1834 # filter any nodes that claim to be part of the known set
1835 1835 def prune(revlog, missing):
1836 1836 return [n for n in missing
1837 1837 if revlog.linkrev(revlog.rev(n)) not in commonrevs]
1838 1838
1839 1839 progress = self.ui.progress
1840 1840 _bundling = _('bundling')
1841 1841 _changesets = _('changesets')
1842 1842 _manifests = _('manifests')
1843 1843 _files = _('files')
1844 1844
1845 1845 def lookup(revlog, x):
1846 1846 if revlog == cl:
1847 1847 c = cl.read(x)
1848 1848 changedfiles.update(c[3])
1849 1849 mfs.setdefault(c[0], x)
1850 1850 count[0] += 1
1851 1851 progress(_bundling, count[0],
1852 1852 unit=_changesets, total=count[1])
1853 1853 return x
1854 1854 elif revlog == mf:
1855 1855 clnode = mfs[x]
1856 1856 mdata = mf.readfast(x)
1857 1857 for f, n in mdata.iteritems():
1858 1858 if f in changedfiles:
1859 1859 fnodes[f].setdefault(n, clnode)
1860 1860 count[0] += 1
1861 1861 progress(_bundling, count[0],
1862 1862 unit=_manifests, total=count[1])
1863 1863 return clnode
1864 1864 else:
1865 1865 progress(_bundling, count[0], item=fstate[0],
1866 1866 unit=_files, total=count[1])
1867 1867 return fstate[1][x]
1868 1868
1869 1869 bundler = changegroup.bundle10(lookup)
1870 1870 reorder = self.ui.config('bundle', 'reorder', 'auto')
1871 1871 if reorder == 'auto':
1872 1872 reorder = None
1873 1873 else:
1874 1874 reorder = util.parsebool(reorder)
1875 1875
1876 1876 def gengroup():
1877 1877 # Create a changenode group generator that will call our functions
1878 1878 # back to lookup the owning changenode and collect information.
1879 1879 count[:] = [0, len(csets)]
1880 1880 for chunk in cl.group(csets, bundler, reorder=reorder):
1881 1881 yield chunk
1882 1882 progress(_bundling, None)
1883 1883
1884 1884 # Create a generator for the manifestnodes that calls our lookup
1885 1885 # and data collection functions back.
1886 1886 for f in changedfiles:
1887 1887 fnodes[f] = {}
1888 1888 count[:] = [0, len(mfs)]
1889 1889 for chunk in mf.group(prune(mf, mfs), bundler, reorder=reorder):
1890 1890 yield chunk
1891 1891 progress(_bundling, None)
1892 1892
1893 1893 mfs.clear()
1894 1894
1895 1895 # Go through all our files in order sorted by name.
1896 1896 count[:] = [0, len(changedfiles)]
1897 1897 for fname in sorted(changedfiles):
1898 1898 filerevlog = self.file(fname)
1899 1899 if not len(filerevlog):
1900 1900 raise util.Abort(_("empty or missing revlog for %s") % fname)
1901 1901 fstate[0] = fname
1902 1902 fstate[1] = fnodes.pop(fname, {})
1903 1903
1904 1904 nodelist = prune(filerevlog, fstate[1])
1905 1905 if nodelist:
1906 1906 count[0] += 1
1907 1907 yield bundler.fileheader(fname)
1908 1908 for chunk in filerevlog.group(nodelist, bundler, reorder):
1909 1909 yield chunk
1910 1910
1911 1911 # Signal that no more groups are left.
1912 1912 yield bundler.close()
1913 1913 progress(_bundling, None)
1914 1914
1915 1915 if csets:
1916 1916 self.hook('outgoing', node=hex(csets[0]), source=source)
1917 1917
1918 1918 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1919 1919
1920 1920 def changegroup(self, basenodes, source):
1921 1921 # to avoid a race we use changegroupsubset() (issue1320)
1922 1922 return self.changegroupsubset(basenodes, self.heads(), source)
1923 1923
1924 1924 def _changegroup(self, nodes, source):
1925 1925 """Compute the changegroup of all nodes that we have that a recipient
1926 1926 doesn't. Return a chunkbuffer object whose read() method will return
1927 1927 successive changegroup chunks.
1928 1928
1929 1929 This is much easier than the previous function as we can assume that
1930 1930 the recipient has any changenode we aren't sending them.
1931 1931
1932 1932 nodes is the set of nodes to send"""
1933 1933
1934 1934 cl = self.changelog
1935 1935 mf = self.manifest
1936 1936 mfs = {}
1937 1937 changedfiles = set()
1938 1938 fstate = ['']
1939 1939 count = [0, 0]
1940 1940
1941 1941 self.hook('preoutgoing', throw=True, source=source)
1942 1942 self.changegroupinfo(nodes, source)
1943 1943
1944 1944 revset = set([cl.rev(n) for n in nodes])
1945 1945
1946 1946 def gennodelst(log):
1947 return [log.node(r) for r in log if log.linkrev(r) in revset]
1947 ln, llr = log.node, log.linkrev
1948 return [ln(r) for r in log if llr(r) in revset]
1949
1948 1950 progress = self.ui.progress
1949 1951 _bundling = _('bundling')
1950 1952 _changesets = _('changesets')
1951 1953 _manifests = _('manifests')
1952 1954 _files = _('files')
1953 1955
1954 1956 def lookup(revlog, x):
1955 1957 if revlog == cl:
1956 1958 c = cl.read(x)
1957 1959 changedfiles.update(c[3])
1958 1960 mfs.setdefault(c[0], x)
1959 1961 count[0] += 1
1960 1962 progress(_bundling, count[0],
1961 1963 unit=_changesets, total=count[1])
1962 1964 return x
1963 1965 elif revlog == mf:
1964 1966 count[0] += 1
1965 1967 progress(_bundling, count[0],
1966 1968 unit=_manifests, total=count[1])
1967 1969 return cl.node(revlog.linkrev(revlog.rev(x)))
1968 1970 else:
1969 1971 progress(_bundling, count[0], item=fstate[0],
1970 1972 total=count[1], unit=_files)
1971 1973 return cl.node(revlog.linkrev(revlog.rev(x)))
1972 1974
1973 1975 bundler = changegroup.bundle10(lookup)
1974 1976 reorder = self.ui.config('bundle', 'reorder', 'auto')
1975 1977 if reorder == 'auto':
1976 1978 reorder = None
1977 1979 else:
1978 1980 reorder = util.parsebool(reorder)
1979 1981
1980 1982 def gengroup():
1981 1983 '''yield a sequence of changegroup chunks (strings)'''
1982 1984 # construct a list of all changed files
1983 1985
1984 1986 count[:] = [0, len(nodes)]
1985 1987 for chunk in cl.group(nodes, bundler, reorder=reorder):
1986 1988 yield chunk
1987 1989 progress(_bundling, None)
1988 1990
1989 1991 count[:] = [0, len(mfs)]
1990 1992 for chunk in mf.group(gennodelst(mf), bundler, reorder=reorder):
1991 1993 yield chunk
1992 1994 progress(_bundling, None)
1993 1995
1994 1996 count[:] = [0, len(changedfiles)]
1995 1997 for fname in sorted(changedfiles):
1996 1998 filerevlog = self.file(fname)
1997 1999 if not len(filerevlog):
1998 2000 raise util.Abort(_("empty or missing revlog for %s") % fname)
1999 2001 fstate[0] = fname
2000 2002 nodelist = gennodelst(filerevlog)
2001 2003 if nodelist:
2002 2004 count[0] += 1
2003 2005 yield bundler.fileheader(fname)
2004 2006 for chunk in filerevlog.group(nodelist, bundler, reorder):
2005 2007 yield chunk
2006 2008 yield bundler.close()
2007 2009 progress(_bundling, None)
2008 2010
2009 2011 if nodes:
2010 2012 self.hook('outgoing', node=hex(nodes[0]), source=source)
2011 2013
2012 2014 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
2013 2015
2014 2016 def addchangegroup(self, source, srctype, url, emptyok=False):
2015 2017 """Add the changegroup returned by source.read() to this repo.
2016 2018 srctype is a string like 'push', 'pull', or 'unbundle'. url is
2017 2019 the URL of the repo where this changegroup is coming from.
2018 2020
2019 2021 Return an integer summarizing the change to this repo:
2020 2022 - nothing changed or no source: 0
2021 2023 - more heads than before: 1+added heads (2..n)
2022 2024 - fewer heads than before: -1-removed heads (-2..-n)
2023 2025 - number of heads stays the same: 1
2024 2026 """
2025 2027 def csmap(x):
2026 2028 self.ui.debug("add changeset %s\n" % short(x))
2027 2029 return len(cl)
2028 2030
2029 2031 def revmap(x):
2030 2032 return cl.rev(x)
2031 2033
2032 2034 if not source:
2033 2035 return 0
2034 2036
2035 2037 self.hook('prechangegroup', throw=True, source=srctype, url=url)
2036 2038
2037 2039 changesets = files = revisions = 0
2038 2040 efiles = set()
2039 2041
2040 2042 # write changelog data to temp files so concurrent readers will not see
2041 2043 # inconsistent view
2042 2044 cl = self.changelog
2043 2045 cl.delayupdate()
2044 2046 oldheads = cl.heads()
2045 2047
2046 2048 tr = self.transaction("\n".join([srctype, util.hidepassword(url)]))
2047 2049 try:
2048 2050 trp = weakref.proxy(tr)
2049 2051 # pull off the changeset group
2050 2052 self.ui.status(_("adding changesets\n"))
2051 2053 clstart = len(cl)
2052 2054 class prog(object):
2053 2055 step = _('changesets')
2054 2056 count = 1
2055 2057 ui = self.ui
2056 2058 total = None
2057 2059 def __call__(self):
2058 2060 self.ui.progress(self.step, self.count, unit=_('chunks'),
2059 2061 total=self.total)
2060 2062 self.count += 1
2061 2063 pr = prog()
2062 2064 source.callback = pr
2063 2065
2064 2066 source.changelogheader()
2065 2067 srccontent = cl.addgroup(source, csmap, trp)
2066 2068 if not (srccontent or emptyok):
2067 2069 raise util.Abort(_("received changelog group is empty"))
2068 2070 clend = len(cl)
2069 2071 changesets = clend - clstart
2070 2072 for c in xrange(clstart, clend):
2071 2073 efiles.update(self[c].files())
2072 2074 efiles = len(efiles)
2073 2075 self.ui.progress(_('changesets'), None)
2074 2076
2075 2077 # pull off the manifest group
2076 2078 self.ui.status(_("adding manifests\n"))
2077 2079 pr.step = _('manifests')
2078 2080 pr.count = 1
2079 2081 pr.total = changesets # manifests <= changesets
2080 2082 # no need to check for empty manifest group here:
2081 2083 # if the result of the merge of 1 and 2 is the same in 3 and 4,
2082 2084 # no new manifest will be created and the manifest group will
2083 2085 # be empty during the pull
2084 2086 source.manifestheader()
2085 2087 self.manifest.addgroup(source, revmap, trp)
2086 2088 self.ui.progress(_('manifests'), None)
2087 2089
2088 2090 needfiles = {}
2089 2091 if self.ui.configbool('server', 'validate', default=False):
2090 2092 # validate incoming csets have their manifests
2091 2093 for cset in xrange(clstart, clend):
2092 2094 mfest = self.changelog.read(self.changelog.node(cset))[0]
2093 2095 mfest = self.manifest.readdelta(mfest)
2094 2096 # store file nodes we must see
2095 2097 for f, n in mfest.iteritems():
2096 2098 needfiles.setdefault(f, set()).add(n)
2097 2099
2098 2100 # process the files
2099 2101 self.ui.status(_("adding file changes\n"))
2100 2102 pr.step = _('files')
2101 2103 pr.count = 1
2102 2104 pr.total = efiles
2103 2105 source.callback = None
2104 2106
2105 2107 while True:
2106 2108 chunkdata = source.filelogheader()
2107 2109 if not chunkdata:
2108 2110 break
2109 2111 f = chunkdata["filename"]
2110 2112 self.ui.debug("adding %s revisions\n" % f)
2111 2113 pr()
2112 2114 fl = self.file(f)
2113 2115 o = len(fl)
2114 2116 if not fl.addgroup(source, revmap, trp):
2115 2117 raise util.Abort(_("received file revlog group is empty"))
2116 2118 revisions += len(fl) - o
2117 2119 files += 1
2118 2120 if f in needfiles:
2119 2121 needs = needfiles[f]
2120 2122 for new in xrange(o, len(fl)):
2121 2123 n = fl.node(new)
2122 2124 if n in needs:
2123 2125 needs.remove(n)
2124 2126 if not needs:
2125 2127 del needfiles[f]
2126 2128 self.ui.progress(_('files'), None)
2127 2129
2128 2130 for f, needs in needfiles.iteritems():
2129 2131 fl = self.file(f)
2130 2132 for n in needs:
2131 2133 try:
2132 2134 fl.rev(n)
2133 2135 except error.LookupError:
2134 2136 raise util.Abort(
2135 2137 _('missing file data for %s:%s - run hg verify') %
2136 2138 (f, hex(n)))
2137 2139
2138 2140 dh = 0
2139 2141 if oldheads:
2140 2142 heads = cl.heads()
2141 2143 dh = len(heads) - len(oldheads)
2142 2144 for h in heads:
2143 2145 if h not in oldheads and 'close' in self[h].extra():
2144 2146 dh -= 1
2145 2147 htext = ""
2146 2148 if dh:
2147 2149 htext = _(" (%+d heads)") % dh
2148 2150
2149 2151 self.ui.status(_("added %d changesets"
2150 2152 " with %d changes to %d files%s\n")
2151 2153 % (changesets, revisions, files, htext))
2152 2154
2153 2155 if changesets > 0:
2154 2156 p = lambda: cl.writepending() and self.root or ""
2155 2157 self.hook('pretxnchangegroup', throw=True,
2156 2158 node=hex(cl.node(clstart)), source=srctype,
2157 2159 url=url, pending=p)
2158 2160
2159 2161 added = [cl.node(r) for r in xrange(clstart, clend)]
2160 2162 publishing = self.ui.configbool('phases', 'publish', True)
2161 2163 if srctype == 'push':
2162 2164 # Old server can not push the boundary themself.
2163 2165 # New server won't push the boundary if changeset already
2164 2166 # existed locally as secrete
2165 2167 #
2166 2168 # We should not use added here but the list of all change in
2167 2169 # the bundle
2168 2170 if publishing:
2169 2171 phases.advanceboundary(self, phases.public, srccontent)
2170 2172 else:
2171 2173 phases.advanceboundary(self, phases.draft, srccontent)
2172 2174 phases.retractboundary(self, phases.draft, added)
2173 2175 elif srctype != 'strip':
2174 2176 # publishing only alter behavior during push
2175 2177 #
2176 2178 # strip should not touch boundary at all
2177 2179 phases.retractboundary(self, phases.draft, added)
2178 2180
2179 2181 # make changelog see real files again
2180 2182 cl.finalize(trp)
2181 2183
2182 2184 tr.close()
2183 2185
2184 2186 if changesets > 0:
2185 2187 def runhooks():
2186 2188 # forcefully update the on-disk branch cache
2187 2189 self.ui.debug("updating the branch cache\n")
2188 2190 self.updatebranchcache()
2189 2191 self.hook("changegroup", node=hex(cl.node(clstart)),
2190 2192 source=srctype, url=url)
2191 2193
2192 2194 for n in added:
2193 2195 self.hook("incoming", node=hex(n), source=srctype,
2194 2196 url=url)
2195 2197 self._afterlock(runhooks)
2196 2198
2197 2199 finally:
2198 2200 tr.release()
2199 2201 # never return 0 here:
2200 2202 if dh < 0:
2201 2203 return dh - 1
2202 2204 else:
2203 2205 return dh + 1
2204 2206
2205 2207 def stream_in(self, remote, requirements):
2206 2208 lock = self.lock()
2207 2209 try:
2208 2210 fp = remote.stream_out()
2209 2211 l = fp.readline()
2210 2212 try:
2211 2213 resp = int(l)
2212 2214 except ValueError:
2213 2215 raise error.ResponseError(
2214 2216 _('Unexpected response from remote server:'), l)
2215 2217 if resp == 1:
2216 2218 raise util.Abort(_('operation forbidden by server'))
2217 2219 elif resp == 2:
2218 2220 raise util.Abort(_('locking the remote repository failed'))
2219 2221 elif resp != 0:
2220 2222 raise util.Abort(_('the server sent an unknown error code'))
2221 2223 self.ui.status(_('streaming all changes\n'))
2222 2224 l = fp.readline()
2223 2225 try:
2224 2226 total_files, total_bytes = map(int, l.split(' ', 1))
2225 2227 except (ValueError, TypeError):
2226 2228 raise error.ResponseError(
2227 2229 _('Unexpected response from remote server:'), l)
2228 2230 self.ui.status(_('%d files to transfer, %s of data\n') %
2229 2231 (total_files, util.bytecount(total_bytes)))
2230 2232 start = time.time()
2231 2233 for i in xrange(total_files):
2232 2234 # XXX doesn't support '\n' or '\r' in filenames
2233 2235 l = fp.readline()
2234 2236 try:
2235 2237 name, size = l.split('\0', 1)
2236 2238 size = int(size)
2237 2239 except (ValueError, TypeError):
2238 2240 raise error.ResponseError(
2239 2241 _('Unexpected response from remote server:'), l)
2240 2242 if self.ui.debugflag:
2241 2243 self.ui.debug('adding %s (%s)\n' %
2242 2244 (name, util.bytecount(size)))
2243 2245 # for backwards compat, name was partially encoded
2244 2246 ofp = self.sopener(store.decodedir(name), 'w')
2245 2247 for chunk in util.filechunkiter(fp, limit=size):
2246 2248 ofp.write(chunk)
2247 2249 ofp.close()
2248 2250 elapsed = time.time() - start
2249 2251 if elapsed <= 0:
2250 2252 elapsed = 0.001
2251 2253 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2252 2254 (util.bytecount(total_bytes), elapsed,
2253 2255 util.bytecount(total_bytes / elapsed)))
2254 2256
2255 2257 # new requirements = old non-format requirements + new format-related
2256 2258 # requirements from the streamed-in repository
2257 2259 requirements.update(set(self.requirements) - self.supportedformats)
2258 2260 self._applyrequirements(requirements)
2259 2261 self._writerequirements()
2260 2262
2261 2263 self.invalidate()
2262 2264 return len(self.heads()) + 1
2263 2265 finally:
2264 2266 lock.release()
2265 2267
2266 2268 def clone(self, remote, heads=[], stream=False):
2267 2269 '''clone remote repository.
2268 2270
2269 2271 keyword arguments:
2270 2272 heads: list of revs to clone (forces use of pull)
2271 2273 stream: use streaming clone if possible'''
2272 2274
2273 2275 # now, all clients that can request uncompressed clones can
2274 2276 # read repo formats supported by all servers that can serve
2275 2277 # them.
2276 2278
2277 2279 # if revlog format changes, client will have to check version
2278 2280 # and format flags on "stream" capability, and use
2279 2281 # uncompressed only if compatible.
2280 2282
2281 2283 if not stream:
2282 2284 # if the server explicitely prefer to stream (for fast LANs)
2283 2285 stream = remote.capable('stream-preferred')
2284 2286
2285 2287 if stream and not heads:
2286 2288 # 'stream' means remote revlog format is revlogv1 only
2287 2289 if remote.capable('stream'):
2288 2290 return self.stream_in(remote, set(('revlogv1',)))
2289 2291 # otherwise, 'streamreqs' contains the remote revlog format
2290 2292 streamreqs = remote.capable('streamreqs')
2291 2293 if streamreqs:
2292 2294 streamreqs = set(streamreqs.split(','))
2293 2295 # if we support it, stream in and adjust our requirements
2294 2296 if not streamreqs - self.supportedformats:
2295 2297 return self.stream_in(remote, streamreqs)
2296 2298 return self.pull(remote, heads)
2297 2299
2298 2300 def pushkey(self, namespace, key, old, new):
2299 2301 self.hook('prepushkey', throw=True, namespace=namespace, key=key,
2300 2302 old=old, new=new)
2301 2303 ret = pushkey.push(self, namespace, key, old, new)
2302 2304 self.hook('pushkey', namespace=namespace, key=key, old=old, new=new,
2303 2305 ret=ret)
2304 2306 return ret
2305 2307
2306 2308 def listkeys(self, namespace):
2307 2309 self.hook('prelistkeys', throw=True, namespace=namespace)
2308 2310 values = pushkey.list(self, namespace)
2309 2311 self.hook('listkeys', namespace=namespace, values=values)
2310 2312 return values
2311 2313
2312 2314 def debugwireargs(self, one, two, three=None, four=None, five=None):
2313 2315 '''used to test argument passing over the wire'''
2314 2316 return "%s %s %s %s %s" % (one, two, three, four, five)
2315 2317
2316 2318 def savecommitmessage(self, text):
2317 2319 fp = self.opener('last-message.txt', 'wb')
2318 2320 try:
2319 2321 fp.write(text)
2320 2322 finally:
2321 2323 fp.close()
2322 2324 return self.pathto(fp.name[len(self.root)+1:])
2323 2325
2324 2326 # used to avoid circular references so destructors work
2325 2327 def aftertrans(files):
2326 2328 renamefiles = [tuple(t) for t in files]
2327 2329 def a():
2328 2330 for src, dest in renamefiles:
2329 2331 util.rename(src, dest)
2330 2332 return a
2331 2333
2332 2334 def undoname(fn):
2333 2335 base, name = os.path.split(fn)
2334 2336 assert name.startswith('journal')
2335 2337 return os.path.join(base, name.replace('journal', 'undo', 1))
2336 2338
2337 2339 def instance(ui, path, create):
2338 2340 return localrepository(ui, util.urllocalpath(path), create)
2339 2341
2340 2342 def islocal(path):
2341 2343 return True
General Comments 0
You need to be logged in to leave comments. Login now