7
7
# it under the terms of the GNU General Public License as published by
8
8
# the Free Software Foundation; either version 2 of the License, or
9
9
# (at your option) any later version.
11
11
# This program is distributed in the hope that it will be useful,
12
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
14
# GNU General Public License for more details.
16
16
# You should have received a copy of the GNU General Public License
17
17
# along with this program; if not, write to the Free Software
18
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
# Remaing to do is to figure out if get_graph should return a simple
21
# map, or a graph object of some kind.
20
24
"""Versioned text file storage api."""
22
from bzrlib.lazy_import import lazy_import
23
lazy_import(globals(), """
27
from copy import deepcopy
28
from unittest import TestSuite
31
import bzrlib.errors as errors
32
from bzrlib.inter import InterObject
33
from bzrlib.symbol_versioning import *
32
34
from bzrlib.transport.memory import MemoryTransport
35
from bzrlib.inter import InterObject
36
from bzrlib.textmerge import TextMerge
37
from bzrlib.symbol_versioning import (deprecated_function,
35
from bzrlib.tsort import topo_sort
43
39
class VersionedFile(object):
58
54
self.finished = False
59
55
self._access_mode = access_mode
62
def check_not_reserved_id(version_id):
63
revision.check_not_reserved_id(version_id)
65
57
def copy_to(self, name, transport):
66
58
"""Copy this versioned file to name on transport."""
67
59
raise NotImplementedError(self.copy_to)
69
61
@deprecated_method(zero_eight)
71
63
"""Return a list of all the versions in this versioned file.
168
154
"""Check the versioned file for integrity."""
169
155
raise NotImplementedError(self.check)
171
def _check_lines_not_unicode(self, lines):
172
"""Check that lines being added to a versioned file are not unicode."""
174
if line.__class__ is not str:
175
raise errors.BzrBadParameterUnicode("lines")
177
def _check_lines_are_lines(self, lines):
178
"""Check that the lines really are full lines without inline EOL."""
180
if '\n' in line[:-1]:
181
raise errors.BzrBadParameterContainsNewline("lines")
183
157
def _check_write_ok(self):
184
158
"""Is the versioned file marked as 'finished' ? Raise if it is."""
185
159
if self.finished:
187
161
if self._access_mode != 'w':
188
162
raise errors.ReadOnlyObjectDirtiedError(self)
190
def enable_cache(self):
191
"""Tell this versioned file that it should cache any data it reads.
193
This is advisory, implementations do not have to support caching.
197
164
def clear_cache(self):
198
"""Remove any data cached in the versioned file object.
200
This only needs to be supported if caches are supported
165
"""Remove any data cached in the versioned file object."""
204
167
def clone_text(self, new_version_id, old_version_id, parents):
205
168
"""Add an identical text to old_version_id as new_version_id.
235
196
the parents list must be a superset of the current
238
version_id = osutils.safe_revision_id(version_id)
239
new_parents = [osutils.safe_revision_id(p) for p in new_parents]
240
199
self._check_write_ok()
241
return self._fix_parents(version_id, new_parents)
200
return self._fix_parents(version, new_parents)
243
def _fix_parents(self, version_id, new_parents):
202
def _fix_parents(self, version, new_parents):
244
203
"""Helper for fix_parents."""
245
204
raise NotImplementedError(self.fix_parents)
260
219
version_id is the version_id created by that delta.
263
for version_id in version_ids:
264
result[version_id] = self.get_delta(version_id)
222
for version in versions:
223
result[version] = self.get_delta(version)
267
def get_sha1(self, version_id):
268
"""Get the stored sha1 sum for the given revision.
270
:param name: The name of the version to lookup
272
raise NotImplementedError(self.get_sha1)
274
226
def get_suffixes(self):
275
227
"""Return the file suffixes associated with this versioned file."""
276
228
raise NotImplementedError(self.get_suffixes)
301
245
raise NotImplementedError(self.get_lines)
303
def get_ancestry(self, version_ids, topo_sorted=True):
247
def get_ancestry(self, version_ids):
304
248
"""Return a list of all ancestors of given version(s). This
305
249
will not include the null revision.
307
This list will not be topologically sorted if topo_sorted=False is
310
251
Must raise RevisionNotPresent if any of the given versions are
311
252
not present in file history."""
312
253
if isinstance(version_ids, basestring):
326
267
raise NotImplementedError(self.get_ancestry_with_ghosts)
328
def get_graph(self, version_ids=None):
329
"""Return a graph from the versioned file.
270
"""Return a graph for the entire versioned file.
331
272
Ghosts are not listed or referenced in the graph.
332
:param version_ids: Versions to select.
333
None means retrieve all versions.
336
if version_ids is None:
337
for version in self.versions():
338
result[version] = self.get_parents(version)
340
pending = set(osutils.safe_revision_id(v) for v in version_ids)
342
version = pending.pop()
343
if version in result:
345
parents = self.get_parents(version)
346
for parent in parents:
350
result[version] = parents
275
for version in self.versions():
276
result[version] = self.get_parents(version)
353
279
def get_graph_with_ghosts(self):
427
def iter_lines_added_or_present_in_versions(self, version_ids=None,
353
def iter_lines_added_or_present_in_versions(self, version_ids=None):
429
354
"""Iterate over the lines in the versioned file from version_ids.
431
356
This may return lines from other versions, and does not return the
432
357
specific version marker at this point. The api may be changed
433
358
during development to include the version that the versioned file
434
359
thinks is relevant, but given that such hints are just guesses,
435
its better not to have it if we don't need it.
437
If a progress bar is supplied, it may be used to indicate progress.
438
The caller is responsible for cleaning up progress bars (because this
360
its better not to have it if we dont need it.
441
362
NOTES: Lines are normalised: they will all have \n terminators.
442
363
Lines are returned in arbitrary order.
481
402
Weave lines present in none of them are skipped entirely.
484
killed-base Dead in base revision
485
killed-both Killed in each revision
488
unchanged Alive in both a and b (possibly created in both)
491
ghost-a Killed in a, unborn in b
492
ghost-b Killed in b, unborn in a
493
irrelevant Not in either revision
495
raise NotImplementedError(VersionedFile.plan_merge)
497
def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
498
b_marker=TextMerge.B_MARKER):
499
return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
502
class PlanWeaveMerge(TextMerge):
503
"""Weave merge that takes a plan as its input.
505
This exists so that VersionedFile.plan_merge is implementable.
506
Most callers will want to use WeaveMerge instead.
509
def __init__(self, plan, a_marker=TextMerge.A_MARKER,
510
b_marker=TextMerge.B_MARKER):
511
TextMerge.__init__(self, a_marker, b_marker)
514
def _merge_struct(self):
404
inc_a = set(self.get_ancestry([ver_a]))
405
inc_b = set(self.get_ancestry([ver_b]))
406
inc_c = inc_a & inc_b
408
for lineno, insert, deleteset, line in self.walk([ver_a, ver_b]):
409
if deleteset & inc_c:
410
# killed in parent; can't be in either a or b
411
# not relevant to our work
412
yield 'killed-base', line
413
elif insert in inc_c:
414
# was inserted in base
415
killed_a = bool(deleteset & inc_a)
416
killed_b = bool(deleteset & inc_b)
417
if killed_a and killed_b:
418
yield 'killed-both', line
420
yield 'killed-a', line
422
yield 'killed-b', line
424
yield 'unchanged', line
425
elif insert in inc_a:
426
if deleteset & inc_a:
427
yield 'ghost-a', line
431
elif insert in inc_b:
432
if deleteset & inc_b:
433
yield 'ghost-b', line
437
# not in either revision
438
yield 'irrelevant', line
440
yield 'unchanged', '' # terminator
442
def weave_merge(self, plan, a_marker='<<<<<<< \n', b_marker='>>>>>>> \n'):
517
445
ch_a = ch_b = False
519
def outstanding_struct():
520
if not lines_a and not lines_b:
522
elif ch_a and not ch_b:
525
elif ch_b and not ch_a:
527
elif lines_a == lines_b:
530
yield (lines_a, lines_b)
532
# We previously considered either 'unchanged' or 'killed-both' lines
533
# to be possible places to resynchronize. However, assuming agreement
534
# on killed-both lines may be too aggressive. -- mbp 20060324
535
for state, line in self.plan:
536
if state == 'unchanged':
446
# TODO: Return a structured form of the conflicts (e.g. 2-tuples for
447
# conflicted regions), rather than just inserting the markers.
449
# TODO: Show some version information (e.g. author, date) on
450
# conflicted regions.
451
for state, line in plan:
452
if state == 'unchanged' or state == 'killed-both':
537
453
# resync and flush queued conflicts changes if any
538
for struct in outstanding_struct():
454
if not lines_a and not lines_b:
456
elif ch_a and not ch_b:
458
for l in lines_a: yield l
459
elif ch_b and not ch_a:
460
for l in lines_b: yield l
461
elif lines_a == lines_b:
462
for l in lines_a: yield l
465
for l in lines_a: yield l
467
for l in lines_b: yield l
542
472
ch_a = ch_b = False
544
474
if state == 'unchanged':
547
477
elif state == 'killed-a':
549
479
lines_b.append(line)
558
488
lines_b.append(line)
560
assert state in ('irrelevant', 'ghost-a', 'ghost-b',
561
'killed-base', 'killed-both'), state
562
for struct in outstanding_struct():
566
class WeaveMerge(PlanWeaveMerge):
567
"""Weave merge that takes a VersionedFile and two versions as its input"""
569
def __init__(self, versionedfile, ver_a, ver_b,
570
a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
571
plan = versionedfile.plan_merge(ver_a, ver_b)
572
PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
490
assert state in ('irrelevant', 'ghost-a', 'ghost-b', 'killed-base',
575
495
class InterVersionedFile(InterObject):
609
529
# Make a new target-format versioned file.
610
530
temp_source = self.target.create_empty("temp", MemoryTransport())
611
531
target = temp_source
612
version_ids = self._get_source_version_ids(version_ids, ignore_missing)
613
graph = self.source.get_graph(version_ids)
614
order = tsort.topo_sort(graph.items())
532
graph = self.source.get_graph()
533
order = topo_sort(graph.items())
615
534
pb = ui.ui_factory.nested_progress_bar()
616
535
parent_texts = {}
658
def _get_source_version_ids(self, version_ids, ignore_missing):
659
"""Determine the version ids to be used from self.source.
661
:param version_ids: The caller-supplied version ids to check. (None
662
for all). If None is in version_ids, it is stripped.
663
:param ignore_missing: if True, remove missing ids from the version
664
list. If False, raise RevisionNotPresent on
665
a missing version id.
666
:return: A set of version ids.
668
if version_ids is None:
669
# None cannot be in source.versions
670
return set(self.source.versions())
672
version_ids = [osutils.safe_revision_id(v) for v in version_ids]
674
return set(self.source.versions()).intersection(set(version_ids))
676
new_version_ids = set()
677
for version in version_ids:
680
if not self.source.has_version(version):
681
raise errors.RevisionNotPresent(version, str(self.source))
683
new_version_ids.add(version)
684
return new_version_ids
578
class InterVersionedFileTestProviderAdapter(object):
579
"""A tool to generate a suite testing multiple inter versioned-file classes.
581
This is done by copying the test once for each interversionedfile provider
582
and injecting the transport_server, transport_readonly_server,
583
versionedfile_factory and versionedfile_factory_to classes into each copy.
584
Each copy is also given a new id() to make it easy to identify.
587
def __init__(self, transport_server, transport_readonly_server, formats):
588
self._transport_server = transport_server
589
self._transport_readonly_server = transport_readonly_server
590
self._formats = formats
592
def adapt(self, test):
594
for (interversionedfile_class,
595
versionedfile_factory,
596
versionedfile_factory_to) in self._formats:
597
new_test = deepcopy(test)
598
new_test.transport_server = self._transport_server
599
new_test.transport_readonly_server = self._transport_readonly_server
600
new_test.interversionedfile_class = interversionedfile_class
601
new_test.versionedfile_factory = versionedfile_factory
602
new_test.versionedfile_factory_to = versionedfile_factory_to
603
def make_new_test_id():
604
new_id = "%s(%s)" % (new_test.id(), interversionedfile_class.__name__)
605
return lambda: new_id
606
new_test.id = make_new_test_id()
607
result.addTest(new_test)
611
def default_test_list():
612
"""Generate the default list of interversionedfile permutations to test."""
613
from bzrlib.weave import WeaveFile
614
from bzrlib.knit import KnitVersionedFile
616
# test the fallback InterVersionedFile from weave to annotated knits
617
result.append((InterVersionedFile,
620
for optimiser in InterVersionedFile._optimisers:
621
result.append((optimiser,
622
optimiser._matching_file_factory,
623
optimiser._matching_file_factory
625
# if there are specific combinations we want to use, we can add them