/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 = {}
235
        self._lines = {}
236
        self._parents = {}
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
237
        self._snapshots = set()
0.9.12 by Aaron Bentley
Make benchmarks for mp
238
        self.snapshot_interval = snapshot_interval
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
239
        self.max_snapshots = max_snapshots
0.9.12 by Aaron Bentley
Make benchmarks for mp
240
241
    def do_snapshot(self, version_id, parent_ids):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
242
        if self.snapshot_interval is None:
243
            return False
244
        if self.max_snapshots is not None and\
245
            len(self._snapshots) == self.max_snapshots:
0.9.14 by Aaron Bentley
Temporarily force snapshots to 44
246
            return False
0.9.12 by Aaron Bentley
Make benchmarks for mp
247
        if len(parent_ids) == 0:
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
248
            return True
249
        for ignored in xrange(self.snapshot_interval):
0.9.12 by Aaron Bentley
Make benchmarks for mp
250
            if len(parent_ids) == 0:
251
                return False
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
252
            version_ids = parent_ids
253
            parent_ids = []
254
            for version_id in version_ids:
255
                if version_id not in self._snapshots:
256
                    parent_ids.extend(self._parents[version_id])
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
257
        else:
258
            return True
0.9.8 by Aaron Bentley
get add_version working
259
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
260
    def add_version(self, lines, version_id, parent_ids,
0.9.20 by Aaron Bentley
Convert to a plugin
261
                    force_snapshot=None, single_parent=False):
0.9.16 by Aaron Bentley
More control over snapshotting, disable caching for inventory
262
        if force_snapshot is None:
263
            do_snapshot = self.do_snapshot(version_id, parent_ids)
264
        else:
265
            do_snapshot = force_snapshot
266
        if do_snapshot:
267
            self._snapshots.add(version_id)
0.9.12 by Aaron Bentley
Make benchmarks for mp
268
            diff = MultiParent([NewText(lines)])
269
        else:
0.9.20 by Aaron Bentley
Convert to a plugin
270
            if single_parent:
271
                parent_lines = self.get_line_list(parent_ids[:1])
272
            else:
273
                parent_lines = self.get_line_list(parent_ids)
0.9.12 by Aaron Bentley
Make benchmarks for mp
274
            diff = MultiParent.from_lines(lines, parent_lines)
0.9.25 by Aaron Bentley
More messy hacking
275
            snapdiff = MultiParent([NewText(lines)])
276
            if diff.is_snapshot():
277
                self._snapshots.add(version_id)
278
            elif diff.patch_len() >= snapdiff.patch_len():
279
                trace.note("Forcing snapshot")
280
                self._snapshots.add(version_id)
0.9.8 by Aaron Bentley
get add_version working
281
        self.add_diff(diff, version_id, parent_ids)
282
        self._lines[version_id] = lines
283
284
    def add_diff(self, diff, version_id, parent_ids):
285
        self._diffs[version_id] = diff
286
        self._parents[version_id] = parent_ids
287
0.9.20 by Aaron Bentley
Convert to a plugin
288
    def import_versionedfile(self, vf, snapshots, no_cache=True,
0.9.22 by Aaron Bentley
Fix restoration bug
289
                             single_parent=False, verify=False):
0.9.20 by Aaron Bentley
Convert to a plugin
290
        """Import all revisions of a versionedfile
291
292
        :param vf: The versionedfile to import
293
        :param snapshots: If provided, the revisions to make snapshots of.
294
            Otherwise, this will be auto-determined
295
        :param no_cache: If true, clear the cache after every add.
296
        :param single_parent: If true, omit all but one parent text, (but
297
            retain parent metadata).
298
        """
0.9.22 by Aaron Bentley
Fix restoration bug
299
        assert no_cache or not verify
0.9.19 by Aaron Bentley
More tweakage
300
        revisions = set(vf.versions())
301
        total = len(revisions)
0.9.20 by Aaron Bentley
Convert to a plugin
302
        pb = ui.ui_factory.nested_progress_bar()
303
        try:
304
            while len(revisions) > 0:
305
                added = set()
306
                for revision in revisions:
307
                    parents = vf.get_parents(revision)
308
                    if [p for p in parents if p not in self._diffs] != []:
309
                        continue
310
                    lines = [a + ' ' + l for a, l in
311
                             vf.annotate_iter(revision)]
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
312
                    if snapshots is None:
0.9.20 by Aaron Bentley
Convert to a plugin
313
                        force_snapshot = None
314
                    else:
0.9.21 by Aaron Bentley
finish converting ft_ to snapshots
315
                        force_snapshot = (revision in snapshots)
0.9.20 by Aaron Bentley
Convert to a plugin
316
                    self.add_version(lines, revision, parents, force_snapshot,
317
                                     single_parent)
318
                    added.add(revision)
319
                    if no_cache:
320
                        self.clear_cache()
0.9.25 by Aaron Bentley
More messy hacking
321
                        vf.clear_cache()
0.9.22 by Aaron Bentley
Fix restoration bug
322
                        if verify:
323
                            assert lines == self.get_line_list([revision])[0]
324
                            self.clear_cache()
0.9.20 by Aaron Bentley
Convert to a plugin
325
                    pb.update('Importing revisions',
326
                              (total - len(revisions)) + len(added), total)
327
                revisions = [r for r in revisions if r not in added]
328
        finally:
329
            pb.finished()
