~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/fetch.py

first cut at merge from integration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
88
88
    count_weaves -- number of file weaves copied
89
89
    """
90
90
    def __init__(self, to_branch, from_branch, last_revision=None, pb=None):
91
 
        if to_branch == from_branch:
92
 
            raise Exception("can't fetch from a branch to itself")
 
91
        if to_branch.base == from_branch.base:
 
92
            raise Exception("can't fetch from a branch to itself %s, %s" % 
 
93
                            (from_branch.base, to_branch.base))
 
94
        
93
95
        self.to_branch = to_branch
94
 
        self.to_weaves = to_branch.weave_store
95
 
        self.to_control = to_branch.control_weaves
96
96
        self.from_branch = from_branch
97
 
        self.from_weaves = from_branch.weave_store
98
 
        self.from_control = from_branch.control_weaves
 
97
        self._last_revision = last_revision
 
98
        if pb is None:
 
99
            self.pb = bzrlib.ui.ui_factory.progress_bar()
 
100
        else:
 
101
            self.pb = pb
 
102
        self.from_branch.lock_read()
 
103
        try:
 
104
            self.to_branch.lock_write()
 
105
            try:
 
106
                self.__fetch()
 
107
            finally:
 
108
                self.to_branch.unlock()
 
109
        finally:
 
110
            self.from_branch.unlock()
 
111
 
 
112
    def __fetch(self):
 
113
        """Primary worker function.
 
114
 
 
115
        This initialises all the needed variables, and then fetches the 
 
116
        requested revisions, finally clearing the progress bar.
 
117
        """
 
118
        self.to_repository = self.to_branch.repository
 
119
        self.to_weaves = self.to_repository.weave_store
 
120
        self.to_control = self.to_repository.control_weaves
 
121
        self.from_repository = self.from_branch.repository
 
122
        self.from_weaves = self.from_repository.weave_store
 
123
        self.from_control = self.from_repository.control_weaves
99
124
        self.failed_revisions = []
100
125
        self.count_copied = 0
101
126
        self.count_total = 0
102
127
        self.count_weaves = 0
103
128
        self.copied_file_ids = set()
104
129
        self.file_ids_names = {}
105
 
        if pb is None:
106
 
            self.pb = bzrlib.ui.ui_factory.progress_bar()
107
 
        else:
108
 
            self.pb = pb
109
 
        self.from_branch.lock_read()
110
130
        try:
111
 
            revs = self._revids_to_fetch(last_revision)
 
131
            revs = self._revids_to_fetch()
112
132
            # nothing to do
113
133
            if revs: 
114
134
                self._fetch_weave_texts(revs)
116
136
                self._fetch_revision_texts(revs)
117
137
                self.count_copied += len(revs)
118
138
        finally:
119
 
            self.from_branch.unlock()
120
139
            self.pb.clear()
121
140
 
122
 
    def _revids_to_fetch(self, last_revision):
123
 
        self.last_revision = self._find_last_revision(last_revision)
124
 
        mutter('fetch up to rev {%s}', self.last_revision)
125
 
        if (self.last_revision is not None and 
126
 
            self.to_branch.has_revision(self.last_revision)):
 
141
    def _revids_to_fetch(self):
 
142
        self._find_last_revision()
 
143
        mutter('fetch up to rev {%s}', self._last_revision)
 
144
        if (self._last_revision is not None and 
 
145
            self.to_repository.has_revision(self._last_revision)):
127
146
            return
128
147
        try:
129
 
            branch_from_revs = set(self.from_branch.get_ancestry(self.last_revision))
 
148
            branch_from_revs = set(self.from_repository.get_ancestry(self._last_revision))
130
149
        except WeaveError:
131
 
            raise InstallFailed([self.last_revision])
 
150
            raise InstallFailed([self._last_revision])
132
151
 
133
152
        self.dest_last_rev = self.to_branch.last_revision()
134
 
        branch_to_revs = set(self.to_branch.get_ancestry(self.dest_last_rev))
 
153
        branch_to_revs = set(self.to_repository.get_ancestry(self.dest_last_rev))
135
154
 
136
155
        return branch_from_revs.difference(branch_to_revs)
137
156
 
138
157
    def _fetch_revision_texts(self, revs):
139
 
        self.to_branch.revision_store.copy_multi(
140
 
            self.from_branch.revision_store, revs)
 
158
        self.to_repository.revision_store.copy_multi(
 
159
            self.from_repository.revision_store, revs)
141
160
 
142
161
    def _fetch_weave_texts(self, revs):
143
162
        file_ids = self.from_branch.fileid_involved_by_set(revs)
168
187
 
169
188
    def _fetch_inventory_weave(self, revs):
170
189
        self.pb.update("inventory merge", 0, 1)
171
 
 
172
 
        from_weave = self.from_control.get_weave('inventory',
173
 
                self.from_branch.get_transaction())
174
 
        to_weave = self.to_control.get_weave('inventory',
175
 
                self.to_branch.get_transaction())
 
190
        from_weave = self.from_repository.get_inventory_weave()
 
191
        to_weave = self.to_repository.get_inventory_weave()
176
192
 
177
193
        if to_weave.numversions() > 0:
178
194
            # destination has contents, must merge
189
205
 
190
206
        self.pb.clear()
191
207
 
192
 
    def _find_last_revision(self, last_revision):
 
208
    def _find_last_revision(self):
193
209
        """Find the limiting source revision.
194
210
 
195
211
        Every ancestor of that revision will be merged across.
196
212
 
197
213
        Returns the revision_id, or returns None if there's no history
198
214
        in the source branch."""
199
 
        if last_revision:
200
 
            return last_revision
 
215
        if self._last_revision:
 
216
            return
201
217
        self.pb.update('get source history')
202
218
        from_history = self.from_branch.revision_history()
203
219
        self.pb.update('get destination history')
204
220
        if from_history:
205
 
            return from_history[-1]
 
221
            self._last_revision = from_history[-1]
206
222
        else:
207
 
            return None                 # no history in the source branch
 
223
            # no history in the source branch
 
224
            self._last_revision = None
208
225
 
209
226
fetch = Fetcher