/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.61 by Martin Pool
doc
24
# TODO: Perhaps have copy method for 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
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
30
# TODO: How to write these to disk?  One option is cPickle, which
31
# would be fast but less friendly to C, and perhaps not portable.  Another is
32
33
# TODO: Nothing here so far assumes the lines are really \n newlines,
34
# rather than being split up in some other way.  We could accomodate
35
# binaries, perhaps by naively splitting on \n or perhaps using
36
# something like a rolling checksum.
37
38
# TODO: Perhaps track SHA-1 in the header for protection?  This would
39
# be redundant with it being stored in the inventory, but perhaps
40
# usefully so?
41
42
# TODO: Track version names as well as indexes. 
43
44
# TODO: Probably do transitive expansion when specifying parents?
0.1.58 by Martin Pool
doc
45
0.1.34 by Martin Pool
remove dead code
46
0.1.66 by Martin Pool
Cope without set/frozenset classes
47
try:
48
    set
49
    frozenset
50
except NameError:
51
    from sets import Set, ImmutableSet
52
    set = Set
53
    frozenset = ImmutableSet
54
    del Set, FrozenSet
55
56
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
57
class VerInfo(object):
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
58
    """Information about a version in a Weave."""
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
59
    included = frozenset()
60
    def __init__(self, included=None):
61
        if included:
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
62
            self.included = frozenset(included)
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
63
0.1.18 by Martin Pool
Better Knit.dump method
64
    def __repr__(self):
65
        s = self.__class__.__name__ + '('
66
        if self.included:
67
            s += 'included=%r' % (list(self.included))
68
        s += ')'
69
        return s
70
0.1.17 by Martin Pool
Use objects rather than tuples for tracking VerInfo for
71
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
72
class WeaveError(Exception):
73
    """Exception in processing weave"""
74
75
76
class WeaveFormatError(WeaveError):
77
    """Weave invariant violated"""
78
    
79
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
80
class Weave(object):
81
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
82
    
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
83
    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.
84
    originating version for each line.
85
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
86
    Texts can be identified in either of two ways:
87
88
    * a nonnegative index number.
89
90
    * a version-id string.
91
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
92
    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.
93
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
94
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
95
    The weave is represented as a list mixing edit instructions and
96
    literal text.  Each entry in _l can be either a string (or
97
    unicode), or a tuple.  If a string, it means that the given line
98
    should be output in the currently active revisions.
99
100
    If a tuple, it gives a processing instruction saying in which
101
    revisions the enclosed lines are active.  The tuple has the form
102
    (instruction, version).
103
104
    The instruction can be '{' or '}' for an insertion block, and '['
105
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
106
    integer version index.  There is no replace operator, only deletes
107
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
108
0.1.41 by Martin Pool
Doc
109
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
110
111
    * A later version can delete lines that were introduced by any
112
      number of ancestor versions; this implies that deletion
113
      instructions can span insertion blocks without regard to the
114
      insertion block's nesting.
115
0.1.41 by Martin Pool
Doc
116
    * Similarly, deletions need not be properly nested with regard to
117
      each other, because they might have been generated by
118
      independent revisions.
119
0.1.45 by Martin Pool
doc
120
    * Insertions are always made by inserting a new bracketed block
121
      into a single point in the previous weave.  This implies they
122
      can nest but not overlap, and the nesting must always have later
123
      insertions on the inside.
124
0.1.41 by Martin Pool
Doc
125
    * It doesn't seem very useful to have an active insertion
126
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
127
      
0.1.41 by Martin Pool
Doc
128
    * Therefore, all instructions are always"considered"; that
129
      is passed onto and off the stack.  An outer inactive block
130
      doesn't disable an inner block.
131
132
    * Lines are enabled if the most recent enclosing insertion is
133
      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
134
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
135
    * There is no point having a deletion directly inside its own
136
      insertion; you might as well just not write it.  And there
137
      should be no way to get an earlier version deleting a later
138
      version.
139
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
140
    _l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
141
        Text of the weave. 
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
142
143
    _v
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
144
        List of versions, indexed by index number.
145
146
        For each version we store the tuple (included_versions), which
