##// END OF EJS Templates
convert: fetch svn changes on demand (in batches)
Brendan Cully -
r4772:69548a9d default
parent child Browse files
Show More
@@ -1,524 +1,527 b''
1 1 # Subversion 1.4/1.5 Python API backend
2 2 #
3 3 # Copyright(C) 2007 Daniel Holth et al
4 4
5 5 import pprint
6 6 import locale
7 7
8 8 from mercurial import util
9 9
10 10 # Subversion stuff. Works best with very recent Python SVN bindings
11 11 # e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing
12 12 # these bindings.
13 13
14 14 from cStringIO import StringIO
15 15
16 16 from common import NoRepo, commit, converter_source
17 17
18 18 try:
19 19 from svn.core import SubversionException, Pool
20 20 import svn.core
21 21 import svn.ra
22 22 import svn.delta
23 23 import svn
24 24 import transport
25 25 except ImportError:
26 26 pass
27 27
28 28 class CompatibilityException(Exception): pass
29 29
30 nbRevisionsPerFetch = 50
30 LOG_BATCH_SIZE = 50
31 31
32 32 class svn_entry(object):
33 33 """Emulate a Subversion path change."""
34 34 __slots__ = ['path', 'copyfrom_path', 'copyfrom_rev', 'action']
35 35 def __init__(self, entry):
36 36 self.copyfrom_path = entry.copyfrom_path
37 37 self.copyfrom_rev = entry.copyfrom_rev
38 38 self.action = entry.action
39 39
40 40 def __str__(self):
41 41 return "%s %s %s" % (self.action, self.copyfrom_path, self.copyfrom_rev)
42 42
43 43 def __repr__(self):
44 44 return self.__str__()
45 45
46 46 class svn_paths(object):
47 47 """Emulate a Subversion ordered dictionary of changed paths."""
48 48 __slots__ = ['values', 'order']
49 49 def __init__(self, orig_paths):
50 50 self.order = []
51 51 self.values = {}
52 52 if hasattr(orig_paths, 'keys'):
53 53 self.order = sorted(orig_paths.keys())
54 54 self.values.update(orig_paths)
55 55 return
56 56 if not orig_paths:
57 57 return
58 58 for path in orig_paths:
59 59 self.order.append(path)
60 60 self.values[path] = svn_entry(orig_paths[path])
61 61 self.order.sort() # maybe the order it came in isn't so great...
62 62
63 63 def __iter__(self):
64 64 return iter(self.order)
65 65
66 66 def __getitem__(self, key):
67 67 return self.values[key]
68 68
69 69 def __str__(self):
70 70 s = "{\n"
71 71 for path in self.order:
72 72 s += "'%s': %s,\n" % (path, self.values[path])
73 73 s += "}"
74 74 return s
75 75
76 76 def __repr__(self):
77 77 return self.__str__()
78 78
79 79 # SVN conversion code stolen from bzr-svn and tailor
80 80 class convert_svn(converter_source):
81 81 def __init__(self, ui, url, rev=None):
82 82 try:
83 83 SubversionException
84 84 except NameError:
85 85 msg = 'subversion python bindings could not be loaded\n'
86 86 ui.warn(msg)
87 87 raise NoRepo(msg)
88 88
89 89 self.ui = ui
90 90 self.encoding = locale.getpreferredencoding()
91 91 latest = None
92 92 if rev:
93 93 try:
94 94 latest = int(rev)
95 95 except ValueError:
96 96 raise util.Abort('svn: revision %s is not an integer' % rev)
97 97 try:
98 98 # Support file://path@rev syntax. Useful e.g. to convert
99 99 # deleted branches.
100 100 url, latest = url.rsplit("@", 1)
101 101 latest = int(latest)
102 102 except ValueError, e:
103 103 pass
104 104 self.url = url
105 105 self.encoding = 'UTF-8' # Subversion is always nominal UTF-8
106 106 try:
107 107 self.transport = transport.SvnRaTransport(url = url)
108 108 self.ra = self.transport.ra
109 109 self.base = svn.ra.get_repos_root(self.ra)
110 110 self.module = self.url[len(self.base):]
111 111 self.modulemap = {} # revision, module
112 112 self.commits = {}
113 113 self.files = {}
114 114 self.uuid = svn.ra.get_uuid(self.ra).decode(self.encoding)
115 115 except SubversionException, e:
116 116 raise NoRepo("couldn't open SVN repo %s" % url)
117 117
118 118 try:
119 119 self.get_blacklist()
120 120 except IOError, e:
121 121 pass
122 122
123 123 if not latest:
124 124 latest = svn.ra.get_latest_revnum(self.ra)
125 125 dirent = svn.ra.stat(self.ra, self.module, latest)
126 126 self.last_changed = dirent.created_rev
127 127
128 128 self.head = self.rev(self.last_changed)
129 129
130 # Should lazily fetch revisions in batches of, say, 1,000...:
131 self._fetch_revisions(from_revnum=self.last_changed, to_revnum=0)
132
133 130 def rev(self, revnum):
134 131 return (u"svn:%s%s@%s" % (self.uuid, self.module, revnum)).decode(self.encoding)
135 132
136 133 def get_blacklist(self):
137 134 """Avoid certain revision numbers.
138 135 It is not uncommon for two nearby revisions to cancel each other
139 136 out, e.g. 'I copied trunk into a subdirectory of itself instead
140 137 of making a branch'. The converted repository is significantly
141 138 smaller if we ignore such revisions."""
142 139 self.blacklist = set()
143 140 blacklist = self.blacklist
144 141 for line in file("blacklist.txt", "r"):
145 142 if not line.startswith("#"):
146 143 try:
147 144 svn_rev = int(line.strip())
148 145 blacklist.add(svn_rev)
149 146 except ValueError, e:
150 147 pass # not an integer or a comment
151 148
152 149 def is_blacklisted(self, svn_rev):
153 150 return svn_rev in self.blacklist
154 151
155 152 def reparent(self, module):
156 153 svn_url = self.base + module
157 154 self.ui.debug("reparent to %s\n" % svn_url.encode(self.encoding))
158 155 svn.ra.reparent(self.ra, svn_url.encode(self.encoding))
159 156
160 157 def _fetch_revisions(self, from_revnum = 0, to_revnum = 347, pb=None):
161 self.parent_cset = None
162 self.child_cset = None
163
158 if not hasattr(self, 'child_rev'):
159 self.child_rev = from_revnum
160 self.child_cset = self.commits.get(self.child_rev)
161 else:
162 self.commits[self.child_rev] = self.child_cset
163
164 164 self.ui.debug('Fetching revisions %d to %d\n' % (from_revnum, to_revnum))
165 165
166 166 def get_entry_from_path(path, module=self.module):
167 167 # Given the repository url of this wc, say
168 168 # "http://server/plone/CMFPlone/branches/Plone-2_0-branch"
169 169 # extract the "entry" portion (a relative path) from what
170 170 # svn log --xml says, ie
171 171 # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py"
172 172 # that is to say "tests/PloneTestCase.py"
173 173
174 174 if path.startswith(module):
175 175 relative = path[len(module):]
176 176 if relative.startswith('/'):
177 177 return relative[1:]
178 178 else:
179 179 return relative
180 180
181 181 # The path is outside our tracked tree...
182 182 self.ui.debug('Ignoring %r since it is not under %r\n' % (path, module))
183 183 return None
184 184
185 185 received = []
186 186 def rcvr(*arg, **args):
187 187 orig_paths, revnum, author, date, message, pool = arg
188 188 new_orig_paths = svn_paths(orig_paths)
189 189 rcvr2(new_orig_paths, revnum, author, date, message, pool)
190 190
191 191 def rcvr2(orig_paths, revnum, author, date, message, pool, better_paths = None):
192 192 if not self.is_blacklisted(revnum):
193 193 received.append((orig_paths, revnum, author, date, message))
194 194
195 195 def after_received(orig_paths, revnum, author, date, message):
196 196 if revnum in self.modulemap:
197 197 new_module = self.modulemap[revnum]
198 198 if new_module != self.module:
199 199 self.module = new_module
200 200 self.reparent(self.module)
201 201
202 202 copyfrom = {} # Map of entrypath, revision for finding source of deleted revisions.
203 203 copies = {}
204 204 entries = []
205 205 self.ui.debug("Parsing revision %d\n" % revnum)
206 206 if orig_paths is not None:
207 207 rev = self.rev(revnum)
208 208 try:
209 209 branch = self.module.split("/")[-1]
210 210 if branch == 'trunk':
211 211 branch = ''
212 212 except IndexError:
213 213 branch = None
214 214
215 215 for path in orig_paths:
216 216 # self.ui.write("path %s\n" % path)
217 217 if path == self.module: # Follow branching back in history
218 218 ent = orig_paths[path]
219 219 if ent:
220 220 if ent.copyfrom_path:
221 221 self.modulemap[ent.copyfrom_rev] = ent.copyfrom_path
222 222 else:
223 223 self.ui.debug("No copyfrom path, don't know what to do.\n")
224 224 # Maybe it was added and there is no more history.
225 225 entrypath = get_entry_from_path(path, module=self.module)
226 226 # self.ui.write("entrypath %s\n" % entrypath)
227 227 if not entrypath:
228 228 # Outside our area of interest
229 229 self.ui.debug("boring@%s: %s\n" % (revnum, path))
230 230 continue
231 231 entry = entrypath.decode(self.encoding)
232 232 ent = orig_paths[path]
233 233
234 234 kind = svn.ra.check_path(self.ra, entrypath, revnum)
235 235 if kind == svn.core.svn_node_file:
236 236 if ent.copyfrom_path:
237 237 copyfrom_path = get_entry_from_path(ent.copyfrom_path)
238 238 if copyfrom_path:
239 239 self.ui.debug("Copied to %s from %s@%s\n" % (entry, copyfrom_path, ent.copyfrom_rev))
240 240 # It's probably important for hg that the source
241 241 # exists in the revision's parent, not just the
242 242 # ent.copyfrom_rev
243 243 fromkind = svn.ra.check_path(self.ra, copyfrom_path, ent.copyfrom_rev)
244 244 if fromkind != 0:
245 245 copies[self.recode(entry)] = self.recode(copyfrom_path)
246 246 entries.append(self.recode(entry))
247 247 elif kind == 0: # gone, but had better be a deleted *file*
248 248 self.ui.debug("gone from %s\n" % ent.copyfrom_rev)
249 249
250 250 fromrev = revnum - 1
251 251 # might always need to be revnum - 1 in these 3 lines?
252 252 old_module = self.modulemap.get(fromrev, self.module)
253 253 basepath = old_module + "/" + get_entry_from_path(path, module=self.module)
254 254 entrypath = old_module + "/" + get_entry_from_path(path, module=self.module)
255 255
256 256 def lookup_parts(p):
257 257 rc = None
258 258 parts = p.split("/")
259 259 for i in range(len(parts)):
260 260 part = "/".join(parts[:i])
261 261 info = part, copyfrom.get(part, None)
262 262 if info[1] is not None:
263 263 self.ui.debug("Found parent directory %s\n" % info)
264 264 rc = info
265 265 return rc
266 266
267 267 self.ui.debug("base, entry %s %s\n" % (basepath, entrypath))
268 268
269 269 frompath, froment = lookup_parts(entrypath) or (None, revnum - 1)
270 270
271 271 # need to remove fragment from lookup_parts and replace with copyfrom_path
272 272 if frompath is not None:
273 273 self.ui.debug("munge-o-matic\n")
274 274 self.ui.debug(entrypath + '\n')
275 275 self.ui.debug(entrypath[len(frompath):] + '\n')
276 276 entrypath = froment.copyfrom_path + entrypath[len(frompath):]
277 277 fromrev = froment.copyfrom_rev
278 278 self.ui.debug("Info: %s %s %s %s\n" % (frompath, froment, ent, entrypath))
279 279
280 280 fromkind = svn.ra.check_path(self.ra, entrypath, fromrev)
281 281 if fromkind == svn.core.svn_node_file: # a deleted file
282 282 entries.append(self.recode(entry))
283 283 else:
284 284 # print "Deleted/moved non-file:", revnum, path, ent
285 285 # children = self._find_children(path, revnum - 1)
286 286 # print "find children %s@%d from %d action %s" % (path, revnum, ent.copyfrom_rev, ent.action)
287 287 # Sometimes this is tricky. For example: in
288 288 # The Subversion Repository revision 6940 a dir
289 289 # was copied and one of its files was deleted
290 290 # from the new location in the same commit. This
291 291 # code can't deal with that yet.
292 292 if ent.action == 'C':
293 293 children = self._find_children(path, fromrev)
294 294 else:
295 295 oroot = entrypath.strip('/')
296 296 nroot = path.strip('/')
297 297 children = self._find_children(oroot, fromrev)
298 298 children = [s.replace(oroot,nroot) for s in children]
299 299 # Mark all [files, not directories] as deleted.
300 300 for child in children:
301 301 # Can we move a child directory and its
302 302 # parent in the same commit? (probably can). Could
303 303 # cause problems if instead of revnum -1,
304 304 # we have to look in (copyfrom_path, revnum - 1)
305 305 entrypath = get_entry_from_path("/" + child, module=old_module)
306 306 if entrypath:
307 307 entry = self.recode(entrypath.decode(self.encoding))
308 308 if entry in copies:
309 309 # deleted file within a copy
310 310 del copies[entry]
311 311 else:
312 312 entries.append(entry)
313 313 elif kind == svn.core.svn_node_dir:
314 314 # Should probably synthesize normal file entries
315 315 # and handle as above to clean up copy/rename handling.
316 316
317 317 # If the directory just had a prop change,
318 318 # then we shouldn't need to look for its children.
319 319 # Also this could create duplicate entries. Not sure
320 320 # whether this will matter. Maybe should make entries a set.
321 321 # print "Changed directory", revnum, path, ent.action, ent.copyfrom_path, ent.copyfrom_rev
322 322 # This will fail if a directory was copied
323 323 # from another branch and then some of its files
324 324 # were deleted in the same transaction.
325 325 children = self._find_children(path, revnum)
326 326 children.sort()
327 327 for child in children:
328 328 # Can we move a child directory and its
329 329 # parent in the same commit? (probably can). Could
330 330 # cause problems if instead of revnum -1,
331 331 # we have to look in (copyfrom_path, revnum - 1)
332 332 entrypath = get_entry_from_path("/" + child, module=self.module)
333 333 # print child, self.module, entrypath
334 334 if entrypath:
335 335 # Need to filter out directories here...
336 336 kind = svn.ra.check_path(self.ra, entrypath, revnum)
337 337 if kind != svn.core.svn_node_dir:
338 338 entries.append(self.recode(entrypath))
339 339
340 340 # Copies here (must copy all from source)
341 341 # Probably not a real problem for us if
342 342 # source does not exist
343 343
344 344 # Can do this with the copy command "hg copy"
345 345 # if ent.copyfrom_path:
346 346 # copyfrom_entry = get_entry_from_path(ent.copyfrom_path.decode(self.encoding),
347 347 # module=self.module)
348 348 # copyto_entry = entrypath
349 349 #
350 350 # print "copy directory", copyfrom_entry, 'to', copyto_entry
351 351 #
352 352 # copies.append((copyfrom_entry, copyto_entry))
353 353
354 354 if ent.copyfrom_path:
355 355 copyfrom_path = ent.copyfrom_path.decode(self.encoding)
356 356 copyfrom_entry = get_entry_from_path(copyfrom_path, module=self.module)
357 357 if copyfrom_entry:
358 358 copyfrom[path] = ent
359 359 self.ui.debug("mark %s came from %s\n" % (path, copyfrom[path]))
360 360
361 361 # Good, /probably/ a regular copy. Really should check
362 362 # to see whether the parent revision actually contains
363 363 # the directory in question.
364 364 children = self._find_children(self.recode(copyfrom_path), ent.copyfrom_rev)
365 365 children.sort()
366 366 for child in children:
367 367 entrypath = get_entry_from_path("/" + child, module=self.module)
368 368 if entrypath:
369 369 entry = entrypath.decode(self.encoding)
370 370 # print "COPY COPY From", copyfrom_entry, entry
371 371 copyto_path = path + entry[len(copyfrom_entry):]
372 372 copyto_entry = get_entry_from_path(copyto_path, module=self.module)
373 373 # print "COPY", entry, "COPY To", copyto_entry
374 374 copies[self.recode(copyto_entry)] = self.recode(entry)
375 375 # copy from quux splort/quuxfile
376 376
377 377 self.modulemap[revnum] = self.module # track backwards in time
378 378 # a list of (filename, id) where id lets us retrieve the file.
379 379 # eg in git, id is the object hash. for svn it'll be the
380 380 self.files[rev] = zip(entries, [rev] * len(entries))
381 381
382 382 # Example SVN datetime. Includes microseconds.
383 383 # ISO-8601 conformant
384 384 # '2007-01-04T17:35:00.902377Z'
385 385 date = util.parsedate(date[:18] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
386 386
387 387 log = message and self.recode(message)
388 388 author = author and self.recode(author) or ''
389 389
390 390 cset = commit(author=author,
391 date=util.datestr(date),
392 desc=log,
393 parents=[],
394 copies=copies,
395 branch=branch)
391 date=util.datestr(date),
392 desc=log,
393 parents=[],
394 copies=copies,
395 branch=branch)
396 396
397 if self.child_cset is not None:
397 if self.child_cset and self.child_rev != rev:
398 398 self.child_cset.parents = [rev]
399
399 self.commits[self.child_rev] = self.child_cset
400 400 self.child_cset = cset
401
402 self.commits[rev] = cset
401 self.child_rev = rev
403 402
404 403 try:
405 404 discover_changed_paths = True
406 405 strict_node_history = False
407 406 svn.ra.get_log(self.ra, [self.module], from_revnum, to_revnum,
408 407 0, discover_changed_paths, strict_node_history, rcvr)
409 408 for args in received:
410 409 after_received(*args)
411 410 self.last_revnum = to_revnum
412 411 except SubversionException, (_, num):
413 412 if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION:
414 413 raise NoSuchRevision(branch=self,
415 414 revision="Revision number %d" % to_revnum)
416 415 raise
417 416
418 417 def getheads(self):
419 418 # svn-url@rev
420 419 # Not safe if someone committed:
421 420 self.heads = [self.head]
422 421 # print self.commits.keys()
423 422 return self.heads
424 423
425 424 def _getfile(self, file, rev):
426 425 io = StringIO()
427 426 # TODO: ra.get_file transmits the whole file instead of diffs.
428 427 mode = ''
429 428 try:
430 429 revnum = int(rev.split("@")[-1])
431 430 if self.module != self.modulemap[revnum]:
432 431 self.module = self.modulemap[revnum]
433 432 self.reparent(self.module)
434 433 info = svn.ra.get_file(self.ra, file, revnum, io)
435 434 if isinstance(info, list):
436 435 info = info[-1]
437 436 mode = ("svn:executable" in info) and 'x' or ''
438 437 mode = ("svn:special" in info) and 'l' or mode
439 438 except SubversionException, e:
440 439 notfound = (svn.core.SVN_ERR_FS_NOT_FOUND,
441 440 svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND)
442 441 if e.apr_err in notfound: # File not found
443 442 raise IOError()
444 443 raise
445 444 data = io.getvalue()
446 445 if mode == 'l':
447 446 link_prefix = "link "
448 447 if data.startswith(link_prefix):
449 448 data = data[len(link_prefix):]
450 449 return data, mode
451 450
452 451 def getfile(self, file, rev):
453 452 data, mode = self._getfile(file, rev)
454 453 self.modecache[(file, rev)] = mode
455 454 return data
456 455
457 456 def getmode(self, file, rev):
458 457 return self.modecache[(file, rev)]
459 458
460 459 def getchanges(self, rev):
461 460 self.modecache = {}
462 461 files = self.files[rev]
463 462 cl = files
464 463 cl.sort()
465 464 return cl
466 465
467 466 def getcommit(self, rev):
467 if rev not in self.commits:
468 revnum = int(rev.split('@')[-1])
469 minrev = revnum - LOG_BATCH_SIZE > 0 and revnum - LOG_BATCH_SIZE or 0
470 self._fetch_revisions(from_revnum=revnum, to_revnum=minrev)
468 471 return self.commits[rev]
469 472
470 473 def gettags(self):
471 474 return []
472 475
473 476 def _find_children(self, path, revnum):
474 477 path = path.strip("/")
475 478
476 479 def _find_children_fallback(path, revnum):
477 480 # SWIG python bindings for getdir are broken up to at least 1.4.3
478 481 if not hasattr(self, 'client_ctx'):
479 482 self.client_ctx = svn.client.create_context()
480 483 optrev = svn.core.svn_opt_revision_t()
481 484 optrev.kind = svn.core.svn_opt_revision_number
482 485 optrev.value.number = revnum
483 486 rpath = '/'.join([self.base, path]).strip('/')
484 487 return ['%s/%s' % (path, x) for x in svn.client.ls(rpath, optrev, True, self.client_ctx).keys()]
485 488
486 489 if hasattr(self, '_find_children_fallback'):
487 490 return _find_children_fallback(path, revnum)
488 491
489 492 self.reparent("/" + path)
490 493 pool = Pool()
491 494
492 495 children = []
493 496 def find_children_inner(children, path, revnum = revnum):
494 497 if hasattr(svn.ra, 'get_dir2'): # Since SVN 1.4
495 498 fields = 0xffffffff # Binding does not provide SVN_DIRENT_ALL
496 499 getdir = svn.ra.get_dir2(self.ra, path, revnum, fields, pool)
497 500 else:
498 501 getdir = svn.ra.get_dir(self.ra, path, revnum, pool)
499 502 if type(getdir) == dict:
500 503 # python binding for getdir is broken up to at least 1.4.3
501 504 raise CompatibilityException()
502 505 dirents = getdir[0]
503 506 if type(dirents) == int:
504 507 # got here once due to infinite recursion bug
505 508 # pprint.pprint(getdir)
506 509 return
507 510 c = dirents.keys()
508 511 c.sort()
509 512 for child in c:
510 513 dirent = dirents[child]
511 514 if dirent.kind == svn.core.svn_node_dir:
512 515 find_children_inner(children, (path + "/" + child).strip("/"))
513 516 else:
514 517 children.append((path + "/" + child).strip("/"))
515 518
516 519 try:
517 520 find_children_inner(children, "")
518 521 except CompatibilityException:
519 522 self._find_children_fallback = True
520 523 self.reparent(self.module)
521 524 return _find_children_fallback(path, revnum)
522 525
523 526 self.reparent(self.module)
524 527 return [path + "/" + c for c in children]
General Comments 0
You need to be logged in to leave comments. Login now