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