/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.9.2 by Aaron Bentley
Get single-parent comparison working
1
from difflib import SequenceMatcher
0.9.25 by Aaron Bentley
More messy hacking
2
from StringIO import StringIO
0.9.19 by Aaron Bentley
More tweakage
3
import sys
4
0.9.25 by Aaron Bentley
More messy hacking
5
from bzrlib import (
6
    patiencediff,
7
    trace,
8
    ui,
9
    )
10
11
from bzrlib.tuned_gzip import GzipFile
0.9.3 by Aaron Bentley
Get three-parent comparisions under test
12
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
13
def topo_iter(vf):
14
    seen = set()
15
    descendants = {}
16
    for version_id in vf.versions():
17
        for parent_id in vf.get_parents(version_id):
18
            descendants.setdefault(parent_id, []).append(version_id)
19
    cur = [v for v in vf.versions() if len(vf.get_parents(v)) == 0]
20
    while len(cur) > 0:
21
        next = []
22
        for version_id in cur:
23
            if version_id in seen:
24
                continue
25
            parents = vf.get_parents(version_id)
26
            if not seen.issuperset(parents):
27
                continue
28
            next.extend(descendants.get(version_id, []))
29
            yield version_id
30
            seen.add(version_id)
31
        cur = next
32
33
0.9.1 by Aaron Bentley
Get trivial case passing
34
class MultiParent(object):
35
0.9.2 by Aaron Bentley
Get single-parent comparison working
36
    def __init__(self, hunks=None):
37
        if hunks is not None:
38
            self.hunks = hunks
39
        else:
40
            self.hunks = []
41
42
    def __repr__(self):
43
        return "MultiParent(%r)" % self.hunks
44
45
    def __eq__(self, other):
46
        if self.__class__ is not other.__class__:
47
            return False
48
        return (self.hunks == other.hunks)
0.9.1 by Aaron Bentley
Get trivial case passing
49
50
    @staticmethod
51
    def from_lines(text, parents=()):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
52
        """Produce a MultiParent from a list of lines and parents"""
0.9.2 by Aaron Bentley
Get single-parent comparison working
53
        def compare(parent):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
54
            matcher = patiencediff.PatienceSequenceMatcher(None, parent,
55
                                                           text)
56
            return matcher.get_matching_blocks()
0.9.2 by Aaron Bentley
Get single-parent comparison working
57
        parent_comparisons = [compare(p) for p in parents]
58
        cur_line = 0
59
        new_text = NewText([])
60
        parent_text = []
61
        block_iter = [iter(i) for i in parent_comparisons]
62
        diff = MultiParent([])
63
        def next_block(p):
64
            try:
65
                return block_iter[p].next()
66
            except StopIteration:
67
                return None
68
        cur_block = [next_block(p) for p, i in enumerate(block_iter)]
69
        while cur_line < len(text):
70
            best_match = None
71
            for p, block in enumerate(cur_block):
72
                if block is None:
73
                    continue
74
                i, j, n = block
75
                while j + n < cur_line:
76
                    block = cur_block[p] = next_block(p)
77
                    if block is None:
78
                        break
79
                    i, j, n = block
80
                if block is None:
81
                    continue
82
                if j > cur_line:
83
                    continue
84
                offset = cur_line - j
85
                i += offset
86
                j = cur_line
87
                n -= offset
88
                if n == 0:
89
                    continue
90
                if best_match is None or n > best_match.num_lines:
91
                    best_match = ParentText(p, i, j, n)
92
            if best_match is None:
93
                new_text.lines.append(text[cur_line])
94
                cur_line += 1
95
            else:
96
                if len(new_text.lines) > 0:
97
                    diff.hunks.append(new_text)
98
                    new_text = NewText([])
99
                diff.hunks.append(best_match)
100
                cur_line += best_match.num_lines
101
        if len(new_text.lines) > 0:
102
            diff.hunks.append(new_text)
0.9.1 by Aaron Bentley
Get trivial case passing
103
        return diff
104
105
    @classmethod
106
    def from_texts(cls, text, parents=()):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
107
        """Produce a MultiParent from a text and list of parent text"""
