/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
938 by Martin Pool
- various optimizations to weave add code
24
0.1.61 by Martin Pool
doc
25
# TODO: Perhaps have copy method for Weave instances?
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
26
0.1.58 by Martin Pool
doc
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
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
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
0.1.85 by Martin Pool
doc
36
# 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
37
38
# TODO: Check that no insertion occurs inside a deletion that was
39
# active in the version of the insertion.
40
912 by Martin Pool
- update todos for weave
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.
0.1.85 by Martin Pool
doc
45
918 by Martin Pool
- start doing new weave-merge algorithm
46
# TODO: Parallel-extract that passes back each line along with a
47
# description of which revisions include it.  Nice for checking all
1393.1.25 by Martin Pool
- comments in weave code
48
# shas or calculating stats in parallel.
918 by Martin Pool
- start doing new weave-merge algorithm
49
1082 by Martin Pool
- lift imports
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
1083 by Martin Pool
- add space to store revision-id in weave files
55
# TODO: Perhaps the API should work only in names to hide the integer
56
# indexes from the user?
57
1328 by Martin Pool
- slight refactoring of weave code
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
1083 by Martin Pool
- add space to store revision-id in weave files
62
1082 by Martin Pool
- lift imports
63
64
import sha
1322 by Martin Pool
- tidy up import
65
from difflib import SequenceMatcher
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
66
0.1.85 by Martin Pool
doc
67
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
68
924 by Martin Pool
- Add IntSet class
69
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
70
class WeaveError(Exception):
71
    """Exception in processing weave"""
72
73
74
class WeaveFormatError(WeaveError):
75
    """Weave invariant violated"""
76
    
77
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
78
class Weave(object):
79
    """weave - versioned text file storage.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
80
    
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
81
    A Weave manages versions of line-based text files, keeping track
82
    of the originating version for each line.
83
84
    To clients the "lines" of the file are represented as a list of strings.
85
    These strings  will typically have terminal newline characters, but
86
    this is not required.  In particular files commonly do not have a newline
87
    at the end of the file.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
88
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
89
    Texts can be identified in either of two ways:
90
91
    * a nonnegative index number.
92
1324 by Martin Pool
- doc
93
    * a version-id string.
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
94
0.1.38 by Martin Pool
Rename knit to weave. (I don't think there's an existing module called weave.)
95
    Typically the index number will be valid only inside this weave and
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
96
    the version-id is used to reference it in the larger world.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
97
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
98
    The weave is represented as a list mixing edit instructions and
944 by Martin Pool
- refactor member names in Weave code
99
    literal text.  Each entry in _weave can be either a string (or
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
100
    unicode), or a tuple.  If a string, it means that the given line
101
    should be output in the currently active revisions.
102
103
    If a tuple, it gives a processing instruction saying in which
104
    revisions the enclosed lines are active.  The tuple has the form
105
    (instruction, version).
106
107
    The instruction can be '{' or '}' for an insertion block, and '['
108
    and ']' for a deletion block respectively.  The version is the
0.1.45 by Martin Pool
doc
109
    integer version index.  There is no replace operator, only deletes
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
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.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
113
0.1.41 by Martin Pool
Doc
114
    Constraints/notes:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
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
0.1.41 by Martin Pool
Doc
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
0.1.45 by Martin Pool
doc
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
0.1.41 by Martin Pool
Doc
130
    * It doesn't seem very useful to have an active insertion
131
      inside an inactive insertion, but it might happen.
0.1.45 by Martin Pool
doc
132
      
0.1.41 by Martin Pool
Doc
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.
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
139
0.1.49 by Martin Pool
Add another constraint: revisions should not delete text that they
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
944 by Martin Pool
- refactor member names in Weave code
145
    _weave
146
        Text of the weave; list of control instruction tuples and strings.
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
147
944 by Martin Pool
- refactor member names in Weave code
148
    _parents
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
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.
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
152
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
153
    _sha1s
1083 by Martin Pool
- add space to store revision-id in weave files
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.
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
161
162
    _weave_name
163
        Descriptive name of this weave; typically the filename if known.
164
        Set by read_weave.
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
165
    """
938 by Martin Pool
- various optimizations to weave add code
166
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
167
    __slots__ = ['_weave', '_parents', '_sha1s', '_names', '_name_map',
168
                 '_weave_name']
938 by Martin Pool
- various optimizations to weave add code
169
    
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
170
    def __init__(self, weave_name=None):
944 by Martin Pool
- refactor member names in Weave code
171
        self._weave = []
172
        self._parents = []
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
173
        self._sha1s = []
1083 by Martin Pool
- add space to store revision-id in weave files
174
        self._names = []
175
        self._name_map = {}
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
176
        self._weave_name = weave_name
0.1.60 by Martin Pool
Weave eq and ne methods
177
178
179
    def __eq__(self, other):
180
        if not isinstance(other, Weave):
181
            return False
944 by Martin Pool
- refactor member names in Weave code
182
        return self._parents == other._parents \
