~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/weave.py

  • Committer: Martin Pool
  • Date: 2005-10-05 03:02:11 UTC
  • mto: (1185.13.3)
  • mto: This revision was merged to the branch mainline in revision 1403.
  • Revision ID: mbp@sourcefrog.net-20051005030211-e2b47edabbbddd34
- Weave._delta is not implemented at the moment

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
 
 
25
# TODO: Perhaps have copy method for Weave instances?
 
26
 
 
27
# XXX: If we do weaves this way, will a merge still behave the same
 
28
# way if it's done in a different order?  That's a pretty desirable
 
29
# property.
 
30
 
 
31
# TODO: Nothing here so far assumes the lines are really \n newlines,
 
32
# rather than being split up in some other way.  We could accomodate
 
33
# binaries, perhaps by naively splitting on \n or perhaps using
 
34
# something like a rolling checksum.
 
35
 
 
36
# TODO: End marker for each version so we can stop reading?
 
37
 
 
38
# TODO: Check that no insertion occurs inside a deletion that was
 
39
# active in the version of the insertion.
 
40
 
 
41
# TODO: In addition to the SHA-1 check, perhaps have some code that
 
42
# checks structural constraints of the weave: ie that insertions are
 
43
# properly nested, that there is no text outside of an insertion, that
 
44
# insertions or deletions are not repeated, etc.
 
45
 
 
46
# TODO: Parallel-extract that passes back each line along with a
 
47
# description of which revisions include it.  Nice for checking all
 
48
# shas or calculating stats in parallel.
 
49
 
 
50
# TODO: Using a single _extract routine and then processing the output
 
51
# is probably inefficient.  It's simple enough that we can afford to
 
52
# have slight specializations for different ways its used: annotate,
 
53
# basis for add, get, etc.
 
54
 
 
55
# TODO: Perhaps the API should work only in names to hide the integer
 
56
# indexes from the user?
 
57
 
 
58
# TODO: Is there any potential performance win by having an add()
 
59
# variant that is passed a pre-cooked version of the single basis
 
60
# version?
 
61
 
 
62
 
 
63
 
 
64
import sha
 
65
from difflib import SequenceMatcher
 
66
 
 
67
 
 
68
 
 
69
 
 
70
class WeaveError(Exception):
 
71
    """Exception in processing weave"""
 
72
 
 
73
 
 
74
class WeaveFormatError(WeaveError):
 
75
    """Weave invariant violated"""
 
76
    
 
77
 
 
78
class Weave(object):
 
79
    """weave - versioned text file storage.
 
80
    
 
81
    A Weave manages versions of line-based text files, keeping track
 
82
    of the originating version for each line.
 
83
 
 
84
    To clients the "lines" of the file are represented as a list of strings.
 
85
    These strings  will typically have terminal newline characters, but
 
86
    this is not required.  In particular files commonly do not have a newline
 
87
    at the end of the file.
 
88
 
 
89
    Texts can be identified in either of two ways:
 
90
 
 
91
    * a nonnegative index number.
 
92
 
 
93
    * a version-id string.
 
94
 
 
95
    Typically the index number will be valid only inside this weave and
 
96
    the version-id is used to reference it in the larger world.
 
97
 
 
98
    The weave is represented as a list mixing edit instructions and
 
99
    literal text.  Each entry in _weave can be either a string (or
 
100
    unicode), or a tuple.  If a string, it means that the given line
 
101
    should be output in the currently active revisions.
 
102
 
 
103
    If a tuple, it gives a processing instruction saying in which
 
104
    revisions the enclosed lines are active.  The tuple has the form
 
105
    (instruction, version).
 
106
 
 
107
    The instruction can be '{' or '}' for an insertion block, and '['
 
108
    and ']' for a deletion block respectively.  The version is the
 
109
    integer version index.  There is no replace operator, only deletes
 
110
    and inserts.  For '}', the end of an insertion, there is no
 
111
    version parameter because it always closes the most recently
 
112
    opened insertion.
 
113
 
 
114
    Constraints/notes:
 
115
 
 
116
    * A later version can delete lines that were introduced by any
 
117
      number of ancestor versions; this implies that deletion
 
118
      instructions can span insertion blocks without regard to the
 
119
      insertion block's nesting.
 
120
 
 
121
    * Similarly, deletions need not be properly nested with regard to
 
122
      each other, because they might have been generated by
 
123
      independent revisions.
 
124
 
 
125
    * Insertions are always made by inserting a new bracketed block
 
126
      into a single point in the previous weave.  This implies they
 
127
      can nest but not overlap, and the nesting must always have later
 
128
      insertions on the inside.
 
129
 
 
130
    * It doesn't seem very useful to have an active insertion
 
131
      inside an inactive insertion, but it might happen.
 
132
      
 
133
    * Therefore, all instructions are always"considered"; that
 
134
      is passed onto and off the stack.  An outer inactive block
 
135
      doesn't disable an inner block.
 
136
 
 
137
    * Lines are enabled if the most recent enclosing insertion is
 
138
      active and none of the enclosing deletions are active.
 
139
 
 
140
    * There is no point having a deletion directly inside its own
 
141
      insertion; you might as well just not write it.  And there
 
142
      should be no way to get an earlier version deleting a later
 
143
      version.
 
144
 
 
145
    _weave
 
146
        Text of the weave; list of control instruction tuples and strings.
 
147
 
 
148
    _parents
 
149
        List of parents, indexed by version number.
 
150
        It is only necessary to store the minimal set of parents for
 
151
        each version; the parent's parents are implied.
 
152
 
 
153
    _sha1s
 
154
        List of hex SHA-1 of each version.
 
155
 
 
156
    _names
 
157
        List of symbolic names for each version.  Each should be unique.
 
158
 
 
159
    _name_map
 
160
        For each name, the version number.
 
161
 
 
162
    _weave_name
 
163
        Descriptive name of this weave; typically the filename if known.
 
164
        Set by read_weave.
 
165
    """
 
