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