1083 by Martin Pool
- add space to store revision-id in weave files
183
               and self._weave == other._weave \
184
               and self._sha1s == other._sha1s 
185
0.1.60 by Martin Pool
Weave eq and ne methods
186
    
187
    def __ne__(self, other):
188
        return not self.__eq__(other)
189
1083 by Martin Pool
- add space to store revision-id in weave files
190
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
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
        
1083 by Martin Pool
- add space to store revision-id in weave files
199
    def lookup(self, name):
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
200
        """Convert symbolic version name to index."""
1083 by Martin Pool
- add space to store revision-id in weave files
201
        try:
202
            return self._name_map[name]
203
        except KeyError:
1260 by Martin Pool
- some updates for fetch/update function
204
            raise WeaveError("name %r not present in weave %r" %
1209 by Martin Pool
- Add Weave._weave_name for debugging purposes
205
                             (name, self._weave_name))
1083 by Martin Pool
- add space to store revision-id in weave files
206
1229 by Martin Pool
- new Weave.idx_to_name and .parents methods
207
208
    def idx_to_name(self, version):
209
        return self._names[version]
210
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
211
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
212
    def _check_repeated_add(self, name, parents, text, sha1):
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
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)
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
221
        if sha1 != self._sha1s[idx]:
1237 by Martin Pool
- allow the same version to be repeatedly added to a weave
222
            raise WeaveError("name \"%s\" already present in weave "
223
                             "with different text" % name)            
224
        return idx
225
        
226
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
227
        
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
228
    def add(self, name, parents, text, sha1=None):
0.1.4 by Martin Pool
Start indexing knits by both integer and version string.
229
        """Add a single text on top of the weave.
0.1.36 by Martin Pool
doc
230
  
0.1.26 by Martin Pool
Refactor parameters to add command
231
        Returns the index number of the newly added version.
232
1083 by Martin Pool
- add space to store revision-id in weave files
233
        name
234
            Symbolic name for this version.
235
            (Typically the revision-id of the revision that added it.)
236
0.1.26 by Martin Pool
Refactor parameters to add command
237
        parents
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
238
            List or set of direct parent version numbers.
239
            
0.1.26 by Martin Pool
Refactor parameters to add command
240
        text
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
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
        """
1393.1.4 by Martin Pool
- weave tool now finds bzrlib if it's not on the default path
246
        from bzrlib.osutils import sha_strings
938 by Martin Pool
- various optimizations to weave add code
247
1083 by Martin Pool
- add space to store revision-id in weave files
248
        assert isinstance(name, basestring)
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
249
        if sha1 is None:
250
            sha1 = sha_strings(text)
1083 by Martin Pool
- add space to store revision-id in weave files
251
        if name in self._name_map:
1323 by Martin Pool
- caller can pass SHA-1 to Weave.add for efficiency
252
            return self._check_repeated_add(name, parents, text, sha1)
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
253
254
        parents = map(self.maybe_lookup, parents)
938 by Martin Pool
- various optimizations to weave add code
255
        self._check_versions(parents)
0.1.82 by Martin Pool
Small weave optimizations
256
        ## self._check_lines(text)
944 by Martin Pool
- refactor member names in Weave code
257
        new_version = len(self._parents)
0.1.5 by Martin Pool
Add test for storing two text versions.
258
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
259
1083 by Martin Pool
- add space to store revision-id in weave files
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[:])
0.1.89 by Martin Pool
Store SHA1 in weave file for later verification
263
        self._sha1s.append(sha1)
1083 by Martin Pool
- add space to store revision-id in weave files
264
        self._names.append(name)
265
        self._name_map[name] = new_version
938 by Martin Pool
- various optimizations to weave add code
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:
944 by Martin Pool
- refactor member names in Weave code
274
                self._weave.append(('{', new_version))
275
                self._weave.extend(text)
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
276
                self._weave.append(('}', None))
938 by Martin Pool
- various optimizations to weave add code
277
        
278
            return new_version
279
941 by Martin Pool
- allow for parents specified to Weave.add to be a set
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
938 by Martin Pool
- various optimizations to weave add code
285
            
286
287
        ancestors = self.inclusions(parents)
288
944 by Martin Pool
- refactor member names in Weave code
289
        l = self._weave
938 by Martin Pool
- various optimizations to weave add code
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
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
298
        # another small special case: a merge, producing the same text
299
        # as auto-merge
938 by Martin Pool
- various optimizations to weave add code
300
        if text == basis_lines:
301
            return new_version            
302
303
        # add a sentinal, because we can also match against the final line
944 by Martin Pool
- refactor member names in Weave code
304
        basis_lineno.append(len(self._weave))
938 by Martin Pool
- various optimizations to weave add code
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:
944 by Martin Pool
- refactor member names in Weave code
337
                self._weave.insert(i1+offset, ('[', new_version))
338
                self._weave.insert(i2+offset+1, (']', new_version))
