author | Matt Mackall <mpm@selenic.com> |
Sun, 10 Dec 2006 02:11:02 -0600 | |
changeset 3854 | 4f6db0233606 |
parent 3821 | 158fce02dc40 |
child 3910 | 4bc5a2405b12 |
permissions | -rwxr-xr-x |
316 | 1 |
#!/usr/bin/env python |
2 |
# |
|
3 |
# This is a generalized framework for converting between SCM |
|
4 |
# repository formats. |
|
5 |
# |
|
6 |
# In its current form, it's hardcoded to convert incrementally between |
|
7 |
# git and Mercurial. |
|
8 |
# |
|
9 |
# To use, you must first import the first git version into Mercurial, |
|
10 |
# and establish a mapping between the git commit hash and the hash in |
|
11 |
# Mercurial for that version. This mapping is kept in a simple text |
|
12 |
# file with lines like so: |
|
13 |
# |
|
14 |
# <git hash> <mercurial hash> |
|
15 |
# |
|
16 |
# To convert the rest of the repo, run: |
|
17 |
# |
|
18 |
# convert-repo <git-dir> <hg-dir> <mapfile> |
|
19 |
# |
|
20 |
# This updates the mapfile on each commit copied, so it can be |
|
21 |
# interrupted and can be run repeatedly to copy new commits. |
|
22 |
||
694 | 23 |
import sys, os, zlib, sha, time |
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
24 |
|
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
25 |
os.environ["HGENCODING"] = "utf-8" |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
26 |
|
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
27 |
from mercurial import hg, ui, util |
316 | 28 |
|
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
29 |
def recode(s): |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
30 |
try: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
31 |
return s.decode("utf-8").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
32 |
except: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
33 |
try: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
34 |
return s.decode("latin-1").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
35 |
except: |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
36 |
return s.decode("utf-8", "replace").encode("utf-8") |
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
37 |
|
316 | 38 |
class convert_git: |
39 |
def __init__(self, path): |
|
40 |
self.path = path |
|
41 |
||
42 |
def getheads(self): |
|
2657
e6a7a6a33a62
make convert-repo deal with git symbolic refs.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2093
diff
changeset
|
43 |
fh = os.popen("GIT_DIR=%s git-rev-parse --verify HEAD" % self.path) |
e6a7a6a33a62
make convert-repo deal with git symbolic refs.
Alexis S. L. Carvalho <alexis@cecm.usp.br>
parents:
2093
diff
changeset
|
44 |
return [fh.read()[:-1]] |
316 | 45 |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
46 |
def catfile(self, rev, type): |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
47 |
if rev == "0" * 40: raise IOError() |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
48 |
fh = os.popen("GIT_DIR=%s git-cat-file %s %s 2>/dev/null" % (self.path, type, rev)) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
49 |
return fh.read() |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
50 |
|
316 | 51 |
def getfile(self, name, rev): |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
52 |
return self.catfile(rev, "blob") |
316 | 53 |
|
54 |
def getchanges(self, version): |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
55 |
fh = os.popen("GIT_DIR=%s git-diff-tree --root -m -r %s" % (self.path, version)) |
316 | 56 |
changes = [] |
57 |
for l in fh: |
|
58 |
if "\t" not in l: continue |
|
59 |
m, f = l[:-1].split("\t") |
|
60 |
m = m.split() |
|
61 |
h = m[3] |
|
62 |
p = (m[1] == "100755") |
|
63 |
changes.append((f, h, p)) |
|
64 |
return changes |
|
65 |
||
66 |
def getcommit(self, version): |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
67 |
c = self.catfile(version, "commit") # read the commit hash |
316 | 68 |
end = c.find("\n\n") |
69 |
message = c[end+2:] |
|
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
70 |
message = recode(message) |
316 | 71 |
l = c[:end].splitlines() |
72 |
manifest = l[0].split()[1] |
|
73 |
parents = [] |
|
74 |
for e in l[1:]: |
|
75 |
n,v = e.split(" ", 1) |
|
76 |
if n == "author": |
|
77 |
p = v.split() |
|
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
78 |
tm, tz = p[-2:] |
316 | 79 |
author = " ".join(p[:-2]) |
80 |
if author[0] == "<": author = author[1:-1] |
|
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
81 |
author = recode(author) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
82 |
if n == "committer": |
431 | 83 |
p = v.split() |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
84 |
tm, tz = p[-2:] |
431 | 85 |
committer = " ".join(p[:-2]) |
86 |
if committer[0] == "<": committer = committer[1:-1] |
|
3821
158fce02dc40
Teach convert-repo to deal with mixed charsets in git
Matt Mackall <mpm@selenic.com>
parents:
2657
diff
changeset
|
87 |
committer = recode(committer) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
88 |
message += "\ncommitter: %s\n" % v |
316 | 89 |
if n == "parent": parents.append(v) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
90 |
|
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
91 |
tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] |
2093
5cc414722587
convert-repo: fix reversed time zone offset
Vadim Gelfer <vadim.gelfer@gmail.com>
parents:
1715
diff
changeset
|
92 |
tz = -int(tzs) * (int(tzh) * 3600 + int(tzm)) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
93 |
date = tm + " " + str(tz) |
316 | 94 |
return (parents, author, date, message) |
95 |
||
694 | 96 |
def gettags(self): |
97 |
tags = {} |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
98 |
for f in os.listdir(self.path + "/refs/tags"): |
694 | 99 |
try: |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
100 |
h = file(self.path + "/refs/tags/" + f).read().strip() |
1386
a1040345fdda
convert-repo: retrieve the commit hash from the tag object for tag import
Matt Mackall <mpm@selenic.com>
parents:
1385
diff
changeset
|
101 |
c = self.catfile(h, "tag") # read the commit hash |
a1040345fdda
convert-repo: retrieve the commit hash from the tag object for tag import
Matt Mackall <mpm@selenic.com>
parents:
1385
diff
changeset
|
102 |
h = c.splitlines()[0].split()[1] |
1237 | 103 |
tags[f] = h |
694 | 104 |
except: |
105 |
pass |
|
106 |
return tags |
|
107 |
||
316 | 108 |
class convert_mercurial: |
109 |
def __init__(self, path): |
|
110 |
self.path = path |
|
111 |
u = ui.ui() |
|
112 |
self.repo = hg.repository(u, path) |
|
113 |
||
114 |
def getheads(self): |
|
115 |
h = self.repo.changelog.heads() |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
116 |
return [ hg.hex(x) for x in h ] |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
117 |
|
316 | 118 |
def putfile(self, f, e, data): |
119 |
self.repo.wfile(f, "w").write(data) |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
120 |
if self.repo.dirstate.state(f) == '?': |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
121 |
self.repo.dirstate.update([f], "a") |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
122 |
|
450 | 123 |
util.set_exec(self.repo.wjoin(f), e) |
316 | 124 |
|
125 |
def delfile(self, f): |
|
126 |
try: |
|
127 |
os.unlink(self.repo.wjoin(f)) |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
128 |
#self.repo.remove([f]) |
316 | 129 |
except: |
130 |
pass |
|
131 |
||
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
132 |
def putcommit(self, files, parents, author, dest, text): |
431 | 133 |
seen = {} |
134 |
pl = [] |
|
135 |
for p in parents: |
|
136 |
if p not in seen: |
|
137 |
pl.append(p) |
|
138 |
seen[p] = 1 |
|
139 |
parents = pl |
|
316 | 140 |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
141 |
if len(parents) < 2: parents.append("0" * 40) |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
142 |
if len(parents) < 2: parents.append("0" * 40) |
431 | 143 |
p2 = parents.pop(0) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
144 |
|
431 | 145 |
while parents: |
146 |
p1 = p2 |
|
147 |
p2 = parents.pop(0) |
|
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
148 |
self.repo.rawcommit(files, text, author, dest, |
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
149 |
hg.bin(p1), hg.bin(p2)) |
431 | 150 |
text = "(octopus merge fixup)\n" |
1389
9b3ef6f3cef5
convert-repo: fix up octopus merge conversion
Matt Mackall <mpm@selenic.com>
parents:
1388
diff
changeset
|
151 |
p2 = hg.hex(self.repo.changelog.tip()) |
431 | 152 |
|
1389
9b3ef6f3cef5
convert-repo: fix up octopus merge conversion
Matt Mackall <mpm@selenic.com>
parents:
1388
diff
changeset
|
153 |
return p2 |
316 | 154 |
|
694 | 155 |
def puttags(self, tags): |
156 |
try: |
|
157 |
old = self.repo.wfile(".hgtags").read() |
|
158 |
oldlines = old.splitlines(1) |
|
159 |
oldlines.sort() |
|
160 |
except: |
|
161 |
oldlines = [] |
|
162 |
||
163 |
k = tags.keys() |
|
164 |
k.sort() |
|
165 |
newlines = [] |
|
166 |
for tag in k: |
|
167 |
newlines.append("%s %s\n" % (tags[tag], tag)) |
|
168 |
||
169 |
newlines.sort() |
|
170 |
||
171 |
if newlines != oldlines: |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
172 |
#print "updating tags" |
694 | 173 |
f = self.repo.wfile(".hgtags", "w") |
174 |
f.write("".join(newlines)) |
|
175 |
f.close() |
|
176 |
if not oldlines: self.repo.add([".hgtags"]) |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
177 |
date = "%s 0" % int(time.mktime(time.gmtime())) |
694 | 178 |
self.repo.rawcommit([".hgtags"], "update tags", "convert-repo", |
179 |
date, self.repo.changelog.tip(), hg.nullid) |
|
1387
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
180 |
return hg.hex(self.repo.changelog.tip()) |
694 | 181 |
|
316 | 182 |
class convert: |
183 |
def __init__(self, source, dest, mapfile): |
|
184 |
self.source = source |
|
185 |
self.dest = dest |
|
186 |
self.mapfile = mapfile |
|
187 |
self.commitcache = {} |
|
188 |
||
189 |
self.map = {} |
|
1655
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
190 |
try: |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
191 |
for l in file(self.mapfile): |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
192 |
sv, dv = l[:-1].split() |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
193 |
self.map[sv] = dv |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
194 |
except IOError: |
7bfd4724932a
convert-repo: automatically create empty map file
Matt Mackall <mpm@selenic.com>
parents:
1389
diff
changeset
|
195 |
pass |
316 | 196 |
|
197 |
def walktree(self, heads): |
|
198 |
visit = heads |
|
199 |
known = {} |
|
200 |
parents = {} |
|
201 |
while visit: |
|
202 |
n = visit.pop(0) |
|
203 |
if n in known or n in self.map: continue |
|
204 |
known[n] = 1 |
|
205 |
self.commitcache[n] = self.source.getcommit(n) |
|
206 |
cp = self.commitcache[n][0] |
|
207 |
for p in cp: |
|
208 |
parents.setdefault(n, []).append(p) |
|
209 |
visit.append(p) |
|
210 |
||
211 |
return parents |
|
212 |
||
213 |
def toposort(self, parents): |
|
214 |
visit = parents.keys() |
|
215 |
seen = {} |
|
216 |
children = {} |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
217 |
|
316 | 218 |
while visit: |
219 |
n = visit.pop(0) |
|
220 |
if n in seen: continue |
|
221 |
seen[n] = 1 |
|
222 |
pc = 0 |
|
223 |
if n in parents: |
|
224 |
for p in parents[n]: |
|
225 |
if p not in self.map: pc += 1 |
|
226 |
visit.append(p) |
|
227 |
children.setdefault(p, []).append(n) |
|
228 |
if not pc: root = n |
|
229 |
||
230 |
s = [] |
|
231 |
removed = {} |
|
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
232 |
visit = children.keys() |
316 | 233 |
while visit: |
234 |
n = visit.pop(0) |
|
235 |
if n in removed: continue |
|
236 |
dep = 0 |
|
237 |
if n in parents: |
|
238 |
for p in parents[n]: |
|
239 |
if p in self.map: continue |
|
240 |
if p not in removed: |
|
241 |
# we're still dependent |
|
242 |
visit.append(n) |
|
243 |
dep = 1 |
|
244 |
break |
|
245 |
||
246 |
if not dep: |
|
247 |
# all n's parents are in the list |
|
248 |
removed[n] = 1 |
|
249 |
s.append(n) |
|
250 |
if n in children: |
|
251 |
for c in children[n]: |
|
252 |
visit.insert(0, c) |
|
253 |
||
254 |
return s |
|
255 |
||
256 |
def copy(self, rev): |
|
257 |
p, a, d, t = self.commitcache[rev] |
|
258 |
files = self.source.getchanges(rev) |
|
259 |
||
260 |
for f,v,e in files: |
|
261 |
try: |
|
262 |
data = self.source.getfile(f, v) |
|
263 |
except IOError, inst: |
|
264 |
self.dest.delfile(f) |
|
265 |
else: |
|
266 |
self.dest.putfile(f, e, data) |
|
267 |
||
268 |
r = [self.map[v] for v in p] |
|
269 |
f = [f for f,v,e in files] |
|
270 |
self.map[rev] = self.dest.putcommit(f, r, a, d, t) |
|
271 |
file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) |
|
272 |
||
273 |
def convert(self): |
|
274 |
heads = self.source.getheads() |
|
275 |
parents = self.walktree(heads) |
|
276 |
t = self.toposort(parents) |
|
1388
5eb2d3c54165
convert-repo: change duplicate elimination
Matt Mackall <mpm@selenic.com>
parents:
1387
diff
changeset
|
277 |
t = [n for n in t if n not in self.map] |
316 | 278 |
num = len(t) |
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
279 |
c = None |
316 | 280 |
|
281 |
for c in t: |
|
282 |
num -= 1 |
|
283 |
desc = self.commitcache[c][3].splitlines()[0] |
|
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
284 |
#print num, desc |
316 | 285 |
self.copy(c) |
286 |
||
694 | 287 |
tags = self.source.gettags() |
288 |
ctags = {} |
|
289 |
for k in tags: |
|
290 |
v = tags[k] |
|
291 |
if v in self.map: |
|
292 |
ctags[k] = self.map[v] |
|
293 |
||
1715
40346aa66b0f
Revert convert-repo changes
Matt Mackall <mpm@selenic.com>
parents:
1656
diff
changeset
|
294 |
if c and ctags: |
1387
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
295 |
nrev = self.dest.puttags(ctags) |
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
296 |
# write another hash correspondence to override the previous |
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
297 |
# one so we don't end up with extra tag heads |
0c7e8d345564
convert-repo: linearize the tag commit
Matt Mackall <mpm@selenic.com>
parents:
1386
diff
changeset
|
298 |
file(self.mapfile, "a").write("%s %s\n" % (c, nrev)) |
694 | 299 |
|
316 | 300 |
gitpath, hgpath, mapfile = sys.argv[1:] |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
301 |
if os.path.isdir(gitpath + "/.git"): |
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
302 |
gitpath += "/.git" |
316 | 303 |
|
304 |
c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile) |
|
305 |
c.convert() |