##// END OF EJS Templates
localrepo: use lock.release for single lock
Simon Heimberg -
r8646:60f9e574 default
parent child Browse files
Show More
@@ -1,2134 +1,2132 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, incorporated herein by reference.
7 7
8 8 from node import bin, hex, nullid, nullrev, short
9 9 from i18n import _
10 10 import repo, changegroup
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 match_
15 15 import merge as merge_
16 16 from lock import release
17 17 import weakref, stat, errno, os, time, inspect
18 18 propertycache = util.propertycache
19 19
20 20 class localrepository(repo.repository):
21 21 capabilities = set(('lookup', 'changegroupsubset', 'branchmap'))
22 22 supported = set('revlogv1 store fncache'.split())
23 23
24 24 def __init__(self, baseui, path=None, create=0):
25 25 repo.repository.__init__(self)
26 26 self.root = os.path.realpath(path)
27 27 self.path = os.path.join(self.root, ".hg")
28 28 self.origroot = path
29 29 self.opener = util.opener(self.path)
30 30 self.wopener = util.opener(self.root)
31 31
32 32 if not os.path.isdir(self.path):
33 33 if create:
34 34 if not os.path.exists(path):
35 35 os.mkdir(path)
36 36 os.mkdir(self.path)
37 37 requirements = ["revlogv1"]
38 38 if baseui.configbool('format', 'usestore', True):
39 39 os.mkdir(os.path.join(self.path, "store"))
40 40 requirements.append("store")
41 41 if baseui.configbool('format', 'usefncache', True):
42 42 requirements.append("fncache")
43 43 # create an invalid changelog
44 44 self.opener("00changelog.i", "a").write(
45 45 '\0\0\0\2' # represents revlogv2
46 46 ' dummy changelog to prevent using the old repo layout'
47 47 )
48 48 reqfile = self.opener("requires", "w")
49 49 for r in requirements:
50 50 reqfile.write("%s\n" % r)
51 51 reqfile.close()
52 52 else:
53 53 raise error.RepoError(_("repository %s not found") % path)
54 54 elif create:
55 55 raise error.RepoError(_("repository %s already exists") % path)
56 56 else:
57 57 # find requirements
58 58 requirements = set()
59 59 try:
60 60 requirements = set(self.opener("requires").read().splitlines())
61 61 except IOError, inst:
62 62 if inst.errno != errno.ENOENT:
63 63 raise
64 64 for r in requirements - self.supported:
65 65 raise error.RepoError(_("requirement '%s' not supported") % r)
66 66
67 67 self.store = store.store(requirements, self.path, util.opener)
68 68 self.spath = self.store.path
69 69 self.sopener = self.store.opener
70 70 self.sjoin = self.store.join
71 71 self.opener.createmode = self.store.createmode
72 72
73 73 self.baseui = baseui
74 74 self.ui = baseui.copy()
75 75 try:
76 76 self.ui.readconfig(self.join("hgrc"), self.root)
77 77 extensions.loadall(self.ui)
78 78 except IOError:
79 79 pass
80 80
81 81 self.tagscache = None
82 82 self._tagstypecache = None
83 83 self.branchcache = None
84 84 self._ubranchcache = None # UTF-8 version of branchcache
85 85 self._branchcachetip = None
86 86 self.nodetagscache = None
87 87 self.filterpats = {}
88 88 self._datafilters = {}
89 89 self._transref = self._lockref = self._wlockref = None
90 90
91 91 @propertycache
92 92 def changelog(self):
93 93 c = changelog.changelog(self.sopener)
94 94 if 'HG_PENDING' in os.environ:
95 95 p = os.environ['HG_PENDING']
96 96 if p.startswith(self.root):
97 97 c.readpending('00changelog.i.a')
98 98 self.sopener.defversion = c.version
99 99 return c
100 100
101 101 @propertycache
102 102 def manifest(self):
103 103 return manifest.manifest(self.sopener)
104 104
105 105 @propertycache
106 106 def dirstate(self):
107 107 return dirstate.dirstate(self.opener, self.ui, self.root)
108 108
109 109 def __getitem__(self, changeid):
110 110 if changeid is None:
111 111 return context.workingctx(self)
112 112 return context.changectx(self, changeid)
113 113
114 114 def __nonzero__(self):
115 115 return True
116 116
117 117 def __len__(self):
118 118 return len(self.changelog)
119 119
120 120 def __iter__(self):
121 121 for i in xrange(len(self)):
122 122 yield i
123 123
124 124 def url(self):
125 125 return 'file:' + self.root
126 126
127 127 def hook(self, name, throw=False, **args):
128 128 return hook.hook(self.ui, self, name, throw, **args)
129 129
130 130 tag_disallowed = ':\r\n'
131 131
132 132 def _tag(self, names, node, message, local, user, date, extra={}):
133 133 if isinstance(names, str):
134 134 allchars = names
135 135 names = (names,)
136 136 else:
137 137 allchars = ''.join(names)
138 138 for c in self.tag_disallowed:
139 139 if c in allchars:
140 140 raise util.Abort(_('%r cannot be used in a tag name') % c)
141 141
142 142 for name in names:
143 143 self.hook('pretag', throw=True, node=hex(node), tag=name,
144 144 local=local)
145 145
146 146 def writetags(fp, names, munge, prevtags):
147 147 fp.seek(0, 2)
148 148 if prevtags and prevtags[-1] != '\n':
149 149 fp.write('\n')
150 150 for name in names:
151 151 m = munge and munge(name) or name
152 152 if self._tagstypecache and name in self._tagstypecache:
153 153 old = self.tagscache.get(name, nullid)
154 154 fp.write('%s %s\n' % (hex(old), m))
155 155 fp.write('%s %s\n' % (hex(node), m))
156 156 fp.close()
157 157
158 158 prevtags = ''
159 159 if local:
160 160 try:
161 161 fp = self.opener('localtags', 'r+')
162 162 except IOError:
163 163 fp = self.opener('localtags', 'a')
164 164 else:
165 165 prevtags = fp.read()
166 166
167 167 # local tags are stored in the current charset
168 168 writetags(fp, names, None, prevtags)
169 169 for name in names:
170 170 self.hook('tag', node=hex(node), tag=name, local=local)
171 171 return
172 172
173 173 try:
174 174 fp = self.wfile('.hgtags', 'rb+')
175 175 except IOError:
176 176 fp = self.wfile('.hgtags', 'ab')
177 177 else:
178 178 prevtags = fp.read()
179 179
180 180 # committed tags are stored in UTF-8
181 181 writetags(fp, names, encoding.fromlocal, prevtags)
182 182
183 183 if '.hgtags' not in self.dirstate:
184 184 self.add(['.hgtags'])
185 185
186 186 tagnode = self.commit(['.hgtags'], message, user, date, extra=extra)
187 187
188 188 for name in names:
189 189 self.hook('tag', node=hex(node), tag=name, local=local)
190 190
191 191 return tagnode
192 192
193 193 def tag(self, names, node, message, local, user, date):
194 194 '''tag a revision with one or more symbolic names.
195 195
196 196 names is a list of strings or, when adding a single tag, names may be a
197 197 string.
198 198
199 199 if local is True, the tags are stored in a per-repository file.
200 200 otherwise, they are stored in the .hgtags file, and a new
201 201 changeset is committed with the change.
202 202
203 203 keyword arguments:
204 204
205 205 local: whether to store tags in non-version-controlled file
206 206 (default False)
207 207
208 208 message: commit message to use if committing
209 209
210 210 user: name of user to use if committing
211 211
212 212 date: date tuple to use if committing'''
213 213
214 214 for x in self.status()[:5]:
215 215 if '.hgtags' in x:
216 216 raise util.Abort(_('working copy of .hgtags is changed '
217 217 '(please commit .hgtags manually)'))
218 218
219 219 self.tags() # instantiate the cache
220 220 self._tag(names, node, message, local, user, date)
221 221
222 222 def tags(self):
223 223 '''return a mapping of tag to node'''
224 224 if self.tagscache:
225 225 return self.tagscache
226 226
227 227 globaltags = {}
228 228 tagtypes = {}
229 229
230 230 def readtags(lines, fn, tagtype):
231 231 filetags = {}
232 232 count = 0
233 233
234 234 def warn(msg):
235 235 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
236 236
237 237 for l in lines:
238 238 count += 1
239 239 if not l:
240 240 continue
241 241 s = l.split(" ", 1)
242 242 if len(s) != 2:
243 243 warn(_("cannot parse entry"))
244 244 continue
245 245 node, key = s
246 246 key = encoding.tolocal(key.strip()) # stored in UTF-8
247 247 try:
248 248 bin_n = bin(node)
249 249 except TypeError:
250 250 warn(_("node '%s' is not well formed") % node)
251 251 continue
252 252 if bin_n not in self.changelog.nodemap:
253 253 warn(_("tag '%s' refers to unknown node") % key)
254 254 continue
255 255
256 256 h = []
257 257 if key in filetags:
258 258 n, h = filetags[key]
259 259 h.append(n)
260 260 filetags[key] = (bin_n, h)
261 261
262 262 for k, nh in filetags.iteritems():
263 263 if k not in globaltags:
264 264 globaltags[k] = nh
265 265 tagtypes[k] = tagtype
266 266 continue
267 267
268 268 # we prefer the global tag if:
269 269 # it supercedes us OR
270 270 # mutual supercedes and it has a higher rank
271 271 # otherwise we win because we're tip-most
272 272 an, ah = nh
273 273 bn, bh = globaltags[k]
274 274 if (bn != an and an in bh and
275 275 (bn not in ah or len(bh) > len(ah))):
276 276 an = bn
277 277 ah.extend([n for n in bh if n not in ah])
278 278 globaltags[k] = an, ah
279 279 tagtypes[k] = tagtype
280 280
281 281 # read the tags file from each head, ending with the tip
282 282 f = None
283 283 for rev, node, fnode in self._hgtagsnodes():
284 284 f = (f and f.filectx(fnode) or
285 285 self.filectx('.hgtags', fileid=fnode))
286 286 readtags(f.data().splitlines(), f, "global")
287 287
288 288 try:
289 289 data = encoding.fromlocal(self.opener("localtags").read())
290 290 # localtags are stored in the local character set
291 291 # while the internal tag table is stored in UTF-8
292 292 readtags(data.splitlines(), "localtags", "local")
293 293 except IOError:
294 294 pass
295 295
296 296 self.tagscache = {}
297 297 self._tagstypecache = {}
298 298 for k, nh in globaltags.iteritems():
299 299 n = nh[0]
300 300 if n != nullid:
301 301 self.tagscache[k] = n
302 302 self._tagstypecache[k] = tagtypes[k]
303 303 self.tagscache['tip'] = self.changelog.tip()
304 304 return self.tagscache
305 305
306 306 def tagtype(self, tagname):
307 307 '''
308 308 return the type of the given tag. result can be:
309 309
310 310 'local' : a local tag
311 311 'global' : a global tag
312 312 None : tag does not exist
313 313 '''
314 314
315 315 self.tags()
316 316
317 317 return self._tagstypecache.get(tagname)
318 318
319 319 def _hgtagsnodes(self):
320 320 last = {}
321 321 ret = []
322 322 for node in reversed(self.heads()):
323 323 c = self[node]
324 324 rev = c.rev()
325 325 try:
326 326 fnode = c.filenode('.hgtags')
327 327 except error.LookupError:
328 328 continue
329 329 ret.append((rev, node, fnode))
330 330 if fnode in last:
331 331 ret[last[fnode]] = None
332 332 last[fnode] = len(ret) - 1
333 333 return [item for item in ret if item]
334 334
335 335 def tagslist(self):
336 336 '''return a list of tags ordered by revision'''
337 337 l = []
338 338 for t, n in self.tags().iteritems():
339 339 try:
340 340 r = self.changelog.rev(n)
341 341 except:
342 342 r = -2 # sort to the beginning of the list if unknown
343 343 l.append((r, t, n))
344 344 return [(t, n) for r, t, n in sorted(l)]
345 345
346 346 def nodetags(self, node):
347 347 '''return the tags associated with a node'''
348 348 if not self.nodetagscache:
349 349 self.nodetagscache = {}
350 350 for t, n in self.tags().iteritems():
351 351 self.nodetagscache.setdefault(n, []).append(t)
352 352 return self.nodetagscache.get(node, [])
353 353
354 354 def _branchtags(self, partial, lrev):
355 355 # TODO: rename this function?
356 356 tiprev = len(self) - 1
357 357 if lrev != tiprev:
358 358 self._updatebranchcache(partial, lrev+1, tiprev+1)
359 359 self._writebranchcache(partial, self.changelog.tip(), tiprev)
360 360
361 361 return partial
362 362
363 363 def branchmap(self):
364 364 tip = self.changelog.tip()
365 365 if self.branchcache is not None and self._branchcachetip == tip:
366 366 return self.branchcache
367 367
368 368 oldtip = self._branchcachetip
369 369 self._branchcachetip = tip
370 370 if self.branchcache is None:
371 371 self.branchcache = {} # avoid recursion in changectx
372 372 else:
373 373 self.branchcache.clear() # keep using the same dict
374 374 if oldtip is None or oldtip not in self.changelog.nodemap:
375 375 partial, last, lrev = self._readbranchcache()
376 376 else:
377 377 lrev = self.changelog.rev(oldtip)
378 378 partial = self._ubranchcache
379 379
380 380 self._branchtags(partial, lrev)
381 381 # this private cache holds all heads (not just tips)
382 382 self._ubranchcache = partial
383 383
384 384 # the branch cache is stored on disk as UTF-8, but in the local
385 385 # charset internally
386 386 for k, v in partial.iteritems():
387 387 self.branchcache[encoding.tolocal(k)] = v
388 388 return self.branchcache
389 389
390 390
391 391 def branchtags(self):
392 392 '''return a dict where branch names map to the tipmost head of
393 393 the branch, open heads come before closed'''
394 394 bt = {}
395 395 for bn, heads in self.branchmap().iteritems():
396 396 head = None
397 397 for i in range(len(heads)-1, -1, -1):
398 398 h = heads[i]
399 399 if 'close' not in self.changelog.read(h)[5]:
400 400 head = h
401 401 break
402 402 # no open heads were found
403 403 if head is None:
404 404 head = heads[-1]
405 405 bt[bn] = head
406 406 return bt
407 407
408 408
409 409 def _readbranchcache(self):
410 410 partial = {}
411 411 try:
412 412 f = self.opener("branchheads.cache")
413 413 lines = f.read().split('\n')
414 414 f.close()
415 415 except (IOError, OSError):
416 416 return {}, nullid, nullrev
417 417
418 418 try:
419 419 last, lrev = lines.pop(0).split(" ", 1)
420 420 last, lrev = bin(last), int(lrev)
421 421 if lrev >= len(self) or self[lrev].node() != last:
422 422 # invalidate the cache
423 423 raise ValueError('invalidating branch cache (tip differs)')
424 424 for l in lines:
425 425 if not l: continue
426 426 node, label = l.split(" ", 1)
427 427 partial.setdefault(label.strip(), []).append(bin(node))
428 428 except KeyboardInterrupt:
429 429 raise
430 430 except Exception, inst:
431 431 if self.ui.debugflag:
432 432 self.ui.warn(str(inst), '\n')
433 433 partial, last, lrev = {}, nullid, nullrev
434 434 return partial, last, lrev
435 435
436 436 def _writebranchcache(self, branches, tip, tiprev):
437 437 try:
438 438 f = self.opener("branchheads.cache", "w", atomictemp=True)
439 439 f.write("%s %s\n" % (hex(tip), tiprev))
440 440 for label, nodes in branches.iteritems():
441 441 for node in nodes:
442 442 f.write("%s %s\n" % (hex(node), label))
443 443 f.rename()
444 444 except (IOError, OSError):
445 445 pass
446 446
447 447 def _updatebranchcache(self, partial, start, end):
448 448 for r in xrange(start, end):
449 449 c = self[r]
450 450 b = c.branch()
451 451 bheads = partial.setdefault(b, [])
452 452 bheads.append(c.node())
453 453 for p in c.parents():
454 454 pn = p.node()
455 455 if pn in bheads:
456 456 bheads.remove(pn)
457 457
458 458 def lookup(self, key):
459 459 if isinstance(key, int):
460 460 return self.changelog.node(key)
461 461 elif key == '.':
462 462 return self.dirstate.parents()[0]
463 463 elif key == 'null':
464 464 return nullid
465 465 elif key == 'tip':
466 466 return self.changelog.tip()
467 467 n = self.changelog._match(key)
468 468 if n:
469 469 return n
470 470 if key in self.tags():
471 471 return self.tags()[key]
472 472 if key in self.branchtags():
473 473 return self.branchtags()[key]
474 474 n = self.changelog._partialmatch(key)
475 475 if n:
476 476 return n
477 477
478 478 # can't find key, check if it might have come from damaged dirstate
479 479 if key in self.dirstate.parents():
480 480 raise error.Abort(_("working directory has unknown parent '%s'!")
481 481 % short(key))
482 482 try:
483 483 if len(key) == 20:
484 484 key = hex(key)
485 485 except:
486 486 pass
487 487 raise error.RepoError(_("unknown revision '%s'") % key)
488 488
489 489 def local(self):
490 490 return True
491 491
492 492 def join(self, f):
493 493 return os.path.join(self.path, f)
494 494
495 495 def wjoin(self, f):
496 496 return os.path.join(self.root, f)
497 497
498 498 def rjoin(self, f):
499 499 return os.path.join(self.root, util.pconvert(f))
500 500
501 501 def file(self, f):
502 502 if f[0] == '/':
503 503 f = f[1:]
504 504 return filelog.filelog(self.sopener, f)
505 505
506 506 def changectx(self, changeid):
507 507 return self[changeid]
508 508
509 509 def parents(self, changeid=None):
510 510 '''get list of changectxs for parents of changeid'''
511 511 return self[changeid].parents()
512 512
513 513 def filectx(self, path, changeid=None, fileid=None):
514 514 """changeid can be a changeset revision, node, or tag.
515 515 fileid can be a file revision or node."""
516 516 return context.filectx(self, path, changeid, fileid)
517 517
518 518 def getcwd(self):
519 519 return self.dirstate.getcwd()
520 520
521 521 def pathto(self, f, cwd=None):
522 522 return self.dirstate.pathto(f, cwd)
523 523
524 524 def wfile(self, f, mode='r'):
525 525 return self.wopener(f, mode)
526 526
527 527 def _link(self, f):
528 528 return os.path.islink(self.wjoin(f))
529 529
530 530 def _filter(self, filter, filename, data):
531 531 if filter not in self.filterpats:
532 532 l = []
533 533 for pat, cmd in self.ui.configitems(filter):
534 534 if cmd == '!':
535 535 continue
536 536 mf = match_.match(self.root, '', [pat])
537 537 fn = None
538 538 params = cmd
539 539 for name, filterfn in self._datafilters.iteritems():
540 540 if cmd.startswith(name):
541 541 fn = filterfn
542 542 params = cmd[len(name):].lstrip()
543 543 break
544 544 if not fn:
545 545 fn = lambda s, c, **kwargs: util.filter(s, c)
546 546 # Wrap old filters not supporting keyword arguments
547 547 if not inspect.getargspec(fn)[2]:
548 548 oldfn = fn
549 549 fn = lambda s, c, **kwargs: oldfn(s, c)
550 550 l.append((mf, fn, params))
551 551 self.filterpats[filter] = l
552 552
553 553 for mf, fn, cmd in self.filterpats[filter]:
554 554 if mf(filename):
555 555 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
556 556 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
557 557 break
558 558
559 559 return data
560 560
561 561 def adddatafilter(self, name, filter):
562 562 self._datafilters[name] = filter
563 563
564 564 def wread(self, filename):
565 565 if self._link(filename):
566 566 data = os.readlink(self.wjoin(filename))
567 567 else:
568 568 data = self.wopener(filename, 'r').read()
569 569 return self._filter("encode", filename, data)
570 570
571 571 def wwrite(self, filename, data, flags):
572 572 data = self._filter("decode", filename, data)
573 573 try:
574 574 os.unlink(self.wjoin(filename))
575 575 except OSError:
576 576 pass
577 577 if 'l' in flags:
578 578 self.wopener.symlink(data, filename)
579 579 else:
580 580 self.wopener(filename, 'w').write(data)
581 581 if 'x' in flags:
582 582 util.set_flags(self.wjoin(filename), False, True)
583 583
584 584 def wwritedata(self, filename, data):
585 585 return self._filter("decode", filename, data)
586 586
587 587 def transaction(self):
588 588 tr = self._transref and self._transref() or None
589 589 if tr and tr.running():
590 590 return tr.nest()
591 591
592 592 # abort here if the journal already exists
593 593 if os.path.exists(self.sjoin("journal")):
594 594 raise error.RepoError(_("journal already exists - run hg recover"))
595 595
596 596 # save dirstate for rollback
597 597 try:
598 598 ds = self.opener("dirstate").read()
599 599 except IOError:
600 600 ds = ""
601 601 self.opener("journal.dirstate", "w").write(ds)
602 602 self.opener("journal.branch", "w").write(self.dirstate.branch())
603 603
604 604 renames = [(self.sjoin("journal"), self.sjoin("undo")),
605 605 (self.join("journal.dirstate"), self.join("undo.dirstate")),
606 606 (self.join("journal.branch"), self.join("undo.branch"))]
607 607 tr = transaction.transaction(self.ui.warn, self.sopener,
608 608 self.sjoin("journal"),
609 609 aftertrans(renames),
610 610 self.store.createmode)
611 611 self._transref = weakref.ref(tr)
612 612 return tr
613 613
614 614 def recover(self):
615 615 lock = self.lock()
616 616 try:
617 617 if os.path.exists(self.sjoin("journal")):
618 618 self.ui.status(_("rolling back interrupted transaction\n"))
619 619 transaction.rollback(self.sopener, self.sjoin("journal"), self.ui.warn)
620 620 self.invalidate()
621 621 return True
622 622 else:
623 623 self.ui.warn(_("no interrupted transaction available\n"))
624 624 return False
625 625 finally:
626 626 lock.release()
627 627
628 628 def rollback(self):
629 629 wlock = lock = None
630 630 try:
631 631 wlock = self.wlock()
632 632 lock = self.lock()
633 633 if os.path.exists(self.sjoin("undo")):
634 634 self.ui.status(_("rolling back last transaction\n"))
635 635 transaction.rollback(self.sopener, self.sjoin("undo"), self.ui.warn)
636 636 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
637 637 try:
638 638 branch = self.opener("undo.branch").read()
639 639 self.dirstate.setbranch(branch)
640 640 except IOError:
641 641 self.ui.warn(_("Named branch could not be reset, "
642 642 "current branch still is: %s\n")
643 643 % encoding.tolocal(self.dirstate.branch()))
644 644 self.invalidate()
645 645 self.dirstate.invalidate()
646 646 else:
647 647 self.ui.warn(_("no rollback information available\n"))
648 648 finally:
649 649 release(lock, wlock)
650 650
651 651 def invalidate(self):
652 652 for a in "changelog manifest".split():
653 653 if a in self.__dict__:
654 654 delattr(self, a)
655 655 self.tagscache = None
656 656 self._tagstypecache = None
657 657 self.nodetagscache = None
658 658 self.branchcache = None
659 659 self._ubranchcache = None
660 660 self._branchcachetip = None
661 661
662 662 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
663 663 try:
664 664 l = lock.lock(lockname, 0, releasefn, desc=desc)
665 665 except error.LockHeld, inst:
666 666 if not wait:
667 667 raise
668 668 self.ui.warn(_("waiting for lock on %s held by %r\n") %
669 669 (desc, inst.locker))
670 670 # default to 600 seconds timeout
671 671 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
672 672 releasefn, desc=desc)
673 673 if acquirefn:
674 674 acquirefn()
675 675 return l
676 676
677 677 def lock(self, wait=True):
678 678 l = self._lockref and self._lockref()
679 679 if l is not None and l.held:
680 680 l.lock()
681 681 return l
682 682
683 683 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
684 684 _('repository %s') % self.origroot)
685 685 self._lockref = weakref.ref(l)
686 686 return l
687 687
688 688 def wlock(self, wait=True):
689 689 l = self._wlockref and self._wlockref()
690 690 if l is not None and l.held:
691 691 l.lock()
692 692 return l
693 693
694 694 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
695 695 self.dirstate.invalidate, _('working directory of %s') %
696 696 self.origroot)
697 697 self._wlockref = weakref.ref(l)
698 698 return l
699 699
700 700 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
701 701 """
702 702 commit an individual file as part of a larger transaction
703 703 """
704 704
705 705 fname = fctx.path()
706 706 text = fctx.data()
707 707 flog = self.file(fname)
708 708 fparent1 = manifest1.get(fname, nullid)
709 709 fparent2 = fparent2o = manifest2.get(fname, nullid)
710 710
711 711 meta = {}
712 712 copy = fctx.renamed()
713 713 if copy and copy[0] != fname:
714 714 # Mark the new revision of this file as a copy of another
715 715 # file. This copy data will effectively act as a parent
716 716 # of this new revision. If this is a merge, the first
717 717 # parent will be the nullid (meaning "look up the copy data")
718 718 # and the second one will be the other parent. For example:
719 719 #
720 720 # 0 --- 1 --- 3 rev1 changes file foo
721 721 # \ / rev2 renames foo to bar and changes it
722 722 # \- 2 -/ rev3 should have bar with all changes and
723 723 # should record that bar descends from
724 724 # bar in rev2 and foo in rev1
725 725 #
726 726 # this allows this merge to succeed:
727 727 #
728 728 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
729 729 # \ / merging rev3 and rev4 should use bar@rev2
730 730 # \- 2 --- 4 as the merge base
731 731 #
732 732
733 733 cfname = copy[0]
734 734 crev = manifest1.get(cfname)
735 735 newfparent = fparent2
736 736
737 737 if manifest2: # branch merge
738 738 if fparent2 == nullid or crev is None: # copied on remote side
739 739 if cfname in manifest2:
740 740 crev = manifest2[cfname]
741 741 newfparent = fparent1
742 742
743 743 # find source in nearest ancestor if we've lost track
744 744 if not crev:
745 745 self.ui.debug(_(" %s: searching for copy revision for %s\n") %
746 746 (fname, cfname))
747 747 for ancestor in self['.'].ancestors():
748 748 if cfname in ancestor:
749 749 crev = ancestor[cfname].filenode()
750 750 break
751 751
752 752 self.ui.debug(_(" %s: copy %s:%s\n") % (fname, cfname, hex(crev)))
753 753 meta["copy"] = cfname
754 754 meta["copyrev"] = hex(crev)
755 755 fparent1, fparent2 = nullid, newfparent
756 756 elif fparent2 != nullid:
757 757 # is one parent an ancestor of the other?
758 758 fparentancestor = flog.ancestor(fparent1, fparent2)
759 759 if fparentancestor == fparent1:
760 760 fparent1, fparent2 = fparent2, nullid
761 761 elif fparentancestor == fparent2:
762 762 fparent2 = nullid
763 763
764 764 # is the file changed?
765 765 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
766 766 changelist.append(fname)
767 767 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
768 768
769 769 # are just the flags changed during merge?
770 770 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
771 771 changelist.append(fname)
772 772
773 773 return fparent1
774 774
775 775 def commit(self, files=None, text="", user=None, date=None, match=None,
776 776 force=False, editor=False, extra={}):
777 777 """Add a new revision to current repository.
778 778
779 779 Revision information is gathered from the working directory, files and
780 780 match can be used to filter the committed files.
781 781 If editor is supplied, it is called to get a commit message.
782 782 """
783 783 wlock = self.wlock()
784 784 try:
785 785 p1, p2 = self.dirstate.parents()
786 786
787 787 if (not force and p2 != nullid and match and
788 788 (match.files() or match.anypats())):
789 789 raise util.Abort(_('cannot partially commit a merge '
790 790 '(do not specify files or patterns)'))
791 791
792 792 if files:
793 793 modified, removed = [], []
794 794 for f in sorted(set(files)):
795 795 s = self.dirstate[f]
796 796 if s in 'nma':
797 797 modified.append(f)
798 798 elif s == 'r':
799 799 removed.append(f)
800 800 else:
801 801 self.ui.warn(_("%s not tracked!\n") % f)
802 802 changes = [modified, [], removed, [], []]
803 803 else:
804 804 changes = self.status(match=match)
805 805
806 806 if (not force and not extra.get("close") and p2 == nullid
807 807 and not (changes[0] or changes[1] or changes[2])
808 808 and self[None].branch() == self['.'].branch()):
809 809 self.ui.status(_("nothing changed\n"))
810 810 return None
811 811
812 812 ms = merge_.mergestate(self)
813 813 for f in changes[0]:
814 814 if f in ms and ms[f] == 'u':
815 815 raise util.Abort(_("unresolved merge conflicts "
816 816 "(see hg resolve)"))
817 817
818 818 wctx = context.workingctx(self, (p1, p2), text, user, date,
819 819 extra, changes)
820 820 if editor:
821 821 wctx._text = editor(self, wctx,
822 822 changes[1], changes[0], changes[2])
823 823 ret = self.commitctx(wctx, True)
824 824
825 825 # update dirstate and mergestate
826 826 for f in changes[0] + changes[1]:
827 827 self.dirstate.normal(f)
828 828 for f in changes[2]:
829 829 self.dirstate.forget(f)
830 830 self.dirstate.setparents(ret)
831 831 ms.reset()
832 832
833 833 return ret
834 834
835 835 finally:
836 836 wlock.release()
837 837
838 838 def commitctx(self, ctx, error=False):
839 839 """Add a new revision to current repository.
840 840
841 841 Revision information is passed via the context argument.
842 842 """
843 843
844 844 tr = lock = None
845 845 removed = ctx.removed()
846 846 p1, p2 = ctx.p1(), ctx.p2()
847 847 m1 = p1.manifest().copy()
848 848 m2 = p2.manifest()
849 849 user = ctx.user()
850 850
851 851 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
852 852 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
853 853
854 854 lock = self.lock()
855 855 try:
856 856 tr = self.transaction()
857 857 trp = weakref.proxy(tr)
858 858
859 859 # check in files
860 860 new = {}
861 861 changed = []
862 862 linkrev = len(self)
863 863 for f in sorted(ctx.modified() + ctx.added()):
864 864 self.ui.note(f + "\n")
865 865 try:
866 866 fctx = ctx[f]
867 867 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
868 868 changed)
869 869 m1.set(f, fctx.flags())
870 870 except (OSError, IOError):
871 871 if error:
872 872 self.ui.warn(_("trouble committing %s!\n") % f)
873 873 raise
874 874 else:
875 875 removed.append(f)
876 876
877 877 # update manifest
878 878 m1.update(new)
879 879 removed = [f for f in sorted(removed) if f in m1 or f in m2]
880 880 drop = [f for f in removed if f in m1]
881 881 for f in drop:
882 882 del m1[f]
883 883 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
884 884 p2.manifestnode(), (new, drop))
885 885
886 886 # update changelog
887 887 self.changelog.delayupdate()
888 888 n = self.changelog.add(mn, changed + removed, ctx.description(),
889 889 trp, p1.node(), p2.node(),
890 890 user, ctx.date(), ctx.extra().copy())
891 891 p = lambda: self.changelog.writepending() and self.root or ""
892 892 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
893 893 parent2=xp2, pending=p)
894 894 self.changelog.finalize(trp)
895 895 tr.close()
896 896
897 897 if self.branchcache:
898 898 self.branchtags()
899 899
900 900 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
901 901 return n
902 902 finally:
903 903 del tr
904 904 lock.release()
905 905
906 906 def walk(self, match, node=None):
907 907 '''
908 908 walk recursively through the directory tree or a given
909 909 changeset, finding all files matched by the match
910 910 function
911 911 '''
912 912 return self[node].walk(match)
913 913
914 914 def status(self, node1='.', node2=None, match=None,
915 915 ignored=False, clean=False, unknown=False):
916 916 """return status of files between two nodes or node and working directory
917 917
918 918 If node1 is None, use the first dirstate parent instead.
919 919 If node2 is None, compare node1 with working directory.
920 920 """
921 921
922 922 def mfmatches(ctx):
923 923 mf = ctx.manifest().copy()
924 924 for fn in mf.keys():
925 925 if not match(fn):
926 926 del mf[fn]
927 927 return mf
928 928
929 929 if isinstance(node1, context.changectx):
930 930 ctx1 = node1
931 931 else:
932 932 ctx1 = self[node1]
933 933 if isinstance(node2, context.changectx):
934 934 ctx2 = node2
935 935 else:
936 936 ctx2 = self[node2]
937 937
938 938 working = ctx2.rev() is None
939 939 parentworking = working and ctx1 == self['.']
940 940 match = match or match_.always(self.root, self.getcwd())
941 941 listignored, listclean, listunknown = ignored, clean, unknown
942 942
943 943 # load earliest manifest first for caching reasons
944 944 if not working and ctx2.rev() < ctx1.rev():
945 945 ctx2.manifest()
946 946
947 947 if not parentworking:
948 948 def bad(f, msg):
949 949 if f not in ctx1:
950 950 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
951 951 return False
952 952 match.bad = bad
953 953
954 954 if working: # we need to scan the working dir
955 955 s = self.dirstate.status(match, listignored, listclean, listunknown)
956 956 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
957 957
958 958 # check for any possibly clean files
959 959 if parentworking and cmp:
960 960 fixup = []
961 961 # do a full compare of any files that might have changed
962 962 for f in sorted(cmp):
963 963 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
964 964 or ctx1[f].cmp(ctx2[f].data())):
965 965 modified.append(f)
966 966 else:
967 967 fixup.append(f)
968 968
969 969 if listclean:
970 970 clean += fixup
971 971
972 972 # update dirstate for files that are actually clean
973 973 if fixup:
974 wlock = None
975 974 try:
975 wlock = self.wlock(False)
976 976 try:
977 977 # updating the dirstate is optional
978 978 # so we don't wait on the lock
979 wlock = self.wlock(False)
980 979 for f in fixup:
981 980 self.dirstate.normal(f)
982 except error.LockError:
983 pass
984 finally:
985 release(wlock)
981 finally:
982 wlock.release()
983 except error.LockError:
984 pass
986 985
987 986 if not parentworking:
988 987 mf1 = mfmatches(ctx1)
989 988 if working:
990 989 # we are comparing working dir against non-parent
991 990 # generate a pseudo-manifest for the working dir
992 991 mf2 = mfmatches(self['.'])
993 992 for f in cmp + modified + added:
994 993 mf2[f] = None
995 994 mf2.set(f, ctx2.flags(f))
996 995 for f in removed:
997 996 if f in mf2:
998 997 del mf2[f]
999 998 else:
1000 999 # we are comparing two revisions
1001 1000 deleted, unknown, ignored = [], [], []
1002 1001 mf2 = mfmatches(ctx2)
1003 1002
1004 1003 modified, added, clean = [], [], []
1005 1004 for fn in mf2:
1006 1005 if fn in mf1:
1007 1006 if (mf1.flags(fn) != mf2.flags(fn) or
1008 1007 (mf1[fn] != mf2[fn] and
1009 1008 (mf2[fn] or ctx1[fn].cmp(ctx2[fn].data())))):
1010 1009 modified.append(fn)
1011 1010 elif listclean:
1012 1011 clean.append(fn)
1013 1012 del mf1[fn]
1014 1013 else:
1015 1014 added.append(fn)
1016 1015 removed = mf1.keys()
1017 1016
1018 1017 r = modified, added, removed, deleted, unknown, ignored, clean
1019 1018 [l.sort() for l in r]
1020 1019 return r
1021 1020
1022 1021 def add(self, list):
1023 1022 wlock = self.wlock()
1024 1023 try:
1025 1024 rejected = []
1026 1025 for f in list:
1027 1026 p = self.wjoin(f)
1028 1027 try:
1029 1028 st = os.lstat(p)
1030 1029 except:
1031 1030 self.ui.warn(_("%s does not exist!\n") % f)
1032 1031 rejected.append(f)
1033 1032 continue
1034 1033 if st.st_size > 10000000:
1035 1034 self.ui.warn(_("%s: files over 10MB may cause memory and"
1036 1035 " performance problems\n"
1037 1036 "(use 'hg revert %s' to unadd the file)\n")
1038 1037 % (f, f))
1039 1038 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1040 1039 self.ui.warn(_("%s not added: only files and symlinks "
1041 1040 "supported currently\n") % f)
1042 1041 rejected.append(p)
1043 1042 elif self.dirstate[f] in 'amn':
1044 1043 self.ui.warn(_("%s already tracked!\n") % f)
1045 1044 elif self.dirstate[f] == 'r':
1046 1045 self.dirstate.normallookup(f)
1047 1046 else:
1048 1047 self.dirstate.add(f)
1049 1048 return rejected
1050 1049 finally:
1051 1050 wlock.release()
1052 1051
1053 1052 def forget(self, list):
1054 1053 wlock = self.wlock()
1055 1054 try:
1056 1055 for f in list:
1057 1056 if self.dirstate[f] != 'a':
1058 1057 self.ui.warn(_("%s not added!\n") % f)
1059 1058 else:
1060 1059 self.dirstate.forget(f)
1061 1060 finally:
1062 1061 wlock.release()
1063 1062
1064 1063 def remove(self, list, unlink=False):
1065 wlock = None
1064 if unlink:
1065 for f in list:
1066 try:
1067 util.unlink(self.wjoin(f))
1068 except OSError, inst:
1069 if inst.errno != errno.ENOENT:
1070 raise
1071 wlock = self.wlock()
1066 1072 try:
1067 if unlink:
1068 for f in list:
1069 try:
1070 util.unlink(self.wjoin(f))
1071 except OSError, inst:
1072 if inst.errno != errno.ENOENT:
1073 raise
1074 wlock = self.wlock()
1075 1073 for f in list:
1076 1074 if unlink and os.path.exists(self.wjoin(f)):
1077 1075 self.ui.warn(_("%s still exists!\n") % f)
1078 1076 elif self.dirstate[f] == 'a':
1079 1077 self.dirstate.forget(f)
1080 1078 elif f not in self.dirstate:
1081 1079 self.ui.warn(_("%s not tracked!\n") % f)
1082 1080 else:
1083 1081 self.dirstate.remove(f)
1084 1082 finally:
1085 release(wlock)
1083 wlock.release()
1086 1084
1087 1085 def undelete(self, list):
1088 1086 manifests = [self.manifest.read(self.changelog.read(p)[0])
1089 1087 for p in self.dirstate.parents() if p != nullid]
1090 1088 wlock = self.wlock()
1091 1089 try:
1092 1090 for f in list:
1093 1091 if self.dirstate[f] != 'r':
1094 1092 self.ui.warn(_("%s not removed!\n") % f)
1095 1093 else:
1096 1094 m = f in manifests[0] and manifests[0] or manifests[1]
1097 1095 t = self.file(f).read(m[f])
1098 1096 self.wwrite(f, t, m.flags(f))
1099 1097 self.dirstate.normal(f)
1100 1098 finally:
1101 1099 wlock.release()
1102 1100
1103 1101 def copy(self, source, dest):
1104 1102 p = self.wjoin(dest)
1105 1103 if not (os.path.exists(p) or os.path.islink(p)):
1106 1104 self.ui.warn(_("%s does not exist!\n") % dest)
1107 1105 elif not (os.path.isfile(p) or os.path.islink(p)):
1108 1106 self.ui.warn(_("copy failed: %s is not a file or a "
1109 1107 "symbolic link\n") % dest)
1110 1108 else:
1111 1109 wlock = self.wlock()
1112 1110 try:
1113 1111 if self.dirstate[dest] in '?r':
1114 1112 self.dirstate.add(dest)
1115 1113 self.dirstate.copy(source, dest)
1116 1114 finally:
1117 1115 wlock.release()
1118 1116
1119 1117 def heads(self, start=None, closed=True):
1120 1118 heads = self.changelog.heads(start)
1121 1119 def display(head):
1122 1120 if closed:
1123 1121 return True
1124 1122 extras = self.changelog.read(head)[5]
1125 1123 return ('close' not in extras)
1126 1124 # sort the output in rev descending order
1127 1125 heads = [(-self.changelog.rev(h), h) for h in heads if display(h)]
1128 1126 return [n for (r, n) in sorted(heads)]
1129 1127
1130 1128 def branchheads(self, branch=None, start=None, closed=True):
1131 1129 if branch is None:
1132 1130 branch = self[None].branch()
1133 1131 branches = self.branchmap()
1134 1132 if branch not in branches:
1135 1133 return []
1136 1134 bheads = branches[branch]
1137 1135 # the cache returns heads ordered lowest to highest
1138 1136 bheads.reverse()
1139 1137 if start is not None:
1140 1138 # filter out the heads that cannot be reached from startrev
1141 1139 bheads = self.changelog.nodesbetween([start], bheads)[2]
1142 1140 if not closed:
1143 1141 bheads = [h for h in bheads if
1144 1142 ('close' not in self.changelog.read(h)[5])]
1145 1143 return bheads
1146 1144
1147 1145 def branches(self, nodes):
1148 1146 if not nodes:
1149 1147 nodes = [self.changelog.tip()]
1150 1148 b = []
1151 1149 for n in nodes:
1152 1150 t = n
1153 1151 while 1:
1154 1152 p = self.changelog.parents(n)
1155 1153 if p[1] != nullid or p[0] == nullid:
1156 1154 b.append((t, n, p[0], p[1]))
1157 1155 break
1158 1156 n = p[0]
1159 1157 return b
1160 1158
1161 1159 def between(self, pairs):
1162 1160 r = []
1163 1161
1164 1162 for top, bottom in pairs:
1165 1163 n, l, i = top, [], 0
1166 1164 f = 1
1167 1165
1168 1166 while n != bottom and n != nullid:
1169 1167 p = self.changelog.parents(n)[0]
1170 1168 if i == f:
1171 1169 l.append(n)
1172 1170 f = f * 2
1173 1171 n = p
1174 1172 i += 1
1175 1173
1176 1174 r.append(l)
1177 1175
1178 1176 return r
1179 1177
1180 1178 def findincoming(self, remote, base=None, heads=None, force=False):
1181 1179 """Return list of roots of the subsets of missing nodes from remote
1182 1180
1183 1181 If base dict is specified, assume that these nodes and their parents
1184 1182 exist on the remote side and that no child of a node of base exists
1185 1183 in both remote and self.
1186 1184 Furthermore base will be updated to include the nodes that exists
1187 1185 in self and remote but no children exists in self and remote.
1188 1186 If a list of heads is specified, return only nodes which are heads
1189 1187 or ancestors of these heads.
1190 1188
1191 1189 All the ancestors of base are in self and in remote.
1192 1190 All the descendants of the list returned are missing in self.
1193 1191 (and so we know that the rest of the nodes are missing in remote, see
1194 1192 outgoing)
1195 1193 """
1196 1194 return self.findcommonincoming(remote, base, heads, force)[1]
1197 1195
1198 1196 def findcommonincoming(self, remote, base=None, heads=None, force=False):
1199 1197 """Return a tuple (common, missing roots, heads) used to identify
1200 1198 missing nodes from remote.
1201 1199
1202 1200 If base dict is specified, assume that these nodes and their parents
1203 1201 exist on the remote side and that no child of a node of base exists
1204 1202 in both remote and self.
1205 1203 Furthermore base will be updated to include the nodes that exists
1206 1204 in self and remote but no children exists in self and remote.
1207 1205 If a list of heads is specified, return only nodes which are heads
1208 1206 or ancestors of these heads.
1209 1207
1210 1208 All the ancestors of base are in self and in remote.
1211 1209 """
1212 1210 m = self.changelog.nodemap
1213 1211 search = []
1214 1212 fetch = set()
1215 1213 seen = set()
1216 1214 seenbranch = set()
1217 1215 if base is None:
1218 1216 base = {}
1219 1217
1220 1218 if not heads:
1221 1219 heads = remote.heads()
1222 1220
1223 1221 if self.changelog.tip() == nullid:
1224 1222 base[nullid] = 1
1225 1223 if heads != [nullid]:
1226 1224 return [nullid], [nullid], list(heads)
1227 1225 return [nullid], [], []
1228 1226
1229 1227 # assume we're closer to the tip than the root
1230 1228 # and start by examining the heads
1231 1229 self.ui.status(_("searching for changes\n"))
1232 1230
1233 1231 unknown = []
1234 1232 for h in heads:
1235 1233 if h not in m:
1236 1234 unknown.append(h)
1237 1235 else:
1238 1236 base[h] = 1
1239 1237
1240 1238 heads = unknown
1241 1239 if not unknown:
1242 1240 return base.keys(), [], []
1243 1241
1244 1242 req = set(unknown)
1245 1243 reqcnt = 0
1246 1244
1247 1245 # search through remote branches
1248 1246 # a 'branch' here is a linear segment of history, with four parts:
1249 1247 # head, root, first parent, second parent
1250 1248 # (a branch always has two parents (or none) by definition)
1251 1249 unknown = remote.branches(unknown)
1252 1250 while unknown:
1253 1251 r = []
1254 1252 while unknown:
1255 1253 n = unknown.pop(0)
1256 1254 if n[0] in seen:
1257 1255 continue
1258 1256
1259 1257 self.ui.debug(_("examining %s:%s\n")
1260 1258 % (short(n[0]), short(n[1])))
1261 1259 if n[0] == nullid: # found the end of the branch
1262 1260 pass
1263 1261 elif n in seenbranch:
1264 1262 self.ui.debug(_("branch already found\n"))
1265 1263 continue
1266 1264 elif n[1] and n[1] in m: # do we know the base?
1267 1265 self.ui.debug(_("found incomplete branch %s:%s\n")
1268 1266 % (short(n[0]), short(n[1])))
1269 1267 search.append(n[0:2]) # schedule branch range for scanning
1270 1268 seenbranch.add(n)
1271 1269 else:
1272 1270 if n[1] not in seen and n[1] not in fetch:
1273 1271 if n[2] in m and n[3] in m:
1274 1272 self.ui.debug(_("found new changeset %s\n") %
1275 1273 short(n[1]))
1276 1274 fetch.add(n[1]) # earliest unknown
1277 1275 for p in n[2:4]:
1278 1276 if p in m:
1279 1277 base[p] = 1 # latest known
1280 1278
1281 1279 for p in n[2:4]:
1282 1280 if p not in req and p not in m:
1283 1281 r.append(p)
1284 1282 req.add(p)
1285 1283 seen.add(n[0])
1286 1284
1287 1285 if r:
1288 1286 reqcnt += 1
1289 1287 self.ui.debug(_("request %d: %s\n") %
1290 1288 (reqcnt, " ".join(map(short, r))))
1291 1289 for p in xrange(0, len(r), 10):
1292 1290 for b in remote.branches(r[p:p+10]):
1293 1291 self.ui.debug(_("received %s:%s\n") %
1294 1292 (short(b[0]), short(b[1])))
1295 1293 unknown.append(b)
1296 1294
1297 1295 # do binary search on the branches we found
1298 1296 while search:
1299 1297 newsearch = []
1300 1298 reqcnt += 1
1301 1299 for n, l in zip(search, remote.between(search)):
1302 1300 l.append(n[1])
1303 1301 p = n[0]
1304 1302 f = 1
1305 1303 for i in l:
1306 1304 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1307 1305 if i in m:
1308 1306 if f <= 2:
1309 1307 self.ui.debug(_("found new branch changeset %s\n") %
1310 1308 short(p))
1311 1309 fetch.add(p)
1312 1310 base[i] = 1
1313 1311 else:
1314 1312 self.ui.debug(_("narrowed branch search to %s:%s\n")
1315 1313 % (short(p), short(i)))
1316 1314 newsearch.append((p, i))
1317 1315 break
1318 1316 p, f = i, f * 2
1319 1317 search = newsearch
1320 1318
1321 1319 # sanity check our fetch list
1322 1320 for f in fetch:
1323 1321 if f in m:
1324 1322 raise error.RepoError(_("already have changeset ")
1325 1323 + short(f[:4]))
1326 1324
1327 1325 if base.keys() == [nullid]:
1328 1326 if force:
1329 1327 self.ui.warn(_("warning: repository is unrelated\n"))
1330 1328 else:
1331 1329 raise util.Abort(_("repository is unrelated"))
1332 1330
1333 1331 self.ui.debug(_("found new changesets starting at ") +
1334 1332 " ".join([short(f) for f in fetch]) + "\n")
1335 1333
1336 1334 self.ui.debug(_("%d total queries\n") % reqcnt)
1337 1335
1338 1336 return base.keys(), list(fetch), heads
1339 1337
1340 1338 def findoutgoing(self, remote, base=None, heads=None, force=False):
1341 1339 """Return list of nodes that are roots of subsets not in remote
1342 1340
1343 1341 If base dict is specified, assume that these nodes and their parents
1344 1342 exist on the remote side.
1345 1343 If a list of heads is specified, return only nodes which are heads
1346 1344 or ancestors of these heads, and return a second element which
1347 1345 contains all remote heads which get new children.
1348 1346 """
1349 1347 if base is None:
1350 1348 base = {}
1351 1349 self.findincoming(remote, base, heads, force=force)
1352 1350
1353 1351 self.ui.debug(_("common changesets up to ")
1354 1352 + " ".join(map(short, base.keys())) + "\n")
1355 1353
1356 1354 remain = set(self.changelog.nodemap)
1357 1355
1358 1356 # prune everything remote has from the tree
1359 1357 remain.remove(nullid)
1360 1358 remove = base.keys()
1361 1359 while remove:
1362 1360 n = remove.pop(0)
1363 1361 if n in remain:
1364 1362 remain.remove(n)
1365 1363 for p in self.changelog.parents(n):
1366 1364 remove.append(p)
1367 1365
1368 1366 # find every node whose parents have been pruned
1369 1367 subset = []
1370 1368 # find every remote head that will get new children
1371 1369 updated_heads = set()
1372 1370 for n in remain:
1373 1371 p1, p2 = self.changelog.parents(n)
1374 1372 if p1 not in remain and p2 not in remain:
1375 1373 subset.append(n)
1376 1374 if heads:
1377 1375 if p1 in heads:
1378 1376 updated_heads.add(p1)
1379 1377 if p2 in heads:
1380 1378 updated_heads.add(p2)
1381 1379
1382 1380 # this is the set of all roots we have to push
1383 1381 if heads:
1384 1382 return subset, list(updated_heads)
1385 1383 else:
1386 1384 return subset
1387 1385
1388 1386 def pull(self, remote, heads=None, force=False):
1389 1387 lock = self.lock()
1390 1388 try:
1391 1389 common, fetch, rheads = self.findcommonincoming(remote, heads=heads,
1392 1390 force=force)
1393 1391 if fetch == [nullid]:
1394 1392 self.ui.status(_("requesting all changes\n"))
1395 1393
1396 1394 if not fetch:
1397 1395 self.ui.status(_("no changes found\n"))
1398 1396 return 0
1399 1397
1400 1398 if heads is None and remote.capable('changegroupsubset'):
1401 1399 heads = rheads
1402 1400
1403 1401 if heads is None:
1404 1402 cg = remote.changegroup(fetch, 'pull')
1405 1403 else:
1406 1404 if not remote.capable('changegroupsubset'):
1407 1405 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1408 1406 cg = remote.changegroupsubset(fetch, heads, 'pull')
1409 1407 return self.addchangegroup(cg, 'pull', remote.url())
1410 1408 finally:
1411 1409 lock.release()
1412 1410
1413 1411 def push(self, remote, force=False, revs=None):
1414 1412 # there are two ways to push to remote repo:
1415 1413 #
1416 1414 # addchangegroup assumes local user can lock remote
1417 1415 # repo (local filesystem, old ssh servers).
1418 1416 #
1419 1417 # unbundle assumes local user cannot lock remote repo (new ssh
1420 1418 # servers, http servers).
1421 1419
1422 1420 if remote.capable('unbundle'):
1423 1421 return self.push_unbundle(remote, force, revs)
1424 1422 return self.push_addchangegroup(remote, force, revs)
1425 1423
1426 1424 def prepush(self, remote, force, revs):
1427 1425 common = {}
1428 1426 remote_heads = remote.heads()
1429 1427 inc = self.findincoming(remote, common, remote_heads, force=force)
1430 1428
1431 1429 update, updated_heads = self.findoutgoing(remote, common, remote_heads)
1432 1430 if revs is not None:
1433 1431 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1434 1432 else:
1435 1433 bases, heads = update, self.changelog.heads()
1436 1434
1437 1435 def checkbranch(lheads, rheads, updatelh):
1438 1436 '''
1439 1437 check whether there are more local heads than remote heads on
1440 1438 a specific branch.
1441 1439
1442 1440 lheads: local branch heads
1443 1441 rheads: remote branch heads
1444 1442 updatelh: outgoing local branch heads
1445 1443 '''
1446 1444
1447 1445 warn = 0
1448 1446
1449 1447 if not revs and len(lheads) > len(rheads):
1450 1448 warn = 1
1451 1449 else:
1452 1450 updatelheads = [self.changelog.heads(x, lheads)
1453 1451 for x in updatelh]
1454 1452 newheads = set(sum(updatelheads, [])) & set(lheads)
1455 1453
1456 1454 if not newheads:
1457 1455 return True
1458 1456
1459 1457 for r in rheads:
1460 1458 if r in self.changelog.nodemap:
1461 1459 desc = self.changelog.heads(r, heads)
1462 1460 l = [h for h in heads if h in desc]
1463 1461 if not l:
1464 1462 newheads.add(r)
1465 1463 else:
1466 1464 newheads.add(r)
1467 1465 if len(newheads) > len(rheads):
1468 1466 warn = 1
1469 1467
1470 1468 if warn:
1471 1469 if not rheads: # new branch requires --force
1472 1470 self.ui.warn(_("abort: push creates new"
1473 1471 " remote branch '%s'!\n" %
1474 1472 self[updatelh[0]].branch()))
1475 1473 else:
1476 1474 self.ui.warn(_("abort: push creates new remote heads!\n"))
1477 1475
1478 1476 self.ui.status(_("(did you forget to merge?"
1479 1477 " use push -f to force)\n"))
1480 1478 return False
1481 1479 return True
1482 1480
1483 1481 if not bases:
1484 1482 self.ui.status(_("no changes found\n"))
1485 1483 return None, 1
1486 1484 elif not force:
1487 1485 # Check for each named branch if we're creating new remote heads.
1488 1486 # To be a remote head after push, node must be either:
1489 1487 # - unknown locally
1490 1488 # - a local outgoing head descended from update
1491 1489 # - a remote head that's known locally and not
1492 1490 # ancestral to an outgoing head
1493 1491 #
1494 1492 # New named branches cannot be created without --force.
1495 1493
1496 1494 if remote_heads != [nullid]:
1497 1495 if remote.capable('branchmap'):
1498 1496 localhds = {}
1499 1497 if not revs:
1500 1498 localhds = self.branchmap()
1501 1499 else:
1502 1500 for n in heads:
1503 1501 branch = self[n].branch()
1504 1502 if branch in localhds:
1505 1503 localhds[branch].append(n)
1506 1504 else:
1507 1505 localhds[branch] = [n]
1508 1506
1509 1507 remotehds = remote.branchmap()
1510 1508
1511 1509 for lh in localhds:
1512 1510 if lh in remotehds:
1513 1511 rheads = remotehds[lh]
1514 1512 else:
1515 1513 rheads = []
1516 1514 lheads = localhds[lh]
1517 1515 updatelh = [upd for upd in update
1518 1516 if self[upd].branch() == lh]
1519 1517 if not updatelh:
1520 1518 continue
1521 1519 if not checkbranch(lheads, rheads, updatelh):
1522 1520 return None, 0
1523 1521 else:
1524 1522 if not checkbranch(heads, remote_heads, update):
1525 1523 return None, 0
1526 1524
1527 1525 if inc:
1528 1526 self.ui.warn(_("note: unsynced remote changes!\n"))
1529 1527
1530 1528
1531 1529 if revs is None:
1532 1530 # use the fast path, no race possible on push
1533 1531 cg = self._changegroup(common.keys(), 'push')
1534 1532 else:
1535 1533 cg = self.changegroupsubset(update, revs, 'push')
1536 1534 return cg, remote_heads
1537 1535
1538 1536 def push_addchangegroup(self, remote, force, revs):
1539 1537 lock = remote.lock()
1540 1538 try:
1541 1539 ret = self.prepush(remote, force, revs)
1542 1540 if ret[0] is not None:
1543 1541 cg, remote_heads = ret
1544 1542 return remote.addchangegroup(cg, 'push', self.url())
1545 1543 return ret[1]
1546 1544 finally:
1547 1545 lock.release()
1548 1546
1549 1547 def push_unbundle(self, remote, force, revs):
1550 1548 # local repo finds heads on server, finds out what revs it
1551 1549 # must push. once revs transferred, if server finds it has
1552 1550 # different heads (someone else won commit/push race), server
1553 1551 # aborts.
1554 1552
1555 1553 ret = self.prepush(remote, force, revs)
1556 1554 if ret[0] is not None:
1557 1555 cg, remote_heads = ret
1558 1556 if force: remote_heads = ['force']
1559 1557 return remote.unbundle(cg, remote_heads, 'push')
1560 1558 return ret[1]
1561 1559
1562 1560 def changegroupinfo(self, nodes, source):
1563 1561 if self.ui.verbose or source == 'bundle':
1564 1562 self.ui.status(_("%d changesets found\n") % len(nodes))
1565 1563 if self.ui.debugflag:
1566 1564 self.ui.debug(_("list of changesets:\n"))
1567 1565 for node in nodes:
1568 1566 self.ui.debug("%s\n" % hex(node))
1569 1567
1570 1568 def changegroupsubset(self, bases, heads, source, extranodes=None):
1571 1569 """This function generates a changegroup consisting of all the nodes
1572 1570 that are descendents of any of the bases, and ancestors of any of
1573 1571 the heads.
1574 1572
1575 1573 It is fairly complex as determining which filenodes and which
1576 1574 manifest nodes need to be included for the changeset to be complete
1577 1575 is non-trivial.
1578 1576
1579 1577 Another wrinkle is doing the reverse, figuring out which changeset in
1580 1578 the changegroup a particular filenode or manifestnode belongs to.
1581 1579
1582 1580 The caller can specify some nodes that must be included in the
1583 1581 changegroup using the extranodes argument. It should be a dict
1584 1582 where the keys are the filenames (or 1 for the manifest), and the
1585 1583 values are lists of (node, linknode) tuples, where node is a wanted
1586 1584 node and linknode is the changelog node that should be transmitted as
1587 1585 the linkrev.
1588 1586 """
1589 1587
1590 1588 if extranodes is None:
1591 1589 # can we go through the fast path ?
1592 1590 heads.sort()
1593 1591 allheads = self.heads()
1594 1592 allheads.sort()
1595 1593 if heads == allheads:
1596 1594 common = []
1597 1595 # parents of bases are known from both sides
1598 1596 for n in bases:
1599 1597 for p in self.changelog.parents(n):
1600 1598 if p != nullid:
1601 1599 common.append(p)
1602 1600 return self._changegroup(common, source)
1603 1601
1604 1602 self.hook('preoutgoing', throw=True, source=source)
1605 1603
1606 1604 # Set up some initial variables
1607 1605 # Make it easy to refer to self.changelog
1608 1606 cl = self.changelog
1609 1607 # msng is short for missing - compute the list of changesets in this
1610 1608 # changegroup.
1611 1609 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1612 1610 self.changegroupinfo(msng_cl_lst, source)
1613 1611 # Some bases may turn out to be superfluous, and some heads may be
1614 1612 # too. nodesbetween will return the minimal set of bases and heads
1615 1613 # necessary to re-create the changegroup.
1616 1614
1617 1615 # Known heads are the list of heads that it is assumed the recipient
1618 1616 # of this changegroup will know about.
1619 1617 knownheads = set()
1620 1618 # We assume that all parents of bases are known heads.
1621 1619 for n in bases:
1622 1620 knownheads.update(cl.parents(n))
1623 1621 knownheads.discard(nullid)
1624 1622 knownheads = list(knownheads)
1625 1623 if knownheads:
1626 1624 # Now that we know what heads are known, we can compute which
1627 1625 # changesets are known. The recipient must know about all
1628 1626 # changesets required to reach the known heads from the null
1629 1627 # changeset.
1630 1628 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1631 1629 junk = None
1632 1630 # Transform the list into a set.
1633 1631 has_cl_set = set(has_cl_set)
1634 1632 else:
1635 1633 # If there were no known heads, the recipient cannot be assumed to
1636 1634 # know about any changesets.
1637 1635 has_cl_set = set()
1638 1636
1639 1637 # Make it easy to refer to self.manifest
1640 1638 mnfst = self.manifest
1641 1639 # We don't know which manifests are missing yet
1642 1640 msng_mnfst_set = {}
1643 1641 # Nor do we know which filenodes are missing.
1644 1642 msng_filenode_set = {}
1645 1643
1646 1644 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1647 1645 junk = None
1648 1646
1649 1647 # A changeset always belongs to itself, so the changenode lookup
1650 1648 # function for a changenode is identity.
1651 1649 def identity(x):
1652 1650 return x
1653 1651
1654 1652 # A function generating function. Sets up an environment for the
1655 1653 # inner function.
1656 1654 def cmp_by_rev_func(revlog):
1657 1655 # Compare two nodes by their revision number in the environment's
1658 1656 # revision history. Since the revision number both represents the
1659 1657 # most efficient order to read the nodes in, and represents a
1660 1658 # topological sorting of the nodes, this function is often useful.
1661 1659 def cmp_by_rev(a, b):
1662 1660 return cmp(revlog.rev(a), revlog.rev(b))
1663 1661 return cmp_by_rev
1664 1662
1665 1663 # If we determine that a particular file or manifest node must be a
1666 1664 # node that the recipient of the changegroup will already have, we can
1667 1665 # also assume the recipient will have all the parents. This function
1668 1666 # prunes them from the set of missing nodes.
1669 1667 def prune_parents(revlog, hasset, msngset):
1670 1668 haslst = list(hasset)
1671 1669 haslst.sort(cmp_by_rev_func(revlog))
1672 1670 for node in haslst:
1673 1671 parentlst = [p for p in revlog.parents(node) if p != nullid]
1674 1672 while parentlst:
1675 1673 n = parentlst.pop()
1676 1674 if n not in hasset:
1677 1675 hasset.add(n)
1678 1676 p = [p for p in revlog.parents(n) if p != nullid]
1679 1677 parentlst.extend(p)
1680 1678 for n in hasset:
1681 1679 msngset.pop(n, None)
1682 1680
1683 1681 # This is a function generating function used to set up an environment
1684 1682 # for the inner function to execute in.
1685 1683 def manifest_and_file_collector(changedfileset):
1686 1684 # This is an information gathering function that gathers
1687 1685 # information from each changeset node that goes out as part of
1688 1686 # the changegroup. The information gathered is a list of which
1689 1687 # manifest nodes are potentially required (the recipient may
1690 1688 # already have them) and total list of all files which were
1691 1689 # changed in any changeset in the changegroup.
1692 1690 #
1693 1691 # We also remember the first changenode we saw any manifest
1694 1692 # referenced by so we can later determine which changenode 'owns'
1695 1693 # the manifest.
1696 1694 def collect_manifests_and_files(clnode):
1697 1695 c = cl.read(clnode)
1698 1696 for f in c[3]:
1699 1697 # This is to make sure we only have one instance of each
1700 1698 # filename string for each filename.
1701 1699 changedfileset.setdefault(f, f)
1702 1700 msng_mnfst_set.setdefault(c[0], clnode)
1703 1701 return collect_manifests_and_files
1704 1702
1705 1703 # Figure out which manifest nodes (of the ones we think might be part
1706 1704 # of the changegroup) the recipient must know about and remove them
1707 1705 # from the changegroup.
1708 1706 def prune_manifests():
1709 1707 has_mnfst_set = set()
1710 1708 for n in msng_mnfst_set:
1711 1709 # If a 'missing' manifest thinks it belongs to a changenode
1712 1710 # the recipient is assumed to have, obviously the recipient
1713 1711 # must have that manifest.
1714 1712 linknode = cl.node(mnfst.linkrev(mnfst.rev(n)))
1715 1713 if linknode in has_cl_set:
1716 1714 has_mnfst_set.add(n)
1717 1715 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1718 1716
1719 1717 # Use the information collected in collect_manifests_and_files to say
1720 1718 # which changenode any manifestnode belongs to.
1721 1719 def lookup_manifest_link(mnfstnode):
1722 1720 return msng_mnfst_set[mnfstnode]
1723 1721
1724 1722 # A function generating function that sets up the initial environment
1725 1723 # the inner function.
1726 1724 def filenode_collector(changedfiles):
1727 1725 next_rev = [0]
1728 1726 # This gathers information from each manifestnode included in the
1729 1727 # changegroup about which filenodes the manifest node references
1730 1728 # so we can include those in the changegroup too.
1731 1729 #
1732 1730 # It also remembers which changenode each filenode belongs to. It
1733 1731 # does this by assuming the a filenode belongs to the changenode
1734 1732 # the first manifest that references it belongs to.
1735 1733 def collect_msng_filenodes(mnfstnode):
1736 1734 r = mnfst.rev(mnfstnode)
1737 1735 if r == next_rev[0]:
1738 1736 # If the last rev we looked at was the one just previous,
1739 1737 # we only need to see a diff.
1740 1738 deltamf = mnfst.readdelta(mnfstnode)
1741 1739 # For each line in the delta
1742 1740 for f, fnode in deltamf.iteritems():
1743 1741 f = changedfiles.get(f, None)
1744 1742 # And if the file is in the list of files we care
1745 1743 # about.
1746 1744 if f is not None:
1747 1745 # Get the changenode this manifest belongs to
1748 1746 clnode = msng_mnfst_set[mnfstnode]
1749 1747 # Create the set of filenodes for the file if
1750 1748 # there isn't one already.
1751 1749 ndset = msng_filenode_set.setdefault(f, {})
1752 1750 # And set the filenode's changelog node to the
1753 1751 # manifest's if it hasn't been set already.
1754 1752 ndset.setdefault(fnode, clnode)
1755 1753 else:
1756 1754 # Otherwise we need a full manifest.
1757 1755 m = mnfst.read(mnfstnode)
1758 1756 # For every file in we care about.
1759 1757 for f in changedfiles:
1760 1758 fnode = m.get(f, None)
1761 1759 # If it's in the manifest
1762 1760 if fnode is not None:
1763 1761 # See comments above.
1764 1762 clnode = msng_mnfst_set[mnfstnode]
1765 1763 ndset = msng_filenode_set.setdefault(f, {})
1766 1764 ndset.setdefault(fnode, clnode)
1767 1765 # Remember the revision we hope to see next.
1768 1766 next_rev[0] = r + 1
1769 1767 return collect_msng_filenodes
1770 1768
1771 1769 # We have a list of filenodes we think we need for a file, lets remove
1772 1770 # all those we know the recipient must have.
1773 1771 def prune_filenodes(f, filerevlog):
1774 1772 msngset = msng_filenode_set[f]
1775 1773 hasset = set()
1776 1774 # If a 'missing' filenode thinks it belongs to a changenode we
1777 1775 # assume the recipient must have, then the recipient must have
1778 1776 # that filenode.
1779 1777 for n in msngset:
1780 1778 clnode = cl.node(filerevlog.linkrev(filerevlog.rev(n)))
1781 1779 if clnode in has_cl_set:
1782 1780 hasset.add(n)
1783 1781 prune_parents(filerevlog, hasset, msngset)
1784 1782
1785 1783 # A function generator function that sets up the a context for the
1786 1784 # inner function.
1787 1785 def lookup_filenode_link_func(fname):
1788 1786 msngset = msng_filenode_set[fname]
1789 1787 # Lookup the changenode the filenode belongs to.
1790 1788 def lookup_filenode_link(fnode):
1791 1789 return msngset[fnode]
1792 1790 return lookup_filenode_link
1793 1791
1794 1792 # Add the nodes that were explicitly requested.
1795 1793 def add_extra_nodes(name, nodes):
1796 1794 if not extranodes or name not in extranodes:
1797 1795 return
1798 1796
1799 1797 for node, linknode in extranodes[name]:
1800 1798 if node not in nodes:
1801 1799 nodes[node] = linknode
1802 1800
1803 1801 # Now that we have all theses utility functions to help out and
1804 1802 # logically divide up the task, generate the group.
1805 1803 def gengroup():
1806 1804 # The set of changed files starts empty.
1807 1805 changedfiles = {}
1808 1806 # Create a changenode group generator that will call our functions
1809 1807 # back to lookup the owning changenode and collect information.
1810 1808 group = cl.group(msng_cl_lst, identity,
1811 1809 manifest_and_file_collector(changedfiles))
1812 1810 for chnk in group:
1813 1811 yield chnk
1814 1812
1815 1813 # The list of manifests has been collected by the generator
1816 1814 # calling our functions back.
1817 1815 prune_manifests()
1818 1816 add_extra_nodes(1, msng_mnfst_set)
1819 1817 msng_mnfst_lst = msng_mnfst_set.keys()
1820 1818 # Sort the manifestnodes by revision number.
1821 1819 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1822 1820 # Create a generator for the manifestnodes that calls our lookup
1823 1821 # and data collection functions back.
1824 1822 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1825 1823 filenode_collector(changedfiles))
1826 1824 for chnk in group:
1827 1825 yield chnk
1828 1826
1829 1827 # These are no longer needed, dereference and toss the memory for
1830 1828 # them.
1831 1829 msng_mnfst_lst = None
1832 1830 msng_mnfst_set.clear()
1833 1831
1834 1832 if extranodes:
1835 1833 for fname in extranodes:
1836 1834 if isinstance(fname, int):
1837 1835 continue
1838 1836 msng_filenode_set.setdefault(fname, {})
1839 1837 changedfiles[fname] = 1
1840 1838 # Go through all our files in order sorted by name.
1841 1839 for fname in sorted(changedfiles):
1842 1840 filerevlog = self.file(fname)
1843 1841 if not len(filerevlog):
1844 1842 raise util.Abort(_("empty or missing revlog for %s") % fname)
1845 1843 # Toss out the filenodes that the recipient isn't really
1846 1844 # missing.
1847 1845 if fname in msng_filenode_set:
1848 1846 prune_filenodes(fname, filerevlog)
1849 1847 add_extra_nodes(fname, msng_filenode_set[fname])
1850 1848 msng_filenode_lst = msng_filenode_set[fname].keys()
1851 1849 else:
1852 1850 msng_filenode_lst = []
1853 1851 # If any filenodes are left, generate the group for them,
1854 1852 # otherwise don't bother.
1855 1853 if len(msng_filenode_lst) > 0:
1856 1854 yield changegroup.chunkheader(len(fname))
1857 1855 yield fname
1858 1856 # Sort the filenodes by their revision #
1859 1857 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1860 1858 # Create a group generator and only pass in a changenode
1861 1859 # lookup function as we need to collect no information
1862 1860 # from filenodes.
1863 1861 group = filerevlog.group(msng_filenode_lst,
1864 1862 lookup_filenode_link_func(fname))
1865 1863 for chnk in group:
1866 1864 yield chnk
1867 1865 if fname in msng_filenode_set:
1868 1866 # Don't need this anymore, toss it to free memory.
1869 1867 del msng_filenode_set[fname]
1870 1868 # Signal that no more groups are left.
1871 1869 yield changegroup.closechunk()
1872 1870
1873 1871 if msng_cl_lst:
1874 1872 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1875 1873
1876 1874 return util.chunkbuffer(gengroup())
1877 1875
1878 1876 def changegroup(self, basenodes, source):
1879 1877 # to avoid a race we use changegroupsubset() (issue1320)
1880 1878 return self.changegroupsubset(basenodes, self.heads(), source)
1881 1879
1882 1880 def _changegroup(self, common, source):
1883 1881 """Generate a changegroup of all nodes that we have that a recipient
1884 1882 doesn't.
1885 1883
1886 1884 This is much easier than the previous function as we can assume that
1887 1885 the recipient has any changenode we aren't sending them.
1888 1886
1889 1887 common is the set of common nodes between remote and self"""
1890 1888
1891 1889 self.hook('preoutgoing', throw=True, source=source)
1892 1890
1893 1891 cl = self.changelog
1894 1892 nodes = cl.findmissing(common)
1895 1893 revset = set([cl.rev(n) for n in nodes])
1896 1894 self.changegroupinfo(nodes, source)
1897 1895
1898 1896 def identity(x):
1899 1897 return x
1900 1898
1901 1899 def gennodelst(log):
1902 1900 for r in log:
1903 1901 if log.linkrev(r) in revset:
1904 1902 yield log.node(r)
1905 1903
1906 1904 def changed_file_collector(changedfileset):
1907 1905 def collect_changed_files(clnode):
1908 1906 c = cl.read(clnode)
1909 1907 changedfileset.update(c[3])
1910 1908 return collect_changed_files
1911 1909
1912 1910 def lookuprevlink_func(revlog):
1913 1911 def lookuprevlink(n):
1914 1912 return cl.node(revlog.linkrev(revlog.rev(n)))
1915 1913 return lookuprevlink
1916 1914
1917 1915 def gengroup():
1918 1916 # construct a list of all changed files
1919 1917 changedfiles = set()
1920 1918
1921 1919 for chnk in cl.group(nodes, identity,
1922 1920 changed_file_collector(changedfiles)):
1923 1921 yield chnk
1924 1922
1925 1923 mnfst = self.manifest
1926 1924 nodeiter = gennodelst(mnfst)
1927 1925 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1928 1926 yield chnk
1929 1927
1930 1928 for fname in sorted(changedfiles):
1931 1929 filerevlog = self.file(fname)
1932 1930 if not len(filerevlog):
1933 1931 raise util.Abort(_("empty or missing revlog for %s") % fname)
1934 1932 nodeiter = gennodelst(filerevlog)
1935 1933 nodeiter = list(nodeiter)
1936 1934 if nodeiter:
1937 1935 yield changegroup.chunkheader(len(fname))
1938 1936 yield fname
1939 1937 lookup = lookuprevlink_func(filerevlog)
1940 1938 for chnk in filerevlog.group(nodeiter, lookup):
1941 1939 yield chnk
1942 1940
1943 1941 yield changegroup.closechunk()
1944 1942
1945 1943 if nodes:
1946 1944 self.hook('outgoing', node=hex(nodes[0]), source=source)
1947 1945
1948 1946 return util.chunkbuffer(gengroup())
1949 1947
1950 1948 def addchangegroup(self, source, srctype, url, emptyok=False):
1951 1949 """add changegroup to repo.
1952 1950
1953 1951 return values:
1954 1952 - nothing changed or no source: 0
1955 1953 - more heads than before: 1+added heads (2..n)
1956 1954 - less heads than before: -1-removed heads (-2..-n)
1957 1955 - number of heads stays the same: 1
1958 1956 """
1959 1957 def csmap(x):
1960 1958 self.ui.debug(_("add changeset %s\n") % short(x))
1961 1959 return len(cl)
1962 1960
1963 1961 def revmap(x):
1964 1962 return cl.rev(x)
1965 1963
1966 1964 if not source:
1967 1965 return 0
1968 1966
1969 1967 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1970 1968
1971 1969 changesets = files = revisions = 0
1972 1970
1973 1971 # write changelog data to temp files so concurrent readers will not see
1974 1972 # inconsistent view
1975 1973 cl = self.changelog
1976 1974 cl.delayupdate()
1977 1975 oldheads = len(cl.heads())
1978 1976
1979 1977 tr = self.transaction()
1980 1978 try:
1981 1979 trp = weakref.proxy(tr)
1982 1980 # pull off the changeset group
1983 1981 self.ui.status(_("adding changesets\n"))
1984 1982 clstart = len(cl)
1985 1983 chunkiter = changegroup.chunkiter(source)
1986 1984 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok:
1987 1985 raise util.Abort(_("received changelog group is empty"))
1988 1986 clend = len(cl)
1989 1987 changesets = clend - clstart
1990 1988
1991 1989 # pull off the manifest group
1992 1990 self.ui.status(_("adding manifests\n"))
1993 1991 chunkiter = changegroup.chunkiter(source)
1994 1992 # no need to check for empty manifest group here:
1995 1993 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1996 1994 # no new manifest will be created and the manifest group will
1997 1995 # be empty during the pull
1998 1996 self.manifest.addgroup(chunkiter, revmap, trp)
1999 1997
2000 1998 # process the files
2001 1999 self.ui.status(_("adding file changes\n"))
2002 2000 while 1:
2003 2001 f = changegroup.getchunk(source)
2004 2002 if not f:
2005 2003 break
2006 2004 self.ui.debug(_("adding %s revisions\n") % f)
2007 2005 fl = self.file(f)
2008 2006 o = len(fl)
2009 2007 chunkiter = changegroup.chunkiter(source)
2010 2008 if fl.addgroup(chunkiter, revmap, trp) is None:
2011 2009 raise util.Abort(_("received file revlog group is empty"))
2012 2010 revisions += len(fl) - o
2013 2011 files += 1
2014 2012
2015 2013 newheads = len(cl.heads())
2016 2014 heads = ""
2017 2015 if oldheads and newheads != oldheads:
2018 2016 heads = _(" (%+d heads)") % (newheads - oldheads)
2019 2017
2020 2018 self.ui.status(_("added %d changesets"
2021 2019 " with %d changes to %d files%s\n")
2022 2020 % (changesets, revisions, files, heads))
2023 2021
2024 2022 if changesets > 0:
2025 2023 p = lambda: cl.writepending() and self.root or ""
2026 2024 self.hook('pretxnchangegroup', throw=True,
2027 2025 node=hex(cl.node(clstart)), source=srctype,
2028 2026 url=url, pending=p)
2029 2027
2030 2028 # make changelog see real files again
2031 2029 cl.finalize(trp)
2032 2030
2033 2031 tr.close()
2034 2032 finally:
2035 2033 del tr
2036 2034
2037 2035 if changesets > 0:
2038 2036 # forcefully update the on-disk branch cache
2039 2037 self.ui.debug(_("updating the branch cache\n"))
2040 2038 self.branchtags()
2041 2039 self.hook("changegroup", node=hex(cl.node(clstart)),
2042 2040 source=srctype, url=url)
2043 2041
2044 2042 for i in xrange(clstart, clend):
2045 2043 self.hook("incoming", node=hex(cl.node(i)),
2046 2044 source=srctype, url=url)
2047 2045
2048 2046 # never return 0 here:
2049 2047 if newheads < oldheads:
2050 2048 return newheads - oldheads - 1
2051 2049 else:
2052 2050 return newheads - oldheads + 1
2053 2051
2054 2052
2055 2053 def stream_in(self, remote):
2056 2054 fp = remote.stream_out()
2057 2055 l = fp.readline()
2058 2056 try:
2059 2057 resp = int(l)
2060 2058 except ValueError:
2061 2059 raise error.ResponseError(
2062 2060 _('Unexpected response from remote server:'), l)
2063 2061 if resp == 1:
2064 2062 raise util.Abort(_('operation forbidden by server'))
2065 2063 elif resp == 2:
2066 2064 raise util.Abort(_('locking the remote repository failed'))
2067 2065 elif resp != 0:
2068 2066 raise util.Abort(_('the server sent an unknown error code'))
2069 2067 self.ui.status(_('streaming all changes\n'))
2070 2068 l = fp.readline()
2071 2069 try:
2072 2070 total_files, total_bytes = map(int, l.split(' ', 1))
2073 2071 except (ValueError, TypeError):
2074 2072 raise error.ResponseError(
2075 2073 _('Unexpected response from remote server:'), l)
2076 2074 self.ui.status(_('%d files to transfer, %s of data\n') %
2077 2075 (total_files, util.bytecount(total_bytes)))
2078 2076 start = time.time()
2079 2077 for i in xrange(total_files):
2080 2078 # XXX doesn't support '\n' or '\r' in filenames
2081 2079 l = fp.readline()
2082 2080 try:
2083 2081 name, size = l.split('\0', 1)
2084 2082 size = int(size)
2085 2083 except (ValueError, TypeError):
2086 2084 raise error.ResponseError(
2087 2085 _('Unexpected response from remote server:'), l)
2088 2086 self.ui.debug(_('adding %s (%s)\n') % (name, util.bytecount(size)))
2089 2087 # for backwards compat, name was partially encoded
2090 2088 ofp = self.sopener(store.decodedir(name), 'w')
2091 2089 for chunk in util.filechunkiter(fp, limit=size):
2092 2090 ofp.write(chunk)
2093 2091 ofp.close()
2094 2092 elapsed = time.time() - start
2095 2093 if elapsed <= 0:
2096 2094 elapsed = 0.001
2097 2095 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
2098 2096 (util.bytecount(total_bytes), elapsed,
2099 2097 util.bytecount(total_bytes / elapsed)))
2100 2098 self.invalidate()
2101 2099 return len(self.heads()) + 1
2102 2100
2103 2101 def clone(self, remote, heads=[], stream=False):
2104 2102 '''clone remote repository.
2105 2103
2106 2104 keyword arguments:
2107 2105 heads: list of revs to clone (forces use of pull)
2108 2106 stream: use streaming clone if possible'''
2109 2107
2110 2108 # now, all clients that can request uncompressed clones can
2111 2109 # read repo formats supported by all servers that can serve
2112 2110 # them.
2113 2111
2114 2112 # if revlog format changes, client will have to check version
2115 2113 # and format flags on "stream" capability, and use
2116 2114 # uncompressed only if compatible.
2117 2115
2118 2116 if stream and not heads and remote.capable('stream'):
2119 2117 return self.stream_in(remote)
2120 2118 return self.pull(remote, heads)
2121 2119
2122 2120 # used to avoid circular references so destructors work
2123 2121 def aftertrans(files):
2124 2122 renamefiles = [tuple(t) for t in files]
2125 2123 def a():
2126 2124 for src, dest in renamefiles:
2127 2125 util.rename(src, dest)
2128 2126 return a
2129 2127
2130 2128 def instance(ui, path, create):
2131 2129 return localrepository(ui, util.drop_scheme('file', path), create)
2132 2130
2133 2131 def islocal(path):
2134 2132 return True
General Comments 0
You need to be logged in to leave comments. Login now