~bzr-pqm/bzr/bzr.dev

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.85 by Martin Pool
doc
48
# TODO: End marker for each version so we can stop reading?
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
49
50
# TODO: Check that no insertion occurs inside a deletion that was
51
# active in the version of the insertion.
52
0.1.85 by Martin Pool
doc
53
# TODO: Perhaps a special slower check() method that verifies more
54
# nesting constraints and the MD5 of each version?
55
56
0.1.34 by Martin Pool
remove dead code
57
0.1.66 by Martin Pool
Cope without set/frozenset classes
58
try:
59
    set
60
    frozenset
61
except NameError:
62
    from sets import Set, ImmutableSet
63
    set = Set
64
    frozenset = ImmutableSet
0.1.67 by Martin Pool
More fixes to try to run on python2.3
65
    del Set, ImmutableSet
0.1.66 by Martin Pool
Cope without set/frozenset classes
66
67
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
68
class WeaveError(Exception):
69
    """Exception in processing weave"""
70
71
72
class WeaveFormatError(WeaveError):
73
    """Weave invariant violated"""
74
    
75
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
76
class Weave(object):
77
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
78
    
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
79
    A Weave manages versions of line-based text files, keeping track
80
    of the originating version for each line.
81
82
    To clients the "lines" of the file are represented as a list of strings.
83
    These strings  will typically have terminal newline characters, but
84
    this is not required.  In particular files commonly do not have a newline
85
    at the end of the file.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
86
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
87
    Texts can be identified in either of two ways:
88
89
    * a nonnegative index number.
90
91
    * a version-id string.
92
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
93
    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.
94
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
95
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
96
    The weave is represented as a list mixing edit instructions and
97
    literal text.  Each entry in _l can be either a string (or
98
    unicode), or a tuple.  If a string, it means that the given line
99
    should be output in the currently active revisions.
100
101
    If a tuple, it gives a processing instruction saying in which
102
    revisions the enclosed lines are active.  The tuple has the form
103
    (instruction, version).
104
105
    The instruction can be '{' or '}' for an insertion block, and '['
106
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
107
    integer version index.  There is no replace operator, only deletes
108
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
109
0.1.41 by Martin Pool
Doc
110
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
111
112
    * A later version can delete lines that were introduced by any
113
      number of ancestor versions; this implies that deletion
114
      instructions can span insertion blocks without regard to the
115
      insertion block's nesting.
116
0.1.41 by Martin Pool
Doc
117
    * Similarly, deletions need not be properly nested with regard to
118
      each other, because they might have been generated by
119
      independent revisions.
120
0.1.45 by Martin Pool
doc
121
    * Insertions are always made by inserting a new bracketed block
122
      into a single point in the previous weave.  This implies they
123
      can nest but not overlap, and the nesting must always have later
124
      insertions on the inside.
125
0.1.41 by Martin Pool
Doc
126
    * It doesn't seem very useful to have an active insertion
127
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
128
      
0.1.41 by Martin Pool
Doc
129
    * Therefore, all instructions are always"considered"; that
130
      is passed onto and off the stack.  An outer inactive block
131
      doesn't disable an inner block.
132
133
    * Lines are enabled if the most recent enclosing insertion is
134
      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
135
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
136
    * There is no point having a deletion directly inside its own
137
      insertion; you might as well just not write it.  And there
138
      should be no way to get an earlier version deleting a later
139
      version.
140
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
141
    _l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
142
        Text of the weave. 
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
143
144
    _v
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
145
        List of versions, indexed by index number.
146
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
147
        For each version we store the set (included_versions), which
148
        lists the previous versions also considered active; the
149
        versions included in those versions are included transitively.
150
        So new versions created from nothing list []; most versions
151
        have a single entry; some have more.
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
152
153
    _sha1s
154
        List of hex SHA-1 of each version, or None if not recorded.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
155
    """
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
156
    def __init__(self):
157
        self._l = []
158
        self._v = []
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
159
        self._sha1s = []
0.1.60 by Martin Pool
Weave eq and ne methods
160
161
162
    def __eq__(self, other):
163
        if not isinstance(other, Weave):
164
            return False
165
        return self._v == other._v \
166
               and self._l == other._l
167
    
168
169
    def __ne__(self, other):
170
        return not self.__eq__(other)
171
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
172
        
0.1.26 by Martin Pool
Refactor parameters to add command
173
    def add(self, parents, text):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
174
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
175
  
0.1.26 by Martin Pool
Refactor parameters to add command
176
        Returns the index number of the newly added version.
177
178
        parents
0.1.64 by Martin Pool
Add test for merging versions
179
            List or set of parent version numbers.  This must normally include
180
            the parents and the parent's parents, or wierd things might happen.
0.1.26 by Martin Pool
Refactor parameters to add command
181
182
        text
183
            Sequence of lines to be added in the new version."""