166
 
 
167
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
 
168
                 '_weave_name']
 
169
    
 
170
    def __init__(self, weave_name=None):
 
171
        self._weave = []
 
172
        self._parents = []
 
173
        self._sha1s = []
 
174
        self._names = []
 
175
        self._name_map = {}
 
176
        self._weave_name = weave_name
 
177
 
 
178
 
 
179
    def __eq__(self, other):
 
180
        if not isinstance(other, Weave):
 
181
            return False
 
182
        return self._parents == other._parents \
 
183
               and self._weave == other._weave \
 
184
               and self._sha1s == other._sha1s 
 
185
 
 
186
    
 
187
    def __ne__(self, other):
 
188
        return not self.__eq__(other)
 
189
 
 
190
 
 
191
    def maybe_lookup(self, name_or_index):
 
192
        """Convert possible symbolic name to index, or pass through indexes."""
 
193
        if isinstance(name_or_index, (int, long)):
 
194
            return name_or_index
 
195
        else:
 
196
            return self.lookup(name_or_index)
 
197
 
 
198
        
 
199
    def lookup(self, name):
 
200
        """Convert symbolic version name to index."""
 
201
        try:
 
202
            return self._name_map[name]
 
203
        except KeyError:
 
204
            raise WeaveError("name %r not present in weave %r" %
 
205
                             (name, self._weave_name))
 
206
 
 
207
 
 
208
    def idx_to_name(self, version):
 
209
        return self._names[version]
 
210
 
 
211
 
 
212
    def _check_repeated_add(self, name, parents, text, sha1):
 
213
        """Check that a duplicated add is OK.
 
214
 
 
215
        If it is, return the (old) index; otherwise raise an exception.
 
216
        """
 
217
        idx = self.lookup(name)
 
218
        if sorted(self._parents[idx]) != sorted(parents):
 
219
            raise WeaveError("name \"%s\" already present in weave "
 
220
                             "with different parents" % name)
 
221
        if sha1 != self._sha1s[idx]:
 
222
            raise WeaveError("name \"%s\" already present in weave "
 
223
                             "with different text" % name)            
 
224
        return idx
 
225
        
 
226
 
 
227
        
 
228
    def add(self, name, parents, text, sha1=None):
 
229
        """Add a single text on top of the weave.
 
230
  
 
231
        Returns the index number of the newly added version.
 
232
 
 
233
        name
 
234
            Symbolic name for this version.
 
235
            (Typically the revision-id of the revision that added it.)
 
236
 
 
237
        parents
 
238
            List or set of direct parent version numbers.
 
239
            
 
240
        text
 
241
            Sequence of lines to be added in the new version.
 
242
 
 
243
        sha -- SHA-1 of the file, if known.  This is trusted to be
 
244
            correct if supplied.
 
245
        """
 
246
        from bzrlib.osutils import sha_strings
 
247
 
 
248
        assert isinstance(name, basestring)
 
249
        if sha1 is None:
 
250
            sha1 = sha_strings(text)
 
251
        if name in self._name_map:
 
252
            return self._check_repeated_add(name, parents, text, sha1)
 
253
 
 
254
        parents = map(self.maybe_lookup, parents)
 
255
        self._check_versions(parents)
 
