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