29
30
from bzrlib import (
33
revision as _mod_revision,
35
from bzrlib.trace import mutter, note
36
from bzrlib.tsort import TopoSorter
36
from bzrlib.trace import mutter
37
from bzrlib.tsort import topo_sort
37
38
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
40
def reconcile(dir, other=None):
39
from bzrlib.i18n import gettext
42
def reconcile(dir, canonicalize_chks=False):
41
43
"""Reconcile the data in dir.
43
45
Currently this is limited to a inventory 'reweave'.
47
49
Directly using Reconciler is recommended for library users that
48
50
desire fine grained control or analysis of the found issues.
50
:param other: another bzrdir to reconcile against.
52
:param canonicalize_chks: Make sure CHKs are in canonical form.
52
reconciler = Reconciler(dir, other=other)
54
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
53
55
reconciler.reconcile()
56
58
class Reconciler(object):
57
59
"""Reconcilers are used to reconcile existing data."""
59
def __init__(self, dir, other=None):
61
def __init__(self, dir, other=None, canonicalize_chks=False):
60
62
"""Create a Reconciler."""
64
self.canonicalize_chks = canonicalize_chks
63
66
def reconcile(self):
64
67
"""Perform reconciliation.
66
69
After reconciliation the following attributes document found issues:
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.
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
75
79
self.pb = ui.ui_factory.nested_progress_bar()
90
94
# Nothing to check here
91
95
self.fixed_branch_history = None
93
self.pb.note('Reconciling branch %s',
97
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
95
98
branch_reconciler = self.branch.reconcile(thorough=True)
96
99
self.fixed_branch_history = branch_reconciler.fixed_history
98
101
def _reconcile_repository(self):
99
102
self.repo = self.bzrdir.find_repository()
100
self.pb.note('Reconciling repository %s',
101
self.repo.bzrdir.root_transport.base)
102
self.pb.update("Reconciling repository", 0, 1)
103
repo_reconciler = self.repo.reconcile(thorough=True)
103
ui.ui_factory.note(gettext('Reconciling repository %s') %
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)
104
115
self.inconsistent_parents = repo_reconciler.inconsistent_parents
105
116
self.garbage_inventories = repo_reconciler.garbage_inventories
106
117
if repo_reconciler.aborted:
108
'Reconcile aborted: revision index has inconsistent parents.')
110
'Run "bzr check" for more details.')
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.'))
112
self.pb.note('Reconciliation complete.')
123
ui.ui_factory.note(gettext('Reconciliation complete.'))
115
126
class BranchReconciler(object):
121
132
self.branch = a_branch
123
134
def reconcile(self):
135
operation = cleanup.OperationWithCleanups(self._reconcile)
136
self.add_cleanup = operation.add_cleanup
137
operation.run_simple()
139
def _reconcile(self):
124
140
self.branch.lock_write()
126
self.pb = ui.ui_factory.nested_progress_bar()
128
self._reconcile_steps()
141
self.add_cleanup(self.branch.unlock)
142
self.pb = ui.ui_factory.nested_progress_bar()
143
self.add_cleanup(self.pb.finished)
144
self._reconcile_steps()
134
146
def _reconcile_steps(self):
135
147
self._reconcile_revision_history()
137
149
def _reconcile_revision_history(self):
138
repo = self.branch.repository
139
150
last_revno, last_revision_id = self.branch.last_revision_info()
140
151
real_history = []
152
graph = self.branch.repository.get_graph()
142
for revid in repo.iter_reverse_revision_history(
154
for revid in graph.iter_lefthand_ancestry(
155
last_revision_id, (_mod_revision.NULL_REVISION,)):
144
156
real_history.append(revid)
145
157
except errors.RevisionNotPresent:
146
158
pass # Hit a ghost left hand parent
151
163
# set_revision_history, as this will regenerate it again.
152
164
# Not really worth a whole BranchReconciler class just for this,
154
self.pb.note('Fixing last revision info %s => %s',
155
last_revno, len(real_history))
166
ui.ui_factory.note(gettext('Fixing last revision info {0} '\
168
last_revno, len(real_history)))
156
169
self.branch.set_last_revision_info(len(real_history),
157
170
last_revision_id)
159
172
self.fixed_history = False
160
self.pb.note('revision_history ok.')
173
ui.ui_factory.note(gettext('revision_history ok.'))
163
176
class RepoReconciler(object):
188
201
"""Perform reconciliation.
190
203
After reconciliation the following attributes document found issues:
191
inconsistent_parents: The number of revisions in the repository whose
192
ancestry was being reported incorrectly.
193
garbage_inventories: The number of inventory objects without revisions
194
that were garbage collected.
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.
210
operation = cleanup.OperationWithCleanups(self._reconcile)
211
self.add_cleanup = operation.add_cleanup
212
operation.run_simple()
214
def _reconcile(self):
196
215
self.repo.lock_write()
198
self.pb = ui.ui_factory.nested_progress_bar()
200
self._reconcile_steps()
216
self.add_cleanup(self.repo.unlock)
217
self.pb = ui.ui_factory.nested_progress_bar()
218
self.add_cleanup(self.pb.finished)
219
self._reconcile_steps()
206
221
def _reconcile_steps(self):
207
222
"""Perform the steps to reconcile this repository."""
215
230
only data-loss causing issues (!self.thorough) or all issues
216
231
(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
220
233
transaction = self.repo.get_transaction()
221
self.pb.update('Reading inventory data')
234
self.pb.update(gettext('Reading inventory data'))
222
235
self.inventory = self.repo.inventories
223
236
self.revisions = self.repo.revisions
224
237
# the total set of revisions to process
238
251
# (no garbage inventories or we are not doing a thorough check)
239
252
if (not self.inconsistent_parents and
240
253
(not self.garbage_inventories or not self.thorough)):
241
self.pb.note('Inventory ok.')
254
ui.ui_factory.note(gettext('Inventory ok.'))
243
self.pb.update('Backing up inventory', 0, 0)
256
self.pb.update(gettext('Backing up inventory'), 0, 0)
244
257
self.repo._backup_inventory()
245
self.pb.note('Backup inventory created.')
258
ui.ui_factory.note(gettext('Backup inventory created.'))
246
259
new_inventories = self.repo._temp_inventories()
248
261
# we have topological order of revisions and non ghost parents ready.
249
262
self._setup_steps(len(self._rev_graph))
250
revision_keys = [(rev_id,) for rev_id in
251
TopoSorter(self._rev_graph.items()).iter_topo_order()]
263
revision_keys = [(rev_id,) for rev_id in topo_sort(self._rev_graph)]
252
264
stream = self._change_inv_parents(
253
265
self.inventory.get_record_stream(revision_keys, 'unordered', True),
254
266
self._new_inv_parents,
259
271
if not (set(new_inventories.keys()) ==
260
272
set([(revid,) for revid in self.pending])):
261
273
raise AssertionError()
262
self.pb.update('Writing weave')
274
self.pb.update(gettext('Writing weave'))
263
275
self.repo._activate_new_inventory()
264
276
self.inventory = None
265
self.pb.note('Inventory regenerated.')
277
ui.ui_factory.note(gettext('Inventory regenerated.'))
267
279
def _new_inv_parents(self, revision_key):
268
280
"""Lookup ghost-filtered parents for revision_key."""
356
368
def _load_indexes(self):
357
369
"""Load indexes for the reconciliation."""
358
370
self.transaction = self.repo.get_transaction()
359
self.pb.update('Reading indexes', 0, 2)
371
self.pb.update(gettext('Reading indexes'), 0, 2)
360
372
self.inventory = self.repo.inventories
361
self.pb.update('Reading indexes', 1, 2)
373
self.pb.update(gettext('Reading indexes'), 1, 2)
362
374
self.repo._check_for_inconsistent_revision_parents()
363
375
self.revisions = self.repo.revisions
364
self.pb.update('Reading indexes', 2, 2)
376
self.pb.update(gettext('Reading indexes'), 2, 2)
366
378
def _gc_inventory(self):
367
379
"""Remove inventories that are not referenced from the revision store."""
368
self.pb.update('Checking unused inventories', 0, 1)
380
self.pb.update(gettext('Checking unused inventories'), 0, 1)
369
381
self._check_garbage_inventories()
370
self.pb.update('Checking unused inventories', 1, 3)
382
self.pb.update(gettext('Checking unused inventories'), 1, 3)
371
383
if not self.garbage_inventories:
372
self.pb.note('Inventory ok.')
384
ui.ui_factory.note(gettext('Inventory ok.'))
374
self.pb.update('Backing up inventory', 0, 0)
386
self.pb.update(gettext('Backing up inventory'), 0, 0)
375
387
self.repo._backup_inventory()
376
self.pb.note('Backup Inventory created')
388
ui.ui_factory.note(gettext('Backup Inventory created'))
377
389
# asking for '' should never return a non-empty weave
378
390
new_inventories = self.repo._temp_inventories()
379
391
# we have topological order of revisions and non ghost parents ready.
380
392
graph = self.revisions.get_parent_map(self.revisions.keys())
381
revision_keys = list(TopoSorter(graph).iter_topo_order())
393
revision_keys = topo_sort(graph)
382
394
revision_ids = [key[-1] for key in revision_keys]
383
395
self._setup_steps(len(revision_keys))
384
396
stream = self._change_inv_parents(
390
402
# the revisionds list
391
403
if not(set(new_inventories.keys()) == set(revision_keys)):
392
404
raise AssertionError()
393
self.pb.update('Writing weave')
405
self.pb.update(gettext('Writing weave'))
394
406
self.repo._activate_new_inventory()
395
407
self.inventory = None
396
self.pb.note('Inventory regenerated.')
408
ui.ui_factory.note(gettext('Inventory regenerated.'))
398
410
def _fix_text_parents(self):
399
411
"""Fix bad versionedfile parent entries.
431
443
versions_list.append(text_key[1])
432
444
# Do the reconcile of individual weaves.
433
445
for num, file_id in enumerate(per_id_bad_parents):
434
self.pb.update('Fixing text parents', num,
446
self.pb.update(gettext('Fixing text parents'), num,
435
447
len(per_id_bad_parents))
436
448
versions_with_bad_parents = per_id_bad_parents[file_id]
437
449
id_unused_versions = set(key[-1] for key in unused_versions
495
507
# - lock the names list
496
508
# - perform a customised pack() that regenerates data as needed
497
509
# - unlock the names list
498
# https://bugs.edge.launchpad.net/bzr/+bug/154173
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
500
518
def _reconcile_steps(self):
501
519
"""Perform the steps to reconcile this repository."""
504
522
collection = self.repo._pack_collection
505
523
collection.ensure_loaded()
506
524
collection.lock_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:
516
self._discard_and_save(packs)
525
self.add_cleanup(collection._unlock_names)
526
packs = collection.all_packs()
527
all_revisions = self.repo.all_revision_ids()
528
total_inventories = len(list(
529
collection.inventory_index.combined_index.iter_all_entries()))
530
if len(all_revisions):
531
if self.canonicalize_chks:
532
reconcile_meth = self.repo._canonicalize_chks_pack
518
# only make a new pack when there is data to copy.
534
reconcile_meth = self.repo._reconcile_pack
535
new_pack = reconcile_meth(collection, packs, ".reconcile",
536
all_revisions, self.pb)
537
if new_pack is not None:
519
538
self._discard_and_save(packs)
520
self.garbage_inventories = total_inventories - len(list(
521
collection.inventory_index.combined_index.iter_all_entries()))
523
collection._unlock_names()
540
# only make a new pack when there is data to copy.
541
self._discard_and_save(packs)
542
self.garbage_inventories = total_inventories - len(list(
543
collection.inventory_index.combined_index.iter_all_entries()))
525
545
def _discard_and_save(self, packs):
526
546
"""Discard some packs from the repository.