256
        ## self._check_lines(text)
 
257
        new_version = len(self._parents)
 
258
 
 
259
 
 
260
        # if we abort after here the (in-memory) weave will be corrupt because only
 
261
        # some fields are updated
 
262
        self._parents.append(parents[:])
 
263
        self._sha1s.append(sha1)
 
264
        self._names.append(name)
 
265
        self._name_map[name] = new_version
 
266
 
 
267
            
 
268
        if not parents:
 
269
            # special case; adding with no parents revision; can do
 
270
            # this more quickly by just appending unconditionally.
 
271
            # even more specially, if we're adding an empty text we
 
272
            # need do nothing at all.
 
273
            if text:
 
274
                self._weave.append(('{', new_version))
 
275
                self._weave.extend(text)
 
276
                self._weave.append(('}', None))
 
277
        
 
278
            return new_version
 
279
 
 
280
        if len(parents) == 1:
 
281
            pv = list(parents)[0]
 
282
            if sha1 == self._sha1s[pv]:
 
283
                # special case: same as the single parent
 
284
                return new_version
 
285
            
 
286
 
 
287
        ancestors = self.inclusions(parents)
 
288
 
 
289
        l = self._weave
 
290
 
 
291
        # basis a list of (origin, lineno, line)
 
292
        basis_lineno = []
 
293
        basis_lines = []
 
294
        for origin, lineno, line in self._extract(ancestors):
 
295
            basis_lineno.append(lineno)
 
296
            basis_lines.append(line)
 
297
 
 
298
        # another small special case: a merge, producing the same text
 
299
        # as auto-merge
 
300
        if text == basis_lines:
 
301
            return new_version            
 
302
 
 
303
        # add a sentinal, because we can also match against the final line
 
304
        basis_lineno.append(len(self._weave))
 
305
 
 
306
        # XXX: which line of the weave should we really consider
 
307
        # matches the end of the file?  the current code says it's the
 
308
        # last line of the weave?
 
309
 
 
310
        #print 'basis_lines:', basis_lines
 
311
        #print 'new_lines:  ', lines
 
312
 
 
313
        s = SequenceMatcher(None, basis_lines, text)
 
314
 
 
315
        # offset gives the number of lines that have been inserted
 
316
        # into the weave up to the current point; if the original edit instruction
 
317
        # says to change line A then we actually change (A+offset)
 
318
        offset = 0
 
319
 
 
320
        for tag, i1, i2, j1, j2 in s.get_opcodes():
 
321
            # i1,i2 are given in offsets within basis_lines; we need to map them
 
322
            # back to offsets within the entire weave
 
323
            #print 'raw match', tag, i1, i2, j1, j2
 
324
            if tag == 'equal':
 
325
                continue
 
326
 
 
327
            i1 = basis_lineno[i1]
 
328
            i2 = basis_lineno[i2]
 
329
 
 
330
            assert 0 <= j1 <= j2 <= len(text)
 
331
 
 
332
            #print tag, i1, i2, j1, j2
 
333
 
 
334
            # the deletion and insertion are handled separately.
 
335
            # first delete the region.
 
336
            if i1 != i2:
 
337
                self._weave.insert(i1+offset, ('[', new_version))
 
338
                self._weave.insert(i2+offset+1, (']', new_version))
 
339
                offset += 2
 
340
 
 
341
            if j1 != j2:
 
342
                # there may have been a deletion spanning up to
 
343
                # i2; we want to insert after this region to make sure
 
344
                # we don't destroy ourselves
 
345
                i = i2 + offset
 
346
                self._weave[i:i] = ([('{', new_version)] 
 
347
                                    + text[j1:j2] 
 
348
                                    + [('}', None)])
 
349
                offset += 2 + (j2 - j1)
 
350
 
 
351
        return new_version
 
352
 
 
353
    def add_identical(self, old_rev_id, new_rev_id, parents):
 
354
        """Add an identical text to old_rev_id as new_rev_id."""
 
355
        old_lines = self.get(self.lookup(old_rev_id))
 
356
        self.add(new_rev_id, parents, old_lines)
 
357
 
 
358
    def inclusions(self, versions):
 
359
        """Return set of all ancestors of given version(s)."""
 
360
        i = set(versions)
 
361
        for v in xrange(max(versions), 0, -1):
 
362
            if v in i:
 
363
                # include all its parents
 
364
                i.update(self._parents[v])
 
365
        return i
 
366
        ## except IndexError:
 
367
        ##     raise ValueError("version %d not present in weave" % v)
 
368
 
 
369
 
 
370
    def parents(self, version):
 
371
        return self._parents[version]
 
