##// END OF EJS Templates
convert: svn: skip revisions without file entries
Brendan Cully -
r4776:03844af5 default
parent child Browse files
Show More
@@ -1,542 +1,547 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 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 if not dirent:
127 127 raise util.Abort('module %s not found in revision %d' % (self.module, latest))
128 128 self.last_changed = dirent.created_rev
129 129
130 130 self.head = self.rev(self.last_changed)
131 131
132 132 def rev(self, revnum):
133 133 return (u"svn:%s%s@%s" % (self.uuid, self.module, revnum)).decode(self.encoding)
134 134
135 135 def revnum(self, rev):
136 136 return int(rev.split('@')[-1])
137 137
138 138 def get_blacklist(self):
139 139 """Avoid certain revision numbers.
140 140 It is not uncommon for two nearby revisions to cancel each other
141 141 out, e.g. 'I copied trunk into a subdirectory of itself instead
142 142 of making a branch'. The converted repository is significantly
143 143 smaller if we ignore such revisions."""
144 144 self.blacklist = set()
145 145 blacklist = self.blacklist
146 146 for line in file("blacklist.txt", "r"):
147 147 if not line.startswith("#"):
148 148 try:
149 149 svn_rev = int(line.strip())
150 150 blacklist.add(svn_rev)
151 151 except ValueError, e:
152 152 pass # not an integer or a comment
153 153
154 154 def is_blacklisted(self, svn_rev):
155 155 return svn_rev in self.blacklist
156 156
157 157 def reparent(self, module):
158 158 svn_url = self.base + module
159 159 self.ui.debug("reparent to %s\n" % svn_url.encode(self.encoding))
160 160 svn.ra.reparent(self.ra, svn_url.encode(self.encoding))
161 161
162 162 def _fetch_revisions(self, from_revnum = 0, to_revnum = 347, pb=None):
163 163 # batching is broken for branches
164 164 to_revnum = 0
165 165 if not hasattr(self, 'child_rev'):
166 166 self.child_rev = from_revnum
167 167 self.child_cset = self.commits.get(self.child_rev)
168 168 else:
169 169 self.commits[self.child_rev] = self.child_cset
170 170 # batching broken
171 171 return
172 172 # if the branch was created in the middle of the last batch,
173 173 # svn log will complain that the path doesn't exist in this batch
174 174 # so we roll the parser back to the last revision where this branch appeared
175 175 revnum = self.revnum(self.child_rev)
176 176 if revnum > from_revnum:
177 177 from_revnum = revnum
178 178
179 179 self.ui.debug('Fetching revisions %d to %d\n' % (from_revnum, to_revnum))
180 180
181 181 def get_entry_from_path(path, module=self.module):
182 182 # Given the repository url of this wc, say
183 183 # "http://server/plone/CMFPlone/branches/Plone-2_0-branch"
184 184 # extract the "entry" portion (a relative path) from what
185 185 # svn log --xml says, ie
186 186 # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py"
187 187 # that is to say "tests/PloneTestCase.py"
188 188
189 189 if path.startswith(module):
190 190 relative = path[len(module):]
191 191 if relative.startswith('/'):
192 192 return relative[1:]
193 193 else:
194 194 return relative
195 195
196 196 # The path is outside our tracked tree...
197 197 self.ui.debug('Ignoring %r since it is not under %r\n' % (path, module))
198 198 return None
199 199
200 200 received = []
201 201 def rcvr(*arg, **args):
202 202 orig_paths, revnum, author, date, message, pool = arg
203 203 new_orig_paths = svn_paths(orig_paths)
204 204 rcvr2(new_orig_paths, revnum, author, date, message, pool)
205 205
206 206 def rcvr2(orig_paths, revnum, author, date, message, pool, better_paths = None):
207 207 if not self.is_blacklisted(revnum):
208 208 received.append((orig_paths, revnum, author, date, message))
209 209
210 210 def after_received(orig_paths, revnum, author, date, message):
211 211 if revnum in self.modulemap:
212 212 new_module = self.modulemap[revnum]
213 213 if new_module != self.module:
214 214 self.module = new_module
215 215 self.reparent(self.module)
216 216
217 217 copyfrom = {} # Map of entrypath, revision for finding source of deleted revisions.
218 218 copies = {}
219 219 entries = []
220 220 self.ui.debug("Parsing revision %d\n" % revnum)
221 221 if orig_paths is not None:
222 222 rev = self.rev(revnum)
223 223 try:
224 224 branch = self.module.split("/")[-1]
225 225 if branch == 'trunk':
226 226 branch = ''
227 227 except IndexError:
228 228 branch = None
229 229
230 230 for path in orig_paths:
231 231 # self.ui.write("path %s\n" % path)
232 232 if path == self.module: # Follow branching back in history
233 233 ent = orig_paths[path]
234 234 if ent:
235 235 if ent.copyfrom_path:
236 236 self.modulemap[ent.copyfrom_rev] = ent.copyfrom_path
237 237 else:
238 238 self.ui.debug("No copyfrom path, don't know what to do.\n")
239 239 # Maybe it was added and there is no more history.
240 240 entrypath = get_entry_from_path(path, module=self.module)
241 241 # self.ui.write("entrypath %s\n" % entrypath)
242 if not entrypath:
242 if entrypath is None:
243 243 # Outside our area of interest
244 244 self.ui.debug("boring@%s: %s\n" % (revnum, path))
245 245 continue
246 246 entry = entrypath.decode(self.encoding)
247 247 ent = orig_paths[path]
248 if not entrypath:
249 # TODO: branch creation event
250 pass
248 251
249 252 kind = svn.ra.check_path(self.ra, entrypath, revnum)
250 253 if kind == svn.core.svn_node_file:
251 254 if ent.copyfrom_path:
252 255 copyfrom_path = get_entry_from_path(ent.copyfrom_path)
253 256 if copyfrom_path:
254 257 self.ui.debug("Copied to %s from %s@%s\n" % (entry, copyfrom_path, ent.copyfrom_rev))
255 258 # It's probably important for hg that the source
256 259 # exists in the revision's parent, not just the
257 260 # ent.copyfrom_rev
258 261 fromkind = svn.ra.check_path(self.ra, copyfrom_path, ent.copyfrom_rev)
259 262 if fromkind != 0:
260 263 copies[self.recode(entry)] = self.recode(copyfrom_path)
261 264 entries.append(self.recode(entry))
262 265 elif kind == 0: # gone, but had better be a deleted *file*
263 266 self.ui.debug("gone from %s\n" % ent.copyfrom_rev)
264 267
265 268 fromrev = revnum - 1
266 269 # might always need to be revnum - 1 in these 3 lines?
267 270 old_module = self.modulemap.get(fromrev, self.module)
268 271 basepath = old_module + "/" + get_entry_from_path(path, module=self.module)
269 272 entrypath = old_module + "/" + get_entry_from_path(path, module=self.module)
270 273
271 274 def lookup_parts(p):
272 275 rc = None
273 276 parts = p.split("/")
274 277 for i in range(len(parts)):
275 278 part = "/".join(parts[:i])
276 279 info = part, copyfrom.get(part, None)
277 280 if info[1] is not None:
278 281 self.ui.debug("Found parent directory %s\n" % info)
279 282 rc = info
280 283 return rc
281 284
282 285 self.ui.debug("base, entry %s %s\n" % (basepath, entrypath))
283 286
284 287 frompath, froment = lookup_parts(entrypath) or (None, revnum - 1)
285 288
286 289 # need to remove fragment from lookup_parts and replace with copyfrom_path
287 290 if frompath is not None:
288 291 self.ui.debug("munge-o-matic\n")
289 292 self.ui.debug(entrypath + '\n')
290 293 self.ui.debug(entrypath[len(frompath):] + '\n')
291 294 entrypath = froment.copyfrom_path + entrypath[len(frompath):]
292 295 fromrev = froment.copyfrom_rev
293 296 self.ui.debug("Info: %s %s %s %s\n" % (frompath, froment, ent, entrypath))
294 297
295 298 fromkind = svn.ra.check_path(self.ra, entrypath, fromrev)
296 299 if fromkind == svn.core.svn_node_file: # a deleted file
297 300 entries.append(self.recode(entry))
298 301 else:
299 302 # print "Deleted/moved non-file:", revnum, path, ent
300 303 # children = self._find_children(path, revnum - 1)
301 304 # print "find children %s@%d from %d action %s" % (path, revnum, ent.copyfrom_rev, ent.action)
302 305 # Sometimes this is tricky. For example: in
303 306 # The Subversion Repository revision 6940 a dir
304 307 # was copied and one of its files was deleted
305 308 # from the new location in the same commit. This
306 309 # code can't deal with that yet.
307 310 if ent.action == 'C':
308 311 children = self._find_children(path, fromrev)
309 312 else:
310 313 oroot = entrypath.strip('/')
311 314 nroot = path.strip('/')
312 315 children = self._find_children(oroot, fromrev)
313 316 children = [s.replace(oroot,nroot) for s in children]
314 317 # Mark all [files, not directories] as deleted.
315 318 for child in children:
316 319 # Can we move a child directory and its
317 320 # parent in the same commit? (probably can). Could
318 321 # cause problems if instead of revnum -1,
319 322 # we have to look in (copyfrom_path, revnum - 1)
320 323 entrypath = get_entry_from_path("/" + child, module=old_module)
321 324 if entrypath:
322 325 entry = self.recode(entrypath.decode(self.encoding))
323 326 if entry in copies:
324 327 # deleted file within a copy
325 328 del copies[entry]
326 329 else:
327 330 entries.append(entry)
328 331 elif kind == svn.core.svn_node_dir:
329 332 # Should probably synthesize normal file entries
330 333 # and handle as above to clean up copy/rename handling.
331 334
332 335 # If the directory just had a prop change,
333 336 # then we shouldn't need to look for its children.
334 337 # Also this could create duplicate entries. Not sure
335 338 # whether this will matter. Maybe should make entries a set.
336 339 # print "Changed directory", revnum, path, ent.action, ent.copyfrom_path, ent.copyfrom_rev
337 340 # This will fail if a directory was copied
338 341 # from another branch and then some of its files
339 342 # were deleted in the same transaction.
340 343 children = self._find_children(path, revnum)
341 344 children.sort()
342 345 for child in children:
343 346 # Can we move a child directory and its
344 347 # parent in the same commit? (probably can). Could
345 348 # cause problems if instead of revnum -1,
346 349 # we have to look in (copyfrom_path, revnum - 1)
347 350 entrypath = get_entry_from_path("/" + child, module=self.module)
348 351 # print child, self.module, entrypath
349 352 if entrypath:
350 353 # Need to filter out directories here...
351 354 kind = svn.ra.check_path(self.ra, entrypath, revnum)
352 355 if kind != svn.core.svn_node_dir:
353 356 entries.append(self.recode(entrypath))
354 357
355 358 # Copies here (must copy all from source)
356 359 # Probably not a real problem for us if
357 360 # source does not exist
358 361
359 362 # Can do this with the copy command "hg copy"
360 363 # if ent.copyfrom_path:
361 364 # copyfrom_entry = get_entry_from_path(ent.copyfrom_path.decode(self.encoding),
362 365 # module=self.module)
363 366 # copyto_entry = entrypath
364 367 #
365 368 # print "copy directory", copyfrom_entry, 'to', copyto_entry
366 369 #
367 370 # copies.append((copyfrom_entry, copyto_entry))
368 371
369 372 if ent.copyfrom_path:
370 373 copyfrom_path = ent.copyfrom_path.decode(self.encoding)
371 374 copyfrom_entry = get_entry_from_path(copyfrom_path, module=self.module)
372 375 if copyfrom_entry:
373 376 copyfrom[path] = ent
374 377 self.ui.debug("mark %s came from %s\n" % (path, copyfrom[path]))
375 378
376 379 # Good, /probably/ a regular copy. Really should check
377 380 # to see whether the parent revision actually contains
378 381 # the directory in question.
379 382 children = self._find_children(self.recode(copyfrom_path), ent.copyfrom_rev)
380 383 children.sort()
381 384 for child in children:
382 385 entrypath = get_entry_from_path("/" + child, module=self.module)
383 386 if entrypath:
384 387 entry = entrypath.decode(self.encoding)
385 388 # print "COPY COPY From", copyfrom_entry, entry
386 389 copyto_path = path + entry[len(copyfrom_entry):]
387 390 copyto_entry = get_entry_from_path(copyto_path, module=self.module)
388 391 # print "COPY", entry, "COPY To", copyto_entry
389 392 copies[self.recode(copyto_entry)] = self.recode(entry)
390 393 # copy from quux splort/quuxfile
391 394
392 395 self.modulemap[revnum] = self.module # track backwards in time
393 396 # a list of (filename, id) where id lets us retrieve the file.
394 397 # eg in git, id is the object hash. for svn it'll be the
395 398 self.files[rev] = zip(entries, [rev] * len(entries))
399 if not entries:
400 return
396 401
397 402 # Example SVN datetime. Includes microseconds.
398 403 # ISO-8601 conformant
399 404 # '2007-01-04T17:35:00.902377Z'
400 405 date = util.parsedate(date[:18] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
401 406
402 407 log = message and self.recode(message)
403 408 author = author and self.recode(author) or ''
404 409
405 410 cset = commit(author=author,
406 411 date=util.datestr(date),
407 412 desc=log,
408 413 parents=[],
409 414 copies=copies,
410 415 branch=branch)
411 416
412 417 if self.child_cset and self.child_rev != rev:
413 418 self.child_cset.parents = [rev]
414 419 self.commits[self.child_rev] = self.child_cset
415 420 self.child_cset = cset
416 421 self.child_rev = rev
417 422
418 423 try:
419 424 discover_changed_paths = True
420 425 strict_node_history = False
421 426 svn.ra.get_log(self.ra, [self.module], from_revnum, to_revnum,
422 427 0, discover_changed_paths, strict_node_history, rcvr)
423 428 for args in received:
424 429 after_received(*args)
425 430 self.last_revnum = to_revnum
426 431 except SubversionException, (_, num):
427 432 if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION:
428 433 raise NoSuchRevision(branch=self,
429 434 revision="Revision number %d" % to_revnum)
430 435 raise
431 436
432 437 def getheads(self):
433 438 # svn-url@rev
434 439 # Not safe if someone committed:
435 440 self.heads = [self.head]
436 441 # print self.commits.keys()
437 442 return self.heads
438 443
439 444 def _getfile(self, file, rev):
440 445 io = StringIO()
441 446 # TODO: ra.get_file transmits the whole file instead of diffs.
442 447 mode = ''
443 448 try:
444 449 revnum = self.revnum(rev)
445 450 if self.module != self.modulemap[revnum]:
446 451 self.module = self.modulemap[revnum]
447 452 self.reparent(self.module)
448 453 info = svn.ra.get_file(self.ra, file, revnum, io)
449 454 if isinstance(info, list):
450 455 info = info[-1]
451 456 mode = ("svn:executable" in info) and 'x' or ''
452 457 mode = ("svn:special" in info) and 'l' or mode
453 458 except SubversionException, e:
454 459 notfound = (svn.core.SVN_ERR_FS_NOT_FOUND,
455 460 svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND)
456 461 if e.apr_err in notfound: # File not found
457 462 raise IOError()
458 463 raise
459 464 data = io.getvalue()
460 465 if mode == 'l':
461 466 link_prefix = "link "
462 467 if data.startswith(link_prefix):
463 468 data = data[len(link_prefix):]
464 469 return data, mode
465 470
466 471 def getfile(self, file, rev):
467 472 data, mode = self._getfile(file, rev)
468 473 self.modecache[(file, rev)] = mode
469 474 return data
470 475
471 476 def getmode(self, file, rev):
472 477 return self.modecache[(file, rev)]
473 478
474 479 def getchanges(self, rev):
475 480 self.modecache = {}
476 481 files = self.files[rev]
477 482 cl = files
478 483 cl.sort()
479 484 return cl
480 485
481 486 def getcommit(self, rev):
482 487 if rev not in self.commits:
483 488 revnum = self.revnum(rev)
484 489 minrev = revnum - LOG_BATCH_SIZE > 0 and revnum - LOG_BATCH_SIZE or 0
485 490 self._fetch_revisions(from_revnum=revnum, to_revnum=minrev)
486 491 return self.commits[rev]
487 492
488 493 def gettags(self):
489 494 return []
490 495
491 496 def _find_children(self, path, revnum):
492 497 path = path.strip("/")
493 498
494 499 def _find_children_fallback(path, revnum):
495 500 # SWIG python bindings for getdir are broken up to at least 1.4.3
496 501 if not hasattr(self, 'client_ctx'):
497 502 self.client_ctx = svn.client.create_context()
498 503 optrev = svn.core.svn_opt_revision_t()
499 504 optrev.kind = svn.core.svn_opt_revision_number
500 505 optrev.value.number = revnum
501 506 rpath = '/'.join([self.base, path]).strip('/')
502 507 return ['%s/%s' % (path, x) for x in svn.client.ls(rpath, optrev, True, self.client_ctx).keys()]
503 508
504 509 if hasattr(self, '_find_children_fallback'):
505 510 return _find_children_fallback(path, revnum)
506 511
507 512 self.reparent("/" + path)
508 513 pool = Pool()
509 514
510 515 children = []
511 516 def find_children_inner(children, path, revnum = revnum):
512 517 if hasattr(svn.ra, 'get_dir2'): # Since SVN 1.4
513 518 fields = 0xffffffff # Binding does not provide SVN_DIRENT_ALL
514 519 getdir = svn.ra.get_dir2(self.ra, path, revnum, fields, pool)
515 520 else:
516 521 getdir = svn.ra.get_dir(self.ra, path, revnum, pool)
517 522 if type(getdir) == dict:
518 523 # python binding for getdir is broken up to at least 1.4.3
519 524 raise CompatibilityException()
520 525 dirents = getdir[0]
521 526 if type(dirents) == int:
522 527 # got here once due to infinite recursion bug
523 528 # pprint.pprint(getdir)
524 529 return
525 530 c = dirents.keys()
526 531 c.sort()
527 532 for child in c:
528 533 dirent = dirents[child]
529 534 if dirent.kind == svn.core.svn_node_dir:
530 535 find_children_inner(children, (path + "/" + child).strip("/"))
531 536 else:
532 537 children.append((path + "/" + child).strip("/"))
533 538
534 539 try:
535 540 find_children_inner(children, "")
536 541 except CompatibilityException:
537 542 self._find_children_fallback = True
538 543 self.reparent(self.module)
539 544 return _find_children_fallback(path, revnum)
540 545
541 546 self.reparent(self.module)
542 547 return [path + "/" + c for c in children]
General Comments 0
You need to be logged in to leave comments. Login now