33
revision as _mod_revision,
36
from .trace import mutter
37
from .tsort import topo_sort
38
from .bzr.versionedfile import AdapterFactory, FulltextContentFactory
39
from .i18n import gettext
42
def reconcile(dir, canonicalize_chks=False):
34
from bzrlib.trace import mutter
35
from bzrlib.tsort import topo_sort
36
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
39
def reconcile(dir, other=None):
43
40
"""Reconcile the data in dir.
45
42
Currently this is limited to a inventory 'reweave'.
49
46
Directly using Reconciler is recommended for library users that
50
47
desire fine grained control or analysis of the found issues.
52
:param canonicalize_chks: Make sure CHKs are in canonical form.
49
:param other: another bzrdir to reconcile against.
54
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
51
reconciler = Reconciler(dir, other=other)
55
52
reconciler.reconcile()
58
55
class Reconciler(object):
59
56
"""Reconcilers are used to reconcile existing data."""
61
def __init__(self, dir, other=None, canonicalize_chks=False):
58
def __init__(self, dir, other=None):
62
59
"""Create a Reconciler."""
64
self.canonicalize_chks = canonicalize_chks
66
62
def reconcile(self):
67
63
"""Perform reconciliation.
69
65
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
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.
79
operation = cleanup.OperationWithCleanups(self._reconcile)
80
self.add_cleanup = operation.add_cleanup
81
operation.run_simple()
74
self.pb = ui.ui_factory.nested_progress_bar()
83
80
def _reconcile(self):
84
81
"""Helper function for performing reconciliation."""
85
self.pb = ui.ui_factory.nested_progress_bar()
86
self.add_cleanup(self.pb.finished)
87
82
self._reconcile_branch()
88
83
self._reconcile_repository()
90
85
def _reconcile_branch(self):
92
self.branch = self.controldir.open_branch()
87
self.branch = self.bzrdir.open_branch()
93
88
except errors.NotBranchError:
94
89
# Nothing to check here
95
90
self.fixed_branch_history = None
97
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
92
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
98
93
branch_reconciler = self.branch.reconcile(thorough=True)
99
94
self.fixed_branch_history = branch_reconciler.fixed_history
101
96
def _reconcile_repository(self):
102
self.repo = self.controldir.find_repository()
103
ui.ui_factory.note(gettext('Reconciling repository %s') %
97
self.repo = self.bzrdir.find_repository()
98
ui.ui_factory.note('Reconciling repository %s' %
104
99
self.repo.user_url)
105
self.pb.update(gettext("Reconciling repository"), 0, 1)
106
if self.canonicalize_chks:
108
self.repo.reconcile_canonicalize_chks
109
except AttributeError:
110
raise errors.BzrError(
111
gettext("%s cannot canonicalize CHKs.") % (self.repo,))
112
repo_reconciler = self.repo.reconcile_canonicalize_chks()
114
repo_reconciler = self.repo.reconcile(thorough=True)
100
self.pb.update("Reconciling repository", 0, 1)
101
repo_reconciler = self.repo.reconcile(thorough=True)
115
102
self.inconsistent_parents = repo_reconciler.inconsistent_parents
116
103
self.garbage_inventories = repo_reconciler.garbage_inventories
117
104
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 "brz check" for more details.'))
106
'Reconcile aborted: revision index has inconsistent parents.')
108
'Run "bzr check" for more details.')
123
ui.ui_factory.note(gettext('Reconciliation complete.'))
110
ui.ui_factory.note('Reconciliation complete.')
126
113
class BranchReconciler(object):
147
134
self._reconcile_revision_history()
149
136
def _reconcile_revision_history(self):
137
repo = self.branch.repository
150
138
last_revno, last_revision_id = self.branch.last_revision_info()
151
139
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,)):
141
for revid in repo.iter_reverse_revision_history(
156
143
real_history.append(revid)
157
144
except errors.RevisionNotPresent:
158
145
pass # Hit a ghost left hand parent
163
150
# set_revision_history, as this will regenerate it again.
164
151
# 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)))
153
ui.ui_factory.note('Fixing last revision info %s => %s' % (
154
last_revno, len(real_history)))
169
155
self.branch.set_last_revision_info(len(real_history),
170
156
last_revision_id)
172
158
self.fixed_history = False
173
ui.ui_factory.note(gettext('revision_history ok.'))
159
ui.ui_factory.note('revision_history ok.')
176
162
class RepoReconciler(object):
201
187
"""Perform reconciliation.
203
189
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.
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.
210
195
operation = cleanup.OperationWithCleanups(self._reconcile)
211
196
self.add_cleanup = operation.add_cleanup
230
215
only data-loss causing issues (!self.thorough) or all issues
231
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
233
220
transaction = self.repo.get_transaction()
234
self.pb.update(gettext('Reading inventory data'))
221
self.pb.update('Reading inventory data')
235
222
self.inventory = self.repo.inventories
236
223
self.revisions = self.repo.revisions
237
224
# the total set of revisions to process
238
self.pending = {key[-1] for key in self.revisions.keys()}
225
self.pending = set([key[-1] for key in self.revisions.keys()])
240
227
# mapping from revision_id to parents
241
228
self._rev_graph = {}
251
238
# (no garbage inventories or we are not doing a thorough check)
252
239
if (not self.inconsistent_parents and
253
240
(not self.garbage_inventories or not self.thorough)):
254
ui.ui_factory.note(gettext('Inventory ok.'))
241
ui.ui_factory.note('Inventory ok.')
256
self.pb.update(gettext('Backing up inventory'), 0, 0)
243
self.pb.update('Backing up inventory', 0, 0)
257
244
self.repo._backup_inventory()
258
ui.ui_factory.note(gettext('Backup inventory created.'))
245
ui.ui_factory.note('Backup inventory created.')
259
246
new_inventories = self.repo._temp_inventories()
261
248
# we have topological order of revisions and non ghost parents ready.
269
256
# if this worked, the set of new_inventories.keys should equal
271
258
if not (set(new_inventories.keys()) ==
272
{(revid,) for revid in self.pending}):
259
set([(revid,) for revid in self.pending])):
273
260
raise AssertionError()
274
self.pb.update(gettext('Writing weave'))
261
self.pb.update('Writing weave')
275
262
self.repo._activate_new_inventory()
276
263
self.inventory = None
277
ui.ui_factory.note(gettext('Inventory regenerated.'))
264
ui.ui_factory.note('Inventory regenerated.')
279
266
def _new_inv_parents(self, revision_key):
280
267
"""Lookup ghost-filtered parents for revision_key."""
368
355
def _load_indexes(self):
369
356
"""Load indexes for the reconciliation."""
370
357
self.transaction = self.repo.get_transaction()
371
self.pb.update(gettext('Reading indexes'), 0, 2)
358
self.pb.update('Reading indexes', 0, 2)
372
359
self.inventory = self.repo.inventories
373
self.pb.update(gettext('Reading indexes'), 1, 2)
360
self.pb.update('Reading indexes', 1, 2)
374
361
self.repo._check_for_inconsistent_revision_parents()
375
362
self.revisions = self.repo.revisions
376
self.pb.update(gettext('Reading indexes'), 2, 2)
363
self.pb.update('Reading indexes', 2, 2)
378
365
def _gc_inventory(self):
379
366
"""Remove inventories that are not referenced from the revision store."""
380
self.pb.update(gettext('Checking unused inventories'), 0, 1)
367
self.pb.update('Checking unused inventories', 0, 1)
381
368
self._check_garbage_inventories()
382
self.pb.update(gettext('Checking unused inventories'), 1, 3)
369
self.pb.update('Checking unused inventories', 1, 3)
383
370
if not self.garbage_inventories:
384
ui.ui_factory.note(gettext('Inventory ok.'))
371
ui.ui_factory.note('Inventory ok.')
386
self.pb.update(gettext('Backing up inventory'), 0, 0)
373
self.pb.update('Backing up inventory', 0, 0)
387
374
self.repo._backup_inventory()
388
ui.ui_factory.note(gettext('Backup Inventory created'))
375
ui.ui_factory.note('Backup Inventory created')
389
376
# asking for '' should never return a non-empty weave
390
377
new_inventories = self.repo._temp_inventories()
391
378
# we have topological order of revisions and non ghost parents ready.
507
494
# - lock the names list
508
495
# - perform a customised pack() that regenerates data as needed
509
496
# - unlock the names list
510
# https://bugs.launchpad.net/bzr/+bug/154173
512
def __init__(self, repo, other=None, thorough=False,
513
canonicalize_chks=False):
514
super(PackReconciler, self).__init__(repo, other=other,
516
self.canonicalize_chks = canonicalize_chks
497
# https://bugs.edge.launchpad.net/bzr/+bug/154173
518
499
def _reconcile_steps(self):
519
500
"""Perform the steps to reconcile this repository."""