31 def __init__(self, ui, source, dest, mapfile, opts): |
31 def __init__(self, ui, source, dest, mapfile, opts): |
32 |
32 |
33 self.source = source |
33 self.source = source |
34 self.dest = dest |
34 self.dest = dest |
35 self.ui = ui |
35 self.ui = ui |
36 self.mapfile = mapfile |
|
37 self.opts = opts |
36 self.opts = opts |
38 self.commitcache = {} |
37 self.commitcache = {} |
|
38 self.mapfile = mapfile |
|
39 self.mapfilefd = None |
39 |
40 |
40 self.map = {} |
41 self.map = {} |
41 try: |
42 try: |
42 for l in file(self.mapfile): |
43 origmapfile = open(self.mapfile, 'r') |
|
44 for l in origmapfile: |
43 sv, dv = l[:-1].split() |
45 sv, dv = l[:-1].split() |
44 self.map[sv] = dv |
46 self.map[sv] = dv |
|
47 origmapfile.close() |
45 except IOError: |
48 except IOError: |
46 pass |
49 pass |
47 |
50 |
48 def walktree(self, heads): |
51 def walktree(self, heads): |
49 visit = heads |
52 visit = heads |
116 s.sort() |
119 s.sort() |
117 s = [e[2] for e in s] |
120 s = [e[2] for e in s] |
118 |
121 |
119 return s |
122 return s |
120 |
123 |
|
124 def mapentry(self, src, dst): |
|
125 if self.mapfilefd is None: |
|
126 try: |
|
127 self.mapfilefd = open(self.mapfile, "a") |
|
128 except IOError, (errno, strerror): |
|
129 raise util.Abort("Could not open map file %s: %s, %s\n" % (self.mapfile, errno, strerror)) |
|
130 self.map[src] = dst |
|
131 self.mapfilefd.write("%s %s\n" % (src, dst)) |
|
132 self.mapfilefd.flush() |
|
133 |
121 def copy(self, rev): |
134 def copy(self, rev): |
122 c = self.commitcache[rev] |
135 c = self.commitcache[rev] |
123 files = self.source.getchanges(rev) |
136 files = self.source.getchanges(rev) |
124 |
137 |
125 for f, v in files: |
138 for f, v in files: |
131 e = self.source.getmode(f, v) |
144 e = self.source.getmode(f, v) |
132 self.dest.putfile(f, e, data) |
145 self.dest.putfile(f, e, data) |
133 |
146 |
134 r = [self.map[v] for v in c.parents] |
147 r = [self.map[v] for v in c.parents] |
135 f = [f for f, v in files] |
148 f = [f for f, v in files] |
136 self.map[rev] = self.dest.putcommit(f, r, c) |
149 newnode = self.dest.putcommit(f, r, c) |
137 file(self.mapfile, "a").write("%s %s\n" % (rev, self.map[rev])) |
150 self.mapentry(rev, newnode) |
138 |
151 |
139 def convert(self): |
152 def convert(self): |
140 self.ui.status("scanning source...\n") |
153 try: |
141 heads = self.source.getheads() |
154 self.ui.status("scanning source...\n") |
142 parents = self.walktree(heads) |
155 heads = self.source.getheads() |
143 self.ui.status("sorting...\n") |
156 parents = self.walktree(heads) |
144 t = self.toposort(parents) |
157 self.ui.status("sorting...\n") |
145 num = len(t) |
158 t = self.toposort(parents) |
146 c = None |
159 num = len(t) |
147 |
160 c = None |
148 self.ui.status("converting...\n") |
161 |
149 for c in t: |
162 self.ui.status("converting...\n") |
150 num -= 1 |
163 for c in t: |
151 desc = self.commitcache[c].desc |
164 num -= 1 |
152 if "\n" in desc: |
165 desc = self.commitcache[c].desc |
153 desc = desc.splitlines()[0] |
166 if "\n" in desc: |
154 self.ui.status("%d %s\n" % (num, desc)) |
167 desc = desc.splitlines()[0] |
155 self.copy(c) |
168 self.ui.status("%d %s\n" % (num, desc)) |
156 |
169 self.copy(c) |
157 tags = self.source.gettags() |
170 |
158 ctags = {} |
171 tags = self.source.gettags() |
159 for k in tags: |
172 ctags = {} |
160 v = tags[k] |
173 for k in tags: |
161 if v in self.map: |
174 v = tags[k] |
162 ctags[k] = self.map[v] |
175 if v in self.map: |
163 |
176 ctags[k] = self.map[v] |
164 if c and ctags: |
177 |
165 nrev = self.dest.puttags(ctags) |
178 if c and ctags: |
166 # write another hash correspondence to override the previous |
179 nrev = self.dest.puttags(ctags) |
167 # one so we don't end up with extra tag heads |
180 # write another hash correspondence to override the previous |
168 if nrev: |
181 # one so we don't end up with extra tag heads |
169 file(self.mapfile, "a").write("%s %s\n" % (c, nrev)) |
182 if nrev: |
|
183 self.mapentry(c, nrev) |
|
184 finally: |
|
185 self.cleanup() |
|
186 |
|
187 def cleanup(self): |
|
188 if self.mapfilefd: |
|
189 self.mapfilefd.close() |
170 |
190 |
171 def _convert(ui, src, dest=None, mapfile=None, **opts): |
191 def _convert(ui, src, dest=None, mapfile=None, **opts): |
172 '''Convert a foreign SCM repository to a Mercurial one. |
192 '''Convert a foreign SCM repository to a Mercurial one. |
173 |
193 |
174 Accepted source formats: |
194 Accepted source formats: |