__init__.py
353 lines
| 11.5 KiB
| text/x-python
|
PythonLexer
|
r4513 | # convert.py Foreign SCM converter | ||
|
r4512 | # | ||
|
r4635 | # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | ||
|
r4512 | # | ||
|
r4513 | # This software may be used and distributed according to the terms | ||
# of the GNU General Public License, incorporated herein by reference. | ||||
|
r4512 | |||
|
r4763 | from common import NoRepo, converter_source, converter_sink | ||
|
r4536 | from cvs import convert_cvs | ||
from git import convert_git | ||||
from hg import convert_mercurial | ||||
|
r4766 | from subversion import convert_svn | ||
|
r4536 | |||
|
r4761 | import os, shutil | ||
|
r4532 | from mercurial import hg, ui, util, commands | ||
|
r4512 | |||
|
r4513 | commands.norepo += " convert" | ||
|
r4766 | converters = [convert_cvs, convert_git, convert_svn, convert_mercurial] | ||
|
r4512 | |||
|
r4780 | def convertsource(ui, path, **opts): | ||
|
r4763 | for c in converters: | ||
|
r4766 | if not hasattr(c, 'getcommit'): | ||
continue | ||||
|
r4763 | try: | ||
|
r4780 | return c(ui, path, **opts) | ||
|
r4763 | except NoRepo: | ||
pass | ||||
raise util.Abort('%s: unknown repository type' % path) | ||||
def convertsink(ui, path): | ||||
|
r4512 | if not os.path.isdir(path): | ||
|
r4532 | raise util.Abort("%s: not a directory" % path) | ||
|
r4512 | for c in converters: | ||
|
r4766 | if not hasattr(c, 'putcommit'): | ||
continue | ||||
|
r4512 | try: | ||
|
r4513 | return c(ui, path) | ||
|
r4512 | except NoRepo: | ||
pass | ||||
|
r4763 | raise util.Abort('%s: unknown repository type' % path) | ||
|
r4512 | |||
class convert(object): | ||||
|
r4513 | def __init__(self, ui, source, dest, mapfile, opts): | ||
|
r4512 | |||
self.source = source | ||||
self.dest = dest | ||||
|
r4513 | self.ui = ui | ||
|
r4512 | self.opts = opts | ||
self.commitcache = {} | ||||
|
r4588 | self.mapfile = mapfile | ||
self.mapfilefd = None | ||||
|
r4589 | self.authors = {} | ||
|
r4590 | self.authorfile = None | ||
|
r4512 | |||
self.map = {} | ||||
try: | ||||
|
r4588 | origmapfile = open(self.mapfile, 'r') | ||
for l in origmapfile: | ||||
|
r4512 | sv, dv = l[:-1].split() | ||
self.map[sv] = dv | ||||
|
r4588 | origmapfile.close() | ||
|
r4512 | except IOError: | ||
pass | ||||
|
r4589 | # Read first the dst author map if any | ||
|
r4590 | authorfile = self.dest.authorfile() | ||
if authorfile and os.path.exists(authorfile): | ||||
self.readauthormap(authorfile) | ||||
|
r4589 | # Extend/Override with new author map if necessary | ||
|
r4590 | if opts.get('authors'): | ||
|
r4589 | self.readauthormap(opts.get('authors')) | ||
|
r4590 | self.authorfile = self.dest.authorfile() | ||
|
r4589 | |||
|
r4512 | def walktree(self, heads): | ||
|
r4719 | '''Return a mapping that identifies the uncommitted parents of every | ||
uncommitted changeset.''' | ||||
|
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 | ||||
|
r4719 | parents[n] = [] | ||
|
r4512 | for p in cp: | ||
|
r4719 | parents[n].append(p) | ||
|
r4512 | visit.append(p) | ||
return parents | ||||
def toposort(self, parents): | ||||
|
r4719 | '''Return an ordering such that every uncommitted changeset is | ||
preceeded by all its uncommitted ancestors.''' | ||||
|
r4512 | visit = parents.keys() | ||
seen = {} | ||||
children = {} | ||||
while visit: | ||||
n = visit.pop(0) | ||||
if n in seen: continue | ||||
seen[n] = 1 | ||||
|
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: | ||||
|
r4512 | visit.append(p) | ||
|
r4719 | children.setdefault(p, []).append(n) | ||
|
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) | ||||
|
r4513 | if self.opts.get('datesort'): | ||
|
r4512 | depth = {} | ||
for n in s: | ||||
depth[n] = 0 | ||||
|
r4532 | pl = [p for p in self.commitcache[n].parents | ||
if p not in self.map] | ||||
|
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 | ||||
|
r4588 | def mapentry(self, src, dst): | ||
if self.mapfilefd is None: | ||||
try: | ||||
self.mapfilefd = open(self.mapfile, "a") | ||||
except IOError, (errno, strerror): | ||||
raise util.Abort("Could not open map file %s: %s, %s\n" % (self.mapfile, errno, strerror)) | ||||
self.map[src] = dst | ||||
self.mapfilefd.write("%s %s\n" % (src, dst)) | ||||
self.mapfilefd.flush() | ||||
|
r4589 | def writeauthormap(self): | ||
|
r4590 | authorfile = self.authorfile | ||
if authorfile: | ||||
|
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): | ||||
|
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)) | ||||
|
r4589 | self.authors[srcauthor] = dstauthor | ||
|
r4590 | except IndexError: | ||
self.ui.warn( | ||||
'Ignoring bad line in author file map %s: %s\n' | ||||
% (authorfile, line)) | ||||
afile.close() | ||||
|
r4589 | |||
|
r4512 | def copy(self, rev): | ||
c = self.commitcache[rev] | ||||
files = self.source.getchanges(rev) | ||||
|
r4957 | |||
|
r4765 | do_copies = (hasattr(c, 'copies') and hasattr(self.dest, 'copyfile')) | ||
|
r4512 | |||
|
r4532 | for f, v in files: | ||
|
r4512 | try: | ||
data = self.source.getfile(f, v) | ||||
except IOError, inst: | ||||
self.dest.delfile(f) | ||||
else: | ||||
e = self.source.getmode(f, v) | ||||
self.dest.putfile(f, e, data) | ||||
|
r4765 | if do_copies: | ||
if f in c.copies: | ||||
# Merely marks that a copy happened. | ||||
self.dest.copyfile(c.copies[f], f) | ||||
|
r4512 | |||
r = [self.map[v] for v in c.parents] | ||||
|
r4532 | f = [f for f, v in files] | ||
|
r4588 | newnode = self.dest.putcommit(f, r, c) | ||
self.mapentry(rev, newnode) | ||||
|
r4512 | |||
def convert(self): | ||||
|
r4588 | try: | ||
|
r4812 | self.source.setrevmap(self.map) | ||
|
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] | ||||
|
r4589 | author = self.commitcache[c].author | ||
author = self.authors.get(author, author) | ||||
self.commitcache[c].author = author | ||||
|
r4588 | self.ui.status("%d %s\n" % (num, desc)) | ||
self.copy(c) | ||||
|
r4512 | |||
|
r4588 | tags = self.source.gettags() | ||
ctags = {} | ||||
for k in tags: | ||||
v = tags[k] | ||||
if v in self.map: | ||||
ctags[k] = self.map[v] | ||||
|
r4512 | |||
|
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) | ||||
|
r4589 | |||
self.writeauthormap() | ||||
|
r4588 | finally: | ||
self.cleanup() | ||||
|
r4512 | |||
|
r4588 | def cleanup(self): | ||
if self.mapfilefd: | ||||
self.mapfilefd.close() | ||||
|
r4512 | |||
|
r4513 | def _convert(ui, src, dest=None, mapfile=None, **opts): | ||
|
r4958 | """Convert a foreign SCM repository to a Mercurial one. | ||
|
r4513 | |||
Accepted source formats: | ||||
- GIT | ||||
- CVS | ||||
|
r4765 | - SVN | ||
|
r4513 | |||
Accepted destination formats: | ||||
- Mercurial | ||||
|
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). | ||||
|
r4883 | If no destination directory name is specified, it defaults to the | ||
|
r4958 | basename of the source with '-hg' appended. If the destination | ||
repository doesn't exist, it will be created. | ||||
|
r4513 | |||
|
r4958 | If <mapfile> isn't given, it will be put in a default location | ||
|
r4883 | (<dest>/.hg/shamap by default). The <mapfile> is a simple text | ||
file that maps each source commit ID to the destination ID for | ||||
that revision, like so: | ||||
|
r4513 | <source ID> <destination ID> | ||
|
r4958 | If the file doesn't exist, it's automatically created. It's updated | ||
|
r4513 | on each commit copied, so convert-repo can be interrupted and can | ||
be run repeatedly to copy new commits. | ||||
|
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 | ||||
|
r4958 | """ | ||
|
r4513 | |||
|
r4895 | util._encoding = 'UTF-8' | ||
|
r4512 | if not dest: | ||
|
r4883 | dest = hg.defaultdest(src) + "-hg" | ||
|
r4513 | ui.status("assuming destination %s\n" % dest) | ||
|
r4521 | |||
# Try to be smart and initalize things when required | ||||
|
r4761 | created = False | ||
|
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) | ||||
|
r4532 | except hg.RepoError: | ||
|
r4521 | raise util.Abort( | ||
|
r4532 | "destination directory %s is not empty.\n" | ||
"Please specify an empty directory to be initialized\n" | ||||
"or an already initialized mercurial repository" | ||||
% dest) | ||||
|
r4521 | else: | ||
ui.status("initializing destination %s repository\n" % dest) | ||||
hg.repository(ui, dest, create=True) | ||||
|
r4761 | created = True | ||
|
r4521 | elif os.path.exists(dest): | ||
|
r4532 | raise util.Abort("destination %s exists and is not a directory" % dest) | ||
|
r4521 | else: | ||
ui.status("initializing destination %s repository\n" % dest) | ||||
hg.repository(ui, dest, create=True) | ||||
|
r4761 | created = True | ||
|
r4532 | |||
|
r4763 | destc = convertsink(ui, dest) | ||
|
r4512 | |||
|
r4761 | try: | ||
|
r4763 | srcc = convertsource(ui, src, rev=opts.get('rev')) | ||
|
r4761 | except Exception: | ||
if created: | ||||
shutil.rmtree(dest, True) | ||||
raise | ||||
|
r4512 | |||
if not mapfile: | ||||
try: | ||||
mapfile = destc.mapfile() | ||||
except: | ||||
mapfile = os.path.join(destc, "map") | ||||
|
r4513 | c = convert(ui, srcc, destc, mapfile, opts) | ||
|
r4512 | c.convert() | ||
|
r4513 | cmdtable = { | ||
|
r4532 | "convert": | ||
(_convert, | ||||
|
r4589 | [('A', 'authors', '', 'username mapping filename'), | ||
|
r4760 | ('r', 'rev', '', 'import up to target revision REV'), | ||
|
r4589 | ('', 'datesort', None, 'try to sort changesets by date')], | ||
|
r4532 | 'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'), | ||
|
r4513 | } | ||