0.9.1 by Aaron Bentley
Get trivial case passing
108
        return cls.from_lines(text.splitlines(True),
109
                              [p.splitlines(True) for p in parents])
110
0.9.4 by Aaron Bentley
Start supporting serialization
111
    def to_patch(self):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
112
        """Yield text lines for a patch"""
0.9.4 by Aaron Bentley
Start supporting serialization
113
        for hunk in self.hunks:
114
            for line in hunk.to_patch():
115
                yield line
116
0.9.25 by Aaron Bentley
More messy hacking
117
    def patch_len(self):
118
        return len(''.join(self.to_patch()))
119
120
    def zipped_patch_len(self):
121
        return len(gzip_string(self.to_patch()))
122
0.9.18 by Aaron Bentley
Implement from_patch
123
    @staticmethod
124
    def from_patch(lines):
0.9.19 by Aaron Bentley
More tweakage
125
        """Produce a MultiParent from a sequence of lines"""
0.9.18 by Aaron Bentley
Implement from_patch
126
        line_iter = iter(lines)
127
        hunks = []
128
        cur_line = None
129
        while(True):
130
            try:
131
                cur_line = line_iter.next()
132
            except StopIteration:
133
                break
134
            if cur_line[0] == 'i':
135
                num_lines = int(cur_line.split(' ')[1])
136
                hunk_lines = [line_iter.next() for x in xrange(num_lines)]
137
                hunk_lines[-1] = hunk_lines[-1][:-1]
138
                hunks.append(NewText(hunk_lines))
139
            elif cur_line[0] == '\n':
140
                hunks[-1].lines[-1] += '\n'
141
            else:
142
                assert cur_line[0] == 'c', cur_line[0]
143
                parent, parent_pos, child_pos, num_lines =\
144
                    [int(v) for v in cur_line.split(' ')[1:]]
145
                hunks.append(ParentText(parent, parent_pos, child_pos,
146
                                        num_lines))
147
        return MultiParent(hunks)
148
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
149
    def range_iterator(self):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
150
        """Iterate through the hunks, with range indicated
151
152
        kind is "new" or "parent".
153
        for "new", data is a list of lines.
154
        for "parent", data is (parent, parent_start, parent_end)
155
        :return: a generator of (start, end, kind, data)
156
        """
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
157
        start = 0
158
        for hunk in self.hunks:
159
            if isinstance(hunk, NewText):
160
                kind = 'new'
161
                end = start + len(hunk.lines)
162
                data = hunk.lines
163
            else:
164
                kind = 'parent'
165
                start = hunk.child_pos
166
                end = start + hunk.num_lines
167
                data = (hunk.parent, hunk.parent_pos, hunk.parent_pos +
168
                        hunk.num_lines)
169
            yield start, end, kind, data
170
            start = end
171
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
172
    def num_lines(self):
173
        extra_n = 0
174
        for hunk in reversed(self.hunks):
175
            if isinstance(hunk, ParentText):
176
               return hunk.child_pos + hunk.num_lines + extra_n
177
            extra_n += len(hunk.lines)
178
        return extra_n
179
0.9.25 by Aaron Bentley
More messy hacking
180
    def is_snapshot(self):
181
        if len(self.hunks) != 1:
182
            return False
183
        return (isinstance(self.hunks[0], NewText))