372
 
 
373
 
 
374
    def minimal_parents(self, version):
 
375
        """Find the minimal set of parents for the version."""
 
376
        included = self._parents[version]
 
377
        if not included:
 
378
            return []
 
379
        
 
380
        li = list(included)
 
381
        li.sort(reverse=True)
 
382
 
 
383
        mininc = []
 
384
        gotit = set()
 
385
 
 
386
        for pv in li:
 
387
            if pv not in gotit:
 
388
                mininc.append(pv)
 
389
                gotit.update(self.inclusions(pv))
 
390
 
 
391
        assert mininc[0] >= 0
 
392
        assert mininc[-1] < version
 
393
        return mininc
 
394
 
 
395
 
 
396
 
 
397
    def _check_lines(self, text):
 
398
        if not isinstance(text, list):
 
399
            raise ValueError("text should be a list, not %s" % type(text))
 
400
 
 
401
        for l in text:
 
402
            if not isinstance(l, basestring):
 
403
                raise ValueError("text line should be a string or unicode, not %s"
 
404
                                 % type(l))
 
405
        
 
406
 
 
407
 
 
408
    def _check_versions(self, indexes):
 
409
        """Check everything in the sequence of indexes is valid"""
 
410
        for i in indexes:
 
411
            try:
 
412
                self._parents[i]
 
413
            except IndexError:
 
414
                raise IndexError("invalid version number %r" % i)
 
415
 
 
416
    
 
417
    def annotate(self, name_or_index):
 
418
        return list(self.annotate_iter(name_or_index))
 
419
 
 
420
 
 
421
    def annotate_iter(self, name_or_index):
 
422
        """Yield list of (index-id, line) pairs for the specified version.
 
423
 
 
424
        The index indicates when the line originated in the weave."""
 
425
        incls = [self.maybe_lookup(name_or_index)]
 
426
        for origin, lineno, text in self._extract(incls):
 
427
            yield origin, text
 
428
 
 
429
 
 
430
    def _walk(self):
 
431
        """Walk the weave.
 
432
 
 
433
        Yields sequence of
 
434
        (lineno, insert, deletes, text)
 
435
        for each literal line.
 
436
        """
 
437
        
 
438
        istack = []
 
439
        dset = set()
 
440
 
 
441
        lineno = 0         # line of weave, 0-based
 
442
 
 
443
        for l in self._weave:
 
444
            if isinstance(l, tuple):
 
445
                c, v = l
 
446
                isactive = None
 
447
                if c == '{':
 
448
                    istack.append(v)
 
449
                elif c == '}':
 
450
                    istack.pop()
 
451
                elif c == '[':
 
452
                    assert v not in dset
 
453
                    dset.add(v)
 
454
                elif c == ']':
 
455
                    dset.remove(v)
 
456
                else:
 
457
                    raise WeaveFormatError('unexpected instruction %r'
 
458
                                           % v)
 
459
            else:
 
460
                assert isinstance(l, basestring)
 
461
                assert istack
 
462
                yield lineno, istack[-1], dset, l
 
463
            lineno += 1
 
464
 
 
465
 
 
466
 
 
467
    def _extract(self, versions):
 
468
        """Yield annotation of lines in included set.
 
469
 
 
470
        Yields a sequence of tuples (origin, lineno, text), where
 
471
        origin is the origin version, lineno the index in the weave,
 
472
        and text the text of the line.
 
473
 
 
474
        The set typically but not necessarily corresponds to a version.
 
475
        """
 
476
        for i in versions:
 
477
            if not isinstance(i, int):
 
478
                raise ValueError(i)
 
479
            
 
480
        included = self.inclusions(versions)
 
481
 
 
482
        istack = []
 
483
        dset = set()
 
484
 
 
485
        lineno = 0         # line of weave, 0-based
 
486
 
 
487
        isactive = None
 
488
 
 
489
        result = []
 
490
 
 
491
        WFE = WeaveFormatError
 
492
 
 
493
        for l in self._weave:
 
494
            if isinstance(l, tuple):
 
495
                c, v = l
 
496
                isactive = None
 
497
                if c == '{':
 
498
                    assert v not in istack
 
499
                    istack.append(v)
 
500
                elif c == '}':
 
501
                    istack.pop()
 
502
                elif c == '[':
 
503
                    if v in included:
 
504
                        assert v not in dset
 
505
                        dset.add(v)
 
506
                else:
 
507
                    assert c == ']'
 
508
                    if v in included:
 
509
                        assert v in dset
 
510
                        dset.remove(v)
 
511
            else:
 
512
                assert isinstance(l, basestring)
 
