convert-repo
650 lines
| 20.4 KiB
| text/plain
|
TextLexer
/ contrib / convert-repo
mpm@selenic.com
|
r316 | #!/usr/bin/env python | ||
# | ||||
# This is a generalized framework for converting between SCM | ||||
# repository formats. | ||||
# | ||||
Alexis S. L. Carvalho
|
r3917 | # To use, run: | ||
# | ||||
Matt Mackall
|
r3939 | # convert-repo <source> [<dest> [<mapfile>]] | ||
Alexis S. L. Carvalho
|
r3917 | # | ||
Matt Mackall
|
r3953 | # Currently accepted source formats: git, cvs | ||
Matt Mackall
|
r3939 | # Currently accepted destination formats: hg | ||
Alexis S. L. Carvalho
|
r3917 | # | ||
Matt Mackall
|
r3939 | # If destination isn't given, a new Mercurial repo named <src>-hg will | ||
# be created. If <mapfile> isn't given, it will be put in a default | ||||
# location (<dest>/.hg/shamap by default) | ||||
mpm@selenic.com
|
r316 | # | ||
Matt Mackall
|
r3939 | # The <mapfile> is a simple text file that maps each source commit ID to | ||
# the destination ID for that revision, like so: | ||||
# | ||||
# <source ID> <destination ID> | ||||
mpm@selenic.com
|
r316 | # | ||
Alexis S. L. Carvalho
|
r3917 | # If the file doesn't exist, it's automatically created. It's updated | ||
# on each commit copied, so convert-repo can be interrupted and can | ||||
# be run repeatedly to copy new commits. | ||||
mpm@selenic.com
|
r316 | |||
csaba.henk@creo.hu
|
r4047 | import sys, os, zlib, sha, time, re, locale, socket | ||
Matt Mackall
|
r3938 | os.environ["HGENCODING"] = "utf-8" | ||
from mercurial import hg, ui, util, fancyopts | ||||
Matt Mackall
|
r3821 | |||
Matt Mackall
|
r3938 | class Abort(Exception): pass | ||
Matt Mackall
|
r3953 | class NoRepo(Exception): pass | ||
Matt Mackall
|
r3938 | |||
Matt Mackall
|
r3954 | class commit: | ||
def __init__(self, **parts): | ||||
for x in "author date desc parents".split(): | ||||
if not x in parts: | ||||
abort("commit missing field %s\n" % x) | ||||
self.__dict__.update(parts) | ||||
Matt Mackall
|
r3938 | quiet = 0 | ||
def status(msg): | ||||
if not quiet: sys.stdout.write(str(msg)) | ||||
Matt Mackall
|
r3821 | |||
Matt Mackall
|
r3938 | def warn(msg): | ||
sys.stderr.write(str(msg)) | ||||
def abort(msg): | ||||
raise Abort(msg) | ||||
mpm@selenic.com
|
r316 | |||
Matt Mackall
|
r3821 | def recode(s): | ||
try: | ||||
return s.decode("utf-8").encode("utf-8") | ||||
except: | ||||
try: | ||||
return s.decode("latin-1").encode("utf-8") | ||||
except: | ||||
return s.decode("utf-8", "replace").encode("utf-8") | ||||
Matt Mackall
|
r3953 | # CVS conversion code inspired by hg-cvs-import and git-cvsimport | ||
class convert_cvs: | ||||
def __init__(self, path): | ||||
self.path = path | ||||
cvs = os.path.join(path, "CVS") | ||||
if not os.path.exists(cvs): | ||||
raise NoRepo("couldn't open CVS repo %s" % path) | ||||
self.changeset = {} | ||||
Matt Mackall
|
r3954 | self.files = {} | ||
Matt Mackall
|
r3953 | self.tags = {} | ||
self.lastbranch = {} | ||||
self.parent = {} | ||||
self.socket = None | ||||
self.cvsroot = file(os.path.join(cvs, "Root")).read()[:-1] | ||||
self.cvsrepo = file(os.path.join(cvs, "Repository")).read()[:-1] | ||||
self.encoding = locale.getpreferredencoding() | ||||
self._parse() | ||||
self._connect() | ||||
def _parse(self): | ||||
if self.changeset: | ||||
return | ||||
d = os.getcwd() | ||||
try: | ||||
os.chdir(self.path) | ||||
id = None | ||||
state = 0 | ||||
Edouard Gomez
|
r4112 | for l in os.popen("cvsps -A -u --cvs-direct -q"): | ||
Matt Mackall
|
r3953 | if state == 0: # header | ||
if l.startswith("PatchSet"): | ||||
id = l[9:-2] | ||||
elif l.startswith("Date"): | ||||
date = util.parsedate(l[6:-1], ["%Y/%m/%d %H:%M:%S"]) | ||||
date = util.datestr(date) | ||||
elif l.startswith("Branch"): | ||||
branch = l[8:-1] | ||||
self.parent[id] = self.lastbranch.get(branch,'bad') | ||||
self.lastbranch[branch] = id | ||||
elif l.startswith("Ancestor branch"): | ||||
ancestor = l[17:-1] | ||||
self.parent[id] = self.lastbranch[ancestor] | ||||
elif l.startswith("Author"): | ||||
author = self.recode(l[8:-1]) | ||||
elif l.startswith("Tag: "): | ||||
Edouard Gomez
|
r4113 | t = l[5:-1].rstrip() | ||
if t != "(none)": | ||||
Matt Mackall
|
r3953 | self.tags[t] = id | ||
elif l.startswith("Log:"): | ||||
state = 1 | ||||
log = "" | ||||
elif state == 1: # log | ||||
if l == "Members: \n": | ||||
files = {} | ||||
log = self.recode(log[:-1]) | ||||
if log.isspace(): | ||||
log = "*** empty log message ***\n" | ||||
state = 2 | ||||
else: | ||||
log += l | ||||
elif state == 2: | ||||
if l == "\n": # | ||||
state = 0 | ||||
Matt Mackall
|
r3954 | p = [self.parent[id]] | ||
if id == "1": | ||||
p = [] | ||||
c = commit(author=author, date=date, parents=p, | ||||
desc=log, branch=branch) | ||||
self.changeset[id] = c | ||||
self.files[id] = files | ||||
Matt Mackall
|
r3953 | else: | ||
file,rev = l[1:-2].rsplit(':',1) | ||||
rev = rev.split("->")[1] | ||||
files[file] = rev | ||||
self.heads = self.lastbranch.values() | ||||
finally: | ||||
os.chdir(d) | ||||
def _connect(self): | ||||
root = self.cvsroot | ||||
csaba.henk@creo.hu
|
r4047 | conntype = None | ||
Matt Mackall
|
r3953 | user, host = None, None | ||
cmd = ['cvs', 'server'] | ||||
status("connecting to %s\n" % root) | ||||
csaba.henk@creo.hu
|
r4047 | if root.startswith(":pserver:"): | ||
root = root[9:] | ||||
m = re.match(r'(?:(.*?)(?::(.*?))?@)?([^:\/]*)(?::(\d*))?(.*)', root) | ||||
if m: | ||||
conntype = "pserver" | ||||
user, passw, serv, port, root = m.groups() | ||||
if not user: | ||||
user = "anonymous" | ||||
rr = ":pserver:" + user + "@" + serv + ":" + root | ||||
if port: | ||||
rr2, port = "-", int(port) | ||||
else: | ||||
rr2, port = rr, 2401 | ||||
rr += str(port) | ||||
Matt Mackall
|
r3953 | |||
csaba.henk@creo.hu
|
r4047 | if not passw: | ||
passw = "A" | ||||
pf = open(os.path.join(os.environ["HOME"], ".cvspass")) | ||||
for l in pf: | ||||
# :pserver:cvs@mea.tmt.tele.fi:/cvsroot/zmailer Ah<Z | ||||
m = re.match(r'(/\d+\s+/)?(.*)', l) | ||||
l = m.group(2) | ||||
w, p = l.split(' ', 1) | ||||
if w in [rr, rr2]: | ||||
passw = p | ||||
break | ||||
pf.close() | ||||
sck = socket.socket() | ||||
sck.connect((serv, port)) | ||||
sck.send("\n".join(["BEGIN AUTH REQUEST", root, user, passw, "END AUTH REQUEST", ""])) | ||||
if sck.recv(128) != "I LOVE YOU\n": | ||||
raise NoRepo("CVS pserver authentication failed") | ||||
self.writep = self.readp = sck.makefile('r+') | ||||
if not conntype and root.startswith(":local:"): | ||||
conntype = "local" | ||||
Matt Mackall
|
r3953 | root = root[7:] | ||
csaba.henk@creo.hu
|
r4047 | |||
if not conntype: | ||||
Matt Mackall
|
r3953 | # :ext:user@host/home/user/path/to/cvsroot | ||
if root.startswith(":ext:"): | ||||
root = root[5:] | ||||
m = re.match(r'(?:([^@:/]+)@)?([^:/]+):?(.*)', root) | ||||
if not m: | ||||
csaba.henk@creo.hu
|
r4047 | conntype = "local" | ||
Matt Mackall
|
r3953 | else: | ||
csaba.henk@creo.hu
|
r4047 | conntype = "rsh" | ||
Matt Mackall
|
r3953 | user, host, root = m.group(1), m.group(2), m.group(3) | ||
csaba.henk@creo.hu
|
r4047 | if conntype != "pserver": | ||
if conntype == "rsh": | ||||
rsh = os.environ.get("CVS_RSH" or "rsh") | ||||
if user: | ||||
cmd = [rsh, '-l', user, host] + cmd | ||||
else: | ||||
cmd = [rsh, host] + cmd | ||||
Matt Mackall
|
r3953 | |||
csaba.henk@creo.hu
|
r4047 | self.writep, self.readp = os.popen2(cmd) | ||
Matt Mackall
|
r3953 | self.realroot = root | ||
self.writep.write("Root %s\n" % root) | ||||
self.writep.write("Valid-responses ok error Valid-requests Mode" | ||||
" M Mbinary E Checked-in Created Updated" | ||||
" Merged Removed\n") | ||||
self.writep.write("valid-requests\n") | ||||
self.writep.flush() | ||||
r = self.readp.readline() | ||||
if not r.startswith("Valid-requests"): | ||||
abort("server sucks\n") | ||||
if "UseUnchanged" in r: | ||||
self.writep.write("UseUnchanged\n") | ||||
self.writep.flush() | ||||
r = self.readp.readline() | ||||
def getheads(self): | ||||
return self.heads | ||||
Matt Mackall
|
r3956 | def _getfile(self, name, rev): | ||
Matt Mackall
|
r3953 | if rev.endswith("(DEAD)"): | ||
raise IOError | ||||
args = ("-N -P -kk -r %s --" % rev).split() | ||||
args.append(os.path.join(self.cvsrepo, name)) | ||||
for x in args: | ||||
self.writep.write("Argument %s\n" % x) | ||||
self.writep.write("Directory .\n%s\nco\n" % self.realroot) | ||||
self.writep.flush() | ||||
data = "" | ||||
while 1: | ||||
line = self.readp.readline() | ||||
if line.startswith("Created ") or line.startswith("Updated "): | ||||
self.readp.readline() # path | ||||
self.readp.readline() # entries | ||||
mode = self.readp.readline()[:-1] | ||||
count = int(self.readp.readline()[:-1]) | ||||
data = self.readp.read(count) | ||||
elif line.startswith(" "): | ||||
data += line[1:] | ||||
elif line.startswith("M "): | ||||
pass | ||||
elif line.startswith("Mbinary "): | ||||
count = int(self.readp.readline()[:-1]) | ||||
data = self.readp.read(count) | ||||
else: | ||||
if line == "ok\n": | ||||
Daniel Holth
|
r4082 | return (data, "x" in mode and "x" or "") | ||
Matt Mackall
|
r3953 | elif line.startswith("E "): | ||
warn("cvs server: %s\n" % line[2:]) | ||||
elif line.startswith("Remove"): | ||||
l = self.readp.readline() | ||||
l = self.readp.readline() | ||||
if l != "ok\n": | ||||
abort("unknown CVS response: %s\n" % l) | ||||
else: | ||||
abort("unknown CVS response: %s\n" % line) | ||||
Matt Mackall
|
r3956 | def getfile(self, file, rev): | ||
data, mode = self._getfile(file, rev) | ||||
self.modecache[(file, rev)] = mode | ||||
return data | ||||
def getmode(self, file, rev): | ||||
return self.modecache[(file, rev)] | ||||
Matt Mackall
|
r3953 | def getchanges(self, rev): | ||
Matt Mackall
|
r3956 | self.modecache = {} | ||
Matt Mackall
|
r3954 | files = self.files[rev] | ||
Matt Mackall
|
r3956 | cl = files.items() | ||
Matt Mackall
|
r3953 | cl.sort() | ||
return cl | ||||
def recode(self, text): | ||||
return text.decode(self.encoding, "replace").encode("utf-8") | ||||
def getcommit(self, rev): | ||||
Matt Mackall
|
r3954 | return self.changeset[rev] | ||
Matt Mackall
|
r3953 | |||
def gettags(self): | ||||
return self.tags | ||||
mpm@selenic.com
|
r316 | class convert_git: | ||
def __init__(self, path): | ||||
Matt Mackall
|
r3938 | if os.path.isdir(path + "/.git"): | ||
path += "/.git" | ||||
mpm@selenic.com
|
r316 | self.path = path | ||
Matt Mackall
|
r4102 | if not os.path.exists(path + "/objects"): | ||
Matt Mackall
|
r3953 | raise NoRepo("couldn't open GIT repo %s" % path) | ||
mpm@selenic.com
|
r316 | |||
def getheads(self): | ||||
Alexis S. L. Carvalho
|
r2657 | fh = os.popen("GIT_DIR=%s git-rev-parse --verify HEAD" % self.path) | ||
return [fh.read()[:-1]] | ||||
mpm@selenic.com
|
r316 | |||
mpm@selenic.com
|
r692 | def catfile(self, rev, type): | ||
if rev == "0" * 40: raise IOError() | ||||
Florian La Roche
|
r1335 | fh = os.popen("GIT_DIR=%s git-cat-file %s %s 2>/dev/null" % (self.path, type, rev)) | ||
mpm@selenic.com
|
r692 | return fh.read() | ||
mpm@selenic.com
|
r316 | def getfile(self, name, rev): | ||
mpm@selenic.com
|
r692 | return self.catfile(rev, "blob") | ||
mpm@selenic.com
|
r316 | |||
Matt Mackall
|
r3956 | def getmode(self, name, rev): | ||
return self.modecache[(name, rev)] | ||||
mpm@selenic.com
|
r316 | def getchanges(self, version): | ||
Matt Mackall
|
r3956 | self.modecache = {} | ||
Florian La Roche
|
r1335 | fh = os.popen("GIT_DIR=%s git-diff-tree --root -m -r %s" % (self.path, version)) | ||
mpm@selenic.com
|
r316 | changes = [] | ||
for l in fh: | ||||
if "\t" not in l: continue | ||||
m, f = l[:-1].split("\t") | ||||
m = m.split() | ||||
h = m[3] | ||||
p = (m[1] == "100755") | ||||
Daniel Holth
|
r4082 | s = (m[1] == "120000") | ||
self.modecache[(f, h)] = (p and "x") or (s and "l") or "" | ||||
Matt Mackall
|
r3956 | changes.append((f, h)) | ||
mpm@selenic.com
|
r316 | return changes | ||
def getcommit(self, version): | ||||
mpm@selenic.com
|
r692 | c = self.catfile(version, "commit") # read the commit hash | ||
mpm@selenic.com
|
r316 | end = c.find("\n\n") | ||
message = c[end+2:] | ||||
Matt Mackall
|
r3821 | message = recode(message) | ||
mpm@selenic.com
|
r316 | l = c[:end].splitlines() | ||
manifest = l[0].split()[1] | ||||
parents = [] | ||||
for e in l[1:]: | ||||
n,v = e.split(" ", 1) | ||||
if n == "author": | ||||
p = v.split() | ||||
Matt Mackall
|
r1385 | tm, tz = p[-2:] | ||
mpm@selenic.com
|
r316 | author = " ".join(p[:-2]) | ||
if author[0] == "<": author = author[1:-1] | ||||
Matt Mackall
|
r3821 | author = recode(author) | ||
mpm@selenic.com
|
r692 | if n == "committer": | ||
mpm@selenic.com
|
r431 | p = v.split() | ||
Matt Mackall
|
r1385 | tm, tz = p[-2:] | ||
mpm@selenic.com
|
r431 | committer = " ".join(p[:-2]) | ||
if committer[0] == "<": committer = committer[1:-1] | ||||
Matt Mackall
|
r3821 | committer = recode(committer) | ||
Matt Mackall
|
r3910 | message += "\ncommitter: %s\n" % committer | ||
mpm@selenic.com
|
r316 | if n == "parent": parents.append(v) | ||
Matt Mackall
|
r1385 | |||
tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] | ||||
Vadim Gelfer
|
r2093 | tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) | ||
Matt Mackall
|
r1385 | date = tm + " " + str(tz) | ||
Matt Mackall
|
r3954 | |||
c = commit(parents=parents, date=date, author=author, desc=message) | ||||
return c | ||||
mpm@selenic.com
|
r316 | |||
mpm@selenic.com
|
r694 | def gettags(self): | ||
tags = {} | ||||
Alexis S. L. Carvalho
|
r4062 | fh = os.popen('git-ls-remote --tags "%s" 2>/dev/null' % self.path) | ||
prefix = 'refs/tags/' | ||||
for line in fh: | ||||
line = line.strip() | ||||
if not line.endswith("^{}"): | ||||
continue | ||||
node, tag = line.split(None, 1) | ||||
if not tag.startswith(prefix): | ||||
continue | ||||
tag = tag[len(prefix):-3] | ||||
tags[tag] = node | ||||
mpm@selenic.com
|
r694 | return tags | ||
mpm@selenic.com
|
r316 | class convert_mercurial: | ||
def __init__(self, path): | ||||
self.path = path | ||||
u = ui.ui() | ||||
Matt Mackall
|
r3938 | try: | ||
self.repo = hg.repository(u, path) | ||||
except: | ||||
Matt Mackall
|
r3953 | raise NoRepo("could open hg repo %s" % path) | ||
Matt Mackall
|
r3938 | |||
def mapfile(self): | ||||
return os.path.join(self.path, ".hg", "shamap") | ||||
mpm@selenic.com
|
r316 | |||
def getheads(self): | ||||
h = self.repo.changelog.heads() | ||||
Florian La Roche
|
r1335 | return [ hg.hex(x) for x in h ] | ||
mpm@selenic.com
|
r692 | |||
mpm@selenic.com
|
r316 | def putfile(self, f, e, data): | ||
Daniel Holth
|
r4082 | self.repo.wwrite(f, data, e) | ||
mpm@selenic.com
|
r692 | if self.repo.dirstate.state(f) == '?': | ||
self.repo.dirstate.update([f], "a") | ||||
mpm@selenic.com
|
r316 | def delfile(self, f): | ||
try: | ||||
os.unlink(self.repo.wjoin(f)) | ||||
mpm@selenic.com
|
r692 | #self.repo.remove([f]) | ||
mpm@selenic.com
|
r316 | except: | ||
pass | ||||
Matt Mackall
|
r3954 | def putcommit(self, files, parents, commit): | ||
mpm@selenic.com
|
r431 | seen = {} | ||
pl = [] | ||||
for p in parents: | ||||
if p not in seen: | ||||
pl.append(p) | ||||
seen[p] = 1 | ||||
parents = pl | ||||
mpm@selenic.com
|
r316 | |||
mpm@selenic.com
|
r692 | if len(parents) < 2: parents.append("0" * 40) | ||
if len(parents) < 2: parents.append("0" * 40) | ||||
mpm@selenic.com
|
r431 | p2 = parents.pop(0) | ||
mpm@selenic.com
|
r692 | |||
Matt Mackall
|
r3954 | text = commit.desc | ||
extra = {} | ||||
try: | ||||
extra["branch"] = commit.branch | ||||
except AttributeError: | ||||
pass | ||||
mpm@selenic.com
|
r431 | while parents: | ||
p1 = p2 | ||||
p2 = parents.pop(0) | ||||
Matt Mackall
|
r3954 | a = self.repo.rawcommit(files, text, commit.author, commit.date, | ||
hg.bin(p1), hg.bin(p2), extra=extra) | ||||
mpm@selenic.com
|
r431 | text = "(octopus merge fixup)\n" | ||
Matt Mackall
|
r1389 | p2 = hg.hex(self.repo.changelog.tip()) | ||
mpm@selenic.com
|
r431 | |||
Matt Mackall
|
r1389 | return p2 | ||
mpm@selenic.com
|
r316 | |||
mpm@selenic.com
|
r694 | def puttags(self, tags): | ||
try: | ||||
old = self.repo.wfile(".hgtags").read() | ||||
oldlines = old.splitlines(1) | ||||
oldlines.sort() | ||||
except: | ||||
oldlines = [] | ||||
k = tags.keys() | ||||
k.sort() | ||||
newlines = [] | ||||
for tag in k: | ||||
newlines.append("%s %s\n" % (tags[tag], tag)) | ||||
newlines.sort() | ||||
if newlines != oldlines: | ||||
Matt Mackall
|
r3938 | status("updating tags\n") | ||
mpm@selenic.com
|
r694 | f = self.repo.wfile(".hgtags", "w") | ||
f.write("".join(newlines)) | ||||
f.close() | ||||
if not oldlines: self.repo.add([".hgtags"]) | ||||
Florian La Roche
|
r1335 | date = "%s 0" % int(time.mktime(time.gmtime())) | ||
mpm@selenic.com
|
r694 | self.repo.rawcommit([".hgtags"], "update tags", "convert-repo", | ||
date, self.repo.changelog.tip(), hg.nullid) | ||||
Matt Mackall
|
r1387 | return hg.hex(self.repo.changelog.tip()) | ||
mpm@selenic.com
|
r694 | |||
Matt Mackall
|
r3953 | converters = [convert_cvs, convert_git, convert_mercurial] | ||
Matt Mackall
|
r3938 | |||
def converter(path): | ||||
if not os.path.isdir(path): | ||||
abort("%s: not a directory\n" % path) | ||||
for c in converters: | ||||
try: | ||||
return c(path) | ||||
Matt Mackall
|
r3953 | except NoRepo: | ||
Matt Mackall
|
r3938 | pass | ||
abort("%s: unknown repository type\n" % path) | ||||
mpm@selenic.com
|
r316 | class convert: | ||
Matt Mackall
|
r3957 | def __init__(self, source, dest, mapfile, opts): | ||
Matt Mackall
|
r3938 | |||
mpm@selenic.com
|
r316 | self.source = source | ||
self.dest = dest | ||||
self.mapfile = mapfile | ||||
Matt Mackall
|
r3957 | self.opts = opts | ||
mpm@selenic.com
|
r316 | self.commitcache = {} | ||
self.map = {} | ||||
Matt Mackall
|
r1655 | try: | ||
for l in file(self.mapfile): | ||||
sv, dv = l[:-1].split() | ||||
self.map[sv] = dv | ||||
except IOError: | ||||
pass | ||||
mpm@selenic.com
|
r316 | |||
def walktree(self, heads): | ||||
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) | ||||
Matt Mackall
|
r3954 | cp = self.commitcache[n].parents | ||
mpm@selenic.com
|
r316 | for p in cp: | ||
parents.setdefault(n, []).append(p) | ||||
visit.append(p) | ||||
return parents | ||||
def toposort(self, parents): | ||||
visit = parents.keys() | ||||
seen = {} | ||||
children = {} | ||||
mpm@selenic.com
|
r692 | |||
mpm@selenic.com
|
r316 | while visit: | ||
n = visit.pop(0) | ||||
if n in seen: continue | ||||
seen[n] = 1 | ||||
pc = 0 | ||||
if n in parents: | ||||
for p in parents[n]: | ||||
if p not in self.map: pc += 1 | ||||
visit.append(p) | ||||
children.setdefault(p, []).append(n) | ||||
if not pc: root = n | ||||
s = [] | ||||
removed = {} | ||||
mpm@selenic.com
|
r692 | visit = children.keys() | ||
mpm@selenic.com
|
r316 | 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 | ||||
Matt Mackall
|
r3957 | if n not in self.map: | ||
s.append(n) | ||||
mpm@selenic.com
|
r316 | if n in children: | ||
for c in children[n]: | ||||
visit.insert(0, c) | ||||
Matt Mackall
|
r3957 | if opts.get('datesort'): | ||
depth = {} | ||||
for n in s: | ||||
depth[n] = 0 | ||||
pl = [p for p in self.commitcache[n].parents if p not in self.map] | ||||
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] | ||||
mpm@selenic.com
|
r316 | return s | ||
def copy(self, rev): | ||||
Matt Mackall
|
r3954 | c = self.commitcache[rev] | ||
mpm@selenic.com
|
r316 | files = self.source.getchanges(rev) | ||
Matt Mackall
|
r3956 | for f,v in files: | ||
mpm@selenic.com
|
r316 | try: | ||
data = self.source.getfile(f, v) | ||||
except IOError, inst: | ||||
self.dest.delfile(f) | ||||
else: | ||||
Matt Mackall
|
r3956 | e = self.source.getmode(f, v) | ||
mpm@selenic.com
|
r316 | self.dest.putfile(f, e, data) | ||
Matt Mackall
|
r3954 | r = [self.map[v] for v in c.parents] | ||
Matt Mackall
|
r3956 | f = [f for f,v in files] | ||
Matt Mackall
|
r3954 | self.map[rev] = self.dest.putcommit(f, r, c) | ||
mpm@selenic.com
|
r316 | file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) | ||
def convert(self): | ||||
Matt Mackall
|
r3938 | status("scanning source...\n") | ||
mpm@selenic.com
|
r316 | heads = self.source.getheads() | ||
parents = self.walktree(heads) | ||||
Matt Mackall
|
r3938 | status("sorting...\n") | ||
mpm@selenic.com
|
r316 | t = self.toposort(parents) | ||
num = len(t) | ||||
Matt Mackall
|
r1715 | c = None | ||
mpm@selenic.com
|
r316 | |||
Matt Mackall
|
r3938 | status("converting...\n") | ||
mpm@selenic.com
|
r316 | for c in t: | ||
num -= 1 | ||||
Matt Mackall
|
r3954 | desc = self.commitcache[c].desc | ||
Matt Mackall
|
r3953 | if "\n" in desc: | ||
desc = desc.splitlines()[0] | ||||
Matt Mackall
|
r3938 | status("%d %s\n" % (num, desc)) | ||
mpm@selenic.com
|
r316 | self.copy(c) | ||
mpm@selenic.com
|
r694 | tags = self.source.gettags() | ||
ctags = {} | ||||
for k in tags: | ||||
v = tags[k] | ||||
if v in self.map: | ||||
ctags[k] = self.map[v] | ||||
Matt Mackall
|
r1715 | if c and ctags: | ||
Matt Mackall
|
r1387 | nrev = self.dest.puttags(ctags) | ||
# write another hash correspondence to override the previous | ||||
# one so we don't end up with extra tag heads | ||||
Matt Mackall
|
r3911 | if nrev: | ||
file(self.mapfile, "a").write("%s %s\n" % (c, nrev)) | ||||
mpm@selenic.com
|
r694 | |||
Matt Mackall
|
r3938 | def command(src, dest=None, mapfile=None, **opts): | ||
srcc = converter(src) | ||||
if not hasattr(srcc, "getcommit"): | ||||
abort("%s: can't read from this repo type\n" % src) | ||||
if not dest: | ||||
dest = src + "-hg" | ||||
status("assuming destination %s\n" % dest) | ||||
if not os.path.isdir(dest): | ||||
status("creating repository %s\n" % dest) | ||||
os.system("hg init " + dest) | ||||
destc = converter(dest) | ||||
if not hasattr(destc, "putcommit"): | ||||
abort("%s: can't write to this repo type\n" % src) | ||||
mpm@selenic.com
|
r316 | |||
Matt Mackall
|
r3938 | if not mapfile: | ||
try: | ||||
mapfile = destc.mapfile() | ||||
except: | ||||
mapfile = os.path.join(destc, "map") | ||||
Matt Mackall
|
r3957 | c = convert(srcc, destc, mapfile, opts) | ||
Matt Mackall
|
r3938 | c.convert() | ||
Matt Mackall
|
r3957 | options = [('q', 'quiet', None, 'suppress output'), | ||
('', 'datesort', None, 'try to sort changesets by date')] | ||||
Matt Mackall
|
r3938 | opts = {} | ||
args = fancyopts.fancyopts(sys.argv[1:], options, opts) | ||||
if opts['quiet']: | ||||
quiet = 1 | ||||
try: | ||||
command(*args, **opts) | ||||
except Abort, inst: | ||||
warn(inst) | ||||
except KeyboardInterrupt: | ||||
status("interrupted\n") | ||||