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