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