~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: Nothing here so far assumes the lines are really \n newlines,
31
# rather than being split up in some other way.  We could accomodate
32
# binaries, perhaps by naively splitting on \n or perhaps using
33
# something like a rolling checksum.
34
35
# TODO: Track version names as well as indexes. 
36
0.1.85 by Martin Pool
doc
37
# 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
38
39
# TODO: Check that no insertion occurs inside a deletion that was
40
# active in the version of the insertion.
41
912 by Martin Pool
- update todos for weave
42
# TODO: In addition to the SHA-1 check, perhaps have some code that
43
# checks structural constraints of the weave: ie that insertions are
44
# properly nested, that there is no text outside of an insertion, that
45
# insertions or deletions are not repeated, etc.
0.1.85 by Martin Pool
doc
46
918 by Martin Pool
- start doing new weave-merge algorithm
47
# TODO: Make the info command just show info, not extract everything:
48
# it can be much faster.
49
50
# TODO: Perhaps use long integers as sets instead of set objects; may
51
# be faster.
52
53
# TODO: Parallel-extract that passes back each line along with a
54
# description of which revisions include it.  Nice for checking all
55
# shas in parallel.
56
57
0.1.85 by Martin Pool
doc
58
0.1.34 by Martin Pool
remove dead code
59
0.1.66 by Martin Pool
Cope without set/frozenset classes
60
try:
61
    set
62
    frozenset
63
except NameError:
64
    from sets import Set, ImmutableSet
65
    set = Set
66
    frozenset = ImmutableSet
0.1.67 by Martin Pool
More fixes to try to run on python2.3
67
    del Set, ImmutableSet
0.1.66 by Martin Pool
Cope without set/frozenset classes
68
69
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
70
class WeaveError(Exception):
71
    """Exception in processing weave"""
72
73
74
class WeaveFormatError(WeaveError):
75
    """Weave invariant violated"""
76
    
77
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
78
class Weave(object):
79
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
80
    
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
81
    A Weave manages versions of line-based text files, keeping track
82
    of the originating version for each line.
83
84
    To clients the "lines" of the file are represented as a list of strings.
85
    These strings  will typically have terminal newline characters, but
86
    this is not required.  In particular files commonly do not have a newline
87
    at the end of the file.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
88
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
89
    Texts can be identified in either of two ways:
90
91
    * a nonnegative index number.
92
93
    * a version-id string.
94
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
95
    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.
96
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
97
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
98
    The weave is represented as a list mixing edit instructions and
99
    literal text.  Each entry in _l can be either a string (or
100
    unicode), or a tuple.  If a string, it means that the given line
101
    should be output in the currently active revisions.
102
103
    If a tuple, it gives a processing instruction saying in which
104
    revisions the enclosed lines are active.  The tuple has the form
105
    (instruction, version).
106
107
    The instruction can be '{' or '}' for an insertion block, and '['
108
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
109
    integer version index.  There is no replace operator, only deletes
110
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
111
0.1.41 by Martin Pool
Doc
112
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
113
114
    * A later version can delete lines that were introduced by any
115
      number of ancestor versions; this implies that deletion
116
      instructions can span insertion blocks without regard to the
117
      insertion block's nesting.
118
0.1.41 by Martin Pool
Doc
119
    * Similarly, deletions need not be properly nested with regard to
120
      each other, because they might have been generated by
121
      independent revisions.
122
0.1.45 by Martin Pool
doc
123
    * Insertions are always made by inserting a new bracketed block
124
      into a single point in the previous weave.  This implies they
125
      can nest but not overlap, and the nesting must always have later
126
      insertions on the inside.
127
0.1.41 by Martin Pool
Doc
128
    * It doesn't seem very useful to have an active insertion
129
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
130
      
0.1.41 by Martin Pool
Doc
131
    * Therefore, all instructions are always"considered"; that
132
      is passed onto and off the stack.  An outer inactive block
133
      doesn't disable an inner block.
