convert: refactor sink initialisation, to remove hardcoding of hg
We also introduce options to explicitly set the source and destination
repository types.
Improve testing of corner cases a little.
--- a/hgext/convert/__init__.py Wed Oct 10 15:30:00 2007 -0700
+++ b/hgext/convert/__init__.py Wed Oct 10 15:42:00 2007 -0700
@@ -19,27 +19,32 @@
commands.norepo += " convert debugsvnlog"
-sink_converters = [mercurial_sink]
-source_converters = [convert_cvs, convert_git, svn_source,
- mercurial_source, darcs_source]
-def convertsource(ui, path, **opts):
- for c in source_converters:
+source_converters = [
+ ('cvs', convert_cvs),
+ ('git', convert_git),
+ ('svn', svn_source),
+ ('hg', mercurial_source),
+ ('darcs', darcs_source),
+ ]
+
+sink_converters = [
+ ('hg', mercurial_sink),
+ ]
+
+def convertsource(ui, path, type, rev):
+ for name, source in source_converters:
try:
- return c.getcommit and c(ui, path, **opts)
- except AttributeError:
- pass
+ if not type or name == type:
+ return source(ui, path, rev)
except NoRepo, inst:
ui.note(_("convert: %s\n") % inst)
raise util.Abort('%s: unknown repository type' % path)
-def convertsink(ui, path):
- if not os.path.isdir(path):
- raise util.Abort("%s: not a directory" % path)
- for c in sink_converters:
+def convertsink(ui, path, type):
+ for name, sink in sink_converters:
try:
- return c.putcommit and c(ui, path)
- except AttributeError:
- pass
+ if not type or name == type:
+ return sink(ui, path)
except NoRepo, inst:
ui.note(_("convert: %s\n") % inst)
raise util.Abort('%s: unknown repository type' % path)
@@ -350,37 +355,14 @@
dest = hg.defaultdest(src) + "-hg"
ui.status("assuming destination %s\n" % dest)
- # Try to be smart and initalize things when required
- created = False
- if os.path.isdir(dest):
- if len(os.listdir(dest)) > 0:
- try:
- hg.repository(ui, dest)
- ui.status("destination %s is a Mercurial repository\n" % dest)
- except hg.RepoError:
- raise util.Abort(
- "destination directory %s is not empty.\n"
- "Please specify an empty directory to be initialized\n"
- "or an already initialized mercurial repository"
- % dest)
- else:
- ui.status("initializing destination %s repository\n" % dest)
- hg.repository(ui, dest, create=True)
- created = True
- elif os.path.exists(dest):
- raise util.Abort("destination %s exists and is not a directory" % dest)
- else:
- ui.status("initializing destination %s repository\n" % dest)
- hg.repository(ui, dest, create=True)
- created = True
-
- destc = convertsink(ui, dest)
+ destc = convertsink(ui, dest, opts.get('dest_type'))
try:
- srcc = convertsource(ui, src, rev=opts.get('rev'))
+ srcc = convertsource(ui, src, opts.get('source_type'),
+ opts.get('rev'))
except Exception:
- if created:
- shutil.rmtree(dest, True)
+ for path in destc.created:
+ shutil.rmtree(path, True)
raise
fmap = opts.get('filemap')
@@ -402,8 +384,10 @@
"convert":
(convert,
[('A', 'authors', '', 'username mapping filename'),
+ ('d', 'dest-type', '', 'destination repository type'),
('', 'filemap', '', 'remap file names using contents of file'),
('r', 'rev', '', 'import up to target revision REV'),
+ ('s', 'source-type', '', 'source repository type'),
('', 'datesort', None, 'try to sort changesets by date')],
'hg convert [OPTION]... SOURCE [DEST [MAPFILE]]'),
"debugsvnlog":
--- a/hgext/convert/common.py Wed Oct 10 15:30:00 2007 -0700
+++ b/hgext/convert/common.py Wed Oct 10 15:42:00 2007 -0700
@@ -116,9 +116,13 @@
def __init__(self, ui, path):
"""Initialize conversion sink (or raise NoRepo("message")
- exception if path is not a valid repository)"""
+ exception if path is not a valid repository)
+
+ created is a list of paths to remove if a fatal error occurs
+ later"""
+ self.ui = ui
self.path = path
- self.ui = ui
+ self.created = []
def getheads(self):
"""Return a list of this repository's heads"""
--- a/hgext/convert/hg.py Wed Oct 10 15:30:00 2007 -0700
+++ b/hgext/convert/hg.py Wed Oct 10 15:42:00 2007 -0700
@@ -21,10 +21,22 @@
self.clonebranches = ui.configbool('convert', 'hg.clonebranches', False)
self.tagsbranch = ui.config('convert', 'hg.tagsbranch', 'default')
self.lastbranch = None
- try:
- self.repo = hg.repository(self.ui, path)
- except:
- raise NoRepo("could not open hg repo %s as sink" % path)
+ if os.path.isdir(path) and len(os.listdir(path)) > 0:
+ try:
+ self.repo = hg.repository(self.ui, path)
+ ui.status(_('destination %s is a Mercurial repository\n') %
+ path)
+ except hg.RepoError, err:
+ ui.print_exc()
+ raise NoRepo(err.args[0])
+ else:
+ try:
+ ui.status(_('initializing destination %s repository\n') % path)
+ self.repo = hg.repository(self.ui, path, create=True)
+ self.created.append(path)
+ except hg.RepoError, err:
+ ui.print_exc()
+ raise NoRepo("could not create hg repo %s as sink" % path)
self.lock = None
self.wlock = None
self.filemapmode = False
--- a/tests/test-convert Wed Oct 10 15:30:00 2007 -0700
+++ b/tests/test-convert Wed Oct 10 15:42:00 2007 -0700
@@ -3,6 +3,8 @@
echo "[extensions]" >> $HGRCPATH
echo "convert=" >> $HGRCPATH
+hg help convert
+
hg init a
cd a
echo a > a
@@ -19,3 +21,17 @@
cd ..
hg convert a 2>&1 | grep -v 'subversion python bindings could not be loaded'
hg --cwd a-hg pull ../a
+
+touch bogusfile
+echo % should fail
+hg convert a bogusfile
+
+mkdir bogusdir
+chmod 000 bogusdir
+
+echo % should fail
+hg convert a bogusdir
+
+echo % should succeed
+chmod 700 bogusdir
+hg convert a bogusdir
--- a/tests/test-convert.out Wed Oct 10 15:30:00 2007 -0700
+++ b/tests/test-convert.out Wed Oct 10 15:42:00 2007 -0700
@@ -1,3 +1,67 @@
+hg convert [OPTION]... SOURCE [DEST [MAPFILE]]
+
+Convert a foreign SCM repository to a Mercurial one.
+
+ Accepted source formats:
+ - CVS
+ - Darcs
+ - git
+ - Subversion
+
+ Accepted destination formats:
+ - Mercurial
+
+ If no revision is given, all revisions will be converted. Otherwise,
+ convert will only import up to the named revision (given in a format
+ understood by the source).
+
+ If no destination directory name is specified, it defaults to the
+ basename of the source with '-hg' appended. If the destination
+ repository doesn't exist, it will be created.
+
+ If <revmapfile> isn't given, it will be put in a default location
+ (<dest>/.hg/shamap by default). The <revmapfile> is a simple text
+ file that maps each source commit ID to the destination ID for
+ that revision, like so:
+ <source ID> <destination ID>
+
+ If the file doesn't exist, it's automatically created. It's updated
+ on each commit copied, so convert-repo can be interrupted and can
+ be run repeatedly to copy new commits.
+
+ The [username mapping] file is a simple text file that maps each source
+ commit author to a destination commit author. It is handy for source SCMs
+ that use unix logins to identify authors (eg: CVS). One line per author
+ mapping and the line format is:
+ srcauthor=whatever string you want
+
+ The filemap is a file that allows filtering and remapping of files
+ and directories. Comment lines start with '#'. Each line can
+ contain one of the following directives:
+
+ include path/to/file
+
+ exclude path/to/file
+
+ rename from/file to/file
+
+ The 'include' directive causes a file, or all files under a
+ directory, to be included in the destination repository. The
+ 'exclude' directive causes files or directories to be omitted.
+ The 'rename' directive renames a file or directory. To rename
+ from a subdirectory into the root of the repository, use '.' as
+ the path to rename to.
+
+options:
+
+ -A --authors username mapping filename
+ -d --dest-type destination repository type
+ --filemap remap file names using contents of file
+ -r --rev import up to target revision REV
+ -s --source-type source repository type
+ --datesort try to sort changesets by date
+
+use "hg -v help convert" to show global options
adding a
assuming destination a-hg
initializing destination a-hg repository
@@ -12,3 +76,18 @@
pulling from ../a
searching for changes
no changes found
+% should fail
+initializing destination bogusfile repository
+abort: cannot create new bundle repository
+% should fail
+abort: Permission denied: bogusdir
+% should succeed
+initializing destination bogusdir repository
+scanning source...
+sorting...
+converting...
+4 a
+3 b
+2 c
+1 d
+0 e