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