/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.1.1 by Martin Pool
Check in old existing knit code.
1
#! /usr/bin/python
2
3
# Copyright (C) 2005 Canonical Ltd
4
0.1.33 by Martin Pool
add gpl text
5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 2 of the License, or
8
# (at your option) any later version.
9
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
# GNU General Public License for more details.
14
15
# You should have received a copy of the GNU General Public License
16
# along with this program; if not, write to the Free Software
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0.1.1 by Martin Pool
Check in old existing knit code.
18
19
# Author: Martin Pool <mbp@canonical.com>
20
21
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
22
"""Weave - storage of related text file versions"""
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
23
0.1.61 by Martin Pool
doc
24
# TODO: Perhaps have copy method for Weave instances?
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
25
0.1.58 by Martin Pool
doc
26
# XXX: If we do weaves this way, will a merge still behave the same
27
# way if it's done in a different order?  That's a pretty desirable
28
# property.
29
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
30
# TODO: How to write these to disk?  One option is cPickle, which
31
# would be fast but less friendly to C, and perhaps not portable.  Another is
32
33
# TODO: Nothing here so far assumes the lines are really \n newlines,
34
# rather than being split up in some other way.  We could accomodate
35
# binaries, perhaps by naively splitting on \n or perhaps using
36
# something like a rolling checksum.
37
38
# TODO: Perhaps track SHA-1 in the header for protection?  This would
39
# be redundant with it being stored in the inventory, but perhaps
40
# usefully so?
41
42
# TODO: Track version names as well as indexes. 
43
44
# TODO: Probably do transitive expansion when specifying parents?
0.1.58 by Martin Pool
doc
45
0.1.68 by Martin Pool
doc
46
# TODO: Separate out some code to read and write weaves.
47
0.1.85 by Martin Pool
doc
48
# TODO: End marker for each version so we can stop reading?
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
49
50
# TODO: Check that no insertion occurs inside a deletion that was
51
# active in the version of the insertion.
52
0.1.85 by Martin Pool
doc
53
# TODO: Perhaps a special slower check() method that verifies more
54
# nesting constraints and the MD5 of each version?
55
56
0.1.34 by Martin Pool
remove dead code
57
0.1.66 by Martin Pool
Cope without set/frozenset classes
58
try:
59
    set
60
    frozenset
61
except NameError:
62
    from sets import Set, ImmutableSet
63
    set = Set
64
    frozenset = ImmutableSet
0.1.67 by Martin Pool
More fixes to try to run on python2.3
65
    del Set, ImmutableSet
0.1.66 by Martin Pool
Cope without set/frozenset classes
66
67
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
68
class WeaveError(Exception):
69
    """Exception in processing weave"""
70
71
72
class WeaveFormatError(WeaveError):
73
    """Weave invariant violated"""
74
    
75
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
76
class Weave(object):
77
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
78
    
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
79
    A Weave manages versions of line-based text files, keeping track
80
    of the originating version for each line.
81
82
    To clients the "lines" of the file are represented as a list of strings.
83
    These strings  will typically have terminal newline characters, but
84
    this is not required.  In particular files commonly do not have a newline
85
    at the end of the file.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
86
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
87
    Texts can be identified in either of two ways:
88
89
    * a nonnegative index number.
90
91
    * a version-id string.
92
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
93
    Typically the index number will be valid only inside this weave and
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
94
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
95
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
96
    The weave is represented as a list mixing edit instructions and
97
    literal text.  Each entry in _l can be either a string (or
98
    unicode), or a tuple.  If a string, it means that the given line
99
    should be output in the currently active revisions.
100
101
    If a tuple, it gives a processing instruction saying in which
102
    revisions the enclosed lines are active.  The tuple has the form
103
    (instruction, version).
104
105
    The instruction can be '{' or '}' for an insertion block, and '['
106
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
107
    integer version index.  There is no replace operator, only deletes
108
    and inserts.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
109
0.1.41 by Martin Pool
Doc
110
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
111
112
    * A later version can delete lines that were introduced by any
113
      number of ancestor versions; this implies that deletion
114
      instructions can span insertion blocks without regard to the
115
      insertion block's nesting.
116
0.1.41 by Martin Pool
Doc
117
    * Similarly, deletions need not be properly nested with regard to
118
      each other, because they might have been generated by
119
      independent revisions.
120
0.1.45 by Martin Pool
doc
121
    * Insertions are always made by inserting a new bracketed block
122
      into a single point in the previous weave.  This implies they