513
                if isactive is None:
 
514
                    isactive = (not dset) and istack and (istack[-1] in included)
 
515
                if isactive:
 
516
                    result.append((istack[-1], lineno, l))
 
517
            lineno += 1
 
518
 
 
519
        if istack:
 
520
            raise WFE("unclosed insertion blocks at end of weave",
 
521
                                   istack)
 
522
        if dset:
 
523
            raise WFE("unclosed deletion blocks at end of weave",
 
524
                                   dset)
 
525
 
 
526
        return result
 
527
    
 
528
 
 
529
 
 
530
    def get_iter(self, name_or_index):
 
531
        """Yield lines for the specified version."""
 
532
        incls = [self.maybe_lookup(name_or_index)]
 
533
        for origin, lineno, line in self._extract(incls):
 
534
            yield line
 
535
 
 
536
 
 
537
    def get_text(self, name_or_index):
 
538
        return ''.join(self.get_iter(name_or_index))
 
539
        assert isinstance(version, int)
 
540
 
 
541
 
 
542
    def get_lines(self, name_or_index):
 
543
        return list(self.get_iter(name_or_index))
 
544
 
 
545
 
 
546
    get = get_lines
 
547
 
 
548
 
 
549
    def mash_iter(self, included):
 
550
        """Return composed version of multiple included versions."""
 
551
        included = map(self.maybe_lookup, included)
 
552
        for origin, lineno, text in self._extract(included):
 
553
            yield text
 
554
 
 
555
 
 
556
    def dump(self, to_file):
 
557
        from pprint import pprint
 
558
        print >>to_file, "Weave._weave = ",
 
559
        pprint(self._weave, to_file)
 
560
        print >>to_file, "Weave._parents = ",
 
561
        pprint(self._parents, to_file)
 
562
 
 
563
 
 
564
 
 
565
    def numversions(self):
 
566
        l = len(self._parents)
 
567
        assert l == len(self._sha1s)
 
568
        return l
 
569
 
 
570
 
 
571
    def __len__(self):
 
572
        return self.numversions()
 
573
 
 
574
 
 
575
    def check(self, progress_bar=None):
 
576
        # check no circular inclusions
 
577
        for version in range(self.numversions()):
 
578
            inclusions = list(self._parents[version])
 
579
            if inclusions:
 
580
                inclusions.sort()
 
581
                if inclusions[-1] >= version:
 
582
                    raise WeaveFormatError("invalid included version %d for index %d"
 
583
                                           % (inclusions[-1], version))
 
584
 
 
585
        # try extracting all versions; this is a bit slow and parallel
 
586
        # extraction could be used
 
587
        nv = self.numversions()
 
588
        for version in range(nv):
 
589
            if progress_bar:
 
590
                progress_bar.update('checking text', version, nv)
 
591
            s = sha.new()
 
592
            for l in self.get_iter(version):
 
593
                s.update(l)
 
594
            hd = s.hexdigest()
 
595
            expected = self._sha1s[version]
 
596
            if hd != expected:
 
597
                raise WeaveError("mismatched sha1 for version %d; "
 
598
                                 "got %s, expected %s"
 
599
                                 % (version, hd, expected))
 
600
 
 
601
        # TODO: check insertions are properly nested, that there are
 
602
        # no lines outside of insertion blocks, that deletions are
 
603
        # properly paired, etc.
 
604
 
 
605
 
 
606
 
 
607
    def merge(self, merge_versions):
 
608
        """Automerge and mark conflicts between versions.
 
609
 
 
610
        This returns a sequence, each entry describing alternatives
 
611
        for a chunk of the file.  Each of the alternatives is given as
 
612
        a list of lines.
 
613
 
 
614
        If there is a chunk of the file where there's no diagreement,
 
615
        only one alternative is given.
 
616
        """
 
617
        # approach: find the included versions common to all the
 
618
        # merged versions
 
619
        raise NotImplementedError()
 
620
 
 
621
 
 
622
 
 
623
    def _delta(self, included, lines):
 
624
        """Return changes from basis to new revision.
 
625
 
 
626
        The old text for comparison is the union of included revisions.
 
627
 
 
628
        This is used in inserting a new text.
 
629
 
 
630
        Delta is returned as a sequence of
 
631
        (weave1, weave2, newlines).
 
632
 
 
633
        This indicates that weave1:weave2 of the old weave should be
 
634
        replaced by the sequence of lines in newlines.  Note that
 
635
        these line numbers are positions in the total weave and don't
 
636
        correspond to the lines in any extracted version, or even the
 
637
        extracted union of included versions.
 
638
 
 
639
        If line1=line2, this is a pure insert; if newlines=[] this is a
 
640
        pure delete.  (Similar to difflib.)
 
641
        """
 
