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