/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.1.1 by Martin Pool
Check in old existing knit code.
1
#! /usr/bin/python
2
3
# Copyright (C) 2005 Canonical Ltd
4
0.1.33 by Martin Pool
add gpl text
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.
9
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.
14
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
0.1.1 by Martin Pool
Check in old existing knit code.
18
19
# Author: Martin Pool <mbp@canonical.com>
20
21
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
22
"""Weave - storage of related text file versions"""
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
23
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
24
# TODO: Perhaps have copy and comparison methods of Weave instances?
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
25
0.1.58 by Martin Pool
doc
26
# XXX: If we do weaves this way, will a merge still behave the same
27
# way if it's done in a different order?  That's a pretty desirable
28
# property.
29
30
0.1.34 by Martin Pool
remove dead code
31
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
32
class VerInfo(object):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
33
    """Information about a version in a Weave."""
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
34
    included = frozenset()
35
    def __init__(self, included=None):
36
        if included:
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
37
            self.included = frozenset(included)
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
38
0.1.18 by Martin Pool
Better Knit.dump method
39
    def __repr__(self):
40
        s = self.__class__.__name__ + '('
41
        if self.included:
42
            s += 'included=%r' % (list(self.included))
43
        s += ')'
44
        return s
45
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
46
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
47
class WeaveError(Exception):
48
    """Exception in processing weave"""
49
50
51
class WeaveFormatError(WeaveError):
52
    """Weave invariant violated"""
53
    
54
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
55
class Weave(object):
56
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
57
    
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
58
    A Weave manages versions of line-based text files, keeping track of the
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
59
    originating version for each line.
60
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
61
    Texts can be identified in either of two ways:
62
63
    * a nonnegative index number.
64
65
    * a version-id string.
66
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
67
    Typically the index number will be valid only inside this weave and
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
68
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
69
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
70
    The weave is represented as a list mixing edit instructions and
71
    literal text.  Each entry in _l can be either a string (or
72
    unicode), or a tuple.  If a string, it means that the given line
73
    should be output in the currently active revisions.
74
75
    If a tuple, it gives a processing instruction saying in which
76
    revisions the enclosed lines are active.  The tuple has the form
77
    (instruction, version).
78
79
    The instruction can be '{' or '}' for an insertion block, and '['
80
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
81
    integer version index.  There is no replace operator, only deletes
82
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
83
0.1.41 by Martin Pool
Doc
84
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
85
86
    * A later version can delete lines that were introduced by any
87
      number of ancestor versions; this implies that deletion
88
      instructions can span insertion blocks without regard to the
89
      insertion block's nesting.
90
0.1.41 by Martin Pool
Doc
91
    * Similarly, deletions need not be properly nested with regard to
92
      each other, because they might have been generated by
93
      independent revisions.
94
0.1.45 by Martin Pool
doc
95
    * Insertions are always made by inserting a new bracketed block
96
      into a single point in the previous weave.  This implies they
97
      can nest but not overlap, and the nesting must always have later
98
      insertions on the inside.
99
0.1.41 by Martin Pool
Doc
100
    * It doesn't seem very useful to have an active insertion
101
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
102
      
0.1.41 by Martin Pool
Doc
103
    * Therefore, all instructions are always"considered"; that
104
      is passed onto and off the stack.  An outer inactive block
105
      doesn't disable an inner block.
106
107
    * Lines are enabled if the most recent enclosing insertion is
108
      active and none of the enclosing deletions are active.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
109
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
110
    * There is no point having a deletion directly inside its own
111
      insertion; you might as well just not write it.  And there
112
      should be no way to get an earlier version deleting a later
113
      version.
114
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
115
    _l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
116
        Text of the weave. 
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
117
118
    _v
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
119
        List of versions, indexed by index number.
120
121
        For each version we store the tuple (included_versions), which
122
        lists the previous versions also considered active.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
123
    """
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
124
    def __init__(self):
125
        self._l = []
126
        self._v = []
0.1.5 by Martin Pool
Add test for storing two text versions.
127
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
128
        
0.1.26 by Martin Pool
Refactor parameters to add command
129
    def add(self, parents, text):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
130
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
131
  
0.1.26 by Martin Pool
Refactor parameters to add command
132
        Returns the index number of the newly added version.
133
134
        parents
135
            List or set of parent version numbers.
136
137
        text
138
            Sequence of lines to be added in the new version."""
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
139
        self._check_versions(parents)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
140
        self._check_lines(text)
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
141
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
142
        idx = len(self._v)
0.1.5 by Martin Pool
Add test for storing two text versions.
143
0.1.26 by Martin Pool
Refactor parameters to add command
144
        if parents:
145
            parents = frozenset(parents)
146
            delta = self._delta(parents, text)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
147
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
148
            # offset gives the number of lines that have been inserted
149
            # into the weave up to the current point; if the original edit instruction
150
            # says to change line A then we actually change (A+offset)