938 by Martin Pool
- various optimizations to weave add code
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
944 by Martin Pool
- refactor member names in Weave code
346
                self._weave[i:i] = ([('{', new_version)] 
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
347
                                    + text[j1:j2] 
348
                                    + [('}', None)])
938 by Martin Pool
- various optimizations to weave add code
349
                offset += 2 + (j2 - j1)
350
351
        return new_version
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
352
1092.2.22 by Robert Collins
text_version and name_version unification looking reasonable
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)
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
357
0.1.78 by Martin Pool
Rename Weave.get_included to inclusions and getiter to get_iter
358
    def inclusions(self, versions):
893 by Martin Pool
- Refactor weave calculation of inclusions
359
        """Return set of all ancestors of given version(s)."""
928 by Martin Pool
- go back to using plain builtin set()
360
        i = set(versions)
1328 by Martin Pool
- slight refactoring of weave code
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)
0.1.77 by Martin Pool
New Weave.get_included() does transitive expansion
368
369
1229 by Martin Pool
- new Weave.idx_to_name and .parents methods
370
    def parents(self, version):
371
        return self._parents[version]
372
373
890 by Martin Pool
- weave info should show minimal expression of parents
374
    def minimal_parents(self, version):
375
        """Find the minimal set of parents for the version."""
944 by Martin Pool
- refactor member names in Weave code
376
        included = self._parents[version]
890 by Martin Pool
- weave info should show minimal expression of parents
377
        if not included:
378
            return []
379
        
380
        li = list(included)
893 by Martin Pool
- Refactor weave calculation of inclusions
381
        li.sort(reverse=True)
890 by Martin Pool
- weave info should show minimal expression of parents
382
383
        mininc = []
928 by Martin Pool
- go back to using plain builtin set()
384
        gotit = set()
890 by Martin Pool
- weave info should show minimal expression of parents
385
386
        for pv in li:
387
            if pv not in gotit:
388
                mininc.append(pv)
893 by Martin Pool
- Refactor weave calculation of inclusions
389
                gotit.update(self.inclusions(pv))
890 by Martin Pool
- weave info should show minimal expression of parents
390
391
        assert mininc[0] >= 0
392
        assert mininc[-1] < version
393
        return mininc
394
395
0.1.75 by Martin Pool
Remove VerInfo class; just store sets directly in the list of
396
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
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):
869 by Martin Pool
- more weave.py command line options
403
                raise ValueError("text line should be a string or unicode, not %s"
404
                                 % type(l))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
405
        
406
407
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
408
    def _check_versions(self, indexes):
409
        """Check everything in the sequence of indexes is valid"""
410
        for i in indexes:
411
            try:
944 by Martin Pool
- refactor member names in Weave code
412
                self._parents[i]
0.1.27 by Martin Pool
Check that version numbers passed in are reasonable
413
            except IndexError:
414
                raise IndexError("invalid version number %r" % i)
415
0.1.2 by Martin Pool
Import testsweet module adapted from bzr.
416
    
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
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):
0.1.7 by Martin Pool
Add trivial annotate text
422
        """Yield list of (index-id, line) pairs for the specified version.
423
424
        The index indicates when the line originated in the weave."""
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
425
        incls = [self.maybe_lookup(name_or_index)]
426
        for origin, lineno, text in self._extract(incls):
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
427
            yield origin, text
0.1.22 by Martin Pool
Calculate delta for new versions relative to a set of parent versions.
428
429
918 by Martin Pool
- start doing new weave-merge algorithm
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 = []
928 by Martin Pool
- go back to using plain builtin set()
439
        dset = set()
918 by Martin Pool
- start doing new weave-merge algorithm
440
441
        lineno = 0         # line of weave, 0-based
442
944 by Martin Pool
- refactor member names in Weave code
443
        for l in self._weave:
918 by Martin Pool
- start doing new weave-merge algorithm
444
            if isinstance(l, tuple):
445
                c, v = l
446
                isactive = None
447
                if c == '{':
448
                    istack.append(v)
449
                elif c == '}':
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
450
                    istack.pop()
918 by Martin Pool
- start doing new weave-merge algorithm
451
                elif c == '[':
926 by Martin Pool
- update more weave code to use intsets
452
                    assert v not in dset
453
                    dset.add(v)
918 by Martin Pool
- start doing new weave-merge algorithm
454
                elif c == ']':
926 by Martin Pool
- update more weave code to use intsets
455
                    dset.remove(v)
918 by Martin Pool
- start doing new weave-merge algorithm
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
893 by Martin Pool
- Refactor weave calculation of inclusions
467
    def _extract(self, versions):
0.1.20 by Martin Pool
Factor out Knit.extract() method
468
        """Yield annotation of lines in included set.
469
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
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
0.1.20 by Martin Pool
Factor out Knit.extract() method
474
        The set typically but not necessarily corresponds to a version.
475
        """
1196 by Martin Pool
- [WIP] retrieve historical texts from weaves
476
        for i in versions:
477
            if not isinstance(i, int):
478
                raise ValueError(i)
479
            
893 by Martin Pool
- Refactor weave calculation of inclusions
480
        included = self.inclusions(versions)
881 by Martin Pool
- faster weave extraction
481
482
        istack = []
928 by Martin Pool
- go back to using plain builtin set()
483
        dset = set()
0.1.48 by Martin Pool
Basic parsing of delete instructions.
484
485
        lineno = 0         # line of weave, 0-based
891 by Martin Pool
- fix up refactoring of weave
486
894 by Martin Pool
- small optimization for weave extract
487
        isactive = None
0.1.85 by Martin Pool
doc
488
931 by Martin Pool
- experiment with making Weave._extract() return a list, not a generator - slightly faster
489
        result = []
490
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
491
        WFE = WeaveFormatError
0.1.95 by Martin Pool
- preliminary merge conflict detection
492
944 by Martin Pool
- refactor member names in Weave code
493
        for l in self._weave:
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
494
            if isinstance(l, tuple):
495
                c, v = l
894 by Martin Pool
- small optimization for weave extract
496
                isactive = None
891 by Martin Pool
- fix up refactoring of weave
497
                if c == '{':
498
                    assert v not in istack
499
                    istack.append(v)
500
                elif c == '}':
1075 by Martin Pool
- don't store redundant version number at end of insert blocks
501
                    istack.pop()
891 by Martin Pool
- fix up refactoring of weave
502
                elif c == '[':
503
                    if v in included:
881 by Martin Pool
- faster weave extraction
504
                        assert v not in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
505
                        dset.add(v)
891 by Martin Pool
- fix up refactoring of weave
506
                else:
507
                    assert c == ']'
508
                    if v in included:
881 by Martin Pool
- faster weave extraction
509
                        assert v in dset
0.1.48 by Martin Pool
Basic parsing of delete instructions.
510
                        dset.remove(v)
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
511
            else:
512
                assert isinstance(l, basestring)
894 by Martin Pool
- small optimization for weave extract
513
                if isactive is None:
514
                    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
515
                if isactive:
931 by Martin Pool
- experiment with making Weave._extract() return a list, not a generator - slightly faster
516
                    result.append((istack[-1], lineno, l))
0.1.39 by Martin Pool
Change to a more realistic weave structure which can represent insertions and
517
            lineno += 1
0.1.7 by Martin Pool
Add trivial annotate text
518
0.1.46 by Martin Pool
More constraints on structure of weave, and checks that they work
519
        if istack:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
520
            raise WFE("unclosed insertion blocks at end of weave",
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
521
                                   istack)
0.1.48 by Martin Pool
Basic parsing of delete instructions.
522
        if dset:
0.1.63 by Martin Pool
Abbreviate WeaveFormatError in some code
523
            raise WFE("unclosed deletion blocks at end of weave",
0.1.48 by Martin Pool
Basic parsing of delete instructions.
524
                                   dset)
0.1.40 by Martin Pool
Add test for extracting from weave with nested insertions
525
931 by Martin Pool
- experiment with making Weave._extract() return a list, not a generator - slightly faster
526
        return result
527
    
528
0.1.7 by Martin Pool
Add trivial annotate text
529
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
530
    def get_iter(self, name_or_index):
0.1.5 by Martin Pool
Add test for storing two text versions.
531
        """Yield lines for the specified version."""
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
532
        incls = [self.maybe_lookup(name_or_index)]
533
        for origin, lineno, line in self._extract(incls):
0.1.8 by Martin Pool
Unify get/annotate code
534
            yield line
0.1.5 by Martin Pool
Add test for storing two text versions.
535
536
1369 by Martin Pool
- try to avoid redundant conversion of strings when retrieving from weaves
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):
1269 by Martin Pool
- some weave operations automatically look up symbolic names if supplied
543
        return list(self.get_iter(name_or_index))
0.1.1 by Martin Pool
Check in old existing knit code.
544
545
1369 by Martin Pool
- try to avoid redundant conversion of strings when retrieving from weaves
546
    get = get_lines