134
135
    * Lines are enabled if the most recent enclosing insertion is
136
      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
137
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
138
    * There is no point having a deletion directly inside its own
139
      insertion; you might as well just not write it.  And there
140
      should be no way to get an earlier version deleting a later
141
      version.
142
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
143
    _l
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
144
        Text of the weave.
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
145
146
    _v
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
147
        List of parents, indexed by version number.
148
        It is only necessary to store the minimal set of parents for
149
        each version; the parent's parents are implied.
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
150
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
151
    _sha1s
152
        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.
153
    """
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
154
    def __init__(self):
155
        self._l = []
156
        self._v = []
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
157
        self._sha1s = []
0.1.60 by Martin Pool
Weave eq and ne methods
158
159
160
    def __eq__(self, other):
161
        if not isinstance(other, Weave):
162
            return False
163
        return self._v == other._v \
164
               and self._l == other._l
165
    
166
167
    def __ne__(self, other):
168
        return not self.__eq__(other)
169
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
170
        
0.1.26 by Martin Pool
Refactor parameters to add command
171
    def add(self, parents, text):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
172
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
173
  
0.1.26 by Martin Pool
Refactor parameters to add command
174
        Returns the index number of the newly added version.
175
176
        parents
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
177
            List or set of direct parent version numbers.
178
            
0.1.26 by Martin Pool
Refactor parameters to add command
179
        text
180
            Sequence of lines to be added in the new version."""
0.1.82 by Martin Pool
Small weave optimizations
181
        ## self._check_versions(parents)
182
        ## self._check_lines(text)
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
183
        idx = len(self._v)
0.1.5 by Martin Pool
Add test for storing two text versions.
184
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
185
        import sha
186
        s = sha.new()
187
        for l in text:
188
            s.update(l)
189
        sha1 = s.hexdigest()
190
        del s
191
918 by Martin Pool
- start doing new weave-merge algorithm
192
        # TODO: It'd probably be faster to append things on to a new
193
        # list rather than modifying the existing one, which is likely
194
        # to cause a lot of copying.
195
0.1.26 by Martin Pool
Refactor parameters to add command
196
        if parents:
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
197
            ancestors = self.inclusions(parents)
198
            delta = self._delta(ancestors, text)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
199
0.1.31 by Martin Pool
Fix insertion of multiple regions, calculating the right line offset as we go.
200
            # offset gives the number of lines that have been inserted
201
            # into the weave up to the current point; if the original edit instruction
202
            # says to change line A then we actually change (A+offset)
203
            offset = 0
204
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
205
            for i1, i2, newlines in delta:
0.1.29 by Martin Pool
Better internal error
206
                assert 0 <= i1
207
                assert i1 <= i2
208
                assert i2 <= len(self._l)
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
209
210
                # the deletion and insertion are handled separately.
211
                # first delete the region.
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
212
                if i1 != i2:
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
213
                    self._l.insert(i1+offset, ('[', idx))
214
                    self._l.insert(i2+offset+1, (']', idx))
215
                    offset += 2
216
                    # is this OK???
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
217
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
218
                if newlines:
0.1.57 by Martin Pool
Fix bug in an update edit that both deletes and inserts -- previously
219
                    # there may have been a deletion spanning up to
220
                    # i2; we want to insert after this region to make sure
221
                    # we don't destroy ourselves
222
                    i = i2 + offset
0.1.56 by Martin Pool
Handle deletion of lines by marking the region with a deletion
223
                    self._l[i:i] = [('{', idx)] \
224
                                   + newlines \
225
                                   + [('}', idx)]
226
                    offset += 2 + len(newlines)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
227
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
228
            self._addversion(parents)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
229
        else:
0.1.26 by Martin Pool
Refactor parameters to add command
230
            # 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
231
            # more quickly by just appending unconditionally
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
232
            self._l.append(('{', idx))
233
            self._l += text
234
            self._l.append(('}', idx))
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
235
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
236
            self._addversion(None)
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
237
238
        self._sha1s.append(sha1)