151
            offset = 0
152
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
153
            for i1, i2, newlines in delta:
0.1.29 by Martin Pool
Better internal error
154
                assert 0 <= i1
155
                assert i1 <= i2
156
                assert i2 <= len(self._l)
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
157
158
                # the deletion and insertion are handled separately.
159
                # first delete the region.
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
160
                if i1 != i2:
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
161
                    self._l.insert(i1+offset, ('[', idx))
162
                    self._l.insert(i2+offset+1, (']', idx))
163
                    offset += 2
164
                    # is this OK???
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
165
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
166
                if newlines:
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
167
                    # there may have been a deletion spanning up to
168
                    # i2; we want to insert after this region to make sure
169
                    # we don't destroy ourselves
170
                    i = i2 + offset
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
171
                    self._l[i:i] = [('{', idx)] \
172
                                   + newlines \
173
                                   + [('}', idx)]
174
                    offset += 2 + len(newlines)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
175
0.1.26 by Martin Pool
Refactor parameters to add command
176
            self._v.append(VerInfo(parents))
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
177
        else:
0.1.26 by Martin Pool
Refactor parameters to add command
178
            # special case; adding with no parents revision; can do this
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
179
            # more quickly by just appending unconditionally
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
180
            self._l.append(('{', idx))
181
            self._l += text
182
            self._l.append(('}', idx))
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
183
184
            self._v.append(VerInfo())
185
            
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
186
        return idx
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
187
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
188
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
189
    def _check_lines(self, text):
190
        if not isinstance(text, list):
191
            raise ValueError("text should be a list, not %s" % type(text))
192
193
        for l in text:
194
            if not isinstance(l, basestring):
195
                raise ValueError("text line should be a string or unicode, not %s" % type(l))
196
        
197
198
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
199
    def _check_versions(self, indexes):
200
        """Check everything in the sequence of indexes is valid"""
201
        for i in indexes:
202
            try:
203
                self._v[i]
204
            except IndexError:
205
                raise IndexError("invalid version number %r" % i)
206
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
207
    
0.1.7 by Martin Pool
Add trivial annotate text
208
    def annotate(self, index):
209
        return list(self.annotate_iter(index))
210
211
212
    def annotate_iter(self, index):
213
        """Yield list of (index-id, line) pairs for the specified version.
214
215
        The index indicates when the line originated in the weave."""
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
216
        try:
217
            vi = self._v[index]
218
        except IndexError:
219
            raise IndexError('version index %d out of range' % index)
0.1.20 by Martin Pool
Factor out Knit.extract() method
220
        included = set(vi.included)
221
        included.add(index)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
222
        for origin, lineno, text in self._extract(included):
223
            yield origin, text
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
224
225
226
    def _extract(self, included):
0.1.20 by Martin Pool
Factor out Knit.extract() method
227
        """Yield annotation of lines in included set.
228
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
229
        Yields a sequence of tuples (origin, lineno, text), where
230
        origin is the origin version, lineno the index in the weave,
231
        and text the text of the line.
232
0.1.20 by Martin Pool
Factor out Knit.extract() method
233
        The set typically but not necessarily corresponds to a version.
234
        """
0.1.48 by Martin Pool
Basic parsing of delete instructions.
235
        istack = []          # versions for which an insertion block is current
236
237
        dset = set()         # versions for which a deletion block is current
238
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
239
        isactive = False
0.1.48 by Martin Pool
Basic parsing of delete instructions.
240
241
        lineno = 0         # line of weave, 0-based
0.1.53 by Martin Pool
doc
242
243
        # TODO: Probably only need to put included revisions in the istack
244
245
        # TODO: Could split this into two functions, one that updates
246
        # the stack and the other that processes the results -- but
247
        # I'm not sure it's really needed.
0.1.20 by Martin Pool
Factor out Knit.extract() method
248
        
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
249
        for l in self._l:
250
            if isinstance(l, tuple):
251
                c, v = l
252
                if c == '{':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
253
                    if istack and (istack[-1] >= v):
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
254
                        raise WeaveFormatError("improperly nested insertions %d>=%d on line %d" 
0.1.48 by Martin Pool
Basic parsing of delete instructions.
255
                                               % (istack[-1], v, lineno))
