/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#! /usr/bin/python
2
 
 
3
 
# Copyright (C) 2005 Canonical Ltd
4
 
 
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
18
 
 
19
 
# Author: Martin Pool <mbp@canonical.com>
20
 
 
21
 
 
22
 
"""Weave - storage of related text file versions"""
23
 
 
24
 
# TODO: Perhaps have copy method for Weave instances?
25
 
 
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
 
 
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?
45
 
 
46
 
# TODO: Separate out some code to read and write weaves.
47
 
 
48
 
# TODO: End marker for each version so we can stop reading?
49
 
 
50
 
# TODO: Check that no insertion occurs inside a deletion that was
51
 
# active in the version of the insertion.
52
 
 
53
 
# TODO: Perhaps a special slower check() method that verifies more
54
 
# nesting constraints and the MD5 of each version?
55
 
 
56
 
 
57
 
 
58
 
try:
59
 
    set
60
 
    frozenset
61
 
except NameError:
62
 
    from sets import Set, ImmutableSet
63
 
    set = Set
64
 
    frozenset = ImmutableSet
65
 
    del Set, ImmutableSet
66
 
 
67
 
 
68
 
class WeaveError(Exception):
69
 
    """Exception in processing weave"""
70
 
 
71
 
 
72
 
class WeaveFormatError(WeaveError):
73
 
    """Weave invariant violated"""
74
 
    
75
 
 
76
 
class Weave(object):
77
 
    """weave - versioned text file storage.
78
 
    
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.
86
 
 
87
 
    Texts can be identified in either of two ways:
88
 
 
89
 
    * a nonnegative index number.
90
 
 
91
 
    * a version-id string.
92
 
 
93
 
    Typically the index number will be valid only inside this weave and
94
 
    the version-id is used to reference it in the larger world.
95
 
 
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
107
 
    integer version index.  There is no replace operator, only deletes
108
 
    and inserts.
109
 
 
110
 
    Constraints/notes:
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
 
 
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
 
 
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
 
 
126
 
    * It doesn't seem very useful to have an active insertion
127
 
      inside an inactive insertion, but it might happen.
128
 
      
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.
135
 
 
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
 
 
141
 
    _l
142
 
        Text of the weave.
143
 
 
144
 
    _v
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.
148
 
 
149
 
    _sha1s
150
 
        List of hex SHA-1 of each version, or None if not recorded.
151
 
    """
152
 
    def __init__(self):
153
 
        self._l = []
154
 
        self._v = []
155
 
        self._sha1s = []
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
 
 
168
 
        
169
 
    def add(self, parents, text):
170
 
        """Add a single text on top of the weave.
171
 
  
172
 
        Returns the index number of the newly added version.
173
 
 
174
 
        parents
175
 
            List or set of direct parent version numbers.
176
 
            
177
 
        text
178
 
            Sequence of lines to be added in the new version."""
179
 
        ## self._check_versions(parents)
180
 
        ## self._check_lines(text)
181
 
        idx = len(self._v)
182
 
 
183
 
        import sha
184
 
        s = sha.new()
185
 
        for l in text:
186
 
            s.update(l)
187
 
        sha1 = s.hexdigest()
188
 
        del s
189
 
 
190
 
        if parents:
191
 
            ancestors = self.inclusions(parents)
192
 
            delta = self._delta(ancestors, text)
193
 
 
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
 
 
199
 
            for i1, i2, newlines in delta:
200
 
                assert 0 <= i1
201
 
                assert i1 <= i2
202
 
                assert i2 <= len(self._l)
203
 
 
204
 
                # the deletion and insertion are handled separately.
205
 
                # first delete the region.
206
 
                if i1 != i2:
207
 
                    self._l.insert(i1+offset, ('[', idx))
208
 
                    self._l.insert(i2+offset+1, (']', idx))
209
 
                    offset += 2
210
 
                    # is this OK???
211
 
 
212
 
                if newlines:
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
217
 
                    self._l[i:i] = [('{', idx)] \