0.1.25 by Martin Pool
Handle insertion of new weave layers that insert text on top of the basis
239
            
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
240
        return idx
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
241
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
242
918 by Martin Pool
- start doing new weave-merge algorithm
243
    def inclusions_bitset(self, versions):
244
        i = 0
245
        for v in versions:
246
            i |= (1L << v)
247
        v = max(versions)
248
        while v >= 0:
249
            if i & (1L << v):
250
                # if v is included, include all its parents
251
                for pv in self._v[v]:
252
                    i |= (1L << pv)
253
            v -= 1
254
        return i
255
256
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
257
    def inclusions(self, versions):
893 by Martin Pool
- Refactor weave calculation of inclusions
258
        """Return set of all ancestors of given version(s)."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
259
        i = set(versions)
893 by Martin Pool
- Refactor weave calculation of inclusions
260
        v = max(versions)
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
261
        try:
893 by Martin Pool
- Refactor weave calculation of inclusions
262
            while v >= 0:
263
                if v in i:
264
                    # include all its parents
265
                    i.update(self._v[v])
266
                v -= 1
267
            return i
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
268
        except IndexError:
269
            raise ValueError("version %d not present in weave" % v)
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
270
271
890 by Martin Pool
- weave info should show minimal expression of parents
272
    def minimal_parents(self, version):
273
        """Find the minimal set of parents for the version."""
274
        included = self._v[version]
275
        if not included:
276
            return []
277
        
278
        li = list(included)
893 by Martin Pool
- Refactor weave calculation of inclusions
279
        li.sort(reverse=True)
890 by Martin Pool
- weave info should show minimal expression of parents
280
281
        mininc = []
282
        gotit = set()
283
284
        for pv in li:
285
            if pv not in gotit:
286
                mininc.append(pv)
893 by Martin Pool
- Refactor weave calculation of inclusions
287
                gotit.update(self.inclusions(pv))
890 by Martin Pool
- weave info should show minimal expression of parents
288
289
        assert mininc[0] >= 0
290
        assert mininc[-1] < version
291
        return mininc
292
293
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
294
    def _addversion(self, parents):
295
        if parents:
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
296
            self._v.append(parents)
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
297
        else:
298
            self._v.append(frozenset())
299
300
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
301
    def _check_lines(self, text):
302
        if not isinstance(text, list):
303
            raise ValueError("text should be a list, not %s" % type(text))
304
305
        for l in text:
306
            if not isinstance(l, basestring):
869 by Martin Pool
- more weave.py command line options
307
                raise ValueError("text line should be a string or unicode, not %s"
308
                                 % type(l))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
309
        
310
311
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
312
    def _check_versions(self, indexes):
313
        """Check everything in the sequence of indexes is valid"""
314
        for i in indexes:
315
            try:
316
                self._v[i]
317
            except IndexError:
318
                raise IndexError("invalid version number %r" % i)
319
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
320
    
0.1.7 by Martin Pool
Add trivial annotate text
321
    def annotate(self, index):
322
        return list(self.annotate_iter(index))
323
324
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
325
    def annotate_iter(self, version):
0.1.7 by Martin Pool
Add trivial annotate text
326
        """Yield list of (index-id, line) pairs for the specified version.
327
328
        The index indicates when the line originated in the weave."""
893 by Martin Pool
- Refactor weave calculation of inclusions
329
        for origin, lineno, text in self._extract([version]):
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
330
            yield origin, text
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
331
332
918 by Martin Pool
- start doing new weave-merge algorithm
333
    def _walk(self):
334
        """Walk the weave.
335
336
        Yields sequence of
337
        (lineno, insert, deletes, text)
338
        for each literal line.
