##// END OF EJS Templates
Improve branch cache sanity check for mq
Matt Mackall -
r3443:e6045fc3 default
parent child Browse files
Show More
@@ -1,1810 +1,1811
1 1 # localrepo.py - read/write repository class for mercurial
2 2 #
3 3 # Copyright 2005, 2006 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from node import *
9 9 from i18n import gettext as _
10 10 from demandload import *
11 11 import repo
12 12 demandload(globals(), "appendfile changegroup")
13 13 demandload(globals(), "changelog dirstate filelog manifest context")
14 14 demandload(globals(), "re lock transaction tempfile stat mdiff errno ui")
15 15 demandload(globals(), "os revlog time util")
16 16
17 17 class localrepository(repo.repository):
18 18 capabilities = ()
19 19
20 20 def __del__(self):
21 21 self.transhandle = None
22 22 def __init__(self, parentui, path=None, create=0):
23 23 repo.repository.__init__(self)
24 24 if not path:
25 25 p = os.getcwd()
26 26 while not os.path.isdir(os.path.join(p, ".hg")):
27 27 oldp = p
28 28 p = os.path.dirname(p)
29 29 if p == oldp:
30 30 raise repo.RepoError(_("There is no Mercurial repository"
31 31 " here (.hg not found)"))
32 32 path = p
33 33 self.path = os.path.join(path, ".hg")
34 34
35 35 if not os.path.isdir(self.path):
36 36 if create:
37 37 if not os.path.exists(path):
38 38 os.mkdir(path)
39 39 os.mkdir(self.path)
40 40 os.mkdir(self.join("data"))
41 41 else:
42 42 raise repo.RepoError(_("repository %s not found") % path)
43 43 elif create:
44 44 raise repo.RepoError(_("repository %s already exists") % path)
45 45
46 46 self.root = os.path.abspath(path)
47 47 self.origroot = path
48 48 self.ui = ui.ui(parentui=parentui)
49 49 self.opener = util.opener(self.path)
50 50 self.wopener = util.opener(self.root)
51 51
52 52 try:
53 53 self.ui.readconfig(self.join("hgrc"), self.root)
54 54 except IOError:
55 55 pass
56 56
57 57 v = self.ui.configrevlog()
58 58 self.revlogversion = int(v.get('format', revlog.REVLOG_DEFAULT_FORMAT))
59 59 self.revlogv1 = self.revlogversion != revlog.REVLOGV0
60 60 fl = v.get('flags', None)
61 61 flags = 0
62 62 if fl != None:
63 63 for x in fl.split():
64 64 flags |= revlog.flagstr(x)
65 65 elif self.revlogv1:
66 66 flags = revlog.REVLOG_DEFAULT_FLAGS
67 67
68 68 v = self.revlogversion | flags
69 69 self.manifest = manifest.manifest(self.opener, v)
70 70 self.changelog = changelog.changelog(self.opener, v)
71 71
72 72 # the changelog might not have the inline index flag
73 73 # on. If the format of the changelog is the same as found in
74 74 # .hgrc, apply any flags found in the .hgrc as well.
75 75 # Otherwise, just version from the changelog
76 76 v = self.changelog.version
77 77 if v == self.revlogversion:
78 78 v |= flags
79 79 self.revlogversion = v
80 80
81 81 self.tagscache = None
82 82 self.branchcache = None
83 83 self.nodetagscache = None
84 84 self.encodepats = None
85 85 self.decodepats = None
86 86 self.transhandle = None
87 87
88 88 self.dirstate = dirstate.dirstate(self.opener, self.ui, self.root)
89 89
90 90 def url(self):
91 91 return 'file:' + self.root
92 92
93 93 def hook(self, name, throw=False, **args):
94 94 def callhook(hname, funcname):
95 95 '''call python hook. hook is callable object, looked up as
96 96 name in python module. if callable returns "true", hook
97 97 fails, else passes. if hook raises exception, treated as
98 98 hook failure. exception propagates if throw is "true".
99 99
100 100 reason for "true" meaning "hook failed" is so that
101 101 unmodified commands (e.g. mercurial.commands.update) can
102 102 be run as hooks without wrappers to convert return values.'''
103 103
104 104 self.ui.note(_("calling hook %s: %s\n") % (hname, funcname))
105 105 d = funcname.rfind('.')
106 106 if d == -1:
107 107 raise util.Abort(_('%s hook is invalid ("%s" not in a module)')
108 108 % (hname, funcname))
109 109 modname = funcname[:d]
110 110 try:
111 111 obj = __import__(modname)
112 112 except ImportError:
113 113 try:
114 114 # extensions are loaded with hgext_ prefix
115 115 obj = __import__("hgext_%s" % modname)
116 116 except ImportError:
117 117 raise util.Abort(_('%s hook is invalid '
118 118 '(import of "%s" failed)') %
119 119 (hname, modname))
120 120 try:
121 121 for p in funcname.split('.')[1:]:
122 122 obj = getattr(obj, p)
123 123 except AttributeError, err:
124 124 raise util.Abort(_('%s hook is invalid '
125 125 '("%s" is not defined)') %
126 126 (hname, funcname))
127 127 if not callable(obj):
128 128 raise util.Abort(_('%s hook is invalid '
129 129 '("%s" is not callable)') %
130 130 (hname, funcname))
131 131 try:
132 132 r = obj(ui=self.ui, repo=self, hooktype=name, **args)
133 133 except (KeyboardInterrupt, util.SignalInterrupt):
134 134 raise
135 135 except Exception, exc:
136 136 if isinstance(exc, util.Abort):
137 137 self.ui.warn(_('error: %s hook failed: %s\n') %
138 138 (hname, exc.args[0]))
139 139 else:
140 140 self.ui.warn(_('error: %s hook raised an exception: '
141 141 '%s\n') % (hname, exc))
142 142 if throw:
143 143 raise
144 144 self.ui.print_exc()
145 145 return True
146 146 if r:
147 147 if throw:
148 148 raise util.Abort(_('%s hook failed') % hname)
149 149 self.ui.warn(_('warning: %s hook failed\n') % hname)
150 150 return r
151 151
152 152 def runhook(name, cmd):
153 153 self.ui.note(_("running hook %s: %s\n") % (name, cmd))
154 154 env = dict([('HG_' + k.upper(), v) for k, v in args.iteritems()])
155 155 r = util.system(cmd, environ=env, cwd=self.root)
156 156 if r:
157 157 desc, r = util.explain_exit(r)
158 158 if throw:
159 159 raise util.Abort(_('%s hook %s') % (name, desc))
160 160 self.ui.warn(_('warning: %s hook %s\n') % (name, desc))
161 161 return r
162 162
163 163 r = False
164 164 hooks = [(hname, cmd) for hname, cmd in self.ui.configitems("hooks")
165 165 if hname.split(".", 1)[0] == name and cmd]
166 166 hooks.sort()
167 167 for hname, cmd in hooks:
168 168 if cmd.startswith('python:'):
169 169 r = callhook(hname, cmd[7:].strip()) or r
170 170 else:
171 171 r = runhook(hname, cmd) or r
172 172 return r
173 173
174 174 tag_disallowed = ':\r\n'
175 175
176 176 def tag(self, name, node, message, local, user, date):
177 177 '''tag a revision with a symbolic name.
178 178
179 179 if local is True, the tag is stored in a per-repository file.
180 180 otherwise, it is stored in the .hgtags file, and a new
181 181 changeset is committed with the change.
182 182
183 183 keyword arguments:
184 184
185 185 local: whether to store tag in non-version-controlled file
186 186 (default False)
187 187
188 188 message: commit message to use if committing
189 189
190 190 user: name of user to use if committing
191 191
192 192 date: date tuple to use if committing'''
193 193
194 194 for c in self.tag_disallowed:
195 195 if c in name:
196 196 raise util.Abort(_('%r cannot be used in a tag name') % c)
197 197
198 198 self.hook('pretag', throw=True, node=hex(node), tag=name, local=local)
199 199
200 200 if local:
201 201 self.opener('localtags', 'a').write('%s %s\n' % (hex(node), name))
202 202 self.hook('tag', node=hex(node), tag=name, local=local)
203 203 return
204 204
205 205 for x in self.status()[:5]:
206 206 if '.hgtags' in x:
207 207 raise util.Abort(_('working copy of .hgtags is changed '
208 208 '(please commit .hgtags manually)'))
209 209
210 210 self.wfile('.hgtags', 'ab').write('%s %s\n' % (hex(node), name))
211 211 if self.dirstate.state('.hgtags') == '?':
212 212 self.add(['.hgtags'])
213 213
214 214 self.commit(['.hgtags'], message, user, date)
215 215 self.hook('tag', node=hex(node), tag=name, local=local)
216 216
217 217 def tags(self):
218 218 '''return a mapping of tag to node'''
219 219 if not self.tagscache:
220 220 self.tagscache = {}
221 221
222 222 def parsetag(line, context):
223 223 if not line:
224 224 return
225 225 s = l.split(" ", 1)
226 226 if len(s) != 2:
227 227 self.ui.warn(_("%s: cannot parse entry\n") % context)
228 228 return
229 229 node, key = s
230 230 key = key.strip()
231 231 try:
232 232 bin_n = bin(node)
233 233 except TypeError:
234 234 self.ui.warn(_("%s: node '%s' is not well formed\n") %
235 235 (context, node))
236 236 return
237 237 if bin_n not in self.changelog.nodemap:
238 238 self.ui.warn(_("%s: tag '%s' refers to unknown node\n") %
239 239 (context, key))
240 240 return
241 241 self.tagscache[key] = bin_n
242 242
243 243 # read the tags file from each head, ending with the tip,
244 244 # and add each tag found to the map, with "newer" ones
245 245 # taking precedence
246 246 heads = self.heads()
247 247 heads.reverse()
248 248 fl = self.file(".hgtags")
249 249 for node in heads:
250 250 change = self.changelog.read(node)
251 251 rev = self.changelog.rev(node)
252 252 fn, ff = self.manifest.find(change[0], '.hgtags')
253 253 if fn is None: continue
254 254 count = 0
255 255 for l in fl.read(fn).splitlines():
256 256 count += 1
257 257 parsetag(l, _(".hgtags (rev %d:%s), line %d") %
258 258 (rev, short(node), count))
259 259 try:
260 260 f = self.opener("localtags")
261 261 count = 0
262 262 for l in f:
263 263 count += 1
264 264 parsetag(l, _("localtags, line %d") % count)
265 265 except IOError:
266 266 pass
267 267
268 268 self.tagscache['tip'] = self.changelog.tip()
269 269
270 270 return self.tagscache
271 271
272 272 def tagslist(self):
273 273 '''return a list of tags ordered by revision'''
274 274 l = []
275 275 for t, n in self.tags().items():
276 276 try:
277 277 r = self.changelog.rev(n)
278 278 except:
279 279 r = -2 # sort to the beginning of the list if unknown
280 280 l.append((r, t, n))
281 281 l.sort()
282 282 return [(t, n) for r, t, n in l]
283 283
284 284 def nodetags(self, node):
285 285 '''return the tags associated with a node'''
286 286 if not self.nodetagscache:
287 287 self.nodetagscache = {}
288 288 for t, n in self.tags().items():
289 289 self.nodetagscache.setdefault(n, []).append(t)
290 290 return self.nodetagscache.get(node, [])
291 291
292 292 def branchtags(self):
293 293 if self.branchcache != None:
294 294 return self.branchcache
295 295
296 296 self.branchcache = {} # avoid recursion in changectx
297 297
298 298 try:
299 299 f = self.opener("branches.cache")
300 300 last, lrev = f.readline().rstrip().split(" ", 1)
301 301 last, lrev = bin(last), int(lrev)
302 if self.changelog.node(lrev) == last: # sanity check
302 if (lrev < self.changelog.count() and
303 self.changelog.node(lrev) == last): # sanity check
303 304 for l in f:
304 305 node, label = l.rstrip().split(" ", 1)
305 306 self.branchcache[label] = bin(node)
306 307 f.close()
307 308 except IOError:
308 309 last, lrev = nullid, -1
309 310 lrev = self.changelog.rev(last)
310 311
311 312 tip = self.changelog.count() - 1
312 313 if lrev != tip:
313 314 for r in xrange(lrev + 1, tip + 1):
314 315 c = self.changectx(r)
315 316 b = c.branch()
316 317 if b:
317 318 self.branchcache[b] = c.node()
318 319 self._writebranchcache()
319 320
320 321 return self.branchcache
321 322
322 323 def _writebranchcache(self):
323 324 f = self.opener("branches.cache", "w")
324 325 t = self.changelog.tip()
325 326 f.write("%s %s\n" % (hex(t), self.changelog.count() - 1))
326 327 for label, node in self.branchcache.iteritems():
327 328 f.write("%s %s\n" % (hex(node), label))
328 329
329 330 def lookup(self, key):
330 331 if key == '.':
331 332 key = self.dirstate.parents()[0]
332 333 if key == nullid:
333 334 raise repo.RepoError(_("no revision checked out"))
334 335 if key in self.tags():
335 336 return self.tags()[key]
336 337 if key in self.branchtags():
337 338 return self.branchtags()[key]
338 339 try:
339 340 return self.changelog.lookup(key)
340 341 except:
341 342 raise repo.RepoError(_("unknown revision '%s'") % key)
342 343
343 344 def dev(self):
344 345 return os.lstat(self.path).st_dev
345 346
346 347 def local(self):
347 348 return True
348 349
349 350 def join(self, f):
350 351 return os.path.join(self.path, f)
351 352
352 353 def wjoin(self, f):
353 354 return os.path.join(self.root, f)
354 355
355 356 def file(self, f):
356 357 if f[0] == '/':
357 358 f = f[1:]
358 359 return filelog.filelog(self.opener, f, self.revlogversion)
359 360
360 361 def changectx(self, changeid=None):
361 362 return context.changectx(self, changeid)
362 363
363 364 def workingctx(self):
364 365 return context.workingctx(self)
365 366
366 367 def parents(self, changeid=None):
367 368 '''
368 369 get list of changectxs for parents of changeid or working directory
369 370 '''
370 371 if changeid is None:
371 372 pl = self.dirstate.parents()
372 373 else:
373 374 n = self.changelog.lookup(changeid)
374 375 pl = self.changelog.parents(n)
375 376 if pl[1] == nullid:
376 377 return [self.changectx(pl[0])]
377 378 return [self.changectx(pl[0]), self.changectx(pl[1])]
378 379
379 380 def filectx(self, path, changeid=None, fileid=None):
380 381 """changeid can be a changeset revision, node, or tag.
381 382 fileid can be a file revision or node."""
382 383 return context.filectx(self, path, changeid, fileid)
383 384
384 385 def getcwd(self):
385 386 return self.dirstate.getcwd()
386 387
387 388 def wfile(self, f, mode='r'):
388 389 return self.wopener(f, mode)
389 390
390 391 def wread(self, filename):
391 392 if self.encodepats == None:
392 393 l = []
393 394 for pat, cmd in self.ui.configitems("encode"):
394 395 mf = util.matcher(self.root, "", [pat], [], [])[1]
395 396 l.append((mf, cmd))
396 397 self.encodepats = l
397 398
398 399 data = self.wopener(filename, 'r').read()
399 400
400 401 for mf, cmd in self.encodepats:
401 402 if mf(filename):
402 403 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
403 404 data = util.filter(data, cmd)
404 405 break
405 406
406 407 return data
407 408
408 409 def wwrite(self, filename, data, fd=None):
409 410 if self.decodepats == None:
410 411 l = []
411 412 for pat, cmd in self.ui.configitems("decode"):
412 413 mf = util.matcher(self.root, "", [pat], [], [])[1]
413 414 l.append((mf, cmd))
414 415 self.decodepats = l
415 416
416 417 for mf, cmd in self.decodepats:
417 418 if mf(filename):
418 419 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
419 420 data = util.filter(data, cmd)
420 421 break
421 422
422 423 if fd:
423 424 return fd.write(data)
424 425 return self.wopener(filename, 'w').write(data)
425 426
426 427 def transaction(self):
427 428 tr = self.transhandle
428 429 if tr != None and tr.running():
429 430 return tr.nest()
430 431
431 432 # save dirstate for rollback
432 433 try:
433 434 ds = self.opener("dirstate").read()
434 435 except IOError:
435 436 ds = ""
436 437 self.opener("journal.dirstate", "w").write(ds)
437 438
438 439 tr = transaction.transaction(self.ui.warn, self.opener,
439 440 self.join("journal"),
440 441 aftertrans(self.path))
441 442 self.transhandle = tr
442 443 return tr
443 444
444 445 def recover(self):
445 446 l = self.lock()
446 447 if os.path.exists(self.join("journal")):
447 448 self.ui.status(_("rolling back interrupted transaction\n"))
448 449 transaction.rollback(self.opener, self.join("journal"))
449 450 self.reload()
450 451 return True
451 452 else:
452 453 self.ui.warn(_("no interrupted transaction available\n"))
453 454 return False
454 455
455 456 def rollback(self, wlock=None):
456 457 if not wlock:
457 458 wlock = self.wlock()
458 459 l = self.lock()
459 460 if os.path.exists(self.join("undo")):
460 461 self.ui.status(_("rolling back last transaction\n"))
461 462 transaction.rollback(self.opener, self.join("undo"))
462 463 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
463 464 self.reload()
464 465 self.wreload()
465 466 else:
466 467 self.ui.warn(_("no rollback information available\n"))
467 468
468 469 def wreload(self):
469 470 self.dirstate.read()
470 471
471 472 def reload(self):
472 473 self.changelog.load()
473 474 self.manifest.load()
474 475 self.tagscache = None
475 476 self.nodetagscache = None
476 477
477 478 def do_lock(self, lockname, wait, releasefn=None, acquirefn=None,
478 479 desc=None):
479 480 try:
480 481 l = lock.lock(self.join(lockname), 0, releasefn, desc=desc)
481 482 except lock.LockHeld, inst:
482 483 if not wait:
483 484 raise
484 485 self.ui.warn(_("waiting for lock on %s held by %s\n") %
485 486 (desc, inst.args[0]))
486 487 # default to 600 seconds timeout
487 488 l = lock.lock(self.join(lockname),
488 489 int(self.ui.config("ui", "timeout") or 600),
489 490 releasefn, desc=desc)
490 491 if acquirefn:
491 492 acquirefn()
492 493 return l
493 494
494 495 def lock(self, wait=1):
495 496 return self.do_lock("lock", wait, acquirefn=self.reload,
496 497 desc=_('repository %s') % self.origroot)
497 498
498 499 def wlock(self, wait=1):
499 500 return self.do_lock("wlock", wait, self.dirstate.write,
500 501 self.wreload,
501 502 desc=_('working directory of %s') % self.origroot)
502 503
503 504 def filecommit(self, fn, manifest1, manifest2, linkrev, transaction, changelist):
504 505 """
505 506 commit an individual file as part of a larger transaction
506 507 """
507 508
508 509 t = self.wread(fn)
509 510 fl = self.file(fn)
510 511 fp1 = manifest1.get(fn, nullid)
511 512 fp2 = manifest2.get(fn, nullid)
512 513
513 514 meta = {}
514 515 cp = self.dirstate.copied(fn)
515 516 if cp:
516 517 meta["copy"] = cp
517 518 if not manifest2: # not a branch merge
518 519 meta["copyrev"] = hex(manifest1.get(cp, nullid))
519 520 fp2 = nullid
520 521 elif fp2 != nullid: # copied on remote side
521 522 meta["copyrev"] = hex(manifest1.get(cp, nullid))
522 523 else: # copied on local side, reversed
523 524 meta["copyrev"] = hex(manifest2.get(cp))
524 525 fp2 = nullid
525 526 self.ui.debug(_(" %s: copy %s:%s\n") %
526 527 (fn, cp, meta["copyrev"]))
527 528 fp1 = nullid
528 529 elif fp2 != nullid:
529 530 # is one parent an ancestor of the other?
530 531 fpa = fl.ancestor(fp1, fp2)
531 532 if fpa == fp1:
532 533 fp1, fp2 = fp2, nullid
533 534 elif fpa == fp2:
534 535 fp2 = nullid
535 536
536 537 # is the file unmodified from the parent? report existing entry
537 538 if fp2 == nullid and not fl.cmp(fp1, t):
538 539 return fp1
539 540
540 541 changelist.append(fn)
541 542 return fl.add(t, meta, transaction, linkrev, fp1, fp2)
542 543
543 544 def rawcommit(self, files, text, user, date, p1=None, p2=None, wlock=None):
544 545 orig_parent = self.dirstate.parents()[0] or nullid
545 546 p1 = p1 or self.dirstate.parents()[0] or nullid
546 547 p2 = p2 or self.dirstate.parents()[1] or nullid
547 548 c1 = self.changelog.read(p1)
548 549 c2 = self.changelog.read(p2)
549 550 m1 = self.manifest.read(c1[0]).copy()
550 551 m2 = self.manifest.read(c2[0])
551 552 changed = []
552 553 removed = []
553 554
554 555 if orig_parent == p1:
555 556 update_dirstate = 1
556 557 else:
557 558 update_dirstate = 0
558 559
559 560 if not wlock:
560 561 wlock = self.wlock()
561 562 l = self.lock()
562 563 tr = self.transaction()
563 564 linkrev = self.changelog.count()
564 565 for f in files:
565 566 try:
566 567 m1[f] = self.filecommit(f, m1, m2, linkrev, tr, changed)
567 568 m1.set(f, util.is_exec(self.wjoin(f), m1.execf(f)))
568 569 except IOError:
569 570 try:
570 571 del m1[f]
571 572 if update_dirstate:
572 573 self.dirstate.forget([f])
573 574 removed.append(f)
574 575 except:
575 576 # deleted from p2?
576 577 pass
577 578
578 579 mnode = self.manifest.add(m1, tr, linkrev, c1[0], c2[0])
579 580 user = user or self.ui.username()
580 581 n = self.changelog.add(mnode, changed + removed, text,
581 582 tr, p1, p2, user, date)
582 583 tr.close()
583 584 if update_dirstate:
584 585 self.dirstate.setparents(n, nullid)
585 586
586 587 def commit(self, files=None, text="", user=None, date=None,
587 588 match=util.always, force=False, lock=None, wlock=None,
588 589 force_editor=False):
589 590 commit = []
590 591 remove = []
591 592 changed = []
592 593
593 594 if files:
594 595 for f in files:
595 596 s = self.dirstate.state(f)
596 597 if s in 'nmai':
597 598 commit.append(f)
598 599 elif s == 'r':
599 600 remove.append(f)
600 601 else:
601 602 self.ui.warn(_("%s not tracked!\n") % f)
602 603 else:
603 604 modified, added, removed, deleted, unknown = self.status(match=match)[:5]
604 605 commit = modified + added
605 606 remove = removed
606 607
607 608 p1, p2 = self.dirstate.parents()
608 609 c1 = self.changelog.read(p1)
609 610 c2 = self.changelog.read(p2)
610 611 m1 = self.manifest.read(c1[0]).copy()
611 612 m2 = self.manifest.read(c2[0])
612 613
613 614 branchname = self.workingctx().branch()
614 615 oldname = c1[5].get("branch", "")
615 616
616 617 if not commit and not remove and not force and p2 == nullid and \
617 618 branchname == oldname:
618 619 self.ui.status(_("nothing changed\n"))
619 620 return None
620 621
621 622 xp1 = hex(p1)
622 623 if p2 == nullid: xp2 = ''
623 624 else: xp2 = hex(p2)
624 625
625 626 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
626 627
627 628 if not wlock:
628 629 wlock = self.wlock()
629 630 if not lock:
630 631 lock = self.lock()
631 632 tr = self.transaction()
632 633
633 634 # check in files
634 635 new = {}
635 636 linkrev = self.changelog.count()
636 637 commit.sort()
637 638 for f in commit:
638 639 self.ui.note(f + "\n")
639 640 try:
640 641 new[f] = self.filecommit(f, m1, m2, linkrev, tr, changed)
641 642 m1.set(f, util.is_exec(self.wjoin(f), m1.execf(f)))
642 643 except IOError:
643 644 self.ui.warn(_("trouble committing %s!\n") % f)
644 645 raise
645 646
646 647 # update manifest
647 648 m1.update(new)
648 649 for f in remove:
649 650 if f in m1:
650 651 del m1[f]
651 652 mn = self.manifest.add(m1, tr, linkrev, c1[0], c2[0], (new, remove))
652 653
653 654 # add changeset
654 655 new = new.keys()
655 656 new.sort()
656 657
657 658 user = user or self.ui.username()
658 659 if not text or force_editor:
659 660 edittext = []
660 661 if text:
661 662 edittext.append(text)
662 663 edittext.append("")
663 664 if p2 != nullid:
664 665 edittext.append("HG: branch merge")
665 666 edittext.extend(["HG: changed %s" % f for f in changed])
666 667 edittext.extend(["HG: removed %s" % f for f in remove])
667 668 if not changed and not remove:
668 669 edittext.append("HG: no files changed")
669 670 edittext.append("")
670 671 # run editor in the repository root
671 672 olddir = os.getcwd()
672 673 os.chdir(self.root)
673 674 text = self.ui.edit("\n".join(edittext), user)
674 675 os.chdir(olddir)
675 676
676 677 lines = [line.rstrip() for line in text.rstrip().splitlines()]
677 678 while lines and not lines[0]:
678 679 del lines[0]
679 680 if not lines:
680 681 return None
681 682 text = '\n'.join(lines)
682 683 extra = {}
683 684 if branchname:
684 685 extra["branch"] = branchname
685 686 n = self.changelog.add(mn, changed + remove, text, tr, p1, p2,
686 687 user, date, extra)
687 688 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
688 689 parent2=xp2)
689 690 tr.close()
690 691
691 692 self.dirstate.setparents(n)
692 693 self.dirstate.update(new, "n")
693 694 self.dirstate.forget(remove)
694 695
695 696 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
696 697 return n
697 698
698 699 def walk(self, node=None, files=[], match=util.always, badmatch=None):
699 700 if node:
700 701 fdict = dict.fromkeys(files)
701 702 for fn in self.manifest.read(self.changelog.read(node)[0]):
702 703 for ffn in fdict:
703 704 # match if the file is the exact name or a directory
704 705 if ffn == fn or fn.startswith("%s/" % ffn):
705 706 del fdict[ffn]
706 707 break
707 708 if match(fn):
708 709 yield 'm', fn
709 710 for fn in fdict:
710 711 if badmatch and badmatch(fn):
711 712 if match(fn):
712 713 yield 'b', fn
713 714 else:
714 715 self.ui.warn(_('%s: No such file in rev %s\n') % (
715 716 util.pathto(self.getcwd(), fn), short(node)))
716 717 else:
717 718 for src, fn in self.dirstate.walk(files, match, badmatch=badmatch):
718 719 yield src, fn
719 720
720 721 def status(self, node1=None, node2=None, files=[], match=util.always,
721 722 wlock=None, list_ignored=False, list_clean=False):
722 723 """return status of files between two nodes or node and working directory
723 724
724 725 If node1 is None, use the first dirstate parent instead.
725 726 If node2 is None, compare node1 with working directory.
726 727 """
727 728
728 729 def fcmp(fn, mf):
729 730 t1 = self.wread(fn)
730 731 return self.file(fn).cmp(mf.get(fn, nullid), t1)
731 732
732 733 def mfmatches(node):
733 734 change = self.changelog.read(node)
734 735 mf = self.manifest.read(change[0]).copy()
735 736 for fn in mf.keys():
736 737 if not match(fn):
737 738 del mf[fn]
738 739 return mf
739 740
740 741 modified, added, removed, deleted, unknown = [], [], [], [], []
741 742 ignored, clean = [], []
742 743
743 744 compareworking = False
744 745 if not node1 or (not node2 and node1 == self.dirstate.parents()[0]):
745 746 compareworking = True
746 747
747 748 if not compareworking:
748 749 # read the manifest from node1 before the manifest from node2,
749 750 # so that we'll hit the manifest cache if we're going through
750 751 # all the revisions in parent->child order.
751 752 mf1 = mfmatches(node1)
752 753
753 754 # are we comparing the working directory?
754 755 if not node2:
755 756 if not wlock:
756 757 try:
757 758 wlock = self.wlock(wait=0)
758 759 except lock.LockException:
759 760 wlock = None
760 761 (lookup, modified, added, removed, deleted, unknown,
761 762 ignored, clean) = self.dirstate.status(files, match,
762 763 list_ignored, list_clean)
763 764
764 765 # are we comparing working dir against its parent?
765 766 if compareworking:
766 767 if lookup:
767 768 # do a full compare of any files that might have changed
768 769 mf2 = mfmatches(self.dirstate.parents()[0])
769 770 for f in lookup:
770 771 if fcmp(f, mf2):
771 772 modified.append(f)
772 773 else:
773 774 clean.append(f)
774 775 if wlock is not None:
775 776 self.dirstate.update([f], "n")
776 777 else:
777 778 # we are comparing working dir against non-parent
778 779 # generate a pseudo-manifest for the working dir
779 780 # XXX: create it in dirstate.py ?
780 781 mf2 = mfmatches(self.dirstate.parents()[0])
781 782 for f in lookup + modified + added:
782 783 mf2[f] = ""
783 784 mf2.set(f, execf=util.is_exec(self.wjoin(f), mf2.execf(f)))
784 785 for f in removed:
785 786 if f in mf2:
786 787 del mf2[f]
787 788 else:
788 789 # we are comparing two revisions
789 790 mf2 = mfmatches(node2)
790 791
791 792 if not compareworking:
792 793 # flush lists from dirstate before comparing manifests
793 794 modified, added, clean = [], [], []
794 795
795 796 # make sure to sort the files so we talk to the disk in a
796 797 # reasonable order
797 798 mf2keys = mf2.keys()
798 799 mf2keys.sort()
799 800 for fn in mf2keys:
800 801 if mf1.has_key(fn):
801 802 if mf1.flags(fn) != mf2.flags(fn) or \
802 803 (mf1[fn] != mf2[fn] and (mf2[fn] != "" or fcmp(fn, mf1))):
803 804 modified.append(fn)
804 805 elif list_clean:
805 806 clean.append(fn)
806 807 del mf1[fn]
807 808 else:
808 809 added.append(fn)
809 810
810 811 removed = mf1.keys()
811 812
812 813 # sort and return results:
813 814 for l in modified, added, removed, deleted, unknown, ignored, clean:
814 815 l.sort()
815 816 return (modified, added, removed, deleted, unknown, ignored, clean)
816 817
817 818 def add(self, list, wlock=None):
818 819 if not wlock:
819 820 wlock = self.wlock()
820 821 for f in list:
821 822 p = self.wjoin(f)
822 823 if not os.path.exists(p):
823 824 self.ui.warn(_("%s does not exist!\n") % f)
824 825 elif not os.path.isfile(p):
825 826 self.ui.warn(_("%s not added: only files supported currently\n")
826 827 % f)
827 828 elif self.dirstate.state(f) in 'an':
828 829 self.ui.warn(_("%s already tracked!\n") % f)
829 830 else:
830 831 self.dirstate.update([f], "a")
831 832
832 833 def forget(self, list, wlock=None):
833 834 if not wlock:
834 835 wlock = self.wlock()
835 836 for f in list:
836 837 if self.dirstate.state(f) not in 'ai':
837 838 self.ui.warn(_("%s not added!\n") % f)
838 839 else:
839 840 self.dirstate.forget([f])
840 841
841 842 def remove(self, list, unlink=False, wlock=None):
842 843 if unlink:
843 844 for f in list:
844 845 try:
845 846 util.unlink(self.wjoin(f))
846 847 except OSError, inst:
847 848 if inst.errno != errno.ENOENT:
848 849 raise
849 850 if not wlock:
850 851 wlock = self.wlock()
851 852 for f in list:
852 853 p = self.wjoin(f)
853 854 if os.path.exists(p):
854 855 self.ui.warn(_("%s still exists!\n") % f)
855 856 elif self.dirstate.state(f) == 'a':
856 857 self.dirstate.forget([f])
857 858 elif f not in self.dirstate:
858 859 self.ui.warn(_("%s not tracked!\n") % f)
859 860 else:
860 861 self.dirstate.update([f], "r")
861 862
862 863 def undelete(self, list, wlock=None):
863 864 p = self.dirstate.parents()[0]
864 865 mn = self.changelog.read(p)[0]
865 866 m = self.manifest.read(mn)
866 867 if not wlock:
867 868 wlock = self.wlock()
868 869 for f in list:
869 870 if self.dirstate.state(f) not in "r":
870 871 self.ui.warn("%s not removed!\n" % f)
871 872 else:
872 873 t = self.file(f).read(m[f])
873 874 self.wwrite(f, t)
874 875 util.set_exec(self.wjoin(f), m.execf(f))
875 876 self.dirstate.update([f], "n")
876 877
877 878 def copy(self, source, dest, wlock=None):
878 879 p = self.wjoin(dest)
879 880 if not os.path.exists(p):
880 881 self.ui.warn(_("%s does not exist!\n") % dest)
881 882 elif not os.path.isfile(p):
882 883 self.ui.warn(_("copy failed: %s is not a file\n") % dest)
883 884 else:
884 885 if not wlock:
885 886 wlock = self.wlock()
886 887 if self.dirstate.state(dest) == '?':
887 888 self.dirstate.update([dest], "a")
888 889 self.dirstate.copy(source, dest)
889 890
890 891 def heads(self, start=None):
891 892 heads = self.changelog.heads(start)
892 893 # sort the output in rev descending order
893 894 heads = [(-self.changelog.rev(h), h) for h in heads]
894 895 heads.sort()
895 896 return [n for (r, n) in heads]
896 897
897 898 # branchlookup returns a dict giving a list of branches for
898 899 # each head. A branch is defined as the tag of a node or
899 900 # the branch of the node's parents. If a node has multiple
900 901 # branch tags, tags are eliminated if they are visible from other
901 902 # branch tags.
902 903 #
903 904 # So, for this graph: a->b->c->d->e
904 905 # \ /
905 906 # aa -----/
906 907 # a has tag 2.6.12
907 908 # d has tag 2.6.13
908 909 # e would have branch tags for 2.6.12 and 2.6.13. Because the node
909 910 # for 2.6.12 can be reached from the node 2.6.13, that is eliminated
910 911 # from the list.
911 912 #
912 913 # It is possible that more than one head will have the same branch tag.
913 914 # callers need to check the result for multiple heads under the same
914 915 # branch tag if that is a problem for them (ie checkout of a specific
915 916 # branch).
916 917 #
917 918 # passing in a specific branch will limit the depth of the search
918 919 # through the parents. It won't limit the branches returned in the
919 920 # result though.
920 921 def branchlookup(self, heads=None, branch=None):
921 922 if not heads:
922 923 heads = self.heads()
923 924 headt = [ h for h in heads ]
924 925 chlog = self.changelog
925 926 branches = {}
926 927 merges = []
927 928 seenmerge = {}
928 929
929 930 # traverse the tree once for each head, recording in the branches
930 931 # dict which tags are visible from this head. The branches
931 932 # dict also records which tags are visible from each tag
932 933 # while we traverse.
933 934 while headt or merges:
934 935 if merges:
935 936 n, found = merges.pop()
936 937 visit = [n]
937 938 else:
938 939 h = headt.pop()
939 940 visit = [h]
940 941 found = [h]
941 942 seen = {}
942 943 while visit:
943 944 n = visit.pop()
944 945 if n in seen:
945 946 continue
946 947 pp = chlog.parents(n)
947 948 tags = self.nodetags(n)
948 949 if tags:
949 950 for x in tags:
950 951 if x == 'tip':
951 952 continue
952 953 for f in found:
953 954 branches.setdefault(f, {})[n] = 1
954 955 branches.setdefault(n, {})[n] = 1
955 956 break
956 957 if n not in found:
957 958 found.append(n)
958 959 if branch in tags:
959 960 continue
960 961 seen[n] = 1
961 962 if pp[1] != nullid and n not in seenmerge:
962 963 merges.append((pp[1], [x for x in found]))
963 964 seenmerge[n] = 1
964 965 if pp[0] != nullid:
965 966 visit.append(pp[0])
966 967 # traverse the branches dict, eliminating branch tags from each
967 968 # head that are visible from another branch tag for that head.
968 969 out = {}
969 970 viscache = {}
970 971 for h in heads:
971 972 def visible(node):
972 973 if node in viscache:
973 974 return viscache[node]
974 975 ret = {}
975 976 visit = [node]
976 977 while visit:
977 978 x = visit.pop()
978 979 if x in viscache:
979 980 ret.update(viscache[x])
980 981 elif x not in ret:
981 982 ret[x] = 1
982 983 if x in branches:
983 984 visit[len(visit):] = branches[x].keys()
984 985 viscache[node] = ret
985 986 return ret
986 987 if h not in branches:
987 988 continue
988 989 # O(n^2), but somewhat limited. This only searches the
989 990 # tags visible from a specific head, not all the tags in the
990 991 # whole repo.
991 992 for b in branches[h]:
992 993 vis = False
993 994 for bb in branches[h].keys():
994 995 if b != bb:
995 996 if b in visible(bb):
996 997 vis = True
997 998 break
998 999 if not vis:
999 1000 l = out.setdefault(h, [])
1000 1001 l[len(l):] = self.nodetags(b)
1001 1002 return out
1002 1003
1003 1004 def branches(self, nodes):
1004 1005 if not nodes:
1005 1006 nodes = [self.changelog.tip()]
1006 1007 b = []
1007 1008 for n in nodes:
1008 1009 t = n
1009 1010 while 1:
1010 1011 p = self.changelog.parents(n)
1011 1012 if p[1] != nullid or p[0] == nullid:
1012 1013 b.append((t, n, p[0], p[1]))
1013 1014 break
1014 1015 n = p[0]
1015 1016 return b
1016 1017
1017 1018 def between(self, pairs):
1018 1019 r = []
1019 1020
1020 1021 for top, bottom in pairs:
1021 1022 n, l, i = top, [], 0
1022 1023 f = 1
1023 1024
1024 1025 while n != bottom:
1025 1026 p = self.changelog.parents(n)[0]
1026 1027 if i == f:
1027 1028 l.append(n)
1028 1029 f = f * 2
1029 1030 n = p
1030 1031 i += 1
1031 1032
1032 1033 r.append(l)
1033 1034
1034 1035 return r
1035 1036
1036 1037 def findincoming(self, remote, base=None, heads=None, force=False):
1037 1038 """Return list of roots of the subsets of missing nodes from remote
1038 1039
1039 1040 If base dict is specified, assume that these nodes and their parents
1040 1041 exist on the remote side and that no child of a node of base exists
1041 1042 in both remote and self.
1042 1043 Furthermore base will be updated to include the nodes that exists
1043 1044 in self and remote but no children exists in self and remote.
1044 1045 If a list of heads is specified, return only nodes which are heads
1045 1046 or ancestors of these heads.
1046 1047
1047 1048 All the ancestors of base are in self and in remote.
1048 1049 All the descendants of the list returned are missing in self.
1049 1050 (and so we know that the rest of the nodes are missing in remote, see
1050 1051 outgoing)
1051 1052 """
1052 1053 m = self.changelog.nodemap
1053 1054 search = []
1054 1055 fetch = {}
1055 1056 seen = {}
1056 1057 seenbranch = {}
1057 1058 if base == None:
1058 1059 base = {}
1059 1060
1060 1061 if not heads:
1061 1062 heads = remote.heads()
1062 1063
1063 1064 if self.changelog.tip() == nullid:
1064 1065 base[nullid] = 1
1065 1066 if heads != [nullid]:
1066 1067 return [nullid]
1067 1068 return []
1068 1069
1069 1070 # assume we're closer to the tip than the root
1070 1071 # and start by examining the heads
1071 1072 self.ui.status(_("searching for changes\n"))
1072 1073
1073 1074 unknown = []
1074 1075 for h in heads:
1075 1076 if h not in m:
1076 1077 unknown.append(h)
1077 1078 else:
1078 1079 base[h] = 1
1079 1080
1080 1081 if not unknown:
1081 1082 return []
1082 1083
1083 1084 req = dict.fromkeys(unknown)
1084 1085 reqcnt = 0
1085 1086
1086 1087 # search through remote branches
1087 1088 # a 'branch' here is a linear segment of history, with four parts:
1088 1089 # head, root, first parent, second parent
1089 1090 # (a branch always has two parents (or none) by definition)
1090 1091 unknown = remote.branches(unknown)
1091 1092 while unknown:
1092 1093 r = []
1093 1094 while unknown:
1094 1095 n = unknown.pop(0)
1095 1096 if n[0] in seen:
1096 1097 continue
1097 1098
1098 1099 self.ui.debug(_("examining %s:%s\n")
1099 1100 % (short(n[0]), short(n[1])))
1100 1101 if n[0] == nullid: # found the end of the branch
1101 1102 pass
1102 1103 elif n in seenbranch:
1103 1104 self.ui.debug(_("branch already found\n"))
1104 1105 continue
1105 1106 elif n[1] and n[1] in m: # do we know the base?
1106 1107 self.ui.debug(_("found incomplete branch %s:%s\n")
1107 1108 % (short(n[0]), short(n[1])))
1108 1109 search.append(n) # schedule branch range for scanning
1109 1110 seenbranch[n] = 1
1110 1111 else:
1111 1112 if n[1] not in seen and n[1] not in fetch:
1112 1113 if n[2] in m and n[3] in m:
1113 1114 self.ui.debug(_("found new changeset %s\n") %
1114 1115 short(n[1]))
1115 1116 fetch[n[1]] = 1 # earliest unknown
1116 1117 for p in n[2:4]:
1117 1118 if p in m:
1118 1119 base[p] = 1 # latest known
1119 1120
1120 1121 for p in n[2:4]:
1121 1122 if p not in req and p not in m:
1122 1123 r.append(p)
1123 1124 req[p] = 1
1124 1125 seen[n[0]] = 1
1125 1126
1126 1127 if r:
1127 1128 reqcnt += 1
1128 1129 self.ui.debug(_("request %d: %s\n") %
1129 1130 (reqcnt, " ".join(map(short, r))))
1130 1131 for p in range(0, len(r), 10):
1131 1132 for b in remote.branches(r[p:p+10]):
1132 1133 self.ui.debug(_("received %s:%s\n") %
1133 1134 (short(b[0]), short(b[1])))
1134 1135 unknown.append(b)
1135 1136
1136 1137 # do binary search on the branches we found
1137 1138 while search:
1138 1139 n = search.pop(0)
1139 1140 reqcnt += 1
1140 1141 l = remote.between([(n[0], n[1])])[0]
1141 1142 l.append(n[1])
1142 1143 p = n[0]
1143 1144 f = 1
1144 1145 for i in l:
1145 1146 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1146 1147 if i in m:
1147 1148 if f <= 2:
1148 1149 self.ui.debug(_("found new branch changeset %s\n") %
1149 1150 short(p))
1150 1151 fetch[p] = 1
1151 1152 base[i] = 1
1152 1153 else:
1153 1154 self.ui.debug(_("narrowed branch search to %s:%s\n")
1154 1155 % (short(p), short(i)))
1155 1156 search.append((p, i))
1156 1157 break
1157 1158 p, f = i, f * 2
1158 1159
1159 1160 # sanity check our fetch list
1160 1161 for f in fetch.keys():
1161 1162 if f in m:
1162 1163 raise repo.RepoError(_("already have changeset ") + short(f[:4]))
1163 1164
1164 1165 if base.keys() == [nullid]:
1165 1166 if force:
1166 1167 self.ui.warn(_("warning: repository is unrelated\n"))
1167 1168 else:
1168 1169 raise util.Abort(_("repository is unrelated"))
1169 1170
1170 1171 self.ui.debug(_("found new changesets starting at ") +
1171 1172 " ".join([short(f) for f in fetch]) + "\n")
1172 1173
1173 1174 self.ui.debug(_("%d total queries\n") % reqcnt)
1174 1175
1175 1176 return fetch.keys()
1176 1177
1177 1178 def findoutgoing(self, remote, base=None, heads=None, force=False):
1178 1179 """Return list of nodes that are roots of subsets not in remote
1179 1180
1180 1181 If base dict is specified, assume that these nodes and their parents
1181 1182 exist on the remote side.
1182 1183 If a list of heads is specified, return only nodes which are heads
1183 1184 or ancestors of these heads, and return a second element which
1184 1185 contains all remote heads which get new children.
1185 1186 """
1186 1187 if base == None:
1187 1188 base = {}
1188 1189 self.findincoming(remote, base, heads, force=force)
1189 1190
1190 1191 self.ui.debug(_("common changesets up to ")
1191 1192 + " ".join(map(short, base.keys())) + "\n")
1192 1193
1193 1194 remain = dict.fromkeys(self.changelog.nodemap)
1194 1195
1195 1196 # prune everything remote has from the tree
1196 1197 del remain[nullid]
1197 1198 remove = base.keys()
1198 1199 while remove:
1199 1200 n = remove.pop(0)
1200 1201 if n in remain:
1201 1202 del remain[n]
1202 1203 for p in self.changelog.parents(n):
1203 1204 remove.append(p)
1204 1205
1205 1206 # find every node whose parents have been pruned
1206 1207 subset = []
1207 1208 # find every remote head that will get new children
1208 1209 updated_heads = {}
1209 1210 for n in remain:
1210 1211 p1, p2 = self.changelog.parents(n)
1211 1212 if p1 not in remain and p2 not in remain:
1212 1213 subset.append(n)
1213 1214 if heads:
1214 1215 if p1 in heads:
1215 1216 updated_heads[p1] = True
1216 1217 if p2 in heads:
1217 1218 updated_heads[p2] = True
1218 1219
1219 1220 # this is the set of all roots we have to push
1220 1221 if heads:
1221 1222 return subset, updated_heads.keys()
1222 1223 else:
1223 1224 return subset
1224 1225
1225 1226 def pull(self, remote, heads=None, force=False, lock=None):
1226 1227 mylock = False
1227 1228 if not lock:
1228 1229 lock = self.lock()
1229 1230 mylock = True
1230 1231
1231 1232 try:
1232 1233 fetch = self.findincoming(remote, force=force)
1233 1234 if fetch == [nullid]:
1234 1235 self.ui.status(_("requesting all changes\n"))
1235 1236
1236 1237 if not fetch:
1237 1238 self.ui.status(_("no changes found\n"))
1238 1239 return 0
1239 1240
1240 1241 if heads is None:
1241 1242 cg = remote.changegroup(fetch, 'pull')
1242 1243 else:
1243 1244 cg = remote.changegroupsubset(fetch, heads, 'pull')
1244 1245 return self.addchangegroup(cg, 'pull', remote.url())
1245 1246 finally:
1246 1247 if mylock:
1247 1248 lock.release()
1248 1249
1249 1250 def push(self, remote, force=False, revs=None):
1250 1251 # there are two ways to push to remote repo:
1251 1252 #
1252 1253 # addchangegroup assumes local user can lock remote
1253 1254 # repo (local filesystem, old ssh servers).
1254 1255 #
1255 1256 # unbundle assumes local user cannot lock remote repo (new ssh
1256 1257 # servers, http servers).
1257 1258
1258 1259 if remote.capable('unbundle'):
1259 1260 return self.push_unbundle(remote, force, revs)
1260 1261 return self.push_addchangegroup(remote, force, revs)
1261 1262
1262 1263 def prepush(self, remote, force, revs):
1263 1264 base = {}
1264 1265 remote_heads = remote.heads()
1265 1266 inc = self.findincoming(remote, base, remote_heads, force=force)
1266 1267 if not force and inc:
1267 1268 self.ui.warn(_("abort: unsynced remote changes!\n"))
1268 1269 self.ui.status(_("(did you forget to sync?"
1269 1270 " use push -f to force)\n"))
1270 1271 return None, 1
1271 1272
1272 1273 update, updated_heads = self.findoutgoing(remote, base, remote_heads)
1273 1274 if revs is not None:
1274 1275 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1275 1276 else:
1276 1277 bases, heads = update, self.changelog.heads()
1277 1278
1278 1279 if not bases:
1279 1280 self.ui.status(_("no changes found\n"))
1280 1281 return None, 1
1281 1282 elif not force:
1282 1283 # FIXME we don't properly detect creation of new heads
1283 1284 # in the push -r case, assume the user knows what he's doing
1284 1285 if not revs and len(remote_heads) < len(heads) \
1285 1286 and remote_heads != [nullid]:
1286 1287 self.ui.warn(_("abort: push creates new remote branches!\n"))
1287 1288 self.ui.status(_("(did you forget to merge?"
1288 1289 " use push -f to force)\n"))
1289 1290 return None, 1
1290 1291
1291 1292 if revs is None:
1292 1293 cg = self.changegroup(update, 'push')
1293 1294 else:
1294 1295 cg = self.changegroupsubset(update, revs, 'push')
1295 1296 return cg, remote_heads
1296 1297
1297 1298 def push_addchangegroup(self, remote, force, revs):
1298 1299 lock = remote.lock()
1299 1300
1300 1301 ret = self.prepush(remote, force, revs)
1301 1302 if ret[0] is not None:
1302 1303 cg, remote_heads = ret
1303 1304 return remote.addchangegroup(cg, 'push', self.url())
1304 1305 return ret[1]
1305 1306
1306 1307 def push_unbundle(self, remote, force, revs):
1307 1308 # local repo finds heads on server, finds out what revs it
1308 1309 # must push. once revs transferred, if server finds it has
1309 1310 # different heads (someone else won commit/push race), server
1310 1311 # aborts.
1311 1312
1312 1313 ret = self.prepush(remote, force, revs)
1313 1314 if ret[0] is not None:
1314 1315 cg, remote_heads = ret
1315 1316 if force: remote_heads = ['force']
1316 1317 return remote.unbundle(cg, remote_heads, 'push')
1317 1318 return ret[1]
1318 1319
1319 1320 def changegroupsubset(self, bases, heads, source):
1320 1321 """This function generates a changegroup consisting of all the nodes
1321 1322 that are descendents of any of the bases, and ancestors of any of
1322 1323 the heads.
1323 1324
1324 1325 It is fairly complex as determining which filenodes and which
1325 1326 manifest nodes need to be included for the changeset to be complete
1326 1327 is non-trivial.
1327 1328
1328 1329 Another wrinkle is doing the reverse, figuring out which changeset in
1329 1330 the changegroup a particular filenode or manifestnode belongs to."""
1330 1331
1331 1332 self.hook('preoutgoing', throw=True, source=source)
1332 1333
1333 1334 # Set up some initial variables
1334 1335 # Make it easy to refer to self.changelog
1335 1336 cl = self.changelog
1336 1337 # msng is short for missing - compute the list of changesets in this
1337 1338 # changegroup.
1338 1339 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1339 1340 # Some bases may turn out to be superfluous, and some heads may be
1340 1341 # too. nodesbetween will return the minimal set of bases and heads
1341 1342 # necessary to re-create the changegroup.
1342 1343
1343 1344 # Known heads are the list of heads that it is assumed the recipient
1344 1345 # of this changegroup will know about.
1345 1346 knownheads = {}
1346 1347 # We assume that all parents of bases are known heads.
1347 1348 for n in bases:
1348 1349 for p in cl.parents(n):
1349 1350 if p != nullid:
1350 1351 knownheads[p] = 1
1351 1352 knownheads = knownheads.keys()
1352 1353 if knownheads:
1353 1354 # Now that we know what heads are known, we can compute which
1354 1355 # changesets are known. The recipient must know about all
1355 1356 # changesets required to reach the known heads from the null
1356 1357 # changeset.
1357 1358 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1358 1359 junk = None
1359 1360 # Transform the list into an ersatz set.
1360 1361 has_cl_set = dict.fromkeys(has_cl_set)
1361 1362 else:
1362 1363 # If there were no known heads, the recipient cannot be assumed to
1363 1364 # know about any changesets.
1364 1365 has_cl_set = {}
1365 1366
1366 1367 # Make it easy to refer to self.manifest
1367 1368 mnfst = self.manifest
1368 1369 # We don't know which manifests are missing yet
1369 1370 msng_mnfst_set = {}
1370 1371 # Nor do we know which filenodes are missing.
1371 1372 msng_filenode_set = {}
1372 1373
1373 1374 junk = mnfst.index[mnfst.count() - 1] # Get around a bug in lazyindex
1374 1375 junk = None
1375 1376
1376 1377 # A changeset always belongs to itself, so the changenode lookup
1377 1378 # function for a changenode is identity.
1378 1379 def identity(x):
1379 1380 return x
1380 1381
1381 1382 # A function generating function. Sets up an environment for the
1382 1383 # inner function.
1383 1384 def cmp_by_rev_func(revlog):
1384 1385 # Compare two nodes by their revision number in the environment's
1385 1386 # revision history. Since the revision number both represents the
1386 1387 # most efficient order to read the nodes in, and represents a
1387 1388 # topological sorting of the nodes, this function is often useful.
1388 1389 def cmp_by_rev(a, b):
1389 1390 return cmp(revlog.rev(a), revlog.rev(b))
1390 1391 return cmp_by_rev
1391 1392
1392 1393 # If we determine that a particular file or manifest node must be a
1393 1394 # node that the recipient of the changegroup will already have, we can
1394 1395 # also assume the recipient will have all the parents. This function
1395 1396 # prunes them from the set of missing nodes.
1396 1397 def prune_parents(revlog, hasset, msngset):
1397 1398 haslst = hasset.keys()
1398 1399 haslst.sort(cmp_by_rev_func(revlog))
1399 1400 for node in haslst:
1400 1401 parentlst = [p for p in revlog.parents(node) if p != nullid]
1401 1402 while parentlst:
1402 1403 n = parentlst.pop()
1403 1404 if n not in hasset:
1404 1405 hasset[n] = 1
1405 1406 p = [p for p in revlog.parents(n) if p != nullid]
1406 1407 parentlst.extend(p)
1407 1408 for n in hasset:
1408 1409 msngset.pop(n, None)
1409 1410
1410 1411 # This is a function generating function used to set up an environment
1411 1412 # for the inner function to execute in.
1412 1413 def manifest_and_file_collector(changedfileset):
1413 1414 # This is an information gathering function that gathers
1414 1415 # information from each changeset node that goes out as part of
1415 1416 # the changegroup. The information gathered is a list of which
1416 1417 # manifest nodes are potentially required (the recipient may
1417 1418 # already have them) and total list of all files which were
1418 1419 # changed in any changeset in the changegroup.
1419 1420 #
1420 1421 # We also remember the first changenode we saw any manifest
1421 1422 # referenced by so we can later determine which changenode 'owns'
1422 1423 # the manifest.
1423 1424 def collect_manifests_and_files(clnode):
1424 1425 c = cl.read(clnode)
1425 1426 for f in c[3]:
1426 1427 # This is to make sure we only have one instance of each
1427 1428 # filename string for each filename.
1428 1429 changedfileset.setdefault(f, f)
1429 1430 msng_mnfst_set.setdefault(c[0], clnode)
1430 1431 return collect_manifests_and_files
1431 1432
1432 1433 # Figure out which manifest nodes (of the ones we think might be part
1433 1434 # of the changegroup) the recipient must know about and remove them
1434 1435 # from the changegroup.
1435 1436 def prune_manifests():
1436 1437 has_mnfst_set = {}
1437 1438 for n in msng_mnfst_set:
1438 1439 # If a 'missing' manifest thinks it belongs to a changenode
1439 1440 # the recipient is assumed to have, obviously the recipient
1440 1441 # must have that manifest.
1441 1442 linknode = cl.node(mnfst.linkrev(n))
1442 1443 if linknode in has_cl_set:
1443 1444 has_mnfst_set[n] = 1
1444 1445 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1445 1446
1446 1447 # Use the information collected in collect_manifests_and_files to say
1447 1448 # which changenode any manifestnode belongs to.
1448 1449 def lookup_manifest_link(mnfstnode):
1449 1450 return msng_mnfst_set[mnfstnode]
1450 1451
1451 1452 # A function generating function that sets up the initial environment
1452 1453 # the inner function.
1453 1454 def filenode_collector(changedfiles):
1454 1455 next_rev = [0]
1455 1456 # This gathers information from each manifestnode included in the
1456 1457 # changegroup about which filenodes the manifest node references
1457 1458 # so we can include those in the changegroup too.
1458 1459 #
1459 1460 # It also remembers which changenode each filenode belongs to. It
1460 1461 # does this by assuming the a filenode belongs to the changenode
1461 1462 # the first manifest that references it belongs to.
1462 1463 def collect_msng_filenodes(mnfstnode):
1463 1464 r = mnfst.rev(mnfstnode)
1464 1465 if r == next_rev[0]:
1465 1466 # If the last rev we looked at was the one just previous,
1466 1467 # we only need to see a diff.
1467 1468 delta = mdiff.patchtext(mnfst.delta(mnfstnode))
1468 1469 # For each line in the delta
1469 1470 for dline in delta.splitlines():
1470 1471 # get the filename and filenode for that line
1471 1472 f, fnode = dline.split('\0')
1472 1473 fnode = bin(fnode[:40])
1473 1474 f = changedfiles.get(f, None)
1474 1475 # And if the file is in the list of files we care
1475 1476 # about.
1476 1477 if f is not None:
1477 1478 # Get the changenode this manifest belongs to
1478 1479 clnode = msng_mnfst_set[mnfstnode]
1479 1480 # Create the set of filenodes for the file if
1480 1481 # there isn't one already.
1481 1482 ndset = msng_filenode_set.setdefault(f, {})
1482 1483 # And set the filenode's changelog node to the
1483 1484 # manifest's if it hasn't been set already.
1484 1485 ndset.setdefault(fnode, clnode)
1485 1486 else:
1486 1487 # Otherwise we need a full manifest.
1487 1488 m = mnfst.read(mnfstnode)
1488 1489 # For every file in we care about.
1489 1490 for f in changedfiles:
1490 1491 fnode = m.get(f, None)
1491 1492 # If it's in the manifest
1492 1493 if fnode is not None:
1493 1494 # See comments above.
1494 1495 clnode = msng_mnfst_set[mnfstnode]
1495 1496 ndset = msng_filenode_set.setdefault(f, {})
1496 1497 ndset.setdefault(fnode, clnode)
1497 1498 # Remember the revision we hope to see next.
1498 1499 next_rev[0] = r + 1
1499 1500 return collect_msng_filenodes
1500 1501
1501 1502 # We have a list of filenodes we think we need for a file, lets remove
1502 1503 # all those we now the recipient must have.
1503 1504 def prune_filenodes(f, filerevlog):
1504 1505 msngset = msng_filenode_set[f]
1505 1506 hasset = {}
1506 1507 # If a 'missing' filenode thinks it belongs to a changenode we
1507 1508 # assume the recipient must have, then the recipient must have
1508 1509 # that filenode.
1509 1510 for n in msngset:
1510 1511 clnode = cl.node(filerevlog.linkrev(n))
1511 1512 if clnode in has_cl_set:
1512 1513 hasset[n] = 1
1513 1514 prune_parents(filerevlog, hasset, msngset)
1514 1515
1515 1516 # A function generator function that sets up the a context for the
1516 1517 # inner function.
1517 1518 def lookup_filenode_link_func(fname):
1518 1519 msngset = msng_filenode_set[fname]
1519 1520 # Lookup the changenode the filenode belongs to.
1520 1521 def lookup_filenode_link(fnode):
1521 1522 return msngset[fnode]
1522 1523 return lookup_filenode_link
1523 1524
1524 1525 # Now that we have all theses utility functions to help out and
1525 1526 # logically divide up the task, generate the group.
1526 1527 def gengroup():
1527 1528 # The set of changed files starts empty.
1528 1529 changedfiles = {}
1529 1530 # Create a changenode group generator that will call our functions
1530 1531 # back to lookup the owning changenode and collect information.
1531 1532 group = cl.group(msng_cl_lst, identity,
1532 1533 manifest_and_file_collector(changedfiles))
1533 1534 for chnk in group:
1534 1535 yield chnk
1535 1536
1536 1537 # The list of manifests has been collected by the generator
1537 1538 # calling our functions back.
1538 1539 prune_manifests()
1539 1540 msng_mnfst_lst = msng_mnfst_set.keys()
1540 1541 # Sort the manifestnodes by revision number.
1541 1542 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1542 1543 # Create a generator for the manifestnodes that calls our lookup
1543 1544 # and data collection functions back.
1544 1545 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1545 1546 filenode_collector(changedfiles))
1546 1547 for chnk in group:
1547 1548 yield chnk
1548 1549
1549 1550 # These are no longer needed, dereference and toss the memory for
1550 1551 # them.
1551 1552 msng_mnfst_lst = None
1552 1553 msng_mnfst_set.clear()
1553 1554
1554 1555 changedfiles = changedfiles.keys()
1555 1556 changedfiles.sort()
1556 1557 # Go through all our files in order sorted by name.
1557 1558 for fname in changedfiles:
1558 1559 filerevlog = self.file(fname)
1559 1560 # Toss out the filenodes that the recipient isn't really
1560 1561 # missing.
1561 1562 if msng_filenode_set.has_key(fname):
1562 1563 prune_filenodes(fname, filerevlog)
1563 1564 msng_filenode_lst = msng_filenode_set[fname].keys()
1564 1565 else:
1565 1566 msng_filenode_lst = []
1566 1567 # If any filenodes are left, generate the group for them,
1567 1568 # otherwise don't bother.
1568 1569 if len(msng_filenode_lst) > 0:
1569 1570 yield changegroup.genchunk(fname)
1570 1571 # Sort the filenodes by their revision #
1571 1572 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1572 1573 # Create a group generator and only pass in a changenode
1573 1574 # lookup function as we need to collect no information
1574 1575 # from filenodes.
1575 1576 group = filerevlog.group(msng_filenode_lst,
1576 1577 lookup_filenode_link_func(fname))
1577 1578 for chnk in group:
1578 1579 yield chnk
1579 1580 if msng_filenode_set.has_key(fname):
1580 1581 # Don't need this anymore, toss it to free memory.
1581 1582 del msng_filenode_set[fname]
1582 1583 # Signal that no more groups are left.
1583 1584 yield changegroup.closechunk()
1584 1585
1585 1586 if msng_cl_lst:
1586 1587 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1587 1588
1588 1589 return util.chunkbuffer(gengroup())
1589 1590
1590 1591 def changegroup(self, basenodes, source):
1591 1592 """Generate a changegroup of all nodes that we have that a recipient
1592 1593 doesn't.
1593 1594
1594 1595 This is much easier than the previous function as we can assume that
1595 1596 the recipient has any changenode we aren't sending them."""
1596 1597
1597 1598 self.hook('preoutgoing', throw=True, source=source)
1598 1599
1599 1600 cl = self.changelog
1600 1601 nodes = cl.nodesbetween(basenodes, None)[0]
1601 1602 revset = dict.fromkeys([cl.rev(n) for n in nodes])
1602 1603
1603 1604 def identity(x):
1604 1605 return x
1605 1606
1606 1607 def gennodelst(revlog):
1607 1608 for r in xrange(0, revlog.count()):
1608 1609 n = revlog.node(r)
1609 1610 if revlog.linkrev(n) in revset:
1610 1611 yield n
1611 1612
1612 1613 def changed_file_collector(changedfileset):
1613 1614 def collect_changed_files(clnode):
1614 1615 c = cl.read(clnode)
1615 1616 for fname in c[3]:
1616 1617 changedfileset[fname] = 1
1617 1618 return collect_changed_files
1618 1619
1619 1620 def lookuprevlink_func(revlog):
1620 1621 def lookuprevlink(n):
1621 1622 return cl.node(revlog.linkrev(n))
1622 1623 return lookuprevlink
1623 1624
1624 1625 def gengroup():
1625 1626 # construct a list of all changed files
1626 1627 changedfiles = {}
1627 1628
1628 1629 for chnk in cl.group(nodes, identity,
1629 1630 changed_file_collector(changedfiles)):
1630 1631 yield chnk
1631 1632 changedfiles = changedfiles.keys()
1632 1633 changedfiles.sort()
1633 1634
1634 1635 mnfst = self.manifest
1635 1636 nodeiter = gennodelst(mnfst)
1636 1637 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1637 1638 yield chnk
1638 1639
1639 1640 for fname in changedfiles:
1640 1641 filerevlog = self.file(fname)
1641 1642 nodeiter = gennodelst(filerevlog)
1642 1643 nodeiter = list(nodeiter)
1643 1644 if nodeiter:
1644 1645 yield changegroup.genchunk(fname)
1645 1646 lookup = lookuprevlink_func(filerevlog)
1646 1647 for chnk in filerevlog.group(nodeiter, lookup):
1647 1648 yield chnk
1648 1649
1649 1650 yield changegroup.closechunk()
1650 1651
1651 1652 if nodes:
1652 1653 self.hook('outgoing', node=hex(nodes[0]), source=source)
1653 1654
1654 1655 return util.chunkbuffer(gengroup())
1655 1656
1656 1657 def addchangegroup(self, source, srctype, url):
1657 1658 """add changegroup to repo.
1658 1659 returns number of heads modified or added + 1."""
1659 1660
1660 1661 def csmap(x):
1661 1662 self.ui.debug(_("add changeset %s\n") % short(x))
1662 1663 return cl.count()
1663 1664
1664 1665 def revmap(x):
1665 1666 return cl.rev(x)
1666 1667
1667 1668 if not source:
1668 1669 return 0
1669 1670
1670 1671 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1671 1672
1672 1673 changesets = files = revisions = 0
1673 1674
1674 1675 tr = self.transaction()
1675 1676
1676 1677 # write changelog data to temp files so concurrent readers will not see
1677 1678 # inconsistent view
1678 1679 cl = None
1679 1680 try:
1680 1681 cl = appendfile.appendchangelog(self.opener, self.changelog.version)
1681 1682
1682 1683 oldheads = len(cl.heads())
1683 1684
1684 1685 # pull off the changeset group
1685 1686 self.ui.status(_("adding changesets\n"))
1686 1687 cor = cl.count() - 1
1687 1688 chunkiter = changegroup.chunkiter(source)
1688 1689 if cl.addgroup(chunkiter, csmap, tr, 1) is None:
1689 1690 raise util.Abort(_("received changelog group is empty"))
1690 1691 cnr = cl.count() - 1
1691 1692 changesets = cnr - cor
1692 1693
1693 1694 # pull off the manifest group
1694 1695 self.ui.status(_("adding manifests\n"))
1695 1696 chunkiter = changegroup.chunkiter(source)
1696 1697 # no need to check for empty manifest group here:
1697 1698 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1698 1699 # no new manifest will be created and the manifest group will
1699 1700 # be empty during the pull
1700 1701 self.manifest.addgroup(chunkiter, revmap, tr)
1701 1702
1702 1703 # process the files
1703 1704 self.ui.status(_("adding file changes\n"))
1704 1705 while 1:
1705 1706 f = changegroup.getchunk(source)
1706 1707 if not f:
1707 1708 break
1708 1709 self.ui.debug(_("adding %s revisions\n") % f)
1709 1710 fl = self.file(f)
1710 1711 o = fl.count()
1711 1712 chunkiter = changegroup.chunkiter(source)
1712 1713 if fl.addgroup(chunkiter, revmap, tr) is None:
1713 1714 raise util.Abort(_("received file revlog group is empty"))
1714 1715 revisions += fl.count() - o
1715 1716 files += 1
1716 1717
1717 1718 cl.writedata()
1718 1719 finally:
1719 1720 if cl:
1720 1721 cl.cleanup()
1721 1722
1722 1723 # make changelog see real files again
1723 1724 self.changelog = changelog.changelog(self.opener, self.changelog.version)
1724 1725 self.changelog.checkinlinesize(tr)
1725 1726
1726 1727 newheads = len(self.changelog.heads())
1727 1728 heads = ""
1728 1729 if oldheads and newheads != oldheads:
1729 1730 heads = _(" (%+d heads)") % (newheads - oldheads)
1730 1731
1731 1732 self.ui.status(_("added %d changesets"
1732 1733 " with %d changes to %d files%s\n")
1733 1734 % (changesets, revisions, files, heads))
1734 1735
1735 1736 if changesets > 0:
1736 1737 self.hook('pretxnchangegroup', throw=True,
1737 1738 node=hex(self.changelog.node(cor+1)), source=srctype,
1738 1739 url=url)
1739 1740
1740 1741 tr.close()
1741 1742
1742 1743 if changesets > 0:
1743 1744 self.hook("changegroup", node=hex(self.changelog.node(cor+1)),
1744 1745 source=srctype, url=url)
1745 1746
1746 1747 for i in range(cor + 1, cnr + 1):
1747 1748 self.hook("incoming", node=hex(self.changelog.node(i)),
1748 1749 source=srctype, url=url)
1749 1750
1750 1751 return newheads - oldheads + 1
1751 1752
1752 1753
1753 1754 def stream_in(self, remote):
1754 1755 fp = remote.stream_out()
1755 1756 resp = int(fp.readline())
1756 1757 if resp != 0:
1757 1758 raise util.Abort(_('operation forbidden by server'))
1758 1759 self.ui.status(_('streaming all changes\n'))
1759 1760 total_files, total_bytes = map(int, fp.readline().split(' ', 1))
1760 1761 self.ui.status(_('%d files to transfer, %s of data\n') %
1761 1762 (total_files, util.bytecount(total_bytes)))
1762 1763 start = time.time()
1763 1764 for i in xrange(total_files):
1764 1765 name, size = fp.readline().split('\0', 1)
1765 1766 size = int(size)
1766 1767 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1767 1768 ofp = self.opener(name, 'w')
1768 1769 for chunk in util.filechunkiter(fp, limit=size):
1769 1770 ofp.write(chunk)
1770 1771 ofp.close()
1771 1772 elapsed = time.time() - start
1772 1773 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1773 1774 (util.bytecount(total_bytes), elapsed,
1774 1775 util.bytecount(total_bytes / elapsed)))
1775 1776 self.reload()
1776 1777 return len(self.heads()) + 1
1777 1778
1778 1779 def clone(self, remote, heads=[], stream=False):
1779 1780 '''clone remote repository.
1780 1781
1781 1782 keyword arguments:
1782 1783 heads: list of revs to clone (forces use of pull)
1783 1784 stream: use streaming clone if possible'''
1784 1785
1785 1786 # now, all clients that can request uncompressed clones can
1786 1787 # read repo formats supported by all servers that can serve
1787 1788 # them.
1788 1789
1789 1790 # if revlog format changes, client will have to check version
1790 1791 # and format flags on "stream" capability, and use
1791 1792 # uncompressed only if compatible.
1792 1793
1793 1794 if stream and not heads and remote.capable('stream'):
1794 1795 return self.stream_in(remote)
1795 1796 return self.pull(remote, heads)
1796 1797
1797 1798 # used to avoid circular references so destructors work
1798 1799 def aftertrans(base):
1799 1800 p = base
1800 1801 def a():
1801 1802 util.rename(os.path.join(p, "journal"), os.path.join(p, "undo"))
1802 1803 util.rename(os.path.join(p, "journal.dirstate"),
1803 1804 os.path.join(p, "undo.dirstate"))
1804 1805 return a
1805 1806
1806 1807 def instance(ui, path, create):
1807 1808 return localrepository(ui, util.drop_scheme('file', path), create)
1808 1809
1809 1810 def islocal(path):
1810 1811 return True
General Comments 0
You need to be logged in to leave comments. Login now