27
27
from bzrlib.weavefile import read_weave, write_weave_v5
28
28
from bzrlib.weave import Weave
29
from bzrlib.store import Store
29
from bzrlib.store import TransportStore, hash_prefix
30
30
from bzrlib.atomicfile import AtomicFile
31
from bzrlib.errors import NoSuchFile
31
from bzrlib.errors import NoSuchFile, FileExists
32
32
from bzrlib.trace import mutter
35
class WeaveStore(Store):
35
class WeaveStore(TransportStore):
36
36
"""Collection of several weave files in a directory.
38
38
This has some shortcuts for reading and writing them.
40
40
FILE_SUFFIX = '.weave'
42
def __init__(self, transport):
42
def __init__(self, transport, prefixed=False):
43
43
self._transport = transport
44
self._prefixed = prefixed
45
46
def filename(self, file_id):
46
47
"""Return the path relative to the transport root."""
47
return file_id + WeaveStore.FILE_SUFFIX
49
return hash_prefix(file_id) + file_id + WeaveStore.FILE_SUFFIX
51
return file_id + WeaveStore.FILE_SUFFIX
49
53
def __iter__(self):
50
54
l = len(WeaveStore.FILE_SUFFIX)
51
for f in self._transport.list_dir('.'):
52
if f.endswith(WeaveStore.FILE_SUFFIX):
55
for relpath, st in self._iter_relpaths():
56
if relpath.endswith(WeaveStore.FILE_SUFFIX):
57
yield os.path.basename(relpath[:-l])
56
59
def __contains__(self, fileid):
61
64
return self._transport.get(self.filename(file_id))
63
66
def _put(self, file_id, f):
69
self._transport.mkdir(hash_prefix(file_id))
64
72
return self._transport.put(self.filename(file_id), f)
66
74
def get_weave(self, file_id, transaction):
73
81
transaction.register_clean(w)
77
84
def get_lines(self, file_id, rev_id, transaction):
78
85
"""Return text from a particular version of a weave.
100
107
write_weave_v5(weave, sio)
103
109
self._put(file_id, sio)
106
111
def add_text(self, file_id, rev_id, new_lines, parents, transaction):
107
112
w = self.get_weave_or_empty(file_id, transaction)
108
113
parent_idxs = map(w.lookup, parents)