/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

  • Committer: Robert Collins
  • Date: 2009-05-23 20:57:12 UTC
  • mfrom: (4371 +trunk)
  • mto: This revision was merged to the branch mainline in revision 4441.
  • Revision ID: robertc@robertcollins.net-20090523205712-lcwbfqk6vwavinuv
MergeĀ .dev.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2008 Canonical Ltd
 
1
# Copyright (C) 2005, 2006, 2008, 2009 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
"""Copying of history from one branch to another.
21
21
that has merged into it.  As the first step of a merge, pull, or
22
22
branch operation we copy history from the source into the destination
23
23
branch.
24
 
 
25
 
The copying is done in a slightly complicated order.  We don't want to
26
 
add a revision to the store until everything it refers to is also
27
 
stored, so that if a revision is present we can totally recreate it.
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 then return to the inventories.
31
24
"""
32
25
 
33
26
import operator
34
27
 
35
28
import bzrlib
36
 
import bzrlib.errors as errors
37
 
from bzrlib.errors import InstallFailed
38
 
from bzrlib.progress import ProgressPhase
 
29
from bzrlib import (
 
30
    errors,
 
31
    symbol_versioning,
 
32
    )
39
33
from bzrlib.revision import NULL_REVISION
40
34
from bzrlib.tsort import topo_sort
41
35
from bzrlib.trace import mutter
42
36
import bzrlib.ui
43
37
from bzrlib.versionedfile import FulltextContentFactory
44
38
 
45
 
# TODO: Avoid repeatedly opening weaves so many times.
46
 
 
47
 
# XXX: This doesn't handle ghost (not present in branch) revisions at
48
 
# all yet.  I'm not sure they really should be supported.
49
 
 
50
 
# NOTE: This doesn't copy revisions which may be present but not
51
 
# merged into the last revision.  I'm not sure we want to do that.
52
 
 
53
 
# - get a list of revisions that need to be pulled in
54
 
# - for each one, pull in that revision file
55
 
#   and get the inventory, and store the inventory with right
56
 
#   parents.
57
 
# - and get the ancestry, and store that with right parents too
58
 
# - and keep a note of all file ids and version seen
59
 
# - then go through all files; for each one get the weave,
60
 
#   and add in all file versions
61
 
 
62
39
 
63
40
class RepoFetcher(object):
64
41
    """Pull revisions and texts from one repository to another.
65
42
 
66
 
    last_revision
67
 
        if set, try to limit to the data this revision references.
68
 
 
69
43
    This should not be used directly, it's essential a object to encapsulate
70
44
    the logic in InterRepository.fetch().
71
45
    """
74
48
        pb=None, find_ghosts=True, fetch_spec=None):
75
49
        """Create a repo fetcher.
76
50
 
 
51
        :param last_revision: If set, try to limit to the data this revision
 
52
            references.
77
53
        :param find_ghosts: If True search the entire history for ghosts.
78
54
        :param _write_group_acquired_callable: Don't use; this parameter only
79
55
            exists to facilitate a hack done in InterPackRepo.fetch.  We would
80
56
            like to remove this parameter.
 
57
        :param pb: ProgressBar object to use; deprecated and ignored.
 
58
            This method will just create one on top of the stack.
81
59
        """
 
60
        if pb is not None:
 
61
            symbol_versioning.warn(
 
62
                symbol_versioning.deprecated_in((1, 14, 0))
 
63
                % "pb parameter to RepoFetcher.__init__")
 
64
            # and for simplicity it is in fact ignored
82
65
        if to_repository.has_same_location(from_repository):
83
66
            # repository.fetch should be taking care of this case.