642
        raise NotImplementedError()
 
643
 
 
644
            
 
645
    def plan_merge(self, ver_a, ver_b):
 
646
        """Return pseudo-annotation indicating how the two versions merge.
 
647
 
 
648
        This is computed between versions a and b and their common
 
649
        base.
 
650
 
 
651
        Weave lines present in none of them are skipped entirely.
 
652
        """
 
653
        inc_a = self.inclusions([ver_a])
 
654
        inc_b = self.inclusions([ver_b])
 
655
        inc_c = inc_a & inc_b
 
656
 
 
657
        for lineno, insert, deleteset, line in self._walk():
 
658
            if deleteset & inc_c:
 
659
                # killed in parent; can't be in either a or b
 
660
                # not relevant to our work
 
661
                yield 'killed-base', line
 
662
            elif insert in inc_c:
 
663
                # was inserted in base
 
664
                killed_a = bool(deleteset & inc_a)
 
665
                killed_b = bool(deleteset & inc_b)
 
666
                if killed_a and killed_b:
 
667
                    yield 'killed-both', line
 
668
                elif killed_a:
 
669
                    yield 'killed-a', line
 
670
                elif killed_b:
 
671
                    yield 'killed-b', line
 
672
                else:
 
673
                    yield 'unchanged', line
 
674
            elif insert in inc_a:
 
675
                if deleteset & inc_a:
 
676
                    yield 'ghost-a', line
 
677
                else:
 
678
                    # new in A; not in B
 
679
                    yield 'new-a', line
 
680
            elif insert in inc_b:
 
681
                if deleteset & inc_b:
 
682
                    yield 'ghost-b', line
 
683
                else:
 
684
                    yield 'new-b', line
 
685
            else:
 
686
                # not in either revision
 
687
                yield 'irrelevant', line
 
688
 
 
689
        yield 'unchanged', ''           # terminator
 
690
 
 
691
 
 
692
 
 
693
    def weave_merge(self, plan):
 
694
        lines_a = []
 
695
        lines_b = []
 
696
        ch_a = ch_b = False
 
697
 
 
698
        for state, line in plan:
 
699
            if state == 'unchanged' or state == 'killed-both':
 
700
                # resync and flush queued conflicts changes if any
 
701
                if not lines_a and not lines_b:
 
702
                    pass
 
703
                elif ch_a and not ch_b:
 
704
                    # one-sided change:                    
 
705
                    for l in lines_a: yield l
 
706
                elif ch_b and not ch_a:
 
707
                    for l in lines_b: yield l
 
708
                elif lines_a == lines_b:
 
709
                    for l in lines_a: yield l
 
710
                else:
 
711
                    yield '<<<<\n'
 
712
                    for l in lines_a: yield l
 
713
                    yield '====\n'
 
714
                    for l in lines_b: yield l
 
715
                    yield '>>>>\n'
 
716
 
 
717
                del lines_a[:]
 
718
                del lines_b[:]
 
719
                ch_a = ch_b = False
 
720
                
 
721
            if state == 'unchanged':
 
722
                if line:
 
723
                    yield line
 
724
            elif state == 'killed-a':
 
725
                ch_a = True
 
726
                lines_b.append(line)
 
727
            elif state == 'killed-b':
 
728
                ch_b = True
 
729
                lines_a.append(line)
 
730
            elif state == 'new-a':
 
731
                ch_a = True
 
732
                lines_a.append(line)
 
733
            elif state == 'new-b':
 
734
                ch_b = True
 
735
                lines_b.append(line)
 
736
            else:
 
737
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 'killed-base',
 
738
                                 'killed-both'), \
 
739
                       state
 
740
 
 
741
                
 
742
 
 
743
 
 
744
 
 
745
 
 
746
 
 
747
def weave_toc(w):
 
748
    """Show the weave's table-of-contents"""
 
749
    print '%6s %50s %10s %10s' % ('ver', 'name', 'sha1', 'parents')
 
750
    for i in (6, 50, 10, 10):
 
751
        print '-' * i,
 
752
    print
 
753
    for i in range(w.numversions()):
 
754
        sha1 = w._sha1s[i]
 
755
        name = w._names[i]
 
756
        parent_str = ' '.join(map(str, w._parents[i]))
 
757
        print '%6d %-50.50s %10.10s %s' % (i, name, sha1, parent_str)
 
758
 
 
759
 
 
760
 
 
761
def weave_stats(weave_file, pb):
 
762
    from bzrlib.weavefile import read_weave
 
