Mercurial > hg > mercurial-crew-with-dirclash
annotate contrib/convert-repo @ 1386:a1040345fdda
convert-repo: retrieve the commit hash from the tag object for tag import
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Wed, 05 Oct 2005 17:11:06 -0700 |
parents | adb3de56635b |
children | 0c7e8d345564 |
rev | line source |
---|---|
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 |
450 | 24 from mercurial import hg, ui, util |
316 | 25 |
26 class convert_git: | |
27 def __init__(self, path): | |
28 self.path = path | |
29 | |
30 def getheads(self): | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
31 return [file(self.path + "/HEAD").read()[:-1]] |
316 | 32 |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
33 def catfile(self, rev, type): |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
34 if rev == "0" * 40: raise IOError() |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
35 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
|
36 return fh.read() |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
37 |
316 | 38 def getfile(self, name, rev): |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
39 return self.catfile(rev, "blob") |
316 | 40 |
41 def getchanges(self, version): | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
42 fh = os.popen("GIT_DIR=%s git-diff-tree --root -m -r %s" % (self.path, version)) |
316 | 43 changes = [] |
44 for l in fh: | |
45 if "\t" not in l: continue | |
46 m, f = l[:-1].split("\t") | |
47 m = m.split() | |
48 h = m[3] | |
49 p = (m[1] == "100755") | |
50 changes.append((f, h, p)) | |
51 return changes | |
52 | |
53 def getcommit(self, version): | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
54 c = self.catfile(version, "commit") # read the commit hash |
316 | 55 end = c.find("\n\n") |
56 message = c[end+2:] | |
57 l = c[:end].splitlines() | |
58 manifest = l[0].split()[1] | |
59 parents = [] | |
60 for e in l[1:]: | |
61 n,v = e.split(" ", 1) | |
62 if n == "author": | |
63 p = v.split() | |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
64 tm, tz = p[-2:] |
316 | 65 author = " ".join(p[:-2]) |
66 if author[0] == "<": author = author[1:-1] | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
67 if n == "committer": |
431 | 68 p = v.split() |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
69 tm, tz = p[-2:] |
431 | 70 committer = " ".join(p[:-2]) |
71 if committer[0] == "<": committer = committer[1:-1] | |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
72 message += "\ncommitter: %s\n" % v |
316 | 73 if n == "parent": parents.append(v) |
1385
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
74 |
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
75 tzs, tzh, tzm = tz[-5:-4] + "1", tz[-4:-2], tz[-2:] |
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
76 tz = int(tzs) * (int(tzh) * 3600 + int(tzm)) |
adb3de56635b
convert-repo: Fix timezone handling
Matt Mackall <mpm@selenic.com>
parents:
1335
diff
changeset
|
77 date = tm + " " + str(tz) |
316 | 78 return (parents, author, date, message) |
79 | |
694 | 80 def gettags(self): |
81 tags = {} | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
82 for f in os.listdir(self.path + "/refs/tags"): |
694 | 83 try: |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
84 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
|
85 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
|
86 h = c.splitlines()[0].split()[1] |
1237 | 87 tags[f] = h |
694 | 88 except: |
89 pass | |
90 return tags | |
91 | |
316 | 92 class convert_mercurial: |
93 def __init__(self, path): | |
94 self.path = path | |
95 u = ui.ui() | |
96 self.repo = hg.repository(u, path) | |
97 | |
98 def getheads(self): | |
99 h = self.repo.changelog.heads() | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
100 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
|
101 |
316 | 102 def putfile(self, f, e, data): |
103 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
|
104 if self.repo.dirstate.state(f) == '?': |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
105 self.repo.dirstate.update([f], "a") |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
106 |
450 | 107 util.set_exec(self.repo.wjoin(f), e) |
316 | 108 |
109 def delfile(self, f): | |
110 try: | |
111 os.unlink(self.repo.wjoin(f)) | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
112 #self.repo.remove([f]) |
316 | 113 except: |
114 pass | |
115 | |
116 def putcommit(self, files, parents, author, dest, text): | |
431 | 117 seen = {} |
118 pl = [] | |
119 for p in parents: | |
120 if p not in seen: | |
121 pl.append(p) | |
122 seen[p] = 1 | |
123 parents = pl | |
316 | 124 |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
125 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
|
126 if len(parents) < 2: parents.append("0" * 40) |
431 | 127 p2 = parents.pop(0) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
128 |
431 | 129 while parents: |
130 p1 = p2 | |
131 p2 = parents.pop(0) | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
132 self.repo.rawcommit(files, text, author, dest, |
431 | 133 hg.bin(p1), hg.bin(p2)) |
134 text = "(octopus merge fixup)\n" | |
135 | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
136 return hg.hex(self.repo.changelog.tip()) |
316 | 137 |
694 | 138 def puttags(self, tags): |
139 try: | |
140 old = self.repo.wfile(".hgtags").read() | |
141 oldlines = old.splitlines(1) | |
142 oldlines.sort() | |
143 except: | |
144 oldlines = [] | |
145 | |
146 k = tags.keys() | |
147 k.sort() | |
148 newlines = [] | |
149 for tag in k: | |
150 newlines.append("%s %s\n" % (tags[tag], tag)) | |
151 | |
152 newlines.sort() | |
153 | |
154 if newlines != oldlines: | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
155 #print "updating tags" |
694 | 156 f = self.repo.wfile(".hgtags", "w") |
157 f.write("".join(newlines)) | |
158 f.close() | |
159 if not oldlines: self.repo.add([".hgtags"]) | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
160 date = "%s 0" % int(time.mktime(time.gmtime())) |
694 | 161 self.repo.rawcommit([".hgtags"], "update tags", "convert-repo", |
162 date, self.repo.changelog.tip(), hg.nullid) | |
163 | |
316 | 164 class convert: |
165 def __init__(self, source, dest, mapfile): | |
166 self.source = source | |
167 self.dest = dest | |
168 self.mapfile = mapfile | |
169 self.commitcache = {} | |
170 | |
171 self.map = {} | |
172 for l in file(self.mapfile): | |
173 sv, dv = l[:-1].split() | |
174 self.map[sv] = dv | |
175 | |
176 def walktree(self, heads): | |
177 visit = heads | |
178 known = {} | |
179 parents = {} | |
180 while visit: | |
181 n = visit.pop(0) | |
182 if n in known or n in self.map: continue | |
183 known[n] = 1 | |
184 self.commitcache[n] = self.source.getcommit(n) | |
185 cp = self.commitcache[n][0] | |
186 for p in cp: | |
187 parents.setdefault(n, []).append(p) | |
188 visit.append(p) | |
189 | |
190 return parents | |
191 | |
192 def toposort(self, parents): | |
193 visit = parents.keys() | |
194 seen = {} | |
195 children = {} | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
196 |
316 | 197 while visit: |
198 n = visit.pop(0) | |
199 if n in seen: continue | |
200 seen[n] = 1 | |
201 pc = 0 | |
202 if n in parents: | |
203 for p in parents[n]: | |
204 if p not in self.map: pc += 1 | |
205 visit.append(p) | |
206 children.setdefault(p, []).append(n) | |
207 if not pc: root = n | |
208 | |
209 s = [] | |
210 removed = {} | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
211 visit = children.keys() |
316 | 212 while visit: |
213 n = visit.pop(0) | |
214 if n in removed: continue | |
215 dep = 0 | |
216 if n in parents: | |
217 for p in parents[n]: | |
218 if p in self.map: continue | |
219 if p not in removed: | |
220 # we're still dependent | |
221 visit.append(n) | |
222 dep = 1 | |
223 break | |
224 | |
225 if not dep: | |
226 # all n's parents are in the list | |
227 removed[n] = 1 | |
228 s.append(n) | |
229 if n in children: | |
230 for c in children[n]: | |
231 visit.insert(0, c) | |
232 | |
233 return s | |
234 | |
235 def copy(self, rev): | |
236 p, a, d, t = self.commitcache[rev] | |
237 files = self.source.getchanges(rev) | |
238 | |
239 for f,v,e in files: | |
240 try: | |
241 data = self.source.getfile(f, v) | |
242 except IOError, inst: | |
243 self.dest.delfile(f) | |
244 else: | |
245 self.dest.putfile(f, e, data) | |
246 | |
247 r = [self.map[v] for v in p] | |
248 f = [f for f,v,e in files] | |
249 self.map[rev] = self.dest.putcommit(f, r, a, d, t) | |
250 file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) | |
251 | |
252 def convert(self): | |
253 heads = self.source.getheads() | |
254 parents = self.walktree(heads) | |
255 t = self.toposort(parents) | |
256 num = len(t) | |
257 | |
258 for c in t: | |
259 num -= 1 | |
260 if c in self.map: continue | |
261 desc = self.commitcache[c][3].splitlines()[0] | |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
262 #print num, desc |
316 | 263 self.copy(c) |
264 | |
694 | 265 tags = self.source.gettags() |
266 ctags = {} | |
267 for k in tags: | |
268 v = tags[k] | |
269 if v in self.map: | |
270 ctags[k] = self.map[v] | |
271 | |
272 self.dest.puttags(ctags) | |
273 | |
316 | 274 gitpath, hgpath, mapfile = sys.argv[1:] |
1335
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
275 if os.path.isdir(gitpath + "/.git"): |
bea6356b8bca
git -> hg conversion script
Florian La Roche <laroche@redhat.com>
parents:
1237
diff
changeset
|
276 gitpath += "/.git" |
316 | 277 |
278 c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile) | |
279 c.convert() |