0.9.19 by Aaron Bentley
More tweakage
330
0.9.23 by Aaron Bentley
handle snapshots all at once
331
    def select_snapshots(self, vf):
332
        distances = {}
333
        descendants = {}
334
        snapshots = set()
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
335
        for version_id in topo_iter(vf):
336
            p_distances = [distances[p] for p in vf.get_parents(version_id)]
337
            if len(p_distances) == 0:
338
                snapshots.add(version_id)
339
                distances[version_id] = 0
340
            else:
341
                max_distance = max(p_distances)
342
                if max_distance + 1 > self.snapshot_interval:
343
                    snapshots.add(version_id)
344
                    distances[version_id] = 0
345
                elif len(descendants) > 1 and max_distance > \
346
                    self.snapshot_interval -4 and False:
0.9.23 by Aaron Bentley
handle snapshots all at once
347
                    snapshots.add(version_id)
348
                    distances[version_id] = 0
349
                else:
0.9.26 by Aaron Bentley
Move topological iteration into an iterator
350
                    distances[version_id] = max_distance + 1
0.9.23 by Aaron Bentley
handle snapshots all at once
351
        return snapshots
352
353
0.9.8 by Aaron Bentley
get add_version working
354
    def clear_cache(self):
355
        self._lines.clear()
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
356
357
    def get_line_list(self, version_ids):
358
        return [self.cache_version(v) for v in version_ids]
359
360
    def cache_version(self, version_id):
361
        try:
362
            return self._lines[version_id]
363
        except KeyError:
364
            pass
365
        diff = self._diffs[version_id]
366
        lines = []
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
367
        reconstructor = _Reconstructor(self._diffs, self._lines,
368
                                       self._parents)
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
369
        reconstructor.reconstruct_version(lines, version_id)
0.9.25 by Aaron Bentley
More messy hacking
370
        #self._lines[version_id] = lines
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
371
        return lines
372
373
374
class _Reconstructor(object):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
375
    """Build a text from the diffs, ancestry graph and cached lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
376
377
    def __init__(self, diffs, lines, parents):
378
        self.diffs = diffs
379
        self.lines = lines
380
        self.parents = parents
381
        self.cursor = {}
382
383
    def reconstruct(self, lines, parent_text, version_id):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
384
        """Append the lines referred to by a ParentText to lines"""
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
385
        parent_id = self.parents[version_id][parent_text.parent]
386
        end = parent_text.parent_pos + parent_text.num_lines
0.9.17 by Aaron Bentley
Dynamically select snapshots based on all parents
387
        return self._reconstruct(lines, parent_id, parent_text.parent_pos,
388
                                 end)
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
389
390
    def _reconstruct(self, lines, req_version_id, req_start, req_end):
0.9.10 by Aaron Bentley
Text reconstruction seems to work
391
        """Append lines for the requested version_id range"""
392
        # stack of pending range requests
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
393
        pending_reqs = [(req_version_id, req_start, req_end)]
394
        while len(pending_reqs) > 0:
395
            req_version_id, req_start, req_end = pending_reqs.pop()
0.9.10 by Aaron Bentley
Text reconstruction seems to work
396
            # lazily allocate cursors for versions
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
397
            try:
398
                start, end, kind, data, iterator = self.cursor[req_version_id]
399
            except KeyError:
400
                iterator = self.diffs[req_version_id].range_iterator()
401
                start, end, kind, data = iterator.next()
0.9.22 by Aaron Bentley
Fix restoration bug
402
            if start > req_start:
403
                iterator = self.diffs[req_version_id].range_iterator()
404
                start, end, kind, data = iterator.next()
405
0.9.10 by Aaron Bentley
Text reconstruction seems to work
406
            # find the first hunk relevant to the request
407
            while end <= req_start:
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
408
                start, end, kind, data = iterator.next()
409
            self.cursor[req_version_id] = start, end, kind, data, iterator
0.9.10 by Aaron Bentley
Text reconstruction seems to work
410
            # if the hunk can't satisfy the whole request, split it in two,
411
            # and leave the second half for later.
412
            if req_end > end:
413
                pending_reqs.append((req_version_id, end, req_end))
414
                req_end = end
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
415
            if kind == 'new':
416
                lines.extend(data[req_start - start: (req_end - start)])
417
            else:
0.9.10 by Aaron Bentley
Text reconstruction seems to work
418
                # If the hunk is a ParentText, rewrite it as a range request
419
                # for the parent, and make it the next pending request.
0.9.9 by Aaron Bentley
Much progress on non-naive text reconstruction
420
                parent, parent_start, parent_end = data
0.9.10 by Aaron Bentley
Text reconstruction seems to work
421
                new_version_id = self.parents[req_version_id][parent]
422
                new_start = parent_start + req_start - start
423
                new_end = parent_end + req_end - end
424
                pending_reqs.append((new_version_id, new_start, new_end))
0.9.11 by Aaron Bentley
Implement reconstruct_version, handle all hunks through that
425
426
    def reconstruct_version(self, lines, version_id):
427
        length = self.diffs[version_id].num_lines()
428
        return self._reconstruct(lines, version_id, 0, length)
0.9.25 by Aaron Bentley
More messy hacking
429
430
def gzip_string(lines):
431
    sio = StringIO()
432
    data_file = GzipFile(None, mode='wb', fileobj=sio)
433
    data_file.writelines(lines)
434
    data_file.close()
435
    return sio.getvalue()