/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/versionedfile.py

  • Committer: Aaron Bentley
  • Date: 2008-03-28 03:09:47 UTC
  • mto: This revision was merged to the branch mainline in revision 3371.
  • Revision ID: aaron@aaronbentley.com-20080328030947-57cfqi7to7k401wc
Have xml5 inherit from xml6 from xml8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005, 2006 Canonical Ltd
 
2
#
 
3
# Authors:
 
4
#   Johan Rydberg <jrydberg@gnu.org>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; either version 2 of the License, or
 
9
# (at your option) any later version.
 
10
#
 
11
# This program is distributed in the hope that it will be useful,
 
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
# GNU General Public License for more details.
 
15
#
 
16
# You should have received a copy of the GNU General Public License
 
17
# along with this program; if not, write to the Free Software
 
18
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
 
 
20
"""Versioned text file storage api."""
 
21
 
 
22
from bzrlib.lazy_import import lazy_import
 
23
lazy_import(globals(), """
 
24
 
 
25
from bzrlib import (
 
26
    errors,
 
27
    osutils,
 
28
    multiparent,
 
29
    tsort,
 
30
    revision,
 
31
    ui,
 
32
    )
 
33
from bzrlib.transport.memory import MemoryTransport
 
34
""")
 
35
 
 
36
from cStringIO import StringIO
 
37
 
 
38
from bzrlib.inter import InterObject
 
39
from bzrlib.symbol_versioning import *
 
40
from bzrlib.textmerge import TextMerge
 
41
 
 
42
 
 
43
class VersionedFile(object):
 
44
    """Versioned text file storage.
 
45
    
 
46
    A versioned file manages versions of line-based text files,
 
47
    keeping track of the originating version for each line.
 
48
 
 
49
    To clients the "lines" of the file are represented as a list of
 
50
    strings. These strings will typically have terminal newline
 
51
    characters, but this is not required.  In particular files commonly
 
52
    do not have a newline at the end of the file.
 
53
 
 
54
    Texts are identified by a version-id string.
 
55
    """
 
56
 
 
57
    def __init__(self, access_mode):
 
58
        self.finished = False
 
59
        self._access_mode = access_mode
 
60
 
 
61
    @staticmethod
 
62
    def check_not_reserved_id(version_id):
 
63
        revision.check_not_reserved_id(version_id)
 
64
 
 
65
    def copy_to(self, name, transport):
 
66
        """Copy this versioned file to name on transport."""
 
67
        raise NotImplementedError(self.copy_to)
 
68
 
 
69
    def versions(self):
 
70
        """Return a unsorted list of versions."""
 
71
        raise NotImplementedError(self.versions)
 
72
 
 
73
    def has_ghost(self, version_id):
 
74
        """Returns whether version is present as a ghost."""
 
75
        raise NotImplementedError(self.has_ghost)
 
76
 
 
77
    def has_version(self, version_id):
 
78
        """Returns whether version is present."""
 
79
        raise NotImplementedError(self.has_version)
 
80
 
 
81
    def add_lines(self, version_id, parents, lines, parent_texts=None,
 
82
        left_matching_blocks=None, nostore_sha=None, random_id=False,
 
83
        check_content=True):
 
84
        """Add a single text on top of the versioned file.
 
85
 
 
86
        Must raise RevisionAlreadyPresent if the new version is
 
87
        already present in file history.
 
88
 
 
89
        Must raise RevisionNotPresent if any of the given parents are
 
90
        not present in file history.
 
91
 
 
92
        :param lines: A list of lines. Each line must be a bytestring. And all
 
93
            of them except the last must be terminated with \n and contain no
 
94
            other \n's. The last line may either contain no \n's or a single
 
95
            terminated \n. If the lines list does meet this constraint the add
 
96
            routine may error or may succeed - but you will be unable to read
 
97
            the data back accurately. (Checking the lines have been split
 
98
            correctly is expensive and extremely unlikely to catch bugs so it
 
99
            is not done at runtime unless check_content is True.)
 
100
        :param parent_texts: An optional dictionary containing the opaque 
 
101
            representations of some or all of the parents of version_id to
 
102
            allow delta optimisations.  VERY IMPORTANT: the texts must be those
 
103
            returned by add_lines or data corruption can be caused.
 
104
        :param left_matching_blocks: a hint about which areas are common
 
105
            between the text and its left-hand-parent.  The format is
 
106
            the SequenceMatcher.get_matching_blocks format.
 
107
        :param nostore_sha: Raise ExistingContent and do not add the lines to
 
108
            the versioned file if the digest of the lines matches this.
 
109
        :param random_id: If True a random id has been selected rather than
 
110
            an id determined by some deterministic process such as a converter
 
111
            from a foreign VCS. When True the backend may choose not to check
 
112
            for uniqueness of the resulting key within the versioned file, so
 
113
            this should only be done when the result is expected to be unique
 
114
            anyway.
 
115
        :param check_content: If True, the lines supplied are verified to be
 
116
            bytestrings that are correctly formed lines.
 
117
        :return: The text sha1, the number of bytes in the text, and an opaque
 
118
                 representation of the inserted version which can be provided
 
119
                 back to future add_lines calls in the parent_texts dictionary.
 
120
        """
 