547
548
0.1.95 by Martin Pool
- preliminary merge conflict detection
549
    def mash_iter(self, included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
550
        """Return composed version of multiple included versions."""
1338 by Martin Pool
- Weave.mash_iter optionally takes names rather than indexes
551
        included = map(self.maybe_lookup, included)
893 by Martin Pool
- Refactor weave calculation of inclusions
552
        for origin, lineno, text in self._extract(included):
0.1.65 by Martin Pool
Add Weave.merge_iter to get automerged lines
553
            yield text
554
555
0.1.11 by Martin Pool
Add Knit.dump method
556
    def dump(self, to_file):
557
        from pprint import pprint
944 by Martin Pool
- refactor member names in Weave code
558
        print >>to_file, "Weave._weave = ",
559
        pprint(self._weave, to_file)
560
        print >>to_file, "Weave._parents = ",
561
        pprint(self._parents, to_file)
0.1.11 by Martin Pool
Add Knit.dump method
562
563
0.1.91 by Martin Pool
Update Weave.check
564
565
    def numversions(self):
944 by Martin Pool
- refactor member names in Weave code
566
        l = len(self._parents)
0.1.91 by Martin Pool
Update Weave.check
567
        assert l == len(self._sha1s)
568
        return l
569
570
946 by Martin Pool
- weave info only shows the weave headers, doesn't extract every version:
571
    def __len__(self):
572
        return self.numversions()
573
574
894 by Martin Pool
- small optimization for weave extract
575
    def check(self, progress_bar=None):
0.1.91 by Martin Pool
Update Weave.check
576
        # check no circular inclusions
577
        for version in range(self.numversions()):
944 by Martin Pool
- refactor member names in Weave code
578
            inclusions = list(self._parents[version])
0.1.91 by Martin Pool
Update Weave.check
579
            if inclusions:
580
                inclusions.sort()
581
                if inclusions[-1] >= version:
0.1.47 by Martin Pool
New WeaveError and WeaveFormatError rather than assertions.
582
                    raise WeaveFormatError("invalid included version %d for index %d"
0.1.91 by Martin Pool
Update Weave.check
583
                                           % (inclusions[-1], version))
584
585
        # try extracting all versions; this is a bit slow and parallel
586
        # extraction could be used
894 by Martin Pool
- small optimization for weave extract
587
        nv = self.numversions()
588
        for version in range(nv):
589
            if progress_bar:
590
                progress_bar.update('checking text', version, nv)
0.1.91 by Martin Pool
Update Weave.check
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))
0.1.18 by Martin Pool
Better Knit.dump method
600
881 by Martin Pool
- faster weave extraction
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
0.1.13 by Martin Pool
Knit structure now allows for versions to include the lines present in other
605
606
0.1.95 by Martin Pool
- preliminary merge conflict detection
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
618
        # approach: find the included versions common to all the
619
        # merged versions
620
        raise NotImplementedError()
621
622
623
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
624
    def _delta(self, included, lines):
625
        """Return changes from basis to new revision.
626
627
        The old text for comparison is the union of included revisions.
628
629
        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.
630
0.1.55 by Martin Pool
doc
631
        Delta is returned as a sequence of
632
        (weave1, weave2, newlines).
633
634
        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.
635
        replaced by the sequence of lines in newlines.  Note that
636
        these line numbers are positions in the total weave and don't
637
        correspond to the lines in any extracted version, or even the
638
        extracted union of included versions.
639
640
        If line1=line2, this is a pure insert; if newlines=[] this is a
641
        pure delete.  (Similar to difflib.)
0.1.21 by Martin Pool
Start computing a delta to insert a new revision
642
        """
643
0.1.1 by Martin Pool
Check in old existing knit code.
644
918 by Martin Pool
- start doing new weave-merge algorithm
645
            
646
    def plan_merge(self, ver_a, ver_b):
647
        """Return pseudo-annotation indicating how the two versions merge.
648
649
        This is computed between versions a and b and their common
650
        base.
651
652
        Weave lines present in none of them are skipped entirely.
653
        """
926 by Martin Pool
- update more weave code to use intsets
654
        inc_a = self.inclusions([ver_a])
655
        inc_b = self.inclusions([ver_b])
918 by Martin Pool
- start doing new weave-merge algorithm
656
        inc_c = inc_a & inc_b
657
658
        for lineno, insert, deleteset, line in self._walk():
659
            if deleteset & inc_c:
660
                # killed in parent; can't be in either a or b
661
                # not relevant to our work
662
                yield 'killed-base', line
926 by Martin Pool
- update more weave code to use intsets
663
            elif insert in inc_c:
918 by Martin Pool
- start doing new weave-merge algorithm
664
                # was inserted in base
665
                killed_a = bool(deleteset & inc_a)
666
                killed_b = bool(deleteset & inc_b)
667
                if killed_a and killed_b:
668
                    yield 'killed-both', line
669
                elif killed_a:
670
                    yield 'killed-a', line
671
                elif killed_b:
672
                    yield 'killed-b', line
673
                else:
674
                    yield 'unchanged', line
926 by Martin Pool
- update more weave code to use intsets
675
            elif insert in inc_a:
918 by Martin Pool
- start doing new weave-merge algorithm
676
                if deleteset & inc_a:
677
                    yield 'ghost-a', line
678
                else:
679
                    # new in A; not in B
680
                    yield 'new-a', line
926 by Martin Pool
- update more weave code to use intsets
681
            elif insert in inc_b:
918 by Martin Pool
- start doing new weave-merge algorithm
682
                if deleteset & inc_b:
683
                    yield 'ghost-b', line
684
                else:
685
                    yield 'new-b', line
686
            else:
687
                # not in either revision
688
                yield 'irrelevant', line
689
919 by Martin Pool
- more development of weave-merge
690
        yield 'unchanged', ''           # terminator