147
        lists the previous versions also considered active.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
148
    """
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
149
    def __init__(self):
150
        self._l = []
151
        self._v = []
0.1.5 by Martin Pool
Add test for storing two text versions.
152
0.1.60 by Martin Pool
Weave eq and ne methods
153
154
155
    def __eq__(self, other):
156
        if not isinstance(other, Weave):
157
            return False
158
        return self._v == other._v \
159
               and self._l == other._l
160
    
161
162
    def __ne__(self, other):
163
        return not self.__eq__(other)
164
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
165
        
0.1.26 by Martin Pool
Refactor parameters to add command
166
    def add(self, parents, text):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
167
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
168
  
0.1.26 by Martin Pool
Refactor parameters to add command
169
        Returns the index number of the newly added version.
170
171
        parents
0.1.64 by Martin Pool
Add test for merging versions
172
            List or set of parent version numbers.  This must normally include
173
            the parents and the parent's parents, or wierd things might happen.
0.1.26 by Martin Pool
Refactor parameters to add command
174
175
        text
176
            Sequence of lines to be added in the new version."""
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
177
        self._check_versions(parents)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
178
        self._check_lines(text)
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
179
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
180
        idx = len(self._v)
0.1.5 by Martin Pool
Add test for storing two text versions.
181
0.1.26 by Martin Pool
Refactor parameters to add command
182
        if parents:
183
            parents = frozenset(parents)
184
            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
185
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
186
            # offset gives the number of lines that have been inserted
187
            # into the weave up to the current point; if the original edit instruction
188
            # says to change line A then we actually change (A+offset)
189
            offset = 0
190
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
191
            for i1, i2, newlines in delta:
0.1.29 by Martin Pool
Better internal error
192
                assert 0 <= i1
193
                assert i1 <= i2
194
                assert i2 <= len(self._l)
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
195
196
                # the deletion and insertion are handled separately.
197
                # first delete the region.
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
198
                if i1 != i2:
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
199
                    self._l.insert(i1+offset, ('[', idx))
200
                    self._l.insert(i2+offset+1, (']', idx))
201
                    offset += 2
202
                    # is this OK???
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
203
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
204
                if newlines:
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
205
                    # there may have been a deletion spanning up to
206
                    # i2; we want to insert after this region to make sure
207
                    # we don't destroy ourselves
208
                    i = i2 + offset
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
209
                    self._l[i:i] = [('{', idx)] \
210
                                   + newlines \
211
                                   + [('}', idx)]
212
                    offset += 2 + len(newlines)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
213
0.1.26 by Martin Pool
Refactor parameters to add command
214
            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
215
        else:
0.1.26 by Martin Pool
Refactor parameters to add command
216
            # 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
217
            # more quickly by just appending unconditionally
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
218
            self._l.append(('{', idx))
219
            self._l += text
220
            self._l.append(('}', idx))
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
221
222
            self._v.append(VerInfo())
223
            
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
224
        return idx
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
225
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
226
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
227
    def _check_lines(self, text):
228
        if not isinstance(text, list):
229
            raise ValueError("text should be a list, not %s" % type(text))
230
231
        for l in text:
232
            if not isinstance(l, basestring):
233
                raise ValueError("text line should be a string or unicode, not %s" % type(l))
234
        
235
236
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
237
    def _check_versions(self, indexes):
238
        """Check everything in the sequence of indexes is valid"""
239
        for i in indexes:
240
            try:
241
                self._v[i]
242
            except IndexError:
243
                raise IndexError("invalid version number %r" % i)
244
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
245
    
0.1.7 by Martin Pool
Add trivial annotate text
246
    def annotate(self, index):
247
        return list(self.annotate_iter(index))
248
249
250
    def annotate_iter(self, index):
251
        """Yield list of (index-id, line) pairs for the specified version.
252
253
        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
254
        try:
255
            vi = self._v[index]
256
        except IndexError:
257
            raise IndexError('version index %d out of range' % index)
0.1.20 by Martin Pool
Factor out Knit.extract() method
258
        included = set(vi.included)
259
        included.add(index)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
260
        for origin, lineno, text in self._extract(included):
261
            yield origin, text
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
262
263
264
    def _extract(self, included):
0.1.20 by Martin Pool
Factor out Knit.extract() method
265
        """Yield annotation of lines in included set.