121
        self._check_write_ok()
 
122
        return self._add_lines(version_id, parents, lines, parent_texts,
 
123
            left_matching_blocks, nostore_sha, random_id, check_content)
 
124
 
 
125
    def _add_lines(self, version_id, parents, lines, parent_texts,
 
126
        left_matching_blocks, nostore_sha, random_id, check_content):
 
127
        """Helper to do the class specific add_lines."""
 
128
        raise NotImplementedError(self.add_lines)
 
129
 
 
130
    def add_lines_with_ghosts(self, version_id, parents, lines,
 
131
        parent_texts=None, nostore_sha=None, random_id=False,
 
132
        check_content=True, left_matching_blocks=None):
 
133
        """Add lines to the versioned file, allowing ghosts to be present.
 
134
        
 
135
        This takes the same parameters as add_lines and returns the same.
 
136
        """
 
137
        self._check_write_ok()
 
138
        return self._add_lines_with_ghosts(version_id, parents, lines,
 
139
            parent_texts, nostore_sha, random_id, check_content, left_matching_blocks)
 
140
 
 
141
    def _add_lines_with_ghosts(self, version_id, parents, lines, parent_texts,
 
142
        nostore_sha, random_id, check_content, left_matching_blocks):
 
143
        """Helper to do class specific add_lines_with_ghosts."""
 
144
        raise NotImplementedError(self.add_lines_with_ghosts)
 
145
 
 
146
    def check(self, progress_bar=None):
 
147
        """Check the versioned file for integrity."""
 
148
        raise NotImplementedError(self.check)
 
149
 
 
150
    def _check_lines_not_unicode(self, lines):
 
151
        """Check that lines being added to a versioned file are not unicode."""
 
152
        for line in lines:
 
153
            if line.__class__ is not str:
 
154
                raise errors.BzrBadParameterUnicode("lines")
 
155
 
 
156
    def _check_lines_are_lines(self, lines):
 
157
        """Check that the lines really are full lines without inline EOL."""
 
158
        for line in lines:
 
159
            if '\n' in line[:-1]:
 
160
                raise errors.BzrBadParameterContainsNewline("lines")
 
161
 
 
162
    def _check_write_ok(self):
 
163
        """Is the versioned file marked as 'finished' ? Raise if it is."""
 
164
        if self.finished:
 
165
            raise errors.OutSideTransaction()
 
166
        if self._access_mode != 'w':
 
167
            raise errors.ReadOnlyObjectDirtiedError(self)
 
168
 
 
169
    def enable_cache(self):
 
170
        """Tell this versioned file that it should cache any data it reads.
 
171
        
 
172
        This is advisory, implementations do not have to support caching.
 
173
        """
 
174
        pass
 
175
    
 
176
    def clear_cache(self):
 
177
        """Remove any data cached in the versioned file object.
 
178
 
 
179
        This only needs to be supported if caches are supported
 
180
        """
 
181
        pass
 
182
 
 
183
    def clone_text(self, new_version_id, old_version_id, parents):
 
184
        """Add an identical text to old_version_id as new_version_id.
 
185
 
 
186
        Must raise RevisionNotPresent if the old version or any of the
 
187
        parents are not present in file history.
 
188
 
 
189
        Must raise RevisionAlreadyPresent if the new version is
 
190
        already present in file history."""
 
191
        self._check_write_ok()
 
192
        return self._clone_text(new_version_id, old_version_id, parents)
 
193
 
 
194
    def _clone_text(self, new_version_id, old_version_id, parents):
 
195
        """Helper function to do the _clone_text work."""
 
196
        raise NotImplementedError(self.clone_text)
 
197
 
 
198
    def create_empty(self, name, transport, mode=None):
 
199
        """Create a new versioned file of this exact type.
 
200
 
 
201
        :param name: the file name
 
202
        :param transport: the transport
 
203
        :param mode: optional file mode.
 
204
        """
 
205
        raise NotImplementedError(self.create_empty)
 
206
 
 
207
    def get_format_signature(self):
 
