67
67
# FIXME: the conflict markers should be *7* characters
69
69
from copy import copy
70
from cStringIO import StringIO
73
from bzrlib.lazy_import import lazy_import
72
from ..lazy_import import lazy_import
74
73
lazy_import(globals(), """
75
from bzrlib import tsort
74
from breezy import tsort
81
from bzrlib.errors import (WeaveError, WeaveFormatError, WeaveParentMismatch,
82
RevisionAlreadyPresent,
84
UnavailableRepresentation,
86
from bzrlib.osutils import dirname, sha, sha_strings, split_lines
87
import bzrlib.patiencediff
88
from bzrlib.revision import NULL_REVISION
89
from bzrlib.symbol_versioning import *
90
from bzrlib.trace import mutter
91
from bzrlib.versionedfile import (
80
from ..errors import (
81
RevisionAlreadyPresent,
83
UnavailableRepresentation,
85
from ..osutils import dirname, sha, sha_strings, split_lines
86
from .. import patiencediff
87
from ..revision import NULL_REVISION
88
from ..sixish import (
91
from ..trace import mutter
92
from .versionedfile import (
92
93
AbsentContentFactory,
95
96
sort_groupcompress,
98
from bzrlib.weavefile import _read_weave_v5, write_weave_v5
99
from .weavefile import _read_weave_v5, write_weave_v5
102
class WeaveError(errors.BzrError):
104
_fmt = "Error in processing weave: %(msg)s"
106
def __init__(self, msg=None):
107
errors.BzrError.__init__(self)
111
class WeaveRevisionAlreadyPresent(WeaveError):
113
_fmt = "Revision {%(revision_id)s} already present in %(weave)s"
115
def __init__(self, revision_id, weave):
117
WeaveError.__init__(self)
118
self.revision_id = revision_id
122
class WeaveRevisionNotPresent(WeaveError):
124
_fmt = "Revision {%(revision_id)s} not present in %(weave)s"
126
def __init__(self, revision_id, weave):
127
WeaveError.__init__(self)
128
self.revision_id = revision_id
132
class WeaveFormatError(WeaveError):
134
_fmt = "Weave invariant violated: %(what)s"
136
def __init__(self, what):
137
WeaveError.__init__(self)
141
class WeaveParentMismatch(WeaveError):
143
_fmt = "Parents are mismatched between two revisions. %(msg)s"
146
class WeaveInvalidChecksum(WeaveError):
148
_fmt = "Text did not match its checksum: %(msg)s"
151
class WeaveTextDiffers(WeaveError):
153
_fmt = ("Weaves differ on text content. Revision:"
154
" {%(revision_id)s}, %(weave_a)s, %(weave_b)s")
156
def __init__(self, revision_id, weave_a, weave_b):
157
WeaveError.__init__(self)
158
self.revision_id = revision_id
159
self.weave_a = weave_a
160
self.weave_b = weave_b
163
class WeaveTextDiffers(WeaveError):
165
_fmt = ("Weaves differ on text content. Revision:"
166
" {%(revision_id)s}, %(weave_a)s, %(weave_b)s")
168
def __init__(self, revision_id, weave_a, weave_b):
169
WeaveError.__init__(self)
170
self.revision_id = revision_id
171
self.weave_a = weave_a
172
self.weave_b = weave_b
101
175
class WeaveContentFactory(ContentFactory):
391
465
def _add_lines(self, version_id, parents, lines, parent_texts,
392
466
left_matching_blocks, nostore_sha, random_id, check_content):
393
467
"""See VersionedFile.add_lines."""
394
idx = self._add(version_id, lines, map(self._lookup, parents),
468
idx = self._add(version_id, lines, list(map(self._lookup, parents)),
395
469
nostore_sha=nostore_sha)
396
470
return sha_strings(lines), sum(map(len, lines)), idx
437
510
self._names.append(version_id)
438
511
self._name_map[version_id] = new_version
442
514
# special case; adding with no parents revision; can do
443
515
# this more quickly by just appending unconditionally.
444
516
# even more specially, if we're adding an empty text we
445
517
# need do nothing at all.
447
self._weave.append(('{', new_version))
519
self._weave.append((b'{', new_version))
448
520
self._weave.extend(lines)
449
self._weave.append(('}', None))
521
self._weave.append((b'}', None))
450
522
return new_version
452
524
if len(parents) == 1:
520
592
if not len(versions):
522
594
i = set(versions)
523
for v in xrange(max(versions), 0, -1):
595
for v in range(max(versions), 0, -1):
525
597
# include all its parents
526
598
i.update(self._parents[v])
528
## except IndexError:
529
## raise ValueError("version %d not present in weave" % v)
531
601
def get_ancestry(self, version_ids, topo_sorted=True):
532
602
"""See VersionedFile.get_ancestry."""
533
if isinstance(version_ids, basestring):
603
if isinstance(version_ids, bytes):
534
604
version_ids = [version_ids]
535
605
i = self._inclusions([self._lookup(v) for v in version_ids])
536
606
return [self._idx_to_name(v) for v in i]
538
def _check_lines(self, text):
539
if not isinstance(text, list):
540
raise ValueError("text should be a list, not %s" % type(text))
543
if not isinstance(l, basestring):
544
raise ValueError("text line should be a string or unicode, not %s"
549
608
def _check_versions(self, indexes):
550
609
"""Check everything in the sequence of indexes is valid"""
551
610
for i in indexes:
688
747
WFE = WeaveFormatError
691
# 449 0 4474.6820 2356.5590 bzrlib.weave:556(_extract)
750
# 449 0 4474.6820 2356.5590 breezy.weave:556(_extract)
692
751
# +285282 0 1676.8040 1676.8040 +<isinstance>
693
752
# 1.6 seconds in 'isinstance'.
694
753
# changing the first isinstance:
695
# 449 0 2814.2660 1577.1760 bzrlib.weave:556(_extract)
754
# 449 0 2814.2660 1577.1760 breezy.weave:556(_extract)
696
755
# +140414 0 762.8050 762.8050 +<isinstance>
697
756
# note that the inline time actually dropped (less function calls)
698
757
# and total processing time was halved.
699
758
# we're still spending ~1/4 of the method in isinstance though.
700
759
# so lets hard code the acceptable string classes we expect:
701
# 449 0 1202.9420 786.2930 bzrlib.weave:556(_extract)
760
# 449 0 1202.9420 786.2930 breezy.weave:556(_extract)
702
761
# +71352 0 377.5560 377.5560 +<method 'append' of 'list'
704
763
# yay, down to ~1/4 the initial extract time, and our inline time
705
764
# has shrunk again, with isinstance no longer dominating.
706
765
# tweaking the stack inclusion test to use a set gives:
707
# 449 0 1122.8030 713.0080 bzrlib.weave:556(_extract)
766
# 449 0 1122.8030 713.0080 breezy.weave:556(_extract)
708
767
# +71352 0 354.9980 354.9980 +<method 'append' of 'list'
710
769
# - a 5% win, or possibly just noise. However with large istacks that
711
770
# 'in' test could dominate, so I'm leaving this change in place -
712
771
# when its fast enough to consider profiling big datasets we can review.
717
773
for l in self._weave:
718
774
if l.__class__ == tuple:
725
781
iset.remove(istack.pop())
727
783
if v in included:
730
786
if v in included:
875
933
this_idx = self._name_map.get(name, -1)
876
934
if this_idx != -1:
877
935
if self._sha1s[this_idx] != other._sha1s[other_idx]:
878
raise errors.WeaveTextDiffers(name, self, other)
936
raise WeaveTextDiffers(name, self, other)
879
937
self_parents = self._parents[this_idx]
880
938
other_parents = other._parents[other_idx]
881
n1 = set([self._names[i] for i in self_parents])
882
n2 = set([other._names[i] for i in other_parents])
939
n1 = {self._names[i] for i in self_parents}
940
n2 = {other._names[i] for i in other_parents}
883
941
if not self._compatible_parents(n1, n2):
884
942
raise WeaveParentMismatch("inconsistent parents "
885
943
"for version {%s}: %s vs %s" % (name, n1, n2))
985
1044
:param msg: An optional message for the progress
989
queue_a = range(wa.num_versions())
990
queue_b = range(wb.num_versions())
991
1047
# first determine combined parents of all versions
992
1048
# map from version name -> all parent names
993
1049
combined_parents = _reweave_parent_graphs(wa, wb)
994
1050
mutter("combined parents: %r", combined_parents)
995
order = tsort.topo_sort(combined_parents.iteritems())
1051
order = tsort.topo_sort(combined_parents.items())
996
1052
mutter("order to reweave: %r", order)
998
1054
if pb and not msg:
1012
1068
lines = list(difflib.unified_diff(lines, lines_b,
1013
1069
wa._weave_name, wb._weave_name))
1014
1070
mutter('lines:\n%s', ''.join(lines))
1015
raise errors.WeaveTextDiffers(name, wa, wb)
1071
raise WeaveTextDiffers(name, wa, wb)
1017
1073
lines = wb.get_lines(name)
1018
1074
wr._add(name, lines, [wr._lookup(i) for i in combined_parents[name]])