3
# Copyright (C) 2005 Canonical Ltd
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
# XXX: Some consideration of the problems that might occur if there are
20
# files whose id differs only in case. That should probably be forbidden.
23
from cStringIO import StringIO
27
from bzrlib.weavefile import read_weave, write_weave_v5
28
from bzrlib.weave import Weave
29
from bzrlib.store import Store
30
from bzrlib.atomicfile import AtomicFile
31
from bzrlib.errors import NoSuchFile
32
from bzrlib.trace import mutter
37
class WeaveStore(Store):
38
"""Collection of several weave files in a directory.
40
This has some shortcuts for reading and writing them.
42
FILE_SUFFIX = '.weave'
44
def __init__(self, transport):
45
self._transport = transport
47
self.enable_cache = False
50
def filename(self, file_id):
51
"""Return the path relative to the transport root."""
52
return file_id + WeaveStore.FILE_SUFFIX
55
l = len(WeaveStore.FILE_SUFFIX)
56
for f in self._transport.list_dir('.'):
57
if f.endswith(WeaveStore.FILE_SUFFIX):
61
def __contains__(self, fileid):
63
return self._transport.has(self.filename(fileid))
65
def _get(self, file_id):
66
return self._transport.get(self.filename(file_id))
68
def _put(self, file_id, f):
69
return self._transport.put(self.filename(file_id), f)
72
def get_weave(self, file_id):
74
if file_id in self._cache:
75
return self._cache[file_id]
76
w = read_weave(self._get(file_id))
78
self._cache[file_id] = w
82
def get_lines(self, file_id, rev_id):
83
"""Return text from a particular version of a weave.
85
Returned as a list of lines."""
86
w = self.get_weave(file_id)
87
return w.get(w.lookup(rev_id))
90
def get_weave_or_empty(self, file_id):
91
"""Return a weave, or an empty one if it doesn't exist."""
93
inf = self._get(file_id)
95
return Weave(weave_name=file_id)
97
return read_weave(inf)
100
def put_weave(self, file_id, weave):
101
"""Write back a modified weave"""
102
if self.enable_cache:
103
self._cache[file_id] = weave
106
write_weave_v5(weave, sio)
109
self._put(file_id, sio)
112
def add_text(self, file_id, rev_id, new_lines, parents):
113
w = self.get_weave_or_empty(file_id)
114
parent_idxs = map(w.lookup, parents)
115
w.add(rev_id, parent_idxs, new_lines)
116
self.put_weave(file_id, w)
118
def add_identical_text(self, file_id, old_rev_id, new_rev_id, parents):
119
w = self.get_weave_or_empty(file_id)
120
parent_idxs = map(w.lookup, parents)
121
w.add_identical(old_rev_id, new_rev_id, parent_idxs)
122
self.put_weave(file_id, w)
124
def copy_multi(self, from_store, file_ids):
125
assert isinstance(from_store, WeaveStore)
127
mutter("copy weave {%s} into %s", f, self)
128
self._put(f, from_store._get(f))