14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
from __future__ import absolute_import
17
19
"""Reconcilers are able to fix some potential data errors in a branch."""
29
31
from bzrlib import (
34
revision as _mod_revision,
34
37
from bzrlib.trace import mutter
35
38
from bzrlib.tsort import topo_sort
36
39
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
39
def reconcile(dir, other=None):
40
from bzrlib.i18n import gettext
43
def reconcile(dir, canonicalize_chks=False):
40
44
"""Reconcile the data in dir.
42
46
Currently this is limited to a inventory 'reweave'.
46
50
Directly using Reconciler is recommended for library users that
47
51
desire fine grained control or analysis of the found issues.
49
:param other: another bzrdir to reconcile against.
53
:param canonicalize_chks: Make sure CHKs are in canonical form.
51
reconciler = Reconciler(dir, other=other)
55
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
52
56
reconciler.reconcile()
55
59
class Reconciler(object):
56
60
"""Reconcilers are used to reconcile existing data."""
58
def __init__(self, dir, other=None):
62
def __init__(self, dir, other=None, canonicalize_chks=False):
59
63
"""Create a Reconciler."""
65
self.canonicalize_chks = canonicalize_chks
62
67
def reconcile(self):
63
68
"""Perform reconciliation.
65
70
After reconciliation the following attributes document found issues:
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.
72
* `inconsistent_parents`: The number of revisions in the repository
73
whose ancestry was being reported incorrectly.
74
* `garbage_inventories`: The number of inventory objects without
75
revisions that were garbage collected.
76
* `fixed_branch_history`: None if there was no branch, False if the
77
branch history was correct, True if the branch history needed to be
74
80
self.pb = ui.ui_factory.nested_progress_bar()
89
95
# Nothing to check here
90
96
self.fixed_branch_history = None
92
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
98
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
93
99
branch_reconciler = self.branch.reconcile(thorough=True)
94
100
self.fixed_branch_history = branch_reconciler.fixed_history
96
102
def _reconcile_repository(self):
97
103
self.repo = self.bzrdir.find_repository()
98
ui.ui_factory.note('Reconciling repository %s' %
104
ui.ui_factory.note(gettext('Reconciling repository %s') %
99
105
self.repo.user_url)
100
self.pb.update("Reconciling repository", 0, 1)
101
repo_reconciler = self.repo.reconcile(thorough=True)
106
self.pb.update(gettext("Reconciling repository"), 0, 1)
107
if self.canonicalize_chks:
109
self.repo.reconcile_canonicalize_chks
110
except AttributeError:
111
raise errors.BzrError(
112
gettext("%s cannot canonicalize CHKs.") % (self.repo,))
113
repo_reconciler = self.repo.reconcile_canonicalize_chks()
115
repo_reconciler = self.repo.reconcile(thorough=True)
102
116
self.inconsistent_parents = repo_reconciler.inconsistent_parents
103
117
self.garbage_inventories = repo_reconciler.garbage_inventories
104
118
if repo_reconciler.aborted:
106
'Reconcile aborted: revision index has inconsistent parents.')
108
'Run "bzr check" for more details.')
119
ui.ui_factory.note(gettext(
120
'Reconcile aborted: revision index has inconsistent parents.'))
121
ui.ui_factory.note(gettext(
122
'Run "bzr check" for more details.'))
110
ui.ui_factory.note('Reconciliation complete.')
124
ui.ui_factory.note(gettext('Reconciliation complete.'))
113
127
class BranchReconciler(object):
134
148
self._reconcile_revision_history()
136
150
def _reconcile_revision_history(self):
137
repo = self.branch.repository
138
151
last_revno, last_revision_id = self.branch.last_revision_info()
139
152
real_history = []
153
graph = self.branch.repository.get_graph()
141
for revid in repo.iter_reverse_revision_history(
155
for revid in graph.iter_lefthand_ancestry(
156
last_revision_id, (_mod_revision.NULL_REVISION,)):
143
157
real_history.append(revid)
144
158
except errors.RevisionNotPresent:
145
159
pass # Hit a ghost left hand parent
150
164
# set_revision_history, as this will regenerate it again.
151
165
# Not really worth a whole BranchReconciler class just for this,
153
ui.ui_factory.note('Fixing last revision info %s => %s' % (
154
last_revno, len(real_history)))
167
ui.ui_factory.note(gettext('Fixing last revision info {0} '\
169
last_revno, len(real_history)))
155
170
self.branch.set_last_revision_info(len(real_history),
156
171
last_revision_id)
158
173
self.fixed_history = False
159
ui.ui_factory.note('revision_history ok.')
174
ui.ui_factory.note(gettext('revision_history ok.'))
162
177
class RepoReconciler(object):
187
202
"""Perform reconciliation.
189
204
After reconciliation the following attributes document found issues:
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.
206
* `inconsistent_parents`: The number of revisions in the repository
207
whose ancestry was being reported incorrectly.
208
* `garbage_inventories`: The number of inventory objects without
209
revisions that were garbage collected.
195
211
operation = cleanup.OperationWithCleanups(self._reconcile)
196
212
self.add_cleanup = operation.add_cleanup
215
231
only data-loss causing issues (!self.thorough) or all issues
216
232
(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
234
transaction = self.repo.get_transaction()
221
self.pb.update('Reading inventory data')
235
self.pb.update(gettext('Reading inventory data'))
222
236
self.inventory = self.repo.inventories
223
237
self.revisions = self.repo.revisions
224
238
# the total set of revisions to process
238
252
# (no garbage inventories or we are not doing a thorough check)
239
253
if (not self.inconsistent_parents and
240
254
(not self.garbage_inventories or not self.thorough)):
241
ui.ui_factory.note('Inventory ok.')
255
ui.ui_factory.note(gettext('Inventory ok.'))
243
self.pb.update('Backing up inventory', 0, 0)
257
self.pb.update(gettext('Backing up inventory'), 0, 0)
244
258
self.repo._backup_inventory()
245
ui.ui_factory.note('Backup inventory created.')
259
ui.ui_factory.note(gettext('Backup inventory created.'))
246
260
new_inventories = self.repo._temp_inventories()
248
262
# we have topological order of revisions and non ghost parents ready.
258
272
if not (set(new_inventories.keys()) ==
259
273
set([(revid,) for revid in self.pending])):
260
274
raise AssertionError()
261
self.pb.update('Writing weave')
275
self.pb.update(gettext('Writing weave'))
262
276
self.repo._activate_new_inventory()
263
277
self.inventory = None
264
ui.ui_factory.note('Inventory regenerated.')
278
ui.ui_factory.note(gettext('Inventory regenerated.'))
266
280
def _new_inv_parents(self, revision_key):
267
281
"""Lookup ghost-filtered parents for revision_key."""
355
369
def _load_indexes(self):
356
370
"""Load indexes for the reconciliation."""
357
371
self.transaction = self.repo.get_transaction()
358
self.pb.update('Reading indexes', 0, 2)
372
self.pb.update(gettext('Reading indexes'), 0, 2)
359
373
self.inventory = self.repo.inventories
360
self.pb.update('Reading indexes', 1, 2)
374
self.pb.update(gettext('Reading indexes'), 1, 2)
361
375
self.repo._check_for_inconsistent_revision_parents()
362
376
self.revisions = self.repo.revisions
363
self.pb.update('Reading indexes', 2, 2)
377
self.pb.update(gettext('Reading indexes'), 2, 2)
365
379
def _gc_inventory(self):
366
380
"""Remove inventories that are not referenced from the revision store."""
367
self.pb.update('Checking unused inventories', 0, 1)
381
self.pb.update(gettext('Checking unused inventories'), 0, 1)
368
382
self._check_garbage_inventories()
369
self.pb.update('Checking unused inventories', 1, 3)
383
self.pb.update(gettext('Checking unused inventories'), 1, 3)
370
384
if not self.garbage_inventories:
371
ui.ui_factory.note('Inventory ok.')
385
ui.ui_factory.note(gettext('Inventory ok.'))
373
self.pb.update('Backing up inventory', 0, 0)
387
self.pb.update(gettext('Backing up inventory'), 0, 0)
374
388
self.repo._backup_inventory()
375
ui.ui_factory.note('Backup Inventory created')
389
ui.ui_factory.note(gettext('Backup Inventory created'))
376
390
# asking for '' should never return a non-empty weave
377
391
new_inventories = self.repo._temp_inventories()
378
392
# we have topological order of revisions and non ghost parents ready.
389
403
# the revisionds list
390
404
if not(set(new_inventories.keys()) == set(revision_keys)):
391
405
raise AssertionError()
392
self.pb.update('Writing weave')
406
self.pb.update(gettext('Writing weave'))
393
407
self.repo._activate_new_inventory()
394
408
self.inventory = None
395
ui.ui_factory.note('Inventory regenerated.')
409
ui.ui_factory.note(gettext('Inventory regenerated.'))
397
411
def _fix_text_parents(self):
398
412
"""Fix bad versionedfile parent entries.
430
444
versions_list.append(text_key[1])
431
445
# Do the reconcile of individual weaves.
432
446
for num, file_id in enumerate(per_id_bad_parents):
433
self.pb.update('Fixing text parents', num,
447
self.pb.update(gettext('Fixing text parents'), num,
434
448
len(per_id_bad_parents))
435
449
versions_with_bad_parents = per_id_bad_parents[file_id]
436
450
id_unused_versions = set(key[-1] for key in unused_versions
494
508
# - lock the names list
495
509
# - perform a customised pack() that regenerates data as needed
496
510
# - unlock the names list
497
# https://bugs.edge.launchpad.net/bzr/+bug/154173
511
# https://bugs.launchpad.net/bzr/+bug/154173
513
def __init__(self, repo, other=None, thorough=False,
514
canonicalize_chks=False):
515
super(PackReconciler, self).__init__(repo, other=other,
517
self.canonicalize_chks = canonicalize_chks
499
519
def _reconcile_steps(self):
500
520
"""Perform the steps to reconcile this repository."""
509
529
total_inventories = len(list(
510
530
collection.inventory_index.combined_index.iter_all_entries()))
511
531
if len(all_revisions):
512
new_pack = self.repo._reconcile_pack(collection, packs,
513
".reconcile", all_revisions, self.pb)
532
if self.canonicalize_chks:
533
reconcile_meth = self.repo._canonicalize_chks_pack
535
reconcile_meth = self.repo._reconcile_pack
536
new_pack = reconcile_meth(collection, packs, ".reconcile",
537
all_revisions, self.pb)
514
538
if new_pack is not None:
515
539
self._discard_and_save(packs)