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