691
692
693
694
    def weave_merge(self, plan):
695
        lines_a = []
696
        lines_b = []
697
        ch_a = ch_b = False
698
699
        for state, line in plan:
700
            if state == 'unchanged' or state == 'killed-both':
701
                # resync and flush queued conflicts changes if any
702
                if not lines_a and not lines_b:
703
                    pass
704
                elif ch_a and not ch_b:
705
                    # one-sided change:                    
706
                    for l in lines_a: yield l
707
                elif ch_b and not ch_a:
708
                    for l in lines_b: yield l
709
                elif lines_a == lines_b:
710
                    for l in lines_a: yield l
711
                else:
712
                    yield '<<<<\n'
713
                    for l in lines_a: yield l
714
                    yield '====\n'
715
                    for l in lines_b: yield l
716
                    yield '>>>>\n'
717
718
                del lines_a[:]
719
                del lines_b[:]
720
                ch_a = ch_b = False
721
                
722
            if state == 'unchanged':
723
                if line:
724
                    yield line
725
            elif state == 'killed-a':
726
                ch_a = True
727
                lines_b.append(line)
728
            elif state == 'killed-b':
729
                ch_b = True
730
                lines_a.append(line)
731
            elif state == 'new-a':
732
                ch_a = True
733
                lines_a.append(line)
734
            elif state == 'new-b':
735
                ch_b = True
736
                lines_b.append(line)
737
            else:
920 by Martin Pool
- add more test cases for weave_merge
738
                assert state in ('irrelevant', 'ghost-a', 'ghost-b', 'killed-base',
739
                                 'killed-both'), \
919 by Martin Pool
- more development of weave-merge
740
                       state
741
742
                
743
744
918 by Martin Pool
- start doing new weave-merge algorithm
745
746
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
747
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
748
def weave_toc(w):
749
    """Show the weave's table-of-contents"""
1083 by Martin Pool
- add space to store revision-id in weave files
750
    print '%6s %50s %10s %10s' % ('ver', 'name', 'sha1', 'parents')
751
    for i in (6, 50, 10, 10):
870 by Martin Pool
- better weave info display
752
        print '-' * i,
753
    print
946 by Martin Pool
- weave info only shows the weave headers, doesn't extract every version:
754
    for i in range(w.numversions()):
0.1.91 by Martin Pool
Update Weave.check
755
        sha1 = w._sha1s[i]
1083 by Martin Pool
- add space to store revision-id in weave files
756
        name = w._names[i]
757
        parent_str = ' '.join(map(str, w._parents[i]))
758
        print '%6d %-50.50s %10.10s %s' % (i, name, sha1, parent_str)
0.1.88 by Martin Pool
Add weave info command.
759
869 by Martin Pool
- more weave.py command line options
760
761
1393.1.4 by Martin Pool
- weave tool now finds bzrlib if it's not on the default path
762
def weave_stats(weave_file, pb):
947 by Martin Pool
- new 'weave stats' command
763
    from bzrlib.weavefile import read_weave
764
765
    wf = file(weave_file, 'rb')
766
    w = read_weave(wf)
767
    # FIXME: doesn't work on pipes
768
    weave_size = wf.tell()
769
770
    total = 0
771
    vers = len(w)
772
    for i in range(vers):
773
        pb.update('checking sizes', i, vers)
1393.1.24 by Martin Pool
- slight speedup for weave stats
774
        for origin, lineno, line in w._extract([i]):
947 by Martin Pool
- new 'weave stats' command
775
            total += len(line)
776
777
    pb.clear()
778
779
    print 'versions          %9d' % vers
780
    print 'weave file        %9d bytes' % weave_size
781
    print 'total contents    %9d bytes' % total
782
    print 'compression ratio %9.2fx' % (float(total) / float(weave_size))
974.1.26 by aaron.bentley at utoronto
merged mbp@sourcefrog.net-20050817233101-0939da1cf91f2472
783
    if vers:
784
        avg = total/vers
785
        print 'average size      %9d bytes' % avg
786
        print 'relative size     %9.2fx' % (float(weave_size) / float(avg))
