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