266
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
267
        Yields a sequence of tuples (origin, lineno, text), where
268
        origin is the origin version, lineno the index in the weave,
269
        and text the text of the line.
270
0.1.20 by Martin Pool
Factor out Knit.extract() method
271
        The set typically but not necessarily corresponds to a version.
272
        """
0.1.48 by Martin Pool
Basic parsing of delete instructions.
273
        istack = []          # versions for which an insertion block is current
274
275
        dset = set()         # versions for which a deletion block is current
276
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
277
        isactive = False
0.1.48 by Martin Pool
Basic parsing of delete instructions.
278
279
        lineno = 0         # line of weave, 0-based
0.1.53 by Martin Pool
doc
280
281
        # TODO: Probably only need to put included revisions in the istack
282
283
        # TODO: Could split this into two functions, one that updates
284
        # the stack and the other that processes the results -- but
285
        # I'm not sure it's really needed.
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
286
287
        WFE = WeaveFormatError
0.1.20 by Martin Pool
Factor out Knit.extract() method
288
        
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
289
        for l in self._l:
290
            if isinstance(l, tuple):
291
                c, v = l
292
                if c == '{':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
293
                    if istack and (istack[-1] >= v):
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
294
                        raise WFE("improperly nested insertions %d>=%d on line %d" 
295
                                  % (istack[-1], v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
296
                    istack.append(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
297
                elif c == '}':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
298
                    try:
299
                        oldv = istack.pop()
300
                    except IndexError:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
301
                        raise WFE("unmatched close of insertion %d on line %d"
302
                                  % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
303
                    if oldv != v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
304
                        raise WFE("mismatched close of insertion %d!=%d on line %d"
305
                                  % (oldv, v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
306
                elif c == '[':
307
                    # block deleted in v
308
                    if v in dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
309
                        raise WFE("repeated deletion marker for version %d on line %d"
310
                                  % (v, lineno))
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
311
                    if istack:
312
                        if istack[-1] == v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
313
                            raise WFE("version %d deletes own text on line %d"
314
                                      % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
315
                        dset.add(v)
316
                elif c == ']':
317
                    if v in dset:
318
                        dset.remove(v)
319
                    else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
320
                        raise WFE("unmatched close of deletion %d on line %d"
321
                                  % (v, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
322
                else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
323
                    raise WFE("invalid processing instruction %r on line %d"
324
                              % (l, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
325
            else:
326
                assert isinstance(l, basestring)
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
327
                if not istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
328
                    raise WFE("literal at top level on line %d"
329
                              % lineno)
0.1.50 by Martin Pool
Basic implementation of deletion markers
330
                isactive = (istack[-1] in included) \
331
                           and not included.intersection(dset)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
332
                if isactive:
0.1.48 by Martin Pool
Basic parsing of delete instructions.
333
                    origin = istack[-1]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
334
                    yield origin, lineno, l
335
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
336
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
337
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
338
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
339
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
340
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
341
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
342
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
343
0.1.7 by Martin Pool
Add trivial annotate text
344
0.1.5 by Martin Pool
Add test for storing two text versions.
345
    def getiter(self, index):
346
        """Yield lines for the specified version."""
0.1.8 by Martin Pool
Unify get/annotate code
347
        for origin, line in self.annotate_iter(index):
348
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
349
350
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
351
    def get(self, index):
0.1.5 by Martin Pool
Add test for storing two text versions.
352
        return list(self.getiter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
353
354
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
355
    def merge_iter(self, included):
356
        """Return composed version of multiple included versions."""
357
        included = frozenset(included)
358
        for origin, lineno, text in self._extract(included):
359
            yield text
360
361
0.1.11 by Martin Pool
Add Knit.dump method
362
    def dump(self, to_file):
363
        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.)
364
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
365
        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.)
366
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
367
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
368
369
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
370
    def check(self):
371
        for vers_info in self._v:
372
            included = set()
373
            for vi in vers_info[0]:
374
                if vi < 0 or vi >= index:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
375
                    raise WeaveFormatError("invalid included version %d for index %d"
376
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
377
                if vi in included:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
378
                    raise WeaveFormatError("repeated included version %d for index %d"
379
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
380
                included.add(vi)
0.1.18 by Martin Pool
Better Knit.dump method
381
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
382
383
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
384
    def _delta(self, included, lines):
385
        """Return changes from basis to new revision.