0.1.82 by Martin Pool
Small weave optimizations
184
        ## self._check_versions(parents)
185
        ## self._check_lines(text)
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
186
        idx = len(self._v)
0.1.5 by Martin Pool
Add test for storing two text versions.
187
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
188
        import sha
189
        s = sha.new()
190
        for l in text:
191
            s.update(l)
192
        sha1 = s.hexdigest()
193
        del s
194
0.1.26 by Martin Pool
Refactor parameters to add command
195
        if parents:
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
196
            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
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.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
226
            self._addversion(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
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
234
            self._addversion(None)
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
235
236
        self._sha1s.append(sha1)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
237
            
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
238
        return idx
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
239
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
240
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
241
    def inclusions(self, versions):
242
        """Expand out everything included by versions."""
243
        i = set(versions)
244
        for v in versions:
245
            i.update(self._v[v])
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
246
        return i
247
248
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
249
    def _addversion(self, parents):
250
        if parents:
251
            self._v.append(frozenset(parents))
252
        else:
253
            self._v.append(frozenset())
254
255
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
256
    def _check_lines(self, text):
257
        if not isinstance(text, list):
258
            raise ValueError("text should be a list, not %s" % type(text))
259
260
        for l in text:
261
            if not isinstance(l, basestring):
262
                raise ValueError("text line should be a string or unicode, not %s" % type(l))
263
        
264
265
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
266
    def _check_versions(self, indexes):
267
        """Check everything in the sequence of indexes is valid"""
268
        for i in indexes:
269
            try:
270
                self._v[i]
271
            except IndexError:
272
                raise IndexError("invalid version number %r" % i)
273
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
274
    
0.1.7 by Martin Pool
Add trivial annotate text
275
    def annotate(self, index):
276
        return list(self.annotate_iter(index))
277
278
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
279
    def annotate_iter(self, version):
0.1.7 by Martin Pool
Add trivial annotate text
280
        """Yield list of (index-id, line) pairs for the specified version.
281
282
        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
283
        included = self.inclusions([version])
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
284
        for origin, lineno, text in self._extract(included):
285
            yield origin, text
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
286
287
288
    def _extract(self, included):
0.1.20 by Martin Pool
Factor out Knit.extract() method
289
        """Yield annotation of lines in included set.
290
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
291
        Yields a sequence of tuples (origin, lineno, text), where
292
        origin is the origin version, lineno the index in the weave,
293
        and text the text of the line.
294
0.1.20 by Martin Pool
Factor out Knit.extract() method
295
        The set typically but not necessarily corresponds to a version.
296
        """
0.1.48 by Martin Pool
Basic parsing of delete instructions.
297
        istack = []          # versions for which an insertion block is current
298
299
        dset = set()         # versions for which a deletion block is current
300
0.1.82 by Martin Pool
Small weave optimizations
301
        isactive = None
0.1.48 by Martin Pool
Basic parsing of delete instructions.
302
303
        lineno = 0         # line of weave, 0-based
0.1.53 by Martin Pool
doc
304
305
        # TODO: Probably only need to put included revisions in the istack
306
307
        # TODO: Could split this into two functions, one that updates
308
        # the stack and the other that processes the results -- but
309
        # I'm not sure it's really needed.
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
310
0.1.85 by Martin Pool
doc
311
        # TODO: In fact, I think we only need to store the *count* of
312
        # active insertions and deletions, and we can maintain that by
313
        # just by just counting as we go along.
314
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
315
        WFE = WeaveFormatError
0.1.95 by Martin Pool
- preliminary merge conflict detection
316
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
317
        for l in self._l:
318
            if isinstance(l, tuple):
0.1.82 by Martin Pool
Small weave optimizations
319
                isactive = None         # recalculate
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
320
                c, v = l
321
                if c == '{':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
322
                    if istack and (istack[-1] >= v):
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
323
                        raise WFE("improperly nested insertions %d>=%d on line %d" 
324
                                  % (istack[-1], v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
325
                    istack.append(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
326
                elif c == '}':
0.1.48 by Martin Pool
Basic parsing of delete instructions.
327
                    try:
328
                        oldv = istack.pop()
329
                    except IndexError:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
330
                        raise WFE("unmatched close of insertion %d on line %d"
331
                                  % (v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
332
                    if oldv != v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
333
                        raise WFE("mismatched close of insertion %d!=%d on line %d"
334
                                  % (oldv, v, lineno))
0.1.48 by Martin Pool
Basic parsing of delete instructions.
335
                elif c == '[':
336
                    # block deleted in v
337
                    if v in dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
338
                        raise WFE("repeated deletion marker for version %d on line %d"
339
                                  % (v, lineno))
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
340
                    if istack:
341
                        if istack[-1] == v:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
342
                            raise WFE("version %d deletes own text on line %d"
343
                                      % (v, lineno))
0.1.95 by Martin Pool
- preliminary merge conflict detection
344
                        # XXX
0.1.48 by Martin Pool
Basic parsing of delete instructions.
345
                        dset.add(v)
346
                elif c == ']':
347
                    if v in dset:
348
                        dset.remove(v)
349
                    else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
350
                        raise WFE("unmatched close of deletion %d on line %d"
351
                                  % (v, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
352
                else:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
353
                    raise WFE("invalid processing instruction %r on line %d"
354
                              % (l, lineno))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
355
            else:
356
                assert isinstance(l, basestring)
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
357
                if not istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
358
                    raise WFE("literal at top level on line %d"
359
                              % lineno)
0.1.82 by Martin Pool
Small weave optimizations
360
                if isactive == None:
361
                    isactive = (istack[-1] in included) \
362
                               and not included.intersection(dset)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
363
                if isactive:
0.1.48 by Martin Pool
Basic parsing of delete instructions.
364
                    origin = istack[-1]
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
365
                    yield origin, lineno, l
366
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
367
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
368
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
369
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
370
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
371
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
372
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
373
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
374
0.1.7 by Martin Pool
Add trivial annotate text
375
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
376
    def get_iter(self, version):
0.1.5 by Martin Pool
Add test for storing two text versions.
377
        """Yield lines for the specified version."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
378
        for origin, lineno, line in self._extract(self.inclusions([version])):
0.1.8 by Martin Pool
Unify get/annotate code
379
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
380
381
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
382
    def get(self, index):
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
383
        return list(self.get_iter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
384
385
0.1.95 by Martin Pool
- preliminary merge conflict detection
386
    def mash_iter(self, included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
387
        """Return composed version of multiple included versions."""
388
        included = frozenset(included)
389
        for origin, lineno, text in self._extract(included):
390
            yield text
391
392
0.1.11 by Martin Pool
Add Knit.dump method
393
    def dump(self, to_file):
394
        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.)
395
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
396
        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.)
397
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
398
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
399
400
0.1.91 by Martin Pool
Update Weave.check
401
402
    def numversions(self):
403
        l = len(self._v)
404
        assert l == len(self._sha1s)
405
        return l
406
407
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
408
    def check(self):
0.1.91 by Martin Pool
Update Weave.check
409
        # check no circular inclusions
410
        for version in range(self.numversions()):
411
            inclusions = list(self._v[version])
412
            if inclusions:
413
                inclusions.sort()
414
                if inclusions[-1] >= version:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
415
                    raise WeaveFormatError("invalid included version %d for index %d"
0.1.91 by Martin Pool
Update Weave.check
416
                                           % (inclusions[-1], version))
417
418
        # try extracting all versions; this is a bit slow and parallel
419
        # extraction could be used
420
        import sha
421
        for version in range(self.numversions()):
422
            s = sha.new()
423
            for l in self.get_iter(version):
424
                s.update(l)
425
            hd = s.hexdigest()
426
            expected = self._sha1s[version]
427
            if hd != expected:
428
                raise WeaveError("mismatched sha1 for version %d; "
429
                                 "got %s, expected %s"
430
                                 % (version, hd, expected))
0.1.18 by Martin Pool
Better Knit.dump method
431
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
432
433
0.1.95 by Martin Pool
- preliminary merge conflict detection
434
    def merge(self, merge_versions):
435
        """Automerge and mark conflicts between versions.
436
437
        This returns a sequence, each entry describing alternatives
438
        for a chunk of the file.  Each of the alternatives is given as
439
        a list of lines.
440
441
        If there is a chunk of the file where there's no diagreement,
442
        only one alternative is given.
443
        """
444
445
        # approach: find the included versions common to all the
446
        # merged versions
447
        raise NotImplementedError()
448
449
450
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
451
    def _delta(self, included, lines):
452
        """Return changes from basis to new revision.
453
454
        The old text for comparison is the union of included revisions.
455
456
        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.
457
0.1.55 by Martin Pool
doc
458
        Delta is returned as a sequence of
459
        (weave1, weave2, newlines).
460
461
        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.
462
        replaced by the sequence of lines in newlines.  Note that
463
        these line numbers are positions in the total weave and don't
464
        correspond to the lines in any extracted version, or even the
465
        extracted union of included versions.
466
467
        If line1=line2, this is a pure insert; if newlines=[] this is a
468
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
469
        """
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
470
        # basis a list of (origin, lineno, line)
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
471
        basis_lineno = []
0.1.83 by Martin Pool
Better delta basis calculation
472
        basis_lines = []
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
473
        for origin, lineno, line in self._extract(included):
474
            basis_lineno.append(lineno)
475
            basis_lines.append(line)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
476
477
        # add a sentinal, because we can also match against the final line
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
478
        basis_lineno.append(len(self._l))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
479
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
480
        # XXX: which line of the weave should we really consider
481
        # matches the end of the file?  the current code says it's the
482
        # last line of the weave?
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
483
484
        from difflib import SequenceMatcher
485
        s = SequenceMatcher(None, basis_lines, lines)
486
0.1.55 by Martin Pool
doc
487
        # TODO: Perhaps return line numbers from composed weave as well?
488
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
489
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
490
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
491
492
            if tag == 'equal':
493
                continue
494
495
            # i1,i2 are given in offsets within basis_lines; we need to map them
496
            # back to offsets within the entire weave
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
497
            real_i1 = basis_lineno[i1]
498
            real_i2 = basis_lineno[i2]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
499
0.1.35 by Martin Pool
Clean up Knit._delta method
500
            assert 0 <= j1
501
            assert j1 <= j2
502
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
503
0.1.35 by Martin Pool
Clean up Knit._delta method
504
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
505
0.1.1 by Martin Pool
Check in old existing knit code.
506
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
507
0.1.88 by Martin Pool
Add weave info command.
508
def weave_info(filename, out):
509
    """Show some text information about the weave."""
510
    from weavefile import read_weave
511
    wf = file(filename, 'rb')
512
    w = read_weave(wf)
513
    # FIXME: doesn't work on pipes
514
    weave_size = wf.tell()
515
    print >>out, "weave file size %d bytes" % weave_size
516
    print >>out, "weave contains %d versions" % len(w._v)
517
518
    total = 0
0.1.91 by Martin Pool
Update Weave.check
519
    print ' %8s %8s %8s %s' % ('version', 'lines', 'bytes', 'sha1')
520
    print ' -------- -------- -------- ----------------------------------------'
0.1.88 by Martin Pool
Add weave info command.
521
    for i in range(len(w._v)):
522
        text = w.get(i)
523
        lines = len(text)
524
        bytes = sum((len(a) for a in text))
0.1.91 by Martin Pool
Update Weave.check
525
        sha1 = w._sha1s[i]
526
        print ' %8d %8d %8d %s' % (i, lines, bytes, sha1)
0.1.88 by Martin Pool
Add weave info command.
527
        total += bytes
528
529
    print >>out, "versions total %d bytes" % total
530
    print >>out, "compression ratio %.3f" % (float(total)/float(weave_size))
531
    
532
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
533
534
def main(argv):
535
    import sys
536
    import os
0.1.91 by Martin Pool
Update Weave.check
537
    from weavefile import write_weave_v1, read_weave
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
538
    cmd = argv[1]
539
    if cmd == 'add':
0.1.91 by Martin Pool
Update Weave.check
540
        w = read_weave(file(argv[2], 'rb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
541
        # at the moment, based on everything in the file
542
        parents = set(range(len(w._v)))
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
543
        lines = sys.stdin.readlines()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
544
        ver = w.add(parents, lines)
545
        write_weave_v1(w, file(argv[2], 'wb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
546
        print 'added %d' % ver
547
    elif cmd == 'init':
548
        fn = argv[2]
549
        if os.path.exists(fn):
550
            raise IOError("file exists")
551
        w = Weave()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
552
        write_weave_v1(w, file(fn, 'wb'))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
553
    elif cmd == 'get':
0.1.91 by Martin Pool
Update Weave.check
554
        w = read_weave(file(argv[2], 'rb'))
0.1.94 by Martin Pool
Fix get_iter call
555
        sys.stdout.writelines(w.get_iter(int(argv[3])))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
556
    elif cmd == 'annotate':
0.1.91 by Martin Pool
Update Weave.check
557
        w = read_weave(file(argv[2], 'rb'))
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
558
        # newline is added to all lines regardless; too hard to get
559
        # reasonable formatting otherwise
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
560
        lasto = None
561
        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.
562
            text = text.rstrip('\r\n')
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
563
            if origin == lasto:
564
                print '      | %s' % (text)
565
            else:
566
                print '%5d | %s' % (origin, text)
567
                lasto = origin
0.1.88 by Martin Pool
Add weave info command.
568
    elif cmd == 'info':
569
        weave_info(argv[2], sys.stdout)
0.1.91 by Martin Pool
Update Weave.check
570
    elif cmd == 'check':
571
        w = read_weave(file(argv[2], 'rb'))
572
        w.check()
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
573
    else:
574
        raise ValueError('unknown command %r' % cmd)
575
    
576
577
if __name__ == '__main__':
578
    import sys
579
    sys.exit(main(sys.argv))