208
        """Get a text description of the data encoding in this file.
 
209
        
 
210
        :since: 0.90
 
211
        """
 
212
        raise NotImplementedError(self.get_format_signature)
 
213
 
 
214
    def make_mpdiffs(self, version_ids):
 
215
        """Create multiparent diffs for specified versions."""
 
216
        knit_versions = set()
 
217
        knit_versions.update(version_ids)
 
218
        parent_map = self.get_parent_map(version_ids)
 
219
        for version_id in version_ids:
 
220
            try:
 
221
                knit_versions.update(parent_map[version_id])
 
222
            except KeyError:
 
223
                raise RevisionNotPresent(version_id, self)
 
224
        # We need to filter out ghosts, because we can't diff against them.
 
225
        knit_versions = set(self.get_parent_map(knit_versions).keys())
 
226
        lines = dict(zip(knit_versions,
 
227
            self._get_lf_split_line_list(knit_versions)))
 
228
        diffs = []
 
229
        for version_id in version_ids:
 
230
            target = lines[version_id]
 
231
            try:
 
232
                parents = [lines[p] for p in parent_map[version_id] if p in
 
233
                    knit_versions]
 
234
            except KeyError:
 
235
                raise RevisionNotPresent(version_id, self)
 
236
            if len(parents) > 0:
 
237
                left_parent_blocks = self._extract_blocks(version_id,
 
238
                                                          parents[0], target)
 
239
            else:
 
240
                left_parent_blocks = None
 
241
            diffs.append(multiparent.MultiParent.from_lines(target, parents,
 
242
                         left_parent_blocks))
 
243
        return diffs
 
244
 
 
245
    def _extract_blocks(self, version_id, source, target):
 
246
        return None
 
247
 
 
248
    def add_mpdiffs(self, records):
 
249
        """Add mpdiffs to this VersionedFile.
 
250
 
 
251
        Records should be iterables of version, parents, expected_sha1,
 
252
        mpdiff. mpdiff should be a MultiParent instance.
 
253
        """
 
254
        # Does this need to call self._check_write_ok()? (IanC 20070919)
 
255
        vf_parents = {}
 
256
        mpvf = multiparent.MultiMemoryVersionedFile()
 
257
        versions = []
 
258
        for version, parent_ids, expected_sha1, mpdiff in records:
 
259
            versions.append(version)
 
260
            mpvf.add_diff(mpdiff, version, parent_ids)
 
261
        needed_parents = set()
 
262
        for version, parent_ids, expected_sha1, mpdiff in records:
 
263
            needed_parents.update(p for p in parent_ids
 
264
                                  if not mpvf.has_version(p))
 
265
        present_parents = set(self.get_parent_map(needed_parents).keys())
 
266
        for parent_id, lines in zip(present_parents,
 
267
                                 self._get_lf_split_line_list(present_parents)):
 
268
            mpvf.add_version(lines, parent_id, [])
 
269
        for (version, parent_ids, expected_sha1, mpdiff), lines in\
 
270
            zip(records, mpvf.get_line_list(versions)):
 
271
            if len(parent_ids) == 1:
 
272
                left_matching_blocks = list(mpdiff.get_matching_blocks(0,
 
273
                    mpvf.get_diff(parent_ids[0]).num_lines()))
 
274
            else:
 
275
                left_matching_blocks = None
 
276
            try:
 
277
                _, _, version_text = self.add_lines_with_ghosts(version,
 
278
                    parent_ids, lines, vf_parents,
 
279
                    left_matching_blocks=left_matching_blocks)
 
280
            except NotImplementedError:
 
281
                # The vf can't handle ghosts, so add lines normally, which will
 
282
                # (reasonably) fail if there are ghosts in the data.
 
283
                _, _, version_text = self.add_lines(version,
 
284
                    parent_ids, lines, vf_parents,
 
285
                    left_matching_blocks=left_matching_blocks)
 
286
            vf_parents[version] = version_text
 
287
        for (version, parent_ids, expected_sha1, mpdiff), sha1 in\
 
288
             zip(records, self.get_sha1s(versions)):
 
289
            if expected_sha1 != sha1:
 
290
                raise errors.VersionedFileInvalidChecksum(version)
 
291
 
 
292
    def get_sha1(self, version_id):
 
293
        """Get the stored sha1 sum for the given revision.
 
294
        
 
295
        :param version_id: The name of the version to lookup
 
296
        """
 
297
        raise NotImplementedError(self.get_sha1)
 
298
 
 
299
    def get_sha1s(self, version_ids):
 
