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