/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


"""Copying of history from one branch to another.

The basic plan is that every branch knows the history of everything
that has merged into it.  As the first step of a merge, pull, or
branch operation we copy history from the source into the destination
branch.

The copying is done in a slightly complicated order.  We don't want to
add a revision to the store until everything it refers to is also
stored, so that if a revision is present we can totally recreate it.
However, we can't know what files are included in a revision until we
read its inventory.  So we query the inventory store of the source for
the ids we need, and then pull those ids and then return to the inventories.
"""

import operator

import bzrlib
import bzrlib.errors as errors
from bzrlib.errors import InstallFailed
from bzrlib.progress import ProgressPhase
from bzrlib.revision import NULL_REVISION
from bzrlib.tsort import topo_sort
from bzrlib.trace import mutter
import bzrlib.ui
from bzrlib.versionedfile import filter_absent, FulltextContentFactory

# TODO: Avoid repeatedly opening weaves so many times.

# XXX: This doesn't handle ghost (not present in branch) revisions at
# all yet.  I'm not sure they really should be supported.

# NOTE: This doesn't copy revisions which may be present but not
# merged into the last revision.  I'm not sure we want to do that.

# - get a list of revisions that need to be pulled in
# - for each one, pull in that revision file
#   and get the inventory, and store the inventory with right
#   parents.
# - and get the ancestry, and store that with right parents too
# - and keep a note of all file ids and version seen
# - then go through all files; for each one get the weave,
#   and add in all file versions

def _pb_stream_adapter(pb, msg, num_keys, stream):
    def adapter():
        for idx, record in enumerate(stream):
            pb.update(msg, idx, num_keys)
            yield record
    return adapter