184
0.9.1 by Aaron Bentley
Get trivial case passing
185
186
class NewText(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
187
    """The contents of text that is introduced by this text"""
0.9.1 by Aaron Bentley
Get trivial case passing
188
189
    def __init__(self, lines):
190
        self.lines = lines
191
192
    def __eq__(self, other):
193
        if self.__class__ is not other.__class__:
194
            return False
195
        return (other.lines == self.lines)
0.9.2 by Aaron Bentley
Get single-parent comparison working
196
197
    def __repr__(self):
198
        return 'NewText(%r)' % self.lines
199
0.9.4 by Aaron Bentley
Start supporting serialization
200
    def to_patch(self):
201
        yield 'i %d\n' % len(self.lines)
202
        for line in self.lines:
203
            yield line
204
        yield '\n'
205
0.9.2 by Aaron Bentley
Get single-parent comparison working
206
207
class ParentText(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
208
    """A reference to text present in a parent text"""
0.9.2 by Aaron Bentley
Get single-parent comparison working
209
210
    def __init__(self, parent, parent_pos, child_pos, num_lines):
211
        self.parent = parent
212
        self.parent_pos = parent_pos
213
        self.child_pos = child_pos
214
        self.num_lines = num_lines
215
216
    def __repr__(self):
217
        return 'ParentText(%(parent)r, %(parent_pos)r, %(child_pos)r,'\
218
            ' %(num_lines)r)' % self.__dict__
219
220
    def __eq__(self, other):
221
        if self.__class__ != other.__class__:
222
            return False
223
        return (self.__dict__ == other.__dict__)
0.9.4 by Aaron Bentley
Start supporting serialization
224
225
    def to_patch(self):
226
        yield 'c %(parent)d %(parent_pos)d %(child_pos)d %(num_lines)d\n'\
227
            % self.__dict__
0.9.8 by Aaron Bentley
get add_version working
228
229
230
class MultiVersionedFile(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
231
    """VersionedFile skeleton for MultiParent"""
0.9.8 by Aaron Bentley
get add_version working
232
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
233
    def __init__(self, snapshot_interval=25, max_snapshots=None):
0.9.8 by Aaron Bentley
get add_version working
234
        self._diffs = {}
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
235
        self._diff_offset = {}
0.9.8 by Aaron Bentley
get add_version working
236
        self._lines = {}
237
        self._parents = {}
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
238
        self._snapshots = set()
0.9.12 by Aaron Bentley
Make benchmarks for mp
239
        self.snapshot_interval = snapshot_interval
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
240
        self.max_snapshots = max_snapshots
0.9.12 by Aaron Bentley
Make benchmarks for mp
241
242
    def do_snapshot(self, version_id, parent_ids):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
243
        if self.snapshot_interval is None:
244
            return False
245
        if self.max_snapshots is not None and\
246
            len(self._snapshots) == self.max_snapshots:
0.9.14 by Aaron Bentley
Temporarily force snapshots to 44
247
            return False
0.9.12 by Aaron Bentley
Make benchmarks for mp
248
        if len(parent_ids) == 0:
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
249
            return True
250
        for ignored in xrange(self.snapshot_interval):
0.9.12 by Aaron Bentley
Make benchmarks for mp
251
            if len(parent_ids) == 0:
252
                return False
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
253
            version_ids = parent_ids
254
            parent_ids = []
255
            for version_id in version_ids:
256
                if version_id not in self._snapshots:
257
                    parent_ids.extend(self._parents[version_id])
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
258
        else:
259
            return True
0.9.8 by Aaron Bentley
get add_version working
260
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
261
    def add_version(self, lines, version_id, parent_ids,
0.9.20 by Aaron Bentley
Convert to a plugin
262
                    force_snapshot=None, single_parent=False):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
263
        if force_snapshot is None:
264
            do_snapshot = self.do_snapshot(version_id, parent_ids)
265
        else:
266
            do_snapshot = force_snapshot
267
        if do_snapshot:
268
            self._snapshots.add(version_id)
0.9.12 by Aaron Bentley
Make benchmarks for mp
269
            diff = MultiParent([NewText(lines)])
270
        else:
0.9.20 by Aaron Bentley
Convert to a plugin
271
            if single_parent:
272
                parent_lines = self.get_line_list(parent_ids[:1])
273
            else:
274
                parent_lines = self.get_line_list(parent_ids)
0.9.12 by Aaron Bentley
Make benchmarks for mp
275
            diff = MultiParent.from_lines(lines, parent_lines)
0.9.25 by Aaron Bentley
More messy hacking
276
            snapdiff = MultiParent([NewText(lines)])
277
            if diff.is_snapshot():
278
                self._snapshots.add(version_id)
279
            elif diff.patch_len() >= snapdiff.patch_len():
280
                trace.note("Forcing snapshot")
281
                self._snapshots.add(version_id)
0.9.8 by Aaron Bentley
get add_version working
282
        self.add_diff(diff, version_id, parent_ids)
283
        self._lines[version_id] = lines
284
285
    def add_diff(self, diff, version_id, parent_ids):
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
286
        self._diffs[version_id] = ''
287
        outfile = open('pknit', 'ab')
288
        start = outfile.tell()
289
        outfile.writelines(diff.to_patch())
290
        end = outfile.tell()
291
        self._diff_offset[version_id] = (start, end)
0.9.8 by Aaron Bentley
get add_version working
292
        self._parents[version_id] = parent_ids
293
0.9.20 by Aaron Bentley
Convert to a plugin
294
    def import_versionedfile(self, vf, snapshots, no_cache=True,
0.9.22 by Aaron Bentley
Fix restoration bug
295
                             single_parent=False, verify=False):
0.9.20 by Aaron Bentley
Convert to a plugin
296
        """Import all revisions of a versionedfile
297
298
        :param vf: The versionedfile to import
299
        :param snapshots: If provided, the revisions to make snapshots of.
300
            Otherwise, this will be auto-determined
301
        :param no_cache: If true, clear the cache after every add.
302
        :param single_parent: If true, omit all but one parent text, (but
303
            retain parent metadata).
304
        """
0.9.22 by Aaron Bentley
Fix restoration bug
305
        assert no_cache or not verify
0.9.19 by Aaron Bentley
More tweakage
306
        revisions = set(vf.versions())
307
        total = len(revisions)
0.9.20 by Aaron Bentley
Convert to a plugin
308
        pb = ui.ui_factory.nested_progress_bar()
309
        try:
310
            while len(revisions) > 0:
311
                added = set()
312
                for revision in revisions:
313
                    parents = vf.get_parents(revision)
314
                    if [p for p in parents if p not in self._diffs] != []:
315
                        continue
316
                    lines = [a + ' ' + l for a, l in
317
                             vf.annotate_iter(revision)]
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
318
                    if snapshots is None:
0.9.20 by Aaron Bentley
Convert to a plugin
319
                        force_snapshot = None
320
                    else:
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
321
                        force_snapshot = (revision in snapshots)
0.9.20 by Aaron Bentley
Convert to a plugin
322
                    self.add_version(lines, revision, parents, force_snapshot,
323
                                     single_parent)
324
                    added.add(revision)
325
                    if no_cache:
326
                        self.clear_cache()
0.9.25 by Aaron Bentley
More messy hacking
327
                        vf.clear_cache()
0.9.22 by Aaron Bentley
Fix restoration bug
328
                        if verify:
329
                            assert lines == self.get_line_list([revision])[0]
330
                            self.clear_cache()
0.9.20 by Aaron Bentley
Convert to a plugin
331
                    pb.update('Importing revisions',
332
                              (total - len(revisions)) + len(added), total)
333
                revisions = [r for r in revisions if r not in added]
334
        finally:
335
            pb.finished()
0.9.19 by Aaron Bentley
More tweakage
336
0.9.23 by Aaron Bentley
handle snapshots all at once
337
    def select_snapshots(self, vf):
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
338
        build_ancestors = {}
0.9.23 by Aaron Bentley
handle snapshots all at once
339
        descendants = {}
340
        snapshots = set()
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
341
        for version_id in topo_iter(vf):
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
342
            potential_build_ancestors = set(vf.get_parents(version_id))
343
            parents = vf.get_parents(version_id)
344
            if len(parents) == 0:
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
345
                snapshots.add(version_id)
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
346
                build_ancestors[version_id] = set()
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
347
            else:
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
348
                for parent in vf.get_parents(version_id):
349
                    potential_build_ancestors.update(build_ancestors[parent])
350
                if len(potential_build_ancestors) > self.snapshot_interval:
351
                    snapshots.add(version_id)
352
                    build_ancestors[version_id] = set()
0.9.23 by Aaron Bentley
handle snapshots all at once
353
                else:
0.9.28 by Aaron Bentley
Update snapshot-picking to use sets of ancestors
354
                    build_ancestors[version_id] = potential_build_ancestors
0.9.23 by Aaron Bentley
handle snapshots all at once
355
        return snapshots
356
357
0.9.8 by Aaron Bentley
get add_version working
358
    def clear_cache(self):
359
        self._lines.clear()
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
360
361
    def get_line_list(self, version_ids):
362
        return [self.cache_version(v) for v in version_ids]
363
364
    def cache_version(self, version_id):
365
        try:
366
            return self._lines[version_id]
367
        except KeyError:
368
            pass
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
369
        diff = self.get_diff(version_id)
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
370
        lines = []
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
371
        reconstructor = _Reconstructor(self, self._lines,
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
372
                                       self._parents)
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
373
        reconstructor.reconstruct_version(lines, version_id)
0.9.25 by Aaron Bentley
More messy hacking
374
        #self._lines[version_id] = lines
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
375
        return lines
376
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
377
    def get_diff(self, version_id):
378
        infile = open('pknit', 'rb')
379
        start, end = self._diff_offset[version_id]
380
        infile.seek(start)
381
        text = infile.read(end - start)
382
        return MultiParent.from_patch(text.splitlines(True))
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
383
384
class _Reconstructor(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
385
    """Build a text from the diffs, ancestry graph and cached lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
386
387
    def __init__(self, diffs, lines, parents):
388
        self.diffs = diffs
389
        self.lines = lines
390
        self.parents = parents
391
        self.cursor = {}
392
393
    def reconstruct(self, lines, parent_text, version_id):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
394
        """Append the lines referred to by a ParentText to lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
395
        parent_id = self.parents[version_id][parent_text.parent]
396
        end = parent_text.parent_pos + parent_text.num_lines
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
397
        return self._reconstruct(lines, parent_id, parent_text.parent_pos,
398
                                 end)
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
399
400
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
401
        """Append lines for the requested version_id range"""
402
        # stack of pending range requests
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
403
        pending_reqs = [(req_version_id, req_start, req_end)]
404
        while len(pending_reqs) > 0:
405
            req_version_id, req_start, req_end = pending_reqs.pop()
0.9.10 by Aaron Bentley
Text reconstruction seems to work
406
            # lazily allocate cursors for versions
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
407
            try:
408
                start, end, kind, data, iterator = self.cursor[req_version_id]
409
            except KeyError:
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
410
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
411
                start, end, kind, data = iterator.next()
0.9.22 by Aaron Bentley
Fix restoration bug
412
            if start > req_start:
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
413
                iterator = self.diffs.get_diff(req_version_id).range_iterator()
0.9.22 by Aaron Bentley
Fix restoration bug
414
                start, end, kind, data = iterator.next()
415
0.9.10 by Aaron Bentley
Text reconstruction seems to work
416
            # find the first hunk relevant to the request
417
            while end <= req_start:
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
418
                start, end, kind, data = iterator.next()
419
            self.cursor[req_version_id] = start, end, kind, data, iterator
0.9.10 by Aaron Bentley
Text reconstruction seems to work
420
            # if the hunk can't satisfy the whole request, split it in two,
421
            # and leave the second half for later.
422
            if req_end > end:
423
                pending_reqs.append((req_version_id, end, req_end))
424
                req_end = end
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
425
            if kind == 'new':
426
                lines.extend(data[req_start - start: (req_end - start)])
427
            else:
0.9.10 by Aaron Bentley
Text reconstruction seems to work
428
                # If the hunk is a ParentText, rewrite it as a range request
429
                # for the parent, and make it the next pending request.
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
430
                parent, parent_start, parent_end = data
0.9.10 by Aaron Bentley
Text reconstruction seems to work
431
                new_version_id = self.parents[req_version_id][parent]
432
                new_start = parent_start + req_start - start
433
                new_end = parent_end + req_end - end
434
                pending_reqs.append((new_version_id, new_start, new_end))
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
435
436
    def reconstruct_version(self, lines, version_id):
0.9.29 by Aaron Bentley
Support using disk for knit reconstruction
437
        length = self.diffs.get_diff(version_id).num_lines()
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
438
        return self._reconstruct(lines, version_id, 0, length)
0.9.25 by Aaron Bentley
More messy hacking
439
440
def gzip_string(lines):
441
    sio = StringIO()
442
    data_file = GzipFile(None, mode='wb', fileobj=sio)
443
    data_file.writelines(lines)
444
    data_file.close()
445
    return sio.getvalue()