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