cvs.py
332 lines
| 11.1 KiB
| text/x-python
|
PythonLexer
Martin Geisler
|
r8250 | # cvs.py: CVS conversion code inspired by hg-cvs-import and git-cvsimport | ||
# | ||||
Raphaël Gomès
|
r47575 | # Copyright 2005-2009 Olivia Mackall <olivia@selenic.com> and others | ||
Martin Geisler
|
r8250 | # | ||
# This software may be used and distributed according to the terms of the | ||||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Brendan Cully
|
r4536 | |||
timeless
|
r28413 | import errno | ||
import os | ||||
import re | ||||
import socket | ||||
Yuya Nishihara
|
r29205 | from mercurial.i18n import _ | ||
Gregory Szorc
|
r43359 | from mercurial.pycompat import ( | ||
getattr, | ||||
open, | ||||
) | ||||
timeless
|
r28413 | from mercurial import ( | ||
encoding, | ||||
error, | ||||
util, | ||||
) | ||||
Yuya Nishihara
|
r37138 | from mercurial.utils import ( | ||
dateutil, | ||||
procutil, | ||||
) | ||||
Brendan Cully
|
r4536 | |||
timeless
|
r28413 | from . import ( | ||
common, | ||||
cvsps, | ||||
) | ||||
timeless
|
r28861 | stringio = util.stringio | ||
timeless
|
r28413 | checktool = common.checktool | ||
commit = common.commit | ||||
converter_source = common.converter_source | ||||
makedatetimestamp = common.makedatetimestamp | ||||
NoRepo = common.NoRepo | ||||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43346 | |||
Brendan Cully
|
r4536 | class convert_cvs(converter_source): | ||
Matt Harbison
|
r35168 | def __init__(self, ui, repotype, path, revs=None): | ||
super(convert_cvs, self).__init__(ui, repotype, path, revs=revs) | ||||
Brendan Cully
|
r4807 | |||
Augie Fackler
|
r43347 | cvs = os.path.join(path, b"CVS") | ||
Brendan Cully
|
r4536 | if not os.path.exists(cvs): | ||
Augie Fackler
|
r43347 | raise NoRepo(_(b"%s does not look like a CVS checkout") % path) | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | checktool(b'cvs') | ||
Patrick Mezard
|
r5497 | |||
Patrick Mezard
|
r8048 | self.changeset = None | ||
Brendan Cully
|
r4536 | self.files = {} | ||
self.tags = {} | ||||
self.lastbranch = {} | ||||
self.socket = None | ||||
Augie Fackler
|
r43347 | self.cvsroot = open(os.path.join(cvs, b"Root"), b'rb').read()[:-1] | ||
self.cvsrepo = open(os.path.join(cvs, b"Repository"), b'rb').read()[:-1] | ||||
Brodie Rao
|
r11987 | self.encoding = encoding.encoding | ||
Frank Kingswood
|
r6690 | |||
Brendan Cully
|
r4536 | self._connect() | ||
Patrick Mezard
|
r8048 | def _parse(self): | ||
if self.changeset is not None: | ||||
Brendan Cully
|
r4536 | return | ||
Patrick Mezard
|
r8048 | self.changeset = {} | ||
Brendan Cully
|
r4536 | |||
Brendan Cully
|
r4760 | maxrev = 0 | ||
Durham Goode
|
r25748 | if self.revs: | ||
if len(self.revs) > 1: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _( | ||
b'cvs source does not support specifying ' | ||||
b'multiple revs' | ||||
) | ||||
Augie Fackler
|
r43346 | ) | ||
Brendan Cully
|
r4760 | # TODO: handle tags | ||
try: | ||||
# patchset number? | ||||
Durham Goode
|
r25748 | maxrev = int(self.revs[0]) | ||
Brendan Cully
|
r4760 | except ValueError: | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b'revision %s is not a patchset number') % self.revs[0] | ||
Augie Fackler
|
r43346 | ) | ||
Brendan Cully
|
r4760 | |||
Matt Harbison
|
r39843 | d = encoding.getcwd() | ||
Brendan Cully
|
r4536 | try: | ||
os.chdir(self.path) | ||||
Frank Kingswood
|
r6690 | |||
Augie Fackler
|
r43347 | cache = b'update' | ||
if not self.ui.configbool(b'convert', b'cvsps.cache'): | ||||
Patrick Mezard
|
r9543 | cache = None | ||
db = cvsps.createlog(self.ui, cache=cache) | ||||
Augie Fackler
|
r43346 | db = cvsps.createchangeset( | ||
self.ui, | ||||
db, | ||||
Augie Fackler
|
r43347 | fuzz=int(self.ui.config(b'convert', b'cvsps.fuzz')), | ||
mergeto=self.ui.config(b'convert', b'cvsps.mergeto'), | ||||
mergefrom=self.ui.config(b'convert', b'cvsps.mergefrom'), | ||||
Augie Fackler
|
r43346 | ) | ||
Thomas Arendsen Hein
|
r5920 | |||
Patrick Mezard
|
r9543 | for cs in db: | ||
Matt Mackall
|
r10282 | if maxrev and cs.id > maxrev: | ||
Patrick Mezard
|
r9543 | break | ||
Augie Fackler
|
r43346 | id = b"%d" % cs.id | ||
Patrick Mezard
|
r9543 | cs.author = self.recode(cs.author) | ||
self.lastbranch[cs.branch] = id | ||||
cs.comment = self.recode(cs.comment) | ||||
Augie Fackler
|
r43347 | if self.ui.configbool(b'convert', b'localtimezone'): | ||
Julian Cowley
|
r17974 | cs.date = makedatetimestamp(cs.date[0]) | ||
Augie Fackler
|
r43347 | date = dateutil.datestr(cs.date, b'%Y-%m-%d %H:%M:%S %1%2') | ||
Patrick Mezard
|
r9543 | self.tags.update(dict.fromkeys(cs.tags, id)) | ||
Thomas Arendsen Hein
|
r5920 | |||
Patrick Mezard
|
r9543 | files = {} | ||
for f in cs.entries: | ||||
Augie Fackler
|
r43347 | files[f.file] = b"%s%s" % ( | ||
b'.'.join([(b"%d" % x) for x in f.revision]), | ||||
[b'', b'(DEAD)'][f.dead], | ||||
Augie Fackler
|
r43346 | ) | ||
Frank Kingswood
|
r6690 | |||
Patrick Mezard
|
r9543 | # add current commit to set | ||
Augie Fackler
|
r43346 | c = commit( | ||
author=cs.author, | ||||
date=date, | ||||
parents=[(b"%d" % p.id) for p in cs.parents], | ||||
desc=cs.comment, | ||||
Augie Fackler
|
r43347 | branch=cs.branch or b'', | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r9543 | self.changeset[id] = c | ||
self.files[id] = files | ||||
Brendan Cully
|
r4536 | |||
self.heads = self.lastbranch.values() | ||||
finally: | ||||
os.chdir(d) | ||||
def _connect(self): | ||||
root = self.cvsroot | ||||
conntype = None | ||||
user, host = None, None | ||||
Augie Fackler
|
r43347 | cmd = [b'cvs', b'server'] | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | self.ui.status(_(b"connecting to %s\n") % root) | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | if root.startswith(b":pserver:"): | ||
Brendan Cully
|
r4536 | root = root[9:] | ||
r50797 | m = re.match( | |||
br'(?:(.*?)(?::(.*?))?@)?([^:/]*)(?::(\d*))?(.*)', root | ||||
) | ||||
Brendan Cully
|
r4536 | if m: | ||
Augie Fackler
|
r43347 | conntype = b"pserver" | ||
Brendan Cully
|
r4536 | user, passw, serv, port, root = m.groups() | ||
if not user: | ||||
Augie Fackler
|
r43347 | user = b"anonymous" | ||
Thomas Arendsen Hein
|
r5082 | if not port: | ||
port = 2401 | ||||
Brendan Cully
|
r4536 | else: | ||
Thomas Arendsen Hein
|
r5082 | port = int(port) | ||
Augie Fackler
|
r43347 | format0 = b":pserver:%s@%s:%s" % (user, serv, root) | ||
format1 = b":pserver:%s@%s:%d%s" % (user, serv, port, root) | ||||
Brendan Cully
|
r4536 | |||
if not passw: | ||||
Augie Fackler
|
r43347 | passw = b"A" | ||
cvspass = os.path.expanduser(b"~/.cvspass") | ||||
Thomas Arendsen Hein
|
r7444 | try: | ||
Augie Fackler
|
r43347 | pf = open(cvspass, b'rb') | ||
Edouard Gomez
|
r7442 | for line in pf.read().splitlines(): | ||
Augie Fackler
|
r43347 | part1, part2 = line.split(b' ', 1) | ||
Brodie Rao
|
r16683 | # /1 :pserver:user@example.com:2401/cvsroot/foo | ||
# Ah<Z | ||||
Augie Fackler
|
r43347 | if part1 == b'/1': | ||
part1, part2 = part2.split(b' ', 1) | ||||
Edouard Gomez
|
r7442 | format = format1 | ||
Brodie Rao
|
r16683 | # :pserver:user@example.com:/cvsroot/foo Ah<Z | ||
Edouard Gomez
|
r7442 | else: | ||
format = format0 | ||||
if part1 == format: | ||||
passw = part2 | ||||
break | ||||
pf.close() | ||||
Gregory Szorc
|
r25660 | except IOError as inst: | ||
Thomas Arendsen Hein
|
r7444 | if inst.errno != errno.ENOENT: | ||
if not getattr(inst, 'filename', None): | ||||
inst.filename = cvspass | ||||
raise | ||||
Brendan Cully
|
r4536 | |||
sck = socket.socket() | ||||
sck.connect((serv, port)) | ||||
Augie Fackler
|
r43346 | sck.send( | ||
Augie Fackler
|
r43347 | b"\n".join( | ||
Augie Fackler
|
r43346 | [ | ||
Augie Fackler
|
r43347 | b"BEGIN AUTH REQUEST", | ||
Augie Fackler
|
r43346 | root, | ||
user, | ||||
passw, | ||||
Augie Fackler
|
r43347 | b"END AUTH REQUEST", | ||
b"", | ||||
Augie Fackler
|
r43346 | ] | ||
) | ||||
) | ||||
Augie Fackler
|
r43347 | if sck.recv(128) != b"I LOVE YOU\n": | ||
raise error.Abort(_(b"CVS pserver authentication failed")) | ||||
Brendan Cully
|
r4536 | |||
r50798 | self.writep = self.readp = sck.makefile('rwb') | |||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | if not conntype and root.startswith(b":local:"): | ||
conntype = b"local" | ||||
Brendan Cully
|
r4536 | root = root[7:] | ||
if not conntype: | ||||
# :ext:user@host/home/user/path/to/cvsroot | ||||
Augie Fackler
|
r43347 | if root.startswith(b":ext:"): | ||
Brendan Cully
|
r4536 | root = root[5:] | ||
Pulkit Goyal
|
r36411 | m = re.match(br'(?:([^@:/]+)@)?([^:/]+):?(.*)', root) | ||
Patrick Mezard
|
r5304 | # Do not take Windows path "c:\foo\bar" for a connection strings | ||
if os.path.isdir(root) or not m: | ||||
Augie Fackler
|
r43347 | conntype = b"local" | ||
Brendan Cully
|
r4536 | else: | ||
Augie Fackler
|
r43347 | conntype = b"rsh" | ||
Brendan Cully
|
r4536 | user, host, root = m.group(1), m.group(2), m.group(3) | ||
Augie Fackler
|
r43347 | if conntype != b"pserver": | ||
if conntype == b"rsh": | ||||
rsh = encoding.environ.get(b"CVS_RSH") or b"ssh" | ||||
Brendan Cully
|
r4536 | if user: | ||
Augie Fackler
|
r43347 | cmd = [rsh, b'-l', user, host] + cmd | ||
Brendan Cully
|
r4536 | else: | ||
cmd = [rsh, host] + cmd | ||||
Patrick Mezard
|
r5303 | # popen2 does not support argument lists under Windows | ||
Manuel Jacob
|
r45403 | cmd = b' '.join(procutil.shellquote(arg) for arg in cmd) | ||
Yuya Nishihara
|
r37138 | self.writep, self.readp = procutil.popen2(cmd) | ||
Brendan Cully
|
r4536 | |||
self.realroot = root | ||||
Augie Fackler
|
r43347 | self.writep.write(b"Root %s\n" % root) | ||
Augie Fackler
|
r43346 | self.writep.write( | ||
Augie Fackler
|
r43347 | b"Valid-responses ok error Valid-requests Mode" | ||
b" M Mbinary E Checked-in Created Updated" | ||||
b" Merged Removed\n" | ||||
Augie Fackler
|
r43346 | ) | ||
Augie Fackler
|
r43347 | self.writep.write(b"valid-requests\n") | ||
Brendan Cully
|
r4536 | self.writep.flush() | ||
r = self.readp.readline() | ||||
Augie Fackler
|
r43347 | if not r.startswith(b"Valid-requests"): | ||
Augie Fackler
|
r43346 | raise error.Abort( | ||
_( | ||||
Augie Fackler
|
r43347 | b'unexpected response from CVS server ' | ||
b'(expected "Valid-requests", but got %r)' | ||||
Augie Fackler
|
r43346 | ) | ||
% r | ||||
) | ||||
Augie Fackler
|
r43347 | if b"UseUnchanged" in r: | ||
self.writep.write(b"UseUnchanged\n") | ||||
Brendan Cully
|
r4536 | self.writep.flush() | ||
Martin von Zweigbergk
|
r41401 | self.readp.readline() | ||
Brendan Cully
|
r4536 | |||
def getheads(self): | ||||
Patrick Mezard
|
r8048 | self._parse() | ||
Brendan Cully
|
r4536 | return self.heads | ||
Patrick Mezard
|
r11134 | def getfile(self, name, rev): | ||
Patrick Mezard
|
r5539 | def chunkedread(fp, count): | ||
Mads Kiilerich
|
r17424 | # file-objects returned by socket.makefile() do not handle | ||
Patrick Mezard
|
r5539 | # large read() requests very well. | ||
chunksize = 65536 | ||||
timeless
|
r28861 | output = stringio() | ||
Patrick Mezard
|
r5539 | while count > 0: | ||
data = fp.read(min(count, chunksize)) | ||||
if not data: | ||||
Augie Fackler
|
r43346 | raise error.Abort( | ||
Augie Fackler
|
r43347 | _(b"%d bytes missing from remote file") % count | ||
Augie Fackler
|
r43346 | ) | ||
Patrick Mezard
|
r5539 | count -= len(data) | ||
output.write(data) | ||||
return output.getvalue() | ||||
Patrick Mezard
|
r11134 | self._parse() | ||
Augie Fackler
|
r43347 | if rev.endswith(b"(DEAD)"): | ||
Mads Kiilerich
|
r22296 | return None, None | ||
Brendan Cully
|
r4536 | |||
Augie Fackler
|
r43347 | args = (b"-N -P -kk -r %s --" % rev).split() | ||
args.append(self.cvsrepo + b'/' + name) | ||||
Brendan Cully
|
r4536 | for x in args: | ||
Augie Fackler
|
r43347 | self.writep.write(b"Argument %s\n" % x) | ||
self.writep.write(b"Directory .\n%s\nco\n" % self.realroot) | ||||
Brendan Cully
|
r4536 | self.writep.flush() | ||
Augie Fackler
|
r43347 | data = b"" | ||
Mads Kiilerich
|
r10800 | mode = None | ||
Martin Geisler
|
r14494 | while True: | ||
Brendan Cully
|
r4536 | line = self.readp.readline() | ||
Augie Fackler
|
r43347 | if line.startswith(b"Created ") or line.startswith(b"Updated "): | ||
Augie Fackler
|
r43346 | self.readp.readline() # path | ||
self.readp.readline() # entries | ||||
Brendan Cully
|
r4536 | mode = self.readp.readline()[:-1] | ||
count = int(self.readp.readline()[:-1]) | ||||
Patrick Mezard
|
r5539 | data = chunkedread(self.readp, count) | ||
Augie Fackler
|
r43347 | elif line.startswith(b" "): | ||
Brendan Cully
|
r4536 | data += line[1:] | ||
Augie Fackler
|
r43347 | elif line.startswith(b"M "): | ||
Brendan Cully
|
r4536 | pass | ||
Augie Fackler
|
r43347 | elif line.startswith(b"Mbinary "): | ||
Brendan Cully
|
r4536 | count = int(self.readp.readline()[:-1]) | ||
Patrick Mezard
|
r5539 | data = chunkedread(self.readp, count) | ||
Brendan Cully
|
r4536 | else: | ||
Augie Fackler
|
r43347 | if line == b"ok\n": | ||
Mads Kiilerich
|
r10800 | if mode is None: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b'malformed response from CVS')) | ||
return (data, b"x" in mode and b"x" or b"") | ||||
elif line.startswith(b"E "): | ||||
self.ui.warn(_(b"cvs server: %s\n") % line[2:]) | ||||
elif line.startswith(b"Remove"): | ||||
Peter Arrenbrecht
|
r7874 | self.readp.readline() | ||
Brendan Cully
|
r4536 | else: | ||
Augie Fackler
|
r43347 | raise error.Abort(_(b"unknown CVS response: %s") % line) | ||
Brendan Cully
|
r4536 | |||
Mads Kiilerich
|
r22300 | def getchanges(self, rev, full): | ||
if full: | ||||
Augie Fackler
|
r43347 | raise error.Abort(_(b"convert from cvs does not support --full")) | ||
Patrick Mezard
|
r8048 | self._parse() | ||
Gregory Szorc
|
r49769 | return sorted(self.files[rev].items()), {}, set() | ||
Brendan Cully
|
r4536 | |||
def getcommit(self, rev): | ||||
Patrick Mezard
|
r8048 | self._parse() | ||
Brendan Cully
|
r4536 | return self.changeset[rev] | ||
def gettags(self): | ||||
Patrick Mezard
|
r8048 | self._parse() | ||
Brendan Cully
|
r4536 | return self.tags | ||
Alexis S. L. Carvalho
|
r5381 | |||
def getchangedfiles(self, rev, i): | ||||
Patrick Mezard
|
r8048 | self._parse() | ||
Matt Mackall
|
r8209 | return sorted(self.files[rev]) | ||