~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

Add a test environment for InterRepository objects, and remove the fetch corner case tests from test_repository.

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
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
 
17
from copy import copy
 
18
import os
 
19
from cStringIO import StringIO
 
20
 
 
21
import bzrlib
 
22
import bzrlib.errors as errors
 
23
from bzrlib.errors import (InstallFailed, NoSuchRevision, WeaveError,
 
24
                           MissingText)
 
25
from bzrlib.trace import mutter, note, warning
 
26
from bzrlib.branch import Branch
 
27
from bzrlib.progress import ProgressBar
 
28
from bzrlib.revision import NULL_REVISION
 
29
from bzrlib.xml5 import serializer_v5
 
30
from bzrlib.osutils import sha_string, split_lines
17
31
 
18
32
"""Copying of history from one branch to another.
19
33
 
26
40
add a revision to the store until everything it refers to is also
27
41
stored, so that if a revision is present we can totally recreate it.
28
42
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.
 
43
read its inventory.  Therefore, we first pull the XML and hold it in
 
44
memory until we've updated all of the files referenced.
32
45
"""
33
46
 
34
 
import bzrlib
35
 
import bzrlib.errors as errors
36
 
from bzrlib.errors import (InstallFailed, NoSuchRevision,
37
 
                           MissingText)
38
 
from bzrlib.trace import mutter
39
 
from bzrlib.progress import ProgressBar, ProgressPhase
40
 
from bzrlib.reconcile import RepoReconciler
41
 
from bzrlib.revision import NULL_REVISION
42
 
from bzrlib.symbol_versioning import *
43
 
 
44
 
 
45
47
# TODO: Avoid repeatedly opening weaves so many times.
46
48
 
47
49
# XXX: This doesn't handle ghost (not present in branch) revisions at
60
62
#   and add in all file versions
61
63
 
62
64
 
63
 
@deprecated_function(zero_eight)
 
65
 
64
66
def greedy_fetch(to_branch, from_branch, revision=None, pb=None):
65
 
    """Legacy API, please see branch.fetch(from_branch, last_revision, pb)."""
66
67
    f = Fetcher(to_branch, from_branch, revision, pb)
67
68
    return f.count_copied, f.failed_revisions
68
69
 
69
 
fetch = greedy_fetch
70
 
 
71
70
 
72
71
class RepoFetcher(object):
73
72
    """Pull revisions and texts from one repository to another.
77
76
 
78
77
    after running:
79
78
    count_copied -- number of revisions copied
80
 
 
81
 
    This should not be used directory, its essential a object to encapsulate
82
 
    the logic in InterRepository.fetch().
 
79
    count_weaves -- number of file weaves copied
83
80
    """
84
81
    def __init__(self, to_repository, from_repository, last_revision=None, pb=None):
85
 
        # result variables.
86
 
        self.failed_revisions = []
87
 
        self.count_copied = 0
88
 
        if to_repository.control_files._transport.base == from_repository.control_files._transport.base:
 
82
        if to_repository.bzrdir.transport.base == from_repository.bzrdir.transport.base:
89
83
            # check that last_revision is in 'from' and then return a no-operation.
90
84
            if last_revision not in (None, NULL_REVISION):
91
85
                from_repository.get_revision(last_revision)
95
89
        # must not mutate self._last_revision as its potentially a shared instance
96
90
        self._last_revision = last_revision
97
91
        if pb is None:
98
 
            self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
99
 
            self.nested_pb = self.pb
 
92
            self.pb = bzrlib.ui.ui_factory.progress_bar()
100
93
        else:
101
94
            self.pb = pb
102
 
            self.nested_pb = None
103
95
        self.from_repository.lock_read()
104
96
        try:
105
97
            self.to_repository.lock_write()
106
98
            try:
107
99
                self.__fetch()
108
100
            finally:
109
 
                if self.nested_pb is not None:
110
 
                    self.nested_pb.finished()
111
101
                self.to_repository.unlock()
112
102
        finally:
113
103
            self.from_repository.unlock()
122
112
        self.to_control = self.to_repository.control_weaves
