29
29
from bzrlib import (
32
revision as _mod_revision,
35
34
from bzrlib.trace import mutter
36
35
from bzrlib.tsort import topo_sort
37
36
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
38
from bzrlib.i18n import gettext
41
def reconcile(dir, canonicalize_chks=False):
39
def reconcile(dir, other=None):
42
40
"""Reconcile the data in dir.
44
42
Currently this is limited to a inventory 'reweave'.
48
46
Directly using Reconciler is recommended for library users that
49
47
desire fine grained control or analysis of the found issues.
51
:param canonicalize_chks: Make sure CHKs are in canonical form.
49
:param other: another bzrdir to reconcile against.
53
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
51
reconciler = Reconciler(dir, other=other)
54
52
reconciler.reconcile()
57
55
class Reconciler(object):
58
56
"""Reconcilers are used to reconcile existing data."""
60
def __init__(self, dir, other=None, canonicalize_chks=False):
58
def __init__(self, dir, other=None):
61
59
"""Create a Reconciler."""
63
self.canonicalize_chks = canonicalize_chks
65
62
def reconcile(self):
66
63
"""Perform reconciliation.
68
65
After reconciliation the following attributes document found issues:
70
* `inconsistent_parents`: The number of revisions in the repository
71
whose ancestry was being reported incorrectly.
72
* `garbage_inventories`: The number of inventory objects without
73
revisions that were garbage collected.
74
* `fixed_branch_history`: None if there was no branch, False if the
75
branch history was correct, True if the branch history needed to be
66
inconsistent_parents: The number of revisions in the repository whose
67
ancestry was being reported incorrectly.
68
garbage_inventories: The number of inventory objects without revisions
69
that were garbage collected.
70
fixed_branch_history: None if there was no branch, False if the branch
71
history was correct, True if the branch history
72
needed to be re-normalized.
78
74
self.pb = ui.ui_factory.nested_progress_bar()
93
89
# Nothing to check here
94
90
self.fixed_branch_history = None
96
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
92
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
97
93
branch_reconciler = self.branch.reconcile(thorough=True)
98
94
self.fixed_branch_history = branch_reconciler.fixed_history
100
96
def _reconcile_repository(self):
101
97
self.repo = self.bzrdir.find_repository()
102
ui.ui_factory.note(gettext('Reconciling repository %s') %
98
ui.ui_factory.note('Reconciling repository %s' %
103
99
self.repo.user_url)
104
self.pb.update(gettext("Reconciling repository"), 0, 1)
105
if self.canonicalize_chks:
107
self.repo.reconcile_canonicalize_chks
108
except AttributeError:
109
raise errors.BzrError(
110
gettext("%s cannot canonicalize CHKs.") % (self.repo,))
111
repo_reconciler = self.repo.reconcile_canonicalize_chks()
113
repo_reconciler = self.repo.reconcile(thorough=True)
100
self.pb.update("Reconciling repository", 0, 1)
101
repo_reconciler = self.repo.reconcile(thorough=True)
114
102
self.inconsistent_parents = repo_reconciler.inconsistent_parents
115
103
self.garbage_inventories = repo_reconciler.garbage_inventories
116
104
if repo_reconciler.aborted:
117
ui.ui_factory.note(gettext(
118
'Reconcile aborted: revision index has inconsistent parents.'))
119
ui.ui_factory.note(gettext(
120
'Run "bzr check" for more details.'))
106
'Reconcile aborted: revision index has inconsistent parents.')
108
'Run "bzr check" for more details.')
122
ui.ui_factory.note(gettext('Reconciliation complete.'))
110
ui.ui_factory.note('Reconciliation complete.')
125
113
class BranchReconciler(object):
146
134
self._reconcile_revision_history()
148
136
def _reconcile_revision_history(self):
137
repo = self.branch.repository
149
138
last_revno, last_revision_id = self.branch.last_revision_info()
150
139
real_history = []
151
graph = self.branch.repository.get_graph()
153
for revid in graph.iter_lefthand_ancestry(
154
last_revision_id, (_mod_revision.NULL_REVISION,)):
141
for revid in repo.iter_reverse_revision_history(
155
143
real_history.append(revid)
156
144
except errors.RevisionNotPresent:
157
145
pass # Hit a ghost left hand parent
162
150
# set_revision_history, as this will regenerate it again.
163
151
# Not really worth a whole BranchReconciler class just for this,
165
ui.ui_factory.note(gettext('Fixing last revision info {0} '\
167
last_revno, len(real_history)))
153
ui.ui_factory.note('Fixing last revision info %s => %s' % (
154
last_revno, len(real_history)))
168
155
self.branch.set_last_revision_info(len(real_history),
169
156
last_revision_id)
171
158
self.fixed_history = False
172
ui.ui_factory.note(gettext('revision_history ok.'))
159
ui.ui_factory.note('revision_history ok.')
175
162
class RepoReconciler(object):
200
187
"""Perform reconciliation.
202
189
After reconciliation the following attributes document found issues:
204
* `inconsistent_parents`: The number of revisions in the repository
205
whose ancestry was being reported incorrectly.
206
* `garbage_inventories`: The number of inventory objects without
207
revisions that were garbage collected.
190
inconsistent_parents: The number of revisions in the repository whose
191
ancestry was being reported incorrectly.
192
garbage_inventories: The number of inventory objects without revisions
193
that were garbage collected.
209
195
operation = cleanup.OperationWithCleanups(self._reconcile)
210
196
self.add_cleanup = operation.add_cleanup
229
215
only data-loss causing issues (!self.thorough) or all issues
230
216
(self.thorough) are treated as requiring the reweave.
218
# local because needing to know about WeaveFile is a wart we want to hide
219
from bzrlib.weave import WeaveFile, Weave
232
220
transaction = self.repo.get_transaction()
233
self.pb.update(gettext('Reading inventory data'))
221
self.pb.update('Reading inventory data')
234
222
self.inventory = self.repo.inventories
235
223
self.revisions = self.repo.revisions
236
224
# the total set of revisions to process
250
238
# (no garbage inventories or we are not doing a thorough check)
251
239
if (not self.inconsistent_parents and
252
240
(not self.garbage_inventories or not self.thorough)):
253
ui.ui_factory.note(gettext('Inventory ok.'))
241
ui.ui_factory.note('Inventory ok.')
255
self.pb.update(gettext('Backing up inventory'), 0, 0)
243
self.pb.update('Backing up inventory', 0, 0)
256
244
self.repo._backup_inventory()
257
ui.ui_factory.note(gettext('Backup inventory created.'))
245
ui.ui_factory.note('Backup inventory created.')
258
246
new_inventories = self.repo._temp_inventories()
260
248
# we have topological order of revisions and non ghost parents ready.
270
258
if not (set(new_inventories.keys()) ==
271
259
set([(revid,) for revid in self.pending])):
272
260
raise AssertionError()
273
self.pb.update(gettext('Writing weave'))
261
self.pb.update('Writing weave')
274
262
self.repo._activate_new_inventory()
275
263
self.inventory = None
276
ui.ui_factory.note(gettext('Inventory regenerated.'))
264
ui.ui_factory.note('Inventory regenerated.')
278
266
def _new_inv_parents(self, revision_key):
279
267
"""Lookup ghost-filtered parents for revision_key."""
367
355
def _load_indexes(self):
368
356
"""Load indexes for the reconciliation."""
369
357
self.transaction = self.repo.get_transaction()
370
self.pb.update(gettext('Reading indexes'), 0, 2)
358
self.pb.update('Reading indexes', 0, 2)
371
359
self.inventory = self.repo.inventories
372
self.pb.update(gettext('Reading indexes'), 1, 2)
360
self.pb.update('Reading indexes', 1, 2)
373
361
self.repo._check_for_inconsistent_revision_parents()
374
362
self.revisions = self.repo.revisions
375
self.pb.update(gettext('Reading indexes'), 2, 2)
363
self.pb.update('Reading indexes', 2, 2)
377
365
def _gc_inventory(self):
378
366
"""Remove inventories that are not referenced from the revision store."""
379
self.pb.update(gettext('Checking unused inventories'), 0, 1)
367
self.pb.update('Checking unused inventories', 0, 1)
380
368
self._check_garbage_inventories()
381
self.pb.update(gettext('Checking unused inventories'), 1, 3)
369
self.pb.update('Checking unused inventories', 1, 3)
382
370
if not self.garbage_inventories:
383
ui.ui_factory.note(gettext('Inventory ok.'))
371
ui.ui_factory.note('Inventory ok.')
385
self.pb.update(gettext('Backing up inventory'), 0, 0)
373
self.pb.update('Backing up inventory', 0, 0)
386
374
self.repo._backup_inventory()
387
ui.ui_factory.note(gettext('Backup Inventory created'))
375
ui.ui_factory.note('Backup Inventory created')
388
376
# asking for '' should never return a non-empty weave
389
377
new_inventories = self.repo._temp_inventories()
390
378
# we have topological order of revisions and non ghost parents ready.
401
389
# the revisionds list
402
390
if not(set(new_inventories.keys()) == set(revision_keys)):
403
391
raise AssertionError()
404
self.pb.update(gettext('Writing weave'))
392
self.pb.update('Writing weave')
405
393
self.repo._activate_new_inventory()
406
394
self.inventory = None
407
ui.ui_factory.note(gettext('Inventory regenerated.'))
395
ui.ui_factory.note('Inventory regenerated.')
409
397
def _fix_text_parents(self):
410
398
"""Fix bad versionedfile parent entries.
442
430
versions_list.append(text_key[1])
443
431
# Do the reconcile of individual weaves.
444
432
for num, file_id in enumerate(per_id_bad_parents):
445
self.pb.update(gettext('Fixing text parents'), num,
433
self.pb.update('Fixing text parents', num,
446
434
len(per_id_bad_parents))
447
435
versions_with_bad_parents = per_id_bad_parents[file_id]
448
436
id_unused_versions = set(key[-1] for key in unused_versions
508
496
# - unlock the names list
509
497
# https://bugs.launchpad.net/bzr/+bug/154173
511
def __init__(self, repo, other=None, thorough=False,
512
canonicalize_chks=False):
513
super(PackReconciler, self).__init__(repo, other=other,
515
self.canonicalize_chks = canonicalize_chks
517
499
def _reconcile_steps(self):
518
500
"""Perform the steps to reconcile this repository."""
519
501
if not self.thorough:
527
509
total_inventories = len(list(
528
510
collection.inventory_index.combined_index.iter_all_entries()))
529
511
if len(all_revisions):
530
if self.canonicalize_chks:
531
reconcile_meth = self.repo._canonicalize_chks_pack
533
reconcile_meth = self.repo._reconcile_pack
534
new_pack = reconcile_meth(collection, packs, ".reconcile",
535
all_revisions, self.pb)
512
new_pack = self.repo._reconcile_pack(collection, packs,
513
".reconcile", all_revisions, self.pb)
536
514
if new_pack is not None:
537
515
self._discard_and_save(packs)