__init__.py
383 lines
| 12.3 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 | |||
|
r5510 | from common import NoRepo, SKIPREV, converter_source, converter_sink, mapfile | ||
|
r4536 | from cvs import convert_cvs | ||
|
r5359 | from darcs import darcs_source | ||
|
r4536 | from git import convert_git | ||
|
r5013 | from hg import mercurial_source, mercurial_sink | ||
|
r5513 | from subversion import debugsvnlog, svn_source, svn_sink | ||
|
r5377 | import filemap | ||
|
r4536 | |||
|
r5376 | import os, shutil | ||
|
r4532 | from mercurial import hg, ui, util, commands | ||
|
r5016 | from mercurial.i18n import _ | ||
|
r4512 | |||
|
r5138 | commands.norepo += " convert debugsvnlog" | ||
|
r4513 | |||
|
r5441 | source_converters = [ | ||
('cvs', convert_cvs), | ||||
('git', convert_git), | ||||
('svn', svn_source), | ||||
('hg', mercurial_source), | ||||
('darcs', darcs_source), | ||||
] | ||||
sink_converters = [ | ||||
('hg', mercurial_sink), | ||||
|
r5513 | ('svn', svn_sink), | ||
|
r5441 | ] | ||
def convertsource(ui, path, type, rev): | ||||
|
r5521 | exceptions = [] | ||
|
r5441 | for name, source in source_converters: | ||
|
r4763 | try: | ||
|
r5441 | if not type or name == type: | ||
return source(ui, path, rev) | ||||
|
r5415 | except NoRepo, inst: | ||
|
r5521 | exceptions.append(inst) | ||
if not ui.quiet: | ||||
for inst in exceptions: | ||||
ui.write(_("%s\n") % inst) | ||||
|
r4763 | raise util.Abort('%s: unknown repository type' % path) | ||
|
r5441 | def convertsink(ui, path, type): | ||
for name, sink in sink_converters: | ||||
|
r4512 | try: | ||
|
r5441 | if not type or name == type: | ||
return sink(ui, path) | ||||
|
r5415 | except NoRepo, inst: | ||
ui.note(_("convert: %s\n") % inst) | ||||
|
r4763 | raise util.Abort('%s: unknown repository type' % path) | ||
|
r4512 | |||
|
r5281 | class converter(object): | ||
|
r5375 | def __init__(self, ui, source, dest, revmapfile, opts): | ||
|
r4512 | |||
self.source = source | ||||
self.dest = dest | ||||
|
r4513 | self.ui = ui | ||
|
r4512 | self.opts = opts | ||
self.commitcache = {} | ||||
|
r4589 | self.authors = {} | ||
|
r4590 | self.authorfile = None | ||
|
r4512 | |||
|
r5510 | self.map = mapfile(ui, revmapfile) | ||
|
r4512 | |||
|
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 | ||||
|
r5203 | commit = self.cachecommit(n) | ||
|
r4719 | parents[n] = [] | ||
|
r5203 | for p in commit.parents: | ||
|
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 | ||||
|
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 | |||
|
r5203 | def cachecommit(self, rev): | ||
commit = self.source.getcommit(rev) | ||||
commit.author = self.authors.get(commit.author, commit.author) | ||||
self.commitcache[rev] = commit | ||||
return commit | ||||
|
r4512 | def copy(self, rev): | ||
|
r5016 | commit = self.commitcache[rev] | ||
do_copies = hasattr(self.dest, 'copyfile') | ||||
filenames = [] | ||||
|
r4957 | |||
|
r5374 | changes = self.source.getchanges(rev) | ||
if isinstance(changes, basestring): | ||||
if changes == SKIPREV: | ||||
dest = SKIPREV | ||||
else: | ||||
dest = self.map[changes] | ||||
|
r5510 | self.map[rev] = dest | ||
|
r5374 | return | ||
files, copies = changes | ||||
|
r5173 | parents = [self.map[r] for r in commit.parents] | ||
if commit.parents: | ||||
|
r5204 | prev = commit.parents[0] | ||
if prev not in self.commitcache: | ||||
self.cachecommit(prev) | ||||
pbranch = self.commitcache[prev].branch | ||||
|
r5173 | else: | ||
pbranch = None | ||||
self.dest.setbranch(commit.branch, pbranch, parents) | ||||
|
r5121 | for f, v in files: | ||
|
r5375 | filenames.append(f) | ||
|
r4512 | try: | ||
data = self.source.getfile(f, v) | ||||
except IOError, inst: | ||||
|
r5375 | self.dest.delfile(f) | ||
|
r4512 | else: | ||
e = self.source.getmode(f, v) | ||||
|
r5375 | self.dest.putfile(f, e, data) | ||
|
r4765 | if do_copies: | ||
|
r5121 | if f in copies: | ||
|
r5375 | copyf = copies[f] | ||
# Merely marks that a copy happened. | ||||
self.dest.copyfile(copyf, f) | ||||
|
r4765 | |||
|
r5375 | newnode = self.dest.putcommit(filenames, parents, commit) | ||
|
r5554 | self.source.converted(rev, newnode) | ||
|
r5510 | self.map[rev] = newnode | ||
|
r4512 | |||
def convert(self): | ||||
|
r4588 | try: | ||
|
r5356 | self.source.before() | ||
|
r5014 | self.dest.before() | ||
|
r5510 | 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] | ||||
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] | ||||
|
r5374 | if self.map.get(v, SKIPREV) != SKIPREV: | ||
|
r4588 | 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: | ||||
|
r5510 | self.map[c] = nrev | ||
|
r4589 | |||
self.writeauthormap() | ||||
|
r4588 | finally: | ||
self.cleanup() | ||||
|
r4512 | |||
|
r4588 | def cleanup(self): | ||
|
r5356 | try: | ||
self.dest.after() | ||||
finally: | ||||
self.source.after() | ||||
|
r5510 | self.map.close() | ||
|
r4512 | |||
|
r5281 | def convert(ui, src, dest=None, revmapfile=None, **opts): | ||
|
r4958 | """Convert a foreign SCM repository to a Mercurial one. | ||
|
r4513 | |||
Accepted source formats: | ||||
|
r5484 | - Mercurial | ||
|
r4513 | - CVS | ||
|
r5359 | - Darcs | ||
- git | ||||
- Subversion | ||||
|
r4513 | |||
Accepted destination formats: | ||||
- Mercurial | ||||
|
r5513 | - Subversion (history on branches is not preserved) | ||
|
r4513 | |||
|
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 | |||
|
r5484 | If <MAPFILE> isn't given, it will be put in a default location | ||
(<dest>/.hg/shamap by default). The <MAPFILE> is a simple text | ||||
|
r4883 | 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 | ||||
|
r5256 | |||
The filemap is a file that allows filtering and remapping of files | ||||
and directories. Comment lines start with '#'. Each line can | ||||
contain one of the following directives: | ||||
include path/to/file | ||||
exclude path/to/file | ||||
rename from/file to/file | ||||
The 'include' directive causes a file, or all files under a | ||||
|
r5484 | directory, to be included in the destination repository, and the | ||
|
r5488 | exclusion of all other files and dirs not explicitely included. | ||
|
r5484 | The 'exclude' directive causes files or directories to be omitted. | ||
The 'rename' directive renames a file or directory. To rename from a | ||||
subdirectory into the root of the repository, use '.' as the path to | ||||
rename to. | ||||
|
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 | |||
|
r5441 | destc = convertsink(ui, dest, opts.get('dest_type')) | ||
|
r4512 | |||
|
r4761 | try: | ||
|
r5441 | srcc = convertsource(ui, src, opts.get('source_type'), | ||
opts.get('rev')) | ||||
|
r4761 | except Exception: | ||
|
r5441 | for path in destc.created: | ||
shutil.rmtree(path, True) | ||||
|
r4761 | raise | ||
|
r4512 | |||
|
r5377 | fmap = opts.get('filemap') | ||
if fmap: | ||||
srcc = filemap.filemap_source(ui, srcc, fmap) | ||||
|
r5378 | destc.setfilemapmode(True) | ||
|
r5377 | |||
|
r5011 | if not revmapfile: | ||
|
r4512 | try: | ||
|
r5011 | revmapfile = destc.revmapfile() | ||
|
r4512 | except: | ||
|
r5011 | revmapfile = os.path.join(destc, "map") | ||
|
r4512 | |||
|
r5375 | c = converter(ui, srcc, destc, revmapfile, opts) | ||
|
r4512 | c.convert() | ||
|
r5127 | |||
|
r4513 | cmdtable = { | ||
|
r4532 | "convert": | ||
|
r5281 | (convert, | ||
|
r4589 | [('A', 'authors', '', 'username mapping filename'), | ||
|
r5441 | ('d', 'dest-type', '', 'destination repository type'), | ||
|
r5016 | ('', 'filemap', '', 'remap file names using contents of file'), | ||
|
r4760 | ('r', 'rev', '', 'import up to target revision REV'), | ||
|
r5441 | ('s', 'source-type', '', 'source repository type'), | ||
|
r4589 | ('', 'datesort', None, 'try to sort changesets by date')], | ||
|
r4532 | 'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'), | ||
|
r5138 | "debugsvnlog": | ||
|
r5127 | (debugsvnlog, | ||
[], | ||||
|
r5138 | 'hg debugsvnlog'), | ||
|
r4513 | } | ||
|
r5127 | |||