##// END OF EJS Templates
More tweaking to rawcommit for repo conversion...
mpm@selenic.com -
r314:3402cb9a default
parent child Browse files
Show More
@@ -1,1302 +1,1306 b''
1 1 # hg.py - repository classes 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 sys, struct, os
9 9 from revlog import *
10 10 from demandload import *
11 11 demandload(globals(), "re lock urllib urllib2 transaction time socket")
12 12 demandload(globals(), "tempfile byterange difflib")
13 13
14 14 def is_exec(f):
15 15 return (os.stat(f).st_mode & 0100 != 0)
16 16
17 17 def set_exec(f, mode):
18 18 s = os.stat(f).st_mode
19 19 if (s & 0100 != 0) == mode:
20 20 return
21 21 if mode:
22 22 # Turn on +x for every +r bit when making a file executable
23 23 # and obey umask.
24 24 umask = os.umask(0)
25 25 os.umask(umask)
26 26 os.chmod(f, s | (s & 0444) >> 2 & ~umask)
27 27 else:
28 28 os.chmod(f, s & 0666)
29 29
30 30 class filelog(revlog):
31 31 def __init__(self, opener, path):
32 32 revlog.__init__(self, opener,
33 33 os.path.join("data", path + ".i"),
34 34 os.path.join("data", path + ".d"))
35 35
36 36 def read(self, node):
37 37 return self.revision(node)
38 38 def add(self, text, transaction, link, p1=None, p2=None):
39 39 return self.addrevision(text, transaction, link, p1, p2)
40 40
41 41 def annotate(self, node):
42 42
43 43 def decorate(text, rev):
44 44 return [(rev, l) for l in text.splitlines(1)]
45 45
46 46 def strip(annotation):
47 47 return [e[1] for e in annotation]
48 48
49 49 def pair(parent, child):
50 50 new = []
51 51 sm = difflib.SequenceMatcher(None, strip(parent), strip(child))
52 52 for o, m, n, s, t in sm.get_opcodes():
53 53 if o == 'equal':
54 54 new += parent[m:n]
55 55 else:
56 56 new += child[s:t]
57 57 return new
58 58
59 59 # find all ancestors
60 60 needed = {node:1}
61 61 visit = [node]
62 62 while visit:
63 63 n = visit.pop(0)
64 64 for p in self.parents(n):
65 65 if p not in needed:
66 66 needed[p] = 1
67 67 visit.append(p)
68 68 else:
69 69 # count how many times we'll use this
70 70 needed[p] += 1
71 71
72 72 # sort by revision which is a topological order
73 73 visit = needed.keys()
74 74 visit = [ (self.rev(n), n) for n in visit ]
75 75 visit.sort()
76 76 visit = [ p[1] for p in visit ]
77 77 hist = {}
78 78
79 79 for n in visit:
80 80 curr = decorate(self.read(n), self.linkrev(n))
81 81 for p in self.parents(n):
82 82 if p != nullid:
83 83 curr = pair(hist[p], curr)
84 84 # trim the history of unneeded revs
85 85 needed[p] -= 1
86 86 if not needed[p]:
87 87 del hist[p]
88 88 hist[n] = curr
89 89
90 90 return hist[n]
91 91
92 92 class manifest(revlog):
93 93 def __init__(self, opener):
94 94 self.mapcache = None
95 95 self.listcache = None
96 96 self.addlist = None
97 97 revlog.__init__(self, opener, "00manifest.i", "00manifest.d")
98 98
99 99 def read(self, node):
100 100 if node == nullid: return {} # don't upset local cache
101 101 if self.mapcache and self.mapcache[0] == node:
102 102 return self.mapcache[1].copy()
103 103 text = self.revision(node)
104 104 map = {}
105 105 flag = {}
106 106 self.listcache = (text, text.splitlines(1))
107 107 for l in self.listcache[1]:
108 108 (f, n) = l.split('\0')
109 109 map[f] = bin(n[:40])
110 110 flag[f] = (n[40:-1] == "x")
111 111 self.mapcache = (node, map, flag)
112 112 return map
113 113
114 114 def readflags(self, node):
115 115 if node == nullid: return {} # don't upset local cache
116 116 if self.mapcache or self.mapcache[0] != node:
117 117 self.read(node)
118 118 return self.mapcache[2]
119 119
120 120 def diff(self, a, b):
121 121 # this is sneaky, as we're not actually using a and b
122 122 if self.listcache and self.addlist and self.listcache[0] == a:
123 123 d = mdiff.diff(self.listcache[1], self.addlist, 1)
124 124 if mdiff.patch(a, d) != b:
125 125 sys.stderr.write("*** sortdiff failed, falling back ***\n")
126 126 return mdiff.textdiff(a, b)
127 127 return d
128 128 else:
129 129 return mdiff.textdiff(a, b)
130 130
131 131 def add(self, map, flags, transaction, link, p1=None, p2=None):
132 132 files = map.keys()
133 133 files.sort()
134 134
135 135 self.addlist = ["%s\000%s%s\n" %
136 136 (f, hex(map[f]), flags[f] and "x" or '')
137 137 for f in files]
138 138 text = "".join(self.addlist)
139 139
140 140 n = self.addrevision(text, transaction, link, p1, p2)
141 141 self.mapcache = (n, map, flags)
142 142 self.listcache = (text, self.addlist)
143 143 self.addlist = None
144 144
145 145 return n
146 146
147 147 class changelog(revlog):
148 148 def __init__(self, opener):
149 149 revlog.__init__(self, opener, "00changelog.i", "00changelog.d")
150 150
151 151 def extract(self, text):
152 152 if not text:
153 153 return (nullid, "", "0", [], "")
154 154 last = text.index("\n\n")
155 155 desc = text[last + 2:]
156 156 l = text[:last].splitlines()
157 157 manifest = bin(l[0])
158 158 user = l[1]
159 159 date = l[2]
160 160 files = l[3:]
161 161 return (manifest, user, date, files, desc)
162 162
163 163 def read(self, node):
164 164 return self.extract(self.revision(node))
165 165
166 166 def add(self, manifest, list, desc, transaction, p1=None, p2=None,
167 167 user=None, date=None):
168 168 user = (user or
169 169 os.environ.get("HGUSER") or
170 170 os.environ.get("EMAIL") or
171 171 os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn())
172 172 date = date or "%d %d" % (time.time(), time.timezone)
173 173 list.sort()
174 174 l = [hex(manifest), user, date] + list + ["", desc]
175 175 text = "\n".join(l)
176 176 return self.addrevision(text, transaction, self.count(), p1, p2)
177 177
178 178 class dirstate:
179 179 def __init__(self, opener, ui, root):
180 180 self.opener = opener
181 181 self.root = root
182 182 self.dirty = 0
183 183 self.ui = ui
184 184 self.map = None
185 185 self.pl = None
186 186
187 187 def __del__(self):
188 188 if self.dirty:
189 189 self.write()
190 190
191 191 def __getitem__(self, key):
192 192 try:
193 193 return self.map[key]
194 194 except TypeError:
195 195 self.read()
196 196 return self[key]
197 197
198 198 def __contains__(self, key):
199 199 if not self.map: self.read()
200 200 return key in self.map
201 201
202 202 def parents(self):
203 203 if not self.pl:
204 204 self.read()
205 205 return self.pl
206 206
207 207 def setparents(self, p1, p2 = nullid):
208 208 self.dirty = 1
209 209 self.pl = p1, p2
210 210
211 211 def state(self, key):
212 212 try:
213 213 return self[key][0]
214 214 except KeyError:
215 215 return "?"
216 216
217 217 def read(self):
218 218 if self.map is not None: return self.map
219 219
220 220 self.map = {}
221 221 self.pl = [nullid, nullid]
222 222 try:
223 223 st = self.opener("dirstate").read()
224 224 if not st: return
225 225 except: return
226 226
227 227 self.pl = [st[:20], st[20: 40]]
228 228
229 229 pos = 40
230 230 while pos < len(st):
231 231 e = struct.unpack(">cllll", st[pos:pos+17])
232 232 l = e[4]
233 233 pos += 17
234 234 f = st[pos:pos + l]
235 235 self.map[f] = e[:4]
236 236 pos += l
237 237
238 238 def update(self, files, state):
239 239 ''' current states:
240 240 n normal
241 241 m needs merging
242 242 r marked for removal
243 243 a marked for addition'''
244 244
245 245 if not files: return
246 246 self.read()
247 247 self.dirty = 1
248 248 for f in files:
249 249 if state == "r":
250 250 self.map[f] = ('r', 0, 0, 0)
251 251 else:
252 252 s = os.stat(os.path.join(self.root, f))
253 253 self.map[f] = (state, s.st_mode, s.st_size, s.st_mtime)
254 254
255 255 def forget(self, files):
256 256 if not files: return
257 257 self.read()
258 258 self.dirty = 1
259 259 for f in files:
260 260 try:
261 261 del self.map[f]
262 262 except KeyError:
263 263 self.ui.warn("not in dirstate: %s!\n" % f)
264 264 pass
265 265
266 266 def clear(self):
267 267 self.map = {}
268 268 self.dirty = 1
269 269
270 270 def write(self):
271 271 st = self.opener("dirstate", "w")
272 272 st.write("".join(self.pl))
273 273 for f, e in self.map.items():
274 274 e = struct.pack(">cllll", e[0], e[1], e[2], e[3], len(f))
275 275 st.write(e + f)
276 276 self.dirty = 0
277 277
278 278 def copy(self):
279 279 self.read()
280 280 return self.map.copy()
281 281
282 282 # used to avoid circular references so destructors work
283 283 def opener(base):
284 284 p = base
285 285 def o(path, mode="r"):
286 286 if p[:7] == "http://":
287 287 f = os.path.join(p, urllib.quote(path))
288 288 return httprangereader(f)
289 289
290 290 f = os.path.join(p, path)
291 291
292 292 mode += "b" # for that other OS
293 293
294 294 if mode[0] != "r":
295 295 try:
296 296 s = os.stat(f)
297 297 except OSError:
298 298 d = os.path.dirname(f)
299 299 if not os.path.isdir(d):
300 300 os.makedirs(d)
301 301 else:
302 302 if s.st_nlink > 1:
303 303 file(f + ".tmp", "w").write(file(f).read())
304 304 os.rename(f+".tmp", f)
305 305
306 306 return file(f, mode)
307 307
308 308 return o
309 309
310 310 class localrepository:
311 311 def __init__(self, ui, path=None, create=0):
312 312 self.remote = 0
313 313 if path and path[:7] == "http://":
314 314 self.remote = 1
315 315 self.path = path
316 316 else:
317 317 if not path:
318 318 p = os.getcwd()
319 319 while not os.path.isdir(os.path.join(p, ".hg")):
320 320 p = os.path.dirname(p)
321 321 if p == "/": raise "No repo found"
322 322 path = p
323 323 self.path = os.path.join(path, ".hg")
324 324
325 325 self.root = path
326 326 self.ui = ui
327 327
328 328 if create:
329 329 os.mkdir(self.path)
330 330 os.mkdir(self.join("data"))
331 331
332 332 self.opener = opener(self.path)
333 333 self.wopener = opener(self.root)
334 334 self.manifest = manifest(self.opener)
335 335 self.changelog = changelog(self.opener)
336 336 self.ignorelist = None
337 337 self.tags = None
338 338
339 339 if not self.remote:
340 340 self.dirstate = dirstate(self.opener, ui, self.root)
341 341
342 342 def ignore(self, f):
343 343 if self.ignorelist is None:
344 344 self.ignorelist = []
345 345 try:
346 346 l = self.wfile(".hgignore")
347 347 for pat in l:
348 348 if pat != "\n":
349 349 self.ignorelist.append(re.compile(pat[:-1]))
350 350 except IOError: pass
351 351 for pat in self.ignorelist:
352 352 if pat.search(f): return True
353 353 return False
354 354
355 355 def lookup(self, key):
356 356 if self.tags is None:
357 357 self.tags = {}
358 358 try:
359 359 # read each head of the tags file, ending with the tip
360 360 # and add each tag found to the map, with "newer" ones
361 361 # taking precedence
362 362 fl = self.file(".hgtags")
363 363 h = fl.heads()
364 364 h.reverse()
365 365 for r in h:
366 366 for l in fl.revision(r).splitlines():
367 367 if l:
368 368 n, k = l.split(" ")
369 369 self.tags[k] = bin(n)
370 370 except KeyError: pass
371 371 self.tags['tip'] = self.changelog.tip()
372 372 try:
373 373 return self.tags[key]
374 374 except KeyError:
375 375 return self.changelog.lookup(key)
376 376
377 377 def join(self, f):
378 378 return os.path.join(self.path, f)
379 379
380 380 def wjoin(self, f):
381 381 return os.path.join(self.root, f)
382 382
383 383 def file(self, f):
384 384 if f[0] == '/': f = f[1:]
385 385 return filelog(self.opener, f)
386 386
387 387 def wfile(self, f, mode='r'):
388 388 return self.wopener(f, mode)
389 389
390 390 def transaction(self):
391 391 # save dirstate for undo
392 392 try:
393 393 ds = self.opener("dirstate").read()
394 394 except IOError:
395 395 ds = ""
396 396 self.opener("undo.dirstate", "w").write(ds)
397 397
398 398 return transaction.transaction(self.opener, self.join("journal"),
399 399 self.join("undo"))
400 400
401 401 def recover(self):
402 402 lock = self.lock()
403 403 if os.path.exists(self.join("recover")):
404 404 self.ui.status("attempting to rollback interrupted transaction\n")
405 405 return transaction.rollback(self.opener, self.join("recover"))
406 406 else:
407 407 self.ui.warn("no interrupted transaction available\n")
408 408
409 409 def undo(self):
410 410 lock = self.lock()
411 411 if os.path.exists(self.join("undo")):
412 412 self.ui.status("attempting to rollback last transaction\n")
413 413 transaction.rollback(self.opener, self.join("undo"))
414 414 self.dirstate = None
415 415 os.rename(self.join("undo.dirstate"), self.join("dirstate"))
416 416 self.dirstate = dirstate(self.opener, self.ui, self.root)
417 417 else:
418 418 self.ui.warn("no undo information available\n")
419 419
420 420 def lock(self, wait = 1):
421 421 try:
422 422 return lock.lock(self.join("lock"), 0)
423 423 except lock.LockHeld, inst:
424 424 if wait:
425 425 self.ui.warn("waiting for lock held by %s\n" % inst.args[0])
426 426 return lock.lock(self.join("lock"), wait)
427 427 raise inst
428 428
429 429 def rawcommit(self, files, text, user, date, p1=None, p2=None):
430 430 p1 = p1 or self.dirstate.parents()[0] or nullid
431 431 p2 = p2 or self.dirstate.parents()[1] or nullid
432 432 c1 = self.changelog.read(p1)
433 433 c2 = self.changelog.read(p2)
434 434 m1 = self.manifest.read(c1[0])
435 435 mf1 = self.manifest.readflags(c1[0])
436 436 m2 = self.manifest.read(c2[0])
437 437
438 438 tr = self.transaction()
439 439 mm = m1.copy()
440 440 mfm = mf1.copy()
441 441 linkrev = self.changelog.count()
442 self.dirstate.setparents(p1, p2)
442 443 for f in files:
443 444 try:
444 445 t = self.wfile(f).read()
445 446 tm = is_exec(self.wjoin(f))
446 447 r = self.file(f)
447 448 mfm[f] = tm
448 449 mm[f] = r.add(t, tr, linkrev,
449 450 m1.get(f, nullid), m2.get(f, nullid))
451 self.dirstate.update([f], "n")
450 452 except IOError:
451 del mm[f]
452 del mfm[f]
453 try:
454 del mm[f]
455 del mfm[f]
456 self.dirstate.forget([f])
457 except:
458 # deleted from p2?
459 pass
453 460
454 461 mnode = self.manifest.add(mm, mfm, tr, linkrev, c1[0], c2[0])
455 462 n = self.changelog.add(mnode, files, text, tr, p1, p2, user, date)
456 463 tr.close()
457 self.dirstate.setparents(p1, p2)
458 self.dirstate.clear()
459 self.dirstate.update(files, "n")
460 464
461 465 def commit(self, files = None, text = ""):
462 466 commit = []
463 467 remove = []
464 468 if files:
465 469 for f in files:
466 470 s = self.dirstate.state(f)
467 471 if s in 'nmai':
468 472 commit.append(f)
469 473 elif s == 'r':
470 474 remove.append(f)
471 475 else:
472 476 self.ui.warn("%s not tracked!\n" % f)
473 477 else:
474 478 (c, a, d, u) = self.diffdir(self.root)
475 479 commit = c + a
476 480 remove = d
477 481
478 482 if not commit and not remove:
479 483 self.ui.status("nothing changed\n")
480 484 return
481 485
482 486 p1, p2 = self.dirstate.parents()
483 487 c1 = self.changelog.read(p1)
484 488 c2 = self.changelog.read(p2)
485 489 m1 = self.manifest.read(c1[0])
486 490 mf1 = self.manifest.readflags(c1[0])
487 491 m2 = self.manifest.read(c2[0])
488 492 lock = self.lock()
489 493 tr = self.transaction()
490 494
491 495 # check in files
492 496 new = {}
493 497 linkrev = self.changelog.count()
494 498 commit.sort()
495 499 for f in commit:
496 500 self.ui.note(f + "\n")
497 501 try:
498 502 fp = self.wjoin(f)
499 503 mf1[f] = is_exec(fp)
500 504 t = file(fp).read()
501 505 except IOError:
502 506 self.warn("trouble committing %s!\n" % f)
503 507 raise
504 508
505 509 r = self.file(f)
506 510 fp1 = m1.get(f, nullid)
507 511 fp2 = m2.get(f, nullid)
508 512 new[f] = r.add(t, tr, linkrev, fp1, fp2)
509 513
510 514 # update manifest
511 515 m1.update(new)
512 516 for f in remove: del m1[f]
513 517 mn = self.manifest.add(m1, mf1, tr, linkrev, c1[0], c2[0])
514 518
515 519 # add changeset
516 520 new = new.keys()
517 521 new.sort()
518 522
519 523 if not text:
520 524 edittext = "\n" + "HG: manifest hash %s\n" % hex(mn)
521 525 edittext += "".join(["HG: changed %s\n" % f for f in new])
522 526 edittext += "".join(["HG: removed %s\n" % f for f in remove])
523 527 edittext = self.ui.edit(edittext)
524 528 if not edittext.rstrip():
525 529 return 1
526 530 text = edittext
527 531
528 532 n = self.changelog.add(mn, new, text, tr, p1, p2)
529 533 tr.close()
530 534
531 535 self.dirstate.setparents(n)
532 536 self.dirstate.update(new, "n")
533 537 self.dirstate.forget(remove)
534 538
535 539 def diffdir(self, path, changeset = None):
536 540 changed = []
537 541 added = []
538 542 unknown = []
539 543 mf = {}
540 544
541 545 if changeset:
542 546 change = self.changelog.read(changeset)
543 547 mf = self.manifest.read(change[0])
544 548 dc = dict.fromkeys(mf)
545 549 else:
546 550 changeset = self.dirstate.parents()[0]
547 551 change = self.changelog.read(changeset)
548 552 mf = self.manifest.read(change[0])
549 553 dc = self.dirstate.copy()
550 554
551 555 def fcmp(fn):
552 556 t1 = self.wfile(fn).read()
553 557 t2 = self.file(fn).revision(mf[fn])
554 558 return cmp(t1, t2)
555 559
556 560 for dir, subdirs, files in os.walk(path):
557 561 d = dir[len(self.root)+1:]
558 562 if ".hg" in subdirs: subdirs.remove(".hg")
559 563
560 564 for f in files:
561 565 fn = os.path.join(d, f)
562 566 try: s = os.stat(os.path.join(self.root, fn))
563 567 except: continue
564 568 if fn in dc:
565 569 c = dc[fn]
566 570 del dc[fn]
567 571 if not c:
568 572 if fcmp(fn):
569 573 changed.append(fn)
570 574 elif c[0] == 'm':
571 575 changed.append(fn)
572 576 elif c[0] == 'a':
573 577 added.append(fn)
574 578 elif c[0] == 'r':
575 579 unknown.append(fn)
576 580 elif c[2] != s.st_size or (c[1] ^ s.st_mode) & 0100:
577 581 changed.append(fn)
578 582 elif c[1] != s.st_mode or c[3] != s.st_mtime:
579 583 if fcmp(fn):
580 584 changed.append(fn)
581 585 else:
582 586 if self.ignore(fn): continue
583 587 unknown.append(fn)
584 588
585 589 deleted = dc.keys()
586 590 deleted.sort()
587 591
588 592 return (changed, added, deleted, unknown)
589 593
590 594 def diffrevs(self, node1, node2):
591 595 changed, added = [], []
592 596
593 597 change = self.changelog.read(node1)
594 598 mf1 = self.manifest.read(change[0])
595 599 change = self.changelog.read(node2)
596 600 mf2 = self.manifest.read(change[0])
597 601
598 602 for fn in mf2:
599 603 if mf1.has_key(fn):
600 604 if mf1[fn] != mf2[fn]:
601 605 changed.append(fn)
602 606 del mf1[fn]
603 607 else:
604 608 added.append(fn)
605 609
606 610 deleted = mf1.keys()
607 611 deleted.sort()
608 612
609 613 return (changed, added, deleted)
610 614
611 615 def add(self, list):
612 616 for f in list:
613 617 p = self.wjoin(f)
614 618 if not os.path.isfile(p):
615 619 self.ui.warn("%s does not exist!\n" % f)
616 620 elif self.dirstate.state(f) == 'n':
617 621 self.ui.warn("%s already tracked!\n" % f)
618 622 else:
619 623 self.dirstate.update([f], "a")
620 624
621 625 def forget(self, list):
622 626 for f in list:
623 627 if self.dirstate.state(f) not in 'ai':
624 628 self.ui.warn("%s not added!\n" % f)
625 629 else:
626 630 self.dirstate.forget([f])
627 631
628 632 def remove(self, list):
629 633 for f in list:
630 634 p = self.wjoin(f)
631 635 if os.path.isfile(p):
632 636 self.ui.warn("%s still exists!\n" % f)
633 637 elif f not in self.dirstate:
634 638 self.ui.warn("%s not tracked!\n" % f)
635 639 else:
636 640 self.dirstate.update([f], "r")
637 641
638 642 def heads(self):
639 643 return self.changelog.heads()
640 644
641 645 def branches(self, nodes):
642 646 if not nodes: nodes = [self.changelog.tip()]
643 647 b = []
644 648 for n in nodes:
645 649 t = n
646 650 while n:
647 651 p = self.changelog.parents(n)
648 652 if p[1] != nullid or p[0] == nullid:
649 653 b.append((t, n, p[0], p[1]))
650 654 break
651 655 n = p[0]
652 656 return b
653 657
654 658 def between(self, pairs):
655 659 r = []
656 660
657 661 for top, bottom in pairs:
658 662 n, l, i = top, [], 0
659 663 f = 1
660 664
661 665 while n != bottom:
662 666 p = self.changelog.parents(n)[0]
663 667 if i == f:
664 668 l.append(n)
665 669 f = f * 2
666 670 n = p
667 671 i += 1
668 672
669 673 r.append(l)
670 674
671 675 return r
672 676
673 677 def newer(self, nodes):
674 678 m = {}
675 679 nl = []
676 680 pm = {}
677 681 cl = self.changelog
678 682 t = l = cl.count()
679 683
680 684 # find the lowest numbered node
681 685 for n in nodes:
682 686 l = min(l, cl.rev(n))
683 687 m[n] = 1
684 688
685 689 for i in xrange(l, t):
686 690 n = cl.node(i)
687 691 if n in m: # explicitly listed
688 692 pm[n] = 1
689 693 nl.append(n)
690 694 continue
691 695 for p in cl.parents(n):
692 696 if p in pm: # parent listed
693 697 pm[n] = 1
694 698 nl.append(n)
695 699 break
696 700
697 701 return nl
698 702
699 703 def getchangegroup(self, remote):
700 704 m = self.changelog.nodemap
701 705 search = []
702 706 fetch = []
703 707 seen = {}
704 708 seenbranch = {}
705 709
706 710 # if we have an empty repo, fetch everything
707 711 if self.changelog.tip() == nullid:
708 712 self.ui.status("requesting all changes\n")
709 713 return remote.changegroup([nullid])
710 714
711 715 # otherwise, assume we're closer to the tip than the root
712 716 self.ui.status("searching for changes\n")
713 717 heads = remote.heads()
714 718 unknown = []
715 719 for h in heads:
716 720 if h not in m:
717 721 unknown.append(h)
718 722
719 723 if not unknown:
720 724 self.ui.status("nothing to do!\n")
721 725 return None
722 726
723 727 unknown = remote.branches(unknown)
724 728 while unknown:
725 729 n = unknown.pop(0)
726 730 seen[n[0]] = 1
727 731
728 732 self.ui.debug("examining %s:%s\n" % (short(n[0]), short(n[1])))
729 733 if n == nullid: break
730 734 if n in seenbranch:
731 735 self.ui.debug("branch already found\n")
732 736 continue
733 737 if n[1] and n[1] in m: # do we know the base?
734 738 self.ui.debug("found incomplete branch %s:%s\n"
735 739 % (short(n[0]), short(n[1])))
736 740 search.append(n) # schedule branch range for scanning
737 741 seenbranch[n] = 1
738 742 else:
739 743 if n[2] in m and n[3] in m:
740 744 if n[1] not in fetch:
741 745 self.ui.debug("found new changeset %s\n" %
742 746 short(n[1]))
743 747 fetch.append(n[1]) # earliest unknown
744 748 continue
745 749
746 750 r = []
747 751 for a in n[2:4]:
748 752 if a not in seen: r.append(a)
749 753
750 754 if r:
751 755 self.ui.debug("requesting %s\n" %
752 756 " ".join(map(short, r)))
753 757 for b in remote.branches(r):
754 758 self.ui.debug("received %s:%s\n" %
755 759 (short(b[0]), short(b[1])))
756 760 if b[0] not in m and b[0] not in seen:
757 761 unknown.append(b)
758 762
759 763 while search:
760 764 n = search.pop(0)
761 765 l = remote.between([(n[0], n[1])])[0]
762 766 p = n[0]
763 767 f = 1
764 768 for i in l + [n[1]]:
765 769 if i in m:
766 770 if f <= 2:
767 771 self.ui.debug("found new branch changeset %s\n" %
768 772 short(p))
769 773 fetch.append(p)
770 774 else:
771 775 self.ui.debug("narrowed branch search to %s:%s\n"
772 776 % (short(p), short(i)))
773 777 search.append((p, i))
774 778 break
775 779 p, f = i, f * 2
776 780
777 781 for f in fetch:
778 782 if f in m:
779 783 raise "already have", short(f[:4])
780 784
781 785 self.ui.note("adding new changesets starting at " +
782 786 " ".join([short(f) for f in fetch]) + "\n")
783 787
784 788 return remote.changegroup(fetch)
785 789
786 790 def changegroup(self, basenodes):
787 791 nodes = self.newer(basenodes)
788 792
789 793 # construct the link map
790 794 linkmap = {}
791 795 for n in nodes:
792 796 linkmap[self.changelog.rev(n)] = n
793 797
794 798 # construct a list of all changed files
795 799 changed = {}
796 800 for n in nodes:
797 801 c = self.changelog.read(n)
798 802 for f in c[3]:
799 803 changed[f] = 1
800 804 changed = changed.keys()
801 805 changed.sort()
802 806
803 807 # the changegroup is changesets + manifests + all file revs
804 808 revs = [ self.changelog.rev(n) for n in nodes ]
805 809
806 810 for y in self.changelog.group(linkmap): yield y
807 811 for y in self.manifest.group(linkmap): yield y
808 812 for f in changed:
809 813 yield struct.pack(">l", len(f) + 4) + f
810 814 g = self.file(f).group(linkmap)
811 815 for y in g:
812 816 yield y
813 817
814 818 def addchangegroup(self, generator):
815 819
816 820 class genread:
817 821 def __init__(self, generator):
818 822 self.g = generator
819 823 self.buf = ""
820 824 def read(self, l):
821 825 while l > len(self.buf):
822 826 try:
823 827 self.buf += self.g.next()
824 828 except StopIteration:
825 829 break
826 830 d, self.buf = self.buf[:l], self.buf[l:]
827 831 return d
828 832
829 833 def getchunk():
830 834 d = source.read(4)
831 835 if not d: return ""
832 836 l = struct.unpack(">l", d)[0]
833 837 if l <= 4: return ""
834 838 return source.read(l - 4)
835 839
836 840 def getgroup():
837 841 while 1:
838 842 c = getchunk()
839 843 if not c: break
840 844 yield c
841 845
842 846 def csmap(x):
843 847 self.ui.debug("add changeset %s\n" % short(x))
844 848 return self.changelog.count()
845 849
846 850 def revmap(x):
847 851 return self.changelog.rev(x)
848 852
849 853 if not generator: return
850 854 changesets = files = revisions = 0
851 855
852 856 source = genread(generator)
853 857 lock = self.lock()
854 858 tr = self.transaction()
855 859
856 860 # pull off the changeset group
857 861 self.ui.status("adding changesets\n")
858 862 co = self.changelog.tip()
859 863 cn = self.changelog.addgroup(getgroup(), csmap, tr, 1) # unique
860 864 changesets = self.changelog.rev(cn) - self.changelog.rev(co)
861 865
862 866 # pull off the manifest group
863 867 self.ui.status("adding manifests\n")
864 868 mm = self.manifest.tip()
865 869 mo = self.manifest.addgroup(getgroup(), revmap, tr)
866 870
867 871 # process the files
868 872 self.ui.status("adding file revisions\n")
869 873 while 1:
870 874 f = getchunk()
871 875 if not f: break
872 876 self.ui.debug("adding %s revisions\n" % f)
873 877 fl = self.file(f)
874 878 o = fl.tip()
875 879 n = fl.addgroup(getgroup(), revmap, tr)
876 880 revisions += fl.rev(n) - fl.rev(o)
877 881 files += 1
878 882
879 883 self.ui.status(("modified %d files, added %d changesets" +
880 884 " and %d new revisions\n")
881 885 % (files, changesets, revisions))
882 886
883 887 tr.close()
884 888 return
885 889
886 890 def update(self, node, allow=False, force=False):
887 891 pl = self.dirstate.parents()
888 892 if not force and pl[1] != nullid:
889 893 self.ui.warn("aborting: outstanding uncommitted merges\n")
890 894 return
891 895
892 896 p1, p2 = pl[0], node
893 897 pa = self.changelog.ancestor(p1, p2)
894 898 m1n = self.changelog.read(p1)[0]
895 899 m2n = self.changelog.read(p2)[0]
896 900 man = self.manifest.ancestor(m1n, m2n)
897 901 m1 = self.manifest.read(m1n)
898 902 mf1 = self.manifest.readflags(m1n)
899 903 m2 = self.manifest.read(m2n)
900 904 mf2 = self.manifest.readflags(m2n)
901 905 ma = self.manifest.read(man)
902 906 mfa = self.manifest.readflags(m2n)
903 907
904 908 (c, a, d, u) = self.diffdir(self.root)
905 909
906 910 # resolve the manifest to determine which files
907 911 # we care about merging
908 912 self.ui.note("resolving manifests\n")
909 913 self.ui.debug(" ancestor %s local %s remote %s\n" %
910 914 (short(man), short(m1n), short(m2n)))
911 915
912 916 merge = {}
913 917 get = {}
914 918 remove = []
915 919 mark = {}
916 920
917 921 # construct a working dir manifest
918 922 mw = m1.copy()
919 923 mfw = mf1.copy()
920 924 for f in a + c + u:
921 925 mw[f] = ""
922 926 mfw[f] = is_exec(self.wjoin(f))
923 927 for f in d:
924 928 if f in mw: del mw[f]
925 929
926 930 for f, n in mw.iteritems():
927 931 if f in m2:
928 932 s = 0
929 933
930 934 # are files different?
931 935 if n != m2[f]:
932 936 a = ma.get(f, nullid)
933 937 # are both different from the ancestor?
934 938 if n != a and m2[f] != a:
935 939 self.ui.debug(" %s versions differ, resolve\n" % f)
936 940 merge[f] = (m1.get(f, nullid), m2[f])
937 941 # merge executable bits
938 942 # "if we changed or they changed, change in merge"
939 943 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
940 944 mode = ((a^b) | (a^c)) ^ a
941 945 merge[f] = (m1.get(f, nullid), m2[f], mode)
942 946 s = 1
943 947 # are we clobbering?
944 948 # is remote's version newer?
945 949 # or are we going back in time?
946 950 elif force or m2[f] != a or (p2 == pa and mw[f] == m1[f]):
947 951 self.ui.debug(" remote %s is newer, get\n" % f)
948 952 get[f] = m2[f]
949 953 s = 1
950 954 else:
951 955 mark[f] = 1
952 956
953 957 if not s and mfw[f] != mf2[f]:
954 958 if force:
955 959 self.ui.debug(" updating permissions for %s\n" % f)
956 960 set_exec(self.wjoin(f), mf2[f])
957 961 else:
958 962 a, b, c = mfa.get(f, 0), mfw[f], mf2[f]
959 963 mode = ((a^b) | (a^c)) ^ a
960 964 if mode != b:
961 965 self.ui.debug(" updating permissions for %s\n" % f)
962 966 set_exec(self.wjoin(f), mode)
963 967 mark[f] = 1
964 968 del m2[f]
965 969 elif f in ma:
966 970 if not force and n != ma[f]:
967 971 r = self.ui.prompt(
968 972 (" local changed %s which remote deleted\n" % f) +
969 973 "(k)eep or (d)elete?", "[kd]", "k")
970 974 if r == "d":
971 975 remove.append(f)
972 976 else:
973 977 self.ui.debug("other deleted %s\n" % f)
974 978 remove.append(f) # other deleted it
975 979 else:
976 980 if n == m1.get(f, nullid): # same as parent
977 981 self.ui.debug("remote deleted %s\n" % f)
978 982 remove.append(f)
979 983 else:
980 984 self.ui.debug("working dir created %s, keeping\n" % f)
981 985
982 986 for f, n in m2.iteritems():
983 987 if f[0] == "/": continue
984 988 if not force and f in ma and n != ma[f]:
985 989 r = self.ui.prompt(
986 990 ("remote changed %s which local deleted\n" % f) +
987 991 "(k)eep or (d)elete?", "[kd]", "k")
988 992 if r == "d": remove.append(f)
989 993 else:
990 994 self.ui.debug("remote created %s\n" % f)
991 995 get[f] = n
992 996
993 997 del mw, m1, m2, ma
994 998
995 999 if force:
996 1000 for f in merge:
997 1001 get[f] = merge[f][1]
998 1002 merge = {}
999 1003
1000 1004 if pa == p1 or pa == p2:
1001 1005 # we don't need to do any magic, just jump to the new rev
1002 1006 mode = 'n'
1003 1007 p1, p2 = p2, nullid
1004 1008 else:
1005 1009 if not allow:
1006 1010 self.ui.status("this update spans a branch" +
1007 1011 " affecting the following files:\n")
1008 1012 fl = merge.keys() + get.keys()
1009 1013 fl.sort()
1010 1014 for f in fl:
1011 1015 cf = ""
1012 1016 if f in merge: cf = " (resolve)"
1013 1017 self.ui.status(" %s%s\n" % (f, cf))
1014 1018 self.ui.warn("aborting update spanning branches!\n")
1015 1019 self.ui.status("(use update -m to perform a branch merge)\n")
1016 1020 return 1
1017 1021 # we have to remember what files we needed to get/change
1018 1022 # because any file that's different from either one of its
1019 1023 # parents must be in the changeset
1020 1024 mode = 'm'
1021 1025 self.dirstate.update(mark.keys(), "m")
1022 1026
1023 1027 self.dirstate.setparents(p1, p2)
1024 1028
1025 1029 # get the files we don't need to change
1026 1030 files = get.keys()
1027 1031 files.sort()
1028 1032 for f in files:
1029 1033 if f[0] == "/": continue
1030 1034 self.ui.note("getting %s\n" % f)
1031 1035 t = self.file(f).read(get[f])
1032 1036 try:
1033 1037 self.wfile(f, "w").write(t)
1034 1038 except IOError:
1035 1039 os.makedirs(os.path.dirname(self.wjoin(f)))
1036 1040 self.wfile(f, "w").write(t)
1037 1041 set_exec(self.wjoin(f), mf2[f])
1038 1042 self.dirstate.update([f], mode)
1039 1043
1040 1044 # merge the tricky bits
1041 1045 files = merge.keys()
1042 1046 files.sort()
1043 1047 for f in files:
1044 1048 self.ui.status("merging %s\n" % f)
1045 1049 m, o, flag = merge[f]
1046 1050 self.merge3(f, m, o)
1047 1051 set_exec(self.wjoin(f), flag)
1048 1052 self.dirstate.update([f], 'm')
1049 1053
1050 1054 for f in remove:
1051 1055 self.ui.note("removing %s\n" % f)
1052 1056 os.unlink(f)
1053 1057 if mode == 'n':
1054 1058 self.dirstate.forget(remove)
1055 1059 else:
1056 1060 self.dirstate.update(remove, 'r')
1057 1061
1058 1062 def merge3(self, fn, my, other):
1059 1063 """perform a 3-way merge in the working directory"""
1060 1064
1061 1065 def temp(prefix, node):
1062 1066 pre = "%s~%s." % (os.path.basename(fn), prefix)
1063 1067 (fd, name) = tempfile.mkstemp("", pre)
1064 1068 f = os.fdopen(fd, "w")
1065 1069 f.write(fl.revision(node))
1066 1070 f.close()
1067 1071 return name
1068 1072
1069 1073 fl = self.file(fn)
1070 1074 base = fl.ancestor(my, other)
1071 1075 a = self.wjoin(fn)
1072 1076 b = temp("other", other)
1073 1077 c = temp("base", base)
1074 1078
1075 1079 self.ui.note("resolving %s\n" % fn)
1076 1080 self.ui.debug("file %s: other %s ancestor %s\n" %
1077 1081 (fn, short(other), short(base)))
1078 1082
1079 1083 cmd = os.environ.get("HGMERGE", "hgmerge")
1080 1084 r = os.system("%s %s %s %s" % (cmd, a, b, c))
1081 1085 if r:
1082 1086 self.ui.warn("merging %s failed!\n" % fn)
1083 1087
1084 1088 os.unlink(b)
1085 1089 os.unlink(c)
1086 1090
1087 1091 def verify(self):
1088 1092 filelinkrevs = {}
1089 1093 filenodes = {}
1090 1094 changesets = revisions = files = 0
1091 1095 errors = 0
1092 1096
1093 1097 seen = {}
1094 1098 self.ui.status("checking changesets\n")
1095 1099 for i in range(self.changelog.count()):
1096 1100 changesets += 1
1097 1101 n = self.changelog.node(i)
1098 1102 if n in seen:
1099 1103 self.ui.warn("duplicate changeset at revision %d\n" % i)
1100 1104 errors += 1
1101 1105 seen[n] = 1
1102 1106
1103 1107 for p in self.changelog.parents(n):
1104 1108 if p not in self.changelog.nodemap:
1105 1109 self.ui.warn("changeset %s has unknown parent %s\n" %
1106 1110 (short(n), short(p)))
1107 1111 errors += 1
1108 1112 try:
1109 1113 changes = self.changelog.read(n)
1110 1114 except Exception, inst:
1111 1115 self.ui.warn("unpacking changeset %s: %s\n" % (short(n), inst))
1112 1116 errors += 1
1113 1117
1114 1118 for f in changes[3]:
1115 1119 filelinkrevs.setdefault(f, []).append(i)
1116 1120
1117 1121 seen = {}
1118 1122 self.ui.status("checking manifests\n")
1119 1123 for i in range(self.manifest.count()):
1120 1124 n = self.manifest.node(i)
1121 1125 if n in seen:
1122 1126 self.ui.warn("duplicate manifest at revision %d\n" % i)
1123 1127 errors += 1
1124 1128 seen[n] = 1
1125 1129
1126 1130 for p in self.manifest.parents(n):
1127 1131 if p not in self.manifest.nodemap:
1128 1132 self.ui.warn("manifest %s has unknown parent %s\n" %
1129 1133 (short(n), short(p)))
1130 1134 errors += 1
1131 1135
1132 1136 try:
1133 1137 delta = mdiff.patchtext(self.manifest.delta(n))
1134 1138 except KeyboardInterrupt:
1135 1139 print "aborted"
1136 1140 sys.exit(0)
1137 1141 except Exception, inst:
1138 1142 self.ui.warn("unpacking manifest %s: %s\n"
1139 1143 % (short(n), inst))
1140 1144 errors += 1
1141 1145
1142 1146 ff = [ l.split('\0') for l in delta.splitlines() ]
1143 1147 for f, fn in ff:
1144 1148 filenodes.setdefault(f, {})[bin(fn[:40])] = 1
1145 1149
1146 1150 self.ui.status("crosschecking files in changesets and manifests\n")
1147 1151 for f in filenodes:
1148 1152 if f not in filelinkrevs:
1149 1153 self.ui.warn("file %s in manifest but not in changesets\n" % f)
1150 1154 errors += 1
1151 1155
1152 1156 for f in filelinkrevs:
1153 1157 if f not in filenodes:
1154 1158 self.ui.warn("file %s in changeset but not in manifest\n" % f)
1155 1159 errors += 1
1156 1160
1157 1161 self.ui.status("checking files\n")
1158 1162 ff = filenodes.keys()
1159 1163 ff.sort()
1160 1164 for f in ff:
1161 1165 if f == "/dev/null": continue
1162 1166 files += 1
1163 1167 fl = self.file(f)
1164 1168 nodes = { nullid: 1 }
1165 1169 seen = {}
1166 1170 for i in range(fl.count()):
1167 1171 revisions += 1
1168 1172 n = fl.node(i)
1169 1173
1170 1174 if n in seen:
1171 1175 self.ui.warn("%s: duplicate revision %d\n" % (f, i))
1172 1176 errors += 1
1173 1177
1174 1178 if n not in filenodes[f]:
1175 1179 self.ui.warn("%s: %d:%s not in manifests\n"
1176 1180 % (f, i, short(n)))
1177 1181 print len(filenodes[f].keys()), fl.count(), f
1178 1182 errors += 1
1179 1183 else:
1180 1184 del filenodes[f][n]
1181 1185
1182 1186 flr = fl.linkrev(n)
1183 1187 if flr not in filelinkrevs[f]:
1184 1188 self.ui.warn("%s:%s points to unexpected changeset %d\n"
1185 1189 % (f, short(n), fl.linkrev(n)))
1186 1190 errors += 1
1187 1191 else:
1188 1192 filelinkrevs[f].remove(flr)
1189 1193
1190 1194 # verify contents
1191 1195 try:
1192 1196 t = fl.read(n)
1193 1197 except Exception, inst:
1194 1198 self.ui.warn("unpacking file %s %s: %s\n"
1195 1199 % (f, short(n), inst))
1196 1200 errors += 1
1197 1201
1198 1202 # verify parents
1199 1203 (p1, p2) = fl.parents(n)
1200 1204 if p1 not in nodes:
1201 1205 self.ui.warn("file %s:%s unknown parent 1 %s" %
1202 1206 (f, short(n), short(p1)))
1203 1207 errors += 1
1204 1208 if p2 not in nodes:
1205 1209 self.ui.warn("file %s:%s unknown parent 2 %s" %
1206 1210 (f, short(n), short(p1)))
1207 1211 errors += 1
1208 1212 nodes[n] = 1
1209 1213
1210 1214 # cross-check
1211 1215 for node in filenodes[f]:
1212 1216 self.ui.warn("node %s in manifests not in %s\n"
1213 1217 % (hex(n), f))
1214 1218 errors += 1
1215 1219
1216 1220 self.ui.status("%d files, %d changesets, %d total revisions\n" %
1217 1221 (files, changesets, revisions))
1218 1222
1219 1223 if errors:
1220 1224 self.ui.warn("%d integrity errors encountered!\n" % errors)
1221 1225 return 1
1222 1226
1223 1227 class remoterepository:
1224 1228 def __init__(self, ui, path):
1225 1229 self.url = path
1226 1230 self.ui = ui
1227 1231
1228 1232 def do_cmd(self, cmd, **args):
1229 1233 self.ui.debug("sending %s command\n" % cmd)
1230 1234 q = {"cmd": cmd}
1231 1235 q.update(args)
1232 1236 qs = urllib.urlencode(q)
1233 1237 cu = "%s?%s" % (self.url, qs)
1234 1238 return urllib.urlopen(cu)
1235 1239
1236 1240 def heads(self):
1237 1241 d = self.do_cmd("heads").read()
1238 1242 try:
1239 1243 return map(bin, d[:-1].split(" "))
1240 1244 except:
1241 1245 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1242 1246 raise
1243 1247
1244 1248 def branches(self, nodes):
1245 1249 n = " ".join(map(hex, nodes))
1246 1250 d = self.do_cmd("branches", nodes=n).read()
1247 1251 try:
1248 1252 br = [ tuple(map(bin, b.split(" "))) for b in d.splitlines() ]
1249 1253 return br
1250 1254 except:
1251 1255 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1252 1256 raise
1253 1257
1254 1258 def between(self, pairs):
1255 1259 n = "\n".join(["-".join(map(hex, p)) for p in pairs])
1256 1260 d = self.do_cmd("between", pairs=n).read()
1257 1261 try:
1258 1262 p = [ l and map(bin, l.split(" ")) or [] for l in d.splitlines() ]
1259 1263 return p
1260 1264 except:
1261 1265 self.ui.warn("unexpected response:\n" + d[:400] + "\n...\n")
1262 1266 raise
1263 1267
1264 1268 def changegroup(self, nodes):
1265 1269 n = " ".join(map(hex, nodes))
1266 1270 zd = zlib.decompressobj()
1267 1271 f = self.do_cmd("changegroup", roots=n)
1268 1272 bytes = 0
1269 1273 while 1:
1270 1274 d = f.read(4096)
1271 1275 bytes += len(d)
1272 1276 if not d:
1273 1277 yield zd.flush()
1274 1278 break
1275 1279 yield zd.decompress(d)
1276 1280 self.ui.note("%d bytes of data transfered\n" % bytes)
1277 1281
1278 1282 def repository(ui, path=None, create=0):
1279 1283 if path and path[:7] == "http://":
1280 1284 return remoterepository(ui, path)
1281 1285 if path and path[:5] == "hg://":
1282 1286 return remoterepository(ui, path.replace("hg://", "http://"))
1283 1287 if path and path[:11] == "old-http://":
1284 1288 return localrepository(ui, path.replace("old-http://", "http://"))
1285 1289 else:
1286 1290 return localrepository(ui, path, create)
1287 1291
1288 1292 class httprangereader:
1289 1293 def __init__(self, url):
1290 1294 self.url = url
1291 1295 self.pos = 0
1292 1296 def seek(self, pos):
1293 1297 self.pos = pos
1294 1298 def read(self, bytes=None):
1295 1299 opener = urllib2.build_opener(byterange.HTTPRangeHandler())
1296 1300 urllib2.install_opener(opener)
1297 1301 req = urllib2.Request(self.url)
1298 1302 end = ''
1299 1303 if bytes: end = self.pos + bytes
1300 1304 req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
1301 1305 f = urllib2.urlopen(req)
1302 1306 return f.read()
General Comments 0
You need to be logged in to leave comments. Login now