339
        """
340
        
341
        istack = []
342
        dset = 0L
343
344
        lineno = 0         # line of weave, 0-based
345
346
        for l in self._l:
347
            if isinstance(l, tuple):
348
                c, v = l
349
                isactive = None
350
                if c == '{':
351
                    istack.append(v)
352
                elif c == '}':
353
                    oldv = istack.pop()
354
                elif c == '[':
355
                    vs = (1L << v)
356
                    assert not (dset & vs)
357
                    dset |= vs
358
                elif c == ']':
359
                    vs = (1L << v)
360
                    assert dset & vs
361
                    dset ^= vs
362
                else:
363
                    raise WeaveFormatError('unexpected instruction %r'
364
                                           % v)
365
            else:
366
                assert isinstance(l, basestring)
367
                assert istack
368
                yield lineno, istack[-1], dset, l
369
            lineno += 1
370
371
372
893 by Martin Pool
- Refactor weave calculation of inclusions
373
    def _extract(self, versions):
0.1.20 by Martin Pool
Factor out Knit.extract() method
374
        """Yield annotation of lines in included set.
375
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
376
        Yields a sequence of tuples (origin, lineno, text), where
377
        origin is the origin version, lineno the index in the weave,
378
        and text the text of the line.
379
0.1.20 by Martin Pool
Factor out Knit.extract() method
380
        The set typically but not necessarily corresponds to a version.
381
        """
893 by Martin Pool
- Refactor weave calculation of inclusions
382
        included = self.inclusions(versions)
881 by Martin Pool
- faster weave extraction
383
384
        istack = []
385
        dset = set()
0.1.48 by Martin Pool
Basic parsing of delete instructions.
386
387
        lineno = 0         # line of weave, 0-based
891 by Martin Pool
- fix up refactoring of weave
388
894 by Martin Pool
- small optimization for weave extract
389
        isactive = None
0.1.85 by Martin Pool
doc
390
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
391
        WFE = WeaveFormatError
0.1.95 by Martin Pool
- preliminary merge conflict detection
392
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
393
        for l in self._l:
394
            if isinstance(l, tuple):
395
                c, v = l
894 by Martin Pool
- small optimization for weave extract
396
                isactive = None
891 by Martin Pool
- fix up refactoring of weave
397
                if c == '{':
398
                    assert v not in istack
399
                    istack.append(v)
400
                elif c == '}':
401
                    oldv = istack.pop()
402
                    assert oldv == v
403
                elif c == '[':
404
                    if v in included:
881 by Martin Pool
- faster weave extraction
405
                        assert v not in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
406
                        dset.add(v)
891 by Martin Pool
- fix up refactoring of weave
407
                else:
408
                    assert c == ']'
409
                    if v in included:
881 by Martin Pool
- faster weave extraction
410
                        assert v in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
411
                        dset.remove(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
412
            else:
413
                assert isinstance(l, basestring)
894 by Martin Pool
- small optimization for weave extract
414
                if isactive is None:
415
                    isactive = (not dset) and istack and (istack[-1] in included)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
416
                if isactive:
888 by Martin Pool
- fix refactoring breakage
417
                    yield istack[-1], lineno, l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
418
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
419
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
420
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
421
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
422
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
423
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
424
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
425
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
426
0.1.7 by Martin Pool
Add trivial annotate text
427
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
428
    def get_iter(self, version):
0.1.5 by Martin Pool
Add test for storing two text versions.
429
        """Yield lines for the specified version."""
893 by Martin Pool
- Refactor weave calculation of inclusions
430
        for origin, lineno, line in self._extract([version]):
0.1.8 by Martin Pool
Unify get/annotate code
431
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
432
433
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
434
    def get(self, index):
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
435
        return list(self.get_iter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
436
437
0.1.95 by Martin Pool
- preliminary merge conflict detection
438
    def mash_iter(self, included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
439
        """Return composed version of multiple included versions."""
440
        included = frozenset(included)
893 by Martin Pool
- Refactor weave calculation of inclusions
441
        for origin, lineno, text in self._extract(included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
442
            yield text
443
444
0.1.11 by Martin Pool
Add Knit.dump method
445
    def dump(self, to_file):
446
        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.)
447
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
448
        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.)
449
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
450
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
451
452
0.1.91 by Martin Pool
Update Weave.check
453
454
    def numversions(self):
455
        l = len(self._v)
456
        assert l == len(self._sha1s)
457
        return l
458
459
894 by Martin Pool
- small optimization for weave extract
460
    def check(self, progress_bar=None):
0.1.91 by Martin Pool
Update Weave.check
461
        # check no circular inclusions
462
        for version in range(self.numversions()):
463
            inclusions = list(self._v[version])
464
            if inclusions:
465
                inclusions.sort()
466
                if inclusions[-1] >= version:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
467
                    raise WeaveFormatError("invalid included version %d for index %d"
0.1.91 by Martin Pool
Update Weave.check
468
                                           % (inclusions[-1], version))
469
470
        # try extracting all versions; this is a bit slow and parallel
471
        # extraction could be used
472
        import sha
894 by Martin Pool
- small optimization for weave extract
473
        nv = self.numversions()
474
        for version in range(nv):
475
            if progress_bar:
476
                progress_bar.update('checking text', version, nv)
0.1.91 by Martin Pool
Update Weave.check
477
            s = sha.new()
478
            for l in self.get_iter(version):
479
                s.update(l)
480
            hd = s.hexdigest()
481
            expected = self._sha1s[version]
482
            if hd != expected:
483
                raise WeaveError("mismatched sha1 for version %d; "
484
                                 "got %s, expected %s"
485
                                 % (version, hd, expected))
0.1.18 by Martin Pool
Better Knit.dump method
486
881 by Martin Pool
- faster weave extraction
487
        # TODO: check insertions are properly nested, that there are
488
        # no lines outside of insertion blocks, that deletions are
489
        # properly paired, etc.
490
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
491
492
0.1.95 by Martin Pool
- preliminary merge conflict detection
493
    def merge(self, merge_versions):
494
        """Automerge and mark conflicts between versions.
