comparison hgext/convert/__init__.py @ 5510:11d7908a3ea8

convert: abstract map files into a class
author Bryan O'Sullivan <bos@serpentine.com>
date Wed, 07 Nov 2007 17:06:02 -0800
parents 247af577fe29
children f0c58fd4b798
comparison
equal deleted inserted replaced
5508:2dd399d8a992 5510:11d7908a3ea8
3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 # 4 #
5 # This software may be used and distributed according to the terms 5 # This software may be used and distributed according to the terms
6 # of the GNU General Public License, incorporated herein by reference. 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 from common import NoRepo, SKIPREV, converter_source, converter_sink 8 from common import NoRepo, SKIPREV, converter_source, converter_sink, mapfile
9 from cvs import convert_cvs 9 from cvs import convert_cvs
10 from darcs import darcs_source 10 from darcs import darcs_source
11 from git import convert_git 11 from git import convert_git
12 from hg import mercurial_source, mercurial_sink 12 from hg import mercurial_source, mercurial_sink
13 from subversion import svn_source, debugsvnlog 13 from subversion import svn_source, debugsvnlog
55 self.source = source 55 self.source = source
56 self.dest = dest 56 self.dest = dest
57 self.ui = ui 57 self.ui = ui
58 self.opts = opts 58 self.opts = opts
59 self.commitcache = {} 59 self.commitcache = {}
60 self.revmapfile = revmapfile
61 self.revmapfilefd = None
62 self.authors = {} 60 self.authors = {}
63 self.authorfile = None 61 self.authorfile = None
64 62
65 self.maporder = [] 63 self.map = mapfile(ui, revmapfile)
66 self.map = {}
67 try:
68 origrevmapfile = open(self.revmapfile, 'r')
69 for l in origrevmapfile:
70 sv, dv = l[:-1].split()
71 if sv not in self.map:
72 self.maporder.append(sv)
73 self.map[sv] = dv
74 origrevmapfile.close()
75 except IOError:
76 pass
77 64
78 # Read first the dst author map if any 65 # Read first the dst author map if any
79 authorfile = self.dest.authorfile() 66 authorfile = self.dest.authorfile()
80 if authorfile and os.path.exists(authorfile): 67 if authorfile and os.path.exists(authorfile):
81 self.readauthormap(authorfile) 68 self.readauthormap(authorfile)
158 s = [(depth[n], self.commitcache[n].date, n) for n in s] 145 s = [(depth[n], self.commitcache[n].date, n) for n in s]
159 s.sort() 146 s.sort()
160 s = [e[2] for e in s] 147 s = [e[2] for e in s]
161 148
162 return s 149 return s
163
164 def mapentry(self, src, dst):
165 if self.revmapfilefd is None:
166 try:
167 self.revmapfilefd = open(self.revmapfile, "a")
168 except IOError, (errno, strerror):
169 raise util.Abort("Could not open map file %s: %s, %s\n" % (self.revmapfile, errno, strerror))
170 self.map[src] = dst
171 self.revmapfilefd.write("%s %s\n" % (src, dst))
172 self.revmapfilefd.flush()
173 150
174 def writeauthormap(self): 151 def writeauthormap(self):
175 authorfile = self.authorfile 152 authorfile = self.authorfile
176 if authorfile: 153 if authorfile:
177 self.ui.status('Writing author map file %s\n' % authorfile) 154 self.ui.status('Writing author map file %s\n' % authorfile)
215 if isinstance(changes, basestring): 192 if isinstance(changes, basestring):
216 if changes == SKIPREV: 193 if changes == SKIPREV:
217 dest = SKIPREV 194 dest = SKIPREV
218 else: 195 else:
219 dest = self.map[changes] 196 dest = self.map[changes]
220 self.mapentry(rev, dest) 197 self.map[rev] = dest
221 return 198 return
222 files, copies = changes 199 files, copies = changes
223 parents = [self.map[r] for r in commit.parents] 200 parents = [self.map[r] for r in commit.parents]
224 if commit.parents: 201 if commit.parents:
225 prev = commit.parents[0] 202 prev = commit.parents[0]
243 copyf = copies[f] 220 copyf = copies[f]
244 # Merely marks that a copy happened. 221 # Merely marks that a copy happened.
245 self.dest.copyfile(copyf, f) 222 self.dest.copyfile(copyf, f)
246 223
247 newnode = self.dest.putcommit(filenames, parents, commit) 224 newnode = self.dest.putcommit(filenames, parents, commit)
248 self.mapentry(rev, newnode) 225 self.map[rev] = newnode
249 226
250 def convert(self): 227 def convert(self):
251 try: 228 try:
252 self.source.before() 229 self.source.before()
253 self.dest.before() 230 self.dest.before()
254 self.source.setrevmap(self.map, self.maporder) 231 self.source.setrevmap(self.map)
255 self.ui.status("scanning source...\n") 232 self.ui.status("scanning source...\n")
256 heads = self.source.getheads() 233 heads = self.source.getheads()
257 parents = self.walktree(heads) 234 parents = self.walktree(heads)
258 self.ui.status("sorting...\n") 235 self.ui.status("sorting...\n")
259 t = self.toposort(parents) 236 t = self.toposort(parents)
279 if c and ctags: 256 if c and ctags:
280 nrev = self.dest.puttags(ctags) 257 nrev = self.dest.puttags(ctags)
281 # write another hash correspondence to override the previous 258 # write another hash correspondence to override the previous
282 # one so we don't end up with extra tag heads 259 # one so we don't end up with extra tag heads
283 if nrev: 260 if nrev:
284 self.mapentry(c, nrev) 261 self.map[c] = nrev
285 262
286 self.writeauthormap() 263 self.writeauthormap()
287 finally: 264 finally:
288 self.cleanup() 265 self.cleanup()
289 266
290 def cleanup(self): 267 def cleanup(self):
291 try: 268 try:
292 self.dest.after() 269 self.dest.after()
293 finally: 270 finally:
294 self.source.after() 271 self.source.after()
295 if self.revmapfilefd: 272 self.map.close()
296 self.revmapfilefd.close()
297 273
298 def convert(ui, src, dest=None, revmapfile=None, **opts): 274 def convert(ui, src, dest=None, revmapfile=None, **opts):
299 """Convert a foreign SCM repository to a Mercurial one. 275 """Convert a foreign SCM repository to a Mercurial one.
300 276
301 Accepted source formats: 277 Accepted source formats: