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