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