739534e162ff50355eaba731b136432e5718141b
[jelmer/subvertpy.git] / convert.py
1 #!/usr/bin/env python2.4
2 #
3 # Copyright (C) 2005-2006 by Jelmer Vernooij
4
5 # Early versions based on svn2bzr
6 # Copyright (C) 2005 by Canonical Ltd
7 # Written by Gustavo Niemeyer <gustavo@niemeyer.net>
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22 #
23 import os
24 import tempfile
25
26 from bzrlib.plugin import load_plugins
27 load_plugins()
28
29 from bzrlib.bzrdir import BzrDir
30 from bzrlib.branch import Branch
31 from bzrlib.errors import BzrError
32 import bzrlib.osutils as osutils
33 from bzrlib.repository import Repository
34 from bzrlib.trace import info
35
36 from format import SvnRemoteAccess, SvnFormat
37 from repository import SvnRepository
38 from transport import SvnRaTransport
39
40 def convert_repository(url, output_dir, scheme, create_shared_repo=True, working_trees=False):
41     tmp_repos = None
42
43     if os.path.isfile(url):
44         tmp_repos = tempfile.mkdtemp(prefix='bzr-svn-dump-')
45         import svn
46         from svn.core import SubversionException
47         from cStringIO import StringIO
48         repos = svn.repos.svn_repos_create(tmp_repos, '', '', None, None)
49         try:
50             svn.repos.load_fs2(repos, open(url), StringIO(), svn.repos.load_uuid_default, '', 0, 0, None)
51         except SubversionException, (svn.core.SVN_ERR_STREAM_MALFORMED_DATA, _):            
52             raise BzrError("%s is not a dump file" % url)
53         
54         url = "svn+file://%s" % tmp_repos
55
56     if create_shared_repo:
57         target_repos = BzrDir.create_repository(output_dir, shared=True)
58         target_repos.set_make_working_trees(working_trees)
59
60     try:
61         source_repos = SvnRemoteAccess(SvnRaTransport(url), SvnFormat(), 
62                                        scheme).open_repository()
63
64         branches = source_repos._log.find_branches(source_repos._latest_revnum)
65         existing_branches = filter(lambda (bp, revnum, exists): exists, 
66                                    branches)
67         info('Importing branches: \n%s' % "".join(map(lambda (bp,revnum,exists): "%s\n" % bp, existing_branches)))
68
69         for (branch, revnum, exists) in existing_branches:
70             source_branch = Branch.open("%s/%s" % (source_repos.base, branch))
71
72             target_dir = os.path.join(output_dir, branch)
73             os.makedirs(target_dir)
74             source_branch.bzrdir.sprout(target_dir, source_branch.last_revision())
75             
76             info('Converted %s:%d\n' % (branch, revnum))
77
78     finally:
79         if tmp_repos:
80             osutils.rmtree(tmp_repos)
81
82