218
 
                                   + newlines \
219
 
                                   + [('}', idx)]
220
 
                    offset += 2 + len(newlines)
221
 
 
222
 
            self._addversion(parents)
223
 
        else:
224
 
            # special case; adding with no parents revision; can do this
225
 
            # more quickly by just appending unconditionally
226
 
            self._l.append(('{', idx))
227
 
            self._l += text
228
 
            self._l.append(('}', idx))
229
 
 
230
 
            self._addversion(None)
231
 
 
232
 
        self._sha1s.append(sha1)
233
 
            
234
 
        return idx
235
 
 
236
 
 
237
 
    def inclusions(self, versions):
238
 
        """Return set of all ancestors of given version(s)."""
239
 
        i = set(versions)
240
 
        v = max(versions)
241
 
        try:
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
248
 
        except IndexError:
249
 
            raise ValueError("version %d not present in weave" % v)
250
 
 
251
 
 
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)
259
 
        li.sort(reverse=True)
260
 
 
261
 
        mininc = []
262
 
        gotit = set()
263
 
 
264
 
        for pv in li:
265
 
            if pv not in gotit:
266
 
                mininc.append(pv)
267
 
                gotit.update(self.inclusions(pv))
268
 
 
269
 
        assert mininc[0] >= 0
270
 
        assert mininc[-1] < version
271
 
        return mininc
272
 
 
273
 
 
274
 
    def _addversion(self, parents):
275
 
        if parents:
276
 
            self._v.append(parents)
277
 
        else:
278
 
            self._v.append(frozenset())
279
 
 
280
 
 
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):
287
 
                raise ValueError("text line should be a string or unicode, not %s"
288
 
                                 % type(l))
289
 
        
290
 
 
291
 
 
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
 
 
300
 
    
301
 
    def annotate(self, index):
302
 
        return list(self.annotate_iter(index))
303
 
 
304
 
 
305
 
    def annotate_iter(self, version):
306
 
        """Yield list of (index-id, line) pairs for the specified version.
307
 
 
308
 
        The index indicates when the line originated in the weave."""
309
 
        for origin, lineno, text in self._extract([version]):
310
 
            yield origin, text
311
 
 
312
 
 
313
 
    def _extract(self, versions):
314
 
        """Yield annotation of lines in included set.
315
 
 
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
 
 
320
 
        The set typically but not necessarily corresponds to a version.
321
 
        """
322
 
        included = self.inclusions(versions)
323
 
 
324
 
        istack = []
325
 
        dset = set()
326
 
 
327
 
        lineno = 0         # line of weave, 0-based
328
 
 
329
 
        isactive = False
330
 
 
331
 
        WFE = WeaveFormatError
332
 
 
333
 
        for l in self._l:
334
 
            if isinstance(l, tuple):
335
 
                c, v = l
336
 
                if c == '{':
337
 
                    assert v not in istack
338
 
                    istack.append(v)
339
 
                    if not dset:
340
 
                        isactive = (v in included)
341
 
                elif c == '}':
342
 
                    oldv = istack.pop()
343
 
                    assert oldv == v
344
 
                    isactive = (not dset) and (istack and istack[-1] in included)
345
 
                elif c == '[':
346
 
                    if v in included:
347
 
                        assert v not in dset
348
 
                        dset.add(v)
349
 
                        isactive = False
350
 
                else:
351
 
                    assert c == ']'
352
 
                    if v in included:
353
 
                        assert v in dset
354
 
                        dset.remove(v)
355
 
                        isactive = (not dset) and (istack and istack[-1] in included)
356
 
            else:
357
 
                assert isinstance(l, basestring)
358
 
                if isactive:
359
 
                    yield istack[-1], lineno, l
360
 
            lineno += 1
361
 
 
362
 
        if istack:
363
 
            raise WFE("unclosed insertion blocks at end of weave",
364
 
                                   istack)
365
 
        if dset:
366
 
            raise WFE("unclosed deletion blocks at end of weave",
367
 
                                   dset)
368
 
 
369
 
 
370
 
    def get_iter(self, version):
371
 
        """Yield lines for the specified version."""
372
 
        for origin, lineno, line in self._extract([version]):
373
 
            yield line
374
 
 
375
 
 
376
 
    def get(self, index):
377
 
        return list(self.get_iter(index))
378
 
 
379
 
 
380
 
    def mash_iter(self, included):
381
 
        """Return composed version of multiple included versions."""
382
 
        included = frozenset(included)
383
 
        for origin, lineno, text in self._extract(included):
384
 
            yield text
385
 
 
386
 
 
387
 
    def dump(self, to_file):
388
 
        from pprint import pprint
389
 
        print >>to_file, "Weave._l = ",
390
 
        pprint(self._l, to_file)
391
 
        print >>to_file, "Weave._v = ",
392
 
        pprint(self._v, to_file)
393
 
 
394
 
 
395
 
 
396
 
    def numversions(self):
397
 
        l = len(self._v)
398
 
        assert l == len(self._sha1s)
399
 
        return l
400
 
 
401
 
 
402
 
    def check(self):
403
 
        # check no circular inclusions
404
 
        for version in range(self.numversions()):
405
 
            inclusions = list(self._v[version])
406
 
            if inclusions:
407
 
                inclusions.sort()
408
 
                if inclusions[-1] >= version:
409
 
                    raise WeaveFormatError("invalid included version %d for index %d"
410
 
                                           % (inclusions[-1], version))
411
 
 
412
 
        # try extracting all versions; this is a bit slow and parallel
413
 
        # extraction could be used
414
 
        import sha
415
 
        for version in range(self.numversions()):
416
 
            s = sha.new()
417
 
            for l in self.get_iter(version):
418
 
                s.update(l)
419
 
            hd = s.hexdigest()
420
 
            expected = self._sha1s[version]
421
 
            if hd != expected:
422
 
                raise WeaveError("mismatched sha1 for version %d; "
423
 
                                 "got %s, expected %s"
424
 
                                 % (version, hd, expected))
425
 
 
426
 
        # TODO: check insertions are properly nested, that there are
427
 
        # no lines outside of insertion blocks, that deletions are
428
 
        # properly paired, etc.
429
 
 
430
 
 
431
 
 
432
 
    def merge(self, merge_versions):
433
 
        """Automerge and mark conflicts between versions.
434
 
 
435
 
        This returns a sequence, each entry describing alternatives
436
 
        for a chunk of the file.  Each of the alternatives is given as
437
 
        a list of lines.
438
 
 
439
 
        If there is a chunk of the file where there's no diagreement,
440
 
        only one alternative is given.
441
 
        """
442
 
 
443
 
        # approach: find the included versions common to all the
444
 
        # merged versions
445
 
        raise NotImplementedError()
446
 
 
447
 
 
448
 
 
449
 
    def _delta(self, included, lines):
450
 
        """Return changes from basis to new revision.
451
 
 
452
 
        The old text for comparison is the union of included revisions.
453
 
 
454
 
        This is used in inserting a new text.
455
 
 
456
 
        Delta is returned as a sequence of
457
 
        (weave1, weave2, newlines).
458
 
 
459
 
        This indicates that weave1:weave2 of the old weave should be
460
 
        replaced by the sequence of lines in newlines.  Note that
461
 
        these line numbers are positions in the total weave and don't
462
 
        correspond to the lines in any extracted version, or even the
463
 
        extracted union of included versions.
464
 
 
465
 
        If line1=line2, this is a pure insert; if newlines=[] this is a
466
 
        pure delete.  (Similar to difflib.)
467
 
        """
468
 
        # basis a list of (origin, lineno, line)
469
 
        basis_lineno = []
470
 
        basis_lines = []
471
 
        for origin, lineno, line in self._extract(included):
472
 
            basis_lineno.append(lineno)