123
113
        self.from_weaves = self.from_repository.weave_store
124
114
        self.from_control = self.from_repository.control_weaves
 
115
        self.failed_revisions = []
 
116
        self.count_copied = 0
125
117
        self.count_total = 0
 
118
        self.count_weaves = 0
 
119
        self.copied_file_ids = set()
126
120
        self.file_ids_names = {}
127
 
        pp = ProgressPhase('fetch phase', 4, self.pb)
128
121
        try:
129
122
            revs = self._revids_to_fetch()
130
 
            # something to do ?
131
 
            if revs:
132
 
                pp.next_phase()
 
123
            # nothing to do
 
124
            if revs: 
133
125
                self._fetch_weave_texts(revs)
134
 
                pp.next_phase()
135
126
                self._fetch_inventory_weave(revs)
136
 
                pp.next_phase()
137
127
                self._fetch_revision_texts(revs)
138
128
                self.count_copied += len(revs)
139
129
        finally:
154
144
        except errors.NoSuchRevision:
155
145
            raise InstallFailed([self._last_revision])
156
146
 
 
147
    def _fetch_revision_texts(self, revs):
 
148
        self.to_repository.revision_store.copy_multi(
 
149
            self.from_repository.revision_store,
 
150
            revs,
 
151
            pb=self.pb)
 
152
 
157
153
    def _fetch_weave_texts(self, revs):
158
 
        texts_pb = bzrlib.ui.ui_factory.nested_progress_bar()
159
 
        try:
160
 
            file_ids = self.from_repository.fileids_altered_by_revision_ids(revs)
161
 
            count = 0
162
 
            num_file_ids = len(file_ids)
163
 
            for file_id, required_versions in file_ids.items():
164
 
                texts_pb.update("fetch texts", count, num_file_ids)
165
 
                count +=1
166
 
                to_weave = self.to_weaves.get_weave_or_empty(file_id,
167
 
                    self.to_repository.get_transaction())
168
 
                from_weave = self.from_weaves.get_weave(file_id,
169
 
                    self.from_repository.get_transaction())
170
 
                # we fetch all the texts, because texts do
171
 
                # not reference anything, and its cheap enough
172
 
                to_weave.join(from_weave, version_ids=required_versions) 
173
 
                # we don't need *all* of this data anymore, but we dont know
174
 
                # what we do. This cache clearing will result in a new read 
175
 
                # of the knit data when we do the checkout, but probably we
176
 
                # want to emit the needed data on the fly rather than at the
177
 
                # end anyhow.
178
 
                # the from weave should know not to cache data being joined,
179
 
                # but its ok to ask it to clear.
180
 
                from_weave.clear_cache()
181
 
                to_weave.clear_cache()
182
 
        finally:
183
 
            texts_pb.finished()
 
154
        file_ids = self.from_repository.fileid_involved_by_set(revs)
 
155
        count = 0
 
156
        num_file_ids = len(file_ids)
 
157
        for file_id in file_ids:
 
158
            self.pb.update("merge weaves", count, num_file_ids)
 
159
            count +=1
 
160
            to_weave = self.to_weaves.get_weave_or_empty(file_id,
 
161
                self.to_repository.get_transaction())
 
162
            from_weave = self.from_weaves.get_weave(file_id,
 
163
                self.from_repository.get_transaction())
 
164
 
 
165
            if to_weave.numversions() > 0:
 
166
                # destination has contents, must merge
 
167
                try:
 
168
                    to_weave.join(from_weave)
 
169
                except errors.WeaveParentMismatch:
 
170
                    to_weave.reweave(from_weave)
 
171
            else:
 
172
                # destination is empty, just replace it
 
173
                to_weave = from_weave.copy()
 
174
 
 
175
            self.to_weaves.put_weave(file_id, to_weave,
 
176
                self.to_repository.get_transaction())
 
177
        self.pb.clear()
184
178
 
185
179
    def _fetch_inventory_weave(self, revs):
186
 
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
187
 
        try:
188
 
            pb.update("fetch inventory", 0, 2)
