/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.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
64
class WeaveError(Exception):
65
    """Exception in processing weave"""
66
67
68
class WeaveFormatError(WeaveError):
69
    """Weave invariant violated"""
70
    
71
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
72
class Weave(object):
73
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
74
    
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
75
    A Weave manages versions of line-based text files, keeping track
76
    of the originating version for each line.
77
78
    To clients the "lines" of the file are represented as a list of strings.
79
    These strings  will typically have terminal newline characters, but
80
    this is not required.  In particular files commonly do not have a newline
81
    at the end of the file.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
82
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
83
    Texts can be identified in either of two ways:
84
85
    * a nonnegative index number.
86
87
    * a version-id string.
88
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
89
    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.
90
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
91
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
92
    The weave is represented as a list mixing edit instructions and
93
    literal text.  Each entry in _l can be either a string (or
94
    unicode), or a tuple.  If a string, it means that the given line
95
    should be output in the currently active revisions.
96
97
    If a tuple, it gives a processing instruction saying in which
98
    revisions the enclosed lines are active.  The tuple has the form
99
    (instruction, version).
100
101
    The instruction can be '{' or '}' for an insertion block, and '['
102
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
103
    integer version index.  There is no replace operator, only deletes
104
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
105
0.1.41 by Martin Pool
Doc
106
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
107
108
    * A later version can delete lines that were introduced by any
109
      number of ancestor versions; this implies that deletion
110
      instructions can span insertion blocks without regard to the
111
      insertion block's nesting.
112
0.1.41 by Martin Pool
Doc
113
    * Similarly, deletions need not be properly nested with regard to
114
      each other, because they might have been generated by
115
      independent revisions.
116
0.1.45 by Martin Pool
doc
117
    * Insertions are always made by inserting a new bracketed block
118
      into a single point in the previous weave.  This implies they
119
      can nest but not overlap, and the nesting must always have later
120
      insertions on the inside.
121
0.1.41 by Martin Pool
Doc
122
    * It doesn't seem very useful to have an active insertion
123
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
124
      
0.1.41 by Martin Pool
Doc
125
    * Therefore, all instructions are always"considered"; that
126
      is passed onto and off the stack.  An outer inactive block
127
      doesn't disable an inner block.
128
129
    * Lines are enabled if the most recent enclosing insertion is
130
      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
131
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
132
    * There is no point having a deletion directly inside its own
133
      insertion; you might as well just not write it.  And there
134
      should be no way to get an earlier version deleting a later
135
      version.
136
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
137
    _l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
138
        Text of the weave. 
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
139
140
    _v
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
141
        List of versions, indexed by index number.
142
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
143
        For each version we store the set (included_versions), which
144
        lists the previous versions also considered active; the
145
        versions included in those versions are included transitively.
146
        So new versions created from nothing list []; most versions
147
        have a single entry; some have more.
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:
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
183
            delta = self._delta(self.inclusions(parents), text)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
184
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
185
            # offset gives the number of lines that have been inserted
186
            # into the weave up to the current point; if the original edit instruction
187
            # says to change line A then we actually change (A+offset)
188
            offset = 0
189
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
190
            for i1, i2, newlines in delta:
0.1.29 by Martin Pool
Better internal error
191
                assert 0 <= i1
192
                assert i1 <= i2
193
                assert i2 <= len(self._l)
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
194
195
                # the deletion and insertion are handled separately.
196
                # first delete the region.
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
197
                if i1 != i2:
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
198
                    self._l.insert(i1+offset, ('[', idx))
199
                    self._l.insert(i2+offset+1, (']', idx))
200
                    offset += 2
201
                    # is this OK???
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
202
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
203
                if newlines:
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
204
                    # there may have been a deletion spanning up to
205
                    # i2; we want to insert after this region to make sure
206
                    # we don't destroy ourselves
207
                    i = i2 + offset
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
208
                    self._l[i:i] = [('{', idx)] \
209
                                   + newlines \
210
                                   + [('}', idx)]
211
                    offset += 2 + len(newlines)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
212
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
213
            # TODO: Could eliminate any parents that are implied by
214
            # the others
215
                    
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
216
            self._addversion(parents)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
217
        else:
0.1.26 by Martin Pool
Refactor parameters to add command
218
            # 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
219
            # more quickly by just appending unconditionally
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
220
            self._l.append(('{', idx))
221
            self._l += text
222
            self._l.append(('}', idx))
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
223
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
224
            self._addversion(None)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
225
            
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
226
        return idx
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
227
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
228
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
229
    def inclusions(self, versions):
230
        """Expand out everything included by versions."""
231
        i = set(versions)
232
        for v in versions:
233
            i.update(self._v[v])
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
234
        return i
235
236
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
237
    def _addversion(self, parents):
238
        if parents:
239
            self._v.append(frozenset(parents))
240
        else:
241
            self._v.append(frozenset())
242
243
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
244
    def _check_lines(self, text):
245
        if not isinstance(text, list):
246
            raise ValueError("text should be a list, not %s" % type(text))
247
248
        for l in text:
249
            if not isinstance(l, basestring):
250
                raise ValueError("text line should be a string or unicode, not %s" % type(l))
251
        
252
253
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
254
    def _check_versions(self, indexes):
255
        """Check everything in the sequence of indexes is valid"""
256
        for i in indexes:
257
            try:
258
                self._v[i]
259
            except IndexError:
260
                raise IndexError("invalid version number %r" % i)
261
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
262
    
0.1.7 by Martin Pool
Add trivial annotate text
263
    def annotate(self, index):
264
        return list(self.annotate_iter(index))
265
266
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
267
    def annotate_iter(self, version):
0.1.7 by Martin Pool
Add trivial annotate text
268
        """Yield list of (index-id, line) pairs for the specified version.
269
270
        The index indicates when the line originated in the weave."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
271
        included = self.inclusions([version])
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.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
357
    def get_iter(self, version):
0.1.5 by Martin Pool
Add test for storing two text versions.
358
        """Yield lines for the specified version."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
359
        for origin, lineno, line in self._extract(self.inclusions([version])):
0.1.8 by Martin Pool
Unify get/annotate code
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.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
364
        return list(self.get_iter(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))