84
67
            raise errors.BzrError('RepoFetcher run '
91
74
        self._last_revision = last_revision
92
75
        self._fetch_spec = fetch_spec
93
76
        self.find_ghosts = find_ghosts
94
 
        if pb is None:
95
 
            self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
96
 
            self.nested_pb = self.pb
97
 
        else:
98
 
            self.pb = pb
99
 
            self.nested_pb = None
100
77
        self.from_repository.lock_read()
 
78
        mutter("Using fetch logic to copy between %s(%s) and %s(%s)",
 
79
               self.from_repository, self.from_repository._format,
 
80
               self.to_repository, self.to_repository._format)
101
81
        try:
102
 
            try:
103
 
                self.__fetch()
104
 
            finally:
105
 
                if self.nested_pb is not None:
106
 
                    self.nested_pb.finished()
 
82
            self.__fetch()
107
83
        finally:
108
84
            self.from_repository.unlock()
109
85
 
121
97
        # assert not missing
122
98
        self.count_total = 0
123
99
        self.file_ids_names = {}
124
 
        pp = ProgressPhase('Transferring', 4, self.pb)
 
100
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
101
        pb.show_pct = pb.show_count = False
125
102
        try:
126
 
            pp.next_phase()
 
103
            pb.update("Finding revisions", 0, 2)
127
104
            search = self._revids_to_fetch()
128
105
            if search is None:
129
106
                return
130
 
            self._fetch_everything_for_search(search, pp)
 
107
            pb.update("Fetching revisions", 1, 2)
 
108
            self._fetch_everything_for_search(search)
131
109
        finally:
132
 
            self.pb.clear()
 
110
            pb.finished()
133
111
 
134
 
    def _fetch_everything_for_search(self, search, pp):
 
112
    def _fetch_everything_for_search(self, search):
135
113
        """Fetch all data for the given set of revisions."""
136
114
        # The first phase is "file".  We pass the progress bar for it directly
137
115
        # into item_keys_introduced_by, which has more information about how
146
124
            raise errors.IncompatibleRepositories(
147
125
                self.from_repository, self.to_repository,
148
126
                "different rich-root support")
149
 
        self.pb = bzrlib.ui.ui_factory.nested_progress_bar()
 
127
        pb = bzrlib.ui.ui_factory.nested_progress_bar()
150
128
        try:
 
129
            pb.update("Get stream source")
151
130
            source = self.from_repository._get_source(
152
131
                self.to_repository._format)
153
132
            stream = source.get_stream(search)
154
133
            from_format = self.from_repository._format
 
134
            pb.update("Inserting stream")
155
135
            resume_tokens, missing_keys = self.sink.insert_stream(
156
136
                stream, from_format, [])
 
137
            if self.to_repository._fallback_repositories:
 
138
                missing_keys.update(
 
139
                    self._parent_inventories(search.get_keys()))
157
140
            if missing_keys:
 
141
                pb.update("Missing keys")
158
142
                stream = source.get_stream_for_missing_keys(missing_keys)
 
143
                pb.update("Inserting missing keys")
159
144
                resume_tokens, missing_keys = self.sink.insert_stream(
160
145
                    stream, from_format, resume_tokens)
161
146
            if missing_keys:
166
151
                raise AssertionError(
167
152
                    "second push failed to commit the fetch %r." % (
168
153
                        resume_tokens,))
 
154
            pb.update("Finishing stream")
169
155
            self.sink.finished()
170
156
        finally:
171
 
            if self.pb is not None:
172
 
                self.pb.finished()
 
157
            pb.finished()
173
158
 
174
159
    def _revids_to_fetch(self):
175
160
        """Determines the exact revisions needed from self.from_repository to
183
168
        if self._last_revision is NULL_REVISION:
184
169
            # explicit limit of no revisions needed
185
170
            return None
186
 
        if (self._last_revision is not None and
187
 
            self.to_repository.has_revision(self._last_revision)):
188
 
            return None
189
 
        try:
190
 
            return self.to_repository.search_missing_revision_ids(
191
 
                self.from_repository, self._last_revision,
192
 
                find_ghosts=self.find_ghosts)
193
 
        except errors.NoSuchRevision, e:
194
 
            raise InstallFailed([self._last_revision])
 
171
        return self.to_repository.search_missing_revision_ids(
 
172
            self.from_repository, self._last_revision,
 
173
            find_ghosts=self.find_ghosts)
 
174
 
 
175
    def _parent_inventories(self, revision_ids):
 
176
        # Find all the parent revisions referenced by the stream, but
 
177
        # not present in the stream, and make sure we send their
 
178
        # inventories.
 
179
        parent_maps = self.to_repository.get_parent_map(revision_ids)
 
180
        parents = set()
 
181
        map(parents.update, parent_maps.itervalues())
 
182
        parents.discard(NULL_REVISION)
 
183
        parents.difference_update(revision_ids)
 
184
        missing_keys = set(('inventories', rev_id) for rev_id in parents)
 
185
        return missing_keys
195
186
 
196
187
 
197
188
class Inter1and2Helper(object):