189
 
            to_weave = self.to_control.get_weave('inventory',
190
 
                    self.to_repository.get_transaction())
191
 
    
192
 
            child_pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
180
        self.pb.update("inventory fetch", 0, 2)
 
181
        from_weave = self.from_repository.get_inventory_weave()
 
182
        to_weave = self.to_repository.get_inventory_weave()
 
183
        self.pb.update("inventory fetch", 1, 2)
 
184
        to_weave = self.to_control.get_weave('inventory',
 
185
                self.to_repository.get_transaction())
 
186
        self.pb.update("inventory fetch", 2, 2)
 
187
 
 
188
        if to_weave.numversions() > 0:
 
189
            # destination has contents, must merge
193
190
            try:
194
 
                # just merge, this is optimisable and its means we don't
195
 
                # copy unreferenced data such as not-needed inventories.
196
 
                pb.update("fetch inventory", 1, 3)
197
 
                from_weave = self.from_repository.get_inventory_weave()
198
 
                pb.update("fetch inventory", 2, 3)
199
 
                # we fetch only the referenced inventories because we do not
200
 
                # know for unselected inventories whether all their required
201
 
                # texts are present in the other repository - it could be
202
 
                # corrupt.
203
 
                to_weave.join(from_weave, pb=child_pb, msg='merge inventory',
204
 
                              version_ids=revs)
205
 
            finally:
206
 
                child_pb.finished()
207
 
        finally:
208
 
            pb.finished()
209
 
 
210
 
 
211
 
class GenericRepoFetcher(RepoFetcher):
212
 
    """This is a generic repo to repo fetcher.
213
 
 
214
 
    This makes minimal assumptions about repo layout and contents.
215
 
    It triggers a reconciliation after fetching to ensure integrity.
216
 
    """
217
 
 
218
 
    def _fetch_revision_texts(self, revs):
219
 
        """Fetch revision object texts"""
220
 
        rev_pb = bzrlib.ui.ui_factory.nested_progress_bar()
221
 
        try:
222
 
            to_txn = self.to_transaction = self.to_repository.get_transaction()
223
 
            count = 0
224
 
            total = len(revs)
225
 
            to_store = self.to_repository._revision_store
226
 
            for rev in revs:
227
 
                pb = bzrlib.ui.ui_factory.nested_progress_bar()
228
 
                try:
229
 
                    pb.update('copying revisions', count, total)
230
 
                    try:
231
 
                        sig_text = self.from_repository.get_signature_text(rev)
232
 
                        to_store.add_revision_signature_text(rev, sig_text, to_txn)
233
 
                    except errors.NoSuchRevision:
234
 
                        # not signed.
235
 
                        pass
236
 
                    to_store.add_revision(self.from_repository.get_revision(rev),
237
 
                                          to_txn)
238
 
                    count += 1
239
 
                finally:
240
 
                    pb.finished()
241
 
            # fixup inventory if needed: 
242
 
            # this is expensive because we have no inverse index to current ghosts.
243
 
            # but on local disk its a few seconds and sftp push is already insane.
244
 
            # so we just-do-it.
245
 
            # FIXME: repository should inform if this is needed.
246
 
            self.to_repository.reconcile()
247
 
        finally:
248
 
            rev_pb.finished()
249
 
    
250
 
 
251
 
class KnitRepoFetcher(RepoFetcher):
252
 
    """This is a knit format repository specific fetcher.
253
 
 
254
 
    This differs from the GenericRepoFetcher by not doing a 
255
 
    reconciliation after copying, and using knit joining to
256
 
    copy revision texts.
257
 
    """
258
 
 
259
 
    def _fetch_revision_texts(self, revs):
260
 
        # may need to be a InterRevisionStore call here.
261
 
        from_transaction = self.from_repository.get_transaction()
262
 
        to_transaction = self.to_repository.get_transaction()
263
 
        to_sf = self.to_repository._revision_store.get_signature_file(
264
 
            to_transaction)
265
 
        from_sf = self.from_repository._revision_store.get_signature_file(
266
 
            from_transaction)
