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 |
8 from common import NoRepo, converter_source, converter_sink |
9 from cvs import convert_cvs |
9 from cvs import convert_cvs |
10 from git import convert_git |
10 from git import convert_git |
11 from hg import convert_mercurial |
11 from hg import convert_mercurial |
12 |
12 |
13 import os, shutil |
13 import os, shutil |
15 |
15 |
16 commands.norepo += " convert" |
16 commands.norepo += " convert" |
17 |
17 |
18 converters = [convert_cvs, convert_git, convert_mercurial] |
18 converters = [convert_cvs, convert_git, convert_mercurial] |
19 |
19 |
20 def converter(ui, path, rev=None): |
20 def convertsource(ui, path, rev=None): |
|
21 for c in converters: |
|
22 try: |
|
23 converter = c(ui, path, rev=rev) |
|
24 if not isinstance(converter, converter_source): |
|
25 raise util.Abort('%s: cannot read from this repository type' % path) |
|
26 return converter |
|
27 except NoRepo: |
|
28 pass |
|
29 raise util.Abort('%s: unknown repository type' % path) |
|
30 |
|
31 def convertsink(ui, path): |
21 if not os.path.isdir(path): |
32 if not os.path.isdir(path): |
22 raise util.Abort("%s: not a directory" % path) |
33 raise util.Abort("%s: not a directory" % path) |
23 for c in converters: |
34 for c in converters: |
24 try: |
35 try: |
25 if rev: |
36 converter = c(ui, path) |
26 return c(ui, path, rev=rev) |
37 if not isinstance(converter, converter_sink): |
27 else: |
38 raise util.Abort('%s: cannot write to this repository type' % path) |
28 return c(ui, path) |
39 return converter |
29 except NoRepo: |
40 except NoRepo: |
30 pass |
41 pass |
31 raise util.Abort("%s: unknown repository type" % path) |
42 raise util.Abort('%s: unknown repository type' % path) |
32 |
43 |
33 class convert(object): |
44 class convert(object): |
34 def __init__(self, ui, source, dest, mapfile, opts): |
45 def __init__(self, ui, source, dest, mapfile, opts): |
35 |
46 |
36 self.source = source |
47 self.source = source |
300 else: |
311 else: |
301 ui.status("initializing destination %s repository\n" % dest) |
312 ui.status("initializing destination %s repository\n" % dest) |
302 hg.repository(ui, dest, create=True) |
313 hg.repository(ui, dest, create=True) |
303 created = True |
314 created = True |
304 |
315 |
305 destc = converter(ui, dest) |
316 destc = convertsink(ui, dest) |
306 if not hasattr(destc, "putcommit"): |
|
307 raise util.Abort("%s: can't write to this repo type" % src) |
|
308 |
317 |
309 try: |
318 try: |
310 srcc = converter(ui, src, rev=opts.get('rev')) |
319 srcc = convertsource(ui, src, rev=opts.get('rev')) |
311 if not hasattr(srcc, "getcommit"): |
|
312 raise util.Abort("%s: can't read from this repo type" % src) |
|
313 except Exception: |
320 except Exception: |
314 if created: |
321 if created: |
315 shutil.rmtree(dest, True) |
322 shutil.rmtree(dest, True) |
316 raise |
323 raise |
317 |
324 |