123
      can nest but not overlap, and the nesting must always have later
124
      insertions on the inside.
125
0.1.41 by Martin Pool
Doc
126
    * It doesn't seem very useful to have an active insertion
127
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
128
      
0.1.41 by Martin Pool
Doc
129
    * Therefore, all instructions are always"considered"; that
130
      is passed onto and off the stack.  An outer inactive block
131
      doesn't disable an inner block.
132
133
    * Lines are enabled if the most recent enclosing insertion is
134
      active and none of the enclosing deletions are active.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
135
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
136
    * There is no point having a deletion directly inside its own
137
      insertion; you might as well just not write it.  And there
138
      should be no way to get an earlier version deleting a later
139
      version.
140
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
141
    _l
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
142
        Text of the weave.
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
143
144
    _v
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
145
        List of parents, indexed by version number.
146
        It is only necessary to store the minimal set of parents for
147
        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
148
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
149
    _sha1s
150
        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.
151
    """
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
152
    def __init__(self):
153
        self._l = []
154
        self._v = []
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
155
        self._sha1s = []
0.1.60 by Martin Pool
Weave eq and ne methods
156
157
158
    def __eq__(self, other):
159
        if not isinstance(other, Weave):
160
            return False
161
        return self._v == other._v \
162
               and self._l == other._l
163
    
164
165
    def __ne__(self, other):
166
        return not self.__eq__(other)
167
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
168
        
0.1.26 by Martin Pool
Refactor parameters to add command
169
    def add(self, parents, text):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
170
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
171
  
0.1.26 by Martin Pool
Refactor parameters to add command
172
        Returns the index number of the newly added version.
173
174
        parents
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
175
            List or set of direct parent version numbers.
176
            
0.1.26 by Martin Pool
Refactor parameters to add command
177
        text
178
            Sequence of lines to be added in the new version."""
0.1.82 by Martin Pool
Small weave optimizations
179
        ## self._check_versions(parents)
180
        ## self._check_lines(text)
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
181
        idx = len(self._v)
0.1.5 by Martin Pool
Add test for storing two text versions.
182
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
183
        import sha
184
        s = sha.new()
185
        for l in text:
186
            s.update(l)
187
        sha1 = s.hexdigest()
188
        del s
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)."""
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
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 = []
262
        gotit = set()
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:
278
            self._v.append(frozenset())
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
893 by Martin Pool
- Refactor weave calculation of inclusions
313
    def _extract(self, versions):
0.1.20 by Martin Pool
Factor out Knit.extract() method
314
        """Yield annotation of lines in included set.
315
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
316
        Yields a sequence of tuples (origin, lineno, text), where
317
        origin is the origin version, lineno the index in the weave,
318
        and text the text of the line.
319
0.1.20 by Martin Pool
Factor out Knit.extract() method
320
        The set typically but not necessarily corresponds to a version.
321
        """
893 by Martin Pool
- Refactor weave calculation of inclusions
322
        included = self.inclusions(versions)
881 by Martin Pool
- faster weave extraction
323
324
        istack = []
325
        dset = set()
0.1.48 by Martin Pool
Basic parsing of delete instructions.
326
327
        lineno = 0         # line of weave, 0-based
891 by Martin Pool
- fix up refactoring of weave
328
894 by Martin Pool
- small optimization for weave extract
329
        isactive = None
0.1.85 by Martin Pool
doc
330
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
331
        WFE = WeaveFormatError
0.1.95 by Martin Pool
- preliminary merge conflict detection
332
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
333
        for l in self._l:
334
            if isinstance(l, tuple):
335
                c, v = l
894 by Martin Pool
- small optimization for weave extract
336
                isactive = None
891 by Martin Pool
- fix up refactoring of weave
337
                if c == '{':
338
                    assert v not in istack
339
                    istack.append(v)
340
                elif c == '}':
341
                    oldv = istack.pop()
342
                    assert oldv == v
343
                elif c == '[':
344
                    if v in included:
881 by Martin Pool
- faster weave extraction
345
                        assert v not in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
346
                        dset.add(v)
891 by Martin Pool
- fix up refactoring of weave
347
                else:
348
                    assert c == ']'
349
                    if v in included:
881 by Martin Pool
- faster weave extraction
350
                        assert v in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
351
                        dset.remove(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
352
            else:
353
                assert isinstance(l, basestring)
894 by Martin Pool
- small optimization for weave extract
354
                if isactive is None:
355
                    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
356
                if isactive:
888 by Martin Pool
- fix refactoring breakage
357
                    yield istack[-1], lineno, l
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
358
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
359
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
360
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
361
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
362
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
363
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
364
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
365
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
366
0.1.7 by Martin Pool
Add trivial annotate text
367
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
368
    def get_iter(self, version):
0.1.5 by Martin Pool
Add test for storing two text versions.
369
        """Yield lines for the specified version."""
893 by Martin Pool
- Refactor weave calculation of inclusions
370
        for origin, lineno, line in self._extract([version]):
0.1.8 by Martin Pool
Unify get/annotate code
371
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
372
373
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
374
    def get(self, index):
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
375
        return list(self.get_iter(index))
0.1.1 by Martin Pool
Check in old existing knit code.
376
377
0.1.95 by Martin Pool
- preliminary merge conflict detection
378
    def mash_iter(self, included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
379
        """Return composed version of multiple included versions."""
380
        included = frozenset(included)
893 by Martin Pool
- Refactor weave calculation of inclusions
381
        for origin, lineno, text in self._extract(included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
382
            yield text
383
384
0.1.11 by Martin Pool
Add Knit.dump method
385
    def dump(self, to_file):
386
        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.)
387
        print >>to_file, "Weave._l = ",
0.1.11 by Martin Pool
Add Knit.dump method
388
        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.)
389
        print >>to_file, "Weave._v = ",
0.1.18 by Martin Pool
Better Knit.dump method
390
        pprint(self._v, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
391
392
0.1.91 by Martin Pool
Update Weave.check
393
394
    def numversions(self):
395
        l = len(self._v)
396
        assert l == len(self._sha1s)
397
        return l
398
399
894 by Martin Pool
- small optimization for weave extract
400
    def check(self, progress_bar=None):
0.1.91 by Martin Pool
Update Weave.check
401
        # check no circular inclusions
402
        for version in range(self.numversions()):
403
            inclusions = list(self._v[version])
404
            if inclusions:
405
                inclusions.sort()
406
                if inclusions[-1] >= version:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
407
                    raise WeaveFormatError("invalid included version %d for index %d"
0.1.91 by Martin Pool
Update Weave.check
408
                                           % (inclusions[-1], version))
409
410
        # try extracting all versions; this is a bit slow and parallel
411
        # extraction could be used
412
        import sha
894 by Martin Pool
- small optimization for weave extract
413
        nv = self.numversions()
414
        for version in range(nv):
415
            if progress_bar:
416
                progress_bar.update('checking text', version, nv)
0.1.91 by Martin Pool
Update Weave.check
417
            s = sha.new()
418
            for l in self.get_iter(version):
419
                s.update(l)
420
            hd = s.hexdigest()
421
            expected = self._sha1s[version]
422
            if hd != expected:
423
                raise WeaveError("mismatched sha1 for version %d; "
424
                                 "got %s, expected %s"
425
                                 % (version, hd, expected))
0.1.18 by Martin Pool
Better Knit.dump method
426
881 by Martin Pool
- faster weave extraction
427
        # TODO: check insertions are properly nested, that there are
428
        # no lines outside of insertion blocks, that deletions are
429
        # properly paired, etc.
430
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
431
432
0.1.95 by Martin Pool
- preliminary merge conflict detection
433
    def merge(self, merge_versions):
434
        """Automerge and mark conflicts between versions.
435
436
        This returns a sequence, each entry describing alternatives
437
        for a chunk of the file.  Each of the alternatives is given as
438
        a list of lines.
439
440
        If there is a chunk of the file where there's no diagreement,
441
        only one alternative is given.
442
        """
443
444
        # approach: find the included versions common to all the
445
        # merged versions
446
        raise NotImplementedError()
447
448
449
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
450
    def _delta(self, included, lines):
451
        """Return changes from basis to new revision.
452
453
        The old text for comparison is the union of included revisions.
454
455
        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.
456
0.1.55 by Martin Pool
doc
457
        Delta is returned as a sequence of
458
        (weave1, weave2, newlines).
459
460
        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.
461
        replaced by the sequence of lines in newlines.  Note that
462
        these line numbers are positions in the total weave and don't
463
        correspond to the lines in any extracted version, or even the
464
        extracted union of included versions.
465
466
        If line1=line2, this is a pure insert; if newlines=[] this is a
467
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
468
        """
0.1.54 by Martin Pool
Fix weave line calculation when making deltas
469
        # basis a list of (origin, lineno, line)
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
470
        basis_lineno = []
0.1.83 by Martin Pool
Better delta basis calculation
471
        basis_lines = []
893 by Martin Pool
- Refactor weave calculation of inclusions
472
        for origin, lineno, line in self._extract(included):
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
473
            basis_lineno.append(lineno)
474
            basis_lines.append(line)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
475
476
        # 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
477
        basis_lineno.append(len(self._l))
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
478
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
479
        # XXX: which line of the weave should we really consider
480
        # matches the end of the file?  the current code says it's the
481
        # last line of the weave?
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
482
483
        from difflib import SequenceMatcher
484
        s = SequenceMatcher(None, basis_lines, lines)
485
0.1.55 by Martin Pool
doc
486
        # TODO: Perhaps return line numbers from composed weave as well?
487
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
488
        for tag, i1, i2, j1, j2 in s.get_opcodes():
0.1.23 by Martin Pool
tidy up
489
            ##print tag, i1, i2, j1, j2
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
490
491
            if tag == 'equal':
492
                continue
493
494
            # i1,i2 are given in offsets within basis_lines; we need to map them
495
            # back to offsets within the entire weave
0.1.84 by Martin Pool
Refactor Weave._delta to calculate less unused information
496
            real_i1 = basis_lineno[i1]
497
            real_i2 = basis_lineno[i2]
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
498
0.1.35 by Martin Pool
Clean up Knit._delta method
499
            assert 0 <= j1
500
            assert j1 <= j2
501
            assert j2 <= len(lines)
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
502
0.1.35 by Martin Pool
Clean up Knit._delta method
503
            yield real_i1, real_i2, lines[j1:j2]
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
504
0.1.1 by Martin Pool
Check in old existing knit code.
505
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
506
0.1.88 by Martin Pool
Add weave info command.
507
def weave_info(filename, out):
508
    """Show some text information about the weave."""
509
    from weavefile import read_weave
510
    wf = file(filename, 'rb')
511
    w = read_weave(wf)
512
    # FIXME: doesn't work on pipes
513
    weave_size = wf.tell()
514
    print >>out, "weave file size %d bytes" % weave_size
515
    print >>out, "weave contains %d versions" % len(w._v)
516
517
    total = 0
870 by Martin Pool
- better weave info display
518
    print '%6s %6s %8s %40s %20s' % ('ver', 'lines', 'bytes', 'sha1', 'parents')
519
    for i in (6, 6, 8, 40, 20):
520
        print '-' * i,
521
    print
0.1.88 by Martin Pool
Add weave info command.
522
    for i in range(len(w._v)):
523
        text = w.get(i)
524
        lines = len(text)
525
        bytes = sum((len(a) for a in text))
0.1.91 by Martin Pool
Update Weave.check
526
        sha1 = w._sha1s[i]
870 by Martin Pool
- better weave info display
527
        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
528
        for pv in w._v[i]:
529
            print pv,
530
        print
0.1.88 by Martin Pool
Add weave info command.
531
        total += bytes
532
533
    print >>out, "versions total %d bytes" % total
534
    print >>out, "compression ratio %.3f" % (float(total)/float(weave_size))
869 by Martin Pool
- more weave.py command line options
535
536
537
def usage():
871 by Martin Pool
- add command for merge-based weave
538
    print """bzr weave tool
539
540
Experimental tool for weave algorithm.
541
869 by Martin Pool
- more weave.py command line options
542
usage:
543
    weave init WEAVEFILE
544
        Create an empty weave file
545
    weave get WEAVEFILE VERSION
546
        Write out specified version.
547
    weave check WEAVEFILE
548
        Check consistency of all versions.
549
    weave info WEAVEFILE
550
        Display table of contents.
551
    weave add WEAVEFILE [BASE...] < NEWTEXT
552
        Add NEWTEXT, with specified parent versions.
553
    weave annotate WEAVEFILE VERSION
554
        Display origin of each line.
555
    weave mash WEAVEFILE VERSION...
556
        Display composite of all selected versions.
557
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
558
        Auto-merge two versions and display conflicts.
871 by Martin Pool
- add command for merge-based weave
559
560
example:
561
562
    % weave init foo.weave
563
    % vi foo.txt
564
    % weave add foo.weave < foo.txt
565
    added version 0
566
567
    (create updated version)
568
    % vi foo.txt
569
    % weave get foo.weave 0 | diff -u - foo.txt
570
    % weave add foo.weave 0 < foo.txt
571
    added version 1
572
573
    % weave get foo.weave 0 > foo.txt       (create forked version)
574
    % vi foo.txt
575
    % weave add foo.weave 0 < foo.txt
576
    added version 2
577
578
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
579
    % vi foo.txt                            (resolve conflicts)
580
    % weave add foo.weave 1 2 < foo.txt     (commit merged version)     
581
    
869 by Martin Pool
- more weave.py command line options
582
"""
0.1.88 by Martin Pool
Add weave info command.
583
    
584
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
585
586
def main(argv):
587
    import sys
588
    import os
869 by Martin Pool
- more weave.py command line options
589
    from weavefile import write_weave, read_weave
894 by Martin Pool
- small optimization for weave extract
590
    from bzrlib.progress import ProgressBar
591
592
    #import psyco
593
    #psyco.full()
594
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
595
    cmd = argv[1]
869 by Martin Pool
- more weave.py command line options
596
597
    def readit():
598
        return read_weave(file(argv[2], 'rb'))
599
    
600
    if cmd == 'help':
601
        usage()
602
    elif cmd == 'add':
603
        w = readit()
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
604
        # at the moment, based on everything in the file
869 by Martin Pool
- more weave.py command line options
605
        parents = map(int, argv[3:])
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
606
        lines = sys.stdin.readlines()
0.1.69 by Martin Pool
Simple text-based format for storing weaves, cleaner than
607
        ver = w.add(parents, lines)
869 by Martin Pool
- more weave.py command line options
608
        write_weave(w, file(argv[2], 'wb'))
609
        print 'added version %d' % ver
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
610
    elif cmd == 'init':
611
        fn = argv[2]
612
        if os.path.exists(fn):
613
            raise IOError("file exists")
614
        w = Weave()
869 by Martin Pool
- more weave.py command line options
615
        write_weave(w, file(fn, 'wb'))
616
    elif cmd == 'get': # get one version
617
        w = readit()
0.1.94 by Martin Pool
Fix get_iter call
618
        sys.stdout.writelines(w.get_iter(int(argv[3])))
869 by Martin Pool
- more weave.py command line options
619
        
620
    elif cmd == 'mash': # get composite
621
        w = readit()
622
        sys.stdout.writelines(w.mash_iter(map(int, argv[3:])))
623
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
624
    elif cmd == 'annotate':
869 by Martin Pool
- more weave.py command line options
625
        w = readit()
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
626
        # newline is added to all lines regardless; too hard to get
627
        # reasonable formatting otherwise
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
628
        lasto = None
629
        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.
630
            text = text.rstrip('\r\n')
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
631
            if origin == lasto:
632
                print '      | %s' % (text)
633
            else:
634
                print '%5d | %s' % (origin, text)
635
                lasto = origin
871 by Martin Pool
- add command for merge-based weave
636
                
0.1.88 by Martin Pool
Add weave info command.
637
    elif cmd == 'info':
638
        weave_info(argv[2], sys.stdout)
871 by Martin Pool
- add command for merge-based weave
639
        
0.1.91 by Martin Pool
Update Weave.check
640
    elif cmd == 'check':
869 by Martin Pool
- more weave.py command line options
641
        w = readit()
894 by Martin Pool
- small optimization for weave extract
642
        pb = ProgressBar()
643
        w.check(pb)
644
        pb.clear()
871 by Martin Pool
- add command for merge-based weave
645
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
646
    elif cmd == 'inclusions':
647
        w = readit()
648
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
649
650
    elif cmd == 'parents':
651
        w = readit()
652
        print ' '.join(map(str, w._v[int(argv[3])]))
653
871 by Martin Pool
- add command for merge-based weave
654
    elif cmd == 'merge':
655
        if len(argv) != 5:
656
            usage()
657
            return 1
658
659
        w = readit()
660
        v1, v2 = map(int, argv[3:5])
661
662
        basis = w.inclusions([v1]).intersection(w.inclusions([v2]))
663
664
        base_lines = list(w.mash_iter(basis))
665
        a_lines = list(w.get(v1))
666
        b_lines = list(w.get(v2))
667
668
        from bzrlib.merge3 import Merge3
669
        m3 = Merge3(base_lines, a_lines, b_lines)
670
671
        name_a = 'version %d' % v1
672
        name_b = 'version %d' % v2
673
        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.
674
    else:
675
        raise ValueError('unknown command %r' % cmd)
676
    
677
678
if __name__ == '__main__':
679
    import sys
680
    sys.exit(main(sys.argv))