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
from bzrlib.trace import mutter
36
from bzrlib.tsort import topo_sort
35
from bzrlib.trace import mutter, note
36
from bzrlib.tsort import TopoSorter
37
37
from bzrlib.versionedfile import AdapterFactory, FulltextContentFactory
40
def reconcile(dir, canonicalize_chks=False):
40
def reconcile(dir, other=None):
41
41
"""Reconcile the data in dir.
43
43
Currently this is limited to a inventory 'reweave'.
47
47
Directly using Reconciler is recommended for library users that
48
48
desire fine grained control or analysis of the found issues.
50
:param canonicalize_chks: Make sure CHKs are in canonical form.
50
:param other: another bzrdir to reconcile against.
52
reconciler = Reconciler(dir, canonicalize_chks=canonicalize_chks)
52
reconciler = Reconciler(dir, other=other)
53
53
reconciler.reconcile()
56
56
class Reconciler(object):
57
57
"""Reconcilers are used to reconcile existing data."""
59
def __init__(self, dir, other=None, canonicalize_chks=False):
59
def __init__(self, dir, other=None):
60
60
"""Create a Reconciler."""
62
self.canonicalize_chks = canonicalize_chks
64
63
def reconcile(self):
65
64
"""Perform reconciliation.
67
66
After reconciliation the following attributes document found issues:
69
* `inconsistent_parents`: The number of revisions in the repository
70
whose ancestry was being reported incorrectly.
71
* `garbage_inventories`: The number of inventory objects without
72
revisions that were garbage collected.
73
* `fixed_branch_history`: None if there was no branch, False if the
74
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.
77
75
self.pb = ui.ui_factory.nested_progress_bar()
92
90
# Nothing to check here
93
91
self.fixed_branch_history = None
95
ui.ui_factory.note('Reconciling branch %s' % self.branch.base)
93
self.pb.note('Reconciling branch %s',
96
95
branch_reconciler = self.branch.reconcile(thorough=True)
97
96
self.fixed_branch_history = branch_reconciler.fixed_history
99
98
def _reconcile_repository(self):
100
99
self.repo = self.bzrdir.find_repository()
101
ui.ui_factory.note('Reconciling repository %s' %
100
self.pb.note('Reconciling repository %s',
101
self.repo.bzrdir.root_transport.base)
103
102
self.pb.update("Reconciling repository", 0, 1)
104
if self.canonicalize_chks:
106
self.repo.reconcile_canonicalize_chks
107
except AttributeError:
108
raise errors.BzrError(
109
"%s cannot canonicalize CHKs." % (self.repo,))
110
repo_reconciler = self.repo.reconcile_canonicalize_chks()
112
repo_reconciler = self.repo.reconcile(thorough=True)
103
repo_reconciler = self.repo.reconcile(thorough=True)
113
104
self.inconsistent_parents = repo_reconciler.inconsistent_parents
114
105
self.garbage_inventories = repo_reconciler.garbage_inventories
115
106
if repo_reconciler.aborted:
117
108
'Reconcile aborted: revision index has inconsistent parents.')
119
110
'Run "bzr check" for more details.')
121
ui.ui_factory.note('Reconciliation complete.')
112
self.pb.note('Reconciliation complete.')
124
115
class BranchReconciler(object):
130
121
self.branch = a_branch
132
123
def reconcile(self):
133
operation = cleanup.OperationWithCleanups(self._reconcile)
134
self.add_cleanup = operation.add_cleanup
135
operation.run_simple()
137
def _reconcile(self):
138
124
self.branch.lock_write()
139
self.add_cleanup(self.branch.unlock)
140
self.pb = ui.ui_factory.nested_progress_bar()
141
self.add_cleanup(self.pb.finished)
142
self._reconcile_steps()
126
self.pb = ui.ui_factory.nested_progress_bar()
128
self._reconcile_steps()
144
134
def _reconcile_steps(self):
145
135
self._reconcile_revision_history()
147
137
def _reconcile_revision_history(self):
138
repo = self.branch.repository
148
139
last_revno, last_revision_id = self.branch.last_revision_info()
149
140
real_history = []
150
graph = self.branch.repository.get_graph()
152
for revid in graph.iter_lefthand_ancestry(
153
last_revision_id, (_mod_revision.NULL_REVISION,)):
142
for revid in repo.iter_reverse_revision_history(
154
144
real_history.append(revid)
155
145
except errors.RevisionNotPresent:
156
146
pass # Hit a ghost left hand parent
161
151
# set_revision_history, as this will regenerate it again.
162
152
# Not really worth a whole BranchReconciler class just for this,
164
ui.ui_factory.note('Fixing last revision info %s => %s' % (
165
last_revno, len(real_history)))
154
self.pb.note('Fixing last revision info %s => %s',
155
last_revno, len(real_history))
166
156
self.branch.set_last_revision_info(len(real_history),
167
157
last_revision_id)
169
159
self.fixed_history = False
170
ui.ui_factory.note('revision_history ok.')
160
self.pb.note('revision_history ok.')
173
163
class RepoReconciler(object):
198
188
"""Perform reconciliation.
200
190
After reconciliation the following attributes document found issues:
202
* `inconsistent_parents`: The number of revisions in the repository
203
whose ancestry was being reported incorrectly.
204
* `garbage_inventories`: The number of inventory objects without
205
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.
207
operation = cleanup.OperationWithCleanups(self._reconcile)
208
self.add_cleanup = operation.add_cleanup
209
operation.run_simple()
211
def _reconcile(self):
212
196
self.repo.lock_write()
213
self.add_cleanup(self.repo.unlock)
214
self.pb = ui.ui_factory.nested_progress_bar()
215
self.add_cleanup(self.pb.finished)
216
self._reconcile_steps()
198
self.pb = ui.ui_factory.nested_progress_bar()
200
self._reconcile_steps()
218
206
def _reconcile_steps(self):
219
207
"""Perform the steps to reconcile this repository."""
227
215
only data-loss causing issues (!self.thorough) or all issues
228
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
230
220
transaction = self.repo.get_transaction()
231
221
self.pb.update('Reading inventory data')
232
222
self.inventory = self.repo.inventories
248
238
# (no garbage inventories or we are not doing a thorough check)
249
239
if (not self.inconsistent_parents and
250
240
(not self.garbage_inventories or not self.thorough)):
251
ui.ui_factory.note('Inventory ok.')
241
self.pb.note('Inventory ok.')
253
243
self.pb.update('Backing up inventory', 0, 0)
254
244
self.repo._backup_inventory()
255
ui.ui_factory.note('Backup inventory created.')
245
self.pb.note('Backup inventory created.')
256
246
new_inventories = self.repo._temp_inventories()
258
248
# we have topological order of revisions and non ghost parents ready.
259
249
self._setup_steps(len(self._rev_graph))
260
revision_keys = [(rev_id,) for rev_id in topo_sort(self._rev_graph)]
250
revision_keys = [(rev_id,) for rev_id in
251
TopoSorter(self._rev_graph.items()).iter_topo_order()]
261
252
stream = self._change_inv_parents(
262
253
self.inventory.get_record_stream(revision_keys, 'unordered', True),
263
254
self._new_inv_parents,
271
262
self.pb.update('Writing weave')
272
263
self.repo._activate_new_inventory()
273
264
self.inventory = None
274
ui.ui_factory.note('Inventory regenerated.')
265
self.pb.note('Inventory regenerated.')
276
267
def _new_inv_parents(self, revision_key):
277
268
"""Lookup ghost-filtered parents for revision_key."""
378
369
self._check_garbage_inventories()
379
370
self.pb.update('Checking unused inventories', 1, 3)
380
371
if not self.garbage_inventories:
381
ui.ui_factory.note('Inventory ok.')
372
self.pb.note('Inventory ok.')
383
374
self.pb.update('Backing up inventory', 0, 0)
384
375
self.repo._backup_inventory()
385
ui.ui_factory.note('Backup Inventory created')
376
self.pb.note('Backup Inventory created')
386
377
# asking for '' should never return a non-empty weave
387
378
new_inventories = self.repo._temp_inventories()
388
379
# we have topological order of revisions and non ghost parents ready.
389
380
graph = self.revisions.get_parent_map(self.revisions.keys())
390
revision_keys = topo_sort(graph)
381
revision_keys = list(TopoSorter(graph).iter_topo_order())
391
382
revision_ids = [key[-1] for key in revision_keys]
392
383
self._setup_steps(len(revision_keys))
393
384
stream = self._change_inv_parents(
402
393
self.pb.update('Writing weave')
403
394
self.repo._activate_new_inventory()
404
395
self.inventory = None
405
ui.ui_factory.note('Inventory regenerated.')
396
self.pb.note('Inventory regenerated.')
407
398
def _fix_text_parents(self):
408
399
"""Fix bad versionedfile parent entries.
504
495
# - lock the names list
505
496
# - perform a customised pack() that regenerates data as needed
506
497
# - unlock the names list
507
# https://bugs.launchpad.net/bzr/+bug/154173
509
def __init__(self, repo, other=None, thorough=False,
510
canonicalize_chks=False):
511
super(PackReconciler, self).__init__(repo, other=other,
513
self.canonicalize_chks = canonicalize_chks
498
# https://bugs.edge.launchpad.net/bzr/+bug/154173
515
500
def _reconcile_steps(self):
516
501
"""Perform the steps to reconcile this repository."""
519
504
collection = self.repo._pack_collection
520
505
collection.ensure_loaded()
521
506
collection.lock_names()
522
self.add_cleanup(collection._unlock_names)
523
packs = collection.all_packs()
524
all_revisions = self.repo.all_revision_ids()
525
total_inventories = len(list(
526
collection.inventory_index.combined_index.iter_all_entries()))
527
if len(all_revisions):
528
if self.canonicalize_chks:
529
reconcile_meth = self.repo._canonicalize_chks_pack
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)
531
reconcile_meth = self.repo._reconcile_pack
532
new_pack = reconcile_meth(collection, packs, ".reconcile",
533
all_revisions, self.pb)
534
if new_pack is not None:
518
# only make a new pack when there is data to copy.
535
519
self._discard_and_save(packs)
537
# only make a new pack when there is data to copy.
538
self._discard_and_save(packs)
539
self.garbage_inventories = total_inventories - len(list(
540
collection.inventory_index.combined_index.iter_all_entries()))
520
self.garbage_inventories = total_inventories - len(list(
521
collection.inventory_index.combined_index.iter_all_entries()))
523
collection._unlock_names()
542
525
def _discard_and_save(self, packs):
543
526
"""Discard some packs from the repository.