256
                    istack.append(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
257
                elif c == '}':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
258
                    try:
259
                        oldv = istack.pop()
260
                    except IndexError:
261
                        raise WeaveFormatError("unmatched close of insertion %d on line %d"
262
                                               % (v, lineno))
263
                    if oldv != v:
264
                        raise WeaveFormatError("mismatched close of insertion %d!=%d on line %d"
265
                                               % (oldv, v, lineno))
266
                elif c == '[':
267
                    # block deleted in v
268
                    if v in dset:
269
                        raise WeaveFormatError("repeated deletion marker for version %d on line %d"
270
                                               % (v, lineno))
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
271
                    if istack:
272
                        if istack[-1] == v:
273
                            raise WeaveFormatError("version %d deletes own text on line %d"
274
                                                   % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
275
                        dset.add(v)
276
                elif c == ']':
277
                    if v in dset:
278
                        dset.remove(v)
279
                    else:
280
                        raise WeaveFormatError("unmatched close of deletion %d on line %d"
281
                                               % (v, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
282
                else:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
283
                    raise WeaveFormatError("invalid processing instruction %r on line %d"
284
                                           % (l, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
285
            else:
286
                assert isinstance(l, basestring)
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
287
                if not istack:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
288
                    raise WeaveFormatError("literal at top level on line %d"
289
                                           % lineno)
0.1.50 by Martin Pool
Basic implementation of deletion markers
290
                isactive = (istack[-1] in included) \
291
                           and not included.intersection(dset)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
292
                if isactive:
0.1.48 by Martin Pool
Basic parsing of delete instructions.
293
                    origin = istack[-1]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
294
                    yield origin, lineno, l
295
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
296
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
297
        if istack:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
298
            raise WeaveFormatError("unclosed insertion blocks at end of weave",
299
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
300
        if dset:
301
            raise WeaveFormatError("unclosed deletion blocks at end of weave",
302
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
303
0.1.7 by Martin Pool
Add trivial annotate text
304
0.1.5 by Martin Pool
Add test for storing two text versions.
305
    def getiter(self, index):
306
        """Yield lines for the specified version."""
0.1.8 by Martin Pool
Unify get/annotate code
307
        for origin, line in self.annotate_iter(index):
308
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
309
310
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
311
    def get(self, index):
0.1.5 by Martin Pool
Add test for storing two text versions.
312
        return list(self.getiter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
313
314
0.1.11 by Martin Pool
Add Knit.dump method
315
    def dump(self, to_file):
316
        from pprint import pprint
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
317
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
318
        pprint(self._l, to_file)
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
319
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
320
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
321
322
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
323
    def check(self):
324
        for vers_info in self._v:
325
            included = set()
326
            for vi in vers_info[0]:
327
                if vi < 0 or vi >= index:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
328
                    raise WeaveFormatError("invalid included version %d for index %d"
329
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
330
                if vi in included:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
331
                    raise WeaveFormatError("repeated included version %d for index %d"
332
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
333
                included.add(vi)
0.1.18 by Martin Pool
Better Knit.dump method
334
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
335
336
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
337
    def _delta(self, included, lines):
338
        """Return changes from basis to new revision.
339
340
        The old text for comparison is the union of included revisions.
341
342
        This is used in inserting a new text.
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
343
0.1.55 by Martin Pool
doc
344
        Delta is returned as a sequence of
345
        (weave1, weave2, newlines).
346
347
        This indicates that weave1:weave2 of the old weave should be
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
348
        replaced by the sequence of lines in newlines.  Note that
349
        these line numbers are positions in the total weave and don't
350
        correspond to the lines in any extracted version, or even the
351
        extracted union of included versions.
352
353
        If line1=line2, this is a pure insert; if newlines=[] this is a
354
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
355
        """
356
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
357
        self._check_versions(included)
358
0.1.23 by Martin Pool
tidy up
359
        ##from pprint import pprint
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
360
361
        # first get basis for comparison
362
        # basis holds (lineno, origin, line)
363
        basis = []
364
0.1.23 by Martin Pool
tidy up
365
        ##print 'my lines:'
366
        ##pprint(self._l)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
367
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
368
        # basis a list of (origin, lineno, line)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
369
        basis = list(self._extract(included))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
370
371
        # now make a parallel list with only the text, to pass to the differ
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
372
        basis_lines = [line for (origin, lineno, line) in basis]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
373
374
        # add a sentinal, because we can also match against the final line
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
375
        basis.append((None, len(self._l), None))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
376
377
        # XXX: which line of the weave should we really consider matches the end of the file?
378
        # the current code says it's the last line of the weave?
379
380
        from difflib import SequenceMatcher
381
        s = SequenceMatcher(None, basis_lines, lines)
382
0.1.23 by Martin Pool
tidy up
383
        ##print 'basis sequence:'
384
        ##pprint(basis)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
385
0.1.55 by Martin Pool
doc
386
        # TODO: Perhaps return line numbers from composed weave as well?
387
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
388
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
389
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
390
391
            if tag == 'equal':
392
                continue
393
394
            # i1,i2 are given in offsets within basis_lines; we need to map them
395
            # back to offsets within the entire weave
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
396
            real_i1 = basis[i1][1]
397
            real_i2 = basis[i2][1]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
398
0.1.35 by Martin Pool
Clean up Knit._delta method
399
            assert 0 <= j1
400
            assert j1 <= j2
401
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
402
0.1.35 by Martin Pool
Clean up Knit._delta method
403
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
404
0.1.1 by Martin Pool
Check in old existing knit code.
405