Show More
@@ -1,826 +1,862 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, sha, socket, os, time, base64, re, urllib2 |
|
9 | 9 | import urllib |
|
10 | 10 | from mercurial import byterange |
|
11 | 11 | from mercurial.transaction import * |
|
12 | 12 | from mercurial.revlog import * |
|
13 | 13 | from difflib import SequenceMatcher |
|
14 | 14 | |
|
15 | 15 | class filelog(revlog): |
|
16 | 16 | def __init__(self, opener, path): |
|
17 | 17 | s = self.encodepath(path) |
|
18 | 18 | revlog.__init__(self, opener, os.path.join("data", s + "i"), |
|
19 | 19 | os.path.join("data", s)) |
|
20 | 20 | |
|
21 | 21 | def encodepath(self, path): |
|
22 | 22 | s = sha.sha(path).digest() |
|
23 | 23 | s = base64.encodestring(s)[:-3] |
|
24 | 24 | s = re.sub("\+", "%", s) |
|
25 | 25 | s = re.sub("/", "_", s) |
|
26 | 26 | return s |
|
27 | 27 | |
|
28 | 28 | def read(self, node): |
|
29 | 29 | return self.revision(node) |
|
30 | 30 | def add(self, text, transaction, link, p1=None, p2=None): |
|
31 | 31 | return self.addrevision(text, transaction, link, p1, p2) |
|
32 | 32 | |
|
33 | 33 | def annotate(self, node): |
|
34 | 34 | revs = [] |
|
35 | 35 | while node != nullid: |
|
36 | 36 | revs.append(node) |
|
37 | 37 | node = self.parents(node)[0] |
|
38 | 38 | revs.reverse() |
|
39 | 39 | prev = [] |
|
40 | 40 | annotate = [] |
|
41 | 41 | for node in revs: |
|
42 | 42 | curr = self.read(node).splitlines(1) |
|
43 | 43 | linkrev = self.linkrev(node) |
|
44 | 44 | sm = SequenceMatcher(None, prev, curr) |
|
45 | 45 | offset = 0 |
|
46 | 46 | for o, m, n, s, t in sm.get_opcodes(): |
|
47 | 47 | if o in ('insert','replace'): |
|
48 | 48 | annotate[m+offset:n+offset] = \ |
|
49 | 49 | [ (linkrev, l) for l in curr[s:t]] |
|
50 | 50 | if o == 'insert': |
|
51 | 51 | offset += m-n |
|
52 | 52 | elif o == 'delete': |
|
53 | 53 | del annotate[m+offset:n+offset] |
|
54 | 54 | offset -= m-n |
|
55 | 55 | assert len(annotate) == len(curr) |
|
56 | 56 | prev = curr |
|
57 | 57 | return annotate |
|
58 | 58 | |
|
59 | 59 | class manifest(revlog): |
|
60 | 60 | def __init__(self, opener): |
|
61 | 61 | self.mapcache = None |
|
62 | 62 | self.listcache = None |
|
63 | 63 | self.addlist = None |
|
64 | 64 | revlog.__init__(self, opener, "00manifest.i", "00manifest.d") |
|
65 | 65 | |
|
66 | 66 | def read(self, node): |
|
67 | 67 | if self.mapcache and self.mapcache[0] == node: |
|
68 | 68 | return self.mapcache[1].copy() |
|
69 | 69 | text = self.revision(node) |
|
70 | 70 | map = {} |
|
71 | 71 | self.listcache = (text, text.splitlines(1)) |
|
72 | 72 | for l in self.listcache[1]: |
|
73 | 73 | (f, n) = l.split('\0') |
|
74 | 74 | map[f] = bin(n[:40]) |
|
75 | 75 | self.mapcache = (node, map) |
|
76 | 76 | return map |
|
77 | 77 | |
|
78 | 78 | def diff(self, a, b): |
|
79 | 79 | # this is sneaky, as we're not actually using a and b |
|
80 | 80 | if self.listcache and len(self.listcache[0]) == len(a): |
|
81 | 81 | return mdiff.diff(self.listcache[1], self.addlist, 1) |
|
82 | 82 | else: |
|
83 | 83 | return mdiff.textdiff(a, b) |
|
84 | 84 | |
|
85 | 85 | def add(self, map, transaction, link, p1=None, p2=None): |
|
86 | 86 | files = map.keys() |
|
87 | 87 | files.sort() |
|
88 | 88 | |
|
89 | 89 | self.addlist = ["%s\000%s\n" % (f, hex(map[f])) for f in files] |
|
90 | 90 | text = "".join(self.addlist) |
|
91 | 91 | |
|
92 | 92 | n = self.addrevision(text, transaction, link, p1, p2) |
|
93 | 93 | self.mapcache = (n, map) |
|
94 | 94 | self.listcache = (text, self.addlist) |
|
95 | 95 | |
|
96 | 96 | return n |
|
97 | 97 | |
|
98 | 98 | class changelog(revlog): |
|
99 | 99 | def __init__(self, opener): |
|
100 | 100 | revlog.__init__(self, opener, "00changelog.i", "00changelog.d") |
|
101 | 101 | |
|
102 | 102 | def extract(self, text): |
|
103 | 103 | if not text: |
|
104 | 104 | return (nullid, "", "0", [], "") |
|
105 | 105 | last = text.index("\n\n") |
|
106 | 106 | desc = text[last + 2:] |
|
107 | 107 | l = text[:last].splitlines() |
|
108 | 108 | manifest = bin(l[0]) |
|
109 | 109 | user = l[1] |
|
110 | 110 | date = l[2] |
|
111 | 111 | files = l[3:] |
|
112 | 112 | return (manifest, user, date, files, desc) |
|
113 | 113 | |
|
114 | 114 | def read(self, node): |
|
115 | 115 | return self.extract(self.revision(node)) |
|
116 | 116 | |
|
117 | 117 | def add(self, manifest, list, desc, transaction, p1=None, p2=None): |
|
118 | 118 | user = (os.environ.get("HGUSER") or |
|
119 | 119 | os.environ.get("EMAIL") or |
|
120 | 120 | os.environ.get("LOGNAME", "unknown") + '@' + socket.getfqdn()) |
|
121 | 121 | date = "%d %d" % (time.time(), time.timezone) |
|
122 | 122 | list.sort() |
|
123 | 123 | l = [hex(manifest), user, date] + list + ["", desc] |
|
124 | 124 | text = "\n".join(l) |
|
125 | 125 | return self.addrevision(text, transaction, self.count(), p1, p2) |
|
126 | 126 | |
|
127 | 127 | class dircache: |
|
128 | 128 | def __init__(self, opener, ui): |
|
129 | 129 | self.opener = opener |
|
130 | 130 | self.dirty = 0 |
|
131 | 131 | self.ui = ui |
|
132 | 132 | self.map = None |
|
133 | 133 | def __del__(self): |
|
134 | 134 | if self.dirty: self.write() |
|
135 | 135 | def __getitem__(self, key): |
|
136 | 136 | try: |
|
137 | 137 | return self.map[key] |
|
138 | 138 | except TypeError: |
|
139 | 139 | self.read() |
|
140 | 140 | return self[key] |
|
141 | 141 | |
|
142 | 142 | def read(self): |
|
143 | 143 | if self.map is not None: return self.map |
|
144 | 144 | |
|
145 | 145 | self.map = {} |
|
146 | 146 | try: |
|
147 | 147 | st = self.opener("dircache").read() |
|
148 | 148 | except: return |
|
149 | 149 | |
|
150 | 150 | pos = 0 |
|
151 | 151 | while pos < len(st): |
|
152 | 152 | e = struct.unpack(">llll", st[pos:pos+16]) |
|
153 | 153 | l = e[3] |
|
154 | 154 | pos += 16 |
|
155 | 155 | f = st[pos:pos + l] |
|
156 | 156 | self.map[f] = e[:3] |
|
157 | 157 | pos += l |
|
158 | 158 | |
|
159 | 159 | def update(self, files): |
|
160 | 160 | if not files: return |
|
161 | 161 | self.read() |
|
162 | 162 | self.dirty = 1 |
|
163 | 163 | for f in files: |
|
164 | 164 | try: |
|
165 | 165 | s = os.stat(f) |
|
166 | 166 | self.map[f] = (s.st_mode, s.st_size, s.st_mtime) |
|
167 | 167 | except IOError: |
|
168 | 168 | self.remove(f) |
|
169 | 169 | |
|
170 | 170 | def taint(self, files): |
|
171 | 171 | if not files: return |
|
172 | 172 | self.read() |
|
173 | 173 | self.dirty = 1 |
|
174 | 174 | for f in files: |
|
175 | 175 | self.map[f] = (0, -1, 0) |
|
176 | 176 | |
|
177 | 177 | def remove(self, files): |
|
178 | 178 | if not files: return |
|
179 | 179 | self.read() |
|
180 | 180 | self.dirty = 1 |
|
181 | 181 | for f in files: |
|
182 | 182 | try: |
|
183 | 183 | del self.map[f] |
|
184 | 184 | except KeyError: |
|
185 | 185 | self.ui.warn("Not in dircache: %s\n" % f) |
|
186 | 186 | pass |
|
187 | 187 | |
|
188 | 188 | def clear(self): |
|
189 | 189 | self.map = {} |
|
190 | 190 | self.dirty = 1 |
|
191 | 191 | |
|
192 | 192 | def write(self): |
|
193 | 193 | st = self.opener("dircache", "w") |
|
194 | 194 | for f, e in self.map.items(): |
|
195 | 195 | e = struct.pack(">llll", e[0], e[1], e[2], len(f)) |
|
196 | 196 | st.write(e + f) |
|
197 | 197 | self.dirty = 0 |
|
198 | 198 | |
|
199 | 199 | def copy(self): |
|
200 | 200 | self.read() |
|
201 | 201 | return self.map.copy() |
|
202 | 202 | |
|
203 | 203 | # used to avoid circular references so destructors work |
|
204 | 204 | def opener(base): |
|
205 | 205 | p = base |
|
206 | 206 | def o(path, mode="r"): |
|
207 | 207 | if p[:7] == "http://": |
|
208 | 208 | f = os.path.join(p, urllib.quote(path)) |
|
209 | 209 | return httprangereader(f) |
|
210 | 210 | |
|
211 | 211 | f = os.path.join(p, path) |
|
212 | 212 | |
|
213 | 213 | if mode != "r" and os.path.isfile(f): |
|
214 | 214 | s = os.stat(f) |
|
215 | 215 | if s.st_nlink > 1: |
|
216 | 216 | file(f + ".tmp", "w").write(file(f).read()) |
|
217 | 217 | os.rename(f+".tmp", f) |
|
218 | 218 | |
|
219 | 219 | return file(f, mode) |
|
220 | 220 | |
|
221 | 221 | return o |
|
222 | 222 | |
|
223 | 223 | class localrepository: |
|
224 | 224 | def __init__(self, ui, path=None, create=0): |
|
225 | 225 | self.remote = 0 |
|
226 | 226 | if path and path[:7] == "http://": |
|
227 | 227 | self.remote = 1 |
|
228 | 228 | self.path = path |
|
229 | 229 | else: |
|
230 | 230 | if not path: |
|
231 | 231 | p = os.getcwd() |
|
232 | 232 | while not os.path.isdir(os.path.join(p, ".hg")): |
|
233 | 233 | p = os.path.dirname(p) |
|
234 | 234 | if p == "/": raise "No repo found" |
|
235 | 235 | path = p |
|
236 | 236 | self.path = os.path.join(path, ".hg") |
|
237 | 237 | |
|
238 | 238 | self.root = path |
|
239 | 239 | self.ui = ui |
|
240 | 240 | |
|
241 | 241 | if create: |
|
242 | 242 | os.mkdir(self.path) |
|
243 | 243 | os.mkdir(self.join("data")) |
|
244 | 244 | |
|
245 | 245 | self.opener = opener(self.path) |
|
246 | 246 | self.manifest = manifest(self.opener) |
|
247 | 247 | self.changelog = changelog(self.opener) |
|
248 | 248 | self.ignorelist = None |
|
249 | 249 | self.tags = None |
|
250 | 250 | |
|
251 | 251 | if not self.remote: |
|
252 | 252 | self.dircache = dircache(self.opener, ui) |
|
253 | 253 | try: |
|
254 | 254 | self.current = bin(self.opener("current").read()) |
|
255 | 255 | except IOError: |
|
256 | 256 | self.current = None |
|
257 | 257 | |
|
258 | 258 | def setcurrent(self, node): |
|
259 | 259 | self.current = node |
|
260 | 260 | self.opener("current", "w").write(hex(node)) |
|
261 | 261 | |
|
262 | 262 | def ignore(self, f): |
|
263 | 263 | if self.ignorelist is None: |
|
264 | 264 | self.ignorelist = [] |
|
265 | 265 | try: |
|
266 | 266 | l = open(os.path.join(self.root, ".hgignore")) |
|
267 | 267 | for pat in l: |
|
268 | 268 | if pat != "\n": |
|
269 | 269 | self.ignorelist.append(re.compile(pat[:-1])) |
|
270 | 270 | except IOError: pass |
|
271 | 271 | for pat in self.ignorelist: |
|
272 | 272 | if pat.search(f): return True |
|
273 | 273 | return False |
|
274 | 274 | |
|
275 | 275 | def lookup(self, key): |
|
276 | 276 | if self.tags is None: |
|
277 | 277 | self.tags = {} |
|
278 | 278 | try: |
|
279 | 279 | fl = self.file(".hgtags") |
|
280 | 280 | for l in fl.revision(fl.tip()).splitlines(): |
|
281 | 281 | if l: |
|
282 | 282 | n, k = l.split(" ") |
|
283 | 283 | self.tags[k] = bin(n) |
|
284 | 284 | except KeyError: pass |
|
285 | 285 | try: |
|
286 | 286 | return self.tags[key] |
|
287 | 287 | except KeyError: |
|
288 | 288 | return self.changelog.lookup(key) |
|
289 | 289 | |
|
290 | 290 | def join(self, f): |
|
291 | 291 | return os.path.join(self.path, f) |
|
292 | 292 | |
|
293 | 293 | def file(self, f): |
|
294 | 294 | return filelog(self.opener, f) |
|
295 | 295 | |
|
296 | 296 | def transaction(self): |
|
297 | 297 | return transaction(self.opener, self.join("journal"), |
|
298 | 298 | self.join("undo")) |
|
299 | 299 | |
|
300 | 300 | def commit(self, parent, update = None, text = ""): |
|
301 | 301 | tr = self.transaction() |
|
302 | 302 | |
|
303 | 303 | try: |
|
304 | 304 | remove = [ l[:-1] for l in self.opener("to-remove") ] |
|
305 | 305 | os.unlink(self.join("to-remove")) |
|
306 | 306 | |
|
307 | 307 | except IOError: |
|
308 | 308 | remove = [] |
|
309 | 309 | |
|
310 | 310 | if update == None: |
|
311 | 311 | update = self.diffdir(self.root, parent)[0] |
|
312 | 312 | |
|
313 | 313 | # check in files |
|
314 | 314 | new = {} |
|
315 | 315 | linkrev = self.changelog.count() |
|
316 | 316 | for f in update: |
|
317 | 317 | self.ui.note(f + "\n") |
|
318 | 318 | try: |
|
319 | 319 | t = file(f).read() |
|
320 | 320 | except IOError: |
|
321 | 321 | remove.append(f) |
|
322 | 322 | continue |
|
323 | 323 | r = self.file(f) |
|
324 | 324 | new[f] = r.add(t, tr, linkrev) |
|
325 | 325 | |
|
326 | 326 | # update manifest |
|
327 | 327 | mmap = self.manifest.read(self.manifest.tip()) |
|
328 | 328 | mmap.update(new) |
|
329 | 329 | for f in remove: |
|
330 | 330 | del mmap[f] |
|
331 | 331 | mnode = self.manifest.add(mmap, tr, linkrev) |
|
332 | 332 | |
|
333 | 333 | # add changeset |
|
334 | 334 | new = new.keys() |
|
335 | 335 | new.sort() |
|
336 | 336 | |
|
337 | 337 | edittext = text + "\n"+"".join(["HG: changed %s\n" % f for f in new]) |
|
338 | 338 | edittext += "".join(["HG: removed %s\n" % f for f in remove]) |
|
339 | 339 | edittext = self.ui.edit(edittext) |
|
340 | 340 | |
|
341 | 341 | n = self.changelog.add(mnode, new, edittext, tr) |
|
342 | 342 | tr.close() |
|
343 | 343 | |
|
344 | 344 | self.setcurrent(n) |
|
345 | 345 | self.dircache.update(new) |
|
346 | 346 | self.dircache.remove(remove) |
|
347 | 347 | |
|
348 | 348 | def checkdir(self, path): |
|
349 | 349 | d = os.path.dirname(path) |
|
350 | 350 | if not d: return |
|
351 | 351 | if not os.path.isdir(d): |
|
352 | 352 | self.checkdir(d) |
|
353 | 353 | os.mkdir(d) |
|
354 | 354 | |
|
355 | 355 | def checkout(self, node): |
|
356 | 356 | # checkout is really dumb at the moment |
|
357 | 357 | # it ought to basically merge |
|
358 | 358 | change = self.changelog.read(node) |
|
359 | 359 | mmap = self.manifest.read(change[0]) |
|
360 | 360 | |
|
361 | 361 | l = mmap.keys() |
|
362 | 362 | l.sort() |
|
363 | 363 | stats = [] |
|
364 | 364 | for f in l: |
|
365 | 365 | self.ui.note(f + "\n") |
|
366 | 366 | r = self.file(f) |
|
367 | 367 | t = r.revision(mmap[f]) |
|
368 | 368 | try: |
|
369 | 369 | file(f, "w").write(t) |
|
370 | 370 | except: |
|
371 | 371 | self.checkdir(f) |
|
372 | 372 | file(f, "w").write(t) |
|
373 | 373 | |
|
374 | 374 | self.setcurrent(node) |
|
375 | 375 | self.dircache.clear() |
|
376 | 376 | self.dircache.update(l) |
|
377 | 377 | |
|
378 | 378 | def diffdir(self, path, changeset): |
|
379 | 379 | changed = [] |
|
380 | 380 | mf = {} |
|
381 | 381 | added = [] |
|
382 | 382 | |
|
383 | 383 | if changeset: |
|
384 | 384 | change = self.changelog.read(changeset) |
|
385 | 385 | mf = self.manifest.read(change[0]) |
|
386 | 386 | |
|
387 | 387 | if changeset == self.current: |
|
388 | 388 | dc = self.dircache.copy() |
|
389 | 389 | else: |
|
390 | 390 | dc = dict.fromkeys(mf) |
|
391 | 391 | |
|
392 | 392 | def fcmp(fn): |
|
393 | 393 | t1 = file(os.path.join(self.root, fn)).read() |
|
394 | 394 | t2 = self.file(fn).revision(mf[fn]) |
|
395 | 395 | return cmp(t1, t2) |
|
396 | 396 | |
|
397 | 397 | for dir, subdirs, files in os.walk(self.root): |
|
398 | 398 | d = dir[len(self.root)+1:] |
|
399 | 399 | if ".hg" in subdirs: subdirs.remove(".hg") |
|
400 | 400 | |
|
401 | 401 | for f in files: |
|
402 | 402 | fn = os.path.join(d, f) |
|
403 | 403 | try: s = os.stat(os.path.join(self.root, fn)) |
|
404 | 404 | except: continue |
|
405 | 405 | if fn in dc: |
|
406 | 406 | c = dc[fn] |
|
407 | 407 | del dc[fn] |
|
408 | 408 | if not c: |
|
409 | 409 | if fcmp(fn): |
|
410 | 410 | changed.append(fn) |
|
411 | 411 | elif c[1] != s.st_size: |
|
412 | 412 | changed.append(fn) |
|
413 | 413 | elif c[0] != s.st_mode or c[2] != s.st_mtime: |
|
414 | 414 | if fcmp(fn): |
|
415 | 415 | changed.append(fn) |
|
416 | 416 | else: |
|
417 | 417 | if self.ignore(fn): continue |
|
418 | 418 | added.append(fn) |
|
419 | 419 | |
|
420 | 420 | deleted = dc.keys() |
|
421 | 421 | deleted.sort() |
|
422 | 422 | |
|
423 | 423 | return (changed, added, deleted) |
|
424 | 424 | |
|
425 | 425 | def diffrevs(self, node1, node2): |
|
426 | 426 | changed, added = [], [] |
|
427 | 427 | |
|
428 | 428 | change = self.changelog.read(node1) |
|
429 | 429 | mf1 = self.manifest.read(change[0]) |
|
430 | 430 | change = self.changelog.read(node2) |
|
431 | 431 | mf2 = self.manifest.read(change[0]) |
|
432 | 432 | |
|
433 | 433 | for fn in mf2: |
|
434 | 434 | if mf1.has_key(fn): |
|
435 | 435 | if mf1[fn] != mf2[fn]: |
|
436 | 436 | changed.append(fn) |
|
437 | 437 | del mf1[fn] |
|
438 | 438 | else: |
|
439 | 439 | added.append(fn) |
|
440 | 440 | |
|
441 | 441 | deleted = mf1.keys() |
|
442 | 442 | deleted.sort() |
|
443 | 443 | |
|
444 | 444 | return (changed, added, deleted) |
|
445 | 445 | |
|
446 | 446 | def add(self, list): |
|
447 | 447 | self.dircache.taint(list) |
|
448 | 448 | |
|
449 | 449 | def remove(self, list): |
|
450 | 450 | dl = self.opener("to-remove", "a") |
|
451 | 451 | for f in list: |
|
452 | 452 | dl.write(f + "\n") |
|
453 | 453 | |
|
454 | 454 | def branches(self, nodes): |
|
455 | 455 | if not nodes: nodes = [self.changelog.tip()] |
|
456 | 456 | b = [] |
|
457 | 457 | for n in nodes: |
|
458 | 458 | t = n |
|
459 | 459 | while n: |
|
460 | 460 | p = self.changelog.parents(n) |
|
461 | 461 | if p[1] != nullid or p[0] == nullid: |
|
462 | 462 | b.append((t, n, p[0], p[1])) |
|
463 | 463 | break |
|
464 | 464 | n = p[0] |
|
465 | 465 | return b |
|
466 | 466 | |
|
467 | 467 | def between(self, pairs): |
|
468 | 468 | r = [] |
|
469 | 469 | |
|
470 | 470 | for top, bottom in pairs: |
|
471 | 471 | n, l, i = top, [], 0 |
|
472 | 472 | f = 1 |
|
473 | 473 | |
|
474 | 474 | while n != bottom: |
|
475 | 475 | p = self.changelog.parents(n)[0] |
|
476 | 476 | if i == f: |
|
477 | 477 | l.append(n) |
|
478 | 478 | f = f * 2 |
|
479 | 479 | n = p |
|
480 | 480 | i += 1 |
|
481 | 481 | |
|
482 | 482 | r.append(l) |
|
483 | 483 | |
|
484 | 484 | return r |
|
485 | 485 | |
|
486 | 486 | def newer(self, nodes): |
|
487 | 487 | m = {} |
|
488 | 488 | nl = [] |
|
489 | 489 | pm = {} |
|
490 | 490 | cl = self.changelog |
|
491 | 491 | t = l = cl.count() |
|
492 | 492 | |
|
493 | 493 | # find the lowest numbered node |
|
494 | 494 | for n in nodes: |
|
495 | 495 | l = min(l, cl.rev(n)) |
|
496 | 496 | m[n] = 1 |
|
497 | 497 | |
|
498 | 498 | for i in xrange(l, t): |
|
499 | 499 | n = cl.node(i) |
|
500 | 500 | if n in m: # explicitly listed |
|
501 | 501 | pm[n] = 1 |
|
502 | 502 | nl.append(n) |
|
503 | 503 | continue |
|
504 | 504 | for p in cl.parents(n): |
|
505 | 505 | if p in pm: # parent listed |
|
506 | 506 | pm[n] = 1 |
|
507 | 507 | nl.append(n) |
|
508 | 508 | break |
|
509 | 509 | |
|
510 | 510 | return nl |
|
511 | 511 | |
|
512 | 512 | def getchangegroup(self, remote): |
|
513 | 513 | tip = remote.branches([])[0] |
|
514 | 514 | self.ui.debug("remote tip branch is %s:%s\n" % |
|
515 | 515 | (short(tip[0]), short(tip[1]))) |
|
516 | 516 | m = self.changelog.nodemap |
|
517 | 517 | unknown = [tip] |
|
518 | 518 | search = [] |
|
519 | 519 | fetch = [] |
|
520 | 520 | |
|
521 | 521 | if tip[0] in m: |
|
522 | 522 | self.ui.note("nothing to do!\n") |
|
523 | 523 | return None |
|
524 | 524 | |
|
525 | 525 | while unknown: |
|
526 | 526 | n = unknown.pop(0) |
|
527 | 527 | if n == nullid: break |
|
528 | 528 | if n[1] and n[1] in m: # do we know the base? |
|
529 | 529 | self.ui.debug("found incomplete branch %s\n" % short(n[1])) |
|
530 | 530 | search.append(n) # schedule branch range for scanning |
|
531 | 531 | else: |
|
532 | 532 | if n[2] in m and n[3] in m: |
|
533 | 533 | if n[1] not in fetch: |
|
534 | 534 | self.ui.debug("found new changeset %s\n" % |
|
535 | 535 | short(n[1])) |
|
536 | 536 | fetch.append(n[1]) # earliest unknown |
|
537 | 537 | continue |
|
538 | 538 | for b in remote.branches([n[2], n[3]]): |
|
539 | 539 | if b[0] not in m: |
|
540 | 540 | unknown.append(b) |
|
541 | 541 | |
|
542 | 542 | while search: |
|
543 | 543 | n = search.pop(0) |
|
544 | 544 | l = remote.between([(n[0], n[1])])[0] |
|
545 | 545 | p = n[0] |
|
546 | 546 | f = 1 |
|
547 | 547 | for i in l + [n[1]]: |
|
548 | 548 | if i in m: |
|
549 | 549 | if f <= 2: |
|
550 | 550 | self.ui.debug("found new branch changeset %s\n" % |
|
551 | 551 | short(p)) |
|
552 | 552 | fetch.append(p) |
|
553 | 553 | else: |
|
554 | 554 | self.ui.debug("narrowed branch search to %s:%s\n" |
|
555 | 555 | % (short(p), short(i))) |
|
556 | 556 | search.append((p, i)) |
|
557 | 557 | break |
|
558 | 558 | p, f = i, f * 2 |
|
559 | 559 | |
|
560 | 560 | for f in fetch: |
|
561 | 561 | if f in m: |
|
562 | 562 | raise "already have", short(f[:4]) |
|
563 | 563 | |
|
564 | 564 | self.ui.note("adding new changesets starting at " + |
|
565 | 565 | " ".join([short(f) for f in fetch]) + "\n") |
|
566 | 566 | |
|
567 | 567 | return remote.changegroup(fetch) |
|
568 | 568 | |
|
569 | 569 | def changegroup(self, basenodes): |
|
570 | 570 | nodes = self.newer(basenodes) |
|
571 | 571 | |
|
572 | 572 | # construct the link map |
|
573 | 573 | linkmap = {} |
|
574 | 574 | for n in nodes: |
|
575 | 575 | linkmap[self.changelog.rev(n)] = n |
|
576 | 576 | |
|
577 | 577 | # construct a list of all changed files |
|
578 | 578 | changed = {} |
|
579 | 579 | for n in nodes: |
|
580 | 580 | c = self.changelog.read(n) |
|
581 | 581 | for f in c[3]: |
|
582 | 582 | changed[f] = 1 |
|
583 | 583 | changed = changed.keys() |
|
584 | 584 | changed.sort() |
|
585 | 585 | |
|
586 | 586 | # the changegroup is changesets + manifests + all file revs |
|
587 | 587 | revs = [ self.changelog.rev(n) for n in nodes ] |
|
588 | 588 | |
|
589 | 589 | yield self.changelog.group(linkmap) |
|
590 | 590 | yield self.manifest.group(linkmap) |
|
591 | 591 | |
|
592 | 592 | for f in changed: |
|
593 | 593 | g = self.file(f).group(linkmap) |
|
594 | 594 | if not g: raise "couldn't find change to %s" % f |
|
595 | 595 | l = struct.pack(">l", len(f)) |
|
596 | 596 | yield "".join([l, f, g]) |
|
597 | 597 | |
|
598 | 598 | def addchangegroup(self, generator): |
|
599 | 599 | class genread: |
|
600 | 600 | def __init__(self, generator): |
|
601 | 601 | self.g = generator |
|
602 | 602 | self.buf = "" |
|
603 | 603 | def read(self, l): |
|
604 | 604 | while l > len(self.buf): |
|
605 | 605 | try: |
|
606 | 606 | self.buf += self.g.next() |
|
607 | 607 | except StopIteration: |
|
608 | 608 | break |
|
609 | 609 | d, self.buf = self.buf[:l], self.buf[l:] |
|
610 | 610 | return d |
|
611 | 611 | |
|
612 | 612 | if not generator: return |
|
613 | 613 | source = genread(generator) |
|
614 | 614 | |
|
615 | 615 | def getchunk(add = 0): |
|
616 | 616 | d = source.read(4) |
|
617 | 617 | if not d: return "" |
|
618 | 618 | l = struct.unpack(">l", d)[0] |
|
619 | 619 | return source.read(l - 4 + add) |
|
620 | 620 | |
|
621 | 621 | tr = self.transaction() |
|
622 | 622 | simple = True |
|
623 | 623 | |
|
624 | 624 | self.ui.status("adding changesets\n") |
|
625 | 625 | # pull off the changeset group |
|
626 | 626 | def report(x): |
|
627 | 627 | self.ui.debug("add changeset %s\n" % short(x)) |
|
628 | 628 | return self.changelog.count() |
|
629 | 629 | |
|
630 | 630 | csg = getchunk() |
|
631 | 631 | co = self.changelog.tip() |
|
632 | 632 | cn = self.changelog.addgroup(csg, report, tr) |
|
633 | 633 | |
|
634 | 634 | self.ui.status("adding manifests\n") |
|
635 | 635 | # pull off the manifest group |
|
636 | 636 | mfg = getchunk() |
|
637 | 637 | mm = self.manifest.tip() |
|
638 | 638 | mo = self.manifest.addgroup(mfg, lambda x: self.changelog.rev(x), tr) |
|
639 | 639 | |
|
640 | 640 | # do we need a resolve? |
|
641 | 641 | if self.changelog.ancestor(co, cn) != co: |
|
642 | 642 | simple = False |
|
643 | 643 | resolverev = self.changelog.count() |
|
644 | 644 | |
|
645 | 645 | # process the files |
|
646 | 646 | self.ui.status("adding files\n") |
|
647 | 647 | new = {} |
|
648 | 648 | while 1: |
|
649 | 649 | f = getchunk(4) |
|
650 | 650 | if not f: break |
|
651 | 651 | fg = getchunk() |
|
652 | 652 | self.ui.debug("adding %s revisions\n" % f) |
|
653 | 653 | fl = self.file(f) |
|
654 | 654 | o = fl.tip() |
|
655 | 655 | n = fl.addgroup(fg, lambda x: self.changelog.rev(x), tr) |
|
656 | 656 | if not simple: |
|
657 | 657 | if o == n: continue |
|
658 | 658 | # this file has changed between branches, so it must be |
|
659 | 659 | # represented in the merge changeset |
|
660 | 660 | new[f] = self.merge3(fl, f, o, n, tr, resolverev) |
|
661 | 661 | |
|
662 | 662 | # For simple merges, we don't need to resolve manifests or changesets |
|
663 | 663 | if simple: |
|
664 | 664 | self.ui.debug("simple merge, skipping resolve\n") |
|
665 | 665 | tr.close() |
|
666 | 666 | return |
|
667 | 667 | |
|
668 | 668 | # resolve the manifest to point to all the merged files |
|
669 | 669 | self.ui.status("resolving manifests\n") |
|
670 | 670 | ma = self.manifest.ancestor(mm, mo) |
|
671 | 671 | omap = self.manifest.read(mo) # other |
|
672 | 672 | amap = self.manifest.read(ma) # ancestor |
|
673 | 673 | mmap = self.manifest.read(mm) # mine |
|
674 | 674 | self.ui.debug("ancestor %s local %s other %s\n" % |
|
675 | 675 | (short(ma), short(mm), short(mo))) |
|
676 | 676 | nmap = {} |
|
677 | 677 | |
|
678 | 678 | for f, mid in mmap.iteritems(): |
|
679 | 679 | if f in omap: |
|
680 | 680 | if mid != omap[f]: |
|
681 | 681 | self.ui.debug("%s versions differ\n" % f) |
|
682 | 682 | if f in new: self.ui.debug("%s updated in resolve\n" % f) |
|
683 | 683 | # use merged version or local version |
|
684 | 684 | nmap[f] = new.get(f, mid) |
|
685 | 685 | else: |
|
686 | 686 | nmap[f] = mid # keep ours |
|
687 | 687 | del omap[f] |
|
688 | 688 | elif f in amap: |
|
689 | 689 | if mid != amap[f]: |
|
690 | 690 | self.ui.debug("local changed %s which other deleted\n" % f) |
|
691 | 691 | pass # we should prompt here |
|
692 | 692 | else: |
|
693 | 693 | self.ui.debug("other deleted %s\n" % f) |
|
694 | 694 | pass # other deleted it |
|
695 | 695 | else: |
|
696 | 696 | self.ui.debug("local created %s\n" %f) |
|
697 | 697 | nmap[f] = mid # we created it |
|
698 | 698 | |
|
699 | 699 | del mmap |
|
700 | 700 | |
|
701 | 701 | for f, oid in omap.iteritems(): |
|
702 | 702 | if f in amap: |
|
703 | 703 | if oid != amap[f]: |
|
704 | 704 | self.ui.debug("other changed %s which we deleted\n" % f) |
|
705 | 705 | pass # this is the nasty case, we should prompt |
|
706 | 706 | else: |
|
707 | 707 | pass # probably safe |
|
708 | 708 | else: |
|
709 | 709 | self.ui.debug("remote created %s\n" % f) |
|
710 | 710 | nmap[f] = new.get(f, oid) # remote created it |
|
711 | 711 | |
|
712 | 712 | del omap |
|
713 | 713 | del amap |
|
714 | 714 | |
|
715 | 715 | node = self.manifest.add(nmap, tr, resolverev, mm, mo) |
|
716 | 716 | |
|
717 | 717 | # Now all files and manifests are merged, we add the changed files |
|
718 | 718 | # and manifest id to the changelog |
|
719 | 719 | self.ui.status("committing merge changeset\n") |
|
720 | 720 | new = new.keys() |
|
721 | 721 | new.sort() |
|
722 | 722 | if co == cn: cn = -1 |
|
723 | 723 | |
|
724 | 724 | edittext = "\nHG: merge resolve\n" + \ |
|
725 | 725 | "".join(["HG: changed %s\n" % f for f in new]) |
|
726 | 726 | edittext = self.ui.edit(edittext) |
|
727 | 727 | n = self.changelog.add(node, new, edittext, tr, co, cn) |
|
728 | 728 | |
|
729 | 729 | tr.close() |
|
730 | 730 | |
|
731 | def merge3(self, fl, fn, my, other, transaction, link): | |
|
732 | """perform a 3-way merge and append the result""" | |
|
733 | ||
|
734 | def temp(prefix, node): | |
|
735 | pre = "%s~%s." % (os.path.basename(fn), prefix) | |
|
736 | (fd, name) = tempfile.mkstemp("", pre) | |
|
737 | f = os.fdopen(fd, "w") | |
|
738 | f.write(fl.revision(node)) | |
|
739 | f.close() | |
|
740 | return name | |
|
741 | ||
|
742 | base = fl.ancestor(my, other) | |
|
743 | self.ui.note("resolving %s\n" % fn) | |
|
744 | self.ui.debug("local %s remote %s ancestor %s\n" % | |
|
745 | (short(my), short(other), short(base))) | |
|
746 | ||
|
747 | if my == base: | |
|
748 | text = fl.revision(other) | |
|
749 | else: | |
|
750 | a = temp("local", my) | |
|
751 | b = temp("remote", other) | |
|
752 | c = temp("parent", base) | |
|
753 | ||
|
754 | cmd = os.environ["HGMERGE"] | |
|
755 | self.ui.debug("invoking merge with %s\n" % cmd) | |
|
756 | r = os.system("%s %s %s %s" % (cmd, a, b, c)) | |
|
757 | if r: | |
|
758 | raise "Merge failed!" | |
|
759 | ||
|
760 | text = open(a).read() | |
|
761 | os.unlink(a) | |
|
762 | os.unlink(b) | |
|
763 | os.unlink(c) | |
|
764 | ||
|
765 | return fl.addrevision(text, transaction, link, my, other) | |
|
766 | ||
|
731 | 767 | class remoterepository: |
|
732 | 768 | def __init__(self, ui, path): |
|
733 | 769 | self.url = path.replace("hg://", "http://", 1) |
|
734 | 770 | self.ui = ui |
|
735 | 771 | |
|
736 | 772 | def do_cmd(self, cmd, **args): |
|
737 | 773 | self.ui.debug("sending %s command\n" % cmd) |
|
738 | 774 | q = {"cmd": cmd} |
|
739 | 775 | q.update(args) |
|
740 | 776 | qs = urllib.urlencode(q) |
|
741 | 777 | cu = "%s?%s" % (self.url, qs) |
|
742 | 778 | return urllib.urlopen(cu) |
|
743 | 779 | |
|
744 | 780 | def branches(self, nodes): |
|
745 | 781 | n = " ".join(map(hex, nodes)) |
|
746 | 782 | d = self.do_cmd("branches", nodes=n).read() |
|
747 | 783 | br = [ map(bin, b.split(" ")) for b in d.splitlines() ] |
|
748 | 784 | return br |
|
749 | 785 | |
|
750 | 786 | def between(self, pairs): |
|
751 | 787 | n = "\n".join(["-".join(map(hex, p)) for p in pairs]) |
|
752 | 788 | d = self.do_cmd("between", pairs=n).read() |
|
753 | 789 | p = [ map(bin, l.split(" ")) for l in d.splitlines() ] |
|
754 | 790 | return p |
|
755 | 791 | |
|
756 | 792 | def changegroup(self, nodes): |
|
757 | 793 | n = " ".join(map(hex, nodes)) |
|
758 | 794 | zd = zlib.decompressobj() |
|
759 | 795 | f = self.do_cmd("changegroup", roots=n) |
|
760 | 796 | while 1: |
|
761 | 797 | d = f.read(4096) |
|
762 | 798 | if not d: |
|
763 | 799 | yield zd.flush() |
|
764 | 800 | break |
|
765 | 801 | yield zd.decompress(d) |
|
766 | 802 | |
|
767 | 803 | def repository(ui, path=None, create=0): |
|
768 | 804 | if path and path[:5] == "hg://": |
|
769 | 805 | return remoterepository(ui, path) |
|
770 | 806 | else: |
|
771 | 807 | return localrepository(ui, path, create) |
|
772 | 808 | |
|
773 | 809 | class ui: |
|
774 | 810 | def __init__(self, verbose=False, debug=False, quiet=False): |
|
775 | 811 | self.quiet = quiet and not verbose and not debug |
|
776 | 812 | self.verbose = verbose or debug |
|
777 | 813 | self.debugflag = debug |
|
778 | 814 | def write(self, *args): |
|
779 | 815 | for a in args: |
|
780 | 816 | sys.stdout.write(str(a)) |
|
781 | 817 | def prompt(self, msg, pat): |
|
782 | 818 | while 1: |
|
783 | 819 | sys.stdout.write(msg) |
|
784 | 820 | r = sys.stdin.readline()[:-1] |
|
785 | 821 | if re.match(pat, r): |
|
786 | 822 | return r |
|
787 | 823 | def status(self, *msg): |
|
788 | 824 | if not self.quiet: self.write(*msg) |
|
789 | 825 | def warn(self, msg): |
|
790 | 826 | self.write(*msg) |
|
791 | 827 | def note(self, msg): |
|
792 | 828 | if self.verbose: self.write(*msg) |
|
793 | 829 | def debug(self, msg): |
|
794 | 830 | if self.debugflag: self.write(*msg) |
|
795 | 831 | def edit(self, text): |
|
796 | 832 | (fd, name) = tempfile.mkstemp("hg") |
|
797 | 833 | f = os.fdopen(fd, "w") |
|
798 | 834 | f.write(text) |
|
799 | 835 | f.close() |
|
800 | 836 | |
|
801 | 837 | editor = os.environ.get("EDITOR", "vi") |
|
802 | 838 | r = os.system("%s %s" % (editor, name)) |
|
803 | 839 | if r: |
|
804 | 840 | raise "Edit failed!" |
|
805 | 841 | |
|
806 | 842 | t = open(name).read() |
|
807 | 843 | t = re.sub("(?m)^HG:.*\n", "", t) |
|
808 | 844 | |
|
809 | 845 | return t |
|
810 | 846 | |
|
811 | 847 | |
|
812 | 848 | class httprangereader: |
|
813 | 849 | def __init__(self, url): |
|
814 | 850 | self.url = url |
|
815 | 851 | self.pos = 0 |
|
816 | 852 | def seek(self, pos): |
|
817 | 853 | self.pos = pos |
|
818 | 854 | def read(self, bytes=None): |
|
819 | 855 | opener = urllib2.build_opener(byterange.HTTPRangeHandler()) |
|
820 | 856 | urllib2.install_opener(opener) |
|
821 | 857 | req = urllib2.Request(self.url) |
|
822 | 858 | end = '' |
|
823 | 859 | if bytes: end = self.pos + bytes |
|
824 | 860 | req.add_header('Range', 'bytes=%d-%s' % (self.pos, end)) |
|
825 | 861 | f = urllib2.urlopen(req) |
|
826 | 862 | return f.read() |
General Comments 0
You need to be logged in to leave comments.
Login now