Import svn2bzr since it can share testing infrastructure
[jelmer/subvertpy.git] / format.py
1 # Copyright (C) 2006 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17 from bzrlib.bzrdir import BzrDirFormat, BzrDir
18 from bzrlib.errors import NotBranchError, NotLocalUrl
19 from bzrlib.lockable_files import TransportLock
20
21 from branch import SvnBranch
22 from repository import SvnRepository
23 from transport import SvnRaTransport
24
25 from svn.core import SubversionException
26 import svn.core
27
28 class SvnRemoteAccess(BzrDir):
29     def __init__(self, _transport, _format):
30         assert isinstance(_transport, SvnRaTransport)
31
32         self.root_transport = self.transport = _transport
33         self._format = _format
34
35         self.url = _transport.base
36         self.branch_path = _transport.path
37
38     def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
39         raise NotImplementedError(SvnRemoteAccess.clone)
40
41     def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
42         # FIXME: honor force_new_repo
43         result = BzrDirFormat.get_default_format().initialize(url)
44         repo = self.open_repository()
45         result_repo = repo.clone(result, revision_id, basis)
46         branch = self.open_branch()
47         branch.sprout(result, revision_id)
48         result.create_workingtree()
49         return result
50
51     def open_repository(self):
52         repos = SvnRepository(self, self.transport.root_url)
53         repos._format = self._format
54         return repos
55
56     # Subversion has all-in-one, so a repository is always present
57     find_repository = open_repository
58
59     # Working trees never exist on remote Subversion repositories
60     def open_workingtree(self):
61         raise NotLocalUrl(self.url)
62
63     def create_workingtree(self, revision_id=None):
64         # TODO
65         raise NotImplementedError(self.create_workingtree)
66
67     def open_branch(self, unsupported=True):
68         repos = self.open_repository()
69
70         try:
71             branch = SvnBranch(repos, self.branch_path)
72         except SubversionException, (msg, num):
73             if num == svn.core.SVN_ERR_RA_ILLEGAL_URL or \
74                num == svn.core.SVN_ERR_WC_NOT_DIRECTORY or \
75                num == svn.core.SVN_ERR_RA_NO_REPOS_UUID or \
76                num == svn.core.SVN_ERR_RA_SVN_REPOS_NOT_FOUND or \
77                num == svn.core.SVN_ERR_FS_NOT_FOUND or \
78                num == svn.core.SVN_ERR_RA_DAV_REQUEST_FAILED:
79                raise NotBranchError(path=self.url)
80             raise
81  
82         branch.bzrdir = self
83         return branch
84
85 class SvnFormat(BzrDirFormat):
86     _lock_class = TransportLock
87
88     @classmethod
89     def probe_transport(klass, transport):
90         format = klass()
91
92         if isinstance(transport, SvnRaTransport):
93             return format
94
95         raise NotBranchError(path=transport.base)
96
97     def _open(self, transport):
98         return SvnRemoteAccess(transport, self)
99
100     def get_format_string(self):
101         return 'Subversion Smart Server'
102
103     def get_format_description(self):
104         return 'Subversion Smart Server'
105
106     def initialize(self, url):
107         raise NotImplementedError(self.initialize)