/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.82 by Martin Pool
Small weave optimizations
177
        ## self._check_versions(parents)
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.82 by Martin Pool
Small weave optimizations
289
        isactive = None
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):
0.1.82 by Martin Pool
Small weave optimizations
303
                isactive = None         # recalculate
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
304
                c, v = l
305
                if c == '{':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
306
                    if istack and (istack[-1] >= v):
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
307
                        raise WFE("improperly nested insertions %d>=%d on line %d" 
308
                                  % (istack[-1], v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
309
                    istack.append(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
310
                elif c == '}':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
311
                    try:
312
                        oldv = istack.pop()
313
                    except IndexError:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
314
                        raise WFE("unmatched close of insertion %d on line %d"
315
                                  % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
316
                    if oldv != v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
317
                        raise WFE("mismatched close of insertion %d!=%d on line %d"
318
                                  % (oldv, v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
319
                elif c == '[':
320
                    # block deleted in v
321
                    if v in dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
322
                        raise WFE("repeated deletion marker for version %d on line %d"
323
                                  % (v, lineno))
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
324
                    if istack:
325
                        if istack[-1] == v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
326
                            raise WFE("version %d deletes own text on line %d"
327
                                      % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
328
                        dset.add(v)
329
                elif c == ']':
330
                    if v in dset:
331
                        dset.remove(v)
332
                    else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
333
                        raise WFE("unmatched close of deletion %d on line %d"
334
                                  % (v, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
335
                else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
336
                    raise WFE("invalid processing instruction %r on line %d"
337
                              % (l, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
338
            else:
339
                assert isinstance(l, basestring)
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
340
                if not istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
341
                    raise WFE("literal at top level on line %d"
342
                              % lineno)
0.1.82 by Martin Pool
Small weave optimizations
343
                if isactive == None:
344
                    isactive = (istack[-1] in included) \
345
                               and not included.intersection(dset)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
346
                if isactive:
0.1.48 by Martin Pool
Basic parsing of delete instructions.
347
                    origin = istack[-1]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
348
                    yield origin, lineno, l
349
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
350
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
351
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
352
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
353
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
354
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
355
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
356
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
357
0.1.7 by Martin Pool
Add trivial annotate text
358
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
359
    def get_iter(self, version):
0.1.5 by Martin Pool
Add test for storing two text versions.
360
        """Yield lines for the specified version."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
361
        for origin, lineno, line in self._extract(self.inclusions([version])):
0.1.8 by Martin Pool
Unify get/annotate code
362
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
363
364
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
365
    def get(self, index):
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
366
        return list(self.get_iter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
367
368
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
369
    def merge_iter(self, included):
370
        """Return composed version of multiple included versions."""
371
        included = frozenset(included)
372
        for origin, lineno, text in self._extract(included):
373
            yield text
374
375
0.1.11 by Martin Pool
Add Knit.dump method
376
    def dump(self, to_file):
377
        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.)
378
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
379
        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.)
380
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
381
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
382
383
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
384
    def check(self):
385
        for vers_info in self._v:
386
            included = set()
387
            for vi in vers_info[0]:
388
                if vi < 0 or vi >= index:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
389
                    raise WeaveFormatError("invalid included version %d for index %d"
390
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
391
                if vi in included:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
392
                    raise WeaveFormatError("repeated included version %d for index %d"
393
                                               % (vi, index))
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
394
                included.add(vi)
0.1.18 by Martin Pool
Better Knit.dump method
395
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
396
397
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
398
    def _delta(self, included, lines):
399
        """Return changes from basis to new revision.
400
401
        The old text for comparison is the union of included revisions.
402
403
        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.
404
0.1.55 by Martin Pool
doc
405
        Delta is returned as a sequence of
406
        (weave1, weave2, newlines).
407
408
        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.
409
        replaced by the sequence of lines in newlines.  Note that
410
        these line numbers are positions in the total weave and don't
411
        correspond to the lines in any extracted version, or even the
412
        extracted union of included versions.
413
414
        If line1=line2, this is a pure insert; if newlines=[] this is a
415
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
416
        """
417
0.1.82 by Martin Pool
Small weave optimizations
418
        ## self._check_versions(included)
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
419
0.1.23 by Martin Pool
tidy up
420
        ##from pprint import pprint
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
421
422
        # first get basis for comparison
423
        # basis holds (lineno, origin, line)
424
        basis = []
425
0.1.23 by Martin Pool
tidy up
426
        ##print 'my lines:'
427
        ##pprint(self._l)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
428
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
429
        # basis a list of (origin, lineno, line)
0.1.83 by Martin Pool
Better delta basis calculation
430
        basis = []
431
        basis_lines = []
432
        for t in self._extract(included):
433
            basis.append(t)
434
            basis_lines.append(t[2])
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
435
436
        # 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
437
        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.
438
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
439
        # XXX: which line of the weave should we really consider
440
        # matches the end of the file?  the current code says it's the
441
        # last line of the weave?
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
442
443
        from difflib import SequenceMatcher
444
        s = SequenceMatcher(None, basis_lines, lines)
445
0.1.23 by Martin Pool
tidy up
446
        ##print 'basis sequence:'
447
        ##pprint(basis)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
448
0.1.55 by Martin Pool
doc
449
        # TODO: Perhaps return line numbers from composed weave as well?
450
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
451
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
452
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
453
454
            if tag == 'equal':
455
                continue
456
457
            # i1,i2 are given in offsets within basis_lines; we need to map them
458
            # back to offsets within the entire weave
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
459
            real_i1 = basis[i1][1]
460
            real_i2 = basis[i2][1]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
461
0.1.35 by Martin Pool
Clean up Knit._delta method
462
            assert 0 <= j1
463
            assert j1 <= j2
464
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
465
0.1.35 by Martin Pool
Clean up Knit._delta method
466
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
467
0.1.1 by Martin Pool
Check in old existing knit code.
468
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
469
470
471
def main(argv):
472
    import sys
473
    import os
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
474
    from weavefile import write_weave_v1, read_weave_v1
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
475
    cmd = argv[1]
476
    if cmd == 'add':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
477
        w = read_weave_v1(file(argv[2], 'rb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
478
        # at the moment, based on everything in the file
479
        parents = set(range(len(w._v)))
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
480
        lines = sys.stdin.readlines()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
481
        ver = w.add(parents, lines)
482
        write_weave_v1(w, file(argv[2], 'wb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
483
        print 'added %d' % ver
484
    elif cmd == 'init':
485
        fn = argv[2]
486
        if os.path.exists(fn):
487
            raise IOError("file exists")
488
        w = Weave()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
489
        write_weave_v1(w, file(fn, 'wb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
490
    elif cmd == 'get':
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
        sys.stdout.writelines(w.getiter(int(argv[3])))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
493
    elif cmd == 'annotate':
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
494
        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.
495
        # newline is added to all lines regardless; too hard to get
496
        # reasonable formatting otherwise
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
497
        lasto = None
498
        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.
499
            text = text.rstrip('\r\n')
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
500
            if origin == lasto:
501
                print '      | %s' % (text)
502
            else:
503
                print '%5d | %s' % (origin, text)
504
                lasto = origin
505
    else:
506
        raise ValueError('unknown command %r' % cmd)
507
    
508
509
if __name__ == '__main__':
510
    import sys
511
    sys.exit(main(sys.argv))