view hgfastimport/__init__.py @ 66:ae32828c68d7

Merge in Jelmer Vernooij's switch to python-fastimport over pyfastimport. Better to use his maintained library than my dusty old forgotten one. Incidentally, this merge deliberately leaves out efc96910ddf2 and descendants: that change broke the tests.
author Greg Ward <greg@gerg.ca>
date Sun, 26 Jul 2015 14:32:32 -0400
parents 9275d497b7ea 76bd0ea8add3
children 6b716ecb1cf3
line wrap: on
line source

from mercurial import encoding
from mercurial.i18n import _
from hgext.convert import convcmd, hg

from hgimport import fastimport_source

def fastimport(ui, repo, *sources, **opts):
    """Convert a git fastimport dump into Mercurial changesets.

    Reads a series of SOURCE fastimport dumps and adds the resulting
    changes to the current Mercurial repository.
    """
    # Would be nice to just call hgext.convert.convcmd.convert() and let
    # it take care of things.  But syntax and semantics are just a
    # little mismatched:
    #   - fastimport takes multiple source paths (mainly because cvs2git
    #     produces 2 dump files)
    #   - fastimport's dest is implicitly the current repo
    #
    # So for the time being, I have copied bits of convert() over here.
    # Boo, hiss.

    # assume fastimport metadata (usernames, commit messages) are
    # encoded UTF-8
    convcmd.orig_encoding = encoding.encoding
    encoding.encoding = 'UTF-8'

    # sink is the current repo, src is the list of fastimport streams
    destc = hg.mercurial_sink(ui, repo.root)
    srcc = fastimport_source(ui, repo, sources)

    # XXX figuring out sortmode copied straight from hgext/convert/convcmd.py
    defaultsort = 'branchsort'          # for efficiency and consistency
    sortmodes = ('branchsort', 'datesort', 'sourcesort')
    sortmode = [m for m in sortmodes if opts.get(m)]
    if len(sortmode) > 1:
        raise util.Abort(_('more than one sort mode specified'))
    sortmode = sortmode and sortmode[0] or defaultsort
    
    # not implemented: filemap, revmapfile
    revmapfile = destc.revmapfile()
    c = convcmd.converter(ui, srcc, destc, revmapfile, opts)
    c.convert(sortmode)

# XXX sort options copied straight from hgext/convert/__init__.py
cmdtable = {
    "fastimport":
        (fastimport,
         [('', 'branchsort', None, _('try to sort changesets by branches')),
          ('', 'datesort', None, _('try to sort changesets by date')),
          ('', 'sourcesort', None, _('preserve source changesets order')),
         ],
         'hg fastimport SOURCE ...')
}