Mercurial > hg > mercurial-crew-with-dirclash
annotate contrib/convert-repo @ 700:d01b93efecd6
Removed extra spaces for default parameters according to PEP8.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Removed extra spaces for default parameters according to PEP8.
manifest hash: 0f6bcc95adf406c8f6dafd92b7c7d3523b6dfe2c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFC1oNhW7P1GVgWeRoRAn76AJ4m8rFUeeUDRC0qVEmjjmPClmAFsQCdE3rN
Juv2dSoGteRm0DBXz7xnF4c=
=PMki
-----END PGP SIGNATURE-----
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Thu, 14 Jul 2005 16:23:13 +0100 |
parents | 695dd9a491da |
children | 51eb248d3348 574869103985 |
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 | |
23 import sys, os, zlib, sha | |
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) |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
38 fh = os.popen("git-cat-file %s %s" % (type, rev)) |
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 | |
84 class convert_mercurial: | |
85 def __init__(self, path): | |
86 self.path = path | |
87 u = ui.ui() | |
88 self.repo = hg.repository(u, path) | |
89 | |
90 def getheads(self): | |
91 h = self.repo.changelog.heads() | |
92 h = [ hg.hex(x) for x in h ] | |
93 return h | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
94 |
316 | 95 def putfile(self, f, e, data): |
96 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
|
97 if self.repo.dirstate.state(f) == '?': |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
98 self.repo.dirstate.update([f], "a") |
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
99 |
450 | 100 util.set_exec(self.repo.wjoin(f), e) |
316 | 101 |
102 def delfile(self, f): | |
103 try: | |
104 os.unlink(self.repo.wjoin(f)) | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
105 #self.repo.remove([f]) |
316 | 106 except: |
107 pass | |
108 | |
109 def putcommit(self, files, parents, author, dest, text): | |
431 | 110 seen = {} |
111 pl = [] | |
112 for p in parents: | |
113 if p not in seen: | |
114 pl.append(p) | |
115 seen[p] = 1 | |
116 parents = pl | |
316 | 117 |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
118 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
|
119 if len(parents) < 2: parents.append("0" * 40) |
431 | 120 p2 = parents.pop(0) |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
121 |
431 | 122 while parents: |
123 p1 = p2 | |
124 p2 = parents.pop(0) | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
125 self.repo.rawcommit(files, text, author, dest, |
431 | 126 hg.bin(p1), hg.bin(p2)) |
127 text = "(octopus merge fixup)\n" | |
128 | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
129 return hg.hex(self.repo.changelog.tip()) |
316 | 130 |
131 class convert: | |
132 def __init__(self, source, dest, mapfile): | |
133 self.source = source | |
134 self.dest = dest | |
135 self.mapfile = mapfile | |
136 self.commitcache = {} | |
137 | |
138 self.map = {} | |
139 for l in file(self.mapfile): | |
140 sv, dv = l[:-1].split() | |
141 self.map[sv] = dv | |
142 | |
143 def walktree(self, heads): | |
144 visit = heads | |
145 known = {} | |
146 parents = {} | |
147 while visit: | |
148 n = visit.pop(0) | |
149 if n in known or n in self.map: continue | |
150 known[n] = 1 | |
151 self.commitcache[n] = self.source.getcommit(n) | |
152 cp = self.commitcache[n][0] | |
153 for p in cp: | |
154 parents.setdefault(n, []).append(p) | |
155 visit.append(p) | |
156 | |
157 return parents | |
158 | |
159 def toposort(self, parents): | |
160 visit = parents.keys() | |
161 seen = {} | |
162 children = {} | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
163 |
316 | 164 while visit: |
165 n = visit.pop(0) | |
166 if n in seen: continue | |
167 seen[n] = 1 | |
168 pc = 0 | |
169 if n in parents: | |
170 for p in parents[n]: | |
171 if p not in self.map: pc += 1 | |
172 visit.append(p) | |
173 children.setdefault(p, []).append(n) | |
174 if not pc: root = n | |
175 | |
176 s = [] | |
177 removed = {} | |
692
695dd9a491da
convert-repo: deal with packed git and other fixes
mpm@selenic.com
parents:
450
diff
changeset
|
178 visit = children.keys() |
316 | 179 while visit: |
180 n = visit.pop(0) | |
181 if n in removed: continue | |
182 dep = 0 | |
183 if n in parents: | |
184 for p in parents[n]: | |
185 if p in self.map: continue | |
186 if p not in removed: | |
187 # we're still dependent | |
188 visit.append(n) | |
189 dep = 1 | |
190 break | |
191 | |
192 if not dep: | |
193 # all n's parents are in the list | |
194 removed[n] = 1 | |
195 s.append(n) | |
196 if n in children: | |
197 for c in children[n]: | |
198 visit.insert(0, c) | |
199 | |
200 return s | |
201 | |
202 def copy(self, rev): | |
203 p, a, d, t = self.commitcache[rev] | |
204 files = self.source.getchanges(rev) | |
205 | |
206 for f,v,e in files: | |
207 try: | |
208 data = self.source.getfile(f, v) | |
209 except IOError, inst: | |
210 self.dest.delfile(f) | |
211 else: | |
212 self.dest.putfile(f, e, data) | |
213 | |
214 r = [self.map[v] for v in p] | |
215 f = [f for f,v,e in files] | |
216 self.map[rev] = self.dest.putcommit(f, r, a, d, t) | |
217 file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) | |
218 | |
219 def convert(self): | |
220 heads = self.source.getheads() | |
221 parents = self.walktree(heads) | |
222 t = self.toposort(parents) | |
223 num = len(t) | |
224 | |
225 for c in t: | |
226 num -= 1 | |
227 if c in self.map: continue | |
228 desc = self.commitcache[c][3].splitlines()[0] | |
229 print num, desc | |
230 self.copy(c) | |
231 | |
232 gitpath, hgpath, mapfile = sys.argv[1:] | |
233 | |
234 c = convert(convert_git(gitpath), convert_mercurial(hgpath), mapfile) | |
235 c.convert() |