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