495
496
        This returns a sequence, each entry describing alternatives
497
        for a chunk of the file.  Each of the alternatives is given as
498
        a list of lines.
499
500
        If there is a chunk of the file where there's no diagreement,
501
        only one alternative is given.
502
        """
503
504
        # approach: find the included versions common to all the
505
        # merged versions
506
        raise NotImplementedError()
507
508
509
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
510
    def _delta(self, included, lines):
511
        """Return changes from basis to new revision.
512
513
        The old text for comparison is the union of included revisions.
514
515
        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.
516
0.1.55 by Martin Pool
doc
517
        Delta is returned as a sequence of
518
        (weave1, weave2, newlines).
519
520
        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.
521
        replaced by the sequence of lines in newlines.  Note that
522
        these line numbers are positions in the total weave and don't
523
        correspond to the lines in any extracted version, or even the
524
        extracted union of included versions.
525
526
        If line1=line2, this is a pure insert; if newlines=[] this is a
527
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
528
        """
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
529
        # basis a list of (origin, lineno, line)
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
530
        basis_lineno = []
0.1.83 by Martin Pool
Better delta basis calculation
531
        basis_lines = []
893 by Martin Pool
- Refactor weave calculation of inclusions
532
        for origin, lineno, line in self._extract(included):
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
533
            basis_lineno.append(lineno)
534
            basis_lines.append(line)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
535
536
        # 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
537
        basis_lineno.append(len(self._l))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
538
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
539
        # XXX: which line of the weave should we really consider
540
        # matches the end of the file?  the current code says it's the
541
        # last line of the weave?
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
542
543
        from difflib import SequenceMatcher
544
        s = SequenceMatcher(None, basis_lines, lines)
545
0.1.55 by Martin Pool
doc
546
        # TODO: Perhaps return line numbers from composed weave as well?
547
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
548
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
549
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
550
551
            if tag == 'equal':
552
                continue
553
554
            # i1,i2 are given in offsets within basis_lines; we need to map them
555
            # back to offsets within the entire weave
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
556
            real_i1 = basis_lineno[i1]
557
            real_i2 = basis_lineno[i2]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
558
0.1.35 by Martin Pool
Clean up Knit._delta method
559
            assert 0 <= j1
560
            assert j1 <= j2
561
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
562
0.1.35 by Martin Pool
Clean up Knit._delta method
563
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
564
0.1.1 by Martin Pool
Check in old existing knit code.
565
918 by Martin Pool
- start doing new weave-merge algorithm
566
            
567
    def plan_merge(self, ver_a, ver_b):
568
        """Return pseudo-annotation indicating how the two versions merge.