300
        """Get the stored sha1 sums for the given revisions.
 
301
 
 
302
        :param version_ids: The names of the versions to lookup
 
303
        :return: a list of sha1s in order according to the version_ids
 
304
        """
 
305
        raise NotImplementedError(self.get_sha1s)
 
306
 
 
307
    def get_suffixes(self):
 
308
        """Return the file suffixes associated with this versioned file."""
 
309
        raise NotImplementedError(self.get_suffixes)
 
310
    
 
311
    def get_text(self, version_id):
 
312
        """Return version contents as a text string.
 
313
 
 
314
        Raises RevisionNotPresent if version is not present in
 
315
        file history.
 
316
        """
 
317
        return ''.join(self.get_lines(version_id))
 
318
    get_string = get_text
 
319
 
 
320
    def get_texts(self, version_ids):
 
321
        """Return the texts of listed versions as a list of strings.
 
322
 
 
323
        Raises RevisionNotPresent if version is not present in
 
324
        file history.
 
325
        """
 
326
        return [''.join(self.get_lines(v)) for v in version_ids]
 
327
 
 
328
    def get_lines(self, version_id):
 
329
        """Return version contents as a sequence of lines.
 
330
 
 
331
        Raises RevisionNotPresent if version is not present in
 
332
        file history.
 
333
        """
 
334
        raise NotImplementedError(self.get_lines)
 
335
 
 
336
    def _get_lf_split_line_list(self, version_ids):
 
337
        return [StringIO(t).readlines() for t in self.get_texts(version_ids)]
 
338
 
 
339
    def get_ancestry(self, version_ids, topo_sorted=True):
 
340
        """Return a list of all ancestors of given version(s). This
 
341
        will not include the null revision.
 
342
 
 
343
        This list will not be topologically sorted if topo_sorted=False is
 
344
        passed.
 
345
 
 
346
        Must raise RevisionNotPresent if any of the given versions are
 
347
        not present in file history."""
 
348
        if isinstance(version_ids, basestring):
 
349
            version_ids = [version_ids]
 
350
        raise NotImplementedError(self.get_ancestry)
 
351
        
 
352
    def get_ancestry_with_ghosts(self, version_ids):
 
353
        """Return a list of all ancestors of given version(s). This
 
354
        will not include the null revision.
 
355
 
 
356
        Must raise RevisionNotPresent if any of the given versions are
 
357
        not present in file history.
 
358
        
 
359
        Ghosts that are known about will be included in ancestry list,
 
360
        but are not explicitly marked.
 
361
        """
 
362
        raise NotImplementedError(self.get_ancestry_with_ghosts)
 
363
        
 
364
    def get_graph(self, version_ids=None):
 
365
        """Return a graph from the versioned file. 
 
366
        
 
367
        Ghosts are not listed or referenced in the graph.
 
368
        :param version_ids: Versions to select.
 
369
                            None means retrieve all versions.
 
370
        """
 
371
        if version_ids is None:
 
372
            return dict(self.iter_parents(self.versions()))
 
373
        result = {}
 
374
        pending = set(version_ids)
 
375
        while pending:
 
376
            this_iteration = pending
 
377
            pending = set()
 
378
            for version, parents in self.iter_parents(this_iteration):
 
379
                result[version] = parents
 
380
                for parent in parents:
 
381
                    if parent in result:
 
382
                        continue
 
383
                    pending.add(parent)
 
384
        return result
 
385
 
 
386
    def get_graph_with_ghosts(self):
 
387
        """Return a graph for the entire versioned file.
 
388
        
 
389
        Ghosts are referenced in parents list but are not
 
390
        explicitly listed.
 
391
        """
 
392
        raise NotImplementedError(self.get_graph_with_ghosts)
 
393
 
 
394
    def get_parent_map(self, version_ids):
 
395
        """Get a map of the parents of version_ids.
 
396
 
 
397
        :param version_ids: The version ids to look up parents for.
 
398
        :return: A mapping from version id to parents.
 
399
        """
 
400
        raise NotImplementedError(self.get_parent_map)
 
401
 
 
402
    @deprecated_method(one_four)
 
403
    def get_parents(self, version_id):
 
404
        """Return version names for parents of a version.
 
405
 
 
406
        Must raise RevisionNotPresent if version is not present in
 
407
        file history.
 
408
        """
 
409
        try:
 
410
            all = self.get_parent_map([version_id])[version_id]
 
411
        except KeyError:
 
412
            raise errors.RevisionNotPresent(version_id, self)
 
413
        result = []
 
414
        parent_parents = self.get_parent_map(all)
 
415
        for version_id in all:
 
416
            if version_id in parent_parents:
 
417
                result.append(version_id)
 
418
        return result
 
