hgext/convert/__init__.py
changeset 5437 71e7c86adcb7
parent 5434 6fa5258be3d4
equal deleted inserted replaced
5436:b4ae8535f834 5437:71e7c86adcb7
    17 from mercurial import hg, ui, util, commands
    17 from mercurial import hg, ui, util, commands
    18 from mercurial.i18n import _
    18 from mercurial.i18n import _
    19 
    19 
    20 commands.norepo += " convert debugsvnlog"
    20 commands.norepo += " convert debugsvnlog"
    21 
    21 
    22 sink_converters = [mercurial_sink]
    22 source_converters = [
    23 source_converters = [convert_cvs, convert_git, svn_source,
    23     ('cvs', convert_cvs),
    24                      mercurial_source, darcs_source]
    24     ('git', convert_git),
    25 def convertsource(ui, path, **opts):
    25     ('svn', svn_source),
    26     for c in source_converters:
    26     ('hg', mercurial_source),
    27         try:
    27     ('darcs', darcs_source),
    28             return c.getcommit and c(ui, path, **opts)
    28     ]
    29         except AttributeError:
    29 
    30             pass
    30 sink_converters = [
       
    31     ('hg', mercurial_sink),
       
    32     ]
       
    33 
       
    34 def convertsource(ui, path, type, rev):
       
    35     for name, source in source_converters:
       
    36         try:
       
    37             if not type or name == type:
       
    38                 return source(ui, path, rev)
    31         except NoRepo, inst:
    39         except NoRepo, inst:
    32             ui.note(_("convert: %s\n") % inst)
    40             ui.note(_("convert: %s\n") % inst)
    33     raise util.Abort('%s: unknown repository type' % path)
    41     raise util.Abort('%s: unknown repository type' % path)
    34 
    42 
    35 def convertsink(ui, path):
    43 def convertsink(ui, path, type):
    36     if not os.path.isdir(path):
    44     for name, sink in sink_converters:
    37         raise util.Abort("%s: not a directory" % path)
    45         try:
    38     for c in sink_converters:
    46             if not type or name == type:
    39         try:
    47                 return sink(ui, path)
    40             return c.putcommit and c(ui, path)
       
    41         except AttributeError:
       
    42             pass
       
    43         except NoRepo, inst:
    48         except NoRepo, inst:
    44             ui.note(_("convert: %s\n") % inst)
    49             ui.note(_("convert: %s\n") % inst)
    45     raise util.Abort('%s: unknown repository type' % path)
    50     raise util.Abort('%s: unknown repository type' % path)
    46 
    51 
    47 class converter(object):
    52 class converter(object):
   348 
   353 
   349     if not dest:
   354     if not dest:
   350         dest = hg.defaultdest(src) + "-hg"
   355         dest = hg.defaultdest(src) + "-hg"
   351         ui.status("assuming destination %s\n" % dest)
   356         ui.status("assuming destination %s\n" % dest)
   352 
   357 
   353     # Try to be smart and initalize things when required
   358     destc = convertsink(ui, dest, opts.get('dest_type'))
   354     created = False
       
   355     if os.path.isdir(dest):
       
   356         if len(os.listdir(dest)) > 0:
       
   357             try:
       
   358                 hg.repository(ui, dest)
       
   359                 ui.status("destination %s is a Mercurial repository\n" % dest)
       
   360             except hg.RepoError:
       
   361                 raise util.Abort(
       
   362                     "destination directory %s is not empty.\n"
       
   363                     "Please specify an empty directory to be initialized\n"
       
   364                     "or an already initialized mercurial repository"
       
   365                     % dest)
       
   366         else:
       
   367             ui.status("initializing destination %s repository\n" % dest)
       
   368             hg.repository(ui, dest, create=True)
       
   369             created = True
       
   370     elif os.path.exists(dest):
       
   371         raise util.Abort("destination %s exists and is not a directory" % dest)
       
   372     else:
       
   373         ui.status("initializing destination %s repository\n" % dest)
       
   374         hg.repository(ui, dest, create=True)
       
   375         created = True
       
   376 
       
   377     destc = convertsink(ui, dest)
       
   378 
   359 
   379     try:
   360     try:
   380         srcc = convertsource(ui, src, rev=opts.get('rev'))
   361         srcc = convertsource(ui, src, opts.get('source_type'),
       
   362                              opts.get('rev'))
   381     except Exception:
   363     except Exception:
   382         if created:
   364         for path in destc.created:
   383             shutil.rmtree(dest, True)
   365             shutil.rmtree(path, True)
   384         raise
   366         raise
   385 
   367 
   386     fmap = opts.get('filemap')
   368     fmap = opts.get('filemap')
   387     if fmap:
   369     if fmap:
   388         srcc = filemap.filemap_source(ui, srcc, fmap)
   370         srcc = filemap.filemap_source(ui, srcc, fmap)
   400 
   382 
   401 cmdtable = {
   383 cmdtable = {
   402     "convert":
   384     "convert":
   403         (convert,
   385         (convert,
   404          [('A', 'authors', '', 'username mapping filename'),
   386          [('A', 'authors', '', 'username mapping filename'),
       
   387           ('d', 'dest-type', '', 'destination repository type'),
   405           ('', 'filemap', '', 'remap file names using contents of file'),
   388           ('', 'filemap', '', 'remap file names using contents of file'),
   406           ('r', 'rev', '', 'import up to target revision REV'),
   389           ('r', 'rev', '', 'import up to target revision REV'),
       
   390           ('s', 'source-type', '', 'source repository type'),
   407           ('', 'datesort', None, 'try to sort changesets by date')],
   391           ('', 'datesort', None, 'try to sort changesets by date')],
   408          'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'),
   392          'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'),
   409     "debugsvnlog":
   393     "debugsvnlog":
   410         (debugsvnlog,
   394         (debugsvnlog,
   411          [],
   395          [],