__init__.py
455 lines
| 15.0 KiB
| text/x-python
|
PythonLexer
Edouard Gomez
|
r4513 | # convert.py Foreign SCM converter | ||
Thomas Arendsen Hein
|
r4512 | # | ||
Thomas Arendsen Hein
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
Thomas Arendsen Hein
|
r4512 | # | ||
Edouard Gomez
|
r4513 | # This software may be used and distributed according to the terms | ||
# of the GNU General Public License, incorporated herein by reference. | ||||
Thomas Arendsen Hein
|
r4512 | |||
Thomas Arendsen Hein
|
r5139 | from common import NoRepo, converter_source, converter_sink | ||
Brendan Cully
|
r4536 | from cvs import convert_cvs | ||
from git import convert_git | ||||
Bryan O'Sullivan
|
r5013 | from hg import mercurial_source, mercurial_sink | ||
Thomas Arendsen Hein
|
r5139 | from subversion import convert_svn, debugsvnlog | ||
Brendan Cully
|
r4536 | |||
Thomas Arendsen Hein
|
r5139 | import os, shlex, shutil | ||
Thomas Arendsen Hein
|
r4532 | from mercurial import hg, ui, util, commands | ||
Bryan O'Sullivan
|
r5016 | from mercurial.i18n import _ | ||
Thomas Arendsen Hein
|
r4512 | |||
Thomas Arendsen Hein
|
r5138 | commands.norepo += " convert debugsvnlog" | ||
Edouard Gomez
|
r4513 | |||
Bryan O'Sullivan
|
r5013 | converters = [convert_cvs, convert_git, convert_svn, mercurial_source, | ||
mercurial_sink] | ||||
Thomas Arendsen Hein
|
r4512 | |||
Brendan Cully
|
r4780 | def convertsource(ui, path, **opts): | ||
Brendan Cully
|
r4763 | for c in converters: | ||
try: | ||||
Bryan O'Sullivan
|
r5018 | return c.getcommit and c(ui, path, **opts) | ||
except (AttributeError, NoRepo): | ||||
Brendan Cully
|
r4763 | pass | ||
raise util.Abort('%s: unknown repository type' % path) | ||||
def convertsink(ui, path): | ||||
Thomas Arendsen Hein
|
r4512 | if not os.path.isdir(path): | ||
Thomas Arendsen Hein
|
r4532 | raise util.Abort("%s: not a directory" % path) | ||
Thomas Arendsen Hein
|
r4512 | for c in converters: | ||
try: | ||||
Bryan O'Sullivan
|
r5018 | return c.putcommit and c(ui, path) | ||
except (AttributeError, NoRepo): | ||||
Thomas Arendsen Hein
|
r4512 | pass | ||
Brendan Cully
|
r4763 | raise util.Abort('%s: unknown repository type' % path) | ||
Thomas Arendsen Hein
|
r4512 | |||
class convert(object): | ||||
Bryan O'Sullivan
|
r5016 | def __init__(self, ui, source, dest, revmapfile, filemapper, opts): | ||
Thomas Arendsen Hein
|
r4512 | |||
self.source = source | ||||
self.dest = dest | ||||
Edouard Gomez
|
r4513 | self.ui = ui | ||
Thomas Arendsen Hein
|
r4512 | self.opts = opts | ||
self.commitcache = {} | ||||
Bryan O'Sullivan
|
r5011 | self.revmapfile = revmapfile | ||
self.revmapfilefd = None | ||||
Edouard Gomez
|
r4589 | self.authors = {} | ||
Brendan Cully
|
r4590 | self.authorfile = None | ||
Bryan O'Sullivan
|
r5016 | self.mapfile = filemapper | ||
Thomas Arendsen Hein
|
r4512 | |||
self.map = {} | ||||
try: | ||||
Bryan O'Sullivan
|
r5011 | origrevmapfile = open(self.revmapfile, 'r') | ||
for l in origrevmapfile: | ||||
Thomas Arendsen Hein
|
r4512 | sv, dv = l[:-1].split() | ||
self.map[sv] = dv | ||||
Bryan O'Sullivan
|
r5011 | origrevmapfile.close() | ||
Thomas Arendsen Hein
|
r4512 | except IOError: | ||
pass | ||||
Edouard Gomez
|
r4589 | # Read first the dst author map if any | ||
Brendan Cully
|
r4590 | authorfile = self.dest.authorfile() | ||
if authorfile and os.path.exists(authorfile): | ||||
self.readauthormap(authorfile) | ||||
Edouard Gomez
|
r4589 | # Extend/Override with new author map if necessary | ||
Brendan Cully
|
r4590 | if opts.get('authors'): | ||
Edouard Gomez
|
r4589 | self.readauthormap(opts.get('authors')) | ||
Brendan Cully
|
r4590 | self.authorfile = self.dest.authorfile() | ||
Edouard Gomez
|
r4589 | |||
Thomas Arendsen Hein
|
r4512 | def walktree(self, heads): | ||
Hollis Blanchard
|
r4719 | '''Return a mapping that identifies the uncommitted parents of every | ||
uncommitted changeset.''' | ||||
Thomas Arendsen Hein
|
r4512 | visit = heads | ||
known = {} | ||||
parents = {} | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n in known or n in self.map: continue | ||||
known[n] = 1 | ||||
self.commitcache[n] = self.source.getcommit(n) | ||||
cp = self.commitcache[n].parents | ||||
Hollis Blanchard
|
r4719 | parents[n] = [] | ||
Thomas Arendsen Hein
|
r4512 | for p in cp: | ||
Hollis Blanchard
|
r4719 | parents[n].append(p) | ||
Thomas Arendsen Hein
|
r4512 | visit.append(p) | ||
return parents | ||||
def toposort(self, parents): | ||||
Hollis Blanchard
|
r4719 | '''Return an ordering such that every uncommitted changeset is | ||
preceeded by all its uncommitted ancestors.''' | ||||
Thomas Arendsen Hein
|
r4512 | visit = parents.keys() | ||
seen = {} | ||||
children = {} | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n in seen: continue | ||||
seen[n] = 1 | ||||
Hollis Blanchard
|
r4719 | # Ensure that nodes without parents are present in the 'children' | ||
# mapping. | ||||
children.setdefault(n, []) | ||||
for p in parents[n]: | ||||
if not p in self.map: | ||||
Thomas Arendsen Hein
|
r4512 | visit.append(p) | ||
Hollis Blanchard
|
r4719 | children.setdefault(p, []).append(n) | ||
Thomas Arendsen Hein
|
r4512 | |||
s = [] | ||||
removed = {} | ||||
visit = children.keys() | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n in removed: continue | ||||
dep = 0 | ||||
if n in parents: | ||||
for p in parents[n]: | ||||
if p in self.map: continue | ||||
if p not in removed: | ||||
# we're still dependent | ||||
visit.append(n) | ||||
dep = 1 | ||||
break | ||||
if not dep: | ||||
# all n's parents are in the list | ||||
removed[n] = 1 | ||||
if n not in self.map: | ||||
s.append(n) | ||||
if n in children: | ||||
for c in children[n]: | ||||
visit.insert(0, c) | ||||
Edouard Gomez
|
r4513 | if self.opts.get('datesort'): | ||
Thomas Arendsen Hein
|
r4512 | depth = {} | ||
for n in s: | ||||
depth[n] = 0 | ||||
Thomas Arendsen Hein
|
r4532 | pl = [p for p in self.commitcache[n].parents | ||
if p not in self.map] | ||||
Thomas Arendsen Hein
|
r4512 | if pl: | ||
depth[n] = max([depth[p] for p in pl]) + 1 | ||||
s = [(depth[n], self.commitcache[n].date, n) for n in s] | ||||
s.sort() | ||||
s = [e[2] for e in s] | ||||
return s | ||||
Edouard Gomez
|
r4588 | def mapentry(self, src, dst): | ||
Bryan O'Sullivan
|
r5011 | if self.revmapfilefd is None: | ||
Edouard Gomez
|
r4588 | try: | ||
Bryan O'Sullivan
|
r5011 | self.revmapfilefd = open(self.revmapfile, "a") | ||
Edouard Gomez
|
r4588 | except IOError, (errno, strerror): | ||
Bryan O'Sullivan
|
r5011 | raise util.Abort("Could not open map file %s: %s, %s\n" % (self.revmapfile, errno, strerror)) | ||
Edouard Gomez
|
r4588 | self.map[src] = dst | ||
Bryan O'Sullivan
|
r5011 | self.revmapfilefd.write("%s %s\n" % (src, dst)) | ||
self.revmapfilefd.flush() | ||||
Edouard Gomez
|
r4588 | |||
Edouard Gomez
|
r4589 | def writeauthormap(self): | ||
Brendan Cully
|
r4590 | authorfile = self.authorfile | ||
if authorfile: | ||||
Edouard Gomez
|
r4589 | self.ui.status('Writing author map file %s\n' % authorfile) | ||
ofile = open(authorfile, 'w+') | ||||
for author in self.authors: | ||||
ofile.write("%s=%s\n" % (author, self.authors[author])) | ||||
ofile.close() | ||||
def readauthormap(self, authorfile): | ||||
Brendan Cully
|
r4590 | afile = open(authorfile, 'r') | ||
for line in afile: | ||||
try: | ||||
srcauthor = line.split('=')[0].strip() | ||||
dstauthor = line.split('=')[1].strip() | ||||
if srcauthor in self.authors and dstauthor != self.authors[srcauthor]: | ||||
self.ui.status( | ||||
'Overriding mapping for author %s, was %s, will be %s\n' | ||||
% (srcauthor, self.authors[srcauthor], dstauthor)) | ||||
else: | ||||
self.ui.debug('Mapping author %s to %s\n' | ||||
% (srcauthor, dstauthor)) | ||||
Edouard Gomez
|
r4589 | self.authors[srcauthor] = dstauthor | ||
Brendan Cully
|
r4590 | except IndexError: | ||
self.ui.warn( | ||||
'Ignoring bad line in author file map %s: %s\n' | ||||
% (authorfile, line)) | ||||
afile.close() | ||||
Edouard Gomez
|
r4589 | |||
Thomas Arendsen Hein
|
r4512 | def copy(self, rev): | ||
Bryan O'Sullivan
|
r5016 | commit = self.commitcache[rev] | ||
do_copies = hasattr(self.dest, 'copyfile') | ||||
filenames = [] | ||||
Thomas Arendsen Hein
|
r4957 | |||
Brendan Cully
|
r5121 | files, copies = self.source.getchanges(rev) | ||
Brendan Cully
|
r5173 | parents = [self.map[r] for r in commit.parents] | ||
if commit.parents: | ||||
pbranch = self.commitcache[commit.parents[0]].branch | ||||
else: | ||||
pbranch = None | ||||
self.dest.setbranch(commit.branch, pbranch, parents) | ||||
Brendan Cully
|
r5121 | for f, v in files: | ||
Bryan O'Sullivan
|
r5016 | newf = self.mapfile(f) | ||
if not newf: | ||||
continue | ||||
filenames.append(newf) | ||||
Thomas Arendsen Hein
|
r4512 | try: | ||
data = self.source.getfile(f, v) | ||||
except IOError, inst: | ||||
Bryan O'Sullivan
|
r5016 | self.dest.delfile(newf) | ||
Thomas Arendsen Hein
|
r4512 | else: | ||
e = self.source.getmode(f, v) | ||||
Bryan O'Sullivan
|
r5016 | self.dest.putfile(newf, e, data) | ||
Daniel Holth
|
r4765 | if do_copies: | ||
Brendan Cully
|
r5121 | if f in copies: | ||
copyf = self.mapfile(copies[f]) | ||||
Bryan O'Sullivan
|
r5016 | if copyf: | ||
# Merely marks that a copy happened. | ||||
self.dest.copyfile(copyf, newf) | ||||
Daniel Holth
|
r4765 | |||
Bryan O'Sullivan
|
r5016 | newnode = self.dest.putcommit(filenames, parents, commit) | ||
Edouard Gomez
|
r4588 | self.mapentry(rev, newnode) | ||
Thomas Arendsen Hein
|
r4512 | |||
def convert(self): | ||||
Edouard Gomez
|
r4588 | try: | ||
Bryan O'Sullivan
|
r5014 | self.dest.before() | ||
Brendan Cully
|
r4812 | self.source.setrevmap(self.map) | ||
Edouard Gomez
|
r4588 | self.ui.status("scanning source...\n") | ||
heads = self.source.getheads() | ||||
parents = self.walktree(heads) | ||||
self.ui.status("sorting...\n") | ||||
t = self.toposort(parents) | ||||
num = len(t) | ||||
c = None | ||||
self.ui.status("converting...\n") | ||||
for c in t: | ||||
num -= 1 | ||||
desc = self.commitcache[c].desc | ||||
if "\n" in desc: | ||||
desc = desc.splitlines()[0] | ||||
Edouard Gomez
|
r4589 | author = self.commitcache[c].author | ||
author = self.authors.get(author, author) | ||||
self.commitcache[c].author = author | ||||
Edouard Gomez
|
r4588 | self.ui.status("%d %s\n" % (num, desc)) | ||
self.copy(c) | ||||
Thomas Arendsen Hein
|
r4512 | |||
Edouard Gomez
|
r4588 | tags = self.source.gettags() | ||
ctags = {} | ||||
for k in tags: | ||||
v = tags[k] | ||||
if v in self.map: | ||||
ctags[k] = self.map[v] | ||||
Thomas Arendsen Hein
|
r4512 | |||
Edouard Gomez
|
r4588 | if c and ctags: | ||
nrev = self.dest.puttags(ctags) | ||||
# write another hash correspondence to override the previous | ||||
# one so we don't end up with extra tag heads | ||||
if nrev: | ||||
self.mapentry(c, nrev) | ||||
Edouard Gomez
|
r4589 | |||
self.writeauthormap() | ||||
Edouard Gomez
|
r4588 | finally: | ||
self.cleanup() | ||||
Thomas Arendsen Hein
|
r4512 | |||
Edouard Gomez
|
r4588 | def cleanup(self): | ||
Bryan O'Sullivan
|
r5014 | self.dest.after() | ||
if self.revmapfilefd: | ||||
self.revmapfilefd.close() | ||||
Thomas Arendsen Hein
|
r4512 | |||
Bryan O'Sullivan
|
r5016 | def rpairs(name): | ||
e = len(name) | ||||
while e != -1: | ||||
yield name[:e], name[e+1:] | ||||
e = name.rfind('/', 0, e) | ||||
class filemapper(object): | ||||
'''Map and filter filenames when importing. | ||||
A name can be mapped to itself, a new name, or None (omit from new | ||||
repository).''' | ||||
def __init__(self, ui, path=None): | ||||
self.ui = ui | ||||
self.include = {} | ||||
self.exclude = {} | ||||
self.rename = {} | ||||
if path: | ||||
if self.parse(path): | ||||
raise util.Abort(_('errors in filemap')) | ||||
def parse(self, path): | ||||
errs = 0 | ||||
def check(name, mapping, listname): | ||||
if name in mapping: | ||||
self.ui.warn(_('%s:%d: %r already in %s list\n') % | ||||
(lex.infile, lex.lineno, name, listname)) | ||||
return 1 | ||||
return 0 | ||||
lex = shlex.shlex(open(path), path, True) | ||||
lex.wordchars += '!@#$%^&*()-=+[]{}|;:,./<>?' | ||||
cmd = lex.get_token() | ||||
while cmd: | ||||
if cmd == 'include': | ||||
name = lex.get_token() | ||||
errs += check(name, self.exclude, 'exclude') | ||||
self.include[name] = name | ||||
elif cmd == 'exclude': | ||||
name = lex.get_token() | ||||
errs += check(name, self.include, 'include') | ||||
errs += check(name, self.rename, 'rename') | ||||
self.exclude[name] = name | ||||
elif cmd == 'rename': | ||||
src = lex.get_token() | ||||
dest = lex.get_token() | ||||
errs += check(src, self.exclude, 'exclude') | ||||
self.rename[src] = dest | ||||
elif cmd == 'source': | ||||
errs += self.parse(lex.get_token()) | ||||
else: | ||||
self.ui.warn(_('%s:%d: unknown directive %r\n') % | ||||
(lex.infile, lex.lineno, cmd)) | ||||
errs += 1 | ||||
cmd = lex.get_token() | ||||
return errs | ||||
def lookup(self, name, mapping): | ||||
for pre, suf in rpairs(name): | ||||
try: | ||||
return mapping[pre], pre, suf | ||||
except KeyError, err: | ||||
pass | ||||
return '', name, '' | ||||
Thomas Arendsen Hein
|
r5143 | |||
Bryan O'Sullivan
|
r5016 | def __call__(self, name): | ||
if self.include: | ||||
inc = self.lookup(name, self.include)[0] | ||||
else: | ||||
inc = name | ||||
if self.exclude: | ||||
exc = self.lookup(name, self.exclude)[0] | ||||
else: | ||||
exc = '' | ||||
if not inc or exc: | ||||
return None | ||||
newpre, pre, suf = self.lookup(name, self.rename) | ||||
if newpre: | ||||
Bryan O'Sullivan
|
r5133 | if newpre == '.': | ||
return suf | ||||
Bryan O'Sullivan
|
r5016 | if suf: | ||
return newpre + '/' + suf | ||||
return newpre | ||||
return name | ||||
Bryan O'Sullivan
|
r5011 | def _convert(ui, src, dest=None, revmapfile=None, **opts): | ||
Thomas Arendsen Hein
|
r4958 | """Convert a foreign SCM repository to a Mercurial one. | ||
Edouard Gomez
|
r4513 | |||
Accepted source formats: | ||||
- GIT | ||||
- CVS | ||||
Daniel Holth
|
r4765 | - SVN | ||
Edouard Gomez
|
r4513 | |||
Accepted destination formats: | ||||
- Mercurial | ||||
Brendan Cully
|
r4760 | If no revision is given, all revisions will be converted. Otherwise, | ||
convert will only import up to the named revision (given in a format | ||||
understood by the source). | ||||
Thomas Arendsen Hein
|
r4883 | If no destination directory name is specified, it defaults to the | ||
Thomas Arendsen Hein
|
r4958 | basename of the source with '-hg' appended. If the destination | ||
repository doesn't exist, it will be created. | ||||
Edouard Gomez
|
r4513 | |||
Bryan O'Sullivan
|
r5011 | If <revmapfile> isn't given, it will be put in a default location | ||
(<dest>/.hg/shamap by default). The <revmapfile> is a simple text | ||||
Thomas Arendsen Hein
|
r4883 | file that maps each source commit ID to the destination ID for | ||
that revision, like so: | ||||
Edouard Gomez
|
r4513 | <source ID> <destination ID> | ||
Thomas Arendsen Hein
|
r4958 | If the file doesn't exist, it's automatically created. It's updated | ||
Edouard Gomez
|
r4513 | on each commit copied, so convert-repo can be interrupted and can | ||
be run repeatedly to copy new commits. | ||||
Edouard Gomez
|
r4589 | |||
The [username mapping] file is a simple text file that maps each source | ||||
commit author to a destination commit author. It is handy for source SCMs | ||||
that use unix logins to identify authors (eg: CVS). One line per author | ||||
mapping and the line format is: | ||||
srcauthor=whatever string you want | ||||
Thomas Arendsen Hein
|
r4958 | """ | ||
Edouard Gomez
|
r4513 | |||
Alexis S. L. Carvalho
|
r4895 | util._encoding = 'UTF-8' | ||
Thomas Arendsen Hein
|
r4512 | if not dest: | ||
Thomas Arendsen Hein
|
r4883 | dest = hg.defaultdest(src) + "-hg" | ||
Edouard Gomez
|
r4513 | ui.status("assuming destination %s\n" % dest) | ||
Edouard Gomez
|
r4521 | |||
# Try to be smart and initalize things when required | ||||
Brendan Cully
|
r4761 | created = False | ||
Edouard Gomez
|
r4521 | if os.path.isdir(dest): | ||
if len(os.listdir(dest)) > 0: | ||||
try: | ||||
hg.repository(ui, dest) | ||||
ui.status("destination %s is a Mercurial repository\n" % dest) | ||||
Thomas Arendsen Hein
|
r4532 | except hg.RepoError: | ||
Edouard Gomez
|
r4521 | raise util.Abort( | ||
Thomas Arendsen Hein
|
r4532 | "destination directory %s is not empty.\n" | ||
"Please specify an empty directory to be initialized\n" | ||||
"or an already initialized mercurial repository" | ||||
% dest) | ||||
Edouard Gomez
|
r4521 | else: | ||
ui.status("initializing destination %s repository\n" % dest) | ||||
hg.repository(ui, dest, create=True) | ||||
Brendan Cully
|
r4761 | created = True | ||
Edouard Gomez
|
r4521 | elif os.path.exists(dest): | ||
Thomas Arendsen Hein
|
r4532 | raise util.Abort("destination %s exists and is not a directory" % dest) | ||
Edouard Gomez
|
r4521 | else: | ||
ui.status("initializing destination %s repository\n" % dest) | ||||
hg.repository(ui, dest, create=True) | ||||
Brendan Cully
|
r4761 | created = True | ||
Thomas Arendsen Hein
|
r4532 | |||
Brendan Cully
|
r4763 | destc = convertsink(ui, dest) | ||
Thomas Arendsen Hein
|
r4512 | |||
Brendan Cully
|
r4761 | try: | ||
Brendan Cully
|
r4763 | srcc = convertsource(ui, src, rev=opts.get('rev')) | ||
Brendan Cully
|
r4761 | except Exception: | ||
if created: | ||||
shutil.rmtree(dest, True) | ||||
raise | ||||
Thomas Arendsen Hein
|
r4512 | |||
Bryan O'Sullivan
|
r5011 | if not revmapfile: | ||
Thomas Arendsen Hein
|
r4512 | try: | ||
Bryan O'Sullivan
|
r5011 | revmapfile = destc.revmapfile() | ||
Thomas Arendsen Hein
|
r4512 | except: | ||
Bryan O'Sullivan
|
r5011 | revmapfile = os.path.join(destc, "map") | ||
Thomas Arendsen Hein
|
r4512 | |||
Bryan O'Sullivan
|
r5016 | |||
c = convert(ui, srcc, destc, revmapfile, filemapper(ui, opts['filemap']), | ||||
opts) | ||||
Thomas Arendsen Hein
|
r4512 | c.convert() | ||
Patrick Mezard
|
r5127 | |||
Edouard Gomez
|
r4513 | cmdtable = { | ||
Thomas Arendsen Hein
|
r4532 | "convert": | ||
(_convert, | ||||
Edouard Gomez
|
r4589 | [('A', 'authors', '', 'username mapping filename'), | ||
Bryan O'Sullivan
|
r5016 | ('', 'filemap', '', 'remap file names using contents of file'), | ||
Brendan Cully
|
r4760 | ('r', 'rev', '', 'import up to target revision REV'), | ||
Edouard Gomez
|
r4589 | ('', 'datesort', None, 'try to sort changesets by date')], | ||
Thomas Arendsen Hein
|
r4532 | 'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'), | ||
Thomas Arendsen Hein
|
r5138 | "debugsvnlog": | ||
Patrick Mezard
|
r5127 | (debugsvnlog, | ||
[], | ||||
Thomas Arendsen Hein
|
r5138 | 'hg debugsvnlog'), | ||
Edouard Gomez
|
r4513 | } | ||
Patrick Mezard
|
r5127 | |||