419
 
 
420
    def get_parents_with_ghosts(self, version_id):
 
421
        """Return version names for parents of version_id.
 
422
 
 
423
        Will raise RevisionNotPresent if version_id is not present
 
424
        in the history.
 
425
 
 
426
        Ghosts that are known about will be included in the parent list,
 
427
        but are not explicitly marked.
 
428
        """
 
429
        try:
 
430
            return list(self.get_parent_map([version_id])[version_id])
 
431
        except KeyError:
 
432
            raise errors.RevisionNotPresent(version_id, self)
 
433
 
 
434
    def annotate_iter(self, version_id):
 
435
        """Yield list of (version-id, line) pairs for the specified
 
436
        version.
 
437
 
 
438
        Must raise RevisionNotPresent if the given version is
 
439
        not present in file history.
 
440
        """
 
441
        raise NotImplementedError(self.annotate_iter)
 
442
 
 
443
    def annotate(self, version_id):
 
444
        return list(self.annotate_iter(version_id))
 
445
 
 
446
    def join(self, other, pb=None, msg=None, version_ids=None,
 
447
             ignore_missing=False):
 
448
        """Integrate versions from other into this versioned file.
 
449
 
 
450
        If version_ids is None all versions from other should be
 
451
        incorporated into this versioned file.
 
452
 
 
453
        Must raise RevisionNotPresent if any of the specified versions
 
454
        are not present in the other file's history unless ignore_missing
 
455
        is supplied in which case they are silently skipped.
 
456
        """
 
457
        self._check_write_ok()
 
458
        return InterVersionedFile.get(other, self).join(
 
459
            pb,
 
460
            msg,
 
461
            version_ids,
 
462
            ignore_missing)
 
463
 
 
464
    def iter_lines_added_or_present_in_versions(self, version_ids=None,
 
465
                                                pb=None):
 
466
        """Iterate over the lines in the versioned file from version_ids.
 
467
 
 
468
        This may return lines from other versions. Each item the returned
 
469
        iterator yields is a tuple of a line and a text version that that line
 
470
        is present in (not introduced in).
 
471
 
 
472
        Ordering of results is in whatever order is most suitable for the
 
473
        underlying storage format.
 
474
 
 
475
        If a progress bar is supplied, it may be used to indicate progress.
 
476
        The caller is responsible for cleaning up progress bars (because this
 
477
        is an iterator).
 
478
 
 
479
        NOTES: Lines are normalised: they will all have \n terminators.
 
480
               Lines are returned in arbitrary order.
 
481
 
 
482
        :return: An iterator over (line, version_id).
 
483
        """
 
484
        raise NotImplementedError(self.iter_lines_added_or_present_in_versions)
 
485
 
 
486
    def iter_parents(self, version_ids):
 
487
        """Iterate through the parents for many version ids.
 
488
 
 
489
        :param version_ids: An iterable yielding version_ids.
 
490
        :return: An iterator that yields (version_id, parents). Requested 
 
491
            version_ids not present in the versioned file are simply skipped.
 
492
            The order is undefined, allowing for different optimisations in
 
493
            the underlying implementation.
 
494
        """
 
495
        return self.get_parent_map(version_ids).iteritems()
 
496
 
 
497
    def transaction_finished(self):
 
498
        """The transaction that this file was opened in has finished.
 
499
 
 
500
        This records self.finished = True and should cause all mutating
 
501
        operations to error.
 
502
        """
 
503
        self.finished = True
 
504
 
 
505
    def plan_merge(self, ver_a, ver_b):
 
506
        """Return pseudo-annotation indicating how the two versions merge.
 
507
 
 
508
        This is computed between versions a and b and their common
 
509
        base.
 
510
 
 
511
        Weave lines present in none of them are skipped entirely.
 
512
 
 
513
        Legend:
 
514
        killed-base Dead in base revision
 
515
        killed-both Killed in each revision
 
516
        killed-a    Killed in a
 
517
        killed-b    Killed in b
 
518
        unchanged   Alive in both a and b (possibly created in both)
 
519
        new-a       Created in a
 
520
        new-b       Created in b
 
521
        ghost-a     Killed in a, unborn in b    
 
522
        ghost-b     Killed in b, unborn in a
 
523
        irrelevant  Not in either revision
 
524
        """
 
525
        raise NotImplementedError(VersionedFile.plan_merge)
 
526
        
 
527
    def weave_merge(self, plan, a_marker=TextMerge.A_MARKER,
 
528
                    b_marker=TextMerge.B_MARKER):
 
529
        return PlanWeaveMerge(plan, a_marker, b_marker).merge_lines()[0]
 