763
 
 
764
    wf = file(weave_file, 'rb')
 
765
    w = read_weave(wf)
 
766
    # FIXME: doesn't work on pipes
 
767
    weave_size = wf.tell()
 
768
 
 
769
    total = 0
 
770
    vers = len(w)
 
771
    for i in range(vers):
 
772
        pb.update('checking sizes', i, vers)
 
773
        for origin, lineno, line in w._extract([i]):
 
774
            total += len(line)
 
775
 
 
776
    pb.clear()
 
777
 
 
778
    print 'versions          %9d' % vers
 
779
    print 'weave file        %9d bytes' % weave_size
 
780
    print 'total contents    %9d bytes' % total
 
781
    print 'compression ratio %9.2fx' % (float(total) / float(weave_size))
 
782
    if vers:
 
783
        avg = total/vers
 
784
        print 'average size      %9d bytes' % avg
 
785
        print 'relative size     %9.2fx' % (float(weave_size) / float(avg))
 
786
 
 
787
 
 
788
def usage():
 
789
    print """bzr weave tool
 
790
 
 
791
Experimental tool for weave algorithm.
 
792
 
 
793
usage:
 
794
    weave init WEAVEFILE
 
795
        Create an empty weave file
 
796
    weave get WEAVEFILE VERSION
 
797
        Write out specified version.
 
798
    weave check WEAVEFILE
 
799
        Check consistency of all versions.
 
800
    weave toc WEAVEFILE
 
801
        Display table of contents.
 
802
    weave add WEAVEFILE NAME [BASE...] < NEWTEXT
 
803
        Add NEWTEXT, with specified parent versions.
 
804
    weave annotate WEAVEFILE VERSION
 
805
        Display origin of each line.
 
806
    weave mash WEAVEFILE VERSION...
 
807
        Display composite of all selected versions.
 
808
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
 
809
        Auto-merge two versions and display conflicts.
 
810
    weave diff WEAVEFILE VERSION1 VERSION2 
 
811
        Show differences between two versions.
 
812
 
 
813
example:
 
814
 
 
815
    % weave init foo.weave
 
816
    % vi foo.txt
 
817
    % weave add foo.weave ver0 < foo.txt
 
818
    added version 0
 
819
 
 
820
    (create updated version)
 
821
    % vi foo.txt
 
822
    % weave get foo.weave 0 | diff -u - foo.txt
 
823
    % weave add foo.weave ver1 0 < foo.txt
 
824
    added version 1
 
825
 
 
826
    % weave get foo.weave 0 > foo.txt       (create forked version)
 
827
    % vi foo.txt
 
828
    % weave add foo.weave ver2 0 < foo.txt
 
829
    added version 2
 
830
 
 
831
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
 
832
    % vi foo.txt                            (resolve conflicts)
 
833
    % weave add foo.weave merged 1 2 < foo.txt     (commit merged version)     
 
834
    
 
835
"""
 
836
    
 
837
 
 
838
 
 
839
def main(argv):
 
840
    import sys
 
841
    import os
 
842
    try:
 
843
        import bzrlib
 
844
    except ImportError:
 
845
        # in case we're run directly from the subdirectory
 
846
        sys.path.append('..')
 
847
        import bzrlib
 
848
    from bzrlib.weavefile import write_weave, read_weave
 
849
    from bzrlib.progress import ProgressBar
 
850
 
 
851
    try:
 
852
        import psyco
 
853
        psyco.full()
 
854
    except ImportError:
 
855
        pass
 
856
 
 
857
    if len(argv) < 2:
 
858
        usage()
 
859
        return 0
 
860
 
 
861
    cmd = argv[1]
 
862
 
 
863
    def readit():
 
864
        return read_weave(file(argv[2], 'rb'))
 
865
    
 
866
    if cmd == 'help':
 
867
        usage()
 
868
    elif cmd == 'add':
 
869
        w = readit()
 
870
        # at the moment, based on everything in the file
 
871
        name = argv[3]
 
872
        parents = map(int, argv[4:])
 
873
        lines = sys.stdin.readlines()
 
874
        ver = w.add(name, parents, lines)
 
875
        write_weave(w, file(argv[2], 'wb'))
 
876
        print 'added version %r %d' % (name, ver)
 
877
    elif cmd == 'init':
 
878
        fn = argv[2]
 
879
        if os.path.exists(fn):
 
880
            raise IOError("file exists")
 
881
        w = Weave()
 
882
        write_weave(w, file(fn, 'wb'))
 
883
    elif cmd == 'get': # get one version
 
884
        w = readit()
 
885
        sys.stdout.writelines(w.get_iter(int(argv[3])))
 
