##// END OF EJS Templates
convert: Ignore empty lines in authormap file.
Marti Raudsepp -
r6184:9d13e712 default
parent child Browse files
Show More
@@ -1,349 +1,351 b''
1 1 # convcmd - convert extension commands definition
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from common import NoRepo, SKIPREV, converter_source, converter_sink, mapfile
9 9 from cvs import convert_cvs
10 10 from darcs import darcs_source
11 11 from git import convert_git
12 12 from hg import mercurial_source, mercurial_sink
13 13 from subversion import debugsvnlog, svn_source, svn_sink
14 14 from gnuarch import gnuarch_source
15 15 import filemap
16 16
17 17 import os, shutil
18 18 from mercurial import hg, util
19 19 from mercurial.i18n import _
20 20
21 21 orig_encoding = 'ascii'
22 22
23 23 def recode(s):
24 24 if isinstance(s, unicode):
25 25 return s.encode(orig_encoding, 'replace')
26 26 else:
27 27 return s.decode('utf-8').encode(orig_encoding, 'replace')
28 28
29 29 source_converters = [
30 30 ('cvs', convert_cvs),
31 31 ('git', convert_git),
32 32 ('svn', svn_source),
33 33 ('hg', mercurial_source),
34 34 ('darcs', darcs_source),
35 35 ('gnuarch', gnuarch_source),
36 36 ]
37 37
38 38 sink_converters = [
39 39 ('hg', mercurial_sink),
40 40 ('svn', svn_sink),
41 41 ]
42 42
43 43 def convertsource(ui, path, type, rev):
44 44 exceptions = []
45 45 for name, source in source_converters:
46 46 try:
47 47 if not type or name == type:
48 48 return source(ui, path, rev)
49 49 except NoRepo, inst:
50 50 exceptions.append(inst)
51 51 if not ui.quiet:
52 52 for inst in exceptions:
53 53 ui.write(_("%s\n") % inst)
54 54 raise util.Abort('%s: unknown repository type' % path)
55 55
56 56 def convertsink(ui, path, type):
57 57 for name, sink in sink_converters:
58 58 try:
59 59 if not type or name == type:
60 60 return sink(ui, path)
61 61 except NoRepo, inst:
62 62 ui.note(_("convert: %s\n") % inst)
63 63 raise util.Abort('%s: unknown repository type' % path)
64 64
65 65 class converter(object):
66 66 def __init__(self, ui, source, dest, revmapfile, opts):
67 67
68 68 self.source = source
69 69 self.dest = dest
70 70 self.ui = ui
71 71 self.opts = opts
72 72 self.commitcache = {}
73 73 self.authors = {}
74 74 self.authorfile = None
75 75
76 76 self.map = mapfile(ui, revmapfile)
77 77
78 78 # Read first the dst author map if any
79 79 authorfile = self.dest.authorfile()
80 80 if authorfile and os.path.exists(authorfile):
81 81 self.readauthormap(authorfile)
82 82 # Extend/Override with new author map if necessary
83 83 if opts.get('authors'):
84 84 self.readauthormap(opts.get('authors'))
85 85 self.authorfile = self.dest.authorfile()
86 86
87 87 self.splicemap = mapfile(ui, opts.get('splicemap'))
88 88
89 89 def walktree(self, heads):
90 90 '''Return a mapping that identifies the uncommitted parents of every
91 91 uncommitted changeset.'''
92 92 visit = heads
93 93 known = {}
94 94 parents = {}
95 95 while visit:
96 96 n = visit.pop(0)
97 97 if n in known or n in self.map: continue
98 98 known[n] = 1
99 99 commit = self.cachecommit(n)
100 100 parents[n] = []
101 101 for p in commit.parents:
102 102 parents[n].append(p)
103 103 visit.append(p)
104 104
105 105 return parents
106 106
107 107 def toposort(self, parents):
108 108 '''Return an ordering such that every uncommitted changeset is
109 109 preceeded by all its uncommitted ancestors.'''
110 110 visit = parents.keys()
111 111 seen = {}
112 112 children = {}
113 113 actives = []
114 114
115 115 while visit:
116 116 n = visit.pop(0)
117 117 if n in seen: continue
118 118 seen[n] = 1
119 119 # Ensure that nodes without parents are present in the 'children'
120 120 # mapping.
121 121 children.setdefault(n, [])
122 122 hasparent = False
123 123 for p in parents[n]:
124 124 if not p in self.map:
125 125 visit.append(p)
126 126 hasparent = True
127 127 children.setdefault(p, []).append(n)
128 128 if not hasparent:
129 129 actives.append(n)
130 130
131 131 del seen
132 132 del visit
133 133
134 134 if self.opts.get('datesort'):
135 135 dates = {}
136 136 def getdate(n):
137 137 if n not in dates:
138 138 dates[n] = util.parsedate(self.commitcache[n].date)
139 139 return dates[n]
140 140
141 141 def picknext(nodes):
142 142 return min([(getdate(n), n) for n in nodes])[1]
143 143 else:
144 144 prev = [None]
145 145 def picknext(nodes):
146 146 # Return the first eligible child of the previously converted
147 147 # revision, or any of them.
148 148 next = nodes[0]
149 149 for n in nodes:
150 150 if prev[0] in parents[n]:
151 151 next = n
152 152 break
153 153 prev[0] = next
154 154 return next
155 155
156 156 s = []
157 157 pendings = {}
158 158 while actives:
159 159 n = picknext(actives)
160 160 actives.remove(n)
161 161 s.append(n)
162 162
163 163 # Update dependents list
164 164 for c in children.get(n, []):
165 165 if c not in pendings:
166 166 pendings[c] = [p for p in parents[c] if p not in self.map]
167 167 try:
168 168 pendings[c].remove(n)
169 169 except ValueError:
170 170 raise util.Abort(_('cycle detected between %s and %s')
171 171 % (recode(c), recode(n)))
172 172 if not pendings[c]:
173 173 # Parents are converted, node is eligible
174 174 actives.insert(0, c)
175 175 pendings[c] = None
176 176
177 177 if len(s) != len(parents):
178 178 raise util.Abort(_("not all revisions were sorted"))
179 179
180 180 return s
181 181
182 182 def writeauthormap(self):
183 183 authorfile = self.authorfile
184 184 if authorfile:
185 185 self.ui.status('Writing author map file %s\n' % authorfile)
186 186 ofile = open(authorfile, 'w+')
187 187 for author in self.authors:
188 188 ofile.write("%s=%s\n" % (author, self.authors[author]))
189 189 ofile.close()
190 190
191 191 def readauthormap(self, authorfile):
192 192 afile = open(authorfile, 'r')
193 193 for line in afile:
194 if line.strip() == '':
195 continue
194 196 try:
195 197 srcauthor = line.split('=')[0].strip()
196 198 dstauthor = line.split('=')[1].strip()
197 199 if srcauthor in self.authors and dstauthor != self.authors[srcauthor]:
198 200 self.ui.status(
199 201 'Overriding mapping for author %s, was %s, will be %s\n'
200 202 % (srcauthor, self.authors[srcauthor], dstauthor))
201 203 else:
202 204 self.ui.debug('Mapping author %s to %s\n'
203 205 % (srcauthor, dstauthor))
204 206 self.authors[srcauthor] = dstauthor
205 207 except IndexError:
206 208 self.ui.warn(
207 209 'Ignoring bad line in author file map %s: %s\n'
208 210 % (authorfile, line))
209 211 afile.close()
210 212
211 213 def cachecommit(self, rev):
212 214 commit = self.source.getcommit(rev)
213 215 commit.author = self.authors.get(commit.author, commit.author)
214 216 self.commitcache[rev] = commit
215 217 return commit
216 218
217 219 def copy(self, rev):
218 220 commit = self.commitcache[rev]
219 221 do_copies = hasattr(self.dest, 'copyfile')
220 222 filenames = []
221 223
222 224 changes = self.source.getchanges(rev)
223 225 if isinstance(changes, basestring):
224 226 if changes == SKIPREV:
225 227 dest = SKIPREV
226 228 else:
227 229 dest = self.map[changes]
228 230 self.map[rev] = dest
229 231 return
230 232 files, copies = changes
231 233 pbranches = []
232 234 if commit.parents:
233 235 for prev in commit.parents:
234 236 if prev not in self.commitcache:
235 237 self.cachecommit(prev)
236 238 pbranches.append((self.map[prev],
237 239 self.commitcache[prev].branch))
238 240 self.dest.setbranch(commit.branch, pbranches)
239 241 for f, v in files:
240 242 filenames.append(f)
241 243 try:
242 244 data = self.source.getfile(f, v)
243 245 except IOError, inst:
244 246 self.dest.delfile(f)
245 247 else:
246 248 e = self.source.getmode(f, v)
247 249 self.dest.putfile(f, e, data)
248 250 if do_copies:
249 251 if f in copies:
250 252 copyf = copies[f]
251 253 # Merely marks that a copy happened.
252 254 self.dest.copyfile(copyf, f)
253 255
254 256 try:
255 257 parents = self.splicemap[rev].replace(',', ' ').split()
256 258 self.ui.status('spliced in %s as parents of %s\n' %
257 259 (parents, rev))
258 260 parents = [self.map.get(p, p) for p in parents]
259 261 except KeyError:
260 262 parents = [b[0] for b in pbranches]
261 263 newnode = self.dest.putcommit(filenames, parents, commit)
262 264 self.source.converted(rev, newnode)
263 265 self.map[rev] = newnode
264 266
265 267 def convert(self):
266 268
267 269 try:
268 270 self.source.before()
269 271 self.dest.before()
270 272 self.source.setrevmap(self.map)
271 273 self.ui.status("scanning source...\n")
272 274 heads = self.source.getheads()
273 275 parents = self.walktree(heads)
274 276 self.ui.status("sorting...\n")
275 277 t = self.toposort(parents)
276 278 num = len(t)
277 279 c = None
278 280
279 281 self.ui.status("converting...\n")
280 282 for c in t:
281 283 num -= 1
282 284 desc = self.commitcache[c].desc
283 285 if "\n" in desc:
284 286 desc = desc.splitlines()[0]
285 287 # convert log message to local encoding without using
286 288 # tolocal() because util._encoding conver() use it as
287 289 # 'utf-8'
288 290 self.ui.status("%d %s\n" % (num, recode(desc)))
289 291 self.ui.note(_("source: %s\n" % recode(c)))
290 292 self.copy(c)
291 293
292 294 tags = self.source.gettags()
293 295 ctags = {}
294 296 for k in tags:
295 297 v = tags[k]
296 298 if self.map.get(v, SKIPREV) != SKIPREV:
297 299 ctags[k] = self.map[v]
298 300
299 301 if c and ctags:
300 302 nrev = self.dest.puttags(ctags)
301 303 # write another hash correspondence to override the previous
302 304 # one so we don't end up with extra tag heads
303 305 if nrev:
304 306 self.map[c] = nrev
305 307
306 308 self.writeauthormap()
307 309 finally:
308 310 self.cleanup()
309 311
310 312 def cleanup(self):
311 313 try:
312 314 self.dest.after()
313 315 finally:
314 316 self.source.after()
315 317 self.map.close()
316 318
317 319 def convert(ui, src, dest=None, revmapfile=None, **opts):
318 320 global orig_encoding
319 321 orig_encoding = util._encoding
320 322 util._encoding = 'UTF-8'
321 323
322 324 if not dest:
323 325 dest = hg.defaultdest(src) + "-hg"
324 326 ui.status("assuming destination %s\n" % dest)
325 327
326 328 destc = convertsink(ui, dest, opts.get('dest_type'))
327 329
328 330 try:
329 331 srcc = convertsource(ui, src, opts.get('source_type'),
330 332 opts.get('rev'))
331 333 except Exception:
332 334 for path in destc.created:
333 335 shutil.rmtree(path, True)
334 336 raise
335 337
336 338 fmap = opts.get('filemap')
337 339 if fmap:
338 340 srcc = filemap.filemap_source(ui, srcc, fmap)
339 341 destc.setfilemapmode(True)
340 342
341 343 if not revmapfile:
342 344 try:
343 345 revmapfile = destc.revmapfile()
344 346 except:
345 347 revmapfile = os.path.join(destc, "map")
346 348
347 349 c = converter(ui, srcc, destc, revmapfile, opts)
348 350 c.convert()
349 351
General Comments 0
You need to be logged in to leave comments. Login now