530
 
 
531
 
 
532
class _PlanMergeVersionedFile(object):
 
533
    """A VersionedFile for uncommitted and committed texts.
 
534
 
 
535
    It is intended to allow merges to be planned with working tree texts.
 
536
    It implements only the small part of the VersionedFile interface used by
 
537
    PlanMerge.  It falls back to multiple versionedfiles for data not stored in
 
538
    _PlanMergeVersionedFile itself.
 
539
    """
 
540
 
 
541
    def __init__(self, file_id, fallback_versionedfiles=None):
 
542
        """Constuctor
 
543
 
 
544
        :param file_id: Used when raising exceptions.
 
545
        :param fallback_versionedfiles: If supplied, the set of fallbacks to
 
546
            use.  Otherwise, _PlanMergeVersionedFile.fallback_versionedfiles
 
547
            can be appended to later.
 
548
        """
 
549
        self._file_id = file_id
 
550
        if fallback_versionedfiles is None:
 
551
            self.fallback_versionedfiles = []
 
552
        else:
 
553
            self.fallback_versionedfiles = fallback_versionedfiles
 
554
        self._parents = {}
 
555
        self._lines = {}
 
556
 
 
557
    def plan_merge(self, ver_a, ver_b, base=None):
 
558
        """See VersionedFile.plan_merge"""
 
559
        from bzrlib.merge import _PlanMerge
 
560
        if base is None:
 
561
            return _PlanMerge(ver_a, ver_b, self).plan_merge()
 
562
        old_plan = list(_PlanMerge(ver_a, base, self).plan_merge())
 
563
        new_plan = list(_PlanMerge(ver_a, ver_b, self).plan_merge())
 
564
        return _PlanMerge._subtract_plans(old_plan, new_plan)
 
565
 
 
566
    def plan_lca_merge(self, ver_a, ver_b, base=None):
 
567
        from bzrlib.merge import _PlanLCAMerge
 
568
        graph = self._get_graph()
 
569
        new_plan = _PlanLCAMerge(ver_a, ver_b, self, graph).plan_merge()
 
570
        if base is None:
 
571
            return new_plan
 
572
        old_plan = _PlanLCAMerge(ver_a, base, self, graph).plan_merge()
 
573
        return _PlanLCAMerge._subtract_plans(list(old_plan), list(new_plan))
 
574
 
 
575
    def add_lines(self, version_id, parents, lines):
 
576
        """See VersionedFile.add_lines
 
577
 
 
578
        Lines are added locally, not fallback versionedfiles.  Also, ghosts are
 
579
        permitted.  Only reserved ids are permitted.
 
580
        """
 
581
        if not revision.is_reserved_id(version_id):
 
582
            raise ValueError('Only reserved ids may be used')
 
583
        if parents is None:
 
584
            raise ValueError('Parents may not be None')
 
585
        if lines is None:
 
586
            raise ValueError('Lines may not be None')
 
587
        self._parents[version_id] = tuple(parents)
 
588
        self._lines[version_id] = lines
 
589
 
 
590
    def get_lines(self, version_id):
 
591
        """See VersionedFile.get_ancestry"""
 
592
        lines = self._lines.get(version_id)
 
593
        if lines is not None:
 
594
            return lines
 
595
        for versionedfile in self.fallback_versionedfiles:
 
596
            try:
 
597
                return versionedfile.get_lines(version_id)
 
598
            except errors.RevisionNotPresent:
 
599
                continue
 
600
        else:
 
601
            raise errors.RevisionNotPresent(version_id, self._file_id)
 
602
 
 
603
    def get_ancestry(self, version_id, topo_sorted=False):
 
604
        """See VersionedFile.get_ancestry.
 
605
 
 
606
        Note that this implementation assumes that if a VersionedFile can
 
607
        answer get_ancestry at all, it can give an authoritative answer.  In
 
608
        fact, ghosts can invalidate this assumption.  But it's good enough
 
609
        99% of the time, and far cheaper/simpler.
 
610
 
 
611
        Also note that the results of this version are never topologically
 
612
        sorted, and are a set.
 
613
        """
 
614
        if topo_sorted:
 
615
            raise ValueError('This implementation does not provide sorting')
 
616
        parents = self._parents.get(version_id)
 
617
        if parents is None:
 
618
            for vf in self.fallback_versionedfiles:
 
619
                try:
 
620
                    return vf.get_ancestry(version_id, topo_sorted=False)
 
621
                except errors.RevisionNotPresent:
 
622
                    continue
 
623
            else:
 
624
                raise errors.RevisionNotPresent(version_id, self._file_id)
 
625
        ancestry = set([version_id])
 
