subversion.py
1368 lines
| 53.1 KiB
| text/x-python
|
PythonLexer
|
r4765 | # Subversion 1.4/1.5 Python API backend | ||
# | ||||
# Copyright(C) 2007 Daniel Holth et al | ||||
|
r28408 | from __future__ import absolute_import | ||
|
r4765 | |||
|
r28408 | import os | ||
import re | ||||
|
r19787 | import xml.dom.minidom | ||
|
r5513 | |||
|
r29205 | from mercurial.i18n import _ | ||
|
r28408 | from mercurial import ( | ||
encoding, | ||||
error, | ||||
|
r30519 | pycompat, | ||
|
r28408 | util, | ||
|
r31246 | vfs as vfsmod, | ||
|
r28408 | ) | ||
|
r37102 | from mercurial.utils import ( | ||
dateutil, | ||||
|
r37138 | procutil, | ||
|
r37102 | stringutil, | ||
) | ||||
|
r4765 | |||
|
r28408 | from . import common | ||
|
r29324 | pickle = util.pickle | ||
|
r28861 | stringio = util.stringio | ||
|
r16511 | propertycache = util.propertycache | ||
|
r28883 | urlerr = util.urlerr | ||
urlreq = util.urlreq | ||||
|
r16511 | |||
|
r28408 | commandline = common.commandline | ||
commit = common.commit | ||||
converter_sink = common.converter_sink | ||||
converter_source = common.converter_source | ||||
decodeargs = common.decodeargs | ||||
encodeargs = common.encodeargs | ||||
makedatetimestamp = common.makedatetimestamp | ||||
mapfile = common.mapfile | ||||
MissingTool = common.MissingTool | ||||
NoRepo = common.NoRepo | ||||
|
r4765 | # Subversion stuff. Works best with very recent Python SVN bindings | ||
# e.g. SVN 1.5 or backports. Thanks to the bzr folks for enhancing | ||||
# these bindings. | ||||
|
r4766 | try: | ||
|
r5010 | import svn | ||
import svn.client | ||||
|
r4766 | import svn.core | ||
import svn.ra | ||||
import svn.delta | ||||
|
r28459 | from . import transport | ||
|
r8221 | import warnings | ||
warnings.filterwarnings('ignore', | ||||
module='svn.core', | ||||
category=DeprecationWarning) | ||||
|
r28460 | svn.core.SubversionException # trigger import to catch error | ||
|
r8221 | |||
|
r4766 | except ImportError: | ||
|
r13480 | svn = None | ||
|
r4765 | |||
|
r7381 | class SvnPathNotFound(Exception): | ||
pass | ||||
|
r13690 | def revsplit(rev): | ||
|
r20419 | """Parse a revision string and return (uuid, path, revnum). | ||
|
r34133 | >>> revsplit(b'svn:a2147622-4a9f-4db4-a8d3-13562ff547b2' | ||
... b'/proj%20B/mytrunk/mytrunk@1') | ||||
|
r20419 | ('a2147622-4a9f-4db4-a8d3-13562ff547b2', '/proj%20B/mytrunk/mytrunk', 1) | ||
|
r34133 | >>> revsplit(b'svn:8af66a51-67f5-4354-b62c-98d67cc7be1d@1') | ||
|
r20419 | ('', '', 1) | ||
|
r34133 | >>> revsplit(b'@7') | ||
|
r20419 | ('', '', 7) | ||
|
r34133 | >>> revsplit(b'7') | ||
|
r20419 | ('', '', 0) | ||
|
r34133 | >>> revsplit(b'bad') | ||
|
r20419 | ('', '', 0) | ||
""" | ||||
parts = rev.rsplit('@', 1) | ||||
revnum = 0 | ||||
if len(parts) > 1: | ||||
revnum = int(parts[1]) | ||||
parts = parts[0].split('/', 1) | ||||
uuid = '' | ||||
|
r13690 | mod = '' | ||
|
r20419 | if len(parts) > 1 and parts[0].startswith('svn:'): | ||
uuid = parts[0][4:] | ||||
|
r13690 | mod = '/' + parts[1] | ||
|
r20419 | return uuid, mod, revnum | ||
|
r13690 | |||
|
r15599 | def quote(s): | ||
# As of svn 1.7, many svn calls expect "canonical" paths. In | ||||
# theory, we should call svn.core.*canonicalize() on all paths | ||||
# before passing them to the API. Instead, we assume the base url | ||||
# is canonical and copy the behaviour of svn URL encoding function | ||||
# so we can extend it safely with new components. The "safe" | ||||
# characters were taken from the "svn_uri__char_validity" table in | ||||
# libsvn_subr/path.c. | ||||
|
r28883 | return urlreq.quote(s, "!$&'()*+,-./:=@_~") | ||
|
r15599 | |||
|
r5008 | def geturl(path): | ||
|
r5010 | try: | ||
|
r5020 | return svn.client.url_from_path(svn.core.svn_path_canonicalize(path)) | ||
|
r28460 | except svn.core.SubversionException: | ||
|
r15599 | # svn.client.url_from_path() fails with local repositories | ||
|
r5010 | pass | ||
|
r5008 | if os.path.isdir(path): | ||
|
r5793 | path = os.path.normpath(os.path.abspath(path)) | ||
|
r34646 | if pycompat.iswindows: | ||
|
r5842 | path = '/' + util.normpath(path) | ||
|
r8886 | # Module URL is later compared with the repository URL returned | ||
# by svn API, which is UTF-8. | ||||
path = encoding.tolocal(path) | ||||
|
r15599 | path = 'file://%s' % quote(path) | ||
return svn.core.svn_path_canonicalize(path) | ||||
|
r5008 | |||
|
r5117 | def optrev(number): | ||
optrev = svn.core.svn_opt_revision_t() | ||||
optrev.kind = svn.core.svn_opt_revision_number | ||||
optrev.value.number = number | ||||
return optrev | ||||
|
r4946 | class changedpath(object): | ||
def __init__(self, p): | ||||
self.copyfrom_path = p.copyfrom_path | ||||
self.copyfrom_rev = p.copyfrom_rev | ||||
self.action = p.action | ||||
|
r16683 | def get_log_child(fp, url, paths, start, end, limit=0, | ||
discover_changed_paths=True, strict_node_history=False): | ||||
|
r5127 | protocol = -1 | ||
def receiver(orig_paths, revnum, author, date, message, pool): | ||||
|
r20057 | paths = {} | ||
|
r5127 | if orig_paths is not None: | ||
for k, v in orig_paths.iteritems(): | ||||
|
r20057 | paths[k] = changedpath(v) | ||
pickle.dump((paths, revnum, author, date, message), | ||||
|
r5143 | fp, protocol) | ||
|
r5127 | try: | ||
# Use an ra of our own so that our parent can consume | ||||
# our results without confusing the server. | ||||
t = transport.SvnRaTransport(url=url) | ||||
svn.ra.get_log(t.ra, paths, start, end, limit, | ||||
discover_changed_paths, | ||||
strict_node_history, | ||||
receiver) | ||||
|
r5873 | except IOError: | ||
# Caller may interrupt the iteration | ||||
pickle.dump(None, fp, protocol) | ||||
|
r25660 | except Exception as inst: | ||
|
r37102 | pickle.dump(stringutil.forcebytestr(inst), fp, protocol) | ||
|
r5127 | else: | ||
pickle.dump(None, fp, protocol) | ||||
|
r36529 | fp.flush() | ||
|
r6397 | # With large history, cleanup process goes crazy and suddenly | ||
# consumes *huge* amount of memory. The output file being closed, | ||||
# there is no need for clean termination. | ||||
os._exit(0) | ||||
|
r5127 | |||
|
r5139 | def debugsvnlog(ui, **opts): | ||
"""Fetch SVN log in a subprocess and channel them back to parent to | ||||
avoid memory collection issues. | ||||
""" | ||||
|
r17053 | if svn is None: | ||
|
r26587 | raise error.Abort(_('debugsvnlog could not load Subversion python ' | ||
|
r17053 | 'bindings')) | ||
|
r30261 | args = decodeargs(ui.fin.read()) | ||
get_log_child(ui.fout, *args) | ||||
|
r5139 | |||
|
r8778 | class logstream(object): | ||
|
r5873 | """Interruptible revision log iterator.""" | ||
def __init__(self, stdout): | ||||
self._stdout = stdout | ||||
def __iter__(self): | ||||
while True: | ||||
|
r9587 | try: | ||
entry = pickle.load(self._stdout) | ||||
except EOFError: | ||||
|
r26587 | raise error.Abort(_('Mercurial failed to run itself, check' | ||
|
r9587 | ' hg executable is in PATH')) | ||
|
r5873 | try: | ||
orig_paths, revnum, author, date, message = entry | ||||
|
r16688 | except (TypeError, ValueError): | ||
|
r5873 | if entry is None: | ||
break | ||||
|
r26587 | raise error.Abort(_("log stream exception '%s'") % entry) | ||
|
r5873 | yield entry | ||
def close(self): | ||||
if self._stdout: | ||||
self._stdout.close() | ||||
self._stdout = None | ||||
|
r20420 | class directlogstream(list): | ||
"""Direct revision log iterator. | ||||
This can be used for debugging and development but it will probably leak | ||||
memory and is not suitable for real conversions.""" | ||||
def __init__(self, url, paths, start, end, limit=0, | ||||
discover_changed_paths=True, strict_node_history=False): | ||||
def receiver(orig_paths, revnum, author, date, message, pool): | ||||
paths = {} | ||||
if orig_paths is not None: | ||||
for k, v in orig_paths.iteritems(): | ||||
paths[k] = changedpath(v) | ||||
self.append((paths, revnum, author, date, message)) | ||||
# Use an ra of our own so that our parent can consume | ||||
# our results without confusing the server. | ||||
t = transport.SvnRaTransport(url=url) | ||||
svn.ra.get_log(t.ra, paths, start, end, limit, | ||||
discover_changed_paths, | ||||
strict_node_history, | ||||
receiver) | ||||
def close(self): | ||||
pass | ||||
|
r8074 | |||
# Check to see if the given path is a local Subversion repo. Verify this by | ||||
# looking for several svn-specific files and directories in the given | ||||
# directory. | ||||
|
r9829 | def filecheck(ui, path, proto): | ||
|
r10282 | for x in ('locks', 'hooks', 'format', 'db'): | ||
|
r8074 | if not os.path.exists(os.path.join(path, x)): | ||
return False | ||||
return True | ||||
# Check to see if a given path is the root of an svn repo over http. We verify | ||||
# this by requesting a version-controlled URL we know can't exist and looking | ||||
# for the svn-specific "not found" XML. | ||||
|
r9829 | def httpcheck(ui, path, proto): | ||
try: | ||||
|
r28883 | opener = urlreq.buildopener() | ||
|
r36149 | rsp = opener.open('%s://%s/!svn/ver/0/.svn' % (proto, path), 'rb') | ||
|
r10282 | data = rsp.read() | ||
|
r28883 | except urlerr.httperror as inst: | ||
|
r9838 | if inst.code != 404: | ||
# Except for 404 we cannot know for sure this is not an svn repo | ||||
|
r9860 | ui.warn(_('svn: cannot probe remote repository, assume it could ' | ||
'be a subversion repository. Use --source-type if you ' | ||||
'know better.\n')) | ||||
|
r9838 | return True | ||
data = inst.fp.read() | ||||
|
r16689 | except Exception: | ||
|
r28883 | # Could be urlerr.urlerror if the URL is invalid or anything else. | ||
|
r9829 | return False | ||
|
r9838 | return '<m:human-readable errcode="160013">' in data | ||
|
r8074 | |||
protomap = {'http': httpcheck, | ||||
'https': httpcheck, | ||||
'file': filecheck, | ||||
} | ||||
|
r9829 | def issvnurl(ui, url): | ||
|
r8764 | try: | ||
proto, path = url.split('://', 1) | ||||
|
r9521 | if proto == 'file': | ||
|
r34646 | if (pycompat.iswindows and path[:1] == '/' | ||
|
r30640 | and path[1:2].isalpha() and path[2:6].lower() == '%3a/'): | ||
|
r17052 | path = path[:2] + ':/' + path[6:] | ||
|
r28883 | path = urlreq.url2pathname(path) | ||
|
r8764 | except ValueError: | ||
proto = 'file' | ||||
path = os.path.abspath(url) | ||||
|
r9521 | if proto == 'file': | ||
|
r16067 | path = util.pconvert(path) | ||
|
r10885 | check = protomap.get(proto, lambda *args: False) | ||
|
r8074 | while '/' in path: | ||
|
r9829 | if check(ui, path, proto): | ||
|
r8074 | return True | ||
path = path.rsplit('/', 1)[0] | ||||
return False | ||||
|
r4765 | # SVN conversion code stolen from bzr-svn and tailor | ||
|
r5876 | # | ||
# Subversion looks like a versioned filesystem, branches structures | ||||
# are defined by conventions and not enforced by the tool. First, | ||||
# we define the potential branches (modules) as "trunk" and "branches" | ||||
# children directories. Revisions are then identified by their | ||||
# module and revision number (and a repository identifier). | ||||
# | ||||
# The revision graph is really a tree (or a forest). By default, a | ||||
# revision parent is the previous revision in the same module. If the | ||||
# module directory is copied/moved from another module then the | ||||
# revision is the module root and its parent the source revision in | ||||
# the parent module. A revision has at most one parent. | ||||
# | ||||
|
r5438 | class svn_source(converter_source): | ||
|
r35168 | def __init__(self, ui, repotype, url, revs=None): | ||
super(svn_source, self).__init__(ui, repotype, url, revs=revs) | ||||
|
r4807 | |||
|
r7973 | if not (url.startswith('svn://') or url.startswith('svn+ssh://') or | ||
(os.path.exists(url) and | ||||
os.path.exists(os.path.join(url, '.svn'))) or | ||||
|
r9829 | issvnurl(ui, url)): | ||
|
r10939 | raise NoRepo(_("%s does not look like a Subversion repository") | ||
% url) | ||||
|
r13480 | if svn is None: | ||
|
r16925 | raise MissingTool(_('could not load Subversion python bindings')) | ||
|
r7447 | |||
try: | ||||
version = svn.core.SVN_VER_MAJOR, svn.core.SVN_VER_MINOR | ||||
if version < (1, 4): | ||||
raise MissingTool(_('Subversion python bindings %d.%d found, ' | ||||
'1.4 or later required') % version) | ||||
except AttributeError: | ||||
raise MissingTool(_('Subversion python bindings are too old, 1.4 ' | ||||
'or later required')) | ||||
|
r4766 | |||
|
r4813 | self.lastrevs = {} | ||
|
r4766 | latest = None | ||
|
r4765 | try: | ||
# Support file://path@rev syntax. Useful e.g. to convert | ||||
# deleted branches. | ||||
|
r4927 | at = url.rfind('@') | ||
if at >= 0: | ||||
|
r10282 | latest = int(url[at + 1:]) | ||
|
r4927 | url = url[:at] | ||
|
r7874 | except ValueError: | ||
|
r4766 | pass | ||
|
r5008 | self.url = geturl(url) | ||
|
r4765 | self.encoding = 'UTF-8' # Subversion is always nominal UTF-8 | ||
try: | ||||
|
r5008 | self.transport = transport.SvnRaTransport(url=self.url) | ||
|
r4765 | self.ra = self.transport.ra | ||
|
r4946 | self.ctx = self.transport.client | ||
|
r7074 | self.baseurl = svn.ra.get_repos_root(self.ra) | ||
|
r6538 | # Module is either empty or a repository path starting with | ||
# a slash and not ending with a slash. | ||||
|
r28883 | self.module = urlreq.unquote(self.url[len(self.baseurl):]) | ||
|
r6847 | self.prevmodule = None | ||
|
r5957 | self.rootmodule = self.module | ||
|
r4765 | self.commits = {} | ||
|
r5121 | self.paths = {} | ||
|
r8886 | self.uuid = svn.ra.get_uuid(self.ra) | ||
|
r28460 | except svn.core.SubversionException: | ||
|
r8206 | ui.traceback() | ||
|
r23583 | svnversion = '%d.%d.%d' % (svn.core.SVN_VER_MAJOR, | ||
svn.core.SVN_VER_MINOR, | ||||
svn.core.SVN_VER_MICRO) | ||||
raise NoRepo(_("%s does not look like a Subversion repository " | ||||
"to libsvn version %s") | ||||
% (self.url, svnversion)) | ||||
|
r4765 | |||
|
r25748 | if revs: | ||
if len(revs) > 1: | ||||
|
r26587 | raise error.Abort(_('subversion source does not support ' | ||
|
r25748 | 'specifying multiple revisions')) | ||
|
r5145 | try: | ||
|
r25748 | latest = int(revs[0]) | ||
|
r5145 | except ValueError: | ||
|
r26587 | raise error.Abort(_('svn: revision %s is not an integer') % | ||
|
r25748 | revs[0]) | ||
|
r5145 | |||
|
r34891 | trunkcfg = self.ui.config('convert', 'svn.trunk') | ||
if trunkcfg is None: | ||||
trunkcfg = 'trunk' | ||||
self.trunkname = trunkcfg.strip('/') | ||||
|
r34177 | self.startrev = self.ui.config('convert', 'svn.startrev') | ||
|
r6173 | try: | ||
self.startrev = int(self.startrev) | ||||
if self.startrev < 0: | ||||
self.startrev = 0 | ||||
except ValueError: | ||||
|
r26587 | raise error.Abort(_('svn: start revision %s is not an integer') | ||
|
r6173 | % self.startrev) | ||
|
r14152 | try: | ||
self.head = self.latest(self.module, latest) | ||||
except SvnPathNotFound: | ||||
self.head = None | ||||
|
r5957 | if not self.head: | ||
|
r26587 | raise error.Abort(_('no revision found in module %s') | ||
|
r8886 | % self.module) | ||
|
r5955 | self.last_changed = self.revnum(self.head) | ||
|
r6210 | |||
|
r22298 | self._changescache = (None, None) | ||
|
r4765 | |||
|
r5554 | if os.path.exists(os.path.join(url, '.svn/entries')): | ||
self.wc = url | ||||
else: | ||||
self.wc = None | ||||
self.convertfp = None | ||||
|
r5510 | def setrevmap(self, revmap): | ||
|
r4840 | lastrevs = {} | ||
|
r36313 | for revid in revmap: | ||
|
r13690 | uuid, module, revnum = revsplit(revid) | ||
|
r4840 | lastrevnum = lastrevs.setdefault(module, revnum) | ||
if revnum > lastrevnum: | ||||
lastrevs[module] = revnum | ||||
self.lastrevs = lastrevs | ||||
|
r4925 | def exists(self, path, optrev): | ||
try: | ||||
|
r15599 | svn.client.ls(self.url.rstrip('/') + '/' + quote(path), | ||
|
r4925 | optrev, False, self.ctx) | ||
|
r5461 | return True | ||
|
r28460 | except svn.core.SubversionException: | ||
|
r5461 | return False | ||
|
r4925 | |||
|
r4840 | def getheads(self): | ||
|
r5854 | |||
|
r6491 | def isdir(path, revnum): | ||
|
r6848 | kind = self._checkpath(path, revnum) | ||
|
r6491 | return kind == svn.core.svn_node_dir | ||
|
r5854 | def getcfgpath(name, rev): | ||
cfgpath = self.ui.config('convert', 'svn.' + name) | ||||
|
r6172 | if cfgpath is not None and cfgpath.strip() == '': | ||
return None | ||||
|
r5854 | path = (cfgpath or name).strip('/') | ||
if not self.exists(path, rev): | ||||
|
r13494 | if self.module.endswith(path) and name == 'trunk': | ||
# we are converting from inside this directory | ||||
return None | ||||
|
r5854 | if cfgpath: | ||
|
r26587 | raise error.Abort(_('expected %s to be at %r, but not found' | ||
) % (name, path)) | ||||
|
r5854 | return None | ||
self.ui.note(_('found %s at %r\n') % (name, path)) | ||||
return path | ||||
|
r5117 | rev = optrev(self.last_changed) | ||
|
r5854 | oldmodule = '' | ||
trunk = getcfgpath('trunk', rev) | ||||
|
r6400 | self.tags = getcfgpath('tags', rev) | ||
|
r5854 | branches = getcfgpath('branches', rev) | ||
# If the project has a trunk or branches, we will extract heads | ||||
# from them. We keep the project root otherwise. | ||||
if trunk: | ||||
oldmodule = self.module or '' | ||||
|
r4925 | self.module += '/' + trunk | ||
|
r5955 | self.head = self.latest(self.module, self.last_changed) | ||
|
r5957 | if not self.head: | ||
|
r26587 | raise error.Abort(_('no revision found in module %s') | ||
|
r8886 | % self.module) | ||
|
r5854 | |||
# First head in the list is the module's head | ||||
self.heads = [self.head] | ||||
|
r6400 | if self.tags is not None: | ||
self.tags = '%s/%s' % (oldmodule , (self.tags or 'tags')) | ||||
|
r5854 | |||
# Check if branches bring a few more heads to the list | ||||
if branches: | ||||
rpath = self.url.strip('/') | ||||
|
r15599 | branchnames = svn.client.ls(rpath + '/' + quote(branches), | ||
|
r7074 | rev, False, self.ctx) | ||
|
r18374 | for branch in sorted(branchnames): | ||
|
r5854 | module = '%s/%s/%s' % (oldmodule, branches, branch) | ||
|
r6491 | if not isdir(module, self.last_changed): | ||
continue | ||||
|
r5955 | brevid = self.latest(module, self.last_changed) | ||
|
r5957 | if not brevid: | ||
|
r8886 | self.ui.note(_('ignoring empty branch %s\n') % branch) | ||
|
r5957 | continue | ||
|
r6956 | self.ui.note(_('found branch %s at %d\n') % | ||
|
r5955 | (branch, self.revnum(brevid))) | ||
self.heads.append(brevid) | ||||
|
r5462 | |||
|
r6173 | if self.startrev and self.heads: | ||
if len(self.heads) > 1: | ||||
|
r26587 | raise error.Abort(_('svn: start revision is not supported ' | ||
|
r6173 | 'with more than one branch')) | ||
revnum = self.revnum(self.heads[0]) | ||||
if revnum < self.startrev: | ||||
|
r26587 | raise error.Abort( | ||
|
r10282 | _('svn: no revision found after start revision %d') | ||
|
r6173 | % self.startrev) | ||
|
r4840 | return self.heads | ||
|
r22300 | def _getchanges(self, rev, full): | ||
|
r5121 | (paths, parents) = self.paths[rev] | ||
|
r22300 | copies = {} | ||
|
r5956 | if parents: | ||
|
r11127 | files, self.removed, copies = self.expandpaths(rev, paths, parents) | ||
|
r22300 | if full or not parents: | ||
|
r5956 | # Perform a full checkout on roots | ||
|
r13690 | uuid, module, revnum = revsplit(rev) | ||
|
r15599 | entries = svn.client.ls(self.baseurl + quote(module), | ||
|
r7074 | optrev(revnum), True, self.ctx) | ||
|
r10282 | files = [n for n, e in entries.iteritems() | ||
|
r5956 | if e.kind == svn.core.svn_node_file] | ||
|
r11127 | self.removed = set() | ||
|
r5956 | |||
|
r5121 | files.sort() | ||
files = zip(files, [rev] * len(files)) | ||||
|
r22298 | return (files, copies) | ||
|
r5121 | |||
|
r22300 | def getchanges(self, rev, full): | ||
|
r22298 | # reuse cache from getchangedfiles | ||
|
r22300 | if self._changescache[0] == rev and not full: | ||
|
r22298 | (files, copies) = self._changescache[1] | ||
else: | ||||
|
r22300 | (files, copies) = self._getchanges(rev, full) | ||
|
r22298 | # caller caches the result, so free it here to release memory | ||
del self.paths[rev] | ||||
|
r24395 | return (files, copies, set()) | ||
|
r4840 | |||
|
r5382 | def getchangedfiles(self, rev, i): | ||
|
r22298 | # called from filemap - cache computed values for reuse in getchanges | ||
|
r22300 | (files, copies) = self._getchanges(rev, False) | ||
|
r22298 | self._changescache = (rev, (files, copies)) | ||
return [f[0] for f in files] | ||||
|
r5382 | |||
|
r4840 | def getcommit(self, rev): | ||
if rev not in self.commits: | ||||
|
r13690 | uuid, module, revnum = revsplit(rev) | ||
|
r4840 | self.module = module | ||
self.reparent(module) | ||||
|
r5875 | # We assume that: | ||
# - requests for revisions after "stop" come from the | ||||
# revision graph backward traversal. Cache all of them | ||||
# down to stop, they will be used eventually. | ||||
# - requests for revisions before "stop" come to get | ||||
# isolated branches parents. Just fetch what is needed. | ||||
|
r4840 | stop = self.lastrevs.get(module, 0) | ||
|
r5875 | if revnum < stop: | ||
stop = revnum + 1 | ||||
|
r5871 | self._fetch_revisions(revnum, stop) | ||
|
r15970 | if rev not in self.commits: | ||
|
r26587 | raise error.Abort(_('svn: revision %s not found') % revnum) | ||
|
r22201 | revcommit = self.commits[rev] | ||
|
r4840 | # caller caches the result, so free it here to release memory | ||
del self.commits[rev] | ||||
|
r22201 | return revcommit | ||
|
r4840 | |||
|
r20373 | def checkrevformat(self, revstr, mapname='splicemap'): | ||
|
r19122 | """ fails if revision format does not match the correct format""" | ||
if not re.match(r'svn:[0-9a-f]{8,8}-[0-9a-f]{4,4}-' | ||||
|
r30132 | r'[0-9a-f]{4,4}-[0-9a-f]{4,4}-[0-9a-f]' | ||
r'{12,12}(.*)\@[0-9]+$',revstr): | ||||
|
r26587 | raise error.Abort(_('%s entry %s is not a valid revision' | ||
|
r20373 | ' identifier') % (mapname, revstr)) | ||
|
r19122 | |||
|
r22414 | def numcommits(self): | ||
return int(self.head.rsplit('@', 1)[1]) - self.startrev | ||||
|
r4840 | def gettags(self): | ||
tags = {} | ||||
|
r6172 | if self.tags is None: | ||
return tags | ||||
|
r6210 | |||
|
r6399 | # svn tags are just a convention, project branches left in a | ||
# 'tags' directory. There is no other relationship than | ||||
# ancestry, which is expensive to discover and makes them hard | ||||
# to update incrementally. Worse, past revisions may be | ||||
# referenced by tags far away in the future, requiring a deep | ||||
# history traversal on every calculation. Current code | ||||
# performs a single backward traversal, tracking moves within | ||||
# the tags directory (tag renaming) and recording a new tag | ||||
# everytime a project is copied from outside the tags | ||||
# directory. It also lists deleted tags, this behaviour may | ||||
# change in the future. | ||||
pendings = [] | ||||
tagspath = self.tags | ||||
start = svn.ra.get_latest_revnum(self.ra) | ||||
|
r11195 | stream = self._getlog([self.tags], start, self.startrev) | ||
try: | ||||
for entry in stream: | ||||
origpaths, revnum, author, date, message = entry | ||||
|
r19468 | if not origpaths: | ||
origpaths = [] | ||||
|
r11195 | copies = [(e.copyfrom_path, e.copyfrom_rev, p) for p, e | ||
in origpaths.iteritems() if e.copyfrom_path] | ||||
# Apply moves/copies from more specific to general | ||||
copies.sort(reverse=True) | ||||
|
r6399 | |||
|
r11195 | srctagspath = tagspath | ||
if copies and copies[-1][2] == tagspath: | ||||
# Track tags directory moves | ||||
srctagspath = copies.pop()[0] | ||||
|
r6399 | |||
|
r11195 | for source, sourcerev, dest in copies: | ||
if not dest.startswith(tagspath + '/'): | ||||
continue | ||||
for tag in pendings: | ||||
if tag[0].startswith(dest): | ||||
tagpath = source + tag[0][len(dest):] | ||||
tag[:2] = [tagpath, sourcerev] | ||||
break | ||||
else: | ||||
pendings.append([source, sourcerev, dest]) | ||||
|
r8248 | |||
|
r11195 | # Filter out tags with children coming from different | ||
# parts of the repository like: | ||||
# /tags/tag.1 (from /trunk:10) | ||||
# /tags/tag.1/foo (from /branches/foo:12) | ||||
# Here/tags/tag.1 discarded as well as its children. | ||||
# It happens with tools like cvs2svn. Such tags cannot | ||||
# be represented in mercurial. | ||||
addeds = dict((p, e.copyfrom_path) for p, e | ||||
in origpaths.iteritems() | ||||
if e.action == 'A' and e.copyfrom_path) | ||||
badroots = set() | ||||
for destroot in addeds: | ||||
for source, sourcerev, dest in pendings: | ||||
if (not dest.startswith(destroot + '/') | ||||
or source.startswith(addeds[destroot] + '/')): | ||||
continue | ||||
badroots.add(destroot) | ||||
break | ||||
|
r8248 | |||
|
r11195 | for badroot in badroots: | ||
pendings = [p for p in pendings if p[2] != badroot | ||||
and not p[2].startswith(badroot + '/')] | ||||
|
r6399 | |||
|
r11195 | # Tell tag renamings from tag creations | ||
|
r15124 | renamings = [] | ||
|
r11195 | for source, sourcerev, dest in pendings: | ||
tagname = dest.split('/')[-1] | ||||
if source.startswith(srctagspath): | ||||
|
r15124 | renamings.append([source, sourcerev, tagname]) | ||
|
r11195 | continue | ||
if tagname in tags: | ||||
# Keep the latest tag value | ||||
continue | ||||
# From revision may be fake, get one with changes | ||||
try: | ||||
tagid = self.latest(source, sourcerev) | ||||
if tagid and tagname not in tags: | ||||
tags[tagname] = tagid | ||||
except SvnPathNotFound: | ||||
# It happens when we are following directories | ||||
# we assumed were copied with their parents | ||||
# but were really created in the tag | ||||
# directory. | ||||
pass | ||||
|
r15124 | pendings = renamings | ||
|
r11195 | tagspath = srctagspath | ||
finally: | ||||
stream.close() | ||||
|
r4946 | return tags | ||
|
r4840 | |||
|
r5554 | def converted(self, rev, destrev): | ||
if not self.wc: | ||||
return | ||||
if self.convertfp is None: | ||||
self.convertfp = open(os.path.join(self.wc, '.svn', 'hg-shamap'), | ||||
|
r36149 | 'ab') | ||
|
r36166 | self.convertfp.write(util.tonativeeol('%s %d\n' | ||
% (destrev, self.revnum(rev)))) | ||||
|
r5554 | self.convertfp.flush() | ||
|
r4810 | def revid(self, revnum, module=None): | ||
|
r8886 | return 'svn:%s%s@%s' % (self.uuid, module or self.module, revnum) | ||
|
r4774 | |||
def revnum(self, rev): | ||||
return int(rev.split('@')[-1]) | ||||
|
r4789 | |||
|
r16464 | def latest(self, path, stop=None): | ||
"""Find the latest revid affecting path, up to stop revision | ||||
number. If stop is None, default to repository latest | ||||
revision. It may return a revision in a different module, | ||||
since a branch may be moved without a change being | ||||
reported. Return None if computed module does not belong to | ||||
rootmodule subtree. | ||||
|
r5955 | """ | ||
|
r16466 | def findchanges(path, start, stop=None): | ||
stream = self._getlog([path], start, stop or 1) | ||||
|
r16465 | try: | ||
for entry in stream: | ||||
paths, revnum, author, date, message = entry | ||||
|
r16466 | if stop is None and paths: | ||
# We do not know the latest changed revision, | ||||
# keep the first one with changed paths. | ||||
break | ||||
|
r16465 | if revnum <= stop: | ||
break | ||||
for p in paths: | ||||
if (not path.startswith(p) or | ||||
not paths[p].copyfrom_path): | ||||
continue | ||||
newpath = paths[p].copyfrom_path + path[len(p):] | ||||
self.ui.debug("branch renamed from %s to %s at %d\n" % | ||||
(path, newpath, revnum)) | ||||
path = newpath | ||||
break | ||||
|
r16466 | if not paths: | ||
revnum = None | ||||
|
r16465 | return revnum, path | ||
finally: | ||||
stream.close() | ||||
|
r6281 | if not path.startswith(self.rootmodule): | ||
# Requests on foreign branches may be forbidden at server level | ||||
|
r9467 | self.ui.debug('ignoring foreign branch %r\n' % path) | ||
|
r6281 | return None | ||
|
r16464 | if stop is None: | ||
|
r4789 | stop = svn.ra.get_latest_revnum(self.ra) | ||
try: | ||||
|
r6847 | prevmodule = self.reparent('') | ||
|
r4789 | dirent = svn.ra.stat(self.ra, path.strip('/'), stop) | ||
|
r6847 | self.reparent(prevmodule) | ||
|
r28460 | except svn.core.SubversionException: | ||
|
r4789 | dirent = None | ||
if not dirent: | ||||
|
r10282 | raise SvnPathNotFound(_('%s not found up to revision %d') | ||
% (path, stop)) | ||||
|
r4789 | |||
|
r8660 | # stat() gives us the previous revision on this line of | ||
# development, but it might be in *another module*. Fetch the | ||||
# log and detect renames down to the latest revision. | ||||
|
r16465 | revnum, realpath = findchanges(path, stop, dirent.created_rev) | ||
|
r16466 | if revnum is None: | ||
# Tools like svnsync can create empty revision, when | ||||
# synchronizing only a subtree for instance. These empty | ||||
# revisions created_rev still have their original values | ||||
# despite all changes having disappeared and can be | ||||
# returned by ra.stat(), at least when stating the root | ||||
# module. In that case, do not trust created_rev and scan | ||||
# the whole history. | ||||
revnum, realpath = findchanges(path, stop) | ||||
if revnum is None: | ||||
self.ui.debug('ignoring empty branch %r\n' % realpath) | ||||
return None | ||||
|
r16465 | if not realpath.startswith(self.rootmodule): | ||
self.ui.debug('ignoring foreign branch %r\n' % realpath) | ||||
|
r5957 | return None | ||
|
r16465 | return self.revid(revnum, realpath) | ||
|
r4789 | |||
|
r4765 | def reparent(self, module): | ||
|
r6847 | """Reparent the svn transport and return the previous parent.""" | ||
if self.prevmodule == module: | ||||
return module | ||||
|
r15599 | svnurl = self.baseurl + quote(module) | ||
|
r6847 | prevmodule = self.prevmodule | ||
if prevmodule is None: | ||||
prevmodule = '' | ||||
|
r9467 | self.ui.debug("reparent to %s\n" % svnurl) | ||
|
r7074 | svn.ra.reparent(self.ra, svnurl) | ||
|
r6847 | self.prevmodule = module | ||
return prevmodule | ||||
|
r4765 | |||
|
r5120 | def expandpaths(self, rev, paths, parents): | ||
|
r11127 | changed, removed = set(), set() | ||
|
r5120 | copies = {} | ||
|
r13690 | new_module, revnum = revsplit(rev)[1:] | ||
|
r5872 | if new_module != self.module: | ||
self.module = new_module | ||||
self.reparent(self.module) | ||||
|
r5121 | |||
|
r38425 | progress = self.ui.makeprogress(_('scanning paths'), unit=_('paths'), | ||
total=len(paths)) | ||||
|
r11137 | for i, (path, ent) in enumerate(paths): | ||
|
r38425 | progress.update(i, item=path) | ||
|
r6539 | entrypath = self.getrelpath(path) | ||
|
r5120 | |||
|
r6848 | kind = self._checkpath(entrypath, revnum) | ||
|
r5120 | if kind == svn.core.svn_node_file: | ||
|
r11127 | changed.add(self.recode(entrypath)) | ||
|
r6546 | if not ent.copyfrom_path or not parents: | ||
|
r6544 | continue | ||
|
r8660 | # Copy sources not in parent revisions cannot be | ||
# represented, ignore their origin for now | ||||
|
r13690 | pmodule, prevnum = revsplit(parents[0])[1:] | ||
|
r6546 | if ent.copyfrom_rev < prevnum: | ||
continue | ||||
copyfrom_path = self.getrelpath(ent.copyfrom_path, pmodule) | ||||
|
r6544 | if not copyfrom_path: | ||
continue | ||||
|
r9467 | self.ui.debug("copied to %s from %s@%s\n" % | ||
|
r6544 | (entrypath, copyfrom_path, ent.copyfrom_rev)) | ||
|
r8885 | copies[self.recode(entrypath)] = self.recode(copyfrom_path) | ||
|
r5120 | elif kind == 0: # gone, but had better be a deleted *file* | ||
|
r9467 | self.ui.debug("gone from %s\n" % ent.copyfrom_rev) | ||
|
r13690 | pmodule, prevnum = revsplit(parents[0])[1:] | ||
|
r8884 | parentpath = pmodule + "/" + entrypath | ||
|
r11128 | fromkind = self._checkpath(entrypath, prevnum, pmodule) | ||
|
r6210 | |||
|
r8881 | if fromkind == svn.core.svn_node_file: | ||
|
r11127 | removed.add(self.recode(entrypath)) | ||
|
r5120 | elif fromkind == svn.core.svn_node_dir: | ||
|
r11123 | oroot = parentpath.strip('/') | ||
nroot = path.strip('/') | ||||
|
r11133 | children = self._iterfiles(oroot, prevnum) | ||
|
r11132 | for childpath in children: | ||
childpath = childpath.replace(oroot, nroot) | ||||
childpath = self.getrelpath("/" + childpath, pmodule) | ||||
|
r11125 | if childpath: | ||
|
r11127 | removed.add(self.recode(childpath)) | ||
|
r5120 | else: | ||
|
r41925 | self.ui.debug('unknown path in revision %d: %s\n' % | ||
|
r5120 | (revnum, path)) | ||
|
r12770 | elif kind == svn.core.svn_node_dir: | ||
|
r5870 | if ent.action == 'M': | ||
|
r11128 | # If the directory just had a prop change, | ||
# then we shouldn't need to look for its children. | ||||
|
r5870 | continue | ||
|
r13052 | if ent.action == 'R' and parents: | ||
|
r11128 | # If a directory is replacing a file, mark the previous | ||
# file as deleted | ||||
|
r13690 | pmodule, prevnum = revsplit(parents[0])[1:] | ||
|
r11128 | pkind = self._checkpath(entrypath, prevnum, pmodule) | ||
if pkind == svn.core.svn_node_file: | ||||
removed.add(self.recode(entrypath)) | ||||
|
r13052 | elif pkind == svn.core.svn_node_dir: | ||
# We do not know what files were kept or removed, | ||||
# mark them all as changed. | ||||
for childpath in self._iterfiles(pmodule, prevnum): | ||||
childpath = self.getrelpath("/" + childpath) | ||||
if childpath: | ||||
changed.add(self.recode(childpath)) | ||||
|
r5870 | |||
|
r11133 | for childpath in self._iterfiles(path, revnum): | ||
|
r11132 | childpath = self.getrelpath("/" + childpath) | ||
if childpath: | ||||
changed.add(self.recode(childpath)) | ||||
|
r5120 | |||
|
r8881 | # Handle directory copies | ||
|
r6543 | if not ent.copyfrom_path or not parents: | ||
|
r6542 | continue | ||
|
r8660 | # Copy sources not in parent revisions cannot be | ||
# represented, ignore their origin for now | ||||
|
r13690 | pmodule, prevnum = revsplit(parents[0])[1:] | ||
|
r6543 | if ent.copyfrom_rev < prevnum: | ||
continue | ||||
|
r8882 | copyfrompath = self.getrelpath(ent.copyfrom_path, pmodule) | ||
|
r6542 | if not copyfrompath: | ||
continue | ||||
|
r9467 | self.ui.debug("mark %s came from %s:%d\n" | ||
|
r6542 | % (path, copyfrompath, ent.copyfrom_rev)) | ||
|
r11133 | children = self._iterfiles(ent.copyfrom_path, ent.copyfrom_rev) | ||
|
r11132 | for childpath in children: | ||
childpath = self.getrelpath("/" + childpath, pmodule) | ||||
if not childpath: | ||||
|
r6542 | continue | ||
|
r11132 | copytopath = path + childpath[len(copyfrompath):] | ||
|
r6542 | copytopath = self.getrelpath(copytopath) | ||
|
r11132 | copies[self.recode(copytopath)] = self.recode(childpath) | ||
|
r5120 | |||
|
r38425 | progress.complete() | ||
|
r11127 | changed.update(removed) | ||
return (list(changed), removed, copies) | ||||
|
r5120 | |||
|
r5871 | def _fetch_revisions(self, from_revnum, to_revnum): | ||
if from_revnum < to_revnum: | ||||
from_revnum, to_revnum = to_revnum, from_revnum | ||||
|
r4940 | self.child_cset = None | ||
|
r6545 | |||
|
r4946 | def parselogentry(orig_paths, revnum, author, date, message): | ||
|
r6210 | """Return the parsed commit object or None, and True if | ||
|
r5872 | the revision is a branch root. | ||
""" | ||||
|
r9467 | self.ui.debug("parsing revision %d (%d changes)\n" % | ||
|
r4946 | (revnum, len(orig_paths))) | ||
|
r4940 | |||
|
r5957 | branched = False | ||
|
r4810 | rev = self.revid(revnum) | ||
|
r4837 | # branch log might return entries for a parent we already have | ||
|
r5871 | |||
|
r8117 | if rev in self.commits or revnum < to_revnum: | ||
|
r5957 | return None, branched | ||
|
r4837 | |||
|
r5120 | parents = [] | ||
|
r5958 | # check whether this revision is the start of a branch or part | ||
# of a branch renaming | ||||
|
r8209 | orig_paths = sorted(orig_paths.iteritems()) | ||
|
r10282 | root_paths = [(p, e) for p, e in orig_paths | ||
if self.module.startswith(p)] | ||||
|
r5958 | if root_paths: | ||
path, ent = root_paths[-1] | ||||
|
r5119 | if ent.copyfrom_path: | ||
|
r5957 | branched = True | ||
|
r5958 | newpath = ent.copyfrom_path + self.module[len(path):] | ||
|
r5119 | # ent.copyfrom_rev may not be the actual last revision | ||
|
r7476 | previd = self.latest(newpath, ent.copyfrom_rev) | ||
|
r5957 | if previd is not None: | ||
|
r13690 | prevmodule, prevnum = revsplit(previd)[1:] | ||
|
r6173 | if prevnum >= self.startrev: | ||
parents = [previd] | ||||
|
r10282 | self.ui.note( | ||
_('found parent of branch %s at %d: %s\n') % | ||||
(self.module, prevnum, prevmodule)) | ||||
|
r5119 | else: | ||
|
r9467 | self.ui.debug("no copyfrom path, don't know what to do.\n") | ||
|
r5119 | |||
|
r5120 | paths = [] | ||
# filter out unrelated paths | ||||
|
r4940 | for path, ent in orig_paths: | ||
|
r6540 | if self.getrelpath(path) is None: | ||
|
r4788 | continue | ||
|
r5120 | paths.append((path, ent)) | ||
|
r4765 | |||
|
r4788 | # Example SVN datetime. Includes microseconds. | ||
# ISO-8601 conformant | ||||
# '2007-01-04T17:35:00.902377Z' | ||||
|
r36625 | date = dateutil.parsedate(date[:19] + " UTC", ["%Y-%m-%dT%H:%M:%S"]) | ||
|
r17974 | if self.ui.configbool('convert', 'localtimezone'): | ||
date = makedatetimestamp(date[0]) | ||||
|
r4765 | |||
|
r24306 | if message: | ||
log = self.recode(message) | ||||
else: | ||||
log = '' | ||||
if author: | ||||
author = self.recode(author) | ||||
else: | ||||
author = '' | ||||
|
r5120 | try: | ||
branch = self.module.split("/")[-1] | ||||
|
r13529 | if branch == self.trunkname: | ||
branch = None | ||||
|
r5120 | except IndexError: | ||
branch = None | ||||
|
r4765 | |||
|
r4788 | cset = commit(author=author, | ||
|
r36625 | date=dateutil.datestr(date, '%Y-%m-%d %H:%M:%S %1%2'), | ||
|
r4957 | desc=log, | ||
|
r4795 | parents=parents, | ||
|
r4873 | branch=branch, | ||
|
r8886 | rev=rev) | ||
|
r4788 | |||
|
r4796 | self.commits[rev] = cset | ||
|
r5872 | # The parents list is *shared* among self.paths and the | ||
# commit object. Both will be updated below. | ||||
self.paths[rev] = (paths, cset.parents) | ||||
|
r4796 | if self.child_cset and not self.child_cset.parents: | ||
|
r5872 | self.child_cset.parents[:] = [rev] | ||
|
r4788 | self.child_cset = cset | ||
|
r5957 | return cset, branched | ||
|
r4796 | |||
|
r6956 | self.ui.note(_('fetching revision log for "%s" from %d to %d\n') % | ||
|
r4797 | (self.module, from_revnum, to_revnum)) | ||
|
r4765 | |||
try: | ||||
|
r5871 | firstcset = None | ||
|
r6173 | lastonbranch = False | ||
|
r6850 | stream = self._getlog([self.module], from_revnum, to_revnum) | ||
|
r5873 | try: | ||
for entry in stream: | ||||
paths, revnum, author, date, message = entry | ||||
|
r6173 | if revnum < self.startrev: | ||
lastonbranch = True | ||||
break | ||||
|
r8172 | if not paths: | ||
|
r9467 | self.ui.debug('revision %d has no entries\n' % revnum) | ||
|
r10618 | # If we ever leave the loop on an empty | ||
# revision, do not try to get a parent branch | ||||
lastonbranch = lastonbranch or revnum == 0 | ||||
|
r5873 | continue | ||
|
r6210 | cset, lastonbranch = parselogentry(paths, revnum, author, | ||
|
r6173 | date, message) | ||
|
r5873 | if cset: | ||
firstcset = cset | ||||
|
r6173 | if lastonbranch: | ||
|
r5873 | break | ||
finally: | ||||
stream.close() | ||||
|
r5871 | |||
|
r6173 | if not lastonbranch and firstcset and not firstcset.parents: | ||
|
r5871 | # The first revision of the sequence (the last fetched one) | ||
# has invalid parents if not a branch root. Find the parent | ||||
# revision now, if any. | ||||
try: | ||||
firstrevnum = self.revnum(firstcset.rev) | ||||
if firstrevnum > 1: | ||||
latest = self.latest(self.module, firstrevnum - 1) | ||||
|
r5957 | if latest: | ||
firstcset.parents.append(latest) | ||||
|
r7381 | except SvnPathNotFound: | ||
|
r5871 | pass | ||
|
r28460 | except svn.core.SubversionException as xxx_todo_changeme: | ||
|
r25660 | (inst, num) = xxx_todo_changeme.args | ||
|
r4765 | if num == svn.core.SVN_ERR_FS_NO_SUCH_REVISION: | ||
|
r26587 | raise error.Abort(_('svn: branch has no revision %s') | ||
|
r16683 | % to_revnum) | ||
|
r4765 | raise | ||
|
r11134 | def getfile(self, file, rev): | ||
|
r4765 | # TODO: ra.get_file transmits the whole file instead of diffs. | ||
|
r11127 | if file in self.removed: | ||
|
r22296 | return None, None | ||
|
r4765 | try: | ||
|
r13690 | new_module, revnum = revsplit(rev)[1:] | ||
|
r5872 | if self.module != new_module: | ||
self.module = new_module | ||||
|
r4765 | self.reparent(self.module) | ||
|
r28861 | io = stringio() | ||
|
r4765 | info = svn.ra.get_file(self.ra, file, revnum, io) | ||
|
r7446 | data = io.getvalue() | ||
|
r17424 | # ra.get_file() seems to keep a reference on the input buffer | ||
|
r17479 | # preventing collection. Release it explicitly. | ||
|
r7446 | io.close() | ||
|
r4765 | if isinstance(info, list): | ||
info = info[-1] | ||||
mode = ("svn:executable" in info) and 'x' or '' | ||||
mode = ("svn:special" in info) and 'l' or mode | ||||
|
r28460 | except svn.core.SubversionException as e: | ||
|
r4765 | notfound = (svn.core.SVN_ERR_FS_NOT_FOUND, | ||
svn.core.SVN_ERR_RA_DAV_PATH_NOT_FOUND) | ||||
if e.apr_err in notfound: # File not found | ||||
|
r22296 | return None, None | ||
|
r4765 | raise | ||
if mode == 'l': | ||||
link_prefix = "link " | ||||
if data.startswith(link_prefix): | ||||
data = data[len(link_prefix):] | ||||
return data, mode | ||||
|
r11133 | def _iterfiles(self, path, revnum): | ||
"""Enumerate all files in path at revnum, recursively.""" | ||||
|
r5114 | path = path.strip('/') | ||
|
r28460 | pool = svn.core.Pool() | ||
|
r15599 | rpath = '/'.join([self.baseurl, quote(path)]).strip('/') | ||
|
r11167 | entries = svn.client.ls(rpath, optrev(revnum), True, self.ctx, pool) | ||
|
r13651 | if path: | ||
path += '/' | ||||
return ((path + p) for p, e in entries.iteritems() | ||||
|
r11133 | if e.kind == svn.core.svn_node_file) | ||
|
r5513 | |||
|
r6539 | def getrelpath(self, path, module=None): | ||
if module is None: | ||||
module = self.module | ||||
# Given the repository url of this wc, say | ||||
# "http://server/plone/CMFPlone/branches/Plone-2_0-branch" | ||||
# extract the "entry" portion (a relative path) from what | ||||
|
r17424 | # svn log --xml says, i.e. | ||
|
r6539 | # "/CMFPlone/branches/Plone-2_0-branch/tests/PloneTestCase.py" | ||
# that is to say "tests/PloneTestCase.py" | ||||
if path.startswith(module): | ||||
relative = path.rstrip('/')[len(module):] | ||||
if relative.startswith('/'): | ||||
return relative[1:] | ||||
elif relative == '': | ||||
return relative | ||||
# The path is outside our tracked tree... | ||||
|
r9467 | self.ui.debug('%r is not under %r, ignoring\n' % (path, module)) | ||
|
r6539 | return None | ||
|
r11128 | def _checkpath(self, path, revnum, module=None): | ||
if module is not None: | ||||
prevmodule = self.reparent('') | ||||
path = module + '/' + path | ||||
try: | ||||
# ra.check_path does not like leading slashes very much, it leads | ||||
# to PROPFIND subversion errors | ||||
return svn.ra.check_path(self.ra, path.strip('/'), revnum) | ||||
finally: | ||||
if module is not None: | ||||
self.reparent(prevmodule) | ||||
|
r12770 | |||
|
r6850 | def _getlog(self, paths, start, end, limit=0, discover_changed_paths=True, | ||
strict_node_history=False): | ||||
# Normalize path names, svn >= 1.5 only wants paths relative to | ||||
# supplied URL | ||||
relpaths = [] | ||||
for p in paths: | ||||
if not p.startswith('/'): | ||||
p = self.module + '/' + p | ||||
relpaths.append(p.strip('/')) | ||||
|
r16683 | args = [self.baseurl, relpaths, start, end, limit, | ||
discover_changed_paths, strict_node_history] | ||||
|
r27314 | # developer config: convert.svn.debugsvnlog | ||
|
r34176 | if not self.ui.configbool('convert', 'svn.debugsvnlog'): | ||
|
r20420 | return directlogstream(*args) | ||
|
r6850 | arg = encodeargs(args) | ||
|
r37138 | hgexe = procutil.hgexecutable() | ||
cmd = '%s debugsvnlog' % procutil.shellquote(hgexe) | ||||
stdin, stdout = procutil.popen2(procutil.quotecommand(cmd)) | ||||
|
r6850 | stdin.write(arg) | ||
|
r10071 | try: | ||
stdin.close() | ||||
except IOError: | ||||
|
r26587 | raise error.Abort(_('Mercurial failed to run itself, check' | ||
|
r10071 | ' hg executable is in PATH')) | ||
|
r6850 | return logstream(stdout) | ||
|
r38079 | pre_revprop_change = b'''#!/bin/sh | ||
|
r5513 | |||
REPOS="$1" | ||||
REV="$2" | ||||
USER="$3" | ||||
PROPNAME="$4" | ||||
ACTION="$5" | ||||
if [ "$ACTION" = "M" -a "$PROPNAME" = "svn:log" ]; then exit 0; fi | ||||
if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-branch" ]; then exit 0; fi | ||||
if [ "$ACTION" = "A" -a "$PROPNAME" = "hg:convert-rev" ]; then exit 0; fi | ||||
echo "Changing prohibited revision property" >&2 | ||||
exit 1 | ||||
''' | ||||
class svn_sink(converter_sink, commandline): | ||||
|
r38079 | commit_re = re.compile(br'Committed revision (\d+).', re.M) | ||
uuid_re = re.compile(br'Repository UUID:\s*(\S+)', re.M) | ||||
|
r5513 | |||
def prerun(self): | ||||
if self.wc: | ||||
os.chdir(self.wc) | ||||
def postrun(self): | ||||
if self.wc: | ||||
os.chdir(self.cwd) | ||||
def join(self, name): | ||||
return os.path.join(self.wc, '.svn', name) | ||||
|
r5760 | |||
|
r5513 | def revmapfile(self): | ||
return self.join('hg-shamap') | ||||
def authorfile(self): | ||||
return self.join('hg-authormap') | ||||
|
r35168 | def __init__(self, ui, repotype, path): | ||
|
r13480 | |||
|
r35168 | converter_sink.__init__(self, ui, repotype, path) | ||
|
r5513 | commandline.__init__(self, ui, 'svn') | ||
self.delete = [] | ||||
|
r5698 | self.setexec = [] | ||
self.delexec = [] | ||||
self.copies = [] | ||||
|
r5513 | self.wc = None | ||
|
r39843 | self.cwd = encoding.getcwd() | ||
|
r5513 | |||
created = False | ||||
if os.path.isfile(os.path.join(path, '.svn', 'entries')): | ||||
|
r17247 | self.wc = os.path.realpath(path) | ||
|
r5513 | self.run0('update') | ||
else: | ||||
|
r36473 | if not re.search(br'^(file|http|https|svn|svn\+ssh)\://', path): | ||
|
r17247 | path = os.path.realpath(path) | ||
if os.path.isdir(os.path.dirname(path)): | ||||
if not os.path.exists(os.path.join(path, 'db', 'fs-type')): | ||||
|
r40251 | ui.status(_("initializing svn repository '%s'\n") % | ||
|
r17247 | os.path.basename(path)) | ||
commandline(ui, 'svnadmin').run0('create', path) | ||||
created = path | ||||
path = util.normpath(path) | ||||
if not path.startswith('/'): | ||||
path = '/' + path | ||||
path = 'file://' + path | ||||
|
r5535 | |||
|
r39843 | wcpath = os.path.join(encoding.getcwd(), os.path.basename(path) + | ||
|
r30519 | '-wc') | ||
|
r40251 | ui.status(_("initializing svn working copy '%s'\n") | ||
|
r10940 | % os.path.basename(wcpath)) | ||
|
r5513 | self.run0('checkout', path, wcpath) | ||
self.wc = wcpath | ||||
|
r31246 | self.opener = vfsmod.vfs(self.wc) | ||
self.wopener = vfsmod.vfs(self.wc) | ||||
|
r5513 | self.childmap = mapfile(ui, self.join('hg-childmap')) | ||
|
r24306 | if util.checkexec(self.wc): | ||
self.is_exec = util.isexec | ||||
else: | ||||
self.is_exec = None | ||||
|
r5513 | |||
if created: | ||||
hook = os.path.join(created, 'hooks', 'pre-revprop-change') | ||||
|
r36149 | fp = open(hook, 'wb') | ||
|
r5513 | fp.write(pre_revprop_change) | ||
fp.close() | ||||
|
r14232 | util.setflags(hook, False, True) | ||
|
r5513 | |||
|
r13530 | output = self.run0('info') | ||
self.uuid = self.uuid_re.search(output).group(1).strip() | ||||
|
r5554 | |||
|
r5513 | def wjoin(self, *names): | ||
return os.path.join(self.wc, *names) | ||||
|
r16511 | @propertycache | ||
def manifest(self): | ||||
# As of svn 1.7, the "add" command fails when receiving | ||||
# already tracked entries, so we have to track and filter them | ||||
# ourselves. | ||||
m = set() | ||||
output = self.run0('ls', recursive=True, xml=True) | ||||
doc = xml.dom.minidom.parseString(output) | ||||
|
r41358 | for e in doc.getElementsByTagName(r'entry'): | ||
|
r16511 | for n in e.childNodes: | ||
|
r41358 | if n.nodeType != n.ELEMENT_NODE or n.tagName != r'name': | ||
|
r16511 | continue | ||
|
r41358 | name = r''.join(c.data for c in n.childNodes | ||
if c.nodeType == c.TEXT_NODE) | ||||
|
r16511 | # Entries are compared with names coming from | ||
# mercurial, so bytes with undefined encoding. Our | ||||
# best bet is to assume they are in local | ||||
# encoding. They will be passed to command line calls | ||||
# later anyway, so they better be. | ||||
|
r31447 | m.add(encoding.unitolocal(name)) | ||
|
r16511 | break | ||
return m | ||||
|
r5513 | def putfile(self, filename, flags, data): | ||
if 'l' in flags: | ||||
self.wopener.symlink(data, filename) | ||||
else: | ||||
try: | ||||
if os.path.islink(self.wjoin(filename)): | ||||
os.unlink(filename) | ||||
except OSError: | ||||
pass | ||||
|
r41766 | |||
if self.is_exec: | ||||
# We need to check executability of the file before the change, | ||||
# because `vfs.write` is able to reset exec bit. | ||||
wasexec = False | ||||
if os.path.exists(self.wjoin(filename)): | ||||
wasexec = self.is_exec(self.wjoin(filename)) | ||||
|
r14168 | self.wopener.write(filename, data) | ||
|
r5536 | |||
if self.is_exec: | ||||
|
r41766 | if wasexec: | ||
|
r17031 | if 'x' not in flags: | ||
self.delexec.append(filename) | ||||
else: | ||||
if 'x' in flags: | ||||
self.setexec.append(filename) | ||||
util.setflags(self.wjoin(filename), False, 'x' in flags) | ||||
|
r5698 | |||
def _copyfile(self, source, dest): | ||||
|
r5513 | # SVN's copy command pukes if the destination file exists, but | ||
# our copyfile method expects to record a copy that has | ||||
# already occurred. Cross the semantic gap. | ||||
wdest = self.wjoin(dest) | ||||
|
r12343 | exists = os.path.lexists(wdest) | ||
|
r5513 | if exists: | ||
|
r38182 | fd, tempname = pycompat.mkstemp( | ||
|
r5513 | prefix='hg-copy-', dir=os.path.dirname(wdest)) | ||
os.close(fd) | ||||
os.unlink(tempname) | ||||
os.rename(wdest, tempname) | ||||
try: | ||||
self.run0('copy', source, dest) | ||||
finally: | ||||
|
r16511 | self.manifest.add(dest) | ||
|
r5513 | if exists: | ||
try: | ||||
os.unlink(wdest) | ||||
except OSError: | ||||
pass | ||||
os.rename(tempname, wdest) | ||||
def dirs_of(self, files): | ||||
|
r8150 | dirs = set() | ||
|
r5513 | for f in files: | ||
if os.path.isdir(self.wjoin(f)): | ||||
dirs.add(f) | ||||
|
r30605 | i = len(f) | ||
for i in iter(lambda: f.rfind('/', 0, i), -1): | ||||
|
r5513 | dirs.add(f[:i]) | ||
return dirs | ||||
|
r5698 | def add_dirs(self, files): | ||
|
r8209 | add_dirs = [d for d in sorted(self.dirs_of(files)) | ||
|
r16511 | if d not in self.manifest] | ||
|
r5513 | if add_dirs: | ||
|
r16511 | self.manifest.update(add_dirs) | ||
|
r5832 | self.xargs(add_dirs, 'add', non_recursive=True, quiet=True) | ||
|
r5698 | return add_dirs | ||
def add_files(self, files): | ||||
|
r16511 | files = [f for f in files if f not in self.manifest] | ||
|
r5513 | if files: | ||
|
r16511 | self.manifest.update(files) | ||
|
r5832 | self.xargs(files, 'add', quiet=True) | ||
|
r5698 | return files | ||
|
r5760 | |||
|
r5513 | def addchild(self, parent, child): | ||
self.childmap[parent] = child | ||||
|
r5554 | def revid(self, rev): | ||
|
r39457 | return "svn:%s@%s" % (self.uuid, rev) | ||
|
r5698 | |||
|
r24395 | def putcommit(self, files, copies, parents, commit, source, revmap, full, | ||
cleanp2): | ||||
|
r15605 | for parent in parents: | ||
try: | ||||
return self.revid(self.childmap[parent]) | ||||
except KeyError: | ||||
pass | ||||
|
r6716 | # Apply changes to working copy | ||
for f, v in files: | ||||
|
r22296 | data, mode = source.getfile(f, v) | ||
if data is None: | ||||
|
r6716 | self.delete.append(f) | ||
else: | ||||
|
r11134 | self.putfile(f, mode, data) | ||
|
r6716 | if f in copies: | ||
self.copies.append([copies[f], f]) | ||||
|
r22300 | if full: | ||
self.delete.extend(sorted(self.manifest.difference(files))) | ||||
|
r6716 | files = [f[0] for f in files] | ||
|
r8150 | entries = set(self.delete) | ||
files = frozenset(files) | ||||
|
r5698 | entries.update(self.add_dirs(files.difference(entries))) | ||
if self.copies: | ||||
for s, d in self.copies: | ||||
self._copyfile(s, d) | ||||
self.copies = [] | ||||
|
r5513 | if self.delete: | ||
|
r5832 | self.xargs(self.delete, 'delete') | ||
|
r16511 | for f in self.delete: | ||
self.manifest.remove(f) | ||||
|
r5513 | self.delete = [] | ||
entries.update(self.add_files(files.difference(entries))) | ||||
|
r5698 | if self.delexec: | ||
|
r5832 | self.xargs(self.delexec, 'propdel', 'svn:executable') | ||
|
r5698 | self.delexec = [] | ||
if self.setexec: | ||||
|
r5832 | self.xargs(self.setexec, 'propset', 'svn:executable', '*') | ||
|
r5698 | self.setexec = [] | ||
|
r38182 | fd, messagefile = pycompat.mkstemp(prefix='hg-convert-') | ||
|
r36853 | fp = os.fdopen(fd, r'wb') | ||
|
r36166 | fp.write(util.tonativeeol(commit.desc)) | ||
|
r5513 | fp.close() | ||
try: | ||||
output = self.run0('commit', | ||||
|
r37102 | username=stringutil.shortuser(commit.author), | ||
|
r5513 | file=messagefile, | ||
|
r5790 | encoding='utf-8') | ||
|
r5513 | try: | ||
rev = self.commit_re.search(output).group(1) | ||||
except AttributeError: | ||||
|
r41765 | if not files: | ||
|
r42592 | return parents[0] if parents else 'None' | ||
|
r5513 | self.ui.warn(_('unexpected svn output:\n')) | ||
self.ui.warn(output) | ||||
|
r26587 | raise error.Abort(_('unable to cope with svn output')) | ||
|
r5513 | if commit.rev: | ||
self.run('propset', 'hg:convert-rev', commit.rev, | ||||
revprop=True, revision=rev) | ||||
if commit.branch and commit.branch != 'default': | ||||
self.run('propset', 'hg:convert-branch', commit.branch, | ||||
revprop=True, revision=rev) | ||||
for parent in parents: | ||||
self.addchild(parent, rev) | ||||
|
r5554 | return self.revid(rev) | ||
|
r5513 | finally: | ||
os.unlink(messagefile) | ||||
def puttags(self, tags): | ||||
|
r11779 | self.ui.warn(_('writing Subversion tags is not yet implemented\n')) | ||
|
r11778 | return None, None | ||
|
r16106 | |||
|
r21635 | def hascommitfrommap(self, rev): | ||
# We trust that revisions referenced in a map still is present | ||||
# TODO: implement something better if necessary and feasible | ||||
return True | ||||
|
r21634 | def hascommitforsplicemap(self, rev): | ||
|
r16106 | # This is not correct as one can convert to an existing subversion | ||
# repository and childmap would not list all revisions. Too bad. | ||||
if rev in self.childmap: | ||||
return True | ||||
|
r26587 | raise error.Abort(_('splice map revision %s not found in subversion ' | ||
|
r16162 | 'child map (revision lookups are not implemented)') | ||
|
r16106 | % rev) | ||