386
387
        The old text for comparison is the union of included revisions.
388
389
        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.
390
0.1.55 by Martin Pool
doc
391
        Delta is returned as a sequence of
392
        (weave1, weave2, newlines).
393
394
        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.
395
        replaced by the sequence of lines in newlines.  Note that
396
        these line numbers are positions in the total weave and don't
397
        correspond to the lines in any extracted version, or even the
398
        extracted union of included versions.
399
400
        If line1=line2, this is a pure insert; if newlines=[] this is a
401
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
402
        """
403
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
404
        self._check_versions(included)
405
0.1.23 by Martin Pool
tidy up
406
        ##from pprint import pprint
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
407
408
        # first get basis for comparison
409
        # basis holds (lineno, origin, line)
410
        basis = []
411
0.1.23 by Martin Pool
tidy up
412
        ##print 'my lines:'
413
        ##pprint(self._l)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
414
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
415
        # 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
416
        basis = list(self._extract(included))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
417
418
        # 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
419
        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.
420
421
        # 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
422
        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.
423
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
424
        # XXX: which line of the weave should we really consider
425
        # matches the end of the file?  the current code says it's the
426
        # last line of the weave?
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
427
428
        from difflib import SequenceMatcher
429
        s = SequenceMatcher(None, basis_lines, lines)
430
0.1.23 by Martin Pool
tidy up
431
        ##print 'basis sequence:'
432
        ##pprint(basis)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
433
0.1.55 by Martin Pool
doc
434
        # TODO: Perhaps return line numbers from composed weave as well?
435
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
436
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
437
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
438
439
            if tag == 'equal':
440
                continue
441
442
            # i1,i2 are given in offsets within basis_lines; we need to map them
443
            # back to offsets within the entire weave
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
444
            real_i1 = basis[i1][1]
445
            real_i2 = basis[i2][1]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
446
0.1.35 by Martin Pool
Clean up Knit._delta method
447
            assert 0 <= j1
448
            assert j1 <= j2
449
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
450
0.1.35 by Martin Pool
Clean up Knit._delta method
451
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
452
0.1.1 by Martin Pool
Check in old existing knit code.
453
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
454
455
456
def main(argv):
457
    import sys
458
    import os
459
    from cPickle import dump, load
460
    cmd = argv[1]
461
    if cmd == 'add':
462
        w = load(file(argv[2], 'rb'))
463
        # at the moment, based on everything in the file
464
        parents = set(range(len(w._v)))
465
        ver = w.add(parents, sys.stdin.readlines())
466
        dump(w, file(argv[2], 'wb'))
467
        print 'added %d' % ver
468
    elif cmd == 'init':
469
        fn = argv[2]
470
        if os.path.exists(fn):
471
            raise IOError("file exists")
472
        w = Weave()
473
        dump(w, file(fn, 'wb'))
474
    elif cmd == 'get':
475
        w = load(file(argv[2], 'rb'))
476
        sys.stdout.writelines(w.get(int(argv[3])))
477
    elif cmd == 'annotate':
478
        w = load(file(argv[2], 'rb'))
479
        # assumes lines are ended
480
        lasto = None
481
        for origin, text in w.annotate(int(argv[3])):
482
            if text[-1] == '\n':
483
                text = text[:-1]
484
            if origin == lasto:
485
                print '      | %s' % (text)
486
            else:
487
                print '%5d | %s' % (origin, text)
488
                lasto = origin
489
    else:
490
        raise ValueError('unknown command %r' % cmd)
491
    
492
493
if __name__ == '__main__':
494
    import sys
495
    sys.exit(main(sys.argv))