267
 
        to_sf.join(from_sf, version_ids=revs, ignore_missing=True)
268
 
        to_rf = self.to_repository._revision_store.get_revision_file(
269
 
            to_transaction)
270
 
        from_rf = self.from_repository._revision_store.get_revision_file(
271
 
            from_transaction)
272
 
        to_rf.join(from_rf, version_ids=revs)
 
191
                to_weave.join(from_weave, pb=self.pb, msg='merge inventory')
 
192
            except errors.WeaveParentMismatch:
 
193
                to_weave.reweave(from_weave, pb=self.pb, msg='reweave inventory')
 
194
        else:
 
195
            # destination is empty, just replace it
 
196
            to_weave = from_weave.copy()
 
197
 
 
198
        self.to_control.put_weave('inventory', to_weave,
 
199
            self.to_repository.get_transaction())
 
200
 
 
201
        self.pb.clear()
273
202
 
274
203
 
275
204
class Fetcher(object):
276
 
    """Backwards compatibility glue for branch.fetch()."""
277
 
 
278
 
    @deprecated_method(zero_eight)
 
205
    """Pull revisions and texts from one branch to another.
 
206
 
 
207
    This doesn't update the destination's history; that can be done
 
208
    separately if desired.  
 
209
 
 
210
    revision_limit
 
211
        If set, pull only up to this revision_id.
 
212
 
 
213
    After running:
 
214
 
 
215
    last_revision -- if last_revision
 
216
        is given it will be that, otherwise the last revision of
 
217
        from_branch
 
218
 
 
219
    count_copied -- number of revisions copied
 
220
 
 
221
    count_weaves -- number of file weaves copied
 
222
    """
279
223
    def __init__(self, to_branch, from_branch, last_revision=None, pb=None):
280
 
        """Please see branch.fetch()."""
281
 
        to_branch.fetch(from_branch, last_revision, pb)
 
224
        if to_branch.base == from_branch.base:
 
225
            raise Exception("can't fetch from a branch to itself %s, %s" % 
 
226
                            (from_branch.base, to_branch.base))
 
227
        
 
228
        self.to_branch = to_branch
 
229
        self.from_branch = from_branch
 
230
        self._last_revision = last_revision
 
231
        if pb is None:
 
232
            self.pb = bzrlib.ui.ui_factory.progress_bar()
 
233
        else:
 
234
            self.pb = pb
 
235
        self.from_branch.lock_read()
 
236
        try:
 
237
            self.to_branch.lock_write()
 
238
            try:
 
239
                self.__fetch()
 
240
            finally:
 
241
                self.to_branch.unlock()
 
242
        finally:
 
243
            self.from_branch.unlock()
 
244
 
 
245
    def __fetch(self):
 
246
        self._find_last_revision()
 
247
        repo_fetcher = RepoFetcher(to_repository=self.to_branch.repository,
 
248
                                   from_repository=self.from_branch.repository,
 
249
                                   pb=self.pb,
 
250
                                   last_revision=self._last_revision)
 
251
        self.failed_revisions = repo_fetcher.failed_revisions
 
252
        self.count_copied = repo_fetcher.count_copied
 
253
        self.count_total = repo_fetcher.count_total
 
254
        self.count_weaves = repo_fetcher.count_weaves
 
255
        self.copied_file_ids = repo_fetcher.copied_file_ids
 
256
 
 
257
    def _find_last_revision(self):
 
258
        """Find the limiting source revision.
 
259
 
 
260
        Every ancestor of that revision will be merged across.
 
261
 
 
262
        Returns the revision_id, or returns None if there's no history
 
263
        in the source branch."""
 
264
        if self._last_revision:
 
265
            return
 
266
        self.pb.update('get source history')
 
267
        from_history = self.from_branch.revision_history()
 
268
        self.pb.update('get destination history')
 
269
        if from_history:
 
270
            self._last_revision = from_history[-1]
 
271
        else:
 
272
            # no history in the source branch
 
273
            self._last_revision = NULL_REVISION
 
274
 
 
275
fetch = Fetcher