947 by Martin Pool
- new 'weave stats' command
787
788
869 by Martin Pool
- more weave.py command line options
789
def usage():
871 by Martin Pool
- add command for merge-based weave
790
    print """bzr weave tool
791
792
Experimental tool for weave algorithm.
793
869 by Martin Pool
- more weave.py command line options
794
usage:
795
    weave init WEAVEFILE
796
        Create an empty weave file
797
    weave get WEAVEFILE VERSION
798
        Write out specified version.
799
    weave check WEAVEFILE
800
        Check consistency of all versions.
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
801
    weave toc WEAVEFILE
869 by Martin Pool
- more weave.py command line options
802
        Display table of contents.
1084 by Martin Pool
- weave add command needs to take a symbolic name too
803
    weave add WEAVEFILE NAME [BASE...] < NEWTEXT
869 by Martin Pool
- more weave.py command line options
804
        Add NEWTEXT, with specified parent versions.
805
    weave annotate WEAVEFILE VERSION
806
        Display origin of each line.
807
    weave mash WEAVEFILE VERSION...
808
        Display composite of all selected versions.
809
    weave merge WEAVEFILE VERSION1 VERSION2 > OUT
810
        Auto-merge two versions and display conflicts.
1329 by Martin Pool
- add diff command to weave utility
811
    weave diff WEAVEFILE VERSION1 VERSION2 
812
        Show differences between two versions.
871 by Martin Pool
- add command for merge-based weave
813
814
example:
815
816
    % weave init foo.weave
817
    % vi foo.txt
1084 by Martin Pool
- weave add command needs to take a symbolic name too
818
    % weave add foo.weave ver0 < foo.txt
871 by Martin Pool
- add command for merge-based weave
819
    added version 0
820
821
    (create updated version)
822
    % vi foo.txt
823
    % weave get foo.weave 0 | diff -u - foo.txt
1084 by Martin Pool
- weave add command needs to take a symbolic name too
824
    % weave add foo.weave ver1 0 < foo.txt
871 by Martin Pool
- add command for merge-based weave
825
    added version 1
826
827
    % weave get foo.weave 0 > foo.txt       (create forked version)
828
    % vi foo.txt
1084 by Martin Pool
- weave add command needs to take a symbolic name too
829
    % weave add foo.weave ver2 0 < foo.txt
871 by Martin Pool
- add command for merge-based weave
830
    added version 2
831
832
    % weave merge foo.weave 1 2 > foo.txt   (merge them)
833
    % vi foo.txt                            (resolve conflicts)
1084 by Martin Pool
- weave add command needs to take a symbolic name too
834
    % weave add foo.weave merged 1 2 < foo.txt     (commit merged version)     
871 by Martin Pool
- add command for merge-based weave
835
    
869 by Martin Pool
- more weave.py command line options
836
"""
0.1.88 by Martin Pool
Add weave info command.
837
    
838
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
839
840
def main(argv):
841
    import sys
842
    import os
1393.1.4 by Martin Pool
- weave tool now finds bzrlib if it's not on the default path
843
    try:
844
        import bzrlib
845
    except ImportError:
846
        # in case we're run directly from the subdirectory
847
        sys.path.append('..')
848
        import bzrlib
1336 by Martin Pool
- tidy up import
849
    from bzrlib.weavefile import write_weave, read_weave
894 by Martin Pool
- small optimization for weave extract
850
    from bzrlib.progress import ProgressBar
851
1078 by Martin Pool
- use psyco for weave if possible
852
    try:
853
        import psyco
854
        psyco.full()
855
    except ImportError:
856
        pass
894 by Martin Pool
- small optimization for weave extract
857
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
858
    if len(argv) < 2:
859
        usage()
860
        return 0
861
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
862
    cmd = argv[1]
869 by Martin Pool
- more weave.py command line options
863
864
    def readit():
865
        return read_weave(file(argv[2], 'rb'))
866
    
867
    if cmd == 'help':
868
        usage()
869
    elif cmd == 'add':
870
        w = readit()
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
871
        # at the moment, based on everything in the file
1084 by Martin Pool
- weave add command needs to take a symbolic name too
872
        name = argv[3]
873
        parents = map(int, argv[4:])
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
874
        lines = sys.stdin.readlines()
1084 by Martin Pool
- weave add command needs to take a symbolic name too
875
        ver = w.add(name, parents, lines)
869 by Martin Pool
- more weave.py command line options
876
        write_weave(w, file(argv[2], 'wb'))
1084 by Martin Pool
- weave add command needs to take a symbolic name too
877
        print 'added version %r %d' % (name, ver)
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
878
    elif cmd == 'init':
879
        fn = argv[2]
880
        if os.path.exists(fn):
881
            raise IOError("file exists")
882
        w = Weave()
869 by Martin Pool
- more weave.py command line options
883
        write_weave(w, file(fn, 'wb'))
884
    elif cmd == 'get': # get one version
885
        w = readit()
0.1.94 by Martin Pool
Fix get_iter call
886
        sys.stdout.writelines(w.get_iter(int(argv[3])))
869 by Martin Pool
- more weave.py command line options
887
        
888
    elif cmd == 'mash': # get composite
889
        w = readit()
890
        sys.stdout.writelines(w.mash_iter(map(int, argv[3:])))
891
1329 by Martin Pool
- add diff command to weave utility
892
    elif cmd == 'diff':
893
        from difflib import unified_diff
894
        w = readit()
895
        fn = argv[2]
896
        v1, v2 = map(int, argv[3:5])
897
        lines1 = w.get(v1)
898
        lines2 = w.get(v2)
899
        diff_gen = unified_diff(lines1, lines2,
900
                                '%s version %d' % (fn, v1),
901
                                '%s version %d' % (fn, v2))