569
570
        This is computed between versions a and b and their common
571
        base.
572
573
        Weave lines present in none of them are skipped entirely.
574
        """
575
        inc_a = self.inclusions_bitset([ver_a])
576
        inc_b = self.inclusions_bitset([ver_b])
577
        inc_c = inc_a & inc_b
578
579
        for lineno, insert, deleteset, line in self._walk():
580
            insertset = (1L << insert)
581
            if deleteset & inc_c:
582
                # killed in parent; can't be in either a or b
583
                # not relevant to our work
584
                yield 'killed-base', line
585
            elif insertset & inc_c:
586
                # was inserted in base
587
                killed_a = bool(deleteset & inc_a)
588
                killed_b = bool(deleteset & inc_b)
589
                if killed_a and killed_b:
590
                    yield 'killed-both', line
591
                elif killed_a:
592
                    yield 'killed-a', line
593
                elif killed_b:
594
                    yield 'killed-b', line
595
                else:
596
                    yield 'unchanged', line
597
            elif insertset & inc_a:
598
                if deleteset & inc_a:
599
                    yield 'ghost-a', line
600
                else:
601
                    # new in A; not in B
602
                    yield 'new-a', line
603
            elif insertset & inc_b:
604
                if deleteset & inc_b:
605
                    yield 'ghost-b', line
606
                else:
607
                    yield 'new-b', line
608
            else:
609
                # not in either revision
610
                yield 'irrelevant', line
611
919 by Martin Pool
- more development of weave-merge
612
        yield 'unchanged', ''           # terminator
613
614
615
616
    def weave_merge(self, plan):
617
        lines_a = []
618
        lines_b = []
619
        ch_a = ch_b = False
620
621
        for state, line in plan:
622
            if state == 'unchanged' or state == 'killed-both':
623
                # resync and flush queued conflicts changes if any
624
                if not lines_a and not lines_b:
625
                    pass
626
                elif ch_a and not ch_b:
627
                    # one-sided change:                    
628
                    for l in lines_a: yield l
629
                elif ch_b and not ch_a:
630
                    for l in lines_b: yield l
631
                elif lines_a == lines_b:
632
                    for l in lines_a: yield l
633
                else:
634
                    yield '<<<<\n'
635
                    for l in lines_a: yield l
636
                    yield '====\n'
637
                    for l in lines_b: yield l
638
                    yield '>>>>\n'
639
640
                del lines_a[:]
641
                del lines_b[:]
642
                ch_a = ch_b = False
643
                
644
            if state == 'unchanged':
645
                if line:
646
                    yield line
647
            elif state == 'killed-a':
648
                ch_a = True
649
                lines_b.append(line)
650
            elif state == 'killed-b':
651
                ch_b = True
652
                lines_a.append(line)
653
            elif state == 'new-a':
654
                ch_a = True
655
                lines_a.append(line)
656
            elif state == 'new-b':
657
                ch_b = True
658
                lines_b.append(line)
659
            else:
920 by Martin Pool
- add more test cases for weave_merge
660
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 'killed-base',
661
                                 'killed-both'), \
919 by Martin Pool
- more development of weave-merge
662
                       state
663
664
                
665
666
918 by Martin Pool
- start doing new weave-merge algorithm
667
668
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
669
0.1.88 by Martin Pool
Add weave info command.
670
def weave_info(filename, out):
671
    """Show some text information about the weave."""
672
    from weavefile import read_weave
673
    wf = file(filename, 'rb')
674
    w = read_weave(wf)
675
    # FIXME: doesn't work on pipes
676
    weave_size = wf.tell()
677
    print >>out, "weave file size %d bytes" % weave_size
678
    print >>out, "weave contains %d versions" % len(w._v)
679
680
    total = 0
870 by Martin Pool
- better weave info display
681
    print '%6s %6s %8s %40s %20s' % ('ver', 'lines', 'bytes', 'sha1', 'parents')
682
    for i in (6, 6, 8, 40, 20):
683
        print '-' * i,
684
    print
0.1.88 by Martin Pool
Add weave info command.
685
    for i in range(len(w._v)):
686
        text = w.get(i)
687
        lines = len(text)
688
        bytes = sum((len(a) for a in text))
0.1.91 by Martin Pool
Update Weave.check
689
        sha1 = w._sha1s[i]
870 by Martin Pool
- better weave info display
690
        print '%6d %6d %8d %40s' % (i, lines, bytes, sha1),
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
691
        for pv in w._v[i]:
692
            print pv,
693
        print
0.1.88 by Martin Pool
Add weave info command.
694
        total += bytes
695
696
    print >>out, "versions total %d bytes" % total
697
    print >>out, "compression ratio %.3f" % (float(total)/float(weave_size))
869 by Martin Pool
- more weave.py command line options
698
699
700
def usage():
871 by Martin Pool
- add command for merge-based weave
701
    print """bzr weave tool
