1
# Copyright (C) 2006-2010 Canonical Ltd
1
# Copyright (C) 2005, 2006 Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
29
29
from bzrlib import (
32
revision as _mod_revision,
35
35
from bzrlib.trace import mutter
36
36
from bzrlib.tsort import topo_sort
37
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
38
from bzrlib.i18n import gettext
41
def reconcile(dir, canonicalize_chks=False):
40
def reconcile(dir, other=None):
42
41
"""Reconcile the data in dir.
44
43
Currently this is limited to a inventory 'reweave'.
48
47
Directly using Reconciler is recommended for library users that
49
48
desire fine grained control or analysis of the found issues.
51
:param canonicalize_chks: Make sure CHKs are in canonical form.
50
:param other: another bzrdir to reconcile against.
53
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
52
reconciler = Reconciler(dir, other=other)
54
53
reconciler.reconcile()
57
56
class Reconciler(object):
58
57
"""Reconcilers are used to reconcile existing data."""
60
def __init__(self, dir, other=None, canonicalize_chks=False):
59
def __init__(self, dir, other=None):
61
60
"""Create a Reconciler."""
63
self.canonicalize_chks = canonicalize_chks
65
63
def reconcile(self):
66
64
"""Perform reconciliation.
68
66
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
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.
78
75
self.pb = ui.ui_factory.nested_progress_bar()
93
90
# Nothing to check here
94
91
self.fixed_branch_history = None
96
ui.ui_factory.note(gettext('Reconciling branch %s') % self.branch.base)
93
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
97
94
branch_reconciler = self.branch.reconcile(thorough=True)
98
95
self.fixed_branch_history = branch_reconciler.fixed_history
100
97
def _reconcile_repository(self):
101
98
self.repo = self.bzrdir.find_repository()
102
ui.ui_factory.note(gettext('Reconciling repository %s') %
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)
99
ui.ui_factory.note('Reconciling repository %s' %
100
self.repo.bzrdir.root_transport.base)
101
self.pb.update("Reconciling repository", 0, 1)
102
repo_reconciler = self.repo.reconcile(thorough=True)
114
103
self.inconsistent_parents = repo_reconciler.inconsistent_parents
115
104
self.garbage_inventories = repo_reconciler.garbage_inventories
116
105
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.'))
107
'Reconcile aborted: revision index has inconsistent parents.')
109
'Run "bzr check" for more details.')
122
ui.ui_factory.note(gettext('Reconciliation complete.'))
111
ui.ui_factory.note('Reconciliation complete.')
125
114
class BranchReconciler(object):
146
135
self._reconcile_revision_history()
148
137
def _reconcile_revision_history(self):
138
repo = self.branch.repository
149
139
last_revno, last_revision_id = self.branch.last_revision_info()
150
140
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,)):
142
for revid in repo.iter_reverse_revision_history(
155
144
real_history.append(revid)
156
145
except errors.RevisionNotPresent:
157
146
pass # Hit a ghost left hand parent
162
151
# set_revision_history, as this will regenerate it again.
163
152
# 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)))
154
ui.ui_factory.note('Fixing last revision info %s => %s' % (
155
last_revno, len(real_history)))
168
156
self.branch.set_last_revision_info(len(real_history),
169
157
last_revision_id)
171
159
self.fixed_history = False
172
ui.ui_factory.note(gettext('revision_history ok.'))
160
ui.ui_factory.note('revision_history ok.')
175
163
class RepoReconciler(object):
200
188
"""Perform reconciliation.
202
190
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.
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.
209
196
operation = cleanup.OperationWithCleanups(self._reconcile)
210
197
self.add_cleanup = operation.add_cleanup
229
216
only data-loss causing issues (!self.thorough) or all issues
230
217
(self.thorough) are treated as requiring the reweave.
219
# local because needing to know about WeaveFile is a wart we want to hide
220
from bzrlib.weave import WeaveFile, Weave
232
221
transaction = self.repo.get_transaction()
233
self.pb.update(gettext('Reading inventory data'))
222
self.pb.update('Reading inventory data')
234
223
self.inventory = self.repo.inventories
235
224
self.revisions = self.repo.revisions
236
225
# the total set of revisions to process
250
239
# (no garbage inventories or we are not doing a thorough check)
251
240
if (not self.inconsistent_parents and
252
241
(not self.garbage_inventories or not self.thorough)):
253
ui.ui_factory.note(gettext('Inventory ok.'))
242
ui.ui_factory.note('Inventory ok.')
255
self.pb.update(gettext('Backing up inventory'), 0, 0)
244
self.pb.update('Backing up inventory', 0, 0)
256
245
self.repo._backup_inventory()
257
ui.ui_factory.note(gettext('Backup inventory created.'))
246
ui.ui_factory.note('Backup inventory created.')
258
247
new_inventories = self.repo._temp_inventories()
260
249
# we have topological order of revisions and non ghost parents ready.
270
259
if not (set(new_inventories.keys()) ==
271
260
set([(revid,) for revid in self.pending])):
272
261
raise AssertionError()
273
self.pb.update(gettext('Writing weave'))
262
self.pb.update('Writing weave')
274
263
self.repo._activate_new_inventory()
275
264
self.inventory = None
276
ui.ui_factory.note(gettext('Inventory regenerated.'))
265
ui.ui_factory.note('Inventory regenerated.')
278
267
def _new_inv_parents(self, revision_key):
279
268
"""Lookup ghost-filtered parents for revision_key."""
367
356
def _load_indexes(self):
368
357
"""Load indexes for the reconciliation."""
369
358
self.transaction = self.repo.get_transaction()
370
self.pb.update(gettext('Reading indexes'), 0, 2)
359
self.pb.update('Reading indexes', 0, 2)
371
360
self.inventory = self.repo.inventories
372
self.pb.update(gettext('Reading indexes'), 1, 2)
361
self.pb.update('Reading indexes', 1, 2)
373
362
self.repo._check_for_inconsistent_revision_parents()
374
363
self.revisions = self.repo.revisions
375
self.pb.update(gettext('Reading indexes'), 2, 2)
364
self.pb.update('Reading indexes', 2, 2)
377
366
def _gc_inventory(self):
378
367
"""Remove inventories that are not referenced from the revision store."""
379
self.pb.update(gettext('Checking unused inventories'), 0, 1)
368
self.pb.update('Checking unused inventories', 0, 1)
380
369
self._check_garbage_inventories()
381
self.pb.update(gettext('Checking unused inventories'), 1, 3)
370
self.pb.update('Checking unused inventories', 1, 3)
382
371
if not self.garbage_inventories:
383
ui.ui_factory.note(gettext('Inventory ok.'))
372
ui.ui_factory.note('Inventory ok.')
385
self.pb.update(gettext('Backing up inventory'), 0, 0)
374
self.pb.update('Backing up inventory', 0, 0)
386
375
self.repo._backup_inventory()
387
ui.ui_factory.note(gettext('Backup Inventory created'))
376
ui.ui_factory.note('Backup Inventory created')
388
377
# asking for '' should never return a non-empty weave
389
378
new_inventories = self.repo._temp_inventories()
390
379
# we have topological order of revisions and non ghost parents ready.
401
390
# the revisionds list
402
391
if not(set(new_inventories.keys()) == set(revision_keys)):
403
392
raise AssertionError()
404
self.pb.update(gettext('Writing weave'))
393
self.pb.update('Writing weave')
405
394
self.repo._activate_new_inventory()
406
395
self.inventory = None
407
ui.ui_factory.note(gettext('Inventory regenerated.'))
396
ui.ui_factory.note('Inventory regenerated.')
409
398
def _fix_text_parents(self):
410
399
"""Fix bad versionedfile parent entries.
442
431
versions_list.append(text_key[1])
443
432
# Do the reconcile of individual weaves.
444
433
for num, file_id in enumerate(per_id_bad_parents):
445
self.pb.update(gettext('Fixing text parents'), num,
434
self.pb.update('Fixing text parents', num,
446
435
len(per_id_bad_parents))
447
436
versions_with_bad_parents = per_id_bad_parents[file_id]
448
437
id_unused_versions = set(key[-1] for key in unused_versions
506
495
# - lock the names list
507
496
# - perform a customised pack() that regenerates data as needed
508
497
# - unlock the names list
509
# 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
498
# https://bugs.edge.launchpad.net/bzr/+bug/154173
517
500
def _reconcile_steps(self):
518
501
"""Perform the steps to reconcile this repository."""
527
510
total_inventories = len(list(
528
511
collection.inventory_index.combined_index.iter_all_entries()))
529
512
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)
513
new_pack = self.repo._reconcile_pack(collection, packs,
514
".reconcile", all_revisions, self.pb)
536
515
if new_pack is not None:
537
516
self._discard_and_save(packs)