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
17
17
"""Reconcilers are able to fix some potential data errors in a branch."""
29
29
from bzrlib import (
35
from bzrlib.trace import mutter, note
36
from bzrlib.tsort import TopoSorter
35
from bzrlib.trace import mutter
36
from bzrlib.tsort import topo_sort
37
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
90
90
# Nothing to check here
91
91
self.fixed_branch_history = None
93
self.pb.note('Reconciling branch %s',
93
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
95
94
branch_reconciler = self.branch.reconcile(thorough=True)
96
95
self.fixed_branch_history = branch_reconciler.fixed_history
98
97
def _reconcile_repository(self):
99
98
self.repo = self.bzrdir.find_repository()
100
self.pb.note('Reconciling repository %s',
101
self.repo.bzrdir.root_transport.base)
99
ui.ui_factory.note('Reconciling repository %s' %
100
self.repo.bzrdir.root_transport.base)
102
101
self.pb.update("Reconciling repository", 0, 1)
103
102
repo_reconciler = self.repo.reconcile(thorough=True)
104
103
self.inconsistent_parents = repo_reconciler.inconsistent_parents
105
104
self.garbage_inventories = repo_reconciler.garbage_inventories
106
105
if repo_reconciler.aborted:
108
107
'Reconcile aborted: revision index has inconsistent parents.')
110
109
'Run "bzr check" for more details.')
112
self.pb.note('Reconciliation complete.')
111
ui.ui_factory.note('Reconciliation complete.')
115
114
class BranchReconciler(object):
121
120
self.branch = a_branch
123
122
def reconcile(self):
123
operation = cleanup.OperationWithCleanups(self._reconcile)
124
self.add_cleanup = operation.add_cleanup
125
operation.run_simple()
127
def _reconcile(self):
124
128
self.branch.lock_write()
126
self.pb = ui.ui_factory.nested_progress_bar()
128
self._reconcile_steps()
129
self.add_cleanup(self.branch.unlock)
130
self.pb = ui.ui_factory.nested_progress_bar()
131
self.add_cleanup(self.pb.finished)
132
self._reconcile_steps()
134
134
def _reconcile_steps(self):
135
135
self._reconcile_revision_history()
137
137
def _reconcile_revision_history(self):
138
138
repo = self.branch.repository
139
139
last_revno, last_revision_id = self.branch.last_revision_info()
140
real_history = list(repo.iter_reverse_revision_history(
142
for revid in repo.iter_reverse_revision_history(
144
real_history.append(revid)
145
except errors.RevisionNotPresent:
146
pass # Hit a ghost left hand parent
142
147
real_history.reverse()
143
148
if last_revno != len(real_history):
144
149
self.fixed_history = True
146
151
# set_revision_history, as this will regenerate it again.
147
152
# Not really worth a whole BranchReconciler class just for this,
149
self.pb.note('Fixing last revision info %s => %s',
150
last_revno, len(real_history))
154
ui.ui_factory.note('Fixing last revision info %s => %s' % (
155
last_revno, len(real_history)))
151
156
self.branch.set_last_revision_info(len(real_history),
152
157
last_revision_id)
154
159
self.fixed_history = False
155
self.pb.note('revision_history ok.')
160
ui.ui_factory.note('revision_history ok.')
158
163
class RepoReconciler(object):
159
164
"""Reconciler that reconciles a repository.
161
166
The goal of repository reconciliation is to make any derived data
162
consistent with the core data committed by a user. This can involve
167
consistent with the core data committed by a user. This can involve
163
168
reindexing, or removing unreferenced data if that can interfere with
164
169
queries in a given repository.
182
187
def reconcile(self):
183
188
"""Perform reconciliation.
185
190
After reconciliation the following attributes document found issues:
186
191
inconsistent_parents: The number of revisions in the repository whose
187
192
ancestry was being reported incorrectly.
188
193
garbage_inventories: The number of inventory objects without revisions
189
194
that were garbage collected.
196
operation = cleanup.OperationWithCleanups(self._reconcile)
197
self.add_cleanup = operation.add_cleanup
198
operation.run_simple()
200
def _reconcile(self):
191
201
self.repo.lock_write()
193
self.pb = ui.ui_factory.nested_progress_bar()
195
self._reconcile_steps()
202
self.add_cleanup(self.repo.unlock)
203
self.pb = ui.ui_factory.nested_progress_bar()
204
self.add_cleanup(self.pb.finished)
205
self._reconcile_steps()
201
207
def _reconcile_steps(self):
202
208
"""Perform the steps to reconcile this repository."""
205
211
def _reweave_inventory(self):
206
212
"""Regenerate the inventory weave for the repository from scratch.
208
This is a smart function: it will only do the reweave if doing it
214
This is a smart function: it will only do the reweave if doing it
209
215
will correct data issues. The self.thorough flag controls whether
210
216
only data-loss causing issues (!self.thorough) or all issues
211
217
(self.thorough) are treated as requiring the reweave.
213
219
# local because needing to know about WeaveFile is a wart we want to hide
214
220
from bzrlib.weave import WeaveFile, Weave
215
221
transaction = self.repo.get_transaction()
216
self.pb.update('Reading inventory data.')
222
self.pb.update('Reading inventory data')
217
223
self.inventory = self.repo.inventories
218
224
self.revisions = self.repo.revisions
219
225
# the total set of revisions to process
229
235
# put a revision into the graph.
230
236
self._graph_revision(rev_id)
231
237
self._check_garbage_inventories()
232
# if there are no inconsistent_parents and
238
# if there are no inconsistent_parents and
233
239
# (no garbage inventories or we are not doing a thorough check)
234
if (not self.inconsistent_parents and
240
if (not self.inconsistent_parents and
235
241
(not self.garbage_inventories or not self.thorough)):
236
self.pb.note('Inventory ok.')
242
ui.ui_factory.note('Inventory ok.')
238
self.pb.update('Backing up inventory...', 0, 0)
244
self.pb.update('Backing up inventory', 0, 0)
239
245
self.repo._backup_inventory()
240
self.pb.note('Backup Inventory created.')
246
ui.ui_factory.note('Backup inventory created.')
241
247
new_inventories = self.repo._temp_inventories()
243
249
# we have topological order of revisions and non ghost parents ready.
244
250
self._setup_steps(len(self._rev_graph))
245
revision_keys = [(rev_id,) for rev_id in
246
TopoSorter(self._rev_graph.items()).iter_topo_order()]
251
revision_keys = [(rev_id,) for rev_id in topo_sort(self._rev_graph)]
247
252
stream = self._change_inv_parents(
248
253
self.inventory.get_record_stream(revision_keys, 'unordered', True),
249
254
self._new_inv_parents,
257
262
self.pb.update('Writing weave')
258
263
self.repo._activate_new_inventory()
259
264
self.inventory = None
260
self.pb.note('Inventory regenerated.')
265
ui.ui_factory.note('Inventory regenerated.')
262
267
def _new_inv_parents(self, revision_key):
263
268
"""Lookup ghost-filtered parents for revision_key."""
351
356
def _load_indexes(self):
352
357
"""Load indexes for the reconciliation."""
353
358
self.transaction = self.repo.get_transaction()
354
self.pb.update('Reading indexes.', 0, 2)
359
self.pb.update('Reading indexes', 0, 2)
355
360
self.inventory = self.repo.inventories
356
self.pb.update('Reading indexes.', 1, 2)
361
self.pb.update('Reading indexes', 1, 2)
357
362
self.repo._check_for_inconsistent_revision_parents()
358
363
self.revisions = self.repo.revisions
359
self.pb.update('Reading indexes.', 2, 2)
364
self.pb.update('Reading indexes', 2, 2)
361
366
def _gc_inventory(self):
362
367
"""Remove inventories that are not referenced from the revision store."""
363
self.pb.update('Checking unused inventories.', 0, 1)
368
self.pb.update('Checking unused inventories', 0, 1)
364
369
self._check_garbage_inventories()
365
self.pb.update('Checking unused inventories.', 1, 3)
370
self.pb.update('Checking unused inventories', 1, 3)
366
371
if not self.garbage_inventories:
367
self.pb.note('Inventory ok.')
372
ui.ui_factory.note('Inventory ok.')
369
self.pb.update('Backing up inventory...', 0, 0)
374
self.pb.update('Backing up inventory', 0, 0)
370
375
self.repo._backup_inventory()
371
self.pb.note('Backup Inventory created.')
376
ui.ui_factory.note('Backup Inventory created')
372
377
# asking for '' should never return a non-empty weave
373
378
new_inventories = self.repo._temp_inventories()
374
379
# we have topological order of revisions and non ghost parents ready.
375
380
graph = self.revisions.get_parent_map(self.revisions.keys())
376
revision_keys = list(TopoSorter(graph).iter_topo_order())
381
revision_keys = topo_sort(graph)
377
382
revision_ids = [key[-1] for key in revision_keys]
378
383
self._setup_steps(len(revision_keys))
379
384
stream = self._change_inv_parents(
388
393
self.pb.update('Writing weave')
389
394
self.repo._activate_new_inventory()
390
395
self.inventory = None
391
self.pb.note('Inventory regenerated.')
396
ui.ui_factory.note('Inventory regenerated.')
393
398
def _fix_text_parents(self):
394
399
"""Fix bad versionedfile parent entries.
499
504
collection = self.repo._pack_collection
500
505
collection.ensure_loaded()
501
506
collection.lock_names()
503
packs = collection.all_packs()
504
all_revisions = self.repo.all_revision_ids()
505
total_inventories = len(list(
506
collection.inventory_index.combined_index.iter_all_entries()))
507
if len(all_revisions):
508
self._packer = repofmt.pack_repo.ReconcilePacker(
509
collection, packs, ".reconcile", all_revisions)
510
new_pack = self._packer.pack(pb=self.pb)
511
if new_pack is not None:
512
self._discard_and_save(packs)
514
# only make a new pack when there is data to copy.
507
self.add_cleanup(collection._unlock_names)
508
packs = collection.all_packs()
509
all_revisions = self.repo.all_revision_ids()
510
total_inventories = len(list(
511
collection.inventory_index.combined_index.iter_all_entries()))
512
if len(all_revisions):
513
new_pack = self.repo._reconcile_pack(collection, packs,
514
".reconcile", all_revisions, self.pb)
515
if new_pack is not None:
515
516
self._discard_and_save(packs)
516
self.garbage_inventories = total_inventories - len(list(
517
collection.inventory_index.combined_index.iter_all_entries()))
519
collection._unlock_names()
518
# only make a new pack when there is data to copy.
519
self._discard_and_save(packs)
520
self.garbage_inventories = total_inventories - len(list(
521
collection.inventory_index.combined_index.iter_all_entries()))
521
523
def _discard_and_save(self, packs):
522
524
"""Discard some packs from the repository.