28
26
from cStringIO import StringIO
31
from errors import BzrError, BzrCheckError
32
from branch import Branch, BZR_BRANCH_FORMAT
33
from trace import mutter
31
from bzrlib.errors import BzrError, BzrCheckError
32
from bzrlib.branch import Branch, LocalBranch, BZR_BRANCH_FORMAT_5
33
from bzrlib.trace import mutter
34
from bzrlib.weavestore import WeaveStore
35
from bzrlib.xml5 import serializer_v5
35
38
# velocitynet.com.au transparently proxies connections and thereby
36
39
# breaks keep-alive -- sucks!
87
ff = get_url(url + '/.bzr/branch-format')
90
fmt_url = url + '/.bzr/branch-format'
92
fmt = fmt.rstrip('\r\n')
93
if fmt != BZR_BRANCH_FORMAT.rstrip('\r\n'):
95
if fmt != BZR_BRANCH_FORMAT_5:
94
96
raise BzrError("sorry, branch format %r not supported at url %s"
98
100
except urllib2.URLError:
102
idx = url.rindex('/')
104
raise BzrError('no branch root found for URL %s' % orig_url)
110
class RemoteBranch(Branch):
103
scheme, host, path = list(urlparse.urlparse(url))[:3]
104
# discard params, query, fragment
106
# strip off one component of the path component
107
idx = path.rfind('/')
108
if idx == -1 or path == '/':
109
raise BzrError('no branch root found for URL %s'
110
' or enclosing directories'
113
url = urlparse.urlunparse((scheme, host, path, '', '', ''))
117
class RemoteBranch(LocalBranch):
111
118
def __init__(self, baseurl, find_root=True):
112
119
"""Create new proxy for a remote branch."""
114
self.baseurl = _find_remote_root(baseurl)
121
self.base = _find_remote_root(baseurl)
116
self.baseurl = baseurl
124
self._check_format(False)
125
# is guaranteed to be a v5 store
119
self.inventory_store = RemoteStore(baseurl + '/.bzr/inventory-store/')
120
self.text_store = RemoteStore(baseurl + '/.bzr/text-store/')
121
self.revision_store = RemoteStore(baseurl + '/.bzr/revision-store/')
127
cfn = self.controlfilename
128
assert self._branch_format == 5
129
self.control_weaves = WeaveStore(cfn([]), get_url)
130
self.weave_store = WeaveStore(cfn('weaves'), get_url)
131
self.revision_store = RemoteStore(cfn('revision-store'))
123
133
def __str__(self):
124
134
b = getattr(self, 'baseurl', 'undefined')
127
137
__repr__ = __str__
139
def setup_caching(self, cache_root):
140
"""Set up cached stores located under cache_root"""
141
from bzrlib.meta_store import CachedStore
142
for store_name in ('inventory_store', 'text_store', 'revision_store'):
143
if not isinstance(getattr(self, store_name), CachedStore):
144
cache_path = os.path.join(cache_root, store_name)
146
new_store = CachedStore(getattr(self, store_name), cache_path)
147
setattr(self, store_name, new_store)
129
149
def controlfile(self, filename, mode):
130
150
if mode not in ('rb', 'rt', 'r'):
131
151
raise BzrError("file mode %r not supported for remote branches" % mode)
132
return get_url(self.baseurl + '/.bzr/' + filename, False)
152
return get_url(self.base + '/.bzr/' + filename, False)
135
155
def lock_read(self):
139
159
def lock_write(self):
140
160
from errors import LockError
141
161
raise LockError("write lock not supported for remote branch %s"
144
164
def unlock(self):
148
168
def relpath(self, path):
149
if not path.startswith(self.baseurl):
169
if not path.startswith(self.base):
150
170
raise BzrError('path %r is not under base URL %r'
151
% (path, self.baseurl))
152
pl = len(self.baseurl)
153
173
return path[pl:].lstrip('/')
156
176
def get_revision(self, revision_id):
157
from bzrlib.revision import Revision
158
from bzrlib.xml import unpack_xml
160
178
revf = self.revision_store[revision_id]
162
180
raise NoSuchRevision(self, revision_id)
163
r = unpack_xml(Revision, revf)
181
r = serializer_v5.read_revision(revf)
164
182
if r.revision_id != revision_id:
165
183
raise BzrCheckError('revision stored as {%s} actually contains {%s}'
166
184
% (revision_id, r.revision_id))
177
195
raise ValueError('invalid store id', name)
178
196
return self._baseurl + '/' + name
198
def __contains__(self, fileid):
180
205
def __getitem__(self, fileid):
181
206
p = self._path(fileid)
183
208
return get_url(p, compressed=True)
209
except urllib2.URLError:
212
return get_url(p, compressed=False)
213
except urllib2.URLError:
185
214
raise KeyError(fileid)