626
        for parent in parents:
 
627
            ancestry.update(self.get_ancestry(parent, topo_sorted=False))
 
628
        return ancestry
 
629
 
 
630
    def get_parent_map(self, version_ids):
 
631
        """See VersionedFile.get_parent_map"""
 
632
        result = {}
 
633
        pending = set(version_ids)
 
634
        for key in version_ids:
 
635
            try:
 
636
                result[key] = self._parents[key]
 
637
            except KeyError:
 
638
                pass
 
639
        pending = pending - set(result.keys())
 
640
        for versionedfile in self.fallback_versionedfiles:
 
641
            parents = versionedfile.get_parent_map(pending)
 
642
            result.update(parents)
 
643
            pending = pending - set(parents.keys())
 
644
            if not pending:
 
645
                return result
 
646
        return result
 
647
 
 
648
    def _get_graph(self):
 
649
        from bzrlib.graph import (
 
650
            DictParentsProvider,
 
651
            Graph,
 
652
            _StackedParentsProvider,
 
653
            )
 
654
        from bzrlib.repofmt.knitrepo import _KnitParentsProvider
 
655
        parent_providers = [DictParentsProvider(self._parents)]
 
656
        for vf in self.fallback_versionedfiles:
 
657
            parent_providers.append(_KnitParentsProvider(vf))
 
658
        return Graph(_StackedParentsProvider(parent_providers))
 
659
 
 
660
 
 
661
class PlanWeaveMerge(TextMerge):
 
662
    """Weave merge that takes a plan as its input.
 
663
    
 
664
    This exists so that VersionedFile.plan_merge is implementable.
 
665
    Most callers will want to use WeaveMerge instead.
 
666
    """
 
667
 
 
668
    def __init__(self, plan, a_marker=TextMerge.A_MARKER,
 
669
                 b_marker=TextMerge.B_MARKER):
 
670
        TextMerge.__init__(self, a_marker, b_marker)
 
671
        self.plan = plan
 
672
 
 
673
    def _merge_struct(self):
 
674
        lines_a = []
 
675
        lines_b = []
 
676
        ch_a = ch_b = False
 
677
 
 
678
        def outstanding_struct():
 
679
            if not lines_a and not lines_b:
 
680
                return
 
681
            elif ch_a and not ch_b:
 
682
                # one-sided change:
 
683
                yield(lines_a,)
 
684
            elif ch_b and not ch_a:
 
685
                yield (lines_b,)
 
686
            elif lines_a == lines_b:
 
687
                yield(lines_a,)
 
688
            else:
 
689
                yield (lines_a, lines_b)
 
690
       
 
691
        # We previously considered either 'unchanged' or 'killed-both' lines
 
692
        # to be possible places to resynchronize.  However, assuming agreement
 
693
        # on killed-both lines may be too aggressive. -- mbp 20060324
 
694
        for state, line in self.plan:
 
695
            if state == 'unchanged':
 
696
                # resync and flush queued conflicts changes if any
 
697
                for struct in outstanding_struct():
 
698
                    yield struct
 
699
                lines_a = []
 
700
                lines_b = []
 
701
                ch_a = ch_b = False
 
702
                
 
703
            if state == 'unchanged':
 
704
                if line:
 
705
                    yield ([line],)
 
706
            elif state == 'killed-a':
 
707
                ch_a = True
 
708
                lines_b.append(line)
 
709
            elif state == 'killed-b':
 
710
                ch_b = True
 
711
                lines_a.append(line)
 
712
            elif state == 'new-a':
 
713
                ch_a = True
 
714
                lines_a.append(line)
 
715
            elif state == 'new-b':
 
716
                ch_b = True
 
717
                lines_b.append(line)
 
718
            elif state == 'conflicted-a':
 
719
                ch_b = ch_a = True
 
720
                lines_a.append(line)
 
721
            elif state == 'conflicted-b':
 
722
                ch_b = ch_a = True
 
723
                lines_b.append(line)
 
724
            else:
 
725
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 
 
726
                                 'killed-base', 'killed-both'), state
 
727
        for struct in outstanding_struct():
 
728
            yield struct
 
729
 
 
730
 
 
731
class WeaveMerge(PlanWeaveMerge):
 
732
    """Weave merge that takes a VersionedFile and two versions as its input."""
 
733
 
 
734
    def __init__(self, versionedfile, ver_a, ver_b, 
 
735
        a_marker=PlanWeaveMerge.A_MARKER, b_marker=PlanWeaveMerge.B_MARKER):
 
736
        plan = versionedfile.plan_merge(ver_a, ver_b)
 
