~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

  • Committer: Michael Ellerman
  • Date: 2006-02-28 14:45:51 UTC
  • mto: (1558.1.18 Aaron's integration)
  • mto: This revision was merged to the branch mainline in revision 1586.
  • Revision ID: michael@ellerman.id.au-20060228144551-3d9941ecde4a0b0a
Update contrib/pwk for -p1 diffs from bzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006 by Canonical Ltd
 
1
# Copyright (C) 2005 by Canonical Ltd
2
2
 
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
26
26
add a revision to the store until everything it refers to is also
27
27
stored, so that if a revision is present we can totally recreate it.
28
28
However, we can't know what files are included in a revision until we
29
 
read its inventory.  So we query the inventory store of the source for
30
 
the ids we need, and then pull those ids and finally actually join
31
 
the inventories.
 
29
read its inventory.  Therefore, we first pull the XML and hold it in
 
30
memory until we've updated all of the files referenced.
32
31
"""
33
32
 
34
33
import bzrlib
35
34
import bzrlib.errors as errors
36
 
from bzrlib.errors import (InstallFailed,
37
 
                           )
 
35
from bzrlib.errors import (InstallFailed, NoSuchRevision, WeaveError,
 
36
                           MissingText)
38
37
from bzrlib.trace import mutter
39
 
from bzrlib.progress import ProgressPhase
 
38
from bzrlib.progress import ProgressBar
40
39
from bzrlib.revision import NULL_REVISION
41
 
from bzrlib.symbol_versioning import (deprecated_function,
42
 
        deprecated_method,
43
 
        zero_eight,
44
 
        )
 
40
from bzrlib.symbol_versioning import *
45
41
 
46
42
 
47
43
# TODO: Avoid repeatedly opening weaves so many times.
97
93
        # must not mutate self._last_revision as its potentially a shared instance
98
94
        self._last_revision = last_revision
99
95
        if pb is None:
100
 
            self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
101
 
            self.nested_pb = self.pb
 
96
            self.pb = bzrlib.ui.ui_factory.progress_bar()
102
97
        else:
103
98
            self.pb = pb
104
 
            self.nested_pb = None
105
99
        self.from_repository.lock_read()
106
100
        try:
107
101
            self.to_repository.lock_write()
108
102
            try:
109
103
                self.__fetch()
110
104
            finally:
111
 
                if self.nested_pb is not None:
112
 
                    self.nested_pb.finished()
113
105
                self.to_repository.unlock()
114
106
        finally:
115
107
            self.from_repository.unlock()
126
118
        self.from_control = self.from_repository.control_weaves
127
119
        self.count_total = 0
128
120
        self.file_ids_names = {}
129
 
        pp = ProgressPhase('Fetch phase', 4, self.pb)
130
121
        try:
131
 
            pp.next_phase()
132
122
            revs = self._revids_to_fetch()
133
 
            # something to do ?
134
 
            if revs:
135
 
                pp.next_phase()
 
123
            # nothing to do
 
124
            if revs: 
136
125
                self._fetch_weave_texts(revs)
137
 
                pp.next_phase()
138
126
                self._fetch_inventory_weave(revs)
139
 
                pp.next_phase()
140
127
                self._fetch_revision_texts(revs)
141
128
                self.count_copied += len(revs)
142
129
        finally:
143
130
            self.pb.clear()
144
131
 
145
132
    def _revids_to_fetch(self):
 
133
        self.pb.update('get destination history')
146
134
        mutter('fetch up to rev {%s}', self._last_revision)
147
135
        if self._last_revision is NULL_REVISION:
148
136
            # explicit limit of no revisions needed
157
145
        except errors.NoSuchRevision:
158
146
            raise InstallFailed([self._last_revision])
159
147
 
 
148
    def _fetch_revision_texts(self, revs):
 
149
        self.to_repository.revision_store.copy_multi(
 
150
            self.from_repository.revision_store,
 
151
            revs,
 
152
            pb=self.pb)
 
153
 
160
154
    def _fetch_weave_texts(self, revs):
161
 
        texts_pb = bzrlib.ui.ui_factory.nested_progress_bar()
162
 
        try:
163
 
            file_ids = self.from_repository.fileids_altered_by_revision_ids(revs)
164
 
            count = 0
165
 
            num_file_ids = len(file_ids)
166
 
            for file_id, required_versions in file_ids.items():
167
 
                texts_pb.update("fetch texts", count, num_file_ids)
168
 
                count +=1
169
 
                to_weave = self.to_weaves.get_weave_or_empty(file_id,
170
 
                    self.to_repository.get_transaction())
171
 
                from_weave = self.from_weaves.get_weave(file_id,
172
 
                    self.from_repository.get_transaction())
173
 
                # we fetch all the texts, because texts do
174
 
                # not reference anything, and its cheap enough
175
 
                to_weave.join(from_weave, version_ids=required_versions) 
176
 
                # we don't need *all* of this data anymore, but we dont know
177
 
                # what we do. This cache clearing will result in a new read 
178
 
                # of the knit data when we do the checkout, but probably we
179
 
                # want to emit the needed data on the fly rather than at the
180
 
                # end anyhow.
181
 
                # the from weave should know not to cache data being joined,
182
 
                # but its ok to ask it to clear.
183
 
                from_weave.clear_cache()
184
 
                to_weave.clear_cache()
185
 
        finally:
186
 
            texts_pb.finished()
 
155
        file_ids = self.from_repository.fileid_involved_by_set(revs)
 
156
        count = 0
 
157
        num_file_ids = len(file_ids)
 
158
        for file_id in file_ids:
 
159
            self.pb.update("merge weaves", count, num_file_ids)
 
160
            count +=1
 
161
            to_weave = self.to_weaves.get_weave_or_empty(file_id,
 
162
                self.to_repository.get_transaction())
 
163
            from_weave = self.from_weaves.get_weave(file_id,
 
164
                self.from_repository.get_transaction())
 
165
 
 
166
            if to_weave.numversions() > 0:
 
167
                # destination has contents, must merge
 
168
                try:
 
169
                    to_weave.join(from_weave)
 
170
                except errors.WeaveParentMismatch:
 
171
                    to_weave.reweave(from_weave)
 
172
            else:
 
173
                # destination is empty, just replace it
 
174
                to_weave = from_weave.copy()
 
175
 
 
176
            self.to_weaves.put_weave(file_id, to_weave,
 
177
                self.to_repository.get_transaction())
 
178
        self.pb.clear()
187
179
 
188
180
    def _fetch_inventory_weave(self, revs):
189
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
190
 
        try:
191
 
            pb.update("fetch inventory", 0, 2)
192
 
            to_weave = self.to_control.get_weave('inventory',
193
 
                    self.to_repository.get_transaction())
194
 
    
195
 
            child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
181
        self.pb.update("inventory fetch", 0, 2)
 
182
        from_weave = self.from_repository.get_inventory_weave()
 
183
        to_weave = self.to_repository.get_inventory_weave()
 
184
        self.pb.update("inventory fetch", 1, 2)
 
185
        to_weave = self.to_control.get_weave('inventory',
 
186
                self.to_repository.get_transaction())
 
187
        self.pb.update("inventory fetch", 2, 2)
 
188
 
 
189
        if to_weave.numversions() > 0:
 
190
            # destination has contents, must merge
196
191
            try:
197
 
                # just merge, this is optimisable and its means we don't
198
 
                # copy unreferenced data such as not-needed inventories.
199
 
                pb.update("fetch inventory", 1, 3)
200
 
                from_weave = self.from_repository.get_inventory_weave()
201
 
                pb.update("fetch inventory", 2, 3)
202
 
                # we fetch only the referenced inventories because we do not
203
 
                # know for unselected inventories whether all their required
204
 
                # texts are present in the other repository - it could be
205
 
                # corrupt.
206
 
                to_weave.join(from_weave, pb=child_pb, msg='merge inventory',
207
 
                              version_ids=revs)
208
 
            finally:
209
 
                child_pb.finished()
210
 
        finally:
211
 
            pb.finished()
212
 
 
213
 
 
214
 
class GenericRepoFetcher(RepoFetcher):
215
 
    """This is a generic repo to repo fetcher.
216
 
 
217
 
    This makes minimal assumptions about repo layout and contents.
218
 
    It triggers a reconciliation after fetching to ensure integrity.
219
 
    """
220
 
 
221
 
    def _fetch_revision_texts(self, revs):
222
 
        """Fetch revision object texts"""
223
 
        rev_pb = bzrlib.ui.ui_factory.nested_progress_bar()
224
 
        try:
225
 
            to_txn = self.to_transaction = self.to_repository.get_transaction()
226
 
            count = 0
227
 
            total = len(revs)
228
 
            to_store = self.to_repository._revision_store
229
 
            for rev in revs:
230
 
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
231
 
                try:
232
 
                    pb.update('copying revisions', count, total)
233
 
                    try:
234
 
                        sig_text = self.from_repository.get_signature_text(rev)
235
 
                        to_store.add_revision_signature_text(rev, sig_text, to_txn)
236
 
                    except errors.NoSuchRevision:
237
 
                        # not signed.
238
 
                        pass
239
 
                    to_store.add_revision(self.from_repository.get_revision(rev),
240
 
                                          to_txn)
241
 
                    count += 1
242
 
                finally:
243
 
                    pb.finished()
244
 
            # fixup inventory if needed: 
245
 
            # this is expensive because we have no inverse index to current ghosts.
246
 
            # but on local disk its a few seconds and sftp push is already insane.
247
 
            # so we just-do-it.
248
 
            # FIXME: repository should inform if this is needed.
249
 
            self.to_repository.reconcile()
250
 
        finally:
251
 
            rev_pb.finished()
252
 
    
253
 
 
254
 
class KnitRepoFetcher(RepoFetcher):
255
 
    """This is a knit format repository specific fetcher.
256
 
 
257
 
    This differs from the GenericRepoFetcher by not doing a 
258
 
    reconciliation after copying, and using knit joining to
259
 
    copy revision texts.
260
 
    """
261
 
 
262
 
    def _fetch_revision_texts(self, revs):
263
 
        # may need to be a InterRevisionStore call here.
264
 
        from_transaction = self.from_repository.get_transaction()
265
 
        to_transaction = self.to_repository.get_transaction()
266
 
        to_sf = self.to_repository._revision_store.get_signature_file(
267
 
            to_transaction)
268
 
        from_sf = self.from_repository._revision_store.get_signature_file(
269
 
            from_transaction)
270
 
        to_sf.join(from_sf, version_ids=revs, ignore_missing=True)
271
 
        to_rf = self.to_repository._revision_store.get_revision_file(
272
 
            to_transaction)
273
 
        from_rf = self.from_repository._revision_store.get_revision_file(
274
 
            from_transaction)
275
 
        to_rf.join(from_rf, version_ids=revs)
 
192
                to_weave.join(from_weave, pb=self.pb, msg='merge inventory')
 
193
            except errors.WeaveParentMismatch:
 
194
                to_weave.reweave(from_weave, pb=self.pb, msg='reweave inventory')
 
195
        else:
 
196
            # destination is empty, just replace it
 
197
            to_weave = from_weave.copy()
 
198
 
 
199
        self.to_control.put_weave('inventory', to_weave,
 
200
            self.to_repository.get_transaction())
 
201
 
 
202
        self.pb.clear()
276
203
 
277
204
 
278
205
class Fetcher(object):
279
 
    """Backwards compatibility glue for branch.fetch()."""
 
206
    """Backwards compatability glue for branch.fetch()."""
280
207
 
281
208
    @deprecated_method(zero_eight)
282
209
    def __init__(self, to_branch, from_branch, last_revision=None, pb=None):