902
        sys.stdout.writelines(diff_gen)
903
            
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
904
    elif cmd == 'annotate':
869 by Martin Pool
- more weave.py command line options
905
        w = readit()
0.1.72 by Martin Pool
Go back to weave lines normally having newlines at the end.
906
        # newline is added to all lines regardless; too hard to get
907
        # reasonable formatting otherwise
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
908
        lasto = None
909
        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.
910
            text = text.rstrip('\r\n')
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
911
            if origin == lasto:
912
                print '      | %s' % (text)
913
            else:
914
                print '%5d | %s' % (origin, text)
915
                lasto = origin
871 by Martin Pool
- add command for merge-based weave
916
                
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
917
    elif cmd == 'toc':
918
        weave_toc(readit())
947 by Martin Pool
- new 'weave stats' command
919
920
    elif cmd == 'stats':
1393.1.4 by Martin Pool
- weave tool now finds bzrlib if it's not on the default path
921
        weave_stats(argv[2], ProgressBar())
871 by Martin Pool
- add command for merge-based weave
922
        
0.1.91 by Martin Pool
Update Weave.check
923
    elif cmd == 'check':
869 by Martin Pool
- more weave.py command line options
924
        w = readit()
894 by Martin Pool
- small optimization for weave extract
925
        pb = ProgressBar()
926
        w.check(pb)
927
        pb.clear()
938 by Martin Pool
- various optimizations to weave add code
928
        print '%d versions ok' % w.numversions()
871 by Martin Pool
- add command for merge-based weave
929
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
930
    elif cmd == 'inclusions':
931
        w = readit()
932
        print ' '.join(map(str, w.inclusions([int(argv[3])])))
933
934
    elif cmd == 'parents':
935
        w = readit()
944 by Martin Pool
- refactor member names in Weave code
936
        print ' '.join(map(str, w._parents[int(argv[3])]))
892 by Martin Pool
- weave stores only direct parents, and calculates and memoizes expansion as needed
937
918 by Martin Pool
- start doing new weave-merge algorithm
938
    elif cmd == 'plan-merge':
939
        w = readit()
940
        for state, line in w.plan_merge(int(argv[3]), int(argv[4])):
919 by Martin Pool
- more development of weave-merge
941
            if line:
942
                print '%14s | %s' % (state, line),
918 by Martin Pool
- start doing new weave-merge algorithm
943
871 by Martin Pool
- add command for merge-based weave
944
    elif cmd == 'merge':
919 by Martin Pool
- more development of weave-merge
945
        w = readit()
946
        p = w.plan_merge(int(argv[3]), int(argv[4]))
947
        sys.stdout.writelines(w.weave_merge(p))
948
            
949
    elif cmd == 'mash-merge':
871 by Martin Pool
- add command for merge-based weave
950
        if len(argv) != 5:
951
            usage()
952
            return 1
953
954
        w = readit()
955
        v1, v2 = map(int, argv[3:5])
956
957
        basis = w.inclusions([v1]).intersection(w.inclusions([v2]))
958
959
        base_lines = list(w.mash_iter(basis))
960
        a_lines = list(w.get(v1))
961
        b_lines = list(w.get(v2))
962
963
        from bzrlib.merge3 import Merge3
964
        m3 = Merge3(base_lines, a_lines, b_lines)
965
966
        name_a = 'version %d' % v1
967
        name_b = 'version %d' % v2
968
        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.
969
    else:
970
        raise ValueError('unknown command %r' % cmd)
971
    
972
1076 by Martin Pool
- add code to run weave utility under profiler
973
974
def profile_main(argv): 
975
    import tempfile, hotshot, hotshot.stats
976
977
    prof_f = tempfile.NamedTemporaryFile()
978
979
    prof = hotshot.Profile(prof_f.name)
980
981
    ret = prof.runcall(main, argv)
982
    prof.close()
983
984
    stats = hotshot.stats.load(prof_f.name)
985
    #stats.strip_dirs()
1079 by Martin Pool
- weavefile can just use lists for read-in ancestry, not frozensets
986
    stats.sort_stats('cumulative')
1076 by Martin Pool
- add code to run weave utility under profiler
987
    ## XXX: Might like to write to stderr or the trace file instead but
988
    ## print_stats seems hardcoded to stdout
989
    stats.print_stats(20)
990
            
991
    return ret
992
993
0.1.62 by Martin Pool
Lame command-line client for reading and writing weaves.
994
if __name__ == '__main__':
995
    import sys
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
996
    if '--profile' in sys.argv:
1078 by Martin Pool
- use psyco for weave if possible
997
        args = sys.argv[:]
1081 by Martin Pool
- if weave tool is invoked with no arguments, show help
998
        args.remove('--profile')
1078 by Martin Pool
- use psyco for weave if possible
999
        sys.exit(profile_main(args))
1000
    else:
1001
        sys.exit(main(sys.argv))
1076 by Martin Pool
- add code to run weave utility under profiler
1002