class RepoFetcher(object):
    """Pull revisions and texts from one repository to another.

    last_revision
        if set, try to limit to the data this revision references.

    after running:
    count_copied -- number of revisions copied

    This should not be used directly, it's essential a object to encapsulate
    the logic in InterRepository.fetch().
    """

    def __init__(self, to_repository, from_repository, last_revision=None, pb=None,
        find_ghosts=True):
        """Create a repo fetcher.

        :param find_ghosts: If True search the entire history for ghosts.
        :param _write_group_acquired_callable: Don't use; this parameter only
            exists to facilitate a hack done in InterPackRepo.fetch.  We would
            like to remove this parameter.
        """
        # result variables.
        self.failed_revisions = []
        self.count_copied = 0
        if to_repository.has_same_location(from_repository):
            # repository.fetch should be taking care of this case.
            raise errors.BzrError('RepoFetcher run '
                    'between two objects at the same location: '
                    '%r and %r' % (to_repository, from_repository))
        self.to_repository = to_repository
        self.from_repository = from_repository
        self.sink = to_repository._get_sink()
        # must not mutate self._last_revision as its potentially a shared instance
        self._last_revision = last_revision
        self.find_ghosts = find_ghosts
        if pb is None:
            self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
            self.nested_pb = self.pb
        else:
            self.pb = pb
            self.nested_pb = None
        self.from_repository.lock_read()
        try:
            self.to_repository.lock_write()
            try:
                self.to_repository.start_write_group()
                try:
                    self.__fetch()
                except:
                    self.to_repository.abort_write_group(suppress_errors=True)
                    raise
                else:
                    self.to_repository.commit_write_group()
            finally:
                try:
                    if self.nested_pb is not None:
                        self.nested_pb.finished()
                finally:
                    self.to_repository.unlock()
        finally:
            self.from_repository.unlock()

    def __fetch(self):
        """Primary worker function.

        This initialises all the needed variables, and then fetches the 
        requested revisions, finally clearing the progress bar.
        """
        # Roughly this is what we're aiming for fetch to become:
        #
        # missing = self.sink.insert_stream(self.source.get_stream(search))
        # if missing:
        #     missing = self.sink.insert_stream(self.source.get_items(missing))
        # assert not missing
        self.count_total = 0
        self.file_ids_names = {}
        pp = ProgressPhase('Transferring', 4, self.pb)
        try:
            pp.next_phase()
            search = self._revids_to_fetch()
            if search is None:
                return
            self._fetch_everything_for_search(search, pp)
        finally:
            self.pb.clear()

    def _fetch_everything_for_search(self, search, pp):
        """Fetch all data for the given set of revisions."""
        # The first phase is "file".  We pass the progress bar for it directly
        # into item_keys_introduced_by, which has more information about how
        # that phase is progressing than we do.  Progress updates for the other
        # phases are taken care of in this function.
        # XXX: there should be a clear owner of the progress reporting.  Perhaps
        # item_keys_introduced_by should have a richer API than it does at the
        # moment, so that it can feed the progress information back to this
        # function?
        self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
        try:
            from_format = self.from_repository._format
            stream = self.get_stream(search, pp)
            missing_keys = self.sink.insert_stream(stream, from_format)
            if missing_keys:
                stream = self.get_stream_for_missing_keys(missing_keys)
                missing_keys = self.sink.insert_stream(stream, from_format)
            if missing_keys:
                raise AssertionError(
                    "second push failed to complete a fetch %r." % (
                        missing_keys,))
            self.sink.finished()
        finally:
            if self.pb is not None:
                self.pb.finished()

    def get_stream(self, search, pp):
        phase = 'file'
        revs = search.get_keys()
        graph = self.from_repository.get_graph()
        revs = list(graph.iter_topo_order(revs))
        data_to_fetch = self.from_repository.item_keys_introduced_by(
            revs, self.pb)
        text_keys = []
        for knit_kind, file_id, revisions in data_to_fetch:
            if knit_kind != phase:
                phase = knit_kind
                # Make a new progress bar for this phase
                self.pb.finished()
                pp.next_phase()
                self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
            if knit_kind == "file":
                # Accumulate file texts
                text_keys.extend([(file_id, revision) for revision in
                    revisions])
            elif knit_kind == "inventory":
                # Now copy the file texts.
                to_texts = self.to_repository.texts
                from_texts = self.from_repository.texts
                yield ('texts', from_texts.get_record_stream(
                    text_keys, self.to_repository._fetch_order,
                    not self.to_repository._fetch_uses_deltas))
                # Cause an error if a text occurs after we have done the
                # copy.
                text_keys = None
                # Before we process the inventory we generate the root
                # texts (if necessary) so that the inventories references
                # will be valid.
                for _ in self._generate_root_texts(revs):
                    yield _
                # NB: This currently reopens the inventory weave in source;
                # using a single stream interface instead would avoid this.
                self.pb.update("fetch inventory", 0, 1)
                # we fetch only the referenced inventories because we do not
                # know for unselected inventories whether all their required
                # texts are present in the other repository - it could be
                # corrupt.
                for info in self._get_inventory_stream(revs):
                    yield info
            elif knit_kind == "signatures":
                # Nothing to do here; this will be taken care of when
                # _fetch_revision_texts happens.
                pass
            elif knit_kind == "revisions":
                for _ in self._fetch_revision_texts(revs, self.pb):
                    yield _
            else:
                raise AssertionError("Unknown knit kind %r" % knit_kind)
        self.count_copied += len(revs)

    def get_stream_for_missing_keys(self, missing_keys):
        # missing keys can only occur when we are byte copying and not
        # translating (because translation means we don't send
        # unreconstructable deltas ever).
        keys = {}
        keys['texts'] = set()
        keys['revisions'] = set()
        keys['inventories'] = set()
        keys['signatures'] = set()
        for key in missing_keys:
            keys[key[0]].add(key[1:])
        if len(keys['revisions']):
            # If we allowed copying revisions at this point, we could end up
            # copying a revision without copying its required texts: a
            # violation of the requirements for repository integrity.
            raise AssertionError(
                'cannot copy revisions to fill in missing deltas %s' % (
                    keys['revisions'],))
        for substream_kind, keys in keys.iteritems():
            vf = getattr(self.from_repository, substream_kind)
            # Ask for full texts always so that we don't need more round trips
            # after this stream.
            stream = vf.get_record_stream(keys,
                self.to_repository._fetch_order, True)
            yield substream_kind, stream

    def _revids_to_fetch(self):
        """Determines the exact revisions needed from self.from_repository to
        install self._last_revision in self.to_repository.

        If no revisions need to be fetched, then this just returns None.
        """
        mutter('fetch up to rev {%s}', self._last_revision)
        if self._last_revision is NULL_REVISION:
            # explicit limit of no revisions needed
            return None
        if (self._last_revision is not None and
            self.to_repository.has_revision(self._last_revision)):
            return None
        try:
            return self.to_repository.search_missing_revision_ids(
                self.from_repository, self._last_revision,
                find_ghosts=self.find_ghosts)
        except errors.NoSuchRevision, e:
            raise InstallFailed([self._last_revision])

    def _get_inventory_stream(self, revision_ids):
        if (self.from_repository._format.supports_chks and
            self.to_repository._format.supports_chks
            and (self.from_repository._format._serializer
                 == self.to_repository._format._serializer)):
            # Both sides support chks, and they use the same serializer, so it
            # is safe to transmit the chk pages and inventory pages across
            # as-is.
            return self._get_chk_inventory_stream(revision_ids)
        elif (not self.from_repository._format.supports_chks):
            # Source repository doesn't support chks. So we can transmit the
            # inventories 'as-is' and either they are just accepted on the
            # target, or the Sink will properly convert it.
            return self._get_simple_inventory_stream(revision_ids)
        else:
            # XXX: Hack to make not-chk->chk fetch: copy the inventories as
            #      inventories. Note that this should probably be done somehow
            #      as part of bzrlib.repository.StreamSink. Except JAM couldn't
            #      figure out how a non-chk repository could possibly handle
            #      deserializing an inventory stream from a chk repo, as it
            #      doesn't have a way to understand individual pages.
            return self._get_convertable_inventory_stream(revision_ids)

    def _get_simple_inventory_stream(self, revision_ids):
        from_weave = self.from_repository.inventories
        yield ('inventories', from_weave.get_record_stream(
            [(rev_id,) for rev_id in revision_ids],
            self.inventory_fetch_order(),
            not self.delta_on_metadata()))

    def _get_chk_inventory_stream(self, revision_ids):
        """Fetch the inventory texts, along with the associated chk maps."""
        from bzrlib import inventory, chk_map
        # We want an inventory outside of the search set, so that we can filter
        # out uninteresting chk pages. For now we use
        # _find_revision_outside_set, but if we had a Search with cut_revs, we
        # could use that instead.
        start_rev_id = self.from_repository._find_revision_outside_set(
                            revision_ids)
        start_rev_key = (start_rev_id,)
        inv_keys_to_fetch = [(rev_id,) for rev_id in revision_ids]
        if start_rev_id != NULL_REVISION:
            inv_keys_to_fetch.append((start_rev_id,))
        # Any repo that supports chk_bytes must also support out-of-order
        # insertion. At least, that is how we expect it to work
        # We use get_record_stream instead of iter_inventories because we want
        # to be able to insert the stream as well. We could instead fetch
        # allowing deltas, and then iter_inventories, but we don't know whether
        # source or target is more 'local' anway.
        inv_stream = self.from_repository.inventories.get_record_stream(
            inv_keys_to_fetch, 'unordered',
            True) # We need them as full-texts so we can find their references
        uninteresting_chk_roots = set()
        interesting_chk_roots = set()
        def filter_inv_stream(inv_stream):
            for idx, record in enumerate(inv_stream):
                ### child_pb.update('fetch inv', idx, len(inv_keys_to_fetch))
                bytes = record.get_bytes_as('fulltext')
                chk_inv = inventory.CHKInventory.deserialise(
                    self.from_repository.chk_bytes, bytes, record.key)
                if record.key == start_rev_key:
                    uninteresting_chk_roots.add(chk_inv.id_to_entry.key())
                    p_id_map = chk_inv.parent_id_basename_to_file_id
                    if p_id_map is not None:
                        uninteresting_chk_roots.add(p_id_map.key())
                else:
                    yield record
                    interesting_chk_roots.add(chk_inv.id_to_entry.key())
                    p_id_map = chk_inv.parent_id_basename_to_file_id
                    if p_id_map is not None:
                        interesting_chk_roots.add(p_id_map.key())
        ### pb.update('fetch inventory', 0, 2)
        yield ('inventories', filter_inv_stream(inv_stream))
        # Now that we have worked out all of the interesting root nodes, grab
        # all of the interesting pages and insert them
        ### pb.update('fetch inventory', 1, 2)
        interesting = chk_map.iter_interesting_nodes(
            self.from_repository.chk_bytes, interesting_chk_roots,
            uninteresting_chk_roots)
        def to_stream_adapter():
            """Adapt the iter_interesting_nodes result to a single stream.

            iter_interesting_nodes returns records as it processes them, which
            can be in batches. But we only want a single stream to be inserted.
            """
            for record, items in interesting:
                for value in record.itervalues():
                    yield value
        # XXX: We could instead call get_record_stream(records.keys())
        #      ATM, this will always insert the records as fulltexts, and
        #      requires that you can hang on to records once you have gone
        #      on to the next one. Further, it causes the target to
        #      recompress the data. Testing shows it to be faster than
        #      requesting the records again, though.
        yield ('chk_bytes', to_stream_adapter())
        ### pb.update('fetch inventory', 2, 2)

    def _get_convertable_inventory_stream(self, revision_ids):
        # XXX: One of source or target is using chks, and they don't have
        #      compatible serializations. The StreamSink code expects to be
        #      able to convert on the target, so we need to put
        #      bytes-on-the-wire that can be converted
        yield ('inventories', self._stream_invs_as_fulltexts(revision_ids))

    def _stream_invs_as_fulltexts(self, revision_ids):
        from_serializer = self.from_repository._format._serializer
        revision_keys = [(rev_id,) for rev_id in revision_ids]
        parent_map = self.from_repository.inventory.get_parent_map(revision_keys)
        for inv in self.from_repository.iter_inventories(revision_ids):
            # XXX: This is a bit hackish, but it works. Basically,
            #      CHKSerializer 'accidentally' supports
            #      read/write_inventory_to_string, even though that is never
            #      the format that is stored on disk. It *does* give us a
            #      single string representation for an inventory, so live with
            #      it for now.
            #      This would be far better if we had a 'serialized inventory
            #      delta' form. Then we could use 'inventory._make_delta', and
            #      transmit that. This would both be faster to generate, and
            #      result in fewer bytes-on-the-wire.
            as_bytes = from_serializer.write_inventory_to_string(inv)
            key = (inv.revision_id,)
            parent_keys = parent_map.get(key, ())
            yield FulltextContentFactory(key, parent_keys, None, as_bytes)

    def _fetch_revision_texts(self, revs, pb):
        # fetch signatures first and then the revision texts
        # may need to be a InterRevisionStore call here.
        from_sf = self.from_repository.signatures
        # A missing signature is just skipped.
        keys = [(rev_id,) for rev_id in revs]
        signatures = filter_absent(from_sf.get_record_stream(
            keys,
            self.to_repository._fetch_order,
            not self.to_repository._fetch_uses_deltas))
        # If a revision has a delta, this is actually expanded inside the
        # insert_record_stream code now, which is an alternate fix for
        # bug #261339
        from_rf = self.from_repository.revisions
        revisions = from_rf.get_record_stream(
            keys,
            self.to_repository._fetch_order,
            not self.delta_on_metadata())
        return [('signatures', signatures), ('revisions', revisions)]

    def _generate_root_texts(self, revs):
        """This will be called by __fetch between fetching weave texts and
        fetching the inventory weave.

        Subclasses should override this if they need to generate root texts
        after fetching weave texts.
        """
        return []

    def inventory_fetch_order(self):
        return self.to_repository._fetch_order

    def delta_on_metadata(self):
        src_serializer = self.from_repository._format._serializer
        target_serializer = self.to_repository._format._serializer
        return (self.to_repository._fetch_uses_deltas and
            src_serializer == target_serializer)


