Merge 0.4.
[jelmer/subvertpy.git] / format.py
1 # Copyright (C) 2006-2008 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 3 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 """Subversion BzrDir formats."""
17
18 from bzrlib.bzrdir import BzrDirFormat, BzrDir, format_registry
19 from bzrlib.errors import UninitializableFormat
20 from bzrlib.lazy_import import lazy_import
21 from bzrlib.lockable_files import TransportLock
22
23 import os
24
25 lazy_import(globals(), """
26 from bzrlib.plugins.svn import errors, remote
27
28 from bzrlib import errors as bzr_errors
29 """)
30
31 def get_rich_root_format():
32     format = BzrDirFormat.get_default_format()
33     if format.repository_format.rich_root_data:
34         return format
35     # Default format does not support rich root data, 
36     # fall back to rich-root
37     format = format_registry.make_bzrdir('rich-root-pack')
38     assert format.repository_format.rich_root_data
39     return format
40
41
42 class SvnRemoteFormat(BzrDirFormat):
43     """Format for the Subversion smart server."""
44     _lock_class = TransportLock
45
46     def __init__(self):
47         super(SvnRemoteFormat, self).__init__()
48         from repository import SvnRepositoryFormat
49         self.repository_format = SvnRepositoryFormat()
50
51     @classmethod
52     def probe_transport(klass, transport):
53         from transport import get_svn_ra_transport
54         from bzrlib.plugins.svn import core
55         format = klass()
56
57         try:
58             transport = get_svn_ra_transport(transport)
59         except core.SubversionException, (_, num):
60             if num in (errors.ERR_RA_ILLEGAL_URL, \
61                        errors.ERR_RA_LOCAL_REPOS_OPEN_FAILED, \
62                        errors.ERR_BAD_URL):
63                 raise bzr_errors.NotBranchError(path=transport.base)
64
65         return format
66
67     def _open(self, transport):
68         from bzrlib.plugins.svn.core import SubversionException
69         try: 
70             return remote.SvnRemoteAccess(transport, self)
71         except SubversionException, (_, num):
72             if num == errors.ERR_RA_DAV_REQUEST_FAILED:
73                 raise bzr_errors.NotBranchError(transport.base)
74             raise
75
76     def get_format_string(self):
77         return 'Subversion Smart Server'
78
79     def get_format_description(self):
80         return 'Subversion Smart Server'
81
82     def initialize_on_transport(self, transport):
83         """See BzrDir.initialize_on_transport()."""
84         from transport import get_svn_ra_transport
85         from bzrlib.transport.local import LocalTransport
86         from bzrlib.plugins.svn import repos
87
88         if not isinstance(transport, LocalTransport):
89             raise NotImplementedError(self.initialize, 
90                 "Can't create Subversion Repositories/branches on "
91                 "non-local transports")
92
93         local_path = transport._local_base.rstrip("/")
94         repos.create(local_path)
95         # All revision property changes
96         revprop_hook = os.path.join(local_path, "hooks", "pre-revprop-change")
97         open(revprop_hook, 'w').write("#!/bin/sh")
98         os.chmod(revprop_hook, os.stat(revprop_hook).st_mode | 0111)
99         return self.open(get_svn_ra_transport(transport), _found=True)
100
101     def is_supported(self):
102         """See BzrDir.is_supported()."""
103         return True
104
105
106 class SvnWorkingTreeDirFormat(BzrDirFormat):
107     """Working Tree implementation that uses Subversion working copies."""
108     _lock_class = TransportLock
109
110     def __init__(self):
111         super(SvnWorkingTreeDirFormat, self).__init__()
112         from repository import SvnRepositoryFormat
113         self.repository_format = SvnRepositoryFormat()
114
115     @classmethod
116     def probe_transport(klass, transport):
117         from bzrlib.transport.local import LocalTransport
118         from bzrlib.plugins.svn import wc
119         format = klass()
120
121         if isinstance(transport, LocalTransport) and \
122             transport.has(wc.get_adm_dir()):
123             return format
124
125         raise bzr_errors.NotBranchError(path=transport.base)
126
127     def _open(self, transport):
128         from bzrlib.plugins.svn.core import SubversionException
129         from workingtree import SvnCheckout
130         try:
131             return SvnCheckout(transport, self)
132         except SubversionException, (_, num):
133             if num in (errors.ERR_RA_LOCAL_REPOS_OPEN_FAILED,):
134                 raise errors.NoSvnRepositoryPresent(transport.base)
135             raise
136
137     def get_format_string(self):
138         return 'Subversion Local Checkout'
139
140     def get_format_description(self):
141         return 'Subversion Local Checkout'
142
143     def initialize_on_transport(self, transport):
144         raise UninitializableFormat(self)
145
146     def get_converter(self, format=None):
147         """See BzrDirFormat.get_converter()."""
148         if format is None:
149             format = get_rich_root_format()
150         raise NotImplementedError(self.get_converter)