##// END OF EJS Templates
vfs: use 'vfs' module directly in 'hgext.convert'...
Pierre-Yves David -
r31246:b023caa6 default
parent child Browse files
Show More
@@ -1,1353 +1,1353
1 1 # Subversion 1.4/1.5 Python API backend
2 2 #
3 3 # Copyright(C) 2007 Daniel Holth et al
4 4 from __future__ import absolute_import
5 5
6 6 import os
7 7 import re
8 8 import tempfile
9 9 import xml.dom.minidom
10 10
11 11 from mercurial.i18n import _
12 12 from mercurial import (
13 13 encoding,
14 14 error,
15 15 pycompat,
16 scmutil,
17 16 util,
17 vfs as vfsmod,
18 18 )
19 19
20 20 from . import common
21 21
22 22 pickle = util.pickle
23 23 stringio = util.stringio
24 24 propertycache = util.propertycache
25 25 urlerr = util.urlerr
26 26 urlreq = util.urlreq
27 27
28 28 commandline = common.commandline
29 29 commit = common.commit
30 30 converter_sink = common.converter_sink
31 31 converter_source = common.converter_source
32 32 decodeargs = common.decodeargs
33 33 encodeargs = common.encodeargs
34 34 makedatetimestamp = common.makedatetimestamp
35 35 mapfile = common.mapfile
36 36 MissingTool = common.MissingTool
37 37 NoRepo = common.NoRepo
38 38
39 39 # Subversion stuff. Works best with very recent Python SVN bindings
40 40 # e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing
41 41 # these bindings.
42 42
43 43 try:
44 44 import svn
45 45 import svn.client
46 46 import svn.core
47 47 import svn.ra
48 48 import svn.delta
49 49 from . import transport
50 50 import warnings
51 51 warnings.filterwarnings('ignore',
52 52 module='svn.core',
53 53 category=DeprecationWarning)
54 54 svn.core.SubversionException # trigger import to catch error
55 55
56 56 except ImportError:
57 57 svn = None
58 58
59 59 class SvnPathNotFound(Exception):
60 60 pass
61 61
62 62 def revsplit(rev):
63 63 """Parse a revision string and return (uuid, path, revnum).
64 64 >>> revsplit('svn:a2147622-4a9f-4db4-a8d3-13562ff547b2'
65 65 ... '/proj%20B/mytrunk/mytrunk@1')
66 66 ('a2147622-4a9f-4db4-a8d3-13562ff547b2', '/proj%20B/mytrunk/mytrunk', 1)
67 67 >>> revsplit('svn:8af66a51-67f5-4354-b62c-98d67cc7be1d@1')
68 68 ('', '', 1)
69 69 >>> revsplit('@7')
70 70 ('', '', 7)
71 71 >>> revsplit('7')
72 72 ('', '', 0)
73 73 >>> revsplit('bad')
74 74 ('', '', 0)
75 75 """
76 76 parts = rev.rsplit('@', 1)
77 77 revnum = 0
78 78 if len(parts) > 1:
79 79 revnum = int(parts[1])
80 80 parts = parts[0].split('/', 1)
81 81 uuid = ''
82 82 mod = ''
83 83 if len(parts) > 1 and parts[0].startswith('svn:'):
84 84 uuid = parts[0][4:]
85 85 mod = '/' + parts[1]
86 86 return uuid, mod, revnum
87 87
88 88 def quote(s):
89 89 # As of svn 1.7, many svn calls expect "canonical" paths. In
90 90 # theory, we should call svn.core.*canonicalize() on all paths
91 91 # before passing them to the API. Instead, we assume the base url
92 92 # is canonical and copy the behaviour of svn URL encoding function
93 93 # so we can extend it safely with new components. The "safe"
94 94 # characters were taken from the "svn_uri__char_validity" table in
95 95 # libsvn_subr/path.c.
96 96 return urlreq.quote(s, "!$&'()*+,-./:=@_~")
97 97
98 98 def geturl(path):
99 99 try:
100 100 return svn.client.url_from_path(svn.core.svn_path_canonicalize(path))
101 101 except svn.core.SubversionException:
102 102 # svn.client.url_from_path() fails with local repositories
103 103 pass
104 104 if os.path.isdir(path):
105 105 path = os.path.normpath(os.path.abspath(path))
106 106 if pycompat.osname == 'nt':
107 107 path = '/' + util.normpath(path)
108 108 # Module URL is later compared with the repository URL returned
109 109 # by svn API, which is UTF-8.
110 110 path = encoding.tolocal(path)
111 111 path = 'file://%s' % quote(path)
112 112 return svn.core.svn_path_canonicalize(path)
113 113
114 114 def optrev(number):
115 115 optrev = svn.core.svn_opt_revision_t()
116 116 optrev.kind = svn.core.svn_opt_revision_number
117 117 optrev.value.number = number
118 118 return optrev
119 119
120 120 class changedpath(object):
121 121 def __init__(self, p):
122 122 self.copyfrom_path = p.copyfrom_path
123 123 self.copyfrom_rev = p.copyfrom_rev
124 124 self.action = p.action
125 125
126 126 def get_log_child(fp, url, paths, start, end, limit=0,
127 127 discover_changed_paths=True, strict_node_history=False):
128 128 protocol = -1
129 129 def receiver(orig_paths, revnum, author, date, message, pool):
130 130 paths = {}
131 131 if orig_paths is not None:
132 132 for k, v in orig_paths.iteritems():
133 133 paths[k] = changedpath(v)
134 134 pickle.dump((paths, revnum, author, date, message),
135 135 fp, protocol)
136 136
137 137 try:
138 138 # Use an ra of our own so that our parent can consume
139 139 # our results without confusing the server.
140 140 t = transport.SvnRaTransport(url=url)
141 141 svn.ra.get_log(t.ra, paths, start, end, limit,
142 142 discover_changed_paths,
143 143 strict_node_history,
144 144 receiver)
145 145 except IOError:
146 146 # Caller may interrupt the iteration
147 147 pickle.dump(None, fp, protocol)
148 148 except Exception as inst:
149 149 pickle.dump(str(inst), fp, protocol)
150 150 else:
151 151 pickle.dump(None, fp, protocol)
152 152 fp.close()
153 153 # With large history, cleanup process goes crazy and suddenly
154 154 # consumes *huge* amount of memory. The output file being closed,
155 155 # there is no need for clean termination.
156 156 os._exit(0)
157 157
158 158 def debugsvnlog(ui, **opts):
159 159 """Fetch SVN log in a subprocess and channel them back to parent to
160 160 avoid memory collection issues.
161 161 """
162 162 if svn is None:
163 163 raise error.Abort(_('debugsvnlog could not load Subversion python '
164 164 'bindings'))
165 165
166 166 args = decodeargs(ui.fin.read())
167 167 get_log_child(ui.fout, *args)
168 168
169 169 class logstream(object):
170 170 """Interruptible revision log iterator."""
171 171 def __init__(self, stdout):
172 172 self._stdout = stdout
173 173
174 174 def __iter__(self):
175 175 while True:
176 176 try:
177 177 entry = pickle.load(self._stdout)
178 178 except EOFError:
179 179 raise error.Abort(_('Mercurial failed to run itself, check'
180 180 ' hg executable is in PATH'))
181 181 try:
182 182 orig_paths, revnum, author, date, message = entry
183 183 except (TypeError, ValueError):
184 184 if entry is None:
185 185 break
186 186 raise error.Abort(_("log stream exception '%s'") % entry)
187 187 yield entry
188 188
189 189 def close(self):
190 190 if self._stdout:
191 191 self._stdout.close()
192 192 self._stdout = None
193 193
194 194 class directlogstream(list):
195 195 """Direct revision log iterator.
196 196 This can be used for debugging and development but it will probably leak
197 197 memory and is not suitable for real conversions."""
198 198 def __init__(self, url, paths, start, end, limit=0,
199 199 discover_changed_paths=True, strict_node_history=False):
200 200
201 201 def receiver(orig_paths, revnum, author, date, message, pool):
202 202 paths = {}
203 203 if orig_paths is not None:
204 204 for k, v in orig_paths.iteritems():
205 205 paths[k] = changedpath(v)
206 206 self.append((paths, revnum, author, date, message))
207 207
208 208 # Use an ra of our own so that our parent can consume
209 209 # our results without confusing the server.
210 210 t = transport.SvnRaTransport(url=url)
211 211 svn.ra.get_log(t.ra, paths, start, end, limit,
212 212 discover_changed_paths,
213 213 strict_node_history,
214 214 receiver)
215 215
216 216 def close(self):
217 217 pass
218 218
219 219 # Check to see if the given path is a local Subversion repo. Verify this by
220 220 # looking for several svn-specific files and directories in the given
221 221 # directory.
222 222 def filecheck(ui, path, proto):
223 223 for x in ('locks', 'hooks', 'format', 'db'):
224 224 if not os.path.exists(os.path.join(path, x)):
225 225 return False
226 226 return True
227 227
228 228 # Check to see if a given path is the root of an svn repo over http. We verify
229 229 # this by requesting a version-controlled URL we know can't exist and looking
230 230 # for the svn-specific "not found" XML.
231 231 def httpcheck(ui, path, proto):
232 232 try:
233 233 opener = urlreq.buildopener()
234 234 rsp = opener.open('%s://%s/!svn/ver/0/.svn' % (proto, path))
235 235 data = rsp.read()
236 236 except urlerr.httperror as inst:
237 237 if inst.code != 404:
238 238 # Except for 404 we cannot know for sure this is not an svn repo
239 239 ui.warn(_('svn: cannot probe remote repository, assume it could '
240 240 'be a subversion repository. Use --source-type if you '
241 241 'know better.\n'))
242 242 return True
243 243 data = inst.fp.read()
244 244 except Exception:
245 245 # Could be urlerr.urlerror if the URL is invalid or anything else.
246 246 return False
247 247 return '<m:human-readable errcode="160013">' in data
248 248
249 249 protomap = {'http': httpcheck,
250 250 'https': httpcheck,
251 251 'file': filecheck,
252 252 }
253 253 def issvnurl(ui, url):
254 254 try:
255 255 proto, path = url.split('://', 1)
256 256 if proto == 'file':
257 257 if (pycompat.osname == 'nt' and path[:1] == '/'
258 258 and path[1:2].isalpha() and path[2:6].lower() == '%3a/'):
259 259 path = path[:2] + ':/' + path[6:]
260 260 path = urlreq.url2pathname(path)
261 261 except ValueError:
262 262 proto = 'file'
263 263 path = os.path.abspath(url)
264 264 if proto == 'file':
265 265 path = util.pconvert(path)
266 266 check = protomap.get(proto, lambda *args: False)
267 267 while '/' in path:
268 268 if check(ui, path, proto):
269 269 return True
270 270 path = path.rsplit('/', 1)[0]
271 271 return False
272 272
273 273 # SVN conversion code stolen from bzr-svn and tailor
274 274 #
275 275 # Subversion looks like a versioned filesystem, branches structures
276 276 # are defined by conventions and not enforced by the tool. First,
277 277 # we define the potential branches (modules) as "trunk" and "branches"
278 278 # children directories. Revisions are then identified by their
279 279 # module and revision number (and a repository identifier).
280 280 #
281 281 # The revision graph is really a tree (or a forest). By default, a
282 282 # revision parent is the previous revision in the same module. If the
283 283 # module directory is copied/moved from another module then the
284 284 # revision is the module root and its parent the source revision in
285 285 # the parent module. A revision has at most one parent.
286 286 #
287 287 class svn_source(converter_source):
288 288 def __init__(self, ui, url, revs=None):
289 289 super(svn_source, self).__init__(ui, url, revs=revs)
290 290
291 291 if not (url.startswith('svn://') or url.startswith('svn+ssh://') or
292 292 (os.path.exists(url) and
293 293 os.path.exists(os.path.join(url, '.svn'))) or
294 294 issvnurl(ui, url)):
295 295 raise NoRepo(_("%s does not look like a Subversion repository")
296 296 % url)
297 297 if svn is None:
298 298 raise MissingTool(_('could not load Subversion python bindings'))
299 299
300 300 try:
301 301 version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR
302 302 if version < (1, 4):
303 303 raise MissingTool(_('Subversion python bindings %d.%d found, '
304 304 '1.4 or later required') % version)
305 305 except AttributeError:
306 306 raise MissingTool(_('Subversion python bindings are too old, 1.4 '
307 307 'or later required'))
308 308
309 309 self.lastrevs = {}
310 310
311 311 latest = None
312 312 try:
313 313 # Support file://path@rev syntax. Useful e.g. to convert
314 314 # deleted branches.
315 315 at = url.rfind('@')
316 316 if at >= 0:
317 317 latest = int(url[at + 1:])
318 318 url = url[:at]
319 319 except ValueError:
320 320 pass
321 321 self.url = geturl(url)
322 322 self.encoding = 'UTF-8' # Subversion is always nominal UTF-8
323 323 try:
324 324 self.transport = transport.SvnRaTransport(url=self.url)
325 325 self.ra = self.transport.ra
326 326 self.ctx = self.transport.client
327 327 self.baseurl = svn.ra.get_repos_root(self.ra)
328 328 # Module is either empty or a repository path starting with
329 329 # a slash and not ending with a slash.
330 330 self.module = urlreq.unquote(self.url[len(self.baseurl):])
331 331 self.prevmodule = None
332 332 self.rootmodule = self.module
333 333 self.commits = {}
334 334 self.paths = {}
335 335 self.uuid = svn.ra.get_uuid(self.ra)
336 336 except svn.core.SubversionException:
337 337 ui.traceback()
338 338 svnversion = '%d.%d.%d' % (svn.core.SVN_VER_MAJOR,
339 339 svn.core.SVN_VER_MINOR,
340 340 svn.core.SVN_VER_MICRO)
341 341 raise NoRepo(_("%s does not look like a Subversion repository "
342 342 "to libsvn version %s")
343 343 % (self.url, svnversion))
344 344
345 345 if revs:
346 346 if len(revs) > 1:
347 347 raise error.Abort(_('subversion source does not support '
348 348 'specifying multiple revisions'))
349 349 try:
350 350 latest = int(revs[0])
351 351 except ValueError:
352 352 raise error.Abort(_('svn: revision %s is not an integer') %
353 353 revs[0])
354 354
355 355 self.trunkname = self.ui.config('convert', 'svn.trunk',
356 356 'trunk').strip('/')
357 357 self.startrev = self.ui.config('convert', 'svn.startrev', default=0)
358 358 try:
359 359 self.startrev = int(self.startrev)
360 360 if self.startrev < 0:
361 361 self.startrev = 0
362 362 except ValueError:
363 363 raise error.Abort(_('svn: start revision %s is not an integer')
364 364 % self.startrev)
365 365
366 366 try:
367 367 self.head = self.latest(self.module, latest)
368 368 except SvnPathNotFound:
369 369 self.head = None
370 370 if not self.head:
371 371 raise error.Abort(_('no revision found in module %s')
372 372 % self.module)
373 373 self.last_changed = self.revnum(self.head)
374 374
375 375 self._changescache = (None, None)
376 376
377 377 if os.path.exists(os.path.join(url, '.svn/entries')):
378 378 self.wc = url
379 379 else:
380 380 self.wc = None
381 381 self.convertfp = None
382 382
383 383 def setrevmap(self, revmap):
384 384 lastrevs = {}
385 385 for revid in revmap.iterkeys():
386 386 uuid, module, revnum = revsplit(revid)
387 387 lastrevnum = lastrevs.setdefault(module, revnum)
388 388 if revnum > lastrevnum:
389 389 lastrevs[module] = revnum
390 390 self.lastrevs = lastrevs
391 391
392 392 def exists(self, path, optrev):
393 393 try:
394 394 svn.client.ls(self.url.rstrip('/') + '/' + quote(path),
395 395 optrev, False, self.ctx)
396 396 return True
397 397 except svn.core.SubversionException:
398 398 return False
399 399
400 400 def getheads(self):
401 401
402 402 def isdir(path, revnum):
403 403 kind = self._checkpath(path, revnum)
404 404 return kind == svn.core.svn_node_dir
405 405
406 406 def getcfgpath(name, rev):
407 407 cfgpath = self.ui.config('convert', 'svn.' + name)
408 408 if cfgpath is not None and cfgpath.strip() == '':
409 409 return None
410 410 path = (cfgpath or name).strip('/')
411 411 if not self.exists(path, rev):
412 412 if self.module.endswith(path) and name == 'trunk':
413 413 # we are converting from inside this directory
414 414 return None
415 415 if cfgpath:
416 416 raise error.Abort(_('expected %s to be at %r, but not found'
417 417 ) % (name, path))
418 418 return None
419 419 self.ui.note(_('found %s at %r\n') % (name, path))
420 420 return path
421 421
422 422 rev = optrev(self.last_changed)
423 423 oldmodule = ''
424 424 trunk = getcfgpath('trunk', rev)
425 425 self.tags = getcfgpath('tags', rev)
426 426 branches = getcfgpath('branches', rev)
427 427
428 428 # If the project has a trunk or branches, we will extract heads
429 429 # from them. We keep the project root otherwise.
430 430 if trunk:
431 431 oldmodule = self.module or ''
432 432 self.module += '/' + trunk
433 433 self.head = self.latest(self.module, self.last_changed)
434 434 if not self.head:
435 435 raise error.Abort(_('no revision found in module %s')
436 436 % self.module)
437 437
438 438 # First head in the list is the module's head
439 439 self.heads = [self.head]
440 440 if self.tags is not None:
441 441 self.tags = '%s/%s' % (oldmodule , (self.tags or 'tags'))
442 442
443 443 # Check if branches bring a few more heads to the list
444 444 if branches:
445 445 rpath = self.url.strip('/')
446 446 branchnames = svn.client.ls(rpath + '/' + quote(branches),
447 447 rev, False, self.ctx)
448 448 for branch in sorted(branchnames):
449 449 module = '%s/%s/%s' % (oldmodule, branches, branch)
450 450 if not isdir(module, self.last_changed):
451 451 continue
452 452 brevid = self.latest(module, self.last_changed)
453 453 if not brevid:
454 454 self.ui.note(_('ignoring empty branch %s\n') % branch)
455 455 continue
456 456 self.ui.note(_('found branch %s at %d\n') %
457 457 (branch, self.revnum(brevid)))
458 458 self.heads.append(brevid)
459 459
460 460 if self.startrev and self.heads:
461 461 if len(self.heads) > 1:
462 462 raise error.Abort(_('svn: start revision is not supported '
463 463 'with more than one branch'))
464 464 revnum = self.revnum(self.heads[0])
465 465 if revnum < self.startrev:
466 466 raise error.Abort(
467 467 _('svn: no revision found after start revision %d')
468 468 % self.startrev)
469 469
470 470 return self.heads
471 471
472 472 def _getchanges(self, rev, full):
473 473 (paths, parents) = self.paths[rev]
474 474 copies = {}
475 475 if parents:
476 476 files, self.removed, copies = self.expandpaths(rev, paths, parents)
477 477 if full or not parents:
478 478 # Perform a full checkout on roots
479 479 uuid, module, revnum = revsplit(rev)
480 480 entries = svn.client.ls(self.baseurl + quote(module),
481 481 optrev(revnum), True, self.ctx)
482 482 files = [n for n, e in entries.iteritems()
483 483 if e.kind == svn.core.svn_node_file]
484 484 self.removed = set()
485 485
486 486 files.sort()
487 487 files = zip(files, [rev] * len(files))
488 488 return (files, copies)
489 489
490 490 def getchanges(self, rev, full):
491 491 # reuse cache from getchangedfiles
492 492 if self._changescache[0] == rev and not full:
493 493 (files, copies) = self._changescache[1]
494 494 else:
495 495 (files, copies) = self._getchanges(rev, full)
496 496 # caller caches the result, so free it here to release memory
497 497 del self.paths[rev]
498 498 return (files, copies, set())
499 499
500 500 def getchangedfiles(self, rev, i):
501 501 # called from filemap - cache computed values for reuse in getchanges
502 502 (files, copies) = self._getchanges(rev, False)
503 503 self._changescache = (rev, (files, copies))
504 504 return [f[0] for f in files]
505 505
506 506 def getcommit(self, rev):
507 507 if rev not in self.commits:
508 508 uuid, module, revnum = revsplit(rev)
509 509 self.module = module
510 510 self.reparent(module)
511 511 # We assume that:
512 512 # - requests for revisions after "stop" come from the
513 513 # revision graph backward traversal. Cache all of them
514 514 # down to stop, they will be used eventually.
515 515 # - requests for revisions before "stop" come to get
516 516 # isolated branches parents. Just fetch what is needed.
517 517 stop = self.lastrevs.get(module, 0)
518 518 if revnum < stop:
519 519 stop = revnum + 1
520 520 self._fetch_revisions(revnum, stop)
521 521 if rev not in self.commits:
522 522 raise error.Abort(_('svn: revision %s not found') % revnum)
523 523 revcommit = self.commits[rev]
524 524 # caller caches the result, so free it here to release memory
525 525 del self.commits[rev]
526 526 return revcommit
527 527
528 528 def checkrevformat(self, revstr, mapname='splicemap'):
529 529 """ fails if revision format does not match the correct format"""
530 530 if not re.match(r'svn:[0-9a-f]{8,8}-[0-9a-f]{4,4}-'
531 531 r'[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]'
532 532 r'{12,12}(.*)\@[0-9]+$',revstr):
533 533 raise error.Abort(_('%s entry %s is not a valid revision'
534 534 ' identifier') % (mapname, revstr))
535 535
536 536 def numcommits(self):
537 537 return int(self.head.rsplit('@', 1)[1]) - self.startrev
538 538
539 539 def gettags(self):
540 540 tags = {}
541 541 if self.tags is None:
542 542 return tags
543 543
544 544 # svn tags are just a convention, project branches left in a
545 545 # 'tags' directory. There is no other relationship than
546 546 # ancestry, which is expensive to discover and makes them hard
547 547 # to update incrementally. Worse, past revisions may be
548 548 # referenced by tags far away in the future, requiring a deep
549 549 # history traversal on every calculation. Current code
550 550 # performs a single backward traversal, tracking moves within
551 551 # the tags directory (tag renaming) and recording a new tag
552 552 # everytime a project is copied from outside the tags
553 553 # directory. It also lists deleted tags, this behaviour may
554 554 # change in the future.
555 555 pendings = []
556 556 tagspath = self.tags
557 557 start = svn.ra.get_latest_revnum(self.ra)
558 558 stream = self._getlog([self.tags], start, self.startrev)
559 559 try:
560 560 for entry in stream:
561 561 origpaths, revnum, author, date, message = entry
562 562 if not origpaths:
563 563 origpaths = []
564 564 copies = [(e.copyfrom_path, e.copyfrom_rev, p) for p, e
565 565 in origpaths.iteritems() if e.copyfrom_path]
566 566 # Apply moves/copies from more specific to general
567 567 copies.sort(reverse=True)
568 568
569 569 srctagspath = tagspath
570 570 if copies and copies[-1][2] == tagspath:
571 571 # Track tags directory moves
572 572 srctagspath = copies.pop()[0]
573 573
574 574 for source, sourcerev, dest in copies:
575 575 if not dest.startswith(tagspath + '/'):
576 576 continue
577 577 for tag in pendings:
578 578 if tag[0].startswith(dest):
579 579 tagpath = source + tag[0][len(dest):]
580 580 tag[:2] = [tagpath, sourcerev]
581 581 break
582 582 else:
583 583 pendings.append([source, sourcerev, dest])
584 584
585 585 # Filter out tags with children coming from different
586 586 # parts of the repository like:
587 587 # /tags/tag.1 (from /trunk:10)
588 588 # /tags/tag.1/foo (from /branches/foo:12)
589 589 # Here/tags/tag.1 discarded as well as its children.
590 590 # It happens with tools like cvs2svn. Such tags cannot
591 591 # be represented in mercurial.
592 592 addeds = dict((p, e.copyfrom_path) for p, e
593 593 in origpaths.iteritems()
594 594 if e.action == 'A' and e.copyfrom_path)
595 595 badroots = set()
596 596 for destroot in addeds:
597 597 for source, sourcerev, dest in pendings:
598 598 if (not dest.startswith(destroot + '/')
599 599 or source.startswith(addeds[destroot] + '/')):
600 600 continue
601 601 badroots.add(destroot)
602 602 break
603 603
604 604 for badroot in badroots:
605 605 pendings = [p for p in pendings if p[2] != badroot
606 606 and not p[2].startswith(badroot + '/')]
607 607
608 608 # Tell tag renamings from tag creations
609 609 renamings = []
610 610 for source, sourcerev, dest in pendings:
611 611 tagname = dest.split('/')[-1]
612 612 if source.startswith(srctagspath):
613 613 renamings.append([source, sourcerev, tagname])
614 614 continue
615 615 if tagname in tags:
616 616 # Keep the latest tag value
617 617 continue
618 618 # From revision may be fake, get one with changes
619 619 try:
620 620 tagid = self.latest(source, sourcerev)
621 621 if tagid and tagname not in tags:
622 622 tags[tagname] = tagid
623 623 except SvnPathNotFound:
624 624 # It happens when we are following directories
625 625 # we assumed were copied with their parents
626 626 # but were really created in the tag
627 627 # directory.
628 628 pass
629 629 pendings = renamings
630 630 tagspath = srctagspath
631 631 finally:
632 632 stream.close()
633 633 return tags
634 634
635 635 def converted(self, rev, destrev):
636 636 if not self.wc:
637 637 return
638 638 if self.convertfp is None:
639 639 self.convertfp = open(os.path.join(self.wc, '.svn', 'hg-shamap'),
640 640 'a')
641 641 self.convertfp.write('%s %d\n' % (destrev, self.revnum(rev)))
642 642 self.convertfp.flush()
643 643
644 644 def revid(self, revnum, module=None):
645 645 return 'svn:%s%s@%s' % (self.uuid, module or self.module, revnum)
646 646
647 647 def revnum(self, rev):
648 648 return int(rev.split('@')[-1])
649 649
650 650 def latest(self, path, stop=None):
651 651 """Find the latest revid affecting path, up to stop revision
652 652 number. If stop is None, default to repository latest
653 653 revision. It may return a revision in a different module,
654 654 since a branch may be moved without a change being
655 655 reported. Return None if computed module does not belong to
656 656 rootmodule subtree.
657 657 """
658 658 def findchanges(path, start, stop=None):
659 659 stream = self._getlog([path], start, stop or 1)
660 660 try:
661 661 for entry in stream:
662 662 paths, revnum, author, date, message = entry
663 663 if stop is None and paths:
664 664 # We do not know the latest changed revision,
665 665 # keep the first one with changed paths.
666 666 break
667 667 if revnum <= stop:
668 668 break
669 669
670 670 for p in paths:
671 671 if (not path.startswith(p) or
672 672 not paths[p].copyfrom_path):
673 673 continue
674 674 newpath = paths[p].copyfrom_path + path[len(p):]
675 675 self.ui.debug("branch renamed from %s to %s at %d\n" %
676 676 (path, newpath, revnum))
677 677 path = newpath
678 678 break
679 679 if not paths:
680 680 revnum = None
681 681 return revnum, path
682 682 finally:
683 683 stream.close()
684 684
685 685 if not path.startswith(self.rootmodule):
686 686 # Requests on foreign branches may be forbidden at server level
687 687 self.ui.debug('ignoring foreign branch %r\n' % path)
688 688 return None
689 689
690 690 if stop is None:
691 691 stop = svn.ra.get_latest_revnum(self.ra)
692 692 try:
693 693 prevmodule = self.reparent('')
694 694 dirent = svn.ra.stat(self.ra, path.strip('/'), stop)
695 695 self.reparent(prevmodule)
696 696 except svn.core.SubversionException:
697 697 dirent = None
698 698 if not dirent:
699 699 raise SvnPathNotFound(_('%s not found up to revision %d')
700 700 % (path, stop))
701 701
702 702 # stat() gives us the previous revision on this line of
703 703 # development, but it might be in *another module*. Fetch the
704 704 # log and detect renames down to the latest revision.
705 705 revnum, realpath = findchanges(path, stop, dirent.created_rev)
706 706 if revnum is None:
707 707 # Tools like svnsync can create empty revision, when
708 708 # synchronizing only a subtree for instance. These empty
709 709 # revisions created_rev still have their original values
710 710 # despite all changes having disappeared and can be
711 711 # returned by ra.stat(), at least when stating the root
712 712 # module. In that case, do not trust created_rev and scan
713 713 # the whole history.
714 714 revnum, realpath = findchanges(path, stop)
715 715 if revnum is None:
716 716 self.ui.debug('ignoring empty branch %r\n' % realpath)
717 717 return None
718 718
719 719 if not realpath.startswith(self.rootmodule):
720 720 self.ui.debug('ignoring foreign branch %r\n' % realpath)
721 721 return None
722 722 return self.revid(revnum, realpath)
723 723
724 724 def reparent(self, module):
725 725 """Reparent the svn transport and return the previous parent."""
726 726 if self.prevmodule == module:
727 727 return module
728 728 svnurl = self.baseurl + quote(module)
729 729 prevmodule = self.prevmodule
730 730 if prevmodule is None:
731 731 prevmodule = ''
732 732 self.ui.debug("reparent to %s\n" % svnurl)
733 733 svn.ra.reparent(self.ra, svnurl)
734 734 self.prevmodule = module
735 735 return prevmodule
736 736
737 737 def expandpaths(self, rev, paths, parents):
738 738 changed, removed = set(), set()
739 739 copies = {}
740 740
741 741 new_module, revnum = revsplit(rev)[1:]
742 742 if new_module != self.module:
743 743 self.module = new_module
744 744 self.reparent(self.module)
745 745
746 746 for i, (path, ent) in enumerate(paths):
747 747 self.ui.progress(_('scanning paths'), i, item=path,
748 748 total=len(paths), unit=_('paths'))
749 749 entrypath = self.getrelpath(path)
750 750
751 751 kind = self._checkpath(entrypath, revnum)
752 752 if kind == svn.core.svn_node_file:
753 753 changed.add(self.recode(entrypath))
754 754 if not ent.copyfrom_path or not parents:
755 755 continue
756 756 # Copy sources not in parent revisions cannot be
757 757 # represented, ignore their origin for now
758 758 pmodule, prevnum = revsplit(parents[0])[1:]
759 759 if ent.copyfrom_rev < prevnum:
760 760 continue
761 761 copyfrom_path = self.getrelpath(ent.copyfrom_path, pmodule)
762 762 if not copyfrom_path:
763 763 continue
764 764 self.ui.debug("copied to %s from %s@%s\n" %
765 765 (entrypath, copyfrom_path, ent.copyfrom_rev))
766 766 copies[self.recode(entrypath)] = self.recode(copyfrom_path)
767 767 elif kind == 0: # gone, but had better be a deleted *file*
768 768 self.ui.debug("gone from %s\n" % ent.copyfrom_rev)
769 769 pmodule, prevnum = revsplit(parents[0])[1:]
770 770 parentpath = pmodule + "/" + entrypath
771 771 fromkind = self._checkpath(entrypath, prevnum, pmodule)
772 772
773 773 if fromkind == svn.core.svn_node_file:
774 774 removed.add(self.recode(entrypath))
775 775 elif fromkind == svn.core.svn_node_dir:
776 776 oroot = parentpath.strip('/')
777 777 nroot = path.strip('/')
778 778 children = self._iterfiles(oroot, prevnum)
779 779 for childpath in children:
780 780 childpath = childpath.replace(oroot, nroot)
781 781 childpath = self.getrelpath("/" + childpath, pmodule)
782 782 if childpath:
783 783 removed.add(self.recode(childpath))
784 784 else:
785 785 self.ui.debug('unknown path in revision %d: %s\n' % \
786 786 (revnum, path))
787 787 elif kind == svn.core.svn_node_dir:
788 788 if ent.action == 'M':
789 789 # If the directory just had a prop change,
790 790 # then we shouldn't need to look for its children.
791 791 continue
792 792 if ent.action == 'R' and parents:
793 793 # If a directory is replacing a file, mark the previous
794 794 # file as deleted
795 795 pmodule, prevnum = revsplit(parents[0])[1:]
796 796 pkind = self._checkpath(entrypath, prevnum, pmodule)
797 797 if pkind == svn.core.svn_node_file:
798 798 removed.add(self.recode(entrypath))
799 799 elif pkind == svn.core.svn_node_dir:
800 800 # We do not know what files were kept or removed,
801 801 # mark them all as changed.
802 802 for childpath in self._iterfiles(pmodule, prevnum):
803 803 childpath = self.getrelpath("/" + childpath)
804 804 if childpath:
805 805 changed.add(self.recode(childpath))
806 806
807 807 for childpath in self._iterfiles(path, revnum):
808 808 childpath = self.getrelpath("/" + childpath)
809 809 if childpath:
810 810 changed.add(self.recode(childpath))
811 811
812 812 # Handle directory copies
813 813 if not ent.copyfrom_path or not parents:
814 814 continue
815 815 # Copy sources not in parent revisions cannot be
816 816 # represented, ignore their origin for now
817 817 pmodule, prevnum = revsplit(parents[0])[1:]
818 818 if ent.copyfrom_rev < prevnum:
819 819 continue
820 820 copyfrompath = self.getrelpath(ent.copyfrom_path, pmodule)
821 821 if not copyfrompath:
822 822 continue
823 823 self.ui.debug("mark %s came from %s:%d\n"
824 824 % (path, copyfrompath, ent.copyfrom_rev))
825 825 children = self._iterfiles(ent.copyfrom_path, ent.copyfrom_rev)
826 826 for childpath in children:
827 827 childpath = self.getrelpath("/" + childpath, pmodule)
828 828 if not childpath:
829 829 continue
830 830 copytopath = path + childpath[len(copyfrompath):]
831 831 copytopath = self.getrelpath(copytopath)
832 832 copies[self.recode(copytopath)] = self.recode(childpath)
833 833
834 834 self.ui.progress(_('scanning paths'), None)
835 835 changed.update(removed)
836 836 return (list(changed), removed, copies)
837 837
838 838 def _fetch_revisions(self, from_revnum, to_revnum):
839 839 if from_revnum < to_revnum:
840 840 from_revnum, to_revnum = to_revnum, from_revnum
841 841
842 842 self.child_cset = None
843 843
844 844 def parselogentry(orig_paths, revnum, author, date, message):
845 845 """Return the parsed commit object or None, and True if
846 846 the revision is a branch root.
847 847 """
848 848 self.ui.debug("parsing revision %d (%d changes)\n" %
849 849 (revnum, len(orig_paths)))
850 850
851 851 branched = False
852 852 rev = self.revid(revnum)
853 853 # branch log might return entries for a parent we already have
854 854
855 855 if rev in self.commits or revnum < to_revnum:
856 856 return None, branched
857 857
858 858 parents = []
859 859 # check whether this revision is the start of a branch or part
860 860 # of a branch renaming
861 861 orig_paths = sorted(orig_paths.iteritems())
862 862 root_paths = [(p, e) for p, e in orig_paths
863 863 if self.module.startswith(p)]
864 864 if root_paths:
865 865 path, ent = root_paths[-1]
866 866 if ent.copyfrom_path:
867 867 branched = True
868 868 newpath = ent.copyfrom_path + self.module[len(path):]
869 869 # ent.copyfrom_rev may not be the actual last revision
870 870 previd = self.latest(newpath, ent.copyfrom_rev)
871 871 if previd is not None:
872 872 prevmodule, prevnum = revsplit(previd)[1:]
873 873 if prevnum >= self.startrev:
874 874 parents = [previd]
875 875 self.ui.note(
876 876 _('found parent of branch %s at %d: %s\n') %
877 877 (self.module, prevnum, prevmodule))
878 878 else:
879 879 self.ui.debug("no copyfrom path, don't know what to do.\n")
880 880
881 881 paths = []
882 882 # filter out unrelated paths
883 883 for path, ent in orig_paths:
884 884 if self.getrelpath(path) is None:
885 885 continue
886 886 paths.append((path, ent))
887 887
888 888 # Example SVN datetime. Includes microseconds.
889 889 # ISO-8601 conformant
890 890 # '2007-01-04T17:35:00.902377Z'
891 891 date = util.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"])
892 892 if self.ui.configbool('convert', 'localtimezone'):
893 893 date = makedatetimestamp(date[0])
894 894
895 895 if message:
896 896 log = self.recode(message)
897 897 else:
898 898 log = ''
899 899
900 900 if author:
901 901 author = self.recode(author)
902 902 else:
903 903 author = ''
904 904
905 905 try:
906 906 branch = self.module.split("/")[-1]
907 907 if branch == self.trunkname:
908 908 branch = None
909 909 except IndexError:
910 910 branch = None
911 911
912 912 cset = commit(author=author,
913 913 date=util.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'),
914 914 desc=log,
915 915 parents=parents,
916 916 branch=branch,
917 917 rev=rev)
918 918
919 919 self.commits[rev] = cset
920 920 # The parents list is *shared* among self.paths and the
921 921 # commit object. Both will be updated below.
922 922 self.paths[rev] = (paths, cset.parents)
923 923 if self.child_cset and not self.child_cset.parents:
924 924 self.child_cset.parents[:] = [rev]
925 925 self.child_cset = cset
926 926 return cset, branched
927 927
928 928 self.ui.note(_('fetching revision log for "%s" from %d to %d\n') %
929 929 (self.module, from_revnum, to_revnum))
930 930
931 931 try:
932 932 firstcset = None
933 933 lastonbranch = False
934 934 stream = self._getlog([self.module], from_revnum, to_revnum)
935 935 try:
936 936 for entry in stream:
937 937 paths, revnum, author, date, message = entry
938 938 if revnum < self.startrev:
939 939 lastonbranch = True
940 940 break
941 941 if not paths:
942 942 self.ui.debug('revision %d has no entries\n' % revnum)
943 943 # If we ever leave the loop on an empty
944 944 # revision, do not try to get a parent branch
945 945 lastonbranch = lastonbranch or revnum == 0
946 946 continue
947 947 cset, lastonbranch = parselogentry(paths, revnum, author,
948 948 date, message)
949 949 if cset:
950 950 firstcset = cset
951 951 if lastonbranch:
952 952 break
953 953 finally:
954 954 stream.close()
955 955
956 956 if not lastonbranch and firstcset and not firstcset.parents:
957 957 # The first revision of the sequence (the last fetched one)
958 958 # has invalid parents if not a branch root. Find the parent
959 959 # revision now, if any.
960 960 try:
961 961 firstrevnum = self.revnum(firstcset.rev)
962 962 if firstrevnum > 1:
963 963 latest = self.latest(self.module, firstrevnum - 1)
964 964 if latest:
965 965 firstcset.parents.append(latest)
966 966 except SvnPathNotFound:
967 967 pass
968 968 except svn.core.SubversionException as xxx_todo_changeme:
969 969 (inst, num) = xxx_todo_changeme.args
970 970 if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION:
971 971 raise error.Abort(_('svn: branch has no revision %s')
972 972 % to_revnum)
973 973 raise
974 974
975 975 def getfile(self, file, rev):
976 976 # TODO: ra.get_file transmits the whole file instead of diffs.
977 977 if file in self.removed:
978 978 return None, None
979 979 mode = ''
980 980 try:
981 981 new_module, revnum = revsplit(rev)[1:]
982 982 if self.module != new_module:
983 983 self.module = new_module
984 984 self.reparent(self.module)
985 985 io = stringio()
986 986 info = svn.ra.get_file(self.ra, file, revnum, io)
987 987 data = io.getvalue()
988 988 # ra.get_file() seems to keep a reference on the input buffer
989 989 # preventing collection. Release it explicitly.
990 990 io.close()
991 991 if isinstance(info, list):
992 992 info = info[-1]
993 993 mode = ("svn:executable" in info) and 'x' or ''
994 994 mode = ("svn:special" in info) and 'l' or mode
995 995 except svn.core.SubversionException as e:
996 996 notfound = (svn.core.SVN_ERR_FS_NOT_FOUND,
997 997 svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND)
998 998 if e.apr_err in notfound: # File not found
999 999 return None, None
1000 1000 raise
1001 1001 if mode == 'l':
1002 1002 link_prefix = "link "
1003 1003 if data.startswith(link_prefix):
1004 1004 data = data[len(link_prefix):]
1005 1005 return data, mode
1006 1006
1007 1007 def _iterfiles(self, path, revnum):
1008 1008 """Enumerate all files in path at revnum, recursively."""
1009 1009 path = path.strip('/')
1010 1010 pool = svn.core.Pool()
1011 1011 rpath = '/'.join([self.baseurl, quote(path)]).strip('/')
1012 1012 entries = svn.client.ls(rpath, optrev(revnum), True, self.ctx, pool)
1013 1013 if path:
1014 1014 path += '/'
1015 1015 return ((path + p) for p, e in entries.iteritems()
1016 1016 if e.kind == svn.core.svn_node_file)
1017 1017
1018 1018 def getrelpath(self, path, module=None):
1019 1019 if module is None:
1020 1020 module = self.module
1021 1021 # Given the repository url of this wc, say
1022 1022 # "http://server/plone/CMFPlone/branches/Plone-2_0-branch"
1023 1023 # extract the "entry" portion (a relative path) from what
1024 1024 # svn log --xml says, i.e.
1025 1025 # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py"
1026 1026 # that is to say "tests/PloneTestCase.py"
1027 1027 if path.startswith(module):
1028 1028 relative = path.rstrip('/')[len(module):]
1029 1029 if relative.startswith('/'):
1030 1030 return relative[1:]
1031 1031 elif relative == '':
1032 1032 return relative
1033 1033
1034 1034 # The path is outside our tracked tree...
1035 1035 self.ui.debug('%r is not under %r, ignoring\n' % (path, module))
1036 1036 return None
1037 1037
1038 1038 def _checkpath(self, path, revnum, module=None):
1039 1039 if module is not None:
1040 1040 prevmodule = self.reparent('')
1041 1041 path = module + '/' + path
1042 1042 try:
1043 1043 # ra.check_path does not like leading slashes very much, it leads
1044 1044 # to PROPFIND subversion errors
1045 1045 return svn.ra.check_path(self.ra, path.strip('/'), revnum)
1046 1046 finally:
1047 1047 if module is not None:
1048 1048 self.reparent(prevmodule)
1049 1049
1050 1050 def _getlog(self, paths, start, end, limit=0, discover_changed_paths=True,
1051 1051 strict_node_history=False):
1052 1052 # Normalize path names, svn >= 1.5 only wants paths relative to
1053 1053 # supplied URL
1054 1054 relpaths = []
1055 1055 for p in paths:
1056 1056 if not p.startswith('/'):
1057 1057 p = self.module + '/' + p
1058 1058 relpaths.append(p.strip('/'))
1059 1059 args = [self.baseurl, relpaths, start, end, limit,
1060 1060 discover_changed_paths, strict_node_history]
1061 1061 # developer config: convert.svn.debugsvnlog
1062 1062 if not self.ui.configbool('convert', 'svn.debugsvnlog', True):
1063 1063 return directlogstream(*args)
1064 1064 arg = encodeargs(args)
1065 1065 hgexe = util.hgexecutable()
1066 1066 cmd = '%s debugsvnlog' % util.shellquote(hgexe)
1067 1067 stdin, stdout = util.popen2(util.quotecommand(cmd))
1068 1068 stdin.write(arg)
1069 1069 try:
1070 1070 stdin.close()
1071 1071 except IOError:
1072 1072 raise error.Abort(_('Mercurial failed to run itself, check'
1073 1073 ' hg executable is in PATH'))
1074 1074 return logstream(stdout)
1075 1075
1076 1076 pre_revprop_change = '''#!/bin/sh
1077 1077
1078 1078 REPOS="$1"
1079 1079 REV="$2"
1080 1080 USER="$3"
1081 1081 PROPNAME="$4"
1082 1082 ACTION="$5"
1083 1083
1084 1084 if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi
1085 1085 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-branch" ]; then exit 0; fi
1086 1086 if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-rev" ]; then exit 0; fi
1087 1087
1088 1088 echo "Changing prohibited revision property" >&2
1089 1089 exit 1
1090 1090 '''
1091 1091
1092 1092 class svn_sink(converter_sink, commandline):
1093 1093 commit_re = re.compile(r'Committed revision (\d+).', re.M)
1094 1094 uuid_re = re.compile(r'Repository UUID:\s*(\S+)', re.M)
1095 1095
1096 1096 def prerun(self):
1097 1097 if self.wc:
1098 1098 os.chdir(self.wc)
1099 1099
1100 1100 def postrun(self):
1101 1101 if self.wc:
1102 1102 os.chdir(self.cwd)
1103 1103
1104 1104 def join(self, name):
1105 1105 return os.path.join(self.wc, '.svn', name)
1106 1106
1107 1107 def revmapfile(self):
1108 1108 return self.join('hg-shamap')
1109 1109
1110 1110 def authorfile(self):
1111 1111 return self.join('hg-authormap')
1112 1112
1113 1113 def __init__(self, ui, path):
1114 1114
1115 1115 converter_sink.__init__(self, ui, path)
1116 1116 commandline.__init__(self, ui, 'svn')
1117 1117 self.delete = []
1118 1118 self.setexec = []
1119 1119 self.delexec = []
1120 1120 self.copies = []
1121 1121 self.wc = None
1122 1122 self.cwd = pycompat.getcwd()
1123 1123
1124 1124 created = False
1125 1125 if os.path.isfile(os.path.join(path, '.svn', 'entries')):
1126 1126 self.wc = os.path.realpath(path)
1127 1127 self.run0('update')
1128 1128 else:
1129 1129 if not re.search(r'^(file|http|https|svn|svn\+ssh)\://', path):
1130 1130 path = os.path.realpath(path)
1131 1131 if os.path.isdir(os.path.dirname(path)):
1132 1132 if not os.path.exists(os.path.join(path, 'db', 'fs-type')):
1133 1133 ui.status(_('initializing svn repository %r\n') %
1134 1134 os.path.basename(path))
1135 1135 commandline(ui, 'svnadmin').run0('create', path)
1136 1136 created = path
1137 1137 path = util.normpath(path)
1138 1138 if not path.startswith('/'):
1139 1139 path = '/' + path
1140 1140 path = 'file://' + path
1141 1141
1142 1142 wcpath = os.path.join(pycompat.getcwd(), os.path.basename(path) +
1143 1143 '-wc')
1144 1144 ui.status(_('initializing svn working copy %r\n')
1145 1145 % os.path.basename(wcpath))
1146 1146 self.run0('checkout', path, wcpath)
1147 1147
1148 1148 self.wc = wcpath
1149 self.opener = scmutil.vfs(self.wc)
1150 self.wopener = scmutil.vfs(self.wc)
1149 self.opener = vfsmod.vfs(self.wc)
1150 self.wopener = vfsmod.vfs(self.wc)
1151 1151 self.childmap = mapfile(ui, self.join('hg-childmap'))
1152 1152 if util.checkexec(self.wc):
1153 1153 self.is_exec = util.isexec
1154 1154 else:
1155 1155 self.is_exec = None
1156 1156
1157 1157 if created:
1158 1158 hook = os.path.join(created, 'hooks', 'pre-revprop-change')
1159 1159 fp = open(hook, 'w')
1160 1160 fp.write(pre_revprop_change)
1161 1161 fp.close()
1162 1162 util.setflags(hook, False, True)
1163 1163
1164 1164 output = self.run0('info')
1165 1165 self.uuid = self.uuid_re.search(output).group(1).strip()
1166 1166
1167 1167 def wjoin(self, *names):
1168 1168 return os.path.join(self.wc, *names)
1169 1169
1170 1170 @propertycache
1171 1171 def manifest(self):
1172 1172 # As of svn 1.7, the "add" command fails when receiving
1173 1173 # already tracked entries, so we have to track and filter them
1174 1174 # ourselves.
1175 1175 m = set()
1176 1176 output = self.run0('ls', recursive=True, xml=True)
1177 1177 doc = xml.dom.minidom.parseString(output)
1178 1178 for e in doc.getElementsByTagName('entry'):
1179 1179 for n in e.childNodes:
1180 1180 if n.nodeType != n.ELEMENT_NODE or n.tagName != 'name':
1181 1181 continue
1182 1182 name = ''.join(c.data for c in n.childNodes
1183 1183 if c.nodeType == c.TEXT_NODE)
1184 1184 # Entries are compared with names coming from
1185 1185 # mercurial, so bytes with undefined encoding. Our
1186 1186 # best bet is to assume they are in local
1187 1187 # encoding. They will be passed to command line calls
1188 1188 # later anyway, so they better be.
1189 1189 m.add(encoding.tolocal(name.encode('utf-8')))
1190 1190 break
1191 1191 return m
1192 1192
1193 1193 def putfile(self, filename, flags, data):
1194 1194 if 'l' in flags:
1195 1195 self.wopener.symlink(data, filename)
1196 1196 else:
1197 1197 try:
1198 1198 if os.path.islink(self.wjoin(filename)):
1199 1199 os.unlink(filename)
1200 1200 except OSError:
1201 1201 pass
1202 1202 self.wopener.write(filename, data)
1203 1203
1204 1204 if self.is_exec:
1205 1205 if self.is_exec(self.wjoin(filename)):
1206 1206 if 'x' not in flags:
1207 1207 self.delexec.append(filename)
1208 1208 else:
1209 1209 if 'x' in flags:
1210 1210 self.setexec.append(filename)
1211 1211 util.setflags(self.wjoin(filename), False, 'x' in flags)
1212 1212
1213 1213 def _copyfile(self, source, dest):
1214 1214 # SVN's copy command pukes if the destination file exists, but
1215 1215 # our copyfile method expects to record a copy that has
1216 1216 # already occurred. Cross the semantic gap.
1217 1217 wdest = self.wjoin(dest)
1218 1218 exists = os.path.lexists(wdest)
1219 1219 if exists:
1220 1220 fd, tempname = tempfile.mkstemp(
1221 1221 prefix='hg-copy-', dir=os.path.dirname(wdest))
1222 1222 os.close(fd)
1223 1223 os.unlink(tempname)
1224 1224 os.rename(wdest, tempname)
1225 1225 try:
1226 1226 self.run0('copy', source, dest)
1227 1227 finally:
1228 1228 self.manifest.add(dest)
1229 1229 if exists:
1230 1230 try:
1231 1231 os.unlink(wdest)
1232 1232 except OSError:
1233 1233 pass
1234 1234 os.rename(tempname, wdest)
1235 1235
1236 1236 def dirs_of(self, files):
1237 1237 dirs = set()
1238 1238 for f in files:
1239 1239 if os.path.isdir(self.wjoin(f)):
1240 1240 dirs.add(f)
1241 1241 i = len(f)
1242 1242 for i in iter(lambda: f.rfind('/', 0, i), -1):
1243 1243 dirs.add(f[:i])
1244 1244 return dirs
1245 1245
1246 1246 def add_dirs(self, files):
1247 1247 add_dirs = [d for d in sorted(self.dirs_of(files))
1248 1248 if d not in self.manifest]
1249 1249 if add_dirs:
1250 1250 self.manifest.update(add_dirs)
1251 1251 self.xargs(add_dirs, 'add', non_recursive=True, quiet=True)
1252 1252 return add_dirs
1253 1253
1254 1254 def add_files(self, files):
1255 1255 files = [f for f in files if f not in self.manifest]
1256 1256 if files:
1257 1257 self.manifest.update(files)
1258 1258 self.xargs(files, 'add', quiet=True)
1259 1259 return files
1260 1260
1261 1261 def addchild(self, parent, child):
1262 1262 self.childmap[parent] = child
1263 1263
1264 1264 def revid(self, rev):
1265 1265 return u"svn:%s@%s" % (self.uuid, rev)
1266 1266
1267 1267 def putcommit(self, files, copies, parents, commit, source, revmap, full,
1268 1268 cleanp2):
1269 1269 for parent in parents:
1270 1270 try:
1271 1271 return self.revid(self.childmap[parent])
1272 1272 except KeyError:
1273 1273 pass
1274 1274
1275 1275 # Apply changes to working copy
1276 1276 for f, v in files:
1277 1277 data, mode = source.getfile(f, v)
1278 1278 if data is None:
1279 1279 self.delete.append(f)
1280 1280 else:
1281 1281 self.putfile(f, mode, data)
1282 1282 if f in copies:
1283 1283 self.copies.append([copies[f], f])
1284 1284 if full:
1285 1285 self.delete.extend(sorted(self.manifest.difference(files)))
1286 1286 files = [f[0] for f in files]
1287 1287
1288 1288 entries = set(self.delete)
1289 1289 files = frozenset(files)
1290 1290 entries.update(self.add_dirs(files.difference(entries)))
1291 1291 if self.copies:
1292 1292 for s, d in self.copies:
1293 1293 self._copyfile(s, d)
1294 1294 self.copies = []
1295 1295 if self.delete:
1296 1296 self.xargs(self.delete, 'delete')
1297 1297 for f in self.delete:
1298 1298 self.manifest.remove(f)
1299 1299 self.delete = []
1300 1300 entries.update(self.add_files(files.difference(entries)))
1301 1301 if self.delexec:
1302 1302 self.xargs(self.delexec, 'propdel', 'svn:executable')
1303 1303 self.delexec = []
1304 1304 if self.setexec:
1305 1305 self.xargs(self.setexec, 'propset', 'svn:executable', '*')
1306 1306 self.setexec = []
1307 1307
1308 1308 fd, messagefile = tempfile.mkstemp(prefix='hg-convert-')
1309 1309 fp = os.fdopen(fd, pycompat.sysstr('w'))
1310 1310 fp.write(commit.desc)
1311 1311 fp.close()
1312 1312 try:
1313 1313 output = self.run0('commit',
1314 1314 username=util.shortuser(commit.author),
1315 1315 file=messagefile,
1316 1316 encoding='utf-8')
1317 1317 try:
1318 1318 rev = self.commit_re.search(output).group(1)
1319 1319 except AttributeError:
1320 1320 if parents and not files:
1321 1321 return parents[0]
1322 1322 self.ui.warn(_('unexpected svn output:\n'))
1323 1323 self.ui.warn(output)
1324 1324 raise error.Abort(_('unable to cope with svn output'))
1325 1325 if commit.rev:
1326 1326 self.run('propset', 'hg:convert-rev', commit.rev,
1327 1327 revprop=True, revision=rev)
1328 1328 if commit.branch and commit.branch != 'default':
1329 1329 self.run('propset', 'hg:convert-branch', commit.branch,
1330 1330 revprop=True, revision=rev)
1331 1331 for parent in parents:
1332 1332 self.addchild(parent, rev)
1333 1333 return self.revid(rev)
1334 1334 finally:
1335 1335 os.unlink(messagefile)
1336 1336
1337 1337 def puttags(self, tags):
1338 1338 self.ui.warn(_('writing Subversion tags is not yet implemented\n'))
1339 1339 return None, None
1340 1340
1341 1341 def hascommitfrommap(self, rev):
1342 1342 # We trust that revisions referenced in a map still is present
1343 1343 # TODO: implement something better if necessary and feasible
1344 1344 return True
1345 1345
1346 1346 def hascommitforsplicemap(self, rev):
1347 1347 # This is not correct as one can convert to an existing subversion
1348 1348 # repository and childmap would not list all revisions. Too bad.
1349 1349 if rev in self.childmap:
1350 1350 return True
1351 1351 raise error.Abort(_('splice map revision %s not found in subversion '
1352 1352 'child map (revision lookups are not implemented)')
1353 1353 % rev)
General Comments 0
You need to be logged in to leave comments. Login now