737
        PlanWeaveMerge.__init__(self, plan, a_marker, b_marker)
 
738
 
 
739
 
 
740
class InterVersionedFile(InterObject):
 
741
    """This class represents operations taking place between two VersionedFiles.
 
742
 
 
743
    Its instances have methods like join, and contain
 
744
    references to the source and target versionedfiles these operations can be 
 
745
    carried out on.
 
746
 
 
747
    Often we will provide convenience methods on 'versionedfile' which carry out
 
748
    operations with another versionedfile - they will always forward to
 
749
    InterVersionedFile.get(other).method_name(parameters).
 
750
    """
 
751
 
 
752
    _optimisers = []
 
753
    """The available optimised InterVersionedFile types."""
 
754
 
 
755
    def join(self, pb=None, msg=None, version_ids=None, ignore_missing=False):
 
756
        """Integrate versions from self.source into self.target.
 
757
 
 
758
        If version_ids is None all versions from source should be
 
759
        incorporated into this versioned file.
 
760
 
 
761
        Must raise RevisionNotPresent if any of the specified versions
 
762
        are not present in the other file's history unless ignore_missing is 
 
763
        supplied in which case they are silently skipped.
 
764
        """
 
765
        # the default join: 
 
766
        # - if the target is empty, just add all the versions from 
 
767
        #   source to target, otherwise:
 
768
        # - make a temporary versioned file of type target
 
769
        # - insert the source content into it one at a time
 
770
        # - join them
 
771
        if not self.target.versions():
 
772
            target = self.target
 
773
        else:
 
774
            # Make a new target-format versioned file. 
 
775
            temp_source = self.target.create_empty("temp", MemoryTransport())
 
776
            target = temp_source
 
777
        version_ids = self._get_source_version_ids(version_ids, ignore_missing)
 
778
        graph = self.source.get_graph(version_ids)
 
779
        order = tsort.topo_sort(graph.items())
 
780
        pb = ui.ui_factory.nested_progress_bar()
 
781
        parent_texts = {}
 
782
        try:
 
783
            # TODO for incremental cross-format work:
 
784
            # make a versioned file with the following content:
 
785
            # all revisions we have been asked to join
 
786
            # all their ancestors that are *not* in target already.
 
787
            # the immediate parents of the above two sets, with 
 
788
            # empty parent lists - these versions are in target already
 
789
            # and the incorrect version data will be ignored.
 
790
            # TODO: for all ancestors that are present in target already,
 
791
            # check them for consistent data, this requires moving sha1 from
 
792
            # 
 
793
            # TODO: remove parent texts when they are not relevant any more for 
 
794
            # memory pressure reduction. RBC 20060313
 
795
            # pb.update('Converting versioned data', 0, len(order))
 
796
            total = len(order)
 
797
            parent_map = self.source.get_parent_map(order)
 
798
            for index, version in enumerate(order):
 
799
                pb.update('Converting versioned data', index, total)
 
800
                _, _, parent_text = target.add_lines(version,
 
801
                                               parent_map[version],
 
802
                                               self.source.get_lines(version),
 
803
                                               parent_texts=parent_texts)
 
804
                parent_texts[version] = parent_text
 
805
            
 
806
            # this should hit the native code path for target
 
807
            if target is not self.target:
 
808
                return self.target.join(temp_source,
 
809
                                        pb,
 
810
                                        msg,
 
811
                                        version_ids,
 
812
                                        ignore_missing)
 
813
            else:
 
814
                return total
 
815
        finally:
 
816
            pb.finished()
 
817
 
 
818
    def _get_source_version_ids(self, version_ids, ignore_missing):
 
819
        """Determine the version ids to be used from self.source.
 
820
 
 
821
        :param version_ids: The caller-supplied version ids to check. (None 
 
822
                            for all). If None is in version_ids, it is stripped.
 
823
        :param ignore_missing: if True, remove missing ids from the version 
 
824
                               list. If False, raise RevisionNotPresent on
 
825
                               a missing version id.
 
826
        :return: A set of version ids.
 
827
        """
 
828
        if version_ids is None:
 
829
            # None cannot be in source.versions
 
830
            return set(self.source.versions())
 
831
        else:
 
832
            if ignore_missing:
 
833
                return set(self.source.versions()).intersection(set(version_ids))
 
834
            else:
 
835
                new_version_ids = set()
 
836
                for version in version_ids:
 
837
                    if version is None:
 
838
                        continue
 
839
                    if not self.source.has_version(version):
 
840
                        raise errors.RevisionNotPresent(version, str(self.source))
 
841
                    else:
 
842
                        new_version_ids.add(version)
 
843
                return new_version_ids