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