class Inter1and2Helper(object):
    """Helper for operations that convert data from model 1 and 2
    
    This is for use by fetchers and converters.
    """

    def __init__(self, source):
        """Constructor.

        :param source: The repository data comes from
        """
        self.source = source

    def iter_rev_trees(self, revs):
        """Iterate through RevisionTrees efficiently.

        Additionally, the inventory's revision_id is set if unset.

        Trees are retrieved in batches of 100, and then yielded in the order
        they were requested.

        :param revs: A list of revision ids
        """
        # In case that revs is not a list.
        revs = list(revs)
        while revs:
            for tree in self.source.revision_trees(revs[:100]):
                if tree.inventory.revision_id is None:
                    tree.inventory.revision_id = tree.get_revision_id()
                yield tree
            revs = revs[100:]

    def _find_root_ids(self, revs, parent_map, graph):
        revision_root = {}
        planned_versions = {}
        for tree in self.iter_rev_trees(revs):
            revision_id = tree.inventory.root.revision
            root_id = tree.get_root_id()
            planned_versions.setdefault(root_id, []).append(revision_id)
            revision_root[revision_id] = root_id
        # Find out which parents we don't already know root ids for
        parents = set()
        for revision_parents in parent_map.itervalues():
            parents.update(revision_parents)
        parents.difference_update(revision_root.keys() + [NULL_REVISION])
        # Limit to revisions present in the versionedfile
        parents = graph.get_parent_map(parents).keys()
        for tree in self.iter_rev_trees(parents):
            root_id = tree.get_root_id()
            revision_root[tree.get_revision_id()] = root_id
        return revision_root, planned_versions

    def generate_root_texts(self, revs):
        """Generate VersionedFiles for all root ids.

        :param revs: the revisions to include
        """
        graph = self.source.get_graph()
        parent_map = graph.get_parent_map(revs)
        rev_order = topo_sort(parent_map)
        rev_id_to_root_id, root_id_to_rev_ids = self._find_root_ids(
            revs, parent_map, graph)
        root_id_order = [(rev_id_to_root_id[rev_id], rev_id) for rev_id in
            rev_order]
        # Guaranteed stable, this groups all the file id operations together
        # retaining topological order within the revisions of a file id.
        # File id splits and joins would invalidate this, but they don't exist
        # yet, and are unlikely to in non-rich-root environments anyway.
        root_id_order.sort(key=operator.itemgetter(0))
        # Create a record stream containing the roots to create.
        def yield_roots():
            for key in root_id_order:
                root_id, rev_id = key
                rev_parents = parent_map[rev_id]
                # We drop revision parents with different file-ids, because
                # that represents a rename of the root to a different location
                # - its not actually a parent for us. (We could look for that
                # file id in the revision tree at considerably more expense,
                # but for now this is sufficient (and reconcile will catch and
                # correct this anyway).
                # When a parent revision is a ghost, we guess that its root id
                # was unchanged (rather than trimming it from the parent list).
                parent_keys = tuple((root_id, parent) for parent in rev_parents
                    if parent != NULL_REVISION and
                        rev_id_to_root_id.get(parent, root_id) == root_id)
                yield FulltextContentFactory(key, parent_keys, None, '')
        return [('texts', yield_roots())]


class Model1toKnit2Fetcher(RepoFetcher):
    """Fetch from a Model1 repository into a Knit2 repository
    """
    def __init__(self, to_repository, from_repository, last_revision=None,
                 pb=None, find_ghosts=True):
        self.helper = Inter1and2Helper(from_repository)
        RepoFetcher.__init__(self, to_repository, from_repository,
            last_revision, pb, find_ghosts)

    def _generate_root_texts(self, revs):
        return self.helper.generate_root_texts(revs)

    def inventory_fetch_order(self):
        return 'topological'

Knit1to2Fetcher = Model1toKnit2Fetcher