886
        
 
887
    elif cmd == 'mash': # get composite
 
888
        w = readit()
 
889
        sys.stdout.writelines(w.mash_iter(map(int, argv[3:])))
 
890
 
 
891
    elif cmd == 'diff':
 
892
        from difflib import unified_diff
 
893
        w = readit()
 
894
        fn = argv[2]
 
895
        v1, v2 = map(int, argv[3:5])
 
896
        lines1 = w.get(v1)
 
897
        lines2 = w.get(v2)
 
898
        diff_gen = unified_diff(lines1, lines2,
 
899
                                '%s version %d' % (fn, v1),
 
900
                                '%s version %d' % (fn, v2))
 
901
        sys.stdout.writelines(diff_gen)
 
902
            
 
903
    elif cmd == 'annotate':
 
904
        w = readit()
 
905
        # newline is added to all lines regardless; too hard to get
 
906
        # reasonable formatting otherwise
 
907
        lasto = None
 
908
        for origin, text in w.annotate(int(argv[3])):
 
909
            text = text.rstrip('\r\n')
 
910
            if origin == lasto:
 
911
                print '      | %s' % (text)
 
912
            else:
 
913
                print '%5d | %s' % (origin, text)
 
914
                lasto = origin
 
915
                
 
916
    elif cmd == 'toc':
 
917
        weave_toc(readit())
 
918
 
 
919
    elif cmd == 'stats':
 
920
        weave_stats(argv[2], ProgressBar())
 
921
        
 
922
    elif cmd == 'check':
 
923
        w = readit()
 
924
        pb = ProgressBar()
 
925
        w.check(pb)
 
926
        pb.clear()
 
927
        print '%d versions ok' % w.numversions()
 
928
 
 
929
    elif cmd == 'inclusions':
 
930
        w = readit()
 
931
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
 
932
 
 
933
    elif cmd == 'parents':
 
934
        w = readit()
 
935
        print ' '.join(map(str, w._parents[int(argv[3])]))
 
936
 
 
937
    elif cmd == 'plan-merge':
 
938
        w = readit()
 
939
        for state, line in w.plan_merge(int(argv[3]), int(argv[4])):
 
940
            if line:
 
941
                print '%14s | %s' % (state, line),
 
942
 
 
943
    elif cmd == 'merge':
 
944
        w = readit()
 
945
        p = w.plan_merge(int(argv[3]), int(argv[4]))
 
946
        sys.stdout.writelines(w.weave_merge(p))
 
947
            
 
948
    elif cmd == 'mash-merge':
 
949
        if len(argv) != 5:
 
950
            usage()
 
951
            return 1
 
952
 
 
953
        w = readit()
 
954
        v1, v2 = map(int, argv[3:5])
 
955
 
 
956
        basis = w.inclusions([v1]).intersection(w.inclusions([v2]))
 
957
 
 
958
        base_lines = list(w.mash_iter(basis))
 
959
        a_lines = list(w.get(v1))
 
960
        b_lines = list(w.get(v2))
 
961
 
 
962
        from bzrlib.merge3 import Merge3
 
963
        m3 = Merge3(base_lines, a_lines, b_lines)
 
964
 
 
965
        name_a = 'version %d' % v1
 
966
        name_b = 'version %d' % v2
 
967
        sys.stdout.writelines(m3.merge_lines(name_a=name_a, name_b=name_b))
 
968
    else:
 
969
        raise ValueError('unknown command %r' % cmd)
 
970
    
 
971
 
 
972
 
 
973
def profile_main(argv): 
 
974
    import tempfile, hotshot, hotshot.stats
 
975
 
 
976
    prof_f = tempfile.NamedTemporaryFile()
 
977
 
 
978
    prof = hotshot.Profile(prof_f.name)
 
979
 
 
980
    ret = prof.runcall(main, argv)
 
981
    prof.close()
 
982
 
 
983
    stats = hotshot.stats.load(prof_f.name)
 
984
    #stats.strip_dirs()
 
985
    stats.sort_stats('cumulative')
 
986
    ## XXX: Might like to write to stderr or the trace file instead but
 
987
    ## print_stats seems hardcoded to stdout
 
988
    stats.print_stats(20)
 
989
            
 
990
    return ret
 
991
 
 
992
 
 
993
if __name__ == '__main__':
 
994
    import sys
 
995
    if '--profile' in sys.argv:
 
996
        args = sys.argv[:]
 
997
        args.remove('--profile')
 
998
        sys.exit(profile_main(args))
 
999
    else:
 
1000
        sys.exit(main(sys.argv))
 
1001