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