702
703
Experimental tool for weave algorithm.
704
869 by Martin Pool
- more weave.py command line options
705
usage:
706
    weave init WEAVEFILE
707
        Create an empty weave file
708
    weave get WEAVEFILE VERSION
709
        Write out specified version.
710
    weave check WEAVEFILE
711
        Check consistency of all versions.
712
    weave info WEAVEFILE
713
        Display table of contents.
714
    weave add WEAVEFILE [BASE...] < NEWTEXT
715
        Add NEWTEXT, with specified parent versions.
716
    weave annotate WEAVEFILE VERSION
717
        Display origin of each line.
718
    weave mash WEAVEFILE VERSION...
719
        Display composite of all selected versions.
720
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
721
        Auto-merge two versions and display conflicts.
871 by Martin Pool
- add command for merge-based weave
722
723
example:
724
725
    % weave init foo.weave
726
    % vi foo.txt
727
    % weave add foo.weave < foo.txt
728
    added version 0
729
730
    (create updated version)
731
    % vi foo.txt
732
    % weave get foo.weave 0 | diff -u - foo.txt
733
    % weave add foo.weave 0 < foo.txt
734
    added version 1
735
736
    % weave get foo.weave 0 > foo.txt       (create forked version)
737
    % vi foo.txt
738
    % weave add foo.weave 0 < foo.txt
739
    added version 2
740
741
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
742
    % vi foo.txt                            (resolve conflicts)
743
    % weave add foo.weave 1 2 < foo.txt     (commit merged version)     
744
    