473
 
            basis_lines.append(line)
474
 
 
475
 
        # add a sentinal, because we can also match against the final line
476
 
        basis_lineno.append(len(self._l))
477
 
 
478
 
        # XXX: which line of the weave should we really consider
479
 
        # matches the end of the file?  the current code says it's the
480
 
        # last line of the weave?
481
 
 
482
 
        from difflib import SequenceMatcher
483
 
        s = SequenceMatcher(None, basis_lines, lines)
484
 
 
485
 
        # TODO: Perhaps return line numbers from composed weave as well?
486
 
 
487
 
        for tag, i1, i2, j1, j2 in s.get_opcodes():
488
 
            ##print tag, i1, i2, j1, j2
489
 
 
490
 
            if tag == 'equal':
491
 
                continue
492
 
 
493
 
            # i1,i2 are given in offsets within basis_lines; we need to map them
494
 
            # back to offsets within the entire weave
495
 
            real_i1 = basis_lineno[i1]
496
 
            real_i2 = basis_lineno[i2]
497
 
 
498
 
            assert 0 <= j1
499
 
            assert j1 <= j2
500
 
            assert j2 <= len(lines)
501
 
 
502
 
            yield real_i1, real_i2, lines[j1:j2]
503
 
 
504
 
 
505
 
 
506
 
def weave_info(filename, out):
507
 
    """Show some text information about the weave."""
508
 
    from weavefile import read_weave
509
 
    wf = file(filename, 'rb')
510
 
    w = read_weave(wf)
511
 
    # FIXME: doesn't work on pipes
512
 
    weave_size = wf.tell()
513
 
    print >>out, "weave file size %d bytes" % weave_size
514
 
    print >>out, "weave contains %d versions" % len(w._v)
515
 
 
516
 
    total = 0
517
 
    print '%6s %6s %8s %40s %20s' % ('ver', 'lines', 'bytes', 'sha1', 'parents')
518
 
    for i in (6, 6, 8, 40, 20):
519
 
        print '-' * i,
520
 
    print
521
 
    for i in range(len(w._v)):
522
 
        text = w.get(i)
523
 
        lines = len(text)
524
 
        bytes = sum((len(a) for a in text))
525
 
        sha1 = w._sha1s[i]
526
 
        print '%6d %6d %8d %40s' % (i, lines, bytes, sha1),
527
 
        for pv in w._v[i]:
528
 
            print pv,
529
 
        print
530
 
        total += bytes
531
 
 
532
 
    print >>out, "versions total %d bytes" % total
533
 
    print >>out, "compression ratio %.3f" % (float(total)/float(weave_size))
534
 
 
535
 
 
536
 
def usage():
537
 
    print """bzr weave tool
538
 
 
539
 
Experimental tool for weave algorithm.
540
 
 
541
 
usage:
542
 
    weave init WEAVEFILE
543
 
        Create an empty weave file
544
 
    weave get WEAVEFILE VERSION
545
 
        Write out specified version.
546
 
    weave check WEAVEFILE
547
 
        Check consistency of all versions.
548
 
    weave info WEAVEFILE
549
 
        Display table of contents.
550
 
    weave add WEAVEFILE [BASE...] < NEWTEXT
551
 
        Add NEWTEXT, with specified parent versions.
552
 
    weave annotate WEAVEFILE VERSION
553
 
        Display origin of each line.
554
 
    weave mash WEAVEFILE VERSION...
555
 
        Display composite of all selected versions.
556
 
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
557
 
        Auto-merge two versions and display conflicts.
558
 
 
559
 
example:
560
 
 
561
 
    % weave init foo.weave
562
 
    % vi foo.txt
563
 
    % weave add foo.weave < foo.txt
564
 
    added version 0
565
 
 
566
 
    (create updated version)
567
 
    % vi foo.txt
568
 
    % weave get foo.weave 0 | diff -u - foo.txt
569
 
    % weave add foo.weave 0 < foo.txt
570
 
    added version 1
571
 
 
572
 
    % weave get foo.weave 0 > foo.txt       (create forked version)
573
 
    % vi foo.txt
574
 
    % weave add foo.weave 0 < foo.txt
575
 
    added version 2
576
 
 
577
 
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
578
 
    % vi foo.txt                            (resolve conflicts)
579
 
    % weave add foo.weave 1 2 < foo.txt     (commit merged version)     
580
 
    
581
 
"""
582
 
    
583
 
 
584
 
 
585
 
