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