869 by Martin Pool
- more weave.py command line options
745
"""
0.1.88 by Martin Pool
Add weave info command.
746
    
747
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
748
749
def main(argv):
750
    import sys
751
    import os
869 by Martin Pool
- more weave.py command line options
752
    from weavefile import write_weave, read_weave
894 by Martin Pool
- small optimization for weave extract
753
    from bzrlib.progress import ProgressBar
754
755
    #import psyco
756
    #psyco.full()
757
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
758
    cmd = argv[1]
869 by Martin Pool
- more weave.py command line options
759
760
    def readit():
761
        return read_weave(file(argv[2], 'rb'))
762
    
763
    if cmd == 'help':
764
        usage()
765
    elif cmd == 'add':
766
        w = readit()
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
767
        # at the moment, based on everything in the file
869 by Martin Pool
- more weave.py command line options
768
        parents = map(int, argv[3:])
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
769
        lines = sys.stdin.readlines()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
770
        ver = w.add(parents, lines)
869 by Martin Pool
- more weave.py command line options
771
        write_weave(w, file(argv[2], 'wb'))
772
        print 'added version %d' % ver
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
773
    elif cmd == 'init':
774
        fn = argv[2]
775
        if os.path.exists(fn):
776
            raise IOError("file exists")
777
        w = Weave()
869 by Martin Pool
- more weave.py command line options
778
        write_weave(w, file(fn, 'wb'))
779
    elif cmd == 'get': # get one version
780
        w = readit()
0.1.94 by Martin Pool
Fix get_iter call
781
        sys.stdout.writelines(w.get_iter(int(argv[3])))
869 by Martin Pool
- more weave.py command line options
782
        
783
    elif cmd == 'mash': # get composite
784
        w = readit()
785
        sys.stdout.writelines(w.mash_iter(map(int, argv[3:])))
786
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
787
    elif cmd == 'annotate':
869 by Martin Pool
- more weave.py command line options
788
        w = readit()
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
789
        # newline is added to all lines regardless; too hard to get
790
        # reasonable formatting otherwise
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
791
        lasto = None
792
        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.
793
            text = text.rstrip('\r\n')
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
794
            if origin == lasto:
795
                print '      | %s' % (text)
796
            else:
797
                print '%5d | %s' % (origin, text)
798
                lasto = origin
871 by Martin Pool
- add command for merge-based weave
799
                
0.1.88 by Martin Pool
Add weave info command.
800
    elif cmd == 'info':
801
        weave_info(argv[2], sys.stdout)
871 by Martin Pool
- add command for merge-based weave
802
        
0.1.91 by Martin Pool
Update Weave.check
803
    elif cmd == 'check':
869 by Martin Pool
- more weave.py command line options
804
        w = readit()
894 by Martin Pool
- small optimization for weave extract
805
        pb = ProgressBar()
806
        w.check(pb)
807
        pb.clear()
871 by Martin Pool
- add command for merge-based weave
808
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
809
    elif cmd == 'inclusions':
810
        w = readit()
811
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
812
813
    elif cmd == 'parents':
814
        w = readit()
815
        print ' '.join(map(str, w._v[int(argv[3])]))
816
918 by Martin Pool
- start doing new weave-merge algorithm
817
    elif cmd == 'plan-merge':
818
        w = readit()
819
        for state, line in w.plan_merge(int(argv[3]), int(argv[4])):
919 by Martin Pool
- more development of weave-merge
820
            if line:
821
                print '%14s | %s' % (state, line),
918 by Martin Pool
- start doing new weave-merge algorithm
822
871 by Martin Pool
- add command for merge-based weave
823
    elif cmd == 'merge':
919 by Martin Pool
- more development of weave-merge
824
        w = readit()
825
        p = w.plan_merge(int(argv[3]), int(argv[4]))
826
        sys.stdout.writelines(w.weave_merge(p))
827
            
828
    elif cmd == 'mash-merge':
871 by Martin Pool
- add command for merge-based weave
829
        if len(argv) != 5:
830
            usage()
831
            return 1
832
833
        w = readit()
834
        v1, v2 = map(int, argv[3:5])
835
836
        basis = w.inclusions([v1]).intersection(w.inclusions([v2]))
837
838
        base_lines = list(w.mash_iter(basis))
839
        a_lines = list(w.get(v1))
840
        b_lines = list(w.get(v2))
841
842
        from bzrlib.merge3 import Merge3
843
        m3 = Merge3(base_lines, a_lines, b_lines)
844
845
        name_a = 'version %d' % v1
846
        name_b = 'version %d' % v2
847
        sys.stdout.writelines(m3.merge_lines(name_a=name_a, name_b=name_b))
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
848
    else:
849
        raise ValueError('unknown command %r' % cmd)
850
    
851
852
if __name__ == '__main__':
853
    import sys
854
    sys.exit(main(sys.argv))