30
29
from bzrlib import (
33
revision as _mod_revision,
36
34
from bzrlib.trace import mutter
37
35
from bzrlib.tsort import topo_sort
38
36
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
39
from bzrlib.i18n import gettext
42
39
def reconcile(dir, canonicalize_chks=False):
67
64
"""Perform reconciliation.
69
66
After reconciliation the following attributes document found issues:
71
* `inconsistent_parents`: The number of revisions in the repository
72
whose ancestry was being reported incorrectly.
73
* `garbage_inventories`: The number of inventory objects without
74
revisions that were garbage collected.
75
* `fixed_branch_history`: None if there was no branch, False if the
76
branch history was correct, True if the branch history needed to be
67
inconsistent_parents: The number of revisions in the repository whose
68
ancestry was being reported incorrectly.
69
garbage_inventories: The number of inventory objects without revisions
70
that were garbage collected.
71
fixed_branch_history: None if there was no branch, False if the branch
72
history was correct, True if the branch history
73
needed to be re-normalized.
79
75
self.pb = ui.ui_factory.nested_progress_bar()
94
90
# Nothing to check here
95
91
self.fixed_branch_history = None
97
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
93
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
98
94
branch_reconciler = self.branch.reconcile(thorough=True)
99
95
self.fixed_branch_history = branch_reconciler.fixed_history
101
97
def _reconcile_repository(self):
102
98
self.repo = self.bzrdir.find_repository()
103
ui.ui_factory.note(gettext('Reconciling repository %s') %
99
ui.ui_factory.note('Reconciling repository %s' %
104
100
self.repo.user_url)
105
self.pb.update(gettext("Reconciling repository"), 0, 1)
101
self.pb.update("Reconciling repository", 0, 1)
106
102
if self.canonicalize_chks:
108
104
self.repo.reconcile_canonicalize_chks
109
105
except AttributeError:
110
106
raise errors.BzrError(
111
gettext("%s cannot canonicalize CHKs.") % (self.repo,))
107
"%s cannot canonicalize CHKs." % (self.repo,))
112
108
repo_reconciler = self.repo.reconcile_canonicalize_chks()
114
110
repo_reconciler = self.repo.reconcile(thorough=True)
115
111
self.inconsistent_parents = repo_reconciler.inconsistent_parents
116
112
self.garbage_inventories = repo_reconciler.garbage_inventories
117
113
if repo_reconciler.aborted:
118
ui.ui_factory.note(gettext(
119
'Reconcile aborted: revision index has inconsistent parents.'))
120
ui.ui_factory.note(gettext(
121
'Run "bzr check" for more details.'))
115
'Reconcile aborted: revision index has inconsistent parents.')
117
'Run "bzr check" for more details.')
123
ui.ui_factory.note(gettext('Reconciliation complete.'))
119
ui.ui_factory.note('Reconciliation complete.')
126
122
class BranchReconciler(object):
147
143
self._reconcile_revision_history()
149
145
def _reconcile_revision_history(self):
146
repo = self.branch.repository
150
147
last_revno, last_revision_id = self.branch.last_revision_info()
151
148
real_history = []
152
graph = self.branch.repository.get_graph()
154
for revid in graph.iter_lefthand_ancestry(
155
last_revision_id, (_mod_revision.NULL_REVISION,)):
150
for revid in repo.iter_reverse_revision_history(
156
152
real_history.append(revid)
157
153
except errors.RevisionNotPresent:
158
154
pass # Hit a ghost left hand parent
163
159
# set_revision_history, as this will regenerate it again.
164
160
# Not really worth a whole BranchReconciler class just for this,
166
ui.ui_factory.note(gettext('Fixing last revision info {0} '\
168
last_revno, len(real_history)))
162
ui.ui_factory.note('Fixing last revision info %s => %s' % (
163
last_revno, len(real_history)))
169
164
self.branch.set_last_revision_info(len(real_history),
170
165
last_revision_id)
172
167
self.fixed_history = False
173
ui.ui_factory.note(gettext('revision_history ok.'))
168
ui.ui_factory.note('revision_history ok.')
176
171
class RepoReconciler(object):
201
196
"""Perform reconciliation.
203
198
After reconciliation the following attributes document found issues:
205
* `inconsistent_parents`: The number of revisions in the repository
206
whose ancestry was being reported incorrectly.
207
* `garbage_inventories`: The number of inventory objects without
208
revisions that were garbage collected.
199
inconsistent_parents: The number of revisions in the repository whose
200
ancestry was being reported incorrectly.
201
garbage_inventories: The number of inventory objects without revisions
202
that were garbage collected.
210
204
operation = cleanup.OperationWithCleanups(self._reconcile)
211
205
self.add_cleanup = operation.add_cleanup
230
224
only data-loss causing issues (!self.thorough) or all issues
231
225
(self.thorough) are treated as requiring the reweave.
227
# local because needing to know about WeaveFile is a wart we want to hide
228
from bzrlib.weave import WeaveFile, Weave
233
229
transaction = self.repo.get_transaction()
234
self.pb.update(gettext('Reading inventory data'))
230
self.pb.update('Reading inventory data')
235
231
self.inventory = self.repo.inventories
236
232
self.revisions = self.repo.revisions
237
233
# the total set of revisions to process
251
247
# (no garbage inventories or we are not doing a thorough check)
252
248
if (not self.inconsistent_parents and
253
249
(not self.garbage_inventories or not self.thorough)):
254
ui.ui_factory.note(gettext('Inventory ok.'))
250
ui.ui_factory.note('Inventory ok.')
256
self.pb.update(gettext('Backing up inventory'), 0, 0)
252
self.pb.update('Backing up inventory', 0, 0)
257
253
self.repo._backup_inventory()
258
ui.ui_factory.note(gettext('Backup inventory created.'))
254
ui.ui_factory.note('Backup inventory created.')
259
255
new_inventories = self.repo._temp_inventories()
261
257
# we have topological order of revisions and non ghost parents ready.
271
267
if not (set(new_inventories.keys()) ==
272
268
set([(revid,) for revid in self.pending])):
273
269
raise AssertionError()
274
self.pb.update(gettext('Writing weave'))
270
self.pb.update('Writing weave')
275
271
self.repo._activate_new_inventory()
276
272
self.inventory = None
277
ui.ui_factory.note(gettext('Inventory regenerated.'))
273
ui.ui_factory.note('Inventory regenerated.')
279
275
def _new_inv_parents(self, revision_key):
280
276
"""Lookup ghost-filtered parents for revision_key."""
368
364
def _load_indexes(self):
369
365
"""Load indexes for the reconciliation."""
370
366
self.transaction = self.repo.get_transaction()
371
self.pb.update(gettext('Reading indexes'), 0, 2)
367
self.pb.update('Reading indexes', 0, 2)
372
368
self.inventory = self.repo.inventories
373
self.pb.update(gettext('Reading indexes'), 1, 2)
369
self.pb.update('Reading indexes', 1, 2)
374
370
self.repo._check_for_inconsistent_revision_parents()
375
371
self.revisions = self.repo.revisions
376
self.pb.update(gettext('Reading indexes'), 2, 2)
372
self.pb.update('Reading indexes', 2, 2)
378
374
def _gc_inventory(self):
379
375
"""Remove inventories that are not referenced from the revision store."""
380
self.pb.update(gettext('Checking unused inventories'), 0, 1)
376
self.pb.update('Checking unused inventories', 0, 1)
381
377
self._check_garbage_inventories()
382
self.pb.update(gettext('Checking unused inventories'), 1, 3)
378
self.pb.update('Checking unused inventories', 1, 3)
383
379
if not self.garbage_inventories:
384
ui.ui_factory.note(gettext('Inventory ok.'))
380
ui.ui_factory.note('Inventory ok.')
386
self.pb.update(gettext('Backing up inventory'), 0, 0)
382
self.pb.update('Backing up inventory', 0, 0)
387
383
self.repo._backup_inventory()
388
ui.ui_factory.note(gettext('Backup Inventory created'))
384
ui.ui_factory.note('Backup Inventory created')
389
385
# asking for '' should never return a non-empty weave
390
386
new_inventories = self.repo._temp_inventories()
391
387
# we have topological order of revisions and non ghost parents ready.
402
398
# the revisionds list
403
399
if not(set(new_inventories.keys()) == set(revision_keys)):
404
400
raise AssertionError()
405
self.pb.update(gettext('Writing weave'))
401
self.pb.update('Writing weave')
406
402
self.repo._activate_new_inventory()
407
403
self.inventory = None
408
ui.ui_factory.note(gettext('Inventory regenerated.'))
404
ui.ui_factory.note('Inventory regenerated.')
410
406
def _fix_text_parents(self):
411
407
"""Fix bad versionedfile parent entries.
443
439
versions_list.append(text_key[1])
444
440
# Do the reconcile of individual weaves.
445
441
for num, file_id in enumerate(per_id_bad_parents):
446
self.pb.update(gettext('Fixing text parents'), num,
442
self.pb.update('Fixing text parents', num,
447
443
len(per_id_bad_parents))
448
444
versions_with_bad_parents = per_id_bad_parents[file_id]
449
445
id_unused_versions = set(key[-1] for key in unused_versions