def main(argv):
586
 
    import sys
587
 
    import os
588
 
    from weavefile import write_weave, read_weave
589
 
    cmd = argv[1]
590
 
 
591
 
    def readit():
592
 
        return read_weave(file(argv[2], 'rb'))
593
 
    
594
 
    if cmd == 'help':
595
 
        usage()
596
 
    elif cmd == 'add':
597
 
        w = readit()
598
 
        # at the moment, based on everything in the file
599
 
        parents = map(int, argv[3:])
600
 
        lines = sys.stdin.readlines()
601
 
        ver = w.add(parents, lines)
602
 
        write_weave(w, file(argv[2], 'wb'))
603
 
        print 'added version %d' % ver
604
 
    elif cmd == 'init':
605
 
        fn = argv[2]
606
 
        if os.path.exists(fn):
607
 
            raise IOError("file exists")
608
 
        w = Weave()
609
 
        write_weave(w, file(fn, 'wb'))
610
 
    elif cmd == 'get': # get one version
611
 
        w = readit()
612
 
        sys.stdout.writelines(w.get_iter(int(argv[3])))
613
 
        
614
 
    elif cmd == 'mash': # get composite
615
 
        w = readit()
616
 
        sys.stdout.writelines(w.mash_iter(map(int, argv[3:])))
617
 
 
618
 
    elif cmd == 'annotate':
619
 
        w = readit()
620
 
        # newline is added to all lines regardless; too hard to get
621
 
        # reasonable formatting otherwise
622
 
        lasto = None
623
 
        for origin, text in w.annotate(int(argv[3])):
624
 
            text = text.rstrip('\r\n')
625
 
            if origin == lasto:
626
 
                print '      | %s' % (text)
627
 
            else:
628
 
                print '%5d | %s' % (origin, text)
629
 
                lasto = origin
630
 
                
631
 
    elif cmd == 'info':
632
 
        weave_info(argv[2], sys.stdout)
633
 
        
634
 
    elif cmd == 'check':
635
 
        w = readit()
636
 
        w.check()
637
 
 
638
 
    elif cmd == 'inclusions':
639
 
        w = readit()
640
 
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
641
 
 
642
 
    elif cmd == 'parents':
643
 
        w = readit()
644
 
        print ' '.join(map(str, w._v[int(argv[3])]))
645
 
 
646
 
    elif cmd == 'merge':
647
 
        if len(argv) != 5:
648
 
            usage()
649
 
            return 1
650
 
 
651
 
        w = readit()
652
 
        v1, v2 = map(int, argv[3:5])
653
 
 
654
 
        basis = w.inclusions([v1]).intersection(w.inclusions([v2]))
655
 
 
656
 
        base_lines = list(w.mash_iter(basis))
657
 
        a_lines = list(w.get(v1))
658
 
        b_lines = list(w.get(v2))
659
 
 
660
 
        from bzrlib.merge3 import Merge3
661
 
        m3 = Merge3(base_lines, a_lines, b_lines)
662
 
 
663
 
        name_a = 'version %d' % v1
664
 
        name_b = 'version %d' % v2
665
 
        sys.stdout.writelines(m3.merge_lines(name_a=name_a, name_b=name_b))
666
 
    else:
667
 
        raise ValueError('unknown command %r' % cmd)
668
 
    
669
 
 
670
 
if __name__ == '__main__':
